lib: fix diagnostics channel
PR-URL: https://github.com/nodejs/node/pull/44154 Fixes: https://github.com/nodejs/node/issues/44143 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
This commit is contained in:
parent
f136e7ec31
commit
fa22337901
@ -6,21 +6,18 @@ const dc = require('diagnostics_channel');
|
||||
const assert = require('assert');
|
||||
const http = require('http');
|
||||
|
||||
const incomingStartChannel = dc.channel('http.server.request.start');
|
||||
const outgoingFinishChannel = dc.channel('http.server.response.finish');
|
||||
|
||||
const als = new AsyncLocalStorage();
|
||||
let context;
|
||||
|
||||
// Bind requests to an AsyncLocalStorage context
|
||||
incomingStartChannel.subscribe(common.mustCall((message) => {
|
||||
dc.subscribe('http.server.request.start', common.mustCall((message) => {
|
||||
als.enterWith(message);
|
||||
context = message;
|
||||
}));
|
||||
|
||||
// When the request ends, verify the context has been maintained
|
||||
// and that the messages contain the expected data
|
||||
outgoingFinishChannel.subscribe(common.mustCall((message) => {
|
||||
dc.subscribe('http.server.response.finish', common.mustCall((message) => {
|
||||
const data = {
|
||||
request,
|
||||
response,
|
||||
|
@ -5,26 +5,24 @@ const http = require('http');
|
||||
const net = require('net');
|
||||
const dc = require('diagnostics_channel');
|
||||
|
||||
const onClientRequestStart = dc.channel('http.client.request.start');
|
||||
const onClientResponseFinish = dc.channel('http.client.response.finish');
|
||||
const onServerRequestStart = dc.channel('http.server.request.start');
|
||||
const onServerResponseFinish = dc.channel('http.server.response.finish');
|
||||
|
||||
const isHTTPServer = (server) => server instanceof http.Server;
|
||||
const isIncomingMessage = (object) => object instanceof http.IncomingMessage;
|
||||
const isOutgoingMessage = (object) => object instanceof http.OutgoingMessage;
|
||||
const isNetSocket = (socket) => socket instanceof net.Socket;
|
||||
|
||||
onClientRequestStart.subscribe(common.mustCall(({ request }) => {
|
||||
dc.subscribe('http.client.request.start', common.mustCall(({ request }) => {
|
||||
assert.strictEqual(isOutgoingMessage(request), true);
|
||||
}));
|
||||
|
||||
onClientResponseFinish.subscribe(common.mustCall(({ request, response }) => {
|
||||
dc.subscribe('http.client.response.finish', common.mustCall(({
|
||||
request,
|
||||
response
|
||||
}) => {
|
||||
assert.strictEqual(isOutgoingMessage(request), true);
|
||||
assert.strictEqual(isIncomingMessage(response), true);
|
||||
}));
|
||||
|
||||
onServerRequestStart.subscribe(common.mustCall(({
|
||||
dc.subscribe('http.server.request.start', common.mustCall(({
|
||||
request,
|
||||
response,
|
||||
socket,
|
||||
@ -36,7 +34,7 @@ onServerRequestStart.subscribe(common.mustCall(({
|
||||
assert.strictEqual(isHTTPServer(server), true);
|
||||
}));
|
||||
|
||||
onServerResponseFinish.subscribe(common.mustCall(({
|
||||
dc.subscribe('http.server.response.finish', common.mustCall(({
|
||||
request,
|
||||
response,
|
||||
socket,
|
||||
|
Loading…
x
Reference in New Issue
Block a user