Skip to content

Commit 86ec3f5

Browse files
committed
fix: fetch in local environment
1 parent d10e467 commit 86ec3f5

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

lib/env/polyfills/fetch.polyfills.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import { getUrlFromResource, getVulcanBuildId } from '#utils';
1515
* @returns {Promise<Response>} A Promise that resolves to the Response object.
1616
*/
1717
async function fetchPolyfill(context, resource, options) {
18-
const { Headers, Response } = context;
18+
const { Headers, Response, RESERVED_FETCH } = context;
1919

20-
const urlObj = getUrlFromResource(resource);
20+
const urlObj = getUrlFromResource(context, resource);
2121

22-
// console.log(filePath);
2322
if (urlObj.href.startsWith('file://')) {
2423
const versionId = getVulcanBuildId();
2524
const file = urlObj.pathname.replace(`/${versionId}`, '');
@@ -35,7 +34,7 @@ async function fetchPolyfill(context, resource, options) {
3534
return response;
3635
}
3736

38-
return fetch(resource, options);
37+
return RESERVED_FETCH(resource, options);
3938
}
4039

4140
export default fetchPolyfill;

lib/env/runtime.env.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ import { fetchPolyfill, FetchEventPolyfill } from './polyfills/index.js';
3636
*/
3737
function runtime(code) {
3838
const extend = (context) => {
39-
context.fetch = (resource, options) =>
39+
context.RESERVED_FETCH = context.fetch.bind(context);
40+
context.fetch = async (resource, options) =>
4041
fetchPolyfill(context, resource, options);
42+
4143
context.FetchEvent = FetchEventPolyfill;
4244
context.FirewallEvent = {}; // TODO: Firewall Event
4345
/*

lib/utils/getUrlFromResource/getUrlFromResource.utils.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ import { Utils } from '#namespaces';
33
/**
44
* @function
55
* @memberof Utils
6+
* @param {globalThis} context - VMContext
67
* @description Creates a URL object based on a received resource
78
* @param {URL|Request|string} resource received resource.
89
* @returns {URL} Generated URL object.
910
*/
10-
function getUrlFromResource(resource) {
11+
function getUrlFromResource(context, resource) {
1112
if (typeof resource === 'string') return new URL(resource);
1213

13-
if (resource instanceof Request) return new URL(resource.url);
14+
if (resource instanceof context.Request) return new URL(resource.url);
1415

15-
if (resource instanceof URL) return resource;
16+
if (resource instanceof context.URL) return resource;
1617

1718
throw new Error(
1819
"Invalid resource input. 'resource' must be 'URL', 'Request' or 'string'.",

0 commit comments

Comments
 (0)