Skip to content

Commit

Permalink
Fix AsyncResource propagation issue (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperair authored Nov 1, 2023
1 parent 38a6773 commit ad8afe6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
15 changes: 15 additions & 0 deletions async-hooks-stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const AsyncResource = {
bind(fn, _type, thisArg) {
return fn.bind(thisArg);
},
};

export class AsyncLocalStorage {
getStore() {
return undefined;
}

run(_store, callback) {
return callback();
}
}
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Queue from 'yocto-queue';
import {AsyncResource} from '#async_hooks';

export default function pLimit(concurrency) {
if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) {
Expand Down Expand Up @@ -31,7 +32,9 @@ export default function pLimit(concurrency) {
};

const enqueue = (fn, resolve, args) => {
queue.enqueue(run.bind(undefined, fn, resolve, args));
queue.enqueue(
AsyncResource.bind(run.bind(undefined, fn, resolve, args)),
);

(async () => {
// This function needs to wait until the next microtask before comparing
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
},
"type": "module",
"exports": "./index.js",
"imports": {
"#async_hooks": {
"node": "async_hooks",
"default": "./async-hooks-stub.js"
}
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
Expand All @@ -20,7 +26,8 @@
},
"files": [
"index.js",
"index.d.ts"
"index.d.ts",
"async-hooks-stub.js"
],
"keywords": [
"promise",
Expand Down
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import delay from 'delay';
import inRange from 'in-range';
import timeSpan from 'time-span';
import randomInt from 'random-int';
import {AsyncLocalStorage} from '#async_hooks';
import pLimit from './index.js';

Check failure on line 7 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 16

`./index.js` import should occur before import of `#async_hooks`

test('concurrency: 1', async t => {
Expand Down Expand Up @@ -40,6 +41,23 @@ test('concurrency: 4', async t => {
await Promise.all(input);
});

test('propagates async execution context properly', async t => {
const concurrency = 2;
const limit = pLimit(concurrency);
const store = new AsyncLocalStorage();

const checkId = async id => {
await Promise.resolve();
t.is(id, store.getStore()?.id);
};

const startContext = async id => store.run({id}, () => limit(checkId, id));

await Promise.all(
Array.from({length: 100}, (_, id) => startContext(id)),
);
});

test('non-promise returning function', async t => {
await t.notThrowsAsync(async () => {
const limit = pLimit(1);
Expand Down

0 comments on commit ad8afe6

Please sign in to comment.