Skip to content

Commit af7164e

Browse files
committed
lib,src: standardize owner_symbol for handles
Instead of somtimes using an `owner` string to link from a native handle object to the corresponding JS object, standardize on a single symbol that fulfills this role. PR-URL: #22002 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]>
1 parent a196aa2 commit af7164e

File tree

12 files changed

+77
-42
lines changed

12 files changed

+77
-42
lines changed

lib/_tls_wrap.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const debug = util.debuglog('tls');
3535
const tls_wrap = process.binding('tls_wrap');
3636
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
3737
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
38+
const { owner_symbol } = require('internal/async_hooks').symbols;
3839
const {
3940
SecureContext: NativeSecureContext
4041
} = process.binding('crypto');
@@ -76,7 +77,7 @@ function onhandshakestart(now) {
7677
else
7778
this.handshakes++;
7879

79-
const { owner } = this;
80+
const owner = this[owner_symbol];
8081
if (this.handshakes > tls.CLIENT_RENEG_LIMIT) {
8182
owner._emitTLSError(new ERR_TLS_SESSION_ATTACK());
8283
return;
@@ -89,7 +90,7 @@ function onhandshakestart(now) {
8990
function onhandshakedone() {
9091
debug('onhandshakedone');
9192

92-
const owner = this.owner;
93+
const owner = this[owner_symbol];
9394

9495
// `newSession` callback wasn't called yet
9596
if (owner._newSessionPending) {
@@ -102,7 +103,7 @@ function onhandshakedone() {
102103

103104

104105
function loadSession(hello) {
105-
const owner = this.owner;
106+
const owner = this[owner_symbol];
106107

107108
var once = false;
108109
function onSession(err, session) {
@@ -130,7 +131,7 @@ function loadSession(hello) {
130131

131132

132133
function loadSNI(info) {
133-
const owner = this.owner;
134+
const owner = this[owner_symbol];
134135
const servername = info.servername;
135136
if (!servername || !owner._SNICallback)
136137
return requestOCSP(owner, info);
@@ -212,7 +213,7 @@ function requestOCSPDone(socket) {
212213

213214

214215
function onnewsession(key, session) {
215-
const owner = this.owner;
216+
const owner = this[owner_symbol];
216217

217218
if (!owner.server)
218219
return;
@@ -241,11 +242,11 @@ function onnewsession(key, session) {
241242

242243

243244
function onocspresponse(resp) {
244-
this.owner.emit('OCSPResponse', resp);
245+
this[owner_symbol].emit('OCSPResponse', resp);
245246
}
246247

247248
function onerror(err) {
248-
const owner = this.owner;
249+
const owner = this[owner_symbol];
249250

250251
if (owner._writableState.errorEmitted)
251252
return;
@@ -364,9 +365,9 @@ for (var n = 0; n < proxiedMethods.length; n++) {
364365

365366
tls_wrap.TLSWrap.prototype.close = function close(cb) {
366367
let ssl;
367-
if (this.owner) {
368-
ssl = this.owner.ssl;
369-
this.owner.ssl = null;
368+
if (this[owner_symbol]) {
369+
ssl = this[owner_symbol].ssl;
370+
this[owner_symbol].ssl = null;
370371
}
371372

372373
// Invoke `destroySSL` on close to clean up possibly pending write requests
@@ -405,7 +406,7 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
405406
handle = options.pipe ?
406407
new Pipe(PipeConstants.SOCKET) :
407408
new TCP(TCPConstants.SOCKET);
408-
handle.owner = this;
409+
handle[owner_symbol] = this;
409410
}
410411

411412
// Wrap socket's handle

lib/dgram.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const { isUint8Array } = require('internal/util/types');
4343
const EventEmitter = require('events');
4444
const {
4545
defaultTriggerAsyncIdScope,
46-
symbols: { async_id_symbol }
46+
symbols: { async_id_symbol, owner_symbol }
4747
} = require('internal/async_hooks');
4848
const { UV_UDP_REUSEADDR } = process.binding('constants').os;
4949

@@ -78,7 +78,7 @@ function Socket(type, listener) {
7878
}
7979

8080
var handle = newHandle(type, lookup);
81-
handle.owner = this;
81+
handle[owner_symbol] = this;
8282

8383
this[async_id_symbol] = handle.getAsyncId();
8484
this.type = type;
@@ -132,7 +132,7 @@ function replaceHandle(self, newHandle) {
132132
newHandle.lookup = oldHandle.lookup;
133133
newHandle.bind = oldHandle.bind;
134134
newHandle.send = oldHandle.send;
135-
newHandle.owner = self;
135+
newHandle[owner_symbol] = self;
136136

137137
// Replace the existing handle by the handle we got from master.
138138
oldHandle.close();
@@ -635,7 +635,7 @@ function stopReceiving(socket) {
635635

636636

637637
function onMessage(nread, handle, buf, rinfo) {
638-
var self = handle.owner;
638+
var self = handle[owner_symbol];
639639
if (nread < 0) {
640640
return self.emit('error', errnoException(nread, 'recvmsg'));
641641
}
@@ -745,6 +745,14 @@ Socket.prototype._stopReceiving = function() {
745745
};
746746

747747

748+
// Legacy alias on the C++ wrapper object. This is not public API, so we may
749+
// want to runtime-deprecate it at some point. There's no hurry, though.
750+
Object.defineProperty(UDP.prototype, 'owner', {
751+
get() { return this[owner_symbol]; },
752+
set(v) { return this[owner_symbol] = v; }
753+
});
754+
755+
748756
module.exports = {
749757
_createSocketHandle,
750758
createSocket,

lib/internal/async_hooks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const async_wrap = process.binding('async_wrap');
3030
* It has a fixed size, so if that is exceeded, calls to the native
3131
* side are used instead in pushAsyncIds() and popAsyncIds().
3232
*/
33-
const { async_hook_fields, async_id_fields } = async_wrap;
33+
const { async_hook_fields, async_id_fields, owner_symbol } = async_wrap;
3434
// Store the pair executionAsyncId and triggerAsyncId in a std::stack on
3535
// Environment::AsyncHooks::async_ids_stack_ tracks the resource responsible for
3636
// the current execution stack. This is unwound as each resource exits. In the
@@ -434,7 +434,7 @@ module.exports = {
434434
symbols: {
435435
async_id_symbol, trigger_async_id_symbol,
436436
init_symbol, before_symbol, after_symbol, destroy_symbol,
437-
promise_resolve_symbol
437+
promise_resolve_symbol, owner_symbol
438438
},
439439
constants: {
440440
kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve

lib/internal/child_process.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const { TTY } = process.binding('tty_wrap');
2727
const { TCP } = process.binding('tcp_wrap');
2828
const { UDP } = process.binding('udp_wrap');
2929
const SocketList = require('internal/socket_list');
30+
const { owner_symbol } = require('internal/async_hooks').symbols;
3031
const { convertToValidSignal } = require('internal/util');
3132
const { isUint8Array } = require('internal/util/types');
3233
const spawn_sync = process.binding('spawn_sync');
@@ -209,7 +210,7 @@ function ChildProcess() {
209210
this.spawnfile = null;
210211

211212
this._handle = new Process();
212-
this._handle.owner = this;
213+
this._handle[owner_symbol] = this;
213214

214215
this._handle.onexit = (exitCode, signalCode) => {
215216
if (signalCode) {

lib/internal/cluster/child.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const assert = require('assert');
33
const util = require('util');
44
const path = require('path');
55
const EventEmitter = require('events');
6+
const { owner_symbol } = require('internal/async_hooks').symbols;
67
const Worker = require('internal/cluster/worker');
78
const { internal, sendHelper } = require('internal/cluster/utils');
89
const cluster = new EventEmitter();
@@ -207,8 +208,8 @@ function _disconnect(masterInitiated) {
207208
delete handles[key];
208209
waitingCount++;
209210

210-
if (handle.owner)
211-
handle.owner.close(checkWaitingCount);
211+
if (handle[owner_symbol])
212+
handle[owner_symbol].close(checkWaitingCount);
212213
else
213214
handle.close(checkWaitingCount);
214215
}

lib/internal/fs/watchers.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const {
1111
getStatsFromBinding,
1212
validatePath
1313
} = require('internal/fs/utils');
14-
const { defaultTriggerAsyncIdScope } = require('internal/async_hooks');
14+
const {
15+
defaultTriggerAsyncIdScope,
16+
symbols: { owner_symbol }
17+
} = require('internal/async_hooks');
1518
const { toNamespacedPath } = require('path');
1619
const { validateUint32 } = require('internal/validators');
1720
const { getPathFromURL } = require('internal/url');
@@ -20,7 +23,6 @@ const assert = require('assert');
2023

2124
const kOldStatus = Symbol('kOldStatus');
2225
const kUseBigint = Symbol('kUseBigint');
23-
const kOwner = Symbol('kOwner');
2426

2527
function emitStop(self) {
2628
self.emit('stop');
@@ -36,7 +38,7 @@ function StatWatcher(bigint) {
3638
util.inherits(StatWatcher, EventEmitter);
3739

3840
function onchange(newStatus, stats) {
39-
const self = this[kOwner];
41+
const self = this[owner_symbol];
4042
if (self[kOldStatus] === -1 &&
4143
newStatus === -1 &&
4244
stats[2/* new nlink */] === stats[16/* old nlink */]) {
@@ -59,7 +61,7 @@ StatWatcher.prototype.start = function(filename, persistent, interval) {
5961
return;
6062

6163
this._handle = new _StatWatcher(this[kUseBigint]);
62-
this._handle[kOwner] = this;
64+
this._handle[owner_symbol] = this;
6365
this._handle.onchange = onchange;
6466
if (!persistent)
6567
this._handle.unref();
@@ -104,7 +106,7 @@ function FSWatcher() {
104106
EventEmitter.call(this);
105107

106108
this._handle = new FSEvent();
107-
this._handle.owner = this;
109+
this._handle[owner_symbol] = this;
108110

109111
this._handle.onchange = (status, eventType, filename) => {
110112
// TODO(joyeecheung): we may check self._handle.initialized here
@@ -131,6 +133,7 @@ function FSWatcher() {
131133
}
132134
util.inherits(FSWatcher, EventEmitter);
133135

136+
134137
// FIXME(joyeecheung): this method is not documented.
135138
// At the moment if filename is undefined, we
136139
// 1. Throw an Error if it's the first time .start() is called
@@ -187,6 +190,13 @@ function emitCloseNT(self) {
187190
self.emit('close');
188191
}
189192

193+
// Legacy alias on the C++ wrapper object. This is not public API, so we may
194+
// want to runtime-deprecate it at some point. There's no hurry, though.
195+
Object.defineProperty(FSEvent.prototype, 'owner', {
196+
get() { return this[owner_symbol]; },
197+
set(v) { return this[owner_symbol] = v; }
198+
});
199+
190200
module.exports = {
191201
FSWatcher,
192202
StatWatcher

lib/internal/http2/core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {
2828
defaultTriggerAsyncIdScope,
2929
symbols: {
3030
async_id_symbol,
31+
owner_symbol,
3132
},
3233
} = require('internal/async_hooks');
3334
const { internalBinding } = require('internal/bootstrap/loaders');
@@ -141,7 +142,7 @@ const kInfoHeaders = Symbol('sent-info-headers');
141142
const kMaybeDestroy = Symbol('maybe-destroy');
142143
const kLocalSettings = Symbol('local-settings');
143144
const kOptions = Symbol('options');
144-
const kOwner = Symbol('owner');
145+
const kOwner = owner_symbol;
145146
const kProceed = Symbol('proceed');
146147
const kProtocol = Symbol('protocol');
147148
const kProxySocket = Symbol('proxy-socket');

lib/internal/wrap_js_stream.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ const { Socket } = require('net');
66
const { JSStream } = process.binding('js_stream');
77
const uv = process.binding('uv');
88
const debug = util.debuglog('stream_wrap');
9+
const { owner_symbol } = require('internal/async_hooks').symbols;
910
const { ERR_STREAM_WRAP } = require('internal/errors').codes;
1011

1112
const kCurrentWriteRequest = Symbol('kCurrentWriteRequest');
1213
const kCurrentShutdownRequest = Symbol('kCurrentShutdownRequest');
1314

14-
function isClosing() { return this.owner.isClosing(); }
15-
function onreadstart() { return this.owner.readStart(); }
16-
function onreadstop() { return this.owner.readStop(); }
17-
function onshutdown(req) { return this.owner.doShutdown(req); }
18-
function onwrite(req, bufs) { return this.owner.doWrite(req, bufs); }
15+
function isClosing() { return this[owner_symbol].isClosing(); }
16+
function onreadstart() { return this[owner_symbol].readStart(); }
17+
function onreadstop() { return this[owner_symbol].readStop(); }
18+
function onshutdown(req) { return this[owner_symbol].doShutdown(req); }
19+
function onwrite(req, bufs) { return this[owner_symbol].doWrite(req, bufs); }
1920

2021
/* This class serves as a wrapper for when the C++ side of Node wants access
2122
* to a standard JS stream. For example, TLS or HTTP do not operate on network
@@ -37,7 +38,7 @@ class JSStreamWrap extends Socket {
3738
this.doClose(cb);
3839
};
3940
// Inside of the following functions, `this` refers to the handle
40-
// and `this.owner` refers to this JSStreamWrap instance.
41+
// and `this[owner_symbol]` refers to this JSStreamWrap instance.
4142
handle.isClosing = isClosing;
4243
handle.onreadstart = onreadstart;
4344
handle.onreadstop = onreadstop;

lib/net.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const {
5656
const {
5757
newAsyncId,
5858
defaultTriggerAsyncIdScope,
59-
symbols: { async_id_symbol }
59+
symbols: { async_id_symbol, owner_symbol }
6060
} = require('internal/async_hooks');
6161
const {
6262
createWriteWrap,
@@ -207,7 +207,7 @@ function initSocketHandle(self) {
207207

208208
// Handle creation may be deferred to bind() or connect() time.
209209
if (self._handle) {
210-
self._handle.owner = self;
210+
self._handle[owner_symbol] = self;
211211
self._handle.onread = onread;
212212
self[async_id_symbol] = getNewAsyncId(self._handle);
213213
}
@@ -371,7 +371,7 @@ Socket.prototype._final = function(cb) {
371371

372372

373373
function afterShutdown(status, handle) {
374-
var self = handle.owner;
374+
var self = handle[owner_symbol];
375375

376376
debug('afterShutdown destroyed=%j', self.destroyed,
377377
self._readableState);
@@ -620,7 +620,7 @@ Socket.prototype._destroy = function(exception, cb) {
620620
// buffer, or when there's an error reading.
621621
function onread(nread, buffer) {
622622
var handle = this;
623-
var self = handle.owner;
623+
var self = handle[owner_symbol];
624624
assert(handle === self._handle, 'handle != self._handle');
625625

626626
self._unrefTimer();
@@ -819,7 +819,7 @@ protoGetter('bytesWritten', function bytesWritten() {
819819

820820

821821
function afterWrite(status, handle, err) {
822-
var self = handle.owner;
822+
var self = handle[owner_symbol];
823823
if (self !== process.stderr && self !== process.stdout)
824824
debug('afterWrite', status);
825825

@@ -1123,7 +1123,7 @@ Socket.prototype.unref = function() {
11231123

11241124

11251125
function afterConnect(status, handle, req, readable, writable) {
1126-
var self = handle.owner;
1126+
var self = handle[owner_symbol];
11271127

11281128
// callback may come after call to destroy
11291129
if (self.destroyed) {
@@ -1325,7 +1325,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
13251325

13261326
this[async_id_symbol] = getNewAsyncId(this._handle);
13271327
this._handle.onconnection = onconnection;
1328-
this._handle.owner = this;
1328+
this._handle[owner_symbol] = this;
13291329

13301330
// Use a backlog of 512 entries. We pass 511 to the listen() call because
13311331
// the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
@@ -1538,7 +1538,7 @@ Server.prototype.address = function() {
15381538

15391539
function onconnection(err, clientHandle) {
15401540
var handle = this;
1541-
var self = handle.owner;
1541+
var self = handle[owner_symbol];
15421542

15431543
debug('onconnection');
15441544

@@ -1669,6 +1669,14 @@ function emitCloseNT(self) {
16691669
}
16701670

16711671

1672+
// Legacy alias on the C++ wrapper object. This is not public API, so we may
1673+
// want to runtime-deprecate it at some point. There's no hurry, though.
1674+
Object.defineProperty(TCP.prototype, 'owner', {
1675+
get() { return this[owner_symbol]; },
1676+
set(v) { return this[owner_symbol] = v; }
1677+
});
1678+
1679+
16721680
Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) {
16731681
return this.listen({ fd: fd });
16741682
}, 'Server.listenFD is deprecated. Use Server.listen({fd: <number>}) instead.',

0 commit comments

Comments
 (0)