Skip to content

Commit

Permalink
fix(commonjs): import p-queue dynamically (#97)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Pilar <tomas.pilar@ibm.com>
Co-authored-by: Tomas Dvorak <toomas2d@gmail.com>
  • Loading branch information
pilartomas and Tomas2D authored Mar 21, 2024
1 parent d44ec5e commit 9c34f48
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 111 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"pinst": "^3.0.0",
"prettier": "^3.0.3",
"ts-node": "^10.9.1",
"tsup": "^7.2.0",
"tsup": "^8.0.2",
"typescript": "^5.3.3",
"vitest": "^0.34.6"
},
Expand Down
11 changes: 8 additions & 3 deletions src/utils/concurrency.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PQueue, { QueueAddOptions } from 'p-queue';
import type { default as PQueueType, QueueAddOptions } from 'p-queue';

import { HttpError } from '../errors.js';

Expand All @@ -11,8 +11,13 @@ function isConcurrencyLimitError(err: unknown): err is HttpError {
);
}

const PQueue = (async () => {
const lib = await import('p-queue');
return lib.default;
})();

export class ConcurrencyLimiter {
private _queue?: PQueue;
private _queue?: PQueueType;
private _limiterPromise?: ReturnType<Limiter>;

constructor(private readonly limiter: Limiter) {}
Expand Down Expand Up @@ -42,6 +47,6 @@ export class ConcurrencyLimiter {

this._limiterPromise = this.limiter();
const { limit } = await this._limiterPromise;
this._queue = new PQueue({ concurrency: limit });
this._queue = new (await PQueue)({ concurrency: limit });
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
"ts-node": {
"esm": true
}
}
}
1 change: 1 addition & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default defineConfig({
format: ['esm', 'cjs'],
platform: 'node',
shims: true,
splitting: true,
env: {
NODE_ENV: 'production',
GENAI_DEFAULT_ENDPOINT: GENAI_DEFAULT_ENDPOINT,
Expand Down
Loading

0 comments on commit 9c34f48

Please sign in to comment.