Skip to content

Commit e85c20b

Browse files
addaleaxjasnell
authored andcommitted
net,http2: merge write error handling & property names
Merge error handling for `net.Socket`s and `Http2Stream`s, and align the callback property names as `callback`. Refs: #19060 PR-URL: #19734 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 2a3a66a commit e85c20b

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

lib/internal/http2/core.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,6 @@ class Http2Stream extends Duplex {
16571657

16581658
const req = createWriteWrap(this[kHandle], afterDoStreamWrite);
16591659
req.stream = this[kID];
1660-
req.callback = cb;
16611660

16621661
writeGeneric(this, req, data, encoding, cb);
16631662

@@ -1690,7 +1689,6 @@ class Http2Stream extends Duplex {
16901689

16911690
var req = createWriteWrap(this[kHandle], afterDoStreamWrite);
16921691
req.stream = this[kID];
1693-
req.callback = cb;
16941692

16951693
writevGeneric(this, req, data, cb);
16961694

lib/internal/stream_base_commons.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,24 @@ function writevGeneric(self, req, data, cb) {
6161
// Retain chunks
6262
if (err === 0) req._chunks = chunks;
6363

64-
if (err)
65-
return self.destroy(errnoException(err, 'write', req.error), cb);
64+
afterWriteDispatched(self, req, err, cb);
6665
}
6766

6867
function writeGeneric(self, req, data, encoding, cb) {
6968
var err = handleWriteReq(req, data, encoding);
7069

71-
if (err)
70+
afterWriteDispatched(self, req, err, cb);
71+
}
72+
73+
function afterWriteDispatched(self, req, err, cb) {
74+
if (err !== 0)
7275
return self.destroy(errnoException(err, 'write', req.error), cb);
76+
77+
if (!req.async) {
78+
cb();
79+
} else {
80+
req.callback = cb;
81+
}
7382
}
7483

7584
module.exports = {

lib/net.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -754,23 +754,13 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
754754
return false;
755755
}
756756

757-
var ret;
758757
var req = createWriteWrap(this._handle, afterWrite);
759758
if (writev)
760-
ret = writevGeneric(this, req, data, cb);
759+
writevGeneric(this, req, data, cb);
761760
else
762-
ret = writeGeneric(this, req, data, encoding, cb);
763-
764-
// Bail out if handle.write* returned an error
765-
if (ret) return ret;
766-
767-
if (!req.async) {
768-
cb();
769-
return;
770-
}
771-
772-
req.cb = cb;
773-
this[kLastWriteQueueSize] = req.bytes;
761+
writeGeneric(this, req, data, encoding, cb);
762+
if (req.async)
763+
this[kLastWriteQueueSize] = req.bytes;
774764
};
775765

776766

@@ -845,7 +835,7 @@ function afterWrite(status, handle, err) {
845835
if (status < 0) {
846836
var ex = errnoException(status, 'write', this.error);
847837
debug('write failure', ex);
848-
self.destroy(ex, this.cb);
838+
self.destroy(ex, this.callback);
849839
return;
850840
}
851841

@@ -854,8 +844,8 @@ function afterWrite(status, handle, err) {
854844
if (self !== process.stderr && self !== process.stdout)
855845
debug('afterWrite call cb');
856846

857-
if (this.cb)
858-
this.cb.call(undefined);
847+
if (this.callback)
848+
this.callback.call(undefined);
859849
}
860850

861851

0 commit comments

Comments
 (0)