From 10192284c4266bfc8a621fcb3ea9d5aa2c2a98c4 Mon Sep 17 00:00:00 2001 From: MadCcc <1075746765@qq.com> Date: Wed, 11 Dec 2024 21:01:26 +0800 Subject: [PATCH 1/2] fix: should not throw when fetch a Request with post --- .gitignore | 1 + src/fetch.ts | 2 +- test/fetch.test.ts | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 872636c1..7b8314e4 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ test/fixtures/ts-cjs-es2021/*.js test/fixtures/ts-esm/*.js .eslintcache .tshy* +.idea/ diff --git a/src/fetch.ts b/src/fetch.ts index b1f63bf2..7c0537a5 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -218,7 +218,7 @@ export class FetchFactory { } as any as RawResponseWithMeta; try { await FetchFactory.#opaqueLocalStorage.run(internalOpaque, async () => { - res = await UndiciFetch(input, init); + res = await UndiciFetch(request, init); }); } catch (e: any) { updateSocketInfo(socketInfo, internalOpaque, e); diff --git a/test/fetch.test.ts b/test/fetch.test.ts index 8c186763..3667ceeb 100644 --- a/test/fetch.test.ts +++ b/test/fetch.test.ts @@ -7,6 +7,7 @@ import { fetch, FetchDiagnosticsMessage, FetchFactory, FetchResponseDiagnosticsMessage, } from '../src/fetch.js'; import { RequestDiagnosticsMessage, ResponseDiagnosticsMessage } from '../src/HttpClient.js'; +import { Request } from 'undici'; describe('fetch.test.ts', () => { let close: any; @@ -109,4 +110,14 @@ describe('fetch.test.ts', () => { assert(stats); assert(Object.keys(stats).length > 0); }); + + it('fetch request with post should work', async () => { + await assert.doesNotReject(async () => { + const request = new Request(_url, { + method: 'POST', + body: 'test-body', + }); + await fetch(request); + }, /Cannot construct a Request with a Request object that has already been used/); + }); }); From f113c899ea87611bd1f884488abb7e0039895943 Mon Sep 17 00:00:00 2001 From: MadCcc <1075746765@qq.com> Date: Wed, 11 Dec 2024 21:40:23 +0800 Subject: [PATCH 2/2] fix: remove duplicate init --- src/fetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fetch.ts b/src/fetch.ts index 7c0537a5..8b225bc7 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -218,7 +218,7 @@ export class FetchFactory { } as any as RawResponseWithMeta; try { await FetchFactory.#opaqueLocalStorage.run(internalOpaque, async () => { - res = await UndiciFetch(request, init); + res = await UndiciFetch(request); }); } catch (e: any) { updateSocketInfo(socketInfo, internalOpaque, e);