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[kClient].connect is not a function #444

Closed
marinedayo opened this issue Mar 28, 2024 · 6 comments
Closed

this[kClient].connect is not a function #444

marinedayo opened this issue Mar 28, 2024 · 6 comments

Comments

@marinedayo
Copy link

marinedayo commented Mar 28, 2024

Error when downloading yarn with proxy settings.

Details with NODE_DEBUG are below:

bash-5.1# corepack -v
0.26.0
bash-5.1# NODE_DEBUG=fetch HTTPS_PROXY=http://127.0.0.1:9999/ yarn version
FETCH 96: connecting to registry.npmjs.org using https:h1
FETCH 96: connection to registry.npmjs.org using https:h1 errored - this[kClient].connect is not a function
FETCH 96: request to GET https://registry.npmjs.org//yarn errored - this[kClient].connect is not a function
Internal Error: Error when performing the request to https://registry.npmjs.org/yarn; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting
    at fetch (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:22878:11)
    at async fetchAsJson (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:22892:20)
    at async fetchLatestStableVersion (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:22942:20)
    at async fetchLatestStableVersion2 (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:22975:14)
    at async Engine.getDefaultVersion (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:23537:25)
    at async Engine.executePackageManagerRequest (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:23644:30)
    at async BinaryCommand.validateAndExecute (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:21164:22)
    at async _Cli.run (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:22139:18)
    at async Object.runMain (/usr/lib/node_modules/corepack/dist/lib/corepack.cjs:24371:12)

this[kClient].connect is not a function error has occurred.

  • v0.26.0 (latest release): error
  • v0.25.2 (bundled by Node.js 20.12.0): error
  • v0.23.0 (bundled by Node.js 20.11.1): no error

corepack imports undici/lib/proxy-agent.js to use ProxyAgent, but connect function is not found here.

https://github.com/nodejs/corepack/blob/v0.26.0/sources/httpUtils.ts#L66-L81
https://github.com/nodejs/undici/blob/v6.6.2/lib/proxy-agent.js
https://github.com/nodejs/undici/blob/v6.6.2/lib/dispatcher-base.js
https://github.com/nodejs/undici/blob/v6.6.2/lib/dispatcher.js

After searching, I found that connect function is assigned to Dispatcher on index.js.

https://github.com/nodejs/undici/blob/v6.6.2/index.js#L24
https://github.com/nodejs/undici/blob/v6.6.2/lib/api/index.js
https://github.com/nodejs/undici/blob/v6.6.2/lib/api/api-connect.js


I hope the above report will help to resolve this issue.

@yoursunny
Copy link

corepack is part of nodejs package distributed by NodeSource.
I'm affected by the same issue after upgrade to nodejs=20.12.0-1nodesource1 package.
To downgrade:

sudo apt install nodejs=20.11.1-1nodesource1

@marinedayo
Copy link
Author

Yes, I am also using nodejs distributed by NodeSource.
I appended the version information to my report.

@marinedayo
Copy link
Author

Probably import ProxyAgent from undici/index.js instead of undici/lib/proxy-agent.js, e.g. as below:

diff --git a/sources/httpUtils.ts b/sources/httpUtils.ts
index e995d1e..b856099 100644
--- a/sources/httpUtils.ts
+++ b/sources/httpUtils.ts
@@ -100,11 +100,7 @@ async function getProxyAgent(input: string | URL) {
 
   if (!proxy) return undefined;
 
-  // Doing a deep import here since undici isn't tree-shakeable
-  const {default: ProxyAgent} = (await import(
-    // @ts-expect-error No types for this specific file
-    `undici/lib/proxy-agent.js`
-  )) as { default: typeof import('undici').ProxyAgent };
+  const {ProxyAgent} = await import(`undici`);
 
   return new ProxyAgent(proxy);
 }

@aduh95
Copy link
Contributor

aduh95 commented Apr 2, 2024

Probably import ProxyAgent from undici/index.js instead of undici/lib/proxy-agent.js

AFAICT it should not have any effect – except making the bundle size bigger. But it's certainly something we can try, we'd just need a way to reproduce the issue first so we can validate if that has any effect.

@marinedayo
Copy link
Author

marinedayo commented Apr 2, 2024

@aduh95 I'm trying to reproduce. Is it the way to reproduce that you expected?

  1. Clone corepack repository.
  2. yarn install
  3. yarn add proxy --dev
  4. yarn build
  5. Write a code named tests/issue444.test.ts, described below.
  6. yarn test -- issue444
import {afterAll, beforeAll, describe, expect, it} from '@jest/globals';
import process from 'node:process';

import { createProxy } from 'proxy'; // Need `yarn add proxy --dev`.
import type { ProxyServer } from 'proxy';

import { fetchAsJson } from '../sources/httpUtils';

describe(`reproduce issue #444`, () => {

  let proxy: ProxyServer;

  beforeAll((done) => {
    proxy = createProxy();
    proxy.listen(done);
  })

  afterAll((done) => {
    proxy.close(done);
  })

  it(`without proxy settings`, async () => {
    await expect(fetchAsJson(`https://registry.npmjs.org/yarn`)).resolves.toEqual(expect.anything());
  });

  it(`with proxy settings`, async () => {
    process.env.HTTPS_PROXY = `http://localhost:${proxy.address().port}`;

    await expect(fetchAsJson(`https://registry.npmjs.org/yarn`)).resolves.toEqual(expect.anything());
    // TODO: Test fails.
    // Received promise rejected instead of resolved
    // Rejected to value: [Error: Error when performing the request to https://registry.npmjs.org/yarn; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting]
  });

});

@marinedayo
Copy link
Author

Looks like the same reason as here currently in discussion. So my issue is closed.

Duplicate of #417

@marinedayo marinedayo closed this as not planned Won't fix, can't repro, duplicate, stale Apr 6, 2024
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