mirror of
https://github.com/nodejs/node.git
synced 2025-05-06 11:29:26 +00:00

Rather than an option, introduce a method and an event... ```js server.on('stream', (stream) => { stream.respond(undefined, { waitForTrailers: true }); stream.on('wantTrailers', () => { stream.sendTrailers({ abc: 'xyz'}); }); stream.end('hello world'); }); ``` This is a breaking change in the API such that the prior `options.getTrailers` is no longer supported at all. Ordinarily this would be semver-major and require a deprecation but the http2 stuff is still experimental. PR-URL: https://github.com/nodejs/node/pull/19959 Reviewed-By: Yuta Hiroto <hello@hiroppy.me> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
'use strict';
|
|
// Flags: --expose-internals
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
const http2 = require('http2');
|
|
const { Http2Stream } = process.binding('http2');
|
|
|
|
const server = http2.createServer();
|
|
|
|
Http2Stream.prototype.respond = () => 1;
|
|
server.on('stream', common.mustCall((stream) => {
|
|
|
|
// Send headers
|
|
stream.respond({ 'content-type': 'text/plain' });
|
|
|
|
// Should throw if headers already sent
|
|
common.expectsError(
|
|
() => stream.respond(),
|
|
{
|
|
type: Error,
|
|
code: 'ERR_HTTP2_HEADERS_SENT',
|
|
message: 'Response has already been initiated.'
|
|
}
|
|
);
|
|
|
|
// Should throw if stream already destroyed
|
|
stream.destroy();
|
|
common.expectsError(
|
|
() => stream.respond(),
|
|
{
|
|
type: Error,
|
|
code: 'ERR_HTTP2_INVALID_STREAM',
|
|
message: 'The stream has been destroyed'
|
|
}
|
|
);
|
|
}));
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const client = http2.connect(`http://localhost:${server.address().port}`);
|
|
const req = client.request();
|
|
|
|
req.on('end', common.mustCall(() => {
|
|
client.close();
|
|
server.close();
|
|
}));
|
|
req.resume();
|
|
req.end();
|
|
}));
|