70bf21e064
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { DurableObject } from "cloudflare:workers";
|
|
|
|
export class FixtureTestContainerB extends DurableObject<Env> {
|
|
container: globalThis.Container;
|
|
|
|
constructor(ctx: DurableObjectState, env: Env) {
|
|
super(ctx, env);
|
|
this.container = ctx.container;
|
|
}
|
|
|
|
async fetch(req: Request) {
|
|
if (!this.container.running) {
|
|
this.container.start({
|
|
entrypoint: ["node", "app.js"],
|
|
enableInternet: false,
|
|
});
|
|
// On the first request we simply start the container and return,
|
|
// on the following requests the container can actually be accessed.
|
|
// Note that we do this this way because container.start is not awaitable
|
|
// meaning that we can't simply wait here for the container to be ready
|
|
return new Response("Container started");
|
|
}
|
|
return this.container
|
|
.getTcpPort(8080)
|
|
.fetch("http://foo/bar/baz", { method: "POST", body: "hello" });
|
|
}
|
|
}
|
|
|
|
export default {
|
|
async fetch(request, env): Promise<Response> {
|
|
const id = env.CONTAINER_B.idFromName("container");
|
|
const stub = env.CONTAINER_B.get(id);
|
|
return stub.fetch(request);
|
|
},
|
|
} satisfies ExportedHandler<Env>;
|