Files
2026-07-22 18:08:04 -05:00

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;
}