|
| 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 | +// The outgoing body chunk diagnostics channels let tools account for the actual |
| 8 | +// bytes being sent without Node paying to measure them when nobody is reading. |
| 9 | +// Consumers compute the byte length themselves from `data` and `encoding`. |
| 10 | +// Refs: https://github.com/nodejs/node/pull/66039 |
| 11 | + |
| 12 | +const requestBodyChunks = []; |
| 13 | +dc.subscribe('http.client.request.bodyChunkSent', ({ message, data, encoding }) => { |
| 14 | + assert.ok(message instanceof http.ClientRequest); |
| 15 | + requestBodyChunks.push({ data, encoding }); |
| 16 | +}); |
| 17 | + |
| 18 | +const responseBodyChunks = []; |
| 19 | +dc.subscribe('http.server.response.bodyChunkSent', ({ message, data, encoding }) => { |
| 20 | + assert.ok(message instanceof http.ServerResponse); |
| 21 | + responseBodyChunks.push({ data, encoding }); |
| 22 | +}); |
| 23 | + |
| 24 | +const requestBody = 'é'.repeat(50) + '漢'.repeat(20); |
| 25 | +const responseBody = 'é'.repeat(100) + '😀'.repeat(10); |
| 26 | + |
| 27 | +function byteLength(data, encoding) { |
| 28 | + return typeof data === 'string' ? Buffer.byteLength(data, encoding) : data.byteLength; |
| 29 | +} |
| 30 | + |
| 31 | +const server = http.createServer(common.mustCall((req, res) => { |
| 32 | + req.on('data', () => {}); |
| 33 | + req.on('end', common.mustCall(() => { |
| 34 | + res.write('é'.repeat(100)); |
| 35 | + res.end('😀'.repeat(10)); |
| 36 | + })); |
| 37 | +})); |
| 38 | + |
| 39 | +server.listen(0, common.mustCall(() => { |
| 40 | + const { port } = server.address(); |
| 41 | + |
| 42 | + const req = http.request({ |
| 43 | + port, |
| 44 | + method: 'POST', |
| 45 | + }, common.mustCall((res) => { |
| 46 | + res.on('data', () => {}); |
| 47 | + res.on('end', common.mustCall(() => { |
| 48 | + // Each user-visible body write is published once, carrying the chunk and |
| 49 | + // its encoding, so the byte length can be recovered without Node needing |
| 50 | + // to measure it. |
| 51 | + const requestSent = requestBodyChunks.reduce( |
| 52 | + (total, { data, encoding }) => total + byteLength(data, encoding), 0); |
| 53 | + assert.strictEqual(requestSent, Buffer.byteLength(requestBody)); |
| 54 | + |
| 55 | + const responseSent = responseBodyChunks.reduce( |
| 56 | + (total, { data, encoding }) => total + byteLength(data, encoding), 0); |
| 57 | + assert.strictEqual(responseSent, Buffer.byteLength(responseBody)); |
| 58 | + |
| 59 | + // The published chunks preserve the exact bytes that were written. |
| 60 | + assert.deepStrictEqual( |
| 61 | + requestBodyChunks.map(({ data }) => data).join(''), |
| 62 | + requestBody); |
| 63 | + assert.deepStrictEqual( |
| 64 | + responseBodyChunks.map(({ data }) => data).join(''), |
| 65 | + responseBody); |
| 66 | + |
| 67 | + server.close(); |
| 68 | + })); |
| 69 | + })); |
| 70 | + |
| 71 | + req.write('é'.repeat(50)); |
| 72 | + req.end('漢'.repeat(20)); |
| 73 | +})); |
0 commit comments