Skip to content

Commit

Permalink
fix: don't leak internal class (#3024)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag authored Apr 1, 2024
1 parent d7f10e1 commit 2d5cbdf
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 30 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
39 changes: 29 additions & 10 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class Request {

this.abort = null

this.publicInterface = null

if (body == null) {
this.body = null
} else if (isStream(body)) {
Expand Down Expand Up @@ -187,10 +189,32 @@ class Request {
this[kHandler] = handler

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

getPublicInterface () {
const self = this
this.publicInterface ??= {
get origin () {
return self.origin
},
get method () {
return self.method
},
get path () {
return self.path
},
get headers () {
return self.headers
},
get completed () {
return self.completed
}
}
return this.publicInterface
}

onBodySent (chunk) {
if (this[kHandler].onBodySent) {
try {
Expand All @@ -203,7 +227,7 @@ class Request {

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

if (this[kHandler].onRequestSent) {
Expand Down Expand Up @@ -236,7 +260,7 @@ class Request {
assert(!this.completed)

if (channels.headers.hasSubscribers) {
channels.headers.publish({ request: this, response: { statusCode, headers, statusText } })
channels.headers.publish({ request: this.getPublicInterface(), response: { statusCode, headers, statusText } })
}

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

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

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

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

if (this.aborted) {
Expand All @@ -309,11 +333,6 @@ class Request {
this.endHandler = null
}
}

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

function processHeader (request, key, val) {
Expand Down
2 changes: 1 addition & 1 deletion lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ function writeH1 (client, request) {
}

if (channels.sendHeaders.hasSubscribers) {
channels.sendHeaders.publish({ request, headers: header, socket })
channels.sendHeaders.publish({ request: request.getPublicInterface(), headers: header, socket })
}

/* istanbul ignore else: assertion */
Expand Down
7 changes: 2 additions & 5 deletions test/node-test/diagnostics-channel/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { Client } = require('../../..')
const { createServer } = require('node:http')

test('Diagnostics channel - get', (t) => {
const assert = tspl(t, { plan: 32 })
const assert = tspl(t, { plan: 31 })
const server = createServer((req, res) => {
res.setHeader('Content-Type', 'text/plain')
res.setHeader('trailer', 'foo')
Expand All @@ -33,8 +33,6 @@ test('Diagnostics channel - get', (t) => {
assert.equal(request.method, 'GET')
assert.equal(request.path, '/')
assert.deepStrictEqual(request.headers, ['bar', 'bar'])
request.addHeader('hello', 'world')
assert.deepStrictEqual(request.headers, ['bar', 'bar', 'hello', 'world'])
})

let _connector
Expand Down Expand Up @@ -77,8 +75,7 @@ test('Diagnostics channel - get', (t) => {
'GET / HTTP/1.1',
`host: localhost:${server.address().port}`,
'connection: keep-alive',
'bar: bar',
'hello: world'
'bar: bar'
]

assert.deepStrictEqual(headers, expectedHeaders.join('\r\n') + '\r\n')
Expand Down
8 changes: 2 additions & 6 deletions test/node-test/diagnostics-channel/post-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { Client } = require('../../..')
const { createServer } = require('node:http')

test('Diagnostics channel - post stream', (t) => {
const assert = tspl(t, { plan: 33 })
const assert = tspl(t, { plan: 31 })
const server = createServer((req, res) => {
req.resume()
res.setHeader('Content-Type', 'text/plain')
Expand All @@ -34,9 +34,6 @@ test('Diagnostics channel - post stream', (t) => {
assert.equal(request.method, 'POST')
assert.equal(request.path, '/')
assert.deepStrictEqual(request.headers, ['bar', 'bar'])
request.addHeader('hello', 'world')
assert.deepStrictEqual(request.headers, ['bar', 'bar', 'hello', 'world'])
assert.deepStrictEqual(request.body, body)
})

let _connector
Expand Down Expand Up @@ -79,8 +76,7 @@ test('Diagnostics channel - post stream', (t) => {
'POST / HTTP/1.1',
`host: localhost:${server.address().port}`,
'connection: keep-alive',
'bar: bar',
'hello: world'
'bar: bar'
]

assert.equal(headers, expectedHeaders.join('\r\n') + '\r\n')
Expand Down
8 changes: 2 additions & 6 deletions test/node-test/diagnostics-channel/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { Client } = require('../../../')
const { createServer } = require('node:http')

test('Diagnostics channel - post', (t) => {
const assert = tspl(t, { plan: 33 })
const assert = tspl(t, { plan: 31 })
const server = createServer((req, res) => {
req.resume()
res.setHeader('Content-Type', 'text/plain')
Expand All @@ -32,9 +32,6 @@ test('Diagnostics channel - post', (t) => {
assert.equal(request.method, 'POST')
assert.equal(request.path, '/')
assert.deepStrictEqual(request.headers, ['bar', 'bar'])
request.addHeader('hello', 'world')
assert.deepStrictEqual(request.headers, ['bar', 'bar', 'hello', 'world'])
assert.deepStrictEqual(request.body, Buffer.from('hello world'))
})

let _connector
Expand Down Expand Up @@ -77,8 +74,7 @@ test('Diagnostics channel - post', (t) => {
'POST / HTTP/1.1',
`host: localhost:${server.address().port}`,
'connection: keep-alive',
'bar: bar',
'hello: world'
'bar: bar'
]

assert.deepStrictEqual(headers, expectedHeaders.join('\r\n') + '\r\n')
Expand Down

0 comments on commit 2d5cbdf

Please sign in to comment.