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

Commit 9a62a02

Browse files
committed
Throw TypeError on Fetcher#fetch illegal invocation
1 parent 31792ba commit 9a62a02

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

packages/core/src/plugins/bindings.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ export class Fetcher {
8787
}
8888

8989
async fetch(input: RequestInfo, init?: RequestInit): Promise<Response> {
90+
if (!(this instanceof Fetcher)) {
91+
throw new TypeError("Illegal invocation");
92+
}
93+
9094
// Always create new Request instance, so clean object passed to services
9195
const req = new Request(input, init);
9296

packages/core/test/plugins/bindings.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,18 @@ test("Fetcher: hides implementation details", (t) => {
540540
const fetcher = new Fetcher(throws, throws);
541541
t.deepEqual(getObjectProperties(fetcher), ["fetch"]);
542542
});
543+
test("Fetcher: fetch: throws on illegal invocation", async (t) => {
544+
const throws = () => {
545+
throw new Error("Should not be called");
546+
};
547+
const fetcher = new Fetcher(throws, throws);
548+
// @ts-expect-error using comma expression to unbind this
549+
// noinspection CommaExpressionJS
550+
await t.throwsAsync(() => (0, fetcher.fetch)("http://localhost"), {
551+
instanceOf: TypeError,
552+
message: "Illegal invocation",
553+
});
554+
});
543555
test("BindingsPlugin: dispatches fetch to mounted service", async (t) => {
544556
const mf = useMiniflare(
545557
{ BindingsPlugin },

0 commit comments

Comments
 (0)