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 1d902e9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
2 changes: 0 additions & 2 deletions docs/docs/api/DiagnosticsChannel.md
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
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
45 changes: 34 additions & 11 deletions lib/core/request.js
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,8 @@ class Request {

this.headers = []

this[kRequest] = null

// Only for H2
this.expectContinue = expectContinue != null ? expectContinue : false

Expand Down Expand Up @@ -187,8 +190,21 @@ class Request {
this[kHandler] = handler

if (channels.create.hasSubscribers) {
channels.create.publish({ request: this })
channels.create.publish({
request: this._getRequest()
})
}
}

_getRequest () {
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 }
}
return this[kRequest]
}

onBodySent (chunk) {
Expand All @@ -203,7 +219,9 @@ class Request {

onRequestSent () {
if (channels.bodySent.hasSubscribers) {
channels.bodySent.publish({ request: this })
channels.bodySent.publish({
request: this._getRequest()
})
}

if (this[kHandler].onRequestSent) {
Expand Down Expand Up @@ -235,8 +253,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._getRequest(),
response: {
statusCode,
headers,
statusText
}
})
}

try {
Expand Down Expand Up @@ -272,7 +297,7 @@ class Request {

this.completed = true
if (channels.trailers.hasSubscribers) {
channels.trailers.publish({ request: this, trailers })
channels.trailers.publish({ request: this._getRequest(), trailers })
}

try {
Expand All @@ -287,7 +312,10 @@ class Request {
this.onFinally()

if (channels.error.hasSubscribers) {
channels.error.publish({ request: this, error })
channels.error.publish({
request: this._getRequest(),
error
})
}

if (this.aborted) {
Expand All @@ -309,11 +337,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
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 1d902e9

Please sign in to comment.