@@ -4,6 +4,9 @@ const chalk = require('chalk');
4
4
const vm = require ( 'vm' ) ;
5
5
const sourceMapSupport = require ( 'source-map-support' ) ;
6
6
7
+ const httpRegex = / ^ h t t p s ? : \/ \/ / ;
8
+ const protocolRelativeRegex = / ^ \/ \/ / ;
9
+
7
10
module . exports = class Sandbox {
8
11
constructor ( globals ) {
9
12
this . globals = globals ;
@@ -56,27 +59,83 @@ module.exports = class Sandbox {
56
59
}
57
60
58
61
buildFetch ( ) {
62
+ let globals ;
63
+
59
64
if ( globalThis . fetch ) {
60
- return {
65
+ globals = {
61
66
fetch : globalThis . fetch ,
62
67
Request : globalThis . Request ,
63
68
Response : globalThis . Response ,
64
69
Headers : globalThis . Headers ,
65
70
AbortController : globalThis . AbortController ,
66
71
} ;
72
+ } else {
73
+ let nodeFetch = require ( 'node-fetch' ) ;
74
+ let {
75
+ AbortController,
76
+ abortableFetch,
77
+ } = require ( 'abortcontroller-polyfill/dist/cjs-ponyfill' ) ;
78
+ let { fetch, Request } = abortableFetch ( {
79
+ fetch : nodeFetch ,
80
+ Request : nodeFetch . Request ,
81
+ } ) ;
82
+
83
+ globals = {
84
+ fetch,
85
+ Request,
86
+ Response : nodeFetch . Response ,
87
+ Headers : nodeFetch . Headers ,
88
+ AbortController,
89
+ } ;
67
90
}
68
91
69
- let nodeFetch = require ( 'node-fetch' ) ;
70
- let { AbortController, abortableFetch } = require ( 'abortcontroller-polyfill/dist/cjs-ponyfill' ) ;
71
- let { fetch, Request } = abortableFetch ( { fetch : nodeFetch , Request : nodeFetch . Request } ) ;
92
+ let originalFetch = globals . fetch ;
72
93
73
- return {
74
- fetch,
75
- Request,
76
- Response : nodeFetch . Response ,
77
- Headers : nodeFetch . Headers ,
78
- AbortController,
94
+ globals . fetch = function __fastbootFetch ( input , init ) {
95
+ if ( input && input . href ) {
96
+ input . url = globals . fetch . __fastbootBuildAbsoluteURL ( input . href ) ;
97
+ } else if ( typeof input === 'string' ) {
98
+ input = globals . fetch . __fastbootBuildAbsoluteURL ( input ) ;
99
+ }
100
+ return originalFetch ( input , init ) ;
101
+ } ;
102
+
103
+ globals . fetch . __fastbootBuildAbsoluteURL = function __fastbootBuildAbsoluteURL ( url ) {
104
+ if ( protocolRelativeRegex . test ( url ) ) {
105
+ let [ host ] = globals . fetch . __fastbootParseRequest ( fetch . __fastbootRequest ) ;
106
+ url = `${ host } ${ url } ` ;
107
+ } else if ( ! httpRegex . test ( url ) ) {
108
+ let [ host , protocol ] = globals . fetch . __fastbootParseRequest ( fetch . __fastbootRequest ) ;
109
+ url = `${ protocol } //${ host } ${ url } ` ;
110
+ }
111
+ return url ;
79
112
} ;
113
+
114
+ globals . fetch . __fastbootParseRequest = function __fastbootParseRequest ( request ) {
115
+ if ( ! request ) {
116
+ throw new Error (
117
+ "Trying to fetch with relative url but ember-fetch hasn't finished loading FastBootInfo, see details at https://github.com/ember-cli/ember-fetch#relative-url"
118
+ ) ;
119
+ }
120
+ // Old Prember version is not sending protocol
121
+ const protocol = request . protocol === 'undefined:' ? 'http:' : request . protocol ;
122
+ return [ request . host , protocol ] ;
123
+ } ;
124
+
125
+ let OriginalRequest = globals . Request ;
126
+ globals . Request = class __FastBootRequest extends OriginalRequest {
127
+ constructor ( input , init ) {
128
+ if ( typeof input === 'string' ) {
129
+ input = globals . fetch . __fastbootBuildAbsoluteURL ( input ) ;
130
+ } else if ( input && input . href ) {
131
+ // WHATWG URL or Node.js Url Object
132
+ input = globals . fetch . __fastbootBuildAbsoluteURL ( input . href ) ;
133
+ }
134
+ super ( input , init ) ;
135
+ }
136
+ } ;
137
+
138
+ return globals ;
80
139
}
81
140
82
141
runScript ( script ) {
0 commit comments