Skip to content

Commit c14aa07

Browse files
addaleaxBridgeAR
authored andcommitted
net: use kHandle symbol for accessing native handle
Use a common `kHandle` for all `StreamBase`-based streams. PR-URL: #26491 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 3e54f90 commit c14aa07

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

Diff for: lib/internal/http2/core.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ const {
106106
updateSettingsBuffer
107107
} = require('internal/http2/util');
108108
const {
109-
createWriteWrap,
110109
writeGeneric,
111110
writevGeneric,
112111
onStreamRead,
113112
kAfterAsyncWrite,
114113
kMaybeDestroy,
115-
kUpdateTimer
114+
kUpdateTimer,
115+
kHandle
116116
} = require('internal/stream_base_commons');
117117
const {
118118
kTimeout,
@@ -151,7 +151,6 @@ const TLSServer = tls.Server;
151151
const kAlpnProtocol = Symbol('alpnProtocol');
152152
const kAuthority = Symbol('authority');
153153
const kEncrypted = Symbol('encrypted');
154-
const kHandle = Symbol('handle');
155154
const kID = Symbol('id');
156155
const kInit = Symbol('init');
157156
const kInfoHeaders = Symbol('sent-info-headers');
@@ -1798,13 +1797,12 @@ class Http2Stream extends Duplex {
17981797
if (!this.headersSent)
17991798
this[kProceed]();
18001799

1801-
const req = createWriteWrap(this[kHandle]);
1802-
req.stream = this[kID];
1800+
let req;
18031801

18041802
if (writev)
1805-
writevGeneric(this, req, data, cb);
1803+
req = writevGeneric(this, data, cb);
18061804
else
1807-
writeGeneric(this, req, data, encoding, cb);
1805+
req = writeGeneric(this, data, encoding, cb);
18081806

18091807
trackWriteState(this, req.bytes);
18101808
}

Diff for: lib/internal/stream_base_commons.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const { owner_symbol } = require('internal/async_hooks').symbols;
1717
const kMaybeDestroy = Symbol('kMaybeDestroy');
1818
const kUpdateTimer = Symbol('kUpdateTimer');
1919
const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
20+
const kHandle = Symbol('kHandle');
2021

2122
function handleWriteReq(req, data, encoding) {
2223
const { handle } = req;
@@ -87,7 +88,8 @@ function createWriteWrap(handle) {
8788
return req;
8889
}
8990

90-
function writevGeneric(self, req, data, cb) {
91+
function writevGeneric(self, data, cb) {
92+
const req = createWriteWrap(self[kHandle]);
9193
var allBuffers = data.allBuffers;
9294
var chunks;
9395
var i;
@@ -109,12 +111,15 @@ function writevGeneric(self, req, data, cb) {
109111
if (err === 0) req._chunks = chunks;
110112

111113
afterWriteDispatched(self, req, err, cb);
114+
return req;
112115
}
113116

114-
function writeGeneric(self, req, data, encoding, cb) {
117+
function writeGeneric(self, data, encoding, cb) {
118+
const req = createWriteWrap(self[kHandle]);
115119
var err = handleWriteReq(req, data, encoding);
116120

117121
afterWriteDispatched(self, req, err, cb);
122+
return req;
118123
}
119124

120125
function afterWriteDispatched(self, req, err, cb) {
@@ -186,4 +191,5 @@ module.exports = {
186191
kAfterAsyncWrite,
187192
kMaybeDestroy,
188193
kUpdateTimer,
194+
kHandle
189195
};

Diff for: lib/net.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ const {
5858
symbols: { async_id_symbol, owner_symbol }
5959
} = require('internal/async_hooks');
6060
const {
61-
createWriteWrap,
6261
writevGeneric,
6362
writeGeneric,
6463
onStreamRead,
6564
kAfterAsyncWrite,
65+
kHandle,
6666
kUpdateTimer
6767
} = require('internal/stream_base_commons');
6868
const {
@@ -236,7 +236,7 @@ function Socket(options) {
236236
// probably be supplied by async_hooks.
237237
this[async_id_symbol] = -1;
238238
this._hadError = false;
239-
this._handle = null;
239+
this[kHandle] = null;
240240
this._parent = null;
241241
this._host = null;
242242
this[kLastWriteQueueSize] = 0;
@@ -714,11 +714,11 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
714714

715715
this._unrefTimer();
716716

717-
var req = createWriteWrap(this._handle);
717+
let req;
718718
if (writev)
719-
writevGeneric(this, req, data, cb);
719+
req = writevGeneric(this, data, cb);
720720
else
721-
writeGeneric(this, req, data, encoding, cb);
721+
req = writeGeneric(this, data, encoding, cb);
722722
if (req.async)
723723
this[kLastWriteQueueSize] = req.bytes;
724724
};
@@ -1608,6 +1608,11 @@ Object.defineProperty(TCP.prototype, 'owner', {
16081608
set(v) { return this[owner_symbol] = v; }
16091609
});
16101610

1611+
Object.defineProperty(Socket.prototype, '_handle', {
1612+
get() { return this[kHandle]; },
1613+
set(v) { return this[kHandle] = v; }
1614+
});
1615+
16111616

16121617
Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) {
16131618
return this.listen({ fd: fd });

0 commit comments

Comments
 (0)