Skip to content

Commit 5d2a005

Browse files
committed
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: nodejs#19060
1 parent 141be92 commit 5d2a005

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
@@ -758,23 +758,13 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
758758
return false;
759759
}
760760

761-
var ret;
762761
var req = createWriteWrap(this._handle, afterWrite);
763762
if (writev)
764-
ret = writevGeneric(this, req, data, cb);
763+
writevGeneric(this, req, data, cb);
765764
else
766-
ret = writeGeneric(this, req, data, encoding, cb);
767-
768-
// Bail out if handle.write* returned an error
769-
if (ret) return ret;
770-
771-
if (!req.async) {
772-
cb();
773-
return;
774-
}
775-
776-
req.cb = cb;
777-
this[kLastWriteQueueSize] = req.bytes;
765+
writeGeneric(this, req, data, encoding, cb);
766+
if (req.async)
767+
this[kLastWriteQueueSize] = req.bytes;
778768
};
779769

780770

@@ -849,7 +839,7 @@ function afterWrite(status, handle, err) {
849839
if (status < 0) {
850840
var ex = errnoException(status, 'write', this.error);
851841
debug('write failure', ex);
852-
self.destroy(ex, this.cb);
842+
self.destroy(ex, this.callback);
853843
return;
854844
}
855845

@@ -858,8 +848,8 @@ function afterWrite(status, handle, err) {
858848
if (self !== process.stderr && self !== process.stdout)
859849
debug('afterWrite call cb');
860850

861-
if (this.cb)
862-
this.cb.call(undefined);
851+
if (this.callback)
852+
this.callback.call(undefined);
863853
}
864854

865855

0 commit comments

Comments
 (0)