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 96b96bd
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const { headerNameLowerCasedRecord } = require('./constants')
const invalidPathRegex = /[^\u0021-\u00ff]/

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

class Request {
constructor (origin, {
Expand Down Expand Up @@ -152,6 +154,19 @@ class Request {

this.headers = []

this[kRequest] = {
method: this.method,
origin: this.origin,
path: this.path,
headers: this.headers
}
this[kResponse] = {
headers: null,
statusCode: 0,
statusText: '',
trailers: null
}

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

Expand Down Expand Up @@ -187,7 +202,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 +220,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 +254,15 @@ class Request {
assert(!this.aborted)
assert(!this.completed)

this[kResponse].statusCode = statusCode
this[kResponse].headers = headers
this[kResponse].statusText = statusText

if (channels.headers.hasSubscribers) {
channels.headers.publish({ request: this, response: { statusCode, headers, statusText } })
channels.headers.publish({
request: this[kRequest],
response: this[kResponse]
})
}

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

this.completed = true
if (channels.trailers.hasSubscribers) {
channels.trailers.publish({ request: this, trailers })
channels.trailers.publish({
request: this[kRequest],
response: this[kResponse] ??= {
trailers
}
})
}

try {
Expand All @@ -287,7 +318,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 Down

0 comments on commit 96b96bd

Please sign in to comment.