mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-15 04:50:40 +08:00
23 lines
847 B
JavaScript
23 lines
847 B
JavaScript
// Patches globalThis.fetch at module init — the interception/polyfill
|
|
// pattern (undici interceptors, msw-style mocks, dotenv-ordered setup).
|
|
// USER-code fetch must read the LIVE global at call time, so calls made
|
|
// after this module evaluates go through the wrapper: it logs each URL and
|
|
// rewrites the fake origin to the harness server (argv[2]).
|
|
const FAKE = "https://gateway.invalid";
|
|
const base = process.argv[2];
|
|
const orig = globalThis.fetch;
|
|
globalThis.fetch = function fetch(input, init) {
|
|
let url =
|
|
typeof input === "string"
|
|
? input
|
|
: input && typeof input.url === "string"
|
|
? input.url
|
|
: String(input);
|
|
console.log(`tap ${url}`);
|
|
if (url.startsWith(FAKE)) url = base + url.slice(FAKE.length);
|
|
return orig(url, init);
|
|
};
|
|
export function tappedFetchName() {
|
|
return globalThis.fetch.name;
|
|
}
|