diff --git a/lib/web/fetch/formdata.js b/lib/web/fetch/formdata.js index 0e95a534f11..9e7d6d0e4ad 100644 --- a/lib/web/fetch/formdata.js +++ b/lib/web/fetch/formdata.js @@ -157,12 +157,27 @@ 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 + + const output = nodeUtil.formatWithOptions(options, state) - return output + // remove [Object null prototype] + return `FormData ${output.slice(output.indexOf(']') + 2)}` } } 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/formdata-inspect-custom.js b/test/fetch/formdata-inspect-custom.js index e52ccdf3fdd..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:\nusername: john_doe\nemail: john@example.com\n' + const expectedOutput = "FormData {\n username: 'john_doe',\n email: 'john@example.com'\n}" 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) }) })