Skip to content

Commit 12d33d3

Browse files
authored
Fix Miniflare 2 code snippets (#20795)
* Update get-started.mdx * fix code block
1 parent 9986849 commit 12d33d3

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

src/content/docs/workers/testing/miniflare/get-started.mdx

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -103,61 +103,63 @@ to workers respectively:
103103
import { Miniflare } from "miniflare";
104104

105105
const mf = new Miniflare({
106+
modules: true,
106107
script: `
107-
export default {
108-
let lastScheduledController;
109-
let lastQueueBatch;
110-
async fetch(request, env, ctx) {
111-
const { pathname } = new URL(request.url);
112-
if (pathname === "/scheduled") {
113-
return Response.json({
114-
scheduledTime: lastScheduledController?.scheduledTime,
115-
cron: lastScheduledController?.cron,
116-
});
117-
} else if (pathname === "/queue") {
118-
return Response.json({
119-
queue: lastQueueBatch.queue,
120-
messages: lastQueueBatch.messages.map((message) => ({
121-
id: message.id,
122-
timestamp: message.timestamp.getTime(),
123-
body: message.body,
124-
bodyType: message.body.constructor.name,
125-
})),
126-
});
127-
} else if (pathname === "/get-url") {
128-
return new Response(request.url);
129-
} else {
130-
return new Response(null, { status: 404 });
131-
}
132-
},
133-
async scheduled(controller, env, ctx) {
134-
lastScheduledController = controller;
135-
if (controller.cron === "* * * * *") controller.noRetry();
136-
},
137-
async queue(batch, env, ctx) {
138-
lastQueueBatch = batch;
139-
if (batch.queue === "needy") batch.retryAll();
140-
for (const message of batch.messages) {
141-
if (message.id === "perfect") message.ack();
142-
}
143-
}
144-
}
145-
`,
108+
let lastScheduledController;
109+
let lastQueueBatch;
110+
export default {
111+
async fetch(request, env, ctx) {
112+
const { pathname } = new URL(request.url);
113+
if (pathname === "/scheduled") {
114+
return Response.json({
115+
scheduledTime: lastScheduledController?.scheduledTime,
116+
cron: lastScheduledController?.cron,
117+
});
118+
} else if (pathname === "/queue") {
119+
return Response.json({
120+
queue: lastQueueBatch.queue,
121+
messages: lastQueueBatch.messages.map((message) => ({
122+
id: message.id,
123+
timestamp: message.timestamp.getTime(),
124+
body: message.body,
125+
bodyType: message.body.constructor.name,
126+
})),
127+
});
128+
} else if (pathname === "/get-url") {
129+
return new Response(request.url);
130+
} else {
131+
return new Response(null, { status: 404 });
132+
}
133+
},
134+
async scheduled(controller, env, ctx) {
135+
lastScheduledController = controller;
136+
if (controller.cron === "* * * * *") controller.noRetry();
137+
},
138+
async queue(batch, env, ctx) {
139+
lastQueueBatch = batch;
140+
if (batch.queue === "needy") batch.retryAll();
141+
for (const message of batch.messages) {
142+
if (message.id === "perfect") message.ack();
143+
}
144+
}
145+
}`,
146146
});
147147

148148
const res = await mf.dispatchFetch("http://localhost:8787/", {
149149
headers: { "X-Message": "Hello Miniflare!" },
150150
});
151151
console.log(await res.text()); // Hello Miniflare!
152152

153+
const worker = await mf.getWorker();
154+
153155
const scheduledResult = await worker.scheduled({
154156
cron: "* * * * *",
155157
});
156158
console.log(scheduledResult); // { outcome: "ok", noRetry: true });
157159

158160
const queueResult = await worker.queue("needy", [
159-
{ id: "a", timestamp: new Date(1000), body: "a" },
160-
{ id: "b", timestamp: new Date(2000), body: { b: 1 } },
161+
{ id: "a", timestamp: new Date(1000), body: "a", attempts: 1 },
162+
{ id: "b", timestamp: new Date(2000), body: { b: 1 }, attempts: 1 },
161163
]);
162164
console.log(queueResult); // { outcome: "ok", retryAll: true, ackAll: false, explicitRetries: [], explicitAcks: []}
163165
```
@@ -367,6 +369,8 @@ const res = await mf.dispatchFetch("http://localhost:8787/", {
367369
});
368370
const text = await res.text();
369371

372+
const worker = await mf.getWorker();
373+
370374
// Dispatch "scheduled" event to worker
371375
const scheduledResult = await worker.scheduled({ cron: "30 * * * *" })
372376

0 commit comments

Comments
 (0)