• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const assert = require('assert');
4const {
5  executionAsyncResource,
6  executionAsyncId,
7  createHook,
8} = require('async_hooks');
9const http = require('http');
10
11const hooked = {};
12createHook({
13  init(asyncId, type, triggerAsyncId, resource) {
14    hooked[asyncId] = resource;
15  },
16}).enable();
17
18const server = http.createServer((req, res) => {
19  res.write('hello');
20  setTimeout(() => {
21    res.end(' world!');
22  }, 1000);
23});
24
25server.listen(0, () => {
26  assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
27  http.get({ port: server.address().port }, (res) => {
28    assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
29    res.on('data', () => {
30      assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
31    });
32    res.on('end', () => {
33      assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
34      server.close();
35    });
36  });
37});
38