A vat's globalOutbound is now always null. A granted host arrives as env.egress, an Egress the app can hand to whichever of its parts should have it; as globalOutbound it was ambient to every module in the vat. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
26 lines
1.0 KiB
JavaScript
26 lines
1.0 KiB
JavaScript
import { DurableObject } from "cloudflare:workers";
|
|
|
|
/** `?url=<target>` fetches it through `env.egress`; with `&ws=1`, opens a WebSocket there and reports the echo of "hello". */
|
|
export class App extends DurableObject {
|
|
async fetch(request) {
|
|
const params = new URL(request.url).searchParams;
|
|
const target = params.get("url");
|
|
const { egress } = this.env;
|
|
if (!egress) return new Response("no egress granted\n");
|
|
try {
|
|
if (!params.has("ws")) return new Response(`got: ${await (await egress.fetch(target)).text()}\n`);
|
|
const resp = await egress.fetch(target, { headers: { Upgrade: "websocket" } });
|
|
const ws = resp.webSocket;
|
|
if (!ws) return new Response(`no websocket: ${resp.status} ${await resp.text()}`);
|
|
ws.accept();
|
|
const reply = new Promise((resolve) => ws.addEventListener("message", (e) => resolve(e.data)));
|
|
ws.send("hello");
|
|
const got = await reply;
|
|
ws.close();
|
|
return new Response(`echo: ${got}\n`);
|
|
} catch (e) {
|
|
return new Response(`refused: ${e.message}\n`);
|
|
}
|
|
}
|
|
}
|