From faed7eb425e4837e157312ae80fd794c1396b633 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Tue, 30 Apr 2024 19:48:33 +0900 Subject: [PATCH 1/8] fix: keep raw header name --- lib/web/fetch/headers.js | 14 +++++++++++++- lib/web/fetch/request.js | 8 +++++--- test/fetch/headers-case.js | 13 +++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index 3157561b470..f9e057a6269 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -250,7 +250,7 @@ class HeadersList { get entries () { const headers = {} - if (this[kHeadersMap].size) { + if (this[kHeadersMap].size !== 0) { for (const { name, value } of this[kHeadersMap].values()) { headers[name] = value } @@ -259,6 +259,18 @@ class HeadersList { return headers } + get entriesRawList () { + const headers = [] + + if (this[kHeadersMap].size !== 0) { + for (const { name, value } of this[kHeadersMap].values()) { + headers.push([name, value]) + } + } + + return headers + } + get entriesList () { const headers = [] diff --git a/lib/web/fetch/request.js b/lib/web/fetch/request.js index 1441a58b06e..80fe312ca0a 100644 --- a/lib/web/fetch/request.js +++ b/lib/web/fetch/request.js @@ -477,9 +477,11 @@ class Request { // 4. If headers is a Headers object, then for each header in its header // list, append header’s name/header’s value to this’s headers. if (headers instanceof HeadersList) { - for (const { 0: key, 1: val } of headers) { - // Note: The header names are already in lowercase. - headersList.append(key, val, true) + const rawList = headersList.entriesRawList + // TODO: optimizing this. + for (let i = 0; i < rawList.length; ++i) { + const { 0: key, 1: value } = rawList[i] + headersList.append(key, value, false) } // Note: Copy the `set-cookie` meta-data. headersList.cookies = headers.cookies diff --git a/test/fetch/headers-case.js b/test/fetch/headers-case.js index bd9770b755f..e74d7ac25be 100644 --- a/test/fetch/headers-case.js +++ b/test/fetch/headers-case.js @@ -1,13 +1,14 @@ 'use strict' -const { fetch, Headers } = require('../..') +const { fetch, Headers, Request } = require('../..') const { createServer } = require('node:http') const { once } = require('node:events') const { test } = require('node:test') const { tspl } = require('@matteo.collina/tspl') +const { closeServerAsPromise } = require('../utils/node-http') test('Headers retain keys case-sensitive', async (t) => { - const assert = tspl(t, { plan: 3 }) + const assert = tspl(t, { plan: 4 }) const server = createServer((req, res) => { assert.ok(req.rawHeaders.includes('Content-Type')) @@ -15,16 +16,16 @@ test('Headers retain keys case-sensitive', async (t) => { res.end() }).listen(0) - t.after(() => server.close()) + t.after(closeServerAsPromise(server)) await once(server, 'listening') + const url = `http://localhost:${server.address().port}` for (const headers of [ new Headers([['Content-Type', 'text/plain']]), { 'Content-Type': 'text/plain' }, [['Content-Type', 'text/plain']] ]) { - await fetch(`http://localhost:${server.address().port}`, { - headers - }) + await fetch(url, { headers }) } + await fetch(new Request(url, { headers: new Headers([['Content-Type', 'text/plain']]) }), { body: null }) }) From 44e8c802a80b0f0276ff07782bc05aa4dbc2db71 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Tue, 30 Apr 2024 19:58:47 +0900 Subject: [PATCH 2/8] fixup --- test/fetch/headers-case.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/fetch/headers-case.js b/test/fetch/headers-case.js index e74d7ac25be..a14e92a57cd 100644 --- a/test/fetch/headers-case.js +++ b/test/fetch/headers-case.js @@ -27,5 +27,6 @@ test('Headers retain keys case-sensitive', async (t) => { ]) { await fetch(url, { headers }) } - await fetch(new Request(url, { headers: new Headers([['Content-Type', 'text/plain']]) }), { body: null }) + // see https://github.com/nodejs/undici/pull/3183 + await fetch(new Request(url, { headers: new Headers([['Content-Type', 'text/plain']]) }), { headers: undefined }) }) From cc0868325e50776dba475bbe5bbe4b38ba7471f1 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Tue, 30 Apr 2024 20:03:06 +0900 Subject: [PATCH 3/8] Apply suggestions from code review --- lib/web/fetch/request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web/fetch/request.js b/lib/web/fetch/request.js index 80fe312ca0a..4c78e501920 100644 --- a/lib/web/fetch/request.js +++ b/lib/web/fetch/request.js @@ -477,7 +477,7 @@ class Request { // 4. If headers is a Headers object, then for each header in its header // list, append header’s name/header’s value to this’s headers. if (headers instanceof HeadersList) { - const rawList = headersList.entriesRawList + const rawList = headers.entriesRawList // TODO: optimizing this. for (let i = 0; i < rawList.length; ++i) { const { 0: key, 1: value } = rawList[i] From 07c0af746d7fec5fbc98d5b7d7bda0d2c5d7ed9f Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Tue, 30 Apr 2024 20:08:51 +0900 Subject: [PATCH 4/8] Apply suggestions from code review --- test/fetch/headers-case.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fetch/headers-case.js b/test/fetch/headers-case.js index a14e92a57cd..d07311a8859 100644 --- a/test/fetch/headers-case.js +++ b/test/fetch/headers-case.js @@ -28,5 +28,5 @@ test('Headers retain keys case-sensitive', async (t) => { await fetch(url, { headers }) } // see https://github.com/nodejs/undici/pull/3183 - await fetch(new Request(url, { headers: new Headers([['Content-Type', 'text/plain']]) }), { headers: undefined }) + await fetch(new Request(url, { headers: new Headers([['Content-Type', 'text/plain']]) }), { method: 'GET' }) }) From 03daeff738d9adb09dd8deef375206fdacfe5bab Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Tue, 30 Apr 2024 20:20:32 +0900 Subject: [PATCH 5/8] Apply suggestions from code review --- lib/web/fetch/request.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/web/fetch/request.js b/lib/web/fetch/request.js index 4c78e501920..8004d8a2484 100644 --- a/lib/web/fetch/request.js +++ b/lib/web/fetch/request.js @@ -478,7 +478,6 @@ class Request { // list, append header’s name/header’s value to this’s headers. if (headers instanceof HeadersList) { const rawList = headers.entriesRawList - // TODO: optimizing this. for (let i = 0; i < rawList.length; ++i) { const { 0: key, 1: value } = rawList[i] headersList.append(key, value, false) From 1f344188f8d102a5a1130629ee65fe80c63ea384 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Tue, 30 Apr 2024 21:12:30 +0900 Subject: [PATCH 6/8] Apply suggestions from code review --- test/fetch/headers-case.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fetch/headers-case.js b/test/fetch/headers-case.js index d07311a8859..78c10b3d547 100644 --- a/test/fetch/headers-case.js +++ b/test/fetch/headers-case.js @@ -28,5 +28,5 @@ test('Headers retain keys case-sensitive', async (t) => { await fetch(url, { headers }) } // see https://github.com/nodejs/undici/pull/3183 - await fetch(new Request(url, { headers: new Headers([['Content-Type', 'text/plain']]) }), { method: 'GET' }) + await fetch(new Request(url, { headers: [['Content-Type', 'text/plain']] }), { method: 'GET' }) }) From 70be495fd6ba70514eecda7fbce54019376e42dc Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Wed, 1 May 2024 17:57:05 +0900 Subject: [PATCH 7/8] Apply suggestions from code review --- lib/web/fetch/headers.js | 12 ++---------- lib/web/fetch/request.js | 4 +--- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index f9e057a6269..d726b5a5d10 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -259,16 +259,8 @@ class HeadersList { return headers } - get entriesRawList () { - const headers = [] - - if (this[kHeadersMap].size !== 0) { - for (const { name, value } of this[kHeadersMap].values()) { - headers.push([name, value]) - } - } - - return headers + rawValues () { + return this[kHeadersMap].values() } get entriesList () { diff --git a/lib/web/fetch/request.js b/lib/web/fetch/request.js index 8004d8a2484..1ce54cea8cc 100644 --- a/lib/web/fetch/request.js +++ b/lib/web/fetch/request.js @@ -477,9 +477,7 @@ class Request { // 4. If headers is a Headers object, then for each header in its header // list, append header’s name/header’s value to this’s headers. if (headers instanceof HeadersList) { - const rawList = headers.entriesRawList - for (let i = 0; i < rawList.length; ++i) { - const { 0: key, 1: value } = rawList[i] + for (const { key, value } of headers.rawValues()) { headersList.append(key, value, false) } // Note: Copy the `set-cookie` meta-data. From 8c339861acb3b57cfc4412fc86f798902df012b7 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Wed, 1 May 2024 18:00:29 +0900 Subject: [PATCH 8/8] Apply suggestions from code review --- lib/web/fetch/request.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/web/fetch/request.js b/lib/web/fetch/request.js index 1ce54cea8cc..08eca21bc53 100644 --- a/lib/web/fetch/request.js +++ b/lib/web/fetch/request.js @@ -477,8 +477,8 @@ class Request { // 4. If headers is a Headers object, then for each header in its header // list, append header’s name/header’s value to this’s headers. if (headers instanceof HeadersList) { - for (const { key, value } of headers.rawValues()) { - headersList.append(key, value, false) + for (const { name, value } of headers.rawValues()) { + headersList.append(name, value, false) } // Note: Copy the `set-cookie` meta-data. headersList.cookies = headers.cookies