Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8d0526f

Browse filesBrowse files
marco-ippolitoaduh95
authored andcommitted
http: add diagnostic channel http.server.response.created
PR-URL: #55622 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
1 parent 6662752 commit 8d0526f
Copy full SHA for 8d0526f

File tree

Expand file treeCollapse file tree

4 files changed

+68
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+68
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/diagnostics_channel.md‎

Copy file name to clipboardExpand all lines: doc/api/diagnostics_channel.md
+8Lines changed: 8 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,14 @@ Emitted when client receives a response.
11591159

11601160
Emitted when server receives a request.
11611161

1162+
`http.server.response.created`
1163+
1164+
* `request` {http.IncomingMessage}
1165+
* `response` {http.ServerResponse}
1166+
1167+
Emitted when server creates a response.
1168+
The event is emitted before the response is sent.
1169+
11621170
`http.server.response.finish`
11631171

11641172
* `request` {http.IncomingMessage}
Collapse file

‎lib/_http_server.js‎

Copy file name to clipboardExpand all lines: lib/_http_server.js
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
9797

9898
const dc = require('diagnostics_channel');
9999
const onRequestStartChannel = dc.channel('http.server.request.start');
100+
const onResponseCreatedChannel = dc.channel('http.server.response.created');
100101
const onResponseFinishChannel = dc.channel('http.server.response.finish');
101102

102103
const kServerResponse = Symbol('ServerResponse');
@@ -224,6 +225,12 @@ function ServerResponse(req, options) {
224225
this._traceEventId = getNextTraceEventId();
225226
traceBegin(HTTP_SERVER_TRACE_EVENT_NAME, this._traceEventId);
226227
}
228+
if (onResponseCreatedChannel.hasSubscribers) {
229+
onResponseCreatedChannel.publish({
230+
request: req,
231+
response: this,
232+
});
233+
}
227234
}
228235
ObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);
229236
ObjectSetPrototypeOf(ServerResponse, OutgoingMessage);
Collapse file
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
const dc = require('diagnostics_channel');
6+
7+
const isOutgoingMessage = (object) => object instanceof http.OutgoingMessage;
8+
const isIncomingMessage = (object) => object instanceof http.IncomingMessage;
9+
10+
dc.subscribe('http.server.response.created', common.mustCall(({
11+
request,
12+
response,
13+
}) => {
14+
assert.strictEqual(request.headers.foo, 'bar');
15+
assert.strictEqual(response.getHeader('baz'), undefined);
16+
assert.strictEqual(isIncomingMessage(request), true);
17+
assert.strictEqual(isOutgoingMessage(response), true);
18+
}));
19+
20+
dc.subscribe('http.server.response.finish', common.mustCall(({
21+
request,
22+
response,
23+
}) => {
24+
assert.strictEqual(request.headers.foo, 'bar');
25+
assert.strictEqual(response.getHeader('baz'), 'bar');
26+
assert.strictEqual(isIncomingMessage(request), true);
27+
assert.strictEqual(isOutgoingMessage(response), true);
28+
}));
29+
30+
const server = http.createServer(common.mustCall((_, res) => {
31+
res.setHeader('baz', 'bar');
32+
res.end('done');
33+
}));
34+
35+
server.listen(() => {
36+
const { port } = server.address();
37+
http.get({
38+
port,
39+
headers: {
40+
'foo': 'bar',
41+
}
42+
}, common.mustCall(() => {
43+
server.close();
44+
}));
45+
});
Collapse file

‎test/parallel/test-diagnostics-channel-http.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-diagnostics-channel-http.js
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ dc.subscribe('http.server.response.finish', common.mustCall(({
5353
assert.strictEqual(isHTTPServer(server), true);
5454
}));
5555

56+
dc.subscribe('http.server.response.created', common.mustCall(({
57+
request,
58+
response,
59+
}) => {
60+
assert.strictEqual(isIncomingMessage(request), true);
61+
assert.strictEqual(isOutgoingMessage(response), true);
62+
}));
63+
5664
dc.subscribe('http.client.request.created', common.mustCall(({ request }) => {
5765
assert.strictEqual(isOutgoingMessage(request), true);
5866
assert.strictEqual(isHTTPServer(server), true);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.