Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2c069a

Browse files
committed
http: add diagnostics channel for http client
1 parent 42ad967 commit e2c069a

File tree

3 files changed

+112
-6
lines changed

3 files changed

+112
-6
lines changed

doc/api/diagnostics_channel.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pipeline. It is use to track subscribers and to publish messages when there
150150
are subscribers present. It exists as a separate object to avoid channel
151151
lookups at publish time, enabling very fast publish speeds and allowing
152152
for heavy use while incurring very minimal cost. Channels are created with
153-
[`diagnostics_channel.channel(name)`][], constructing a channel directly
153+
\[`diagnostics_channel.channel(name)`]\[], constructing a channel directly
154154
with `new Channel(name)` is not supported.
155155

156156
#### `channel.hasSubscribers`
@@ -236,7 +236,8 @@ added:
236236

237237
Register a message handler to subscribe to this channel. This message handler
238238
will be run synchronously whenever a message is published to the channel. Any
239-
errors thrown in the message handler will trigger an [`'uncaughtException'`][].
239+
errors thrown in the message handler will trigger an
240+
\[`'uncaughtException'`]\[].
240241

241242
```mjs
242243
import diagnostics_channel from 'node:diagnostics_channel';
@@ -277,7 +278,7 @@ changes:
277278
* Returns: {boolean} `true` if the handler was found, `false` otherwise.
278279

279280
Remove a message handler previously registered to this channel with
280-
[`channel.subscribe(onMessage)`][].
281+
\[`channel.subscribe(onMessage)`]\[].
281282

282283
```mjs
283284
import diagnostics_channel from 'node:diagnostics_channel';
@@ -307,6 +308,33 @@ channel.subscribe(onMessage);
307308
channel.unsubscribe(onMessage);
308309
```
309310

310-
[`'uncaughtException'`]: process.md#event-uncaughtexception
311-
[`channel.subscribe(onMessage)`]: #channelsubscribeonmessage
312-
[`diagnostics_channel.channel(name)`]: #diagnostics_channelchannelname
311+
### Built-in Channels
312+
313+
#### HTTP
314+
315+
`http.client.request.start`: Emited with arguments `request` when client
316+
starts a request.
317+
318+
* `request` {http.ClientRequest}
319+
320+
`http.client.response.finish`: Emited with arguments `request`, `response`
321+
when client receives a response.
322+
323+
* `request` {http.ClientRequest}
324+
* `response` {http.IncomingMessage}
325+
326+
`http.server.request.start`: Emited with arguments `request`, `response`,
327+
`socket`, `server` when server receives a request.
328+
329+
* `request` {http.IncomingMessage}
330+
* `response` {http.ServerResponse}
331+
* `socket` {net.Socket}
332+
* `server` {http.Server}
333+
334+
`http.server.response.finish`: Emited with arguments `request`, `response`,
335+
`socket`, `server` when server sends a response.
336+
337+
* `request` {http.IncomingMessage}
338+
* `response` {http.ServerResponse}
339+
* `socket` {net.Socket}
340+
* `server` {http.Server}

lib/_http_client.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ const {
9090

9191
const kClientRequestStatistics = Symbol('ClientRequestStatistics');
9292

93+
const dc = require('diagnostics_channel');
94+
const onClientRequestStartChannel = dc.channel('http.client.request.start');
95+
const onClientResponseFinishChannel = dc.channel('http.client.response.finish');
96+
9397
const { addAbortSignal, finished } = require('stream');
9498

9599
let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
@@ -367,6 +371,11 @@ ClientRequest.prototype._finish = function _finish() {
367371
},
368372
});
369373
}
374+
if (onClientRequestStartChannel.hasSubscribers) {
375+
onClientRequestStartChannel.publish({
376+
request: this,
377+
});
378+
}
370379
};
371380

372381
ClientRequest.prototype._implicitHeader = function _implicitHeader() {
@@ -645,6 +654,12 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
645654
},
646655
});
647656
}
657+
if (onClientResponseFinishChannel.hasSubscribers) {
658+
onClientResponseFinishChannel.publish({
659+
request: req,
660+
response: res,
661+
});
662+
}
648663
req.res = res;
649664
res.req = req;
650665

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
const net = require('net');
6+
const dc = require('diagnostics_channel');
7+
8+
const onClientRequestStart = dc.channel('http.client.request.start');
9+
const onClientResponseFinish = dc.channel('http.client.response.finish');
10+
const onServerRequestStart = dc.channel('http.server.request.start');
11+
const onServerResponseFinish = dc.channel('http.server.response.finish');
12+
13+
const isHTTPServer = (server) => server instanceof http.Server;
14+
const isIncomingMessage = (object) => object instanceof http.IncomingMessage;
15+
const isOutgoingMessage = (object) => object instanceof http.OutgoingMessage;
16+
const isNetSocket = (socket) => socket instanceof net.Socket;
17+
18+
onClientRequestStart.subscribe(common.mustCall(({ request }) => {
19+
assert.strictEqual(isOutgoingMessage(request), true);
20+
}));
21+
22+
onClientResponseFinish.subscribe(common.mustCall(({ request, response }) => {
23+
assert.strictEqual(isOutgoingMessage(request), true);
24+
assert.strictEqual(isIncomingMessage(response), true);
25+
}));
26+
27+
onServerRequestStart.subscribe(common.mustCall(({
28+
request,
29+
response,
30+
socket,
31+
server,
32+
}) => {
33+
assert.strictEqual(isIncomingMessage(request), true);
34+
assert.strictEqual(isOutgoingMessage(response), true);
35+
assert.strictEqual(isNetSocket(socket), true);
36+
assert.strictEqual(isHTTPServer(server), true);
37+
}));
38+
39+
onServerResponseFinish.subscribe(common.mustCall(({
40+
request,
41+
response,
42+
socket,
43+
server,
44+
}) => {
45+
assert.strictEqual(isIncomingMessage(request), true);
46+
assert.strictEqual(isOutgoingMessage(response), true);
47+
assert.strictEqual(isNetSocket(socket), true);
48+
assert.strictEqual(isHTTPServer(server), true);
49+
}));
50+
51+
const server = http.createServer(common.mustCall((req, res) => {
52+
res.end('done');
53+
}));
54+
55+
server.listen(() => {
56+
const { port } = server.address();
57+
http.get(`http://localhost:${port}`, (res) => {
58+
res.resume();
59+
res.on('end', () => {
60+
server.close();
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)