From 37c58b228bf3222fb5df3de126d6228d7d6a83cc Mon Sep 17 00:00:00 2001 From: jjohnson Date: Thu, 10 Aug 2023 11:59:14 -0500 Subject: [PATCH] Trigger scheduled handler on '/cdn-cgi/mf/scheduled' --- package-lock.json | 16 +++++------ packages/miniflare/package.json | 2 +- .../src/workers/core/entry.worker.ts | 23 ++++++++++++++- packages/miniflare/test/index.spec.ts | 28 +++++++++++++++++++ 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 929fa0087..0271a71a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -129,9 +129,9 @@ } }, "node_modules/@cloudflare/workers-types": { - "version": "4.20230419.0", - "resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20230419.0.tgz", - "integrity": "sha512-MfNBlHrI/ekRkbLtdAo23D4hkXF+3QD92OCwuXxCUK73HtMHuBqkMp9T/8KFbKNRCnz7PzUderc7Jr5m3eeW3g==", + "version": "4.20230807.0", + "resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20230807.0.tgz", + "integrity": "sha512-gQczWuGE2rxmpzOCNn0zLbx8Xz0gqspdE9S7tu4Xax39q1csgO/E9flcS+KG3GHB522ugOh84inmABDhpeJnvQ==", "dev": true }, "node_modules/@esbuild/android-arm": { @@ -5464,7 +5464,7 @@ "zod": "^3.20.6" }, "devDependencies": { - "@cloudflare/workers-types": "^4.20230419.0", + "@cloudflare/workers-types": "^4.20230807.0", "@types/better-sqlite3": "^7.6.2", "@types/debug": "^4.1.7", "@types/estree": "^1.0.0", @@ -5563,9 +5563,9 @@ "optional": true }, "@cloudflare/workers-types": { - "version": "4.20230419.0", - "resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20230419.0.tgz", - "integrity": "sha512-MfNBlHrI/ekRkbLtdAo23D4hkXF+3QD92OCwuXxCUK73HtMHuBqkMp9T/8KFbKNRCnz7PzUderc7Jr5m3eeW3g==", + "version": "4.20230807.0", + "resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20230807.0.tgz", + "integrity": "sha512-gQczWuGE2rxmpzOCNn0zLbx8Xz0gqspdE9S7tu4Xax39q1csgO/E9flcS+KG3GHB522ugOh84inmABDhpeJnvQ==", "dev": true }, "@esbuild/android-arm": { @@ -7685,7 +7685,7 @@ "miniflare": { "version": "file:packages/miniflare", "requires": { - "@cloudflare/workers-types": "^4.20230419.0", + "@cloudflare/workers-types": "^4.20230807.0", "@types/better-sqlite3": "^7.6.2", "@types/debug": "^4.1.7", "@types/estree": "^1.0.0", diff --git a/packages/miniflare/package.json b/packages/miniflare/package.json index bc780a21f..dd492202c 100644 --- a/packages/miniflare/package.json +++ b/packages/miniflare/package.json @@ -45,7 +45,7 @@ "zod": "^3.20.6" }, "devDependencies": { - "@cloudflare/workers-types": "^4.20230419.0", + "@cloudflare/workers-types": "^4.20230807.0", "@types/better-sqlite3": "^7.6.2", "@types/debug": "^4.1.7", "@types/estree": "^1.0.0", diff --git a/packages/miniflare/src/workers/core/entry.worker.ts b/packages/miniflare/src/workers/core/entry.worker.ts index 1c20d7625..efa78fc3a 100644 --- a/packages/miniflare/src/workers/core/entry.worker.ts +++ b/packages/miniflare/src/workers/core/entry.worker.ts @@ -164,6 +164,24 @@ async function handleQueue( return Response.json(queueResponse); } +async function handleScheduled( + params: URLSearchParams, + service: Fetcher +): Promise { + const time = params.get("time"); + const scheduledTime = time ? new Date(parseInt(time)) : undefined; + const cron = params.get("cron") ?? undefined; + + const result = await service.scheduled({ + scheduledTime, + cron, + }); + + return new Response(result.outcome, { + status: result.outcome === "ok" ? 200 : 500, + }); +} + export default >{ async fetch(request, env, ctx) { const startTime = Date.now(); @@ -183,11 +201,14 @@ export default >{ try { const customEvent = request.headers.get(CoreHeaders.CUSTOM_EVENT); - // TODO(soon): support scheduled events, requires support from workerd if (customEvent === "queue") { return await handleQueue(request, url, service, startTime); } + if (url.pathname === "/cdn-cgi/mf/scheduled") { + return await handleScheduled(url.searchParams, service); + } + let response = await service.fetch(request); if (!isDispatchFetch) { response = await maybePrettifyError(request, response, env); diff --git a/packages/miniflare/test/index.spec.ts b/packages/miniflare/test/index.spec.ts index df9179a75..9d89fc967 100644 --- a/packages/miniflare/test/index.spec.ts +++ b/packages/miniflare/test/index.spec.ts @@ -498,3 +498,31 @@ test("Miniflare: Accepts https requests", async (t) => { t.assert(log.logs[0][1].startsWith("Ready on https://")); }); + +test("Miniflare: Manually triggered scheduled events", async (t) => { + const log = new TestLog(t); + + const mf = new Miniflare({ + log, + modules: true, + script: ` + let scheduledRun = false; + export default { + fetch() { + return new Response(scheduledRun); + }, + scheduled() { + scheduledRun = true; + } + }`, + }); + + let res = await mf.dispatchFetch("http://localhost"); + t.is(await res.text(), "false"); + + res = await mf.dispatchFetch("http://localhost/cdn-cgi/mf/scheduled"); + t.is(await res.text(), "ok"); + + res = await mf.dispatchFetch("http://localhost"); + t.is(await res.text(), "true"); +});