| 63 | |
| 64 | export class WorkspaceProxy extends WorkerEntrypoint<unknown, WorkspaceProxyProps> { |
| 65 | override async fetch(request: Request): Promise<Response> { |
| 66 | const url = new URL(request.url); |
| 67 | |
| 68 | const callback = url.pathname.match(/^\/__workspace_connect\/([0-9a-f-]{36})\/(health|ws)$/); |
| 69 | if (callback?.[2] === "health") { |
| 70 | return new Response("ok\n", { |
| 71 | headers: { "content-type": "text/plain; charset=utf-8" }, |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | if (url.pathname === "/health") { |
| 76 | return new Response("ok\n", { |
| 77 | headers: { "content-type": "text/plain; charset=utf-8" }, |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | if (url.pathname === "/ws" || callback?.[2] === "ws") { |
| 82 | if (callback?.[1]) { |
| 83 | url.pathname = "/ws"; |
| 84 | url.searchParams.set("token", callback[1]); |
| 85 | request = new Request(url, request); |
| 86 | } |
| 87 | const { binding, id } = this.ctx.props; |
| 88 | const ns = (this.env as Record<string, unknown>)[binding] as |
| 89 | | DurableObjectNamespace |
| 90 | | undefined; |
| 91 | if (!ns) { |
| 92 | return new Response(`WorkspaceProxy: env.${binding} is not a DurableObjectNamespace`, { |
| 93 | status: 500, |
| 94 | }); |
| 95 | } |
| 96 | const stub = ns.get(ns.idFromString(id)); |
| 97 | return stub.fetch(request); |
| 98 | } |
| 99 | |
| 100 | return new Response("not found", { status: 404 }); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | export class ArtifactsCLITarget extends RpcTarget { |