Skip to content

Commit

Permalink
async_hooks: use instanceof to identify resource types
Browse files Browse the repository at this point in the history
Signed-off-by: Darshan Sen <[email protected]>
  • Loading branch information
RaisinTen committed Nov 7, 2021
1 parent 0a655cb commit bea58a1
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ class AsyncResource {
}
}

let _tls_wrap;

function lazyLoadTlsWrap() {
_tls_wrap ??= require('_tls_wrap');
}

let dgram;

function lazyLoadDgram() {
dgram ??= require('dgram');
}

let internal_dgram;

function lazyLoadInternalDgram() {
internal_dgram ??= require('internal/dgram');
}

let net;

function lazyLoadNet() {
net ??= require('net');
}

function lookupResourceWithStorage() {
// When a TCP/UDP object is created, its owner_symbol is uninitialized, so
// calling executionAsyncResource() would return the TCP/UDP object itself.
Expand All @@ -263,11 +287,23 @@ function lookupResourceWithStorage() {
// object, which does not contain the storage that was saved initially. Hence,
// in the case of a Socket/TLSSocket object, we must use the underlying
// TCP/UDP object, if available, for storage.

lazyLoadDgram();
lazyLoadInternalDgram();
lazyLoadNet();
lazyLoadTlsWrap();

let resource = executionAsyncResource();
if ((resource.constructor.name === 'Socket' ||
resource.constructor.name === 'TLSSocket') &&
resource._handle != null)

if ((resource instanceof net.Socket ||
resource instanceof _tls_wrap.TLSSocket) &&
resource._handle != null) {
resource = resource._handle;
} else if (resource instanceof dgram.Socket &&
resource[internal_dgram.kStateSymbol].handle != null) {
resource = resource[internal_dgram.kStateSymbol].handle;
}

return resource;
}

Expand Down

0 comments on commit bea58a1

Please sign in to comment.