diff --git a/lib/internal/http2.js b/lib/internal/http2.js index 0d973ad1c0..2b7f7f1f69 100644 --- a/lib/internal/http2.js +++ b/lib/internal/http2.js @@ -1307,6 +1307,11 @@ function initializeTLSOptions(options) { return options; } +function onErrorSecureServerSession(err, conn) { + if (!this.emit('clientError', err, conn)) + conn.destroy(err); +} + class Http2SecureServerSession extends TLSServer { constructor(options, requestListener) { super(initializeTLSOptions(options), connectionListener); @@ -1314,10 +1319,7 @@ class Http2SecureServerSession extends TLSServer { this.timeout = kDefaultSocketTimeout; if (typeof requestListener === 'function') this.on('request', requestListener); - this.on('tlsClientError', (err, conn) => { - if (!this.emit('clientError', err, conn)) - conn.destroy(err); - }); + this.on('tlsClientError', onErrorSecureServerSession); } setTimeout(msecs, callback) { @@ -1475,9 +1477,8 @@ class Http2ClientSession extends EventEmitter { this[kSocket] = socket; const session = this[kSession] = createClientSession(options, socket); - socket.once('error', (error) => { - console.log(error); - }); + // TODO remove this + socket.once('error', console.log); socket.on('resume', socketOnResume); socket.on('pause', socketOnPause); socket.on('drain', socketOnDrain); @@ -1605,20 +1606,7 @@ class Http2ClientRequest extends Http2Outgoing { const _handle = this.stream.session.request(mapToHeaders(this[kHeaders]), true); if (_handle instanceof http2.Http2Stream) { this[kId] = _handle.getId(); - this.stream.once('handle', () => { - if (this[kTrailers] instanceof Map) { - for (const v of this[kTrailers]) { - const key = String(v[0]); - const value = v[1]; - if (Array.isArray(value) && value.length > 0) { - for (const item of value) - this.stream.addTrailer(key, String(item)); - } else { - this.stream.addTrailer(key, String(value)); - } - } - } - }); + this.stream.once('handle', kBeginSendHandle(this)) this.stream._handle = _handle; } } @@ -1629,6 +1617,23 @@ class Http2ClientRequest extends Http2Outgoing { } } +function kBeginSendHandle(that) { + return function() { + if (that[kTrailers] instanceof Map) { + for (const v of that[kTrailers]) { + const key = String(v[0]); + const value = v[1]; + if (Array.isArray(value) && value.length > 0) { + for (const item of value) + that.stream.addTrailer(key, String(item)); + } else { + that.stream.addTrailer(key, String(value)); + } + } + } + } +} + class Http2ClientResponse extends Http2Incoming { constructor(stream, headers, options) { super(stream, headers, options);