Diffinery

homeflare/celldpublic

41 lines · 1.0 KBRaw History
1import { WorkerEntrypoint } from "cloudflare:workers";
2
3const WORKER_ID = "hello-v1";
4const WORKER_MAIN = "worker.js";
5const WORKER_SOURCE = `
6 export default {
7 fetch() {
8 console.log("hello from loaded code");
9 return new Response("Hello from a Dynamic Worker!\\n");
10 }
11 };
12`;
13
14export class DynamicWorkerTail extends WorkerEntrypoint {
15 tail(events) {
16 for (const event of events) {
17 for (const log of event.logs) {
18 console.log(`dynamic-worker-tail: ${log.message.join(" ")}`, {
19 workerId: this.ctx.props.workerId,
20 level: log.level,
21 });
22 }
23 }
24 }
25}
26
27export default {
28 fetch(request, env, ctx) {
29 const worker = env.LOADER.get(WORKER_ID, () => ({
30 compatibilityDate: "2025-01-01",
31 mainModule: WORKER_MAIN,
32 modules: { [WORKER_MAIN]: WORKER_SOURCE },
33 tails: [
34 ctx.exports.DynamicWorkerTail({
35 props: { workerId: WORKER_ID },
36 }),
37 ],
38 }));
39 return worker.getEntrypoint().fetch(request);
40 },
41};