Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commonjs): import p-queue dynamically #97

Merged
merged 5 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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