panva / openid-client

OAuth 2 / OpenID Connect Client API for JavaScript Runtimes
MIT License
1.83k stars 392 forks source link

TypeError: The "listener" argument must be of type function. #523

Closed cjcooper closed 2 years ago

cjcooper commented 2 years ago

TypeError: The "listener" argument must be of type function. Received an instance of Object at checkListener (node:events:266:3) at ClientRequest.once (node:events:647:3) at new ClientRequest (node:_http_client:228:10) at Object.request (node:https:357:10)

To Reproduce

const issuer = await Issuer.discover('http://localhost:4011');

Steps to reproduce the behaviour:

  1. just call that one line. it appears node is not using the second overload of http/https request when url is a string. on the line (url.protocol === 'https:' ? https.request : http.request)(url.href, opts); if .href is removed then it operates as expected. I absolutely agree that the documents state a string should work, but it isn't. it seems odd to add the url object, which is accepted by the function, but not actually use that object directly.

Expected behaviour Make the web calls instead of throwing exception.

Environment:

Additional context Add any other context about the problem here.

this was first noted here: https://github.com/panva/node-openid-client/issues/500.

panva commented 2 years ago

I am not able to reproduce this issue. #500 was due to electron mixing up chromium and node's globals, that's not what you're describing here.

➜  client git:(main) nvm use 16.16.0
Now using node v16.16.0 (npm v8.11.0)
➜  client git:(main) node -e "let{Issuer}=require('.'); Issuer.discover('https://op.panva.cz').then(()=>console.log('works on ' + process.version))"
works on v16.16.0
➜  client git:(main) nvm install 16.17.0                                                                                                            
v16.17.0 is already installed.
Now using node v16.17.0 (npm v8.17.0)
➜  client git:(main) node -e "let{Issuer}=require('.'); Issuer.discover('https://op.panva.cz').then(()=>console.log('works on ' + process.version))"
works on v16.17.0

simple node & express, typescript application

Please provide a simple project repo I can clone, npm install and run to reproduce your issue then.

pongstylin commented 2 years ago

I was about to report the same issue. But when I ran your test, it passed. So I looked deeper and figured out the problem. Turns out the stack trace was pretty important:

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object
    at checkListener (node:events:259:3)
    at ClientRequest.once (node:events:661:3)
    at new ClientRequest (node:_http_client:215:10)
    at Object.request (node:https:353:10)
    at C:\Projects\tactics\node_modules\agent-base\patch-core.js:25:22
    at Function.request (C:\Projects\tactics\node_modules\openid-client\lib\helpers\request.js:120:73)
    at Function.discover (C:\Projects\tactics\node_modules\openid-client\lib\issuer.js:171:36)

Notice that "patch-core.js"? That is where the problem actually lay. I was using an older version of "web-push" in my project, which had a dependency on "https-proxy-agent", which had a dependency on "agent-base". Installing the latest version of "web-push" cleared everything up.

panva commented 2 years ago

Thank you @pongstylin that clears it up!