Skip to content

Commit

Permalink
fix: don't leak internal class
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Mar 31, 2024
1 parent 7485cd9 commit 3b69310
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
2 changes: 0 additions & 2 deletions docs/docs/api/DiagnosticsChannel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']
})
```

Expand Down
2 changes: 1 addition & 1 deletion lib/core/diagnostics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 29 additions & 11 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const { headerNameLowerCasedRecord } = require('./constants')
const invalidPathRegex = /[^\u0021-\u00ff]/

const kHandler = Symbol('handler')
const kRequest = Symbol('request')

class Request {
constructor (origin, {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -187,7 +196,9 @@ class Request {
this[kHandler] = handler

if (channels.create.hasSubscribers) {
channels.create.publish({ request: this })
channels.create.publish({
request: this[kRequest]
})
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -309,11 +332,6 @@ class Request {
this.endHandler = null
}
}

addHeader (key, value) {
processHeader(this, key, value)
return this
}
}

function processHeader (request, key, val) {
Expand Down
1 change: 0 additions & 1 deletion types/diagnostics-channel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ declare namespace DiagnosticsChannel {
method?: Dispatcher.HttpMethod;
path: string;
headers: string;
addHeader(key: string, value: string): Request;
}
interface Response {
statusCode: number;
Expand Down

0 comments on commit 3b69310

Please sign in to comment.