Skip to content

An open-source, faster Devin alternative

TypeScript

sdk 2.1.2: call fetch as a plain function so a Worker's native fetch accepts it

Igor Zalutski160ba15393a2
diff --git a/sdks/typescript/CHANGELOG.md b/sdks/typescript/CHANGELOG.md
index 6b6cb9e3..b1346e6e 100644
--- a/sdks/typescript/CHANGELOG.md
+++ b/sdks/typescript/CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog
+## 2.1.2
+
+- `@opencomputer/sdk/agents`: the transport called `fetch` as a method of the client, which a native fetch refuses with `Illegal invocation` in workerd; it is now called as a plain function. Found by the Development proof of a Worker without Node compatibility. No API change.
+
## 2.0.0
Breaking. The `OpenComputer` class that wrapped the Durable Agent Sessions
diff --git a/sdks/typescript/package-lock.json b/sdks/typescript/package-lock.json
index 850a6af3..557305db 100644
--- a/sdks/typescript/package-lock.json
+++ b/sdks/typescript/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@opencomputer/sdk",
- "version": "2.1.0",
+ "version": "2.1.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@opencomputer/sdk",
- "version": "2.1.0",
+ "version": "2.1.2",
"license": "MIT",
"dependencies": {
"undici": "^6.0.0 || ^7.0.0"
diff --git a/sdks/typescript/package.json b/sdks/typescript/package.json
index d7f85ce0..060ba1d3 100644
--- a/sdks/typescript/package.json
+++ b/sdks/typescript/package.json
@@ -1,6 +1,6 @@
{
"name": "@opencomputer/sdk",
- "version": "2.1.0",
+ "version": "2.1.2",
"description": "TypeScript SDK for OpenComputer - cloud sandboxes and the Serverless Agents management API",
"type": "module",
"main": "dist/index.js",
diff --git a/sdks/typescript/src/agents/http.ts b/sdks/typescript/src/agents/http.ts
index 014003ba..a8638581 100644
--- a/sdks/typescript/src/agents/http.ts
+++ b/sdks/typescript/src/agents/http.ts
@@ -54,7 +54,11 @@ export class Http {
this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
const f = options.fetch ?? (typeof fetch === "function" ? fetch : undefined);
if (!f) throw new Error("No global fetch is available; pass { fetch } to the client.");
- this.doFetch = f;
+ // Call it as a plain function, never as a method of this client: a native
+ // fetch checks its receiver, and workerd answers `Illegal invocation` when
+ // the receiver is anything but the global; a caller's own fetch keeps
+ // whatever binding it came with.
+ this.doFetch = (input, init) => f(input, init);
}
url(path: string, query?: Query): string {
diff --git a/sdks/typescript/src/agents/receiver.test.ts b/sdks/typescript/src/agents/receiver.test.ts
new file mode 100644
index 00000000..dfd95415
--- /dev/null
+++ b/sdks/typescript/src/agents/receiver.test.ts
@@ -0,0 +1,33 @@
+import { afterEach, describe, expect, it } from "vitest";
+import { OpenComputer } from "./index.js";
+
+// A fetch that behaves like a native one in workerd: it refuses to run with
+// a receiver other than the global. Calling `this.doFetch(...)` on the client
+// handed it the client as `this`, which is the "Illegal invocation" the
+// Development proof P4 hit on the first request of a Worker without Node
+// compatibility.
+function receiverCheckingFetch(this: unknown, input: string | URL | Request): Promise<Response> {
+ if (this !== undefined && this !== globalThis) {
+ return Promise.reject(new TypeError("Illegal invocation"));
+ }
+ const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
+ return Promise.resolve(Response.json({ events: [] }, { status: 200, headers: { "x-url": url } }));
+}
+
+describe("the transport's receiver", () => {
+ const original = globalThis.fetch;
+ afterEach(() => {
+ globalThis.fetch = original;
+ });
+
+ it("calls an injected fetch as a plain function, not as a method of the client", async () => {
+ const oc = new OpenComputer({ apiKey: "k", baseUrl: "https://example.invalid", fetch: receiverCheckingFetch as typeof fetch });
+ await expect(oc.sessions.events.list("ses-1", { after: 0 })).resolves.toEqual([]);
+ });
+
+ it("calls the global fetch the same way when none is injected", async () => {
+ globalThis.fetch = receiverCheckingFetch as typeof fetch;
+ const oc = new OpenComputer({ apiKey: "k", baseUrl: "https://example.invalid" });
+ await expect(oc.sessions.events.list("ses-1", { after: 0 })).resolves.toEqual([]);
+ });
+});
diff --git a/sdks/typescript/src/version.ts b/sdks/typescript/src/version.ts
index 6a27461a..01a7ba35 100644
--- a/sdks/typescript/src/version.ts
+++ b/sdks/typescript/src/version.ts
@@ -27,7 +27,7 @@ export const SDK_VERSION_HEADER = "x-oc-sdk-version";
* only failure mode that matters — a stale constant here would silently route a
* v2 caller to the old runtime.
*/
-export const SDK_VERSION = "2.0.0";
+export const SDK_VERSION = "2.1.2";
/** The identifying header, as a spreadable object. */
export function sdkVersionHeaders(): Record<string, string> {