|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('node:assert') |
| 4 | +const { AsyncLocalStorage } = require('node:async_hooks') |
| 5 | +const diagnosticsChannel = require('node:diagnostics_channel') |
| 6 | +const { once } = require('node:events') |
| 7 | +const { readFileSync } = require('node:fs') |
| 8 | +const { createServer } = require('node:http') |
| 9 | +const { createSecureServer } = require('node:http2') |
| 10 | +const { createServer: createHttpsServer } = require('node:https') |
| 11 | +const { join } = require('node:path') |
| 12 | +const { test } = require('node:test') |
| 13 | + |
| 14 | +const { Agent, Pool, fetch, request } = require('..') |
| 15 | + |
| 16 | +const key = readFileSync(join(__dirname, 'fixtures', 'key.pem'), 'utf8') |
| 17 | +const cert = readFileSync(join(__dirname, 'fixtures', 'cert.pem'), 'utf8') |
| 18 | + |
| 19 | +function collectContexts (t, storage) { |
| 20 | + const contexts = {} |
| 21 | + const onCreate = ({ request }) => { |
| 22 | + contexts[request.path] = storage.getStore() |
| 23 | + } |
| 24 | + diagnosticsChannel.subscribe('undici:request:create', onCreate) |
| 25 | + t.after(() => diagnosticsChannel.unsubscribe('undici:request:create', onCreate)) |
| 26 | + return contexts |
| 27 | +} |
| 28 | + |
| 29 | +async function listen (t, server) { |
| 30 | + server.listen(0) |
| 31 | + await once(server, 'listening') |
| 32 | + t.after(() => server.close()) |
| 33 | + return server.address().port |
| 34 | +} |
| 35 | + |
| 36 | +const expected = { '/a': 'a', '/b': 'b', '/c': 'c' } |
| 37 | + |
| 38 | +// https://github.com/nodejs/undici/issues/5981 |
| 39 | +for (const [name, createTlsServer] of [ |
| 40 | + ['h1', () => createHttpsServer({ key, cert }, (req, res) => res.end('ok'))], |
| 41 | + ['h2', () => createSecureServer({ key, cert, allowHTTP1: true }, (req, res) => res.end('ok'))] |
| 42 | +]) { |
| 43 | + test(`requests queued during ALPN negotiation keep their async context (${name})`, async (t) => { |
| 44 | + const storage = new AsyncLocalStorage() |
| 45 | + const contexts = collectContexts(t, storage) |
| 46 | + const port = await listen(t, createTlsServer()) |
| 47 | + |
| 48 | + const dispatcher = new Agent({ connect: { rejectUnauthorized: false } }) |
| 49 | + t.after(() => dispatcher.close()) |
| 50 | + |
| 51 | + await Promise.all(['a', 'b', 'c'].map((name) => storage.run(name, async () => { |
| 52 | + const res = await fetch(`https://localhost:${port}/${name}`, { dispatcher }) |
| 53 | + await res.text() |
| 54 | + }))) |
| 55 | + |
| 56 | + assert.deepStrictEqual(contexts, expected) |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +test('requests queued in a Pool keep their async context', async (t) => { |
| 61 | + const storage = new AsyncLocalStorage() |
| 62 | + const contexts = collectContexts(t, storage) |
| 63 | + const port = await listen(t, createServer((req, res) => res.end('ok'))) |
| 64 | + |
| 65 | + const pool = new Pool(`http://localhost:${port}`, { connections: 1 }) |
| 66 | + t.after(() => pool.close()) |
| 67 | + |
| 68 | + await Promise.all(['a', 'b', 'c'].map((name) => storage.run(name, async () => { |
| 69 | + const { body } = await request(`http://localhost:${port}/${name}`, { dispatcher: pool }) |
| 70 | + await body.dump() |
| 71 | + }))) |
| 72 | + |
| 73 | + assert.deepStrictEqual(contexts, expected) |
| 74 | +}) |
0 commit comments