From 1cc61d0a8f47c5c505acb721db0cc4c759c6c606 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 28 Jun 2025 21:12:43 +0800 Subject: [PATCH 1/6] test: fix fail tests --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index be5fffae..f98564d6 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ content-type: application/json } ``` -This exmaple can use `options.data` with `application/json` content type: +This example can use `options.data` with `application/json` content type: ```js await request('https://example.com', { From e615bb76c24be987d5a3275de2503f64307bc0e9 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 28 Jun 2025 21:24:21 +0800 Subject: [PATCH 2/6] f --- .github/workflows/nodejs.yml | 2 +- package.json | 4 ++-- test/patch-for-node16.cjs | 13 +++++++++++++ test/patch-structuredClone.cjs | 7 ------- 4 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 test/patch-for-node16.cjs delete mode 100644 test/patch-structuredClone.cjs diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index d3c74609..5a5926ad 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -13,6 +13,6 @@ jobs: uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest, macos-latest, windows-latest' - version: '16, 18, 20, 22, 23' + version: '16, 18, 20, 22, 24' secrets: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/package.json b/package.json index 11b1ea1f..05cdc62a 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "test": "npm run lint -- --fix && vitest run", "test-keepalive": "cross-env TEST_KEEPALIVE_COUNT=50 vitest run --test-timeout 180000 keep-alive-header.test.ts", "test-node16": "node examples/httpclient.cjs && node examples/search_github.cjs && node examples/timing.cjs", - "cov": "cross-env NODE_OPTIONS='--require ./test/patch-structuredClone.cjs' vitest run --coverage", + "cov": "cross-env NODE_OPTIONS='--require ./test/patch-for-node16.cjs' vitest run --coverage", "ci": "npm run lint && npm run cov && npm run prepublishOnly && npm pack && attw --pack", "clean": "rm -rf dist", "prepublishOnly": "npm run build" @@ -78,7 +78,7 @@ "tshy": "^3.0.0", "tshy-after": "^1.0.0", "typescript": "^5.0.4", - "vitest": "^3.0.2" + "vitest": "^3.2.4" }, "engines": { "node": ">= 18.19.0" diff --git a/test/patch-for-node16.cjs b/test/patch-for-node16.cjs new file mode 100644 index 00000000..6e0f6d90 --- /dev/null +++ b/test/patch-for-node16.cjs @@ -0,0 +1,13 @@ +// vitest require structuredClone +if (!('structuredClone' in globalThis)) { + const structuredClone = require('@ungap/structured-clone').default; + + globalThis.structuredClone = structuredClone; + // console.debug('patched structuredClone for Node.js %s', process.version); +} + +// vitest require crypto.getRandomValues +const crypto = require('node:crypto'); +if (typeof crypto.getRandomValues !== 'function') { + crypto.getRandomValues = crypto.webcrypto.getRandomValues.bind(crypto.webcrypto); +} diff --git a/test/patch-structuredClone.cjs b/test/patch-structuredClone.cjs deleted file mode 100644 index c5612599..00000000 --- a/test/patch-structuredClone.cjs +++ /dev/null @@ -1,7 +0,0 @@ -const structuredClone = require('@ungap/structured-clone').default; - -// vitest require structuredClone -if (!('structuredClone' in globalThis)) { - globalThis.structuredClone = structuredClone; - // console.debug('patched structuredClone for Node.js %s', process.version); -} From 0149b9f08948a1a22dd178155725080f579d0f6f Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 28 Jun 2025 21:48:44 +0800 Subject: [PATCH 3/6] f --- package.json | 2 +- src/HttpClient.ts | 6 ++++-- src/fetch.ts | 6 ++++-- test/HttpClient.test.ts | 3 ++- test/fetch.test.ts | 2 +- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 05cdc62a..dc661f44 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "test-node16": "node examples/httpclient.cjs && node examples/search_github.cjs && node examples/timing.cjs", "cov": "cross-env NODE_OPTIONS='--require ./test/patch-for-node16.cjs' vitest run --coverage", "ci": "npm run lint && npm run cov && npm run prepublishOnly && npm pack && attw --pack", - "clean": "rm -rf dist", + "clean": "rm -rf dist && tsc -b --clean", "prepublishOnly": "npm run build" }, "dependencies": { diff --git a/src/HttpClient.ts b/src/HttpClient.ts index d913cdad..fab557b3 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -227,9 +227,11 @@ export class HttpClient extends EventEmitter { return poolStatsMap; } for (const [ key, ref ] of clients) { - const pool = typeof ref.deref === 'function' ? ref.deref() : ref as unknown as Pool; - const stats = pool?.stats; + const pool = (typeof ref.deref === 'function' ? ref.deref() : ref) as unknown as (Pool & { dispatcher: Pool }); + // NOTE: pool become to { dispatcher: Pool } in undici@v7 + const stats = pool?.stats ?? pool?.dispatcher?.stats; if (!stats) continue; + poolStatsMap[key] = { connected: stats.connected, free: stats.free, diff --git a/src/fetch.ts b/src/fetch.ts index 7a7f09f5..48091d11 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -114,9 +114,11 @@ export class FetchFactory { return poolStatsMap; } for (const [ key, ref ] of clients) { - const pool = typeof ref.deref === 'function' ? ref.deref() : ref as unknown as Pool; - const stats = pool?.stats; + const pool = (typeof ref.deref === 'function' ? ref.deref() : ref) as unknown as (Pool & { dispatcher: Pool }); + // NOTE: pool become to { dispatcher: Pool } in undici@v7 + const stats = pool?.stats ?? pool?.dispatcher?.stats; if (!stats) continue; + poolStatsMap[key] = { connected: stats.connected, free: stats.free, diff --git a/test/HttpClient.test.ts b/test/HttpClient.test.ts index 0dc3c04a..cd2fa393 100644 --- a/test/HttpClient.test.ts +++ b/test/HttpClient.test.ts @@ -326,7 +326,8 @@ describe('HttpClient.test.ts', () => { const response = await httpclient.request(_url); assert.equal(response.status, 200); - assert.equal(Object.keys(httpclient.getDispatcherPoolStats()).length, 1); + assert.equal(Object.keys(httpclient.getDispatcherPoolStats()).length, 1, + `dispatcher pool stats: ${JSON.stringify(httpclient.getDispatcherPoolStats())}`); }); it('should check non-ip hostname with custom lookup', async () => { diff --git a/test/fetch.test.ts b/test/fetch.test.ts index 3667ceeb..e3c1b5cd 100644 --- a/test/fetch.test.ts +++ b/test/fetch.test.ts @@ -108,7 +108,7 @@ describe('fetch.test.ts', () => { const stats = FetchFactory.getDispatcherPoolStats(); assert(stats); - assert(Object.keys(stats).length > 0); + assert(Object.keys(stats).length > 0, `dispatcher pool stats: ${JSON.stringify(stats)}`); }); it('fetch request with post should work', async () => { From a24c671a356da8589c17a835cf5d452330f55501 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 28 Jun 2025 21:50:36 +0800 Subject: [PATCH 4/6] f --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dc661f44..21dc7bde 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "test": "npm run lint -- --fix && vitest run", "test-keepalive": "cross-env TEST_KEEPALIVE_COUNT=50 vitest run --test-timeout 180000 keep-alive-header.test.ts", "test-node16": "node examples/httpclient.cjs && node examples/search_github.cjs && node examples/timing.cjs", - "cov": "cross-env NODE_OPTIONS='--require ./test/patch-for-node16.cjs' vitest run --coverage", + "cov": "cross-env NODE_OPTIONS=\"--require ./test/patch-for-node16.cjs\" vitest run --coverage", "ci": "npm run lint && npm run cov && npm run prepublishOnly && npm pack && attw --pack", "clean": "rm -rf dist && tsc -b --clean", "prepublishOnly": "npm run build" From cfa7cdd00f00e348f8f85c92525cc386e9c0db64 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 28 Jun 2025 21:54:33 +0800 Subject: [PATCH 5/6] f --- test/fixtures/socket_server.ts | 1 + test/options.socketPath.test.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/fixtures/socket_server.ts b/test/fixtures/socket_server.ts index ea531304..8e1c2bd4 100644 --- a/test/fixtures/socket_server.ts +++ b/test/fixtures/socket_server.ts @@ -2,6 +2,7 @@ import { createServer, Server } from 'node:http'; const socketPathPrefix = '/tmp/urllib.unix.sock'; let index = 0; + export async function startServer(): Promise<{ server: Server, url: string, diff --git a/test/options.socketPath.test.ts b/test/options.socketPath.test.ts index fc6bcf3c..3e7fd6e8 100644 --- a/test/options.socketPath.test.ts +++ b/test/options.socketPath.test.ts @@ -19,7 +19,7 @@ describe.skipIf(isWindows())('options.socketPath.test.ts', () => { afterAll(async () => { await close(); - await server2.closeServer(); + await server2?.closeServer(); }); it('should request socket successfully', async () => { From 333b129e3c3af4ed9bebe2c48a619c07d1357820 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 28 Jun 2025 22:01:22 +0800 Subject: [PATCH 6/6] f --- package.json | 3 ++- test/{patch-for-node16.cjs => patch-for-node16-18.cjs} | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) rename test/{patch-for-node16.cjs => patch-for-node16-18.cjs} (72%) diff --git a/package.json b/package.json index 21dc7bde..914c5ee1 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "test": "npm run lint -- --fix && vitest run", "test-keepalive": "cross-env TEST_KEEPALIVE_COUNT=50 vitest run --test-timeout 180000 keep-alive-header.test.ts", "test-node16": "node examples/httpclient.cjs && node examples/search_github.cjs && node examples/timing.cjs", - "cov": "cross-env NODE_OPTIONS=\"--require ./test/patch-for-node16.cjs\" vitest run --coverage", + "cov": "cross-env NODE_OPTIONS=\"--require ./test/patch-for-node16-18.cjs\" vitest run --coverage", "ci": "npm run lint && npm run cov && npm run prepublishOnly && npm pack && attw --pack", "clean": "rm -rf dist && tsc -b --clean", "prepublishOnly": "npm run build" @@ -74,6 +74,7 @@ "iconv-lite": "^0.6.3", "proxy": "^1.0.2", "selfsigned": "^2.4.1", + "string.prototype.towellformed": "^1.0.2", "tar-stream": "^2.2.0", "tshy": "^3.0.0", "tshy-after": "^1.0.0", diff --git a/test/patch-for-node16.cjs b/test/patch-for-node16-18.cjs similarity index 72% rename from test/patch-for-node16.cjs rename to test/patch-for-node16-18.cjs index 6e0f6d90..b140c688 100644 --- a/test/patch-for-node16.cjs +++ b/test/patch-for-node16-18.cjs @@ -11,3 +11,8 @@ const crypto = require('node:crypto'); if (typeof crypto.getRandomValues !== 'function') { crypto.getRandomValues = crypto.webcrypto.getRandomValues.bind(crypto.webcrypto); } + +// undici@v7 require String.prototype.toWellFormed, patch on Node.js 16 and 18 +if (typeof String.prototype.toWellFormed !== 'function') { + require('string.prototype.towellformed/auto'); +}