mirror of
https://github.com/nodejs/node.git
synced 2025-05-11 01:27:14 +00:00

PR-URL: https://github.com/nodejs/node/pull/17406 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> This is a significant cleanup and refactoring of the cleanup/close/destroy logic for Http2Stream and Http2Session. There are significant changes here in the timing and ordering of cleanup logic, JS apis. and various related necessary edits.
52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
const assert = require('assert');
|
|
const h2 = require('http2');
|
|
const { PADDING_STRATEGY_CALLBACK } = h2.constants;
|
|
|
|
function selectPadding(frameLen, max) {
|
|
assert.strictEqual(typeof frameLen, 'number');
|
|
assert.strictEqual(typeof max, 'number');
|
|
assert(max >= frameLen);
|
|
return max;
|
|
}
|
|
|
|
// selectPadding will be called three times:
|
|
// 1. For the client request headers frame
|
|
// 2. For the server response headers frame
|
|
// 3. For the server response data frame
|
|
const options = {
|
|
paddingStrategy: PADDING_STRATEGY_CALLBACK,
|
|
selectPadding: common.mustCall(selectPadding, 3)
|
|
};
|
|
|
|
const server = h2.createServer(options);
|
|
server.on('stream', common.mustCall(onStream));
|
|
|
|
function onStream(stream, headers, flags) {
|
|
stream.respond({
|
|
'content-type': 'text/html',
|
|
':status': 200
|
|
});
|
|
stream.end('hello world');
|
|
}
|
|
|
|
server.listen(0);
|
|
|
|
server.on('listening', common.mustCall(() => {
|
|
const client = h2.connect(`http://localhost:${server.address().port}`,
|
|
options);
|
|
|
|
const req = client.request({ ':path': '/' });
|
|
req.on('response', common.mustCall());
|
|
req.resume();
|
|
req.on('end', common.mustCall(() => {
|
|
server.close();
|
|
client.close();
|
|
}));
|
|
req.end();
|
|
}));
|