From abd324a279cad7f98267db4bb5502b92cbda29a9 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 1 Apr 2018 20:56:11 +0200 Subject: [PATCH] net,http2: merge write error handling & property names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Merge error handling for `net.Socket`s and `Http2Stream`s, and align the callback property names as `callback`. Refs: https://github.com/nodejs/node/issues/19060 PR-URL: https://github.com/nodejs/node/pull/19734 Reviewed-By: Luigi Pinca Reviewed-By: Tobias Nießen Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Trivikram Kamat --- lib/internal/http2/core.js | 2 -- lib/internal/stream_base_commons.js | 15 ++++++++++++--- lib/net.js | 24 +++++++----------------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 54497377b12c65..2d69cf3f4e01ff 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -1621,7 +1621,6 @@ class Http2Stream extends Duplex { const req = createWriteWrap(this[kHandle], afterDoStreamWrite); req.stream = this[kID]; - req.callback = cb; writeGeneric(this, req, data, encoding, cb); @@ -1654,7 +1653,6 @@ class Http2Stream extends Duplex { var req = createWriteWrap(this[kHandle], afterDoStreamWrite); req.stream = this[kID]; - req.callback = cb; writevGeneric(this, req, data, cb); diff --git a/lib/internal/stream_base_commons.js b/lib/internal/stream_base_commons.js index d902a501524791..b252b1d8ff7670 100644 --- a/lib/internal/stream_base_commons.js +++ b/lib/internal/stream_base_commons.js @@ -61,15 +61,24 @@ function writevGeneric(self, req, data, cb) { // Retain chunks if (err === 0) req._chunks = chunks; - if (err) - return self.destroy(errnoException(err, 'write', req.error), cb); + afterWriteDispatched(self, req, err, cb); } function writeGeneric(self, req, data, encoding, cb) { var err = handleWriteReq(req, data, encoding); - if (err) + afterWriteDispatched(self, req, err, cb); +} + +function afterWriteDispatched(self, req, err, cb) { + if (err !== 0) return self.destroy(errnoException(err, 'write', req.error), cb); + + if (!req.async) { + cb(); + } else { + req.callback = cb; + } } module.exports = { diff --git a/lib/net.js b/lib/net.js index 2febf21463d07d..7d4f189db71e33 100644 --- a/lib/net.js +++ b/lib/net.js @@ -726,23 +726,13 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) { return false; } - var ret; var req = createWriteWrap(this._handle, afterWrite); if (writev) - ret = writevGeneric(this, req, data, cb); + writevGeneric(this, req, data, cb); else - ret = writeGeneric(this, req, data, encoding, cb); - - // Bail out if handle.write* returned an error - if (ret) return ret; - - if (!req.async) { - cb(); - return; - } - - req.cb = cb; - this[kLastWriteQueueSize] = req.bytes; + writeGeneric(this, req, data, encoding, cb); + if (req.async) + this[kLastWriteQueueSize] = req.bytes; }; @@ -817,7 +807,7 @@ function afterWrite(status, handle, err) { if (status < 0) { var ex = errnoException(status, 'write', this.error); debug('write failure', ex); - self.destroy(ex, this.cb); + self.destroy(ex, this.callback); return; } @@ -826,8 +816,8 @@ function afterWrite(status, handle, err) { if (self !== process.stderr && self !== process.stdout) debug('afterWrite call cb'); - if (this.cb) - this.cb.call(undefined); + if (this.callback) + this.callback.call(undefined); }