better-convex 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/plugins/ratelimit/index.js +23 -17
- package/package.json +1 -1
|
@@ -511,8 +511,7 @@ var Ratelimit = class Ratelimit {
|
|
|
511
511
|
while (Date.now() <= deadline) {
|
|
512
512
|
latest = await this.limit(identifier);
|
|
513
513
|
if (latest.success) return latest;
|
|
514
|
-
|
|
515
|
-
await new Promise((resolve) => setTimeout(resolve, waitMs));
|
|
514
|
+
await sleep(Math.max(1, Math.min(latest.reset, deadline) - Date.now()));
|
|
516
515
|
}
|
|
517
516
|
return latest;
|
|
518
517
|
}
|
|
@@ -707,22 +706,14 @@ var Ratelimit = class Ratelimit {
|
|
|
707
706
|
}
|
|
708
707
|
async runWithTimeout(operation) {
|
|
709
708
|
if (this.timeout <= 0) return operation();
|
|
710
|
-
|
|
711
|
-
const timeoutResult = this.timeoutResponse(this.failureMode === "open");
|
|
712
|
-
let timerUnavailable = false;
|
|
713
|
-
const timeoutPromise = new Promise((resolve) => {
|
|
714
|
-
try {
|
|
715
|
-
timeoutHandle = setTimeout(() => resolve(timeoutResult), this.timeout);
|
|
716
|
-
} catch {
|
|
717
|
-
timerUnavailable = true;
|
|
718
|
-
resolve(timeoutResult);
|
|
719
|
-
}
|
|
720
|
-
});
|
|
721
|
-
if (timerUnavailable) return operation();
|
|
709
|
+
const startedAt = Date.now();
|
|
722
710
|
try {
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
711
|
+
const result = await operation();
|
|
712
|
+
if (Date.now() - startedAt > this.timeout) return this.timeoutResponse(this.failureMode === "open");
|
|
713
|
+
return result;
|
|
714
|
+
} catch (error) {
|
|
715
|
+
if (Date.now() - startedAt > this.timeout) return this.timeoutResponse(this.failureMode === "open");
|
|
716
|
+
throw error;
|
|
726
717
|
}
|
|
727
718
|
}
|
|
728
719
|
timeoutResponse(success) {
|
|
@@ -767,6 +758,21 @@ function pickSampleShards(total, sample) {
|
|
|
767
758
|
}
|
|
768
759
|
return selected.length > 0 ? selected : [0];
|
|
769
760
|
}
|
|
761
|
+
async function sleep(ms) {
|
|
762
|
+
try {
|
|
763
|
+
await new Promise((resolve) => {
|
|
764
|
+
setTimeout(resolve, ms);
|
|
765
|
+
});
|
|
766
|
+
} catch (error) {
|
|
767
|
+
if (isTimerUnsupportedError(error)) throw new Error("blockUntilReady is not supported in Convex queries/mutations. Use an action or non-Convex runtime.");
|
|
768
|
+
throw error;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
function isTimerUnsupportedError(error) {
|
|
772
|
+
if (!(error instanceof Error)) return false;
|
|
773
|
+
const message = error.message.toLowerCase();
|
|
774
|
+
return message.includes("can't use settimeout in queries and mutations") || message.includes("settimeout");
|
|
775
|
+
}
|
|
770
776
|
async function resolveIdentifier(identifierOption, ctx, fromClient) {
|
|
771
777
|
if (!identifierOption) {
|
|
772
778
|
if (!fromClient) throw new Error("hookAPI requires identifier in options or request args");
|