diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 8d8f200262d1d9..9bd503a799e4f5 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -1804,6 +1804,13 @@ class ServerHttp2Stream extends Http2Stream { const stream = new ServerHttp2Stream(session, ret, options, headers); + if (options.weight !== undefined) { + stream.priority({ + weight: options.weight, + silent: true + }); + } + // If the push stream is a head request, close the writable side of // the stream immediately as there won't be any data sent. if (headRequest) { diff --git a/test/parallel/test-http2-server-push-priority.js b/test/parallel/test-http2-server-push-priority.js new file mode 100644 index 00000000000000..e9753ccec48593 --- /dev/null +++ b/test/parallel/test-http2-server-push-priority.js @@ -0,0 +1,47 @@ +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); +const assert = require('assert'); +const http2 = require('http2'); + + +const server = http2.createServer(); +server.on('stream', common.mustCall((stream, headers, flags) => { + const expectedWeight = 256; + const options = { weight: expectedWeight }; + stream.pushStream({}, options, common.mustCall((stream, headers, flags) => { + // The priority of push streams is changed silently by nghttp2, + // without emitting a PRIORITY frame. Flags are ignored. + assert.strictEqual(stream.state.weight, expectedWeight); + assert.strictEqual(flags & http2.constants.NGHTTP2_FLAG_PRIORITY, 0); + })); + stream.respond({ + 'content-type': 'text/html', + ':status': 200 + }); + stream.end('test'); +})); + +server.listen(0, common.mustCall(() => { + const port = server.address().port; + const client = http2.connect(`http://localhost:${port}`); + + const headers = { ':path': '/' }; + const req = client.request(headers); + + client.on('stream', common.mustCall((stream, headers, flags) => { + // Since the push priority is set silently, the client is not informed. + const expectedWeight = http2.constants.NGHTTP2_DEFAULT_WEIGHT; + assert.strictEqual(stream.state.weight, expectedWeight); + assert.strictEqual(flags & http2.constants.NGHTTP2_FLAG_PRIORITY, 0); + })); + + req.on('data', common.mustCall(() => {})); + req.on('end', common.mustCall(() => { + server.close(); + client.destroy(); + })); + req.end(); +}));