mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
539 stars 123 forks source link

`@mswjs/interceptors@0.17.10` breaks `URL` as fetch argument #584

Closed gerbyzation closed 3 weeks ago

gerbyzation commented 3 months ago

fetch supports being called with an instance of URL:

// working.mjs
const url = "https://www.google.com/";

const response = await fetch(new URL(url));
console.log(response.status);
❯ node working.mjs
200

However with msw@1.3.3 this breaks:

// not-working.mjs
import { setupServer } from "msw/node";

const server = setupServer();
server.listen();

const url = "https://www.google.com/";

const response = await fetch(new URL(url));
console.log(response.status);

server.close();
❯ node not-working.mjs
node:internal/errors:496
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:405:5)
    at new URL (node:internal/url:778:13)
    at FetchInterceptor.<anonymous> (/Users/gerbenneven/project/msw-fetch-URL-invalid/node_modules/.pnpm/@mswjs+interceptors@0.17.10/node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:116:38)
    at step (/Users/gerbenneven/projects/msw-fetch-URL-invalid/node_modules/.pnpm/@mswjs+interceptors@0.17.10/node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:59:23)
    at Object.next (/Users/gerbenneven/projects/msw-fetch-URL-invalid/node_modules/.pnpm/@mswjs+interceptors@0.17.10/node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:40:53)
    at fulfilled (/Users/gerbenneven/projects/msw-fetch-URL-invalid/node_modules/.pnpm/@mswjs+interceptors@0.17.10/node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:31:58) {
  input: 'undefined',
  code: 'ERR_INVALID_URL'
}

Node.js v20.6.1

Repro URL: https://github.com/gerbyzation/msw-fetch-url-invalid

From what I can tell this is the offending line: https://github.com/mswjs/interceptors/blob/0c2f79db35b13a2db2dd9c5614a23b808d623220/src/interceptors/fetch/index.ts#L43

When input is a URL instance it's not a string, so uses input.url which evaluates to undefined.

Note that this is from the source of 0.17.8 as 0.17.9 & 0.17.10 have no git tags associated, the code is practically the same.

kettanaito commented 3 weeks ago

Please try this on the latest version and let me know. A lot of things has changed since then. Thanks.