mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 11:41:22 +00:00

PR-URL: https://github.com/nodejs/node/pull/34895 Reviewed-By: Bryan English <bryan@bryanenglish.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
66 lines
1.5 KiB
JavaScript
66 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const { AsyncLocalStorage } = require('async_hooks');
|
|
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) => {
|
|
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) => {
|
|
const data = {
|
|
request,
|
|
response,
|
|
server,
|
|
socket: request.socket
|
|
};
|
|
|
|
// Context is maintained
|
|
compare(als.getStore(), context);
|
|
|
|
compare(context, data);
|
|
compare(message, data);
|
|
}));
|
|
|
|
let request;
|
|
let response;
|
|
|
|
const server = http.createServer(common.mustCall((req, res) => {
|
|
request = req;
|
|
response = res;
|
|
|
|
setTimeout(() => {
|
|
res.end('done');
|
|
}, 1);
|
|
}));
|
|
|
|
server.listen(() => {
|
|
const { port } = server.address();
|
|
http.get(`http://localhost:${port}`, (res) => {
|
|
res.resume();
|
|
res.on('end', () => {
|
|
server.close();
|
|
});
|
|
});
|
|
});
|
|
|
|
function compare(a, b) {
|
|
assert.strictEqual(a.request, b.request);
|
|
assert.strictEqual(a.response, b.response);
|
|
assert.strictEqual(a.socket, b.socket);
|
|
assert.strictEqual(a.server, b.server);
|
|
}
|