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

this._getSession is not a function while use HttpsProxyAgent #675

Open
WhoisUnknown opened this issue Nov 7, 2024 · 2 comments
Open

this._getSession is not a function while use HttpsProxyAgent #675

WhoisUnknown opened this issue Nov 7, 2024 · 2 comments

Comments

@WhoisUnknown
Copy link

Hi, I encounter a bug when I use proxy for requests

TypeError: this._getSession is not a function
    at HttpsProxyAgent.createConnection (node:https:140:26)
    at MockHttpSocket.passthrough (example/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:160:25)
    at _ClientRequestInterceptor.onRequest (example/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/index.ts:151:21)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

Example repo https://github.com/WhoisUnknown/example-interseptor

I used version 0.36.10

@fionawhim
Copy link

Seeing this as well, with 0.37.1 and Node v20.12.0. My use case is MSW running in a Vitest test, and trying to access (unmocked) HTTPS endpoints.

I think the code in the Node repo is here: https://github.com/nodejs/node/blob/v20.x/lib/https.js#L163

Possibly a situation in the way that the createConnection method is used here? It gets bound to something without a _getSession method, but an _agentKey is still being passed into options?

@sebws
Copy link

sebws commented Dec 11, 2024

public createConnection(options: any, callback: any) {
const createConnection =
(this.customAgent instanceof https.Agent &&
this.customAgent.createConnection) ||
super.createConnection
const socket = new MockHttpSocket({
connectionOptions: options,
createConnection: createConnection.bind(
this.customAgent || this,
options,
callback
),
onRequest: this.onRequest.bind(this),
onResponse: this.onResponse.bind(this),
})

Looks like what happens is that this.customAgent is there, but it isn't an instance of https.Agent, so createConnection is taken from super.createConnection. Then when we go to bind at L75-L79, it binds super.createConnection to this.customAgent. No good!

Given that createConnection does exist on the HttpsProxyAgent (this.customAgent), I think it shouldn't be that super.createConnection is now bound to this instead of this.customAgent, instead that the check isn't if this.customAgent is an instance of https.Agent but something else?

I'm not sure of the intent here. Hopefully somebody who knows better what's intended to happen here, might just be @kettanaito

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

No branches or pull requests

3 participants