Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 8646485

Browse files
committed
Fix AbortSignal.timeout()/scheduler.wait() argument validation
1 parent 92839d3 commit 8646485

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

packages/core/src/standards/timers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export const AbortSignal =
4040
// Polyfill `AbortSignal.timeout` as described here:
4141
// https://community.cloudflare.com/t/2021-12-10-workers-runtime-release-notes/334982
4242
// @ts-expect-error `timeout` isn't included in Node.js yet
43-
AbortSignal.timeout ??= (ms?: number) => {
44-
if (typeof ms !== "number") {
43+
AbortSignal.timeout ??= function (ms?: number) {
44+
if (arguments.length === 0) {
4545
throw new TypeError(
4646
"Failed to execute 'timeout' on 'AbortSignal': parameter 1 is not of type 'integer'."
4747
);
@@ -57,7 +57,7 @@ export interface SchedulerWaitOptions {
5757

5858
export class Scheduler {
5959
wait(ms?: number, options?: SchedulerWaitOptions): Promise<void> {
60-
if (typeof ms !== "number") {
60+
if (arguments.length === 0) {
6161
throw new TypeError(
6262
"Failed to execute 'wait' on 'Scheduler': parameter 1 is not of type 'integer'."
6363
);

packages/core/test/standards/timers.spec.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
triggerPromise,
1111
waitsForInputGate,
1212
} from "@miniflare/shared-test";
13-
import test, { ThrowsExpectation } from "ava";
13+
import test from "ava";
1414

1515
test("inputGatedSetTimeout: calls callback with no input gate in context", async (t) => {
1616
const [trigger, promise] = triggerPromise<[number, string]>();
@@ -109,15 +109,17 @@ test("AbortSignal.timeout: triggers signal after timeout", async (t) => {
109109
t.true(aborted);
110110
});
111111
test("AbortSignal.timeout: requires numeric timeout", (t) => {
112-
const expectations: ThrowsExpectation = {
112+
// @ts-expect-error `timeout` isn't included in Node.js yet
113+
t.throws(() => AbortSignal.timeout(), {
113114
instanceOf: TypeError,
114115
message:
115116
"Failed to execute 'timeout' on 'AbortSignal': parameter 1 is not of type 'integer'.",
116-
};
117-
// @ts-expect-error `timeout` isn't included in Node.js yet
118-
t.throws(() => AbortSignal.timeout(), expectations);
119-
// @ts-expect-error `timeout` isn't included in Node.js yet
120-
t.throws(() => AbortSignal.timeout("42"), expectations);
117+
});
118+
// @ts-expect-error this is valid in the real Workers runtime
119+
AbortSignal.timeout(undefined);
120+
// @ts-expect-error this is valid in the real Workers runtime
121+
// noinspection TypeScriptValidateJSTypes
122+
AbortSignal.timeout("1");
121123
});
122124
test("AbortSignal.timeout: included on constructor obtained via AbortController#signal prototype", (t) => {
123125
const controller = new AbortController();
@@ -153,13 +155,14 @@ test("scheduler.wait: does nothing if aborted after resolve", async (t) => {
153155
controller.abort();
154156
t.pass();
155157
});
156-
test("scheduler.wait: requires numeric timeout", (t) => {
157-
const expectations: ThrowsExpectation = {
158+
test("scheduler.wait: requires numeric timeout", async (t) => {
159+
t.throws(() => scheduler.wait(), {
158160
instanceOf: TypeError,
159161
message:
160162
"Failed to execute 'wait' on 'Scheduler': parameter 1 is not of type 'integer'.",
161-
};
162-
t.throws(() => scheduler.wait(), expectations);
163-
// @ts-expect-error `timeout` isn't included in Node.js yet
164-
t.throws(() => scheduler.wait("42"), expectations);
163+
});
164+
await scheduler.wait(undefined);
165+
// @ts-expect-error this is valid in the real Workers runtime
166+
// noinspection TypeScriptValidateJSTypes
167+
await scheduler.wait("1");
165168
});

0 commit comments

Comments
 (0)