Skip to content

Commit

Permalink
nwip: attempt to wire up the durable object
Browse files Browse the repository at this point in the history
  • Loading branch information
aboodman committed Jan 28, 2022
1 parent 56b043b commit bb015d8
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 176 deletions.
2 changes: 1 addition & 1 deletion bindings.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
interface Bindings {
COUNTER: DurableObjectNamespace;
server: DurableObjectNamespace;
}
29 changes: 0 additions & 29 deletions src/counter.ts

This file was deleted.

29 changes: 18 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 = /\/(?<name>[^/]+)(?<action>.*)/.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<Bindings> = { fetch: handleRequest };

// Make sure we export the Counter Durable Object class
export { Counter } from "./counter";
export { Server } from "./server/server";
export default worker;
39 changes: 0 additions & 39 deletions src/response.ts

This file was deleted.

38 changes: 0 additions & 38 deletions test/counter.spec.ts

This file was deleted.

4 changes: 2 additions & 2 deletions test/db/data.test.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions test/ff/fast-forward.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions test/ff/get-patch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
24 changes: 0 additions & 24 deletions test/index.spec.ts

This file was deleted.

4 changes: 2 additions & 2 deletions test/process/process-frame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
4 changes: 2 additions & 2 deletions test/process/process-mutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions test/process/process-pending.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions test/process/process-room.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
13 changes: 0 additions & 13 deletions test/response.spec.ts

This file was deleted.

4 changes: 2 additions & 2 deletions test/server/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions test/server/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
test("foo", () => {
console.log("hi");
});

/*
import { ClientID, ClientMap, Socket } from "../../src/types/client-state";
import { Mocket } from "../util/test-utils";
Expand Down
4 changes: 2 additions & 2 deletions test/storage/replicache-transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "miniflare-typescript-esbuild-jest"
name = "reps"
type = "javascript"

account_id = ""
Expand All @@ -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"
Expand Down

0 comments on commit bb015d8

Please sign in to comment.