Skip to content

Commit

Permalink
Fix error on early async hooks implementations
Browse files Browse the repository at this point in the history
fixes #42
  • Loading branch information
dougwilson committed Feb 22, 2022
1 parent e4c33cc commit 9f25172
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
- Node.js 5.x
- Node.js 6.x
- Node.js 7.x
- Node.js 8.0 # test early async_hooks
- Node.js 8.x
- Node.js 10.x
- Node.js 11.x
Expand Down Expand Up @@ -72,6 +73,10 @@ jobs:
node-version: "7.10"
npm-i: [email protected] [email protected]

- name: Node.js 8.0
node-version: "8.0"
npm-i: [email protected] [email protected]

- name: Node.js 8.x
node-version: "8.17"
npm-i: [email protected]
Expand Down
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Fix error on early async hooks implementations

2.4.0 / 2022-02-21
==================

Expand Down
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,29 @@ function tryRequireAsyncHooks () {
try {
return require('async_hooks')
} catch (e) {
/* istanbul ignore next */
return {}
}
}

/**
* Wrap function with async resource
* Wrap function with async resource, if possible.
* AsyncResource.bind static method backported.
* @private
*/

function wrap (fn) {
if (!asyncHooks.AsyncResource) {
/* istanbul ignore next */
var res

// create anonymous resource
if (asyncHooks.AsyncResource) {
res = new asyncHooks.AsyncResource(fn.name || 'bound-anonymous-fn')
}

// incompatible node.js
if (!res || !res.runInAsyncScope) {
return fn
}

// AsyncResource.bind static method backported
var res = new asyncHooks.AsyncResource(fn.name || 'bound-anonymous-fn')
// return bound function
return res.runInAsyncScope.bind(res, fn, null)
}

0 comments on commit 9f25172

Please sign in to comment.