diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 5d70f436bbc676..106991dbd4224d 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -95,6 +95,7 @@ const kSession = Symbol('session'); const kState = Symbol('state'); const kType = Symbol('type'); const kUpdateTimer = Symbol('update-timer'); +const kWriteGeneric = Symbol('write-generic'); const kDefaultSocketTimeout = 2 * 60 * 1000; @@ -1635,13 +1636,16 @@ class Http2Stream extends Duplex { 'bug in Node.js'); } - _write(data, encoding, cb) { + [kWriteGeneric](writev, data, encoding, cb) { // When the Http2Stream is first created, it is corked until the // handle and the stream ID is assigned. However, if the user calls // uncork() before that happens, the Duplex will attempt to pass // writes through. Those need to be queued up here. if (this.pending) { - this.once('ready', this._write.bind(this, data, encoding, cb)); + this.once( + 'ready', + this[kWriteGeneric].bind(this, writev, data, encoding, cb) + ); return; } @@ -1665,53 +1669,30 @@ class Http2Stream extends Duplex { req.callback = cb; req.oncomplete = afterDoStreamWrite; req.async = false; - const err = createWriteReq(req, handle, data, encoding); + + let err; + if (writev) { + const chunks = new Array(data.length << 1); + for (var i = 0; i < data.length; i++) { + const entry = data[i]; + chunks[i * 2] = entry.chunk; + chunks[i * 2 + 1] = entry.encoding; + } + err = handle.writev(req, chunks); + } else { + err = createWriteReq(req, handle, data, encoding); + } if (err) return this.destroy(errors.errnoException(err, 'write', req.error), cb); trackWriteState(this, req.bytes); } - _writev(data, cb) { - // When the Http2Stream is first created, it is corked until the - // handle and the stream ID is assigned. However, if the user calls - // uncork() before that happens, the Duplex will attempt to pass - // writes through. Those need to be queued up here. - if (this.pending) { - this.once('ready', this._writev.bind(this, data, cb)); - return; - } - - // If the stream has been destroyed, there's nothing else we can do - // because the handle has been destroyed. This should only be an - // issue if a write occurs before the 'ready' event in the case where - // the duplex is uncorked before the stream is ready to go. In that - // case, drop the data on the floor. An error should have already been - // emitted. - if (this.destroyed) - return; - - this[kUpdateTimer](); - - if (!this.headersSent) - this[kProceed](); + _write(data, encoding, cb) { + this[kWriteGeneric](false, data, encoding, cb); + } - const handle = this[kHandle]; - const req = new WriteWrap(); - req.stream = this[kID]; - req.handle = handle; - req.callback = cb; - req.oncomplete = afterDoStreamWrite; - req.async = false; - const chunks = new Array(data.length << 1); - for (var i = 0; i < data.length; i++) { - const entry = data[i]; - chunks[i * 2] = entry.chunk; - chunks[i * 2 + 1] = entry.encoding; - } - const err = handle.writev(req, chunks); - if (err) - return this.destroy(errors.errnoException(err, 'write', req.error), cb); - trackWriteState(this, req.bytes); + _writev(data, cb) { + this[kWriteGeneric](true, data, '', cb); } _final(cb) {