From e69ec5cb2950c7577c79efd03c43f067c49cbf19 Mon Sep 17 00:00:00 2001 From: Khafra Date: Fri, 1 Mar 2024 20:23:52 -0500 Subject: [PATCH 1/3] for some reason this fixes the test --- test/fetch/content-length.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/fetch/content-length.js b/test/fetch/content-length.js index 637ff0b800e..cc26a724695 100644 --- a/test/fetch/content-length.js +++ b/test/fetch/content-length.js @@ -6,7 +6,6 @@ const { createServer } = require('node:http') const { once } = require('node:events') const { Blob } = require('node:buffer') const { fetch, FormData } = require('../..') -const { closeServerAsPromise } = require('../utils/node-http') // https://github.com/nodejs/undici/issues/1783 test('Content-Length is set when using a FormData body with fetch', async (t) => { @@ -18,7 +17,6 @@ test('Content-Length is set when using a FormData body with fetch', async (t) => }).listen(0) await once(server, 'listening') - t.after(closeServerAsPromise(server)) const fd = new FormData() fd.set('file', new Blob(['hello world 👋'], { type: 'text/plain' }), 'readme.md') @@ -27,5 +25,5 @@ test('Content-Length is set when using a FormData body with fetch', async (t) => await fetch(`http://localhost:${server.address().port}`, { method: 'POST', body: fd - }) + }).finally(() => server.close()) }) From 774a0a41b31ddde117c12291d0941353560b0089 Mon Sep 17 00:00:00 2001 From: Khafra Date: Thu, 14 Mar 2024 18:57:53 -0400 Subject: [PATCH 2/3] cleanup custom format --- lib/web/fetch/formdata.js | 22 +++++++++++++++++----- lib/web/fetch/headers.js | 10 ++-------- lib/web/fetch/request.js | 4 +++- lib/web/fetch/response.js | 4 +++- test/fetch/content-length.js | 4 +++- test/fetch/formdata-inspect-custom.js | 2 +- test/fetch/request-inspect-custom.js | 2 +- 7 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/web/fetch/formdata.js b/lib/web/fetch/formdata.js index 0e95a534f11..c5ae0834bf0 100644 --- a/lib/web/fetch/formdata.js +++ b/lib/web/fetch/formdata.js @@ -157,12 +157,24 @@ class FormData { } [nodeUtil.inspect.custom] (depth, options) { - let output = 'FormData:\n' - this[kState].forEach(entry => { - output += `${entry.name}: ${entry.value}\n` - }) + const state = this[kState].reduce((a, b) => { + if (a[b.name]) { + if (Array.isArray(a[b.name])) { + a[b.name].push(b.value) + } else { + a[b.name] = [a[b.name], b.value] + } + } else { + a[b.name] = b.value + } + + return a + }, { __proto__: null }) + + options.depth ??= depth + options.colors ??= true - return output + return `FormData ${nodeUtil.formatWithOptions(options, state)}` } } diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index b2580e90e04..b849b0e356c 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -572,16 +572,10 @@ class Headers { return (this[kHeadersList][kHeadersSortedMap] = headers) } - [Symbol.for('nodejs.util.inspect.custom')] () { - webidl.brandCheck(this, Headers) - - return this[kHeadersList] - } - [util.inspect.custom] (depth, options) { - const inspected = util.inspect(this[kHeadersList].entries) + options.depth ??= depth - return `Headers ${inspected}` + return `Headers ${util.formatWithOptions(options, this[kHeadersList].entries)}` } } diff --git a/lib/web/fetch/request.js b/lib/web/fetch/request.js index 82de276060b..a35b90bf9f3 100644 --- a/lib/web/fetch/request.js +++ b/lib/web/fetch/request.js @@ -778,6 +778,8 @@ class Request { options.depth = 2 } + options.colors ??= true + const properties = { method: this.method, url: this.url, @@ -796,7 +798,7 @@ class Request { signal: this.signal } - return nodeUtil.formatWithOptions(options, { ...properties }) + return `Request ${nodeUtil.formatWithOptions(options, properties)}` } } diff --git a/lib/web/fetch/response.js b/lib/web/fetch/response.js index bcb54237fcc..182e3dc0199 100644 --- a/lib/web/fetch/response.js +++ b/lib/web/fetch/response.js @@ -259,6 +259,8 @@ class Response { options.depth = 2 } + options.colors ??= true + const properties = { status: this.status, statusText: this.statusText, @@ -271,7 +273,7 @@ class Response { url: this.url } - return nodeUtil.formatWithOptions(options, `Response ${nodeUtil.inspect(properties)}`) + return `Response ${nodeUtil.formatWithOptions(options, properties)}` } } diff --git a/test/fetch/content-length.js b/test/fetch/content-length.js index cc26a724695..637ff0b800e 100644 --- a/test/fetch/content-length.js +++ b/test/fetch/content-length.js @@ -6,6 +6,7 @@ const { createServer } = require('node:http') const { once } = require('node:events') const { Blob } = require('node:buffer') const { fetch, FormData } = require('../..') +const { closeServerAsPromise } = require('../utils/node-http') // https://github.com/nodejs/undici/issues/1783 test('Content-Length is set when using a FormData body with fetch', async (t) => { @@ -17,6 +18,7 @@ test('Content-Length is set when using a FormData body with fetch', async (t) => }).listen(0) await once(server, 'listening') + t.after(closeServerAsPromise(server)) const fd = new FormData() fd.set('file', new Blob(['hello world 👋'], { type: 'text/plain' }), 'readme.md') @@ -25,5 +27,5 @@ test('Content-Length is set when using a FormData body with fetch', async (t) => await fetch(`http://localhost:${server.address().port}`, { method: 'POST', body: fd - }).finally(() => server.close()) + }) }) diff --git a/test/fetch/formdata-inspect-custom.js b/test/fetch/formdata-inspect-custom.js index e52ccdf3fdd..22fd5ebe0de 100644 --- a/test/fetch/formdata-inspect-custom.js +++ b/test/fetch/formdata-inspect-custom.js @@ -10,7 +10,7 @@ test('FormData class custom inspection', () => { formData.append('username', 'john_doe') formData.append('email', 'john@example.com') - const expectedOutput = 'FormData:\nusername: john_doe\nemail: john@example.com\n' + const expectedOutput = "FormData { username: 'john_doe', email: 'john@example.com' }" assert.deepStrictEqual(inspect(formData), expectedOutput) }) diff --git a/test/fetch/request-inspect-custom.js b/test/fetch/request-inspect-custom.js index 7f4290d1d4e..7cb7fc73152 100644 --- a/test/fetch/request-inspect-custom.js +++ b/test/fetch/request-inspect-custom.js @@ -16,7 +16,7 @@ describe('Request custom inspection', () => { const inspectedOutput = util.inspect(request) - const expectedOutput = '{\n method: \'POST\',\n url: \'https://example.com/api\',\n headers: Headers { \'Content-Type\': \'application/json\' },\n destination: \'\',\n referrer: \'about:client\',\n referrerPolicy: \'\',\n mode: \'cors\',\n credentials: \'same-origin\',\n cache: \'default\',\n redirect: \'follow\',\n integrity: \'\',\n keepalive: false,\n isReloadNavigation: false,\n isHistoryNavigation: false,\n signal: AbortSignal { aborted: false }\n}' + const expectedOutput = "Request {\n method: 'POST',\n url: 'https://example.com/api',\n headers: Headers { 'Content-Type': 'application/json' },\n destination: '',\n referrer: 'about:client',\n referrerPolicy: '',\n mode: 'cors',\n credentials: 'same-origin',\n cache: 'default',\n redirect: 'follow',\n integrity: '',\n keepalive: false,\n isReloadNavigation: false,\n isHistoryNavigation: false,\n signal: AbortSignal { aborted: false }\n}" assert.strictEqual(inspectedOutput, expectedOutput) }) }) From db795e49548aa275d4ff0011a7ca4ec239b789e6 Mon Sep 17 00:00:00 2001 From: Khafra Date: Thu, 14 Mar 2024 19:17:01 -0400 Subject: [PATCH 3/3] fixup --- lib/web/fetch/formdata.js | 5 ++++- test/fetch/formdata-inspect-custom.js | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/web/fetch/formdata.js b/lib/web/fetch/formdata.js index c5ae0834bf0..9e7d6d0e4ad 100644 --- a/lib/web/fetch/formdata.js +++ b/lib/web/fetch/formdata.js @@ -174,7 +174,10 @@ class FormData { options.depth ??= depth options.colors ??= true - return `FormData ${nodeUtil.formatWithOptions(options, state)}` + const output = nodeUtil.formatWithOptions(options, state) + + // remove [Object null prototype] + return `FormData ${output.slice(output.indexOf(']') + 2)}` } } diff --git a/test/fetch/formdata-inspect-custom.js b/test/fetch/formdata-inspect-custom.js index 22fd5ebe0de..4fb70069439 100644 --- a/test/fetch/formdata-inspect-custom.js +++ b/test/fetch/formdata-inspect-custom.js @@ -10,7 +10,7 @@ test('FormData class custom inspection', () => { formData.append('username', 'john_doe') formData.append('email', 'john@example.com') - const expectedOutput = "FormData { username: 'john_doe', email: 'john@example.com' }" + const expectedOutput = "FormData {\n username: 'john_doe',\n email: 'john@example.com'\n}" assert.deepStrictEqual(inspect(formData), expectedOutput) })