diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 66dc28c46fb333..19a67743bc31da 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -1497,8 +1497,7 @@ class Http2Stream extends Duplex { } const server = session[kServer]; - - if (err && server) { + if (server !== undefined && err) { server.emit('streamError', err, this); } @@ -1515,10 +1514,10 @@ function continueStreamDestroy(self, err, callback) { // Submit RST-STREAM frame if one hasn't been sent already and the // stream hasn't closed normally... - if (!state.rst && !session.destroyed) { - const code = - err instanceof Error ? - NGHTTP2_INTERNAL_ERROR : NGHTTP2_NO_ERROR; + const rst = state.rst; + let code = state.rstCode; + if (!rst && !session.destroyed) { + code = err instanceof Error ? NGHTTP2_INTERNAL_ERROR : NGHTTP2_NO_ERROR; session.rstStream(self, code); } @@ -1530,13 +1529,10 @@ function continueStreamDestroy(self, err, callback) { setImmediate(finishStreamDestroy, self, handle); - // All done - const rst = state.rst; - const code = rst ? state.rstCode : NGHTTP2_NO_ERROR; // RST code 8 not emitted as an error as its used by clients to signify // abort and is already covered by aborted event, also allows more // seamless compatibility with http1 - if (!err && code !== NGHTTP2_NO_ERROR && code !== NGHTTP2_CANCEL) { + if (code !== NGHTTP2_NO_ERROR && code !== NGHTTP2_CANCEL && !err) { err = new errors.Error('ERR_HTTP2_STREAM_ERROR', code); } callback(err); @@ -1546,8 +1542,7 @@ function continueStreamDestroy(self, err, callback) { function finishStreamDestroy(self, handle) { const id = self[kID]; - const session = self[kSession]; - session[kState].streams.delete(id); + self[kSession][kState].streams.delete(id); delete self[kSession]; if (handle !== undefined) handle.destroyStream(id);