@@ -4,6 +4,9 @@ const chalk = require('chalk');
44const vm = require ( 'vm' ) ;
55const sourceMapSupport = require ( 'source-map-support' ) ;
66
7+ const httpRegex = / ^ h t t p s ? : \/ \/ / ;
8+ const protocolRelativeRegex = / ^ \/ \/ / ;
9+
710module . exports = class Sandbox {
811 constructor ( globals ) {
912 this . globals = globals ;
@@ -56,27 +59,82 @@ module.exports = class Sandbox {
5659 }
5760
5861 buildFetch ( ) {
62+ let globals ;
63+
5964 if ( globalThis . fetch ) {
60- return {
65+ globals = {
6166 fetch : globalThis . fetch ,
6267 Request : globalThis . Request ,
6368 Response : globalThis . Response ,
6469 Headers : globalThis . Headers ,
6570 AbortController : globalThis . AbortController ,
6671 } ;
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+ } ;
6790 }
6891
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 ;
93+ globals . fetch = function __fastbootFetch ( input , init ) {
94+ if ( input && input . href ) {
95+ input . url = globals . fetch . __fastbootBuildAbsoluteURL ( input . href ) ;
96+ } else if ( typeof input === 'string' ) {
97+ input = globals . fetch . __fastbootBuildAbsoluteURL ( input ) ;
98+ }
99+ return originalFetch ( input , init ) ;
100+ } ;
72101
73- return {
74- fetch,
75- Request,
76- Response : nodeFetch . Response ,
77- Headers : nodeFetch . Headers ,
78- AbortController,
102+ globals . fetch . __fastbootBuildAbsoluteURL = function __fastbootBuildAbsoluteURL ( url ) {
103+ if ( protocolRelativeRegex . test ( url ) ) {
104+ let [ host ] = globals . fetch . __fastbootParseRequest ( url , fetch . __fastbootRequest ) ;
105+ url = `${ host } ${ url } ` ;
106+ } else if ( ! httpRegex . test ( url ) ) {
107+ let [ host , protocol ] = globals . fetch . __fastbootParseRequest ( url , fetch . __fastbootRequest ) ;
108+ url = `${ protocol } //${ host } ${ url } ` ;
109+ }
110+ return url ;
79111 } ;
112+
113+ globals . fetch . __fastbootParseRequest = function __fastbootParseRequest ( url , request ) {
114+ if ( ! request ) {
115+ throw new Error (
116+ `Using fetch with relative URL ${ url } , but application instance has not been initialized yet.`
117+ ) ;
118+ }
119+ // Old Prember version is not sending protocol
120+ const protocol = request . protocol === 'undefined:' ? 'http:' : request . protocol ;
121+ return [ request . host , protocol ] ;
122+ } ;
123+
124+ let OriginalRequest = globals . Request ;
125+ globals . Request = class __FastBootRequest extends OriginalRequest {
126+ constructor ( input , init ) {
127+ if ( typeof input === 'string' ) {
128+ input = globals . fetch . __fastbootBuildAbsoluteURL ( input ) ;
129+ } else if ( input && input . href ) {
130+ // WHATWG URL or Node.js Url Object
131+ input = globals . fetch . __fastbootBuildAbsoluteURL ( input . href ) ;
132+ }
133+ super ( input , init ) ;
134+ }
135+ } ;
136+
137+ return globals ;
80138 }
81139
82140 runScript ( script ) {
0 commit comments