Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in node-fetch@3.1.0 + Electron renderer process. "TypeError: The "listener" argument must be of type function" #1376

Closed
babatakao opened this issue Nov 11, 2021 · 2 comments · Fixed by #1378
Labels

Comments

@babatakao
Copy link

Reproduction

Steps to reproduce the behavior:

  1. Use node-fetch@3.1 in Electron renderer process.

I've created a sample project: https://github.com/babatakao/node-fetch-3-electron-test . With this sample,

  1. Clone the project.
  2. npm ci
  3. npm run build
  4. npm start

Note that the project uses webpack since node: prefix of esm import, used by node-fetch, is not supported by Electron renderer.

Expected behavior

done alert is shown.

Actual behavior

image

TypeError: The "listener" argument must be of type function. Received an instance of Object
    at __node_internal_captureLargerStackTrace (node:internal/errors:463)
    at new NodeError (node:internal/errors:370)
    at __node_internal_ (node:internal/validators:229)
    at checkListener (node:events:131)
    at ClientRequest.once (node:events:533)
    at new ClientRequest (node:_http_client:209)
    at request (node:http:96)
    at renderer.js:111
    at new Promise (<anonymous>)
    at fetch (renderer.js:66)
    at renderer.js:7025
    at renderer.js:7030
    at renderer.js:7032

Your Environment

software version
node-fetch 3.1.0
node 16.13.0 (Electron 15 uses Node 16.5)
npm 7.20.6
Operating System Windows 10 x64

Additional context

The issue does not occur on node-fetch@3.0.0. Thus it seems a recent regression.

I've found the root cause. At here, new URL constructor references NodeJS URL in most cases. However, it references Chromium renderer native URL in the Electron renderer process. This causes type mismatch: NodeJS http library checks that whether the URL has searchParamsSymbol or not at https://github.com/nodejs/node/blob/v16.13.0/lib/_http_client.js#L117.

I suspect that this should be import {URL, format as formatUrl} from 'node:url'; 🤔

@babatakao babatakao added the bug label Nov 11, 2021
@ROpdebee
Copy link

We're facing a similar issue when using node-fetch in a jest test suite with the jsdom environment, since new URL will refer to jsdom's URL, not node's.

@jimmywarting
Copy link
Collaborator

I think this could be the cause: #1268 (this was released into 3.1.0)

We mostly just pass the URL onwards to node's http.request(url, options) and that url just so happens to be a URL-class now...

I think if we cast it into a string just right before we hand it to NodeJS that it could solve the issue.

node-fetch/src/index.js

Lines 80 to 81 in 30c3cfe

// Send request
const request_ = send(parsedURL, options);

- const request_ = send(parsedURL, options);
+ const request_ = send(parsedURL.toString(), options);

I applied the patch to the repo you created (thx for that) and it did seem to solve the issue.


This way it should be working with any instances of URL, wether it's a polyfilled, or any other URL class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants