Skip to content

Commit bea58a1

Browse files
committed
async_hooks: use instanceof to identify resource types
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
1 parent 0a655cb commit bea58a1

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

lib/async_hooks.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,30 @@ class AsyncResource {
252252
}
253253
}
254254

255+
let _tls_wrap;
256+
257+
function lazyLoadTlsWrap() {
258+
_tls_wrap ??= require('_tls_wrap');
259+
}
260+
261+
let dgram;
262+
263+
function lazyLoadDgram() {
264+
dgram ??= require('dgram');
265+
}
266+
267+
let internal_dgram;
268+
269+
function lazyLoadInternalDgram() {
270+
internal_dgram ??= require('internal/dgram');
271+
}
272+
273+
let net;
274+
275+
function lazyLoadNet() {
276+
net ??= require('net');
277+
}
278+
255279
function lookupResourceWithStorage() {
256280
// When a TCP/UDP object is created, its owner_symbol is uninitialized, so
257281
// calling executionAsyncResource() would return the TCP/UDP object itself.
@@ -263,11 +287,23 @@ function lookupResourceWithStorage() {
263287
// object, which does not contain the storage that was saved initially. Hence,
264288
// in the case of a Socket/TLSSocket object, we must use the underlying
265289
// TCP/UDP object, if available, for storage.
290+
291+
lazyLoadDgram();
292+
lazyLoadInternalDgram();
293+
lazyLoadNet();
294+
lazyLoadTlsWrap();
295+
266296
let resource = executionAsyncResource();
267-
if ((resource.constructor.name === 'Socket' ||
268-
resource.constructor.name === 'TLSSocket') &&
269-
resource._handle != null)
297+
298+
if ((resource instanceof net.Socket ||
299+
resource instanceof _tls_wrap.TLSSocket) &&
300+
resource._handle != null) {
270301
resource = resource._handle;
302+
} else if (resource instanceof dgram.Socket &&
303+
resource[internal_dgram.kStateSymbol].handle != null) {
304+
resource = resource[internal_dgram.kStateSymbol].handle;
305+
}
306+
271307
return resource;
272308
}
273309

0 commit comments

Comments
 (0)