From 3b69310d4e050b81fe3a3eb8ff675f56c50b8802 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 31 Mar 2024 09:44:32 +0200 Subject: [PATCH] fix: don't leak internal class --- docs/docs/api/DiagnosticsChannel.md | 2 -- lib/core/diagnostics.js | 2 +- lib/core/request.js | 40 +++++++++++++++++++++-------- types/diagnostics-channel.d.ts | 1 - 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/docs/docs/api/DiagnosticsChannel.md b/docs/docs/api/DiagnosticsChannel.md index 099c072f6c6..cb0421d84d4 100644 --- a/docs/docs/api/DiagnosticsChannel.md +++ b/docs/docs/api/DiagnosticsChannel.md @@ -20,8 +20,6 @@ diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => { console.log('method', request.method) console.log('path', request.path) console.log('headers') // array of strings, e.g: ['foo', 'bar'] - request.addHeader('hello', 'world') - console.log('headers', request.headers) // e.g. ['foo', 'bar', 'hello', 'world'] }) ``` diff --git a/lib/core/diagnostics.js b/lib/core/diagnostics.js index e1af3db6112..7dfd862c1fc 100644 --- a/lib/core/diagnostics.js +++ b/lib/core/diagnostics.js @@ -15,7 +15,7 @@ const channels = { // Request create: diagnosticsChannel.channel('undici:request:create'), bodySent: diagnosticsChannel.channel('undici:request:bodySent'), - headers: diagnosticsChannel.channel('undici:request:headers'), + response: diagnosticsChannel.channel('undici:request:response'), trailers: diagnosticsChannel.channel('undici:request:trailers'), error: diagnosticsChannel.channel('undici:request:error'), // WebSocket diff --git a/lib/core/request.js b/lib/core/request.js index 37839d3c949..618c08d5279 100644 --- a/lib/core/request.js +++ b/lib/core/request.js @@ -25,6 +25,7 @@ const { headerNameLowerCasedRecord } = require('./constants') const invalidPathRegex = /[^\u0021-\u00ff]/ const kHandler = Symbol('handler') +const kRequest = Symbol('request') class Request { constructor (origin, { @@ -152,6 +153,14 @@ class Request { this.headers = [] + this[kRequest] = { + get method () { return this.method }, + get origin () { return this.origin }, + get path () { return this.path }, + get headers () { return this.headers }, + get completed () { return this.completed } + } + // Only for H2 this.expectContinue = expectContinue != null ? expectContinue : false @@ -187,7 +196,9 @@ class Request { this[kHandler] = handler if (channels.create.hasSubscribers) { - channels.create.publish({ request: this }) + channels.create.publish({ + request: this[kRequest] + }) } } @@ -203,7 +214,9 @@ class Request { onRequestSent () { if (channels.bodySent.hasSubscribers) { - channels.bodySent.publish({ request: this }) + channels.bodySent.publish({ + request: this[kRequest] + }) } if (this[kHandler].onRequestSent) { @@ -235,8 +248,15 @@ class Request { assert(!this.aborted) assert(!this.completed) - if (channels.headers.hasSubscribers) { - channels.headers.publish({ request: this, response: { statusCode, headers, statusText } }) + if (channels.response.hasSubscribers) { + channels.response.publish({ + request: this[kRequest], + response: { + statusCode, + headers, + statusText + } + }) } try { @@ -272,7 +292,7 @@ class Request { this.completed = true if (channels.trailers.hasSubscribers) { - channels.trailers.publish({ request: this, trailers }) + channels.trailers.publish({ request: this[kRequest], trailers }) } try { @@ -287,7 +307,10 @@ class Request { this.onFinally() if (channels.error.hasSubscribers) { - channels.error.publish({ request: this, error }) + channels.error.publish({ + request: this[kRequest], + error + }) } if (this.aborted) { @@ -309,11 +332,6 @@ class Request { this.endHandler = null } } - - addHeader (key, value) { - processHeader(this, key, value) - return this - } } function processHeader (request, key, val) { diff --git a/types/diagnostics-channel.d.ts b/types/diagnostics-channel.d.ts index 85d44823978..d7f06a31d6f 100644 --- a/types/diagnostics-channel.d.ts +++ b/types/diagnostics-channel.d.ts @@ -10,7 +10,6 @@ declare namespace DiagnosticsChannel { method?: Dispatcher.HttpMethod; path: string; headers: string; - addHeader(key: string, value: string): Request; } interface Response { statusCode: number;