@@ -51,25 +51,68 @@ var __exec;
51
51
// System clobbering protection (mostly for Traceur)
52
52
var curSystem ;
53
53
var callCounter = 0 ;
54
+ function preExec ( loader , load ) {
55
+ if ( callCounter ++ == 0 )
56
+ curSystem = __global . System ;
57
+ __global . System = __global . SystemJS = loader ;
58
+ }
59
+ function postExec ( ) {
60
+ if ( -- callCounter == 0 )
61
+ __global . System = __global . SystemJS = curSystem ;
62
+ curLoad = undefined ;
63
+ }
64
+
54
65
__exec = function ( load ) {
55
- if ( load . metadata . integrity )
56
- throw new TypeError ( 'Subresource integrity checking is only supported on modules with scriptLoad:true metadata or through the SystemCSP build.' ) ;
66
+ if ( ( load . metadata . integrity || load . metadata . nonce ) && supportsScriptExec )
67
+ return scriptExec . call ( this , load ) ;
57
68
try {
58
- if ( callCounter ++ == 0 )
59
- curSystem = __global . System ;
60
- __global . System = __global . SystemJS = this ;
69
+ preExec ( this , load ) ;
61
70
curLoad = load ;
62
71
( 0 , eval ) ( getSource ( load ) ) ;
63
- if ( -- callCounter == 0 )
64
- __global . System = __global . SystemJS = curSystem ;
65
- curLoad = undefined ;
72
+ postExec ( ) ;
66
73
}
67
74
catch ( e ) {
68
- if ( -- callCounter == 0 )
69
- __global . System = __global . SystemJS = curSystem ;
70
- curLoad = undefined ;
75
+ postExec ( ) ;
71
76
throw addToError ( e , 'Evaluating ' + load . address ) ;
72
77
}
73
78
} ;
74
79
80
+ var supportsScriptExec = false ;
81
+ if ( isBrowser && typeof document != 'undefined' && document . getElementsByTagName ) {
82
+ var scripts = document . getElementsByTagName ( 'script' ) ;
83
+ $__curScript = scripts [ scripts . length - 1 ] ;
84
+
85
+ if ( ! ( window . chrome && window . chrome . extension || navigator . userAgent . match ( / ^ N o d e \. j s / ) ) )
86
+ supportsScriptExec = true ;
87
+ }
88
+
89
+ // script execution via injecting a script tag into the page
90
+ // this allows CSP integrity and nonce to be set for CSP environments
91
+ var head ;
92
+ function scriptExec ( load ) {
93
+ if ( ! head )
94
+ head = document . head || document . body || document . documentElement ;
95
+
96
+ var script = document . createElement ( 'script' ) ;
97
+ script . text = getSource ( load , false ) ;
98
+ var onerror = window . onerror ;
99
+ var e ;
100
+ window . onerror = function ( _e ) {
101
+ e = addToError ( _e , 'Evaluating ' + load . address ) ;
102
+ }
103
+ preExec ( this , load ) ;
104
+
105
+ if ( load . metadata . integrity )
106
+ script . setAttribute ( 'integrity' , load . metadata . integrity ) ;
107
+ if ( load . metadata . nonce )
108
+ script . setAttribute ( 'nonce' , load . metadata . nonce ) ;
109
+
110
+ head . appendChild ( script ) ;
111
+ head . removeChild ( script ) ;
112
+ postExec ( ) ;
113
+ window . onerror = onerror ;
114
+ if ( e )
115
+ throw e ;
116
+ }
117
+
75
118
} ) ( ) ;
0 commit comments