GeezerVison is showing github.com — Revert "fix: don't leak internal class (#3024)" · nodejs/undici@c346b66
File treeExpand file treeCollapse file tree Open diff view settings test/node-test/diagnostics-channel
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/docs/api/DiagnosticsChannel.md +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,8 @@ diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => { | 20 | 20 | console.log('method', request.method) | 21 | 21 | console.log('path', request.path) | 22 | 22 | console.log('headers') // array of strings, e.g: ['foo', 'bar'] | | 23 | + request.addHeader('hello', 'world') | | 24 | + console.log('headers', request.headers) // e.g. ['foo', 'bar', 'hello', 'world'] | 23 | 25 | }) | 24 | 26 | ``` | 25 | 27 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/core/request.js +10-29Lines changed: 10 additions & 29 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -91,8 +91,6 @@ class Request { | 91 | 91 |
| 92 | 92 | this.abort = null | 93 | 93 |
| 94 | | - this.publicInterface = null | 95 | | - | 96 | 94 | if (body == null) { | 97 | 95 | this.body = null | 98 | 96 | } else if (isStream(body)) { | @@ -189,32 +187,10 @@ class Request { | 189 | 187 | this[kHandler] = handler | 190 | 188 |
| 191 | 189 | if (channels.create.hasSubscribers) { | 192 | | - channels.create.publish({ request: this.getPublicInterface() }) | | 190 | + channels.create.publish({ request: this }) | 193 | 191 | } | 194 | 192 | } | 195 | 193 |
| 196 | | - getPublicInterface () { | 197 | | - const self = this | 198 | | - this.publicInterface ??= { | 199 | | - get origin () { | 200 | | - return self.origin | 201 | | - }, | 202 | | - get method () { | 203 | | - return self.method | 204 | | - }, | 205 | | - get path () { | 206 | | - return self.path | 207 | | - }, | 208 | | - get headers () { | 209 | | - return self.headers | 210 | | - }, | 211 | | - get completed () { | 212 | | - return self.completed | 213 | | - } | 214 | | - } | 215 | | - return this.publicInterface | 216 | | - } | 217 | | - | 218 | 194 | onBodySent (chunk) { | 219 | 195 | if (this[kHandler].onBodySent) { | 220 | 196 | try { | @@ -227,7 +203,7 @@ class Request { | 227 | 203 |
| 228 | 204 | onRequestSent () { | 229 | 205 | if (channels.bodySent.hasSubscribers) { | 230 | | - channels.bodySent.publish({ request: this.getPublicInterface() }) | | 206 | + channels.bodySent.publish({ request: this }) | 231 | 207 | } | 232 | 208 |
| 233 | 209 | if (this[kHandler].onRequestSent) { | @@ -260,7 +236,7 @@ class Request { | 260 | 236 | assert(!this.completed) | 261 | 237 |
| 262 | 238 | if (channels.headers.hasSubscribers) { | 263 | | - channels.headers.publish({ request: this.getPublicInterface(), response: { statusCode, headers, statusText } }) | | 239 | + channels.headers.publish({ request: this, response: { statusCode, headers, statusText } }) | 264 | 240 | } | 265 | 241 |
| 266 | 242 | try { | @@ -296,7 +272,7 @@ class Request { | 296 | 272 |
| 297 | 273 | this.completed = true | 298 | 274 | if (channels.trailers.hasSubscribers) { | 299 | | - channels.trailers.publish({ request: this.getPublicInterface(), trailers }) | | 275 | + channels.trailers.publish({ request: this, trailers }) | 300 | 276 | } | 301 | 277 |
| 302 | 278 | try { | @@ -311,7 +287,7 @@ class Request { | 311 | 287 | this.onFinally() | 312 | 288 |
| 313 | 289 | if (channels.error.hasSubscribers) { | 314 | | - channels.error.publish({ request: this.getPublicInterface(), error }) | | 290 | + channels.error.publish({ request: this, error }) | 315 | 291 | } | 316 | 292 |
| 317 | 293 | if (this.aborted) { | @@ -333,6 +309,11 @@ class Request { | 333 | 309 | this.endHandler = null | 334 | 310 | } | 335 | 311 | } | | 312 | + | | 313 | + addHeader (key, value) { | | 314 | + processHeader(this, key, value) | | 315 | + return this | | 316 | + } | 336 | 317 | } | 337 | 318 |
| 338 | 319 | function processHeader (request, key, val) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/dispatcher/client-h1.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -993,7 +993,7 @@ function writeH1 (client, request) { | 993 | 993 | } | 994 | 994 |
| 995 | 995 | if (channels.sendHeaders.hasSubscribers) { | 996 | | - channels.sendHeaders.publish({ request: request.getPublicInterface(), headers: header, socket }) | | 996 | + channels.sendHeaders.publish({ request, headers: header, socket }) | 997 | 997 | } | 998 | 998 |
| 999 | 999 | /* istanbul ignore else: assertion */ | |
Collapse file Copy file name to clipboardExpand all lines: test/node-test/diagnostics-channel/get.js +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,7 +7,7 @@ const { Client } = require('../../..') | 7 | 7 | const { createServer } = require('node:http') | 8 | 8 |
| 9 | 9 | test('Diagnostics channel - get', (t) => { | 10 | | - const assert = tspl(t, { plan: 31 }) | | 10 | + const assert = tspl(t, { plan: 32 }) | 11 | 11 | const server = createServer((req, res) => { | 12 | 12 | res.setHeader('Content-Type', 'text/plain') | 13 | 13 | res.setHeader('trailer', 'foo') | @@ -33,6 +33,8 @@ test('Diagnostics channel - get', (t) => { | 33 | 33 | assert.equal(request.method, 'GET') | 34 | 34 | assert.equal(request.path, '/') | 35 | 35 | assert.deepStrictEqual(request.headers, ['bar', 'bar']) | | 36 | + request.addHeader('hello', 'world') | | 37 | + assert.deepStrictEqual(request.headers, ['bar', 'bar', 'hello', 'world']) | 36 | 38 | }) | 37 | 39 |
| 38 | 40 | let _connector | @@ -75,7 +77,8 @@ test('Diagnostics channel - get', (t) => { | 75 | 77 | 'GET / HTTP/1.1', | 76 | 78 | `host: localhost:${server.address().port}`, | 77 | 79 | 'connection: keep-alive', | 78 | | - 'bar: bar' | | 80 | + 'bar: bar', | | 81 | + 'hello: world' | 79 | 82 | ] | 80 | 83 |
| 81 | 84 | assert.deepStrictEqual(headers, expectedHeaders.join('\r\n') + '\r\n') | |
Collapse file Copy file name to clipboardExpand all lines: test/node-test/diagnostics-channel/post-stream.js +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,7 +8,7 @@ const { Client } = require('../../..') | 8 | 8 | const { createServer } = require('node:http') | 9 | 9 |
| 10 | 10 | test('Diagnostics channel - post stream', (t) => { | 11 | | - const assert = tspl(t, { plan: 31 }) | | 11 | + const assert = tspl(t, { plan: 33 }) | 12 | 12 | const server = createServer((req, res) => { | 13 | 13 | req.resume() | 14 | 14 | res.setHeader('Content-Type', 'text/plain') | @@ -34,6 +34,9 @@ test('Diagnostics channel - post stream', (t) => { | 34 | 34 | assert.equal(request.method, 'POST') | 35 | 35 | assert.equal(request.path, '/') | 36 | 36 | assert.deepStrictEqual(request.headers, ['bar', 'bar']) | | 37 | + request.addHeader('hello', 'world') | | 38 | + assert.deepStrictEqual(request.headers, ['bar', 'bar', 'hello', 'world']) | | 39 | + assert.deepStrictEqual(request.body, body) | 37 | 40 | }) | 38 | 41 |
| 39 | 42 | let _connector | @@ -76,7 +79,8 @@ test('Diagnostics channel - post stream', (t) => { | 76 | 79 | 'POST / HTTP/1.1', | 77 | 80 | `host: localhost:${server.address().port}`, | 78 | 81 | 'connection: keep-alive', | 79 | | - 'bar: bar' | | 82 | + 'bar: bar', | | 83 | + 'hello: world' | 80 | 84 | ] | 81 | 85 |
| 82 | 86 | assert.equal(headers, expectedHeaders.join('\r\n') + '\r\n') | |
Collapse file Copy file name to clipboardExpand all lines: test/node-test/diagnostics-channel/post.js +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,7 +7,7 @@ const { Client } = require('../../../') | 7 | 7 | const { createServer } = require('node:http') | 8 | 8 |
| 9 | 9 | test('Diagnostics channel - post', (t) => { | 10 | | - const assert = tspl(t, { plan: 31 }) | | 10 | + const assert = tspl(t, { plan: 33 }) | 11 | 11 | const server = createServer((req, res) => { | 12 | 12 | req.resume() | 13 | 13 | res.setHeader('Content-Type', 'text/plain') | @@ -32,6 +32,9 @@ test('Diagnostics channel - post', (t) => { | 32 | 32 | assert.equal(request.method, 'POST') | 33 | 33 | assert.equal(request.path, '/') | 34 | 34 | assert.deepStrictEqual(request.headers, ['bar', 'bar']) | | 35 | + request.addHeader('hello', 'world') | | 36 | + assert.deepStrictEqual(request.headers, ['bar', 'bar', 'hello', 'world']) | | 37 | + assert.deepStrictEqual(request.body, Buffer.from('hello world')) | 35 | 38 | }) | 36 | 39 |
| 37 | 40 | let _connector | @@ -74,7 +77,8 @@ test('Diagnostics channel - post', (t) => { | 74 | 77 | 'POST / HTTP/1.1', | 75 | 78 | `host: localhost:${server.address().port}`, | 76 | 79 | 'connection: keep-alive', | 77 | | - 'bar: bar' | | 80 | + 'bar: bar', | | 81 | + 'hello: world' | 78 | 82 | ] | 79 | 83 |
| 80 | 84 | assert.deepStrictEqual(headers, expectedHeaders.join('\r\n') + '\r\n') | |
|