This update does several significant things: 1. It eliminates the base Nghttp2* classes and folds those in to node::http2::Http2Session and node::http2::Http2Stream 2. It makes node::http2::Http2Stream a StreamBase instance and sends that out to JS-land to act as the [kHandle] for the JavaScript Http2Stream class. 3. It shifts some of the callbacks from C++ off of the JavaScript Http2Session class to the Http2Stream class. 4. It refactors the data provider structure for FD and Stream based sending to help encapsulate those functions easier 5. It streamlines some of the functions at the C++ layer to eliminate now unnecessary redirections 6. It cleans up node_http2.cc for better readability and maintainability 7. It refactors some of the debug output 8. Because Http2Stream instances are now StreamBases, they are now also trackable using async-hooks 9. The Stream::OnRead algorithm has been simplified with a couple bugs fixed. 10. I've eliminated node_http2_core.h and node_http2_core-inl.h 11. Detect invalid handshake a report protocol error to session 12. Refactor out of memory error, improve other errors 13. Add Http2Session.prototype.ping PR-URL: https://github.com/nodejs/node/pull/17105 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Sebastiaan Deckers <sebdeckers83@gmail.com>
118 lines
2.8 KiB
JavaScript
118 lines
2.8 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
const http2 = require('http2');
|
|
const {
|
|
constants,
|
|
Http2Stream,
|
|
nghttp2ErrorString
|
|
} = process.binding('http2');
|
|
|
|
// tests error handling within pushStream
|
|
// - NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE (should emit session error)
|
|
// - NGHTTP2_ERR_STREAM_CLOSED (should emit stream error)
|
|
// - every other NGHTTP2 error from binding (should emit stream error)
|
|
|
|
const specificTestKeys = [
|
|
'NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE',
|
|
'NGHTTP2_ERR_STREAM_CLOSED'
|
|
];
|
|
|
|
const specificTests = [
|
|
{
|
|
ngError: constants.NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE,
|
|
error: {
|
|
code: 'ERR_HTTP2_OUT_OF_STREAMS',
|
|
type: Error,
|
|
message: 'No stream ID is available because ' +
|
|
'maximum stream ID has been reached'
|
|
},
|
|
type: 'stream'
|
|
},
|
|
{
|
|
ngError: constants.NGHTTP2_ERR_STREAM_CLOSED,
|
|
error: {
|
|
code: 'ERR_HTTP2_STREAM_CLOSED',
|
|
type: Error,
|
|
message: 'The stream is already closed'
|
|
},
|
|
type: 'stream'
|
|
},
|
|
];
|
|
|
|
const genericTests = Object.getOwnPropertyNames(constants)
|
|
.filter((key) => (
|
|
key.indexOf('NGHTTP2_ERR') === 0 && specificTestKeys.indexOf(key) < 0
|
|
))
|
|
.map((key) => ({
|
|
ngError: constants[key],
|
|
error: {
|
|
code: 'ERR_HTTP2_ERROR',
|
|
type: Error,
|
|
message: nghttp2ErrorString(constants[key])
|
|
},
|
|
type: 'stream'
|
|
}));
|
|
|
|
|
|
const tests = specificTests.concat(genericTests);
|
|
|
|
let currentError;
|
|
|
|
// mock submitPushPromise because we only care about testing error handling
|
|
Http2Stream.prototype.pushPromise = () => currentError.ngError;
|
|
|
|
const server = http2.createServer();
|
|
server.on('stream', common.mustCall((stream, headers) => {
|
|
const errorMustCall = common.expectsError(currentError.error);
|
|
const errorMustNotCall = common.mustNotCall(
|
|
`${currentError.error.code} should emit on ${currentError.type}`
|
|
);
|
|
|
|
if (currentError.type === 'stream') {
|
|
stream.session.on('error', errorMustNotCall);
|
|
stream.on('error', errorMustCall);
|
|
stream.on('error', common.mustCall(() => {
|
|
stream.respond();
|
|
stream.end();
|
|
}));
|
|
} else {
|
|
stream.session.once('error', errorMustCall);
|
|
stream.on('error', errorMustNotCall);
|
|
}
|
|
|
|
stream.pushStream({}, () => {});
|
|
}, tests.length));
|
|
|
|
server.listen(0, common.mustCall(() => runTest(tests.shift())));
|
|
|
|
function runTest(test) {
|
|
const port = server.address().port;
|
|
const url = `http://localhost:${port}`;
|
|
const headers = {
|
|
':path': '/',
|
|
':method': 'POST',
|
|
':scheme': 'http',
|
|
':authority': `localhost:${port}`
|
|
};
|
|
|
|
const client = http2.connect(url);
|
|
const req = client.request(headers);
|
|
|
|
currentError = test;
|
|
req.resume();
|
|
req.end();
|
|
|
|
req.on('end', common.mustCall(() => {
|
|
client.destroy();
|
|
|
|
if (!tests.length) {
|
|
server.close();
|
|
} else {
|
|
runTest(tests.shift());
|
|
}
|
|
}));
|
|
}
|