Skip to content

Commit e7f5886

Browse files
addaleaxBridgeAR
authored andcommitted
net: use kHandle symbol for accessing native handle
Use a common `kHandle` for all `StreamBase`-based streams. PR-URL: nodejs#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 3414bc7 commit e7f5886

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

lib/internal/http2/core.js

Lines changed: 4 additions & 6 deletions
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,
115114
kUpdateTimer,
115+
kHandle,
116116
kSession,
117117
setStreamTimeout
118118
} = require('internal/stream_base_commons');
@@ -149,7 +149,6 @@ const TLSServer = tls.Server;
149149
const kAlpnProtocol = Symbol('alpnProtocol');
150150
const kAuthority = Symbol('authority');
151151
const kEncrypted = Symbol('encrypted');
152-
const kHandle = Symbol('handle');
153152
const kID = Symbol('id');
154153
const kInit = Symbol('init');
155154
const kInfoHeaders = Symbol('sent-info-headers');
@@ -1795,13 +1794,12 @@ class Http2Stream extends Duplex {
17951794
if (!this.headersSent)
17961795
this[kProceed]();
17971796

1798-
const req = createWriteWrap(this[kHandle]);
1799-
req.stream = this[kID];
1797+
let req;
18001798

18011799
if (writev)
1802-
writevGeneric(this, req, data, cb);
1800+
req = writevGeneric(this, data, cb);
18031801
else
1804-
writeGeneric(this, req, data, encoding, cb);
1802+
req = writeGeneric(this, data, encoding, cb);
18051803

18061804
trackWriteState(this, req.bytes);
18071805
}

lib/internal/stream_base_commons.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ const {
2727
const kMaybeDestroy = Symbol('kMaybeDestroy');
2828
const kUpdateTimer = Symbol('kUpdateTimer');
2929
const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
30-
const kSession = Symbol('session');
30+
const kHandle = Symbol('kHandle');
31+
const kSession = Symbol('kSession');
3132

3233
function handleWriteReq(req, data, encoding) {
3334
const { handle } = req;
@@ -98,7 +99,8 @@ function createWriteWrap(handle) {
9899
return req;
99100
}
100101

101-
function writevGeneric(self, req, data, cb) {
102+
function writevGeneric(self, data, cb) {
103+
const req = createWriteWrap(self[kHandle]);
102104
var allBuffers = data.allBuffers;
103105
var chunks;
104106
var i;
@@ -120,12 +122,15 @@ function writevGeneric(self, req, data, cb) {
120122
if (err === 0) req._chunks = chunks;
121123

122124
afterWriteDispatched(self, req, err, cb);
125+
return req;
123126
}
124127

125-
function writeGeneric(self, req, data, encoding, cb) {
128+
function writeGeneric(self, data, encoding, cb) {
129+
const req = createWriteWrap(self[kHandle]);
126130
var err = handleWriteReq(req, data, encoding);
127131

128132
afterWriteDispatched(self, req, err, cb);
133+
return req;
129134
}
130135

131136
function afterWriteDispatched(self, req, err, cb) {
@@ -229,6 +234,7 @@ module.exports = {
229234
kAfterAsyncWrite,
230235
kMaybeDestroy,
231236
kUpdateTimer,
237+
kHandle,
232238
kSession,
233239
setStreamTimeout
234240
};

lib/net.js

Lines changed: 10 additions & 5 deletions
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
setStreamTimeout
6868
} = require('internal/stream_base_commons');
@@ -233,7 +233,7 @@ function Socket(options) {
233233
// probably be supplied by async_hooks.
234234
this[async_id_symbol] = -1;
235235
this._hadError = false;
236-
this._handle = null;
236+
this[kHandle] = null;
237237
this._parent = null;
238238
this._host = null;
239239
this[kLastWriteQueueSize] = 0;
@@ -689,11 +689,11 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
689689

690690
this._unrefTimer();
691691

692-
var req = createWriteWrap(this._handle);
692+
let req;
693693
if (writev)
694-
writevGeneric(this, req, data, cb);
694+
req = writevGeneric(this, data, cb);
695695
else
696-
writeGeneric(this, req, data, encoding, cb);
696+
req = writeGeneric(this, data, encoding, cb);
697697
if (req.async)
698698
this[kLastWriteQueueSize] = req.bytes;
699699
};
@@ -1584,6 +1584,11 @@ Object.defineProperty(TCP.prototype, 'owner', {
15841584
set(v) { return this[owner_symbol] = v; }
15851585
});
15861586

1587+
Object.defineProperty(Socket.prototype, '_handle', {
1588+
get() { return this[kHandle]; },
1589+
set(v) { return this[kHandle] = v; }
1590+
});
1591+
15871592

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

0 commit comments

Comments
 (0)