diff --git a/bindings.d.ts b/bindings.d.ts index b4a29c6..84d19fb 100644 --- a/bindings.d.ts +++ b/bindings.d.ts @@ -1,3 +1,3 @@ interface Bindings { - COUNTER: DurableObjectNamespace; + server: DurableObjectNamespace; } diff --git a/src/counter.ts b/src/counter.ts deleted file mode 100644 index edeec40..0000000 --- a/src/counter.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { buildResponse } from "./response"; - -export class Counter implements DurableObject { - // Store this.state for later access - constructor(private readonly state: DurableObjectState) {} - - async fetch(request: Request) { - // Get the current count, defaulting to 0 - let value = (await this.state.storage.get("count")) ?? 0; - - const { pathname } = new URL(request.url); - let emoji = "➡️"; - if (pathname === "/increment") { - // Increment, then store the new value - this.state.storage.put("count", ++value); - emoji = "⬆️"; - } else if (pathname === "/decrement") { - // Decrement, then store the new value - this.state.storage.put("count", --value); - emoji = "⬇️"; - } else if (pathname !== "/") { - // If no route matched, return 404 response - return buildResponse("😢 Not Found", 404); - } - - // Return response containing new value, potentially after incrementing/decrementing - return buildResponse(`${emoji} ${value}`); - } -} diff --git a/src/index.ts b/src/index.ts index af0d771..610c0c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,23 +1,30 @@ export async function handleRequest(request: Request, env: Bindings) { // Match route against pattern /:name/*action const url = new URL(request.url); - const match = /\/(?[^/]+)(?.*)/.exec(url.pathname); - if (!match?.groups) { - // If we didn't specify a name, default to "test" - return Response.redirect(`${url.origin}/test/increment`, 302); + + if (url.pathname !== "/connect") { + return new Response("unknown route", { + status: 400, + }); + } + + const roomID = url.searchParams.get("roomID"); + if (roomID === null || roomID === "") { + return new Response("roomID parameter required", { + status: 400, + }); } // Forward the request to the named Durable Object... - const { COUNTER } = env; - const id = COUNTER.idFromName(match.groups.name); - const stub = COUNTER.get(id); - // ...removing the name prefix from URL - url.pathname = match.groups.action; + const { server } = env; + const id = server.idFromName(roomID); + console.log("id", id); + const stub = server.get(id); + console.log("stub", stub); return stub.fetch(url.toString()); } const worker: ExportedHandler = { fetch: handleRequest }; -// Make sure we export the Counter Durable Object class -export { Counter } from "./counter"; +export { Server } from "./server/server"; export default worker; diff --git a/src/response.ts b/src/response.ts deleted file mode 100644 index 5d0f188..0000000 --- a/src/response.ts +++ /dev/null @@ -1,39 +0,0 @@ -export function buildResponse(text: string, status = 200) { - // Build a HTML response containing the text - const html = ` - - - - Example - - - -

${text}

-

/:id//:id/increment/:id/decrement

- - - `; - - return new Response(html, { - status, - headers: { - // Content-Type must include text/html for live reload to work - "Content-Type": "text/html; charset=UTF-8", - }, - }); -} diff --git a/test/counter.spec.ts b/test/counter.spec.ts deleted file mode 100644 index d92679a..0000000 --- a/test/counter.spec.ts +++ /dev/null @@ -1,38 +0,0 @@ -const { COUNTER } = getMiniflareBindings(); -const id = COUNTER.newUniqueId(); -const stub = COUNTER.get(id); - -// Note this is beforeAll, not beforeEach, yet each test still has isolated storage. -// See https://v2.miniflare.dev/jest.html#isolated-storage for more details. -beforeAll(async () => { - const storage = await getMiniflareDurableObjectStorage(id); - await storage.put("count", 5); -}); - -test("should increment count", async () => { - const res = await stub.fetch(new Request("http://localhost/increment")); - expect(await res.text()).toContain("⬆️ 6"); -}); - -test("should decrement count", async () => { - const res = await stub.fetch(new Request("http://localhost/decrement")); - // Note that the increment from above was automatically "undone", as we expect 4 not 5 - expect(await res.text()).toContain("⬇️ 4"); -}); - -test("should get current count", async () => { - const res = await stub.fetch("http://localhost/"); - expect(await res.text()).toContain("➡️ 5"); -}); - -test("should default count to 0", async () => { - const storage = await getMiniflareDurableObjectStorage(id); - await storage.delete("count"); - const res = await stub.fetch(new Request("http://localhost/")); - expect(await res.text()).toContain("➡️ 0"); -}); - -test("should return 404 on not found", async () => { - const res = await stub.fetch("http://localhost/unknown"); - expect(res.status).toBe(404); -}); diff --git a/test/db/data.test.ts b/test/db/data.test.ts index 57da306..faab66a 100644 --- a/test/db/data.test.ts +++ b/test/db/data.test.ts @@ -1,8 +1,8 @@ import { z } from "zod"; import { delEntry, getEntry, putEntry } from "../../src/db/data"; -const { COUNTER } = getMiniflareBindings(); -const id = COUNTER.newUniqueId(); +const { server } = getMiniflareBindings(); +const id = server.newUniqueId(); test("getEntry", async () => { type Case = { diff --git a/test/ff/fast-forward.test.ts b/test/ff/fast-forward.test.ts index 418f363..006277d 100644 --- a/test/ff/fast-forward.test.ts +++ b/test/ff/fast-forward.test.ts @@ -10,8 +10,8 @@ import { putUserValue, UserValue } from "../../src/types/user-value"; import { must } from "../../src/util/must"; import { fastForwardRoom } from "../../src/ff/fast-forward"; -const { COUNTER } = getMiniflareBindings(); -const id = COUNTER.newUniqueId(); +const { server } = getMiniflareBindings(); +const id = server.newUniqueId(); test("fastForward", async () => { type Case = { diff --git a/test/ff/get-patch.test.ts b/test/ff/get-patch.test.ts index f6b8f8e..0cc140e 100644 --- a/test/ff/get-patch.test.ts +++ b/test/ff/get-patch.test.ts @@ -4,8 +4,8 @@ import { Version } from "../../src/types/version"; import { ReplicacheTransaction } from "../../src/storage/replicache-transaction"; import { DurableStorage } from "../../src/storage/durable-storage"; -const { COUNTER } = getMiniflareBindings(); -const id = COUNTER.newUniqueId(); +const { server } = getMiniflareBindings(); +const id = server.newUniqueId(); test("getPatch", async () => { type Case = { diff --git a/test/index.spec.ts b/test/index.spec.ts deleted file mode 100644 index 3caba75..0000000 --- a/test/index.spec.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { handleRequest } from "@/index"; - -test("should redirect to example page on no route match", async () => { - const env = getMiniflareBindings(); - const res = await handleRequest(new Request("http://localhost"), env); - expect(res.status).toBe(302); - expect(res.headers.get("Location")).toBe("http://localhost/test/increment"); -}); - -test("should pass-through to durable object", async () => { - const env = getMiniflareBindings(); - const { COUNTER } = env; - const id = COUNTER.idFromName("name"); - const storage = await getMiniflareDurableObjectStorage(id); - await storage.put("count", 10); - - const req = new Request("http://localhost/name/increment"); - const res = await handleRequest(req, env); - expect(res.status).toBe(200); - expect(await res.text()).toContain("⬆️ 11"); - - const newValue = await storage.get("count"); - expect(newValue).toBe(11); -}); diff --git a/test/process/process-frame.test.ts b/test/process/process-frame.test.ts index 061362a..4f7bc87 100644 --- a/test/process/process-frame.test.ts +++ b/test/process/process-frame.test.ts @@ -14,8 +14,8 @@ import { clientMutation, clientRecord, userValue } from "../util/test-utils"; import { processFrame } from "../../src/process/process-frame"; import { LogContext } from "../../src/util/logger"; -const { COUNTER } = getMiniflareBindings(); -const id = COUNTER.newUniqueId(); +const { server } = getMiniflareBindings(); +const id = server.newUniqueId(); test("processFrame", async () => { const records = new Map([ diff --git a/test/process/process-mutation.test.ts b/test/process/process-mutation.test.ts index 38420f4..e2a4703 100644 --- a/test/process/process-mutation.test.ts +++ b/test/process/process-mutation.test.ts @@ -15,8 +15,8 @@ import { } from "../../src/process/process-mutation"; import { LogContext } from "../../src/util/logger"; -const { COUNTER } = getMiniflareBindings(); -const id = COUNTER.newUniqueId(); +const { server } = getMiniflareBindings(); +const id = server.newUniqueId(); test("processMutation", async () => { type Case = { diff --git a/test/process/process-pending.test.ts b/test/process/process-pending.test.ts index f5b527e..d210136 100644 --- a/test/process/process-pending.test.ts +++ b/test/process/process-pending.test.ts @@ -13,8 +13,8 @@ import { client, clientRecord, Mocket, mutation } from "../util/test-utils"; import { processPending } from "../../src/process/process-pending"; import { LogContext } from "../../src/util/logger"; -const { COUNTER } = getMiniflareBindings(); -const id = COUNTER.newUniqueId(); +const { server } = getMiniflareBindings(); +const id = server.newUniqueId(); test("processPending", async () => { type Case = { diff --git a/test/process/process-room.test.ts b/test/process/process-room.test.ts index 9f7ee8c..e91cc82 100644 --- a/test/process/process-room.test.ts +++ b/test/process/process-room.test.ts @@ -13,8 +13,8 @@ import { client, clientRecord, mutation } from "../util/test-utils"; import { FRAME_LENGTH_MS, processRoom } from "../../src/process/process-room"; import { LogContext } from "../../src/util/logger"; -const { COUNTER } = getMiniflareBindings(); -const id = COUNTER.newUniqueId(); +const { server } = getMiniflareBindings(); +const id = server.newUniqueId(); test("processRoom", async () => { type Case = { diff --git a/test/response.spec.ts b/test/response.spec.ts deleted file mode 100644 index d2dab04..0000000 --- a/test/response.spec.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { buildResponse } from "@/response"; - -test("should build response", async () => { - const res = buildResponse("text"); - expect(res.status).toBe(200); - expect(res.headers.get("Content-Type")).toBe("text/html; charset=UTF-8"); - expect(await res.text()).toContain("

text

"); -}); - -test("should build response with custom status", () => { - const res = buildResponse("not found", 404); - expect(res.status).toBe(404); -}); diff --git a/test/server/connect.test.ts b/test/server/connect.test.ts index a798cda..b47ca29 100644 --- a/test/server/connect.test.ts +++ b/test/server/connect.test.ts @@ -9,8 +9,8 @@ import { client, clientRecord, Mocket } from "../util/test-utils"; import { handleConnection } from "../../src/server/connect"; import { LogContext } from "../../src/util/logger"; -const { COUNTER } = getMiniflareBindings(); -const id = COUNTER.newUniqueId(); +const { server } = getMiniflareBindings(); +const id = server.newUniqueId(); function freshClient(id: string, socket: Socket = new Mocket()) { const [clientID, c] = client(id, socket); diff --git a/test/server/server.test.ts b/test/server/server.test.ts index cd94ca8..cadc357 100644 --- a/test/server/server.test.ts +++ b/test/server/server.test.ts @@ -1,3 +1,7 @@ +test("foo", () => { + console.log("hi"); +}); + /* import { ClientID, ClientMap, Socket } from "../../src/types/client-state"; import { Mocket } from "../util/test-utils"; diff --git a/test/storage/replicache-transaction.test.ts b/test/storage/replicache-transaction.test.ts index e3814be..7ba4bb7 100644 --- a/test/storage/replicache-transaction.test.ts +++ b/test/storage/replicache-transaction.test.ts @@ -7,8 +7,8 @@ import { } from "../../src/types/user-value"; import { DurableStorage } from "@/storage/durable-storage"; -const { COUNTER } = getMiniflareBindings(); -const id = COUNTER.newUniqueId(); +const { server } = getMiniflareBindings(); +const id = server.newUniqueId(); test("ReplicacheTransaction", async () => { const storage = new DurableStorage( diff --git a/wrangler.toml b/wrangler.toml index 7673154..e8ae8bd 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -1,4 +1,4 @@ -name = "miniflare-typescript-esbuild-jest" +name = "reps" type = "javascript" account_id = "" @@ -11,12 +11,12 @@ compatibility_flags = [] [durable_objects] bindings = [ - { name = "COUNTER", class_name = "Counter" } + { name = "server", class_name = "Server" } ] [[migrations]] tag = "v1" -new_classes = ["Counter"] +new_classes = ["Server"] [build] command = "npm run build"