diff --git a/backend/data.test.ts b/backend/db/data.test.ts similarity index 100% rename from backend/data.test.ts rename to backend/db/data.test.ts diff --git a/backend/data.ts b/backend/db/data.ts similarity index 97% rename from backend/data.ts rename to backend/db/data.ts index e4c891a..06cc688 100644 --- a/backend/data.ts +++ b/backend/db/data.ts @@ -1,7 +1,7 @@ import { JSONValue } from "replicache"; import { ZodSchema } from "zod"; import { Executor, transact } from "./pg"; -import { RoomID } from "./room-state"; +import { RoomID } from "../types/room-state"; export async function createDatabase() { await transact(async (executor) => { diff --git a/backend/pg.ts b/backend/db/pg.ts similarity index 100% rename from backend/pg.ts rename to backend/db/pg.ts diff --git a/backend/get-patch.test.ts b/backend/fastforward/get-patch.test.ts similarity index 91% rename from backend/get-patch.test.ts rename to backend/fastforward/get-patch.test.ts index 41b320e..523e073 100644 --- a/backend/get-patch.test.ts +++ b/backend/fastforward/get-patch.test.ts @@ -1,13 +1,13 @@ import { expect } from "chai"; import { setup, test } from "mocha"; import { PatchOperation } from "replicache"; -import { createDatabase } from "./data"; -import { withExecutor } from "./pg"; +import { createDatabase } from "../db/data"; +import { withExecutor } from "../db/pg"; import { getPatch } from "./get-patch"; -import { NullableVersion } from "./version"; -import { ReplicacheTransaction } from "./replicache-transaction"; -import { EntryCache } from "./entry-cache"; -import { DBStorage } from "./db-storage"; +import { NullableVersion } from "../types/version"; +import { ReplicacheTransaction } from "../storage/replicache-transaction"; +import { EntryCache } from "../storage/entry-cache"; +import { DBStorage } from "../storage/db-storage"; setup(async () => { await withExecutor(async () => { diff --git a/backend/get-patch.ts b/backend/fastforward/get-patch.ts similarity index 83% rename from backend/get-patch.ts rename to backend/fastforward/get-patch.ts index 722bda6..aeb32e6 100644 --- a/backend/get-patch.ts +++ b/backend/fastforward/get-patch.ts @@ -1,8 +1,8 @@ import { z } from "zod"; -import { Patch } from "../protocol/poke"; -import { Executor } from "./pg"; -import { userValuePrefix, userValueSchema } from "./user-value"; -import { NullableVersion } from "./version"; +import { Patch } from "../../protocol/poke"; +import { Executor } from "../db/pg"; +import { userValuePrefix, userValueSchema } from "../types/user-value"; +import { NullableVersion } from "../types/version"; export async function getPatch( executor: Executor, diff --git a/backend/main.ts b/backend/main.ts index f472c2b..2a7c79b 100644 --- a/backend/main.ts +++ b/backend/main.ts @@ -3,7 +3,7 @@ import { Command } from "commander"; import { createServer, IncomingMessage, Server as NodeServer } from "http"; import { WebSocket } from "ws"; import { parse } from "url"; -import { Socket } from "./client-state"; +import { Socket } from "./types/client-state"; import { Server } from "./server"; const dev = process.env.NODE_ENV !== "production"; diff --git a/backend/process/process-mutation.test.ts b/backend/process/process-mutation.test.ts index d2ac985..68a1edf 100644 --- a/backend/process/process-mutation.test.ts +++ b/backend/process/process-mutation.test.ts @@ -2,16 +2,16 @@ import { ClientRecord, getClientRecord, putClientRecord, -} from "../client-record"; -import { MemStorage } from "../mem-storage"; -import { clientRecord, mutation } from "../test-utils"; +} from "../types/client-record"; +import { MemStorage } from "../storage/mem-storage"; +import { clientRecord, mutation } from "../util/test-utils"; import { expect } from "chai"; import { test } from "mocha"; import { JSONType } from "protocol/json"; import { Mutation } from "protocol/push"; import { WriteTransaction } from "replicache"; import { MutatorMap, processMutation } from "./process-mutation"; -import { getUserValue } from "../user-value"; +import { getUserValue } from "../types/user-value"; test("processMutation", async () => { type Case = { diff --git a/backend/process/process-mutation.ts b/backend/process/process-mutation.ts index 23c98a3..d8730b0 100644 --- a/backend/process/process-mutation.ts +++ b/backend/process/process-mutation.ts @@ -1,10 +1,10 @@ import { Mutation } from "../../protocol/push"; -import { ReplicacheTransaction } from "../replicache-transaction"; -import { Version } from "../version"; -import { Storage } from "../storage"; -import { EntryCache } from "../entry-cache"; -import { getClientRecord, putClientRecord } from "../client-record"; -import { ClientID } from "../client-state"; +import { ReplicacheTransaction } from "../storage/replicache-transaction"; +import { Version } from "../types/version"; +import { Storage } from "../storage/storage"; +import { EntryCache } from "../storage/entry-cache"; +import { getClientRecord, putClientRecord } from "../types/client-record"; +import { ClientID } from "../types/client-state"; export type Mutator = (tx: ReplicacheTransaction, args: any) => Promise; export type MutatorMap = Map; diff --git a/backend/server.test.ts b/backend/server.test.ts index 34a20e2..24752f8 100644 --- a/backend/server.test.ts +++ b/backend/server.test.ts @@ -4,12 +4,12 @@ import { ClientRecord, clientRecordKey, clientRecordSchema, -} from "./client-record"; -import { createDatabase, getEntry, putEntry } from "./data"; -import { transact, withExecutor } from "./pg"; +} from "./types/client-record"; +import { createDatabase, getEntry, putEntry } from "./db/data"; +import { transact, withExecutor } from "./db/pg"; import { Server } from "./server"; -import { RoomMap } from "./room-state"; -import { Socket } from "./client-state"; +import { RoomMap } from "./types/room-state"; +import { Socket } from "./types/client-state"; import { Mutation } from "../protocol/push"; import { client, @@ -19,7 +19,7 @@ import { room, roomMap, sleep, -} from "./test-utils"; +} from "./util/test-utils"; setup(async () => { await withExecutor(async () => { diff --git a/backend/server.ts b/backend/server.ts index 6bd0151..a088a8f 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -1,16 +1,16 @@ import { parse } from "url"; -import { RoomID, RoomMap } from "./room-state"; -import { Lock } from "./lock"; -import { ClientID, ClientState, Socket } from "./client-state"; +import { RoomID, RoomMap } from "./types/room-state"; +import { Lock } from "./util/lock"; +import { ClientID, ClientState, Socket } from "./types/client-state"; import { clientRecordKey, clientRecordSchema, ClientRecord, -} from "./client-record"; -import { DBStorage } from "./db-storage"; -import { transact } from "./pg"; +} from "./types/client-record"; +import { DBStorage } from "./storage/db-storage"; +import { transact } from "./db/pg"; import { PushBody } from "../protocol/push"; -import { sendError } from "./socket"; +import { sendError } from "./util/socket"; import { upstreamSchema } from "../protocol/up"; export type Now = () => number; diff --git a/backend/db-storage.test.ts b/backend/storage/db-storage.test.ts similarity index 88% rename from backend/db-storage.test.ts rename to backend/storage/db-storage.test.ts index cdb8e79..3596663 100644 --- a/backend/db-storage.test.ts +++ b/backend/storage/db-storage.test.ts @@ -1,10 +1,10 @@ -import { transact, withExecutor } from "./pg"; +import { transact, withExecutor } from "../db/pg"; import { expect } from "chai"; import { setup, test } from "mocha"; import { DBStorage } from "./db-storage"; -import { createDatabase, getEntry } from "./data"; +import { createDatabase, getEntry } from "../db/data"; import { z } from "zod"; -import { resolver } from "../frontend/resolver"; +import { resolver } from "../../frontend/resolver"; setup(async () => { await withExecutor(async () => { diff --git a/backend/db-storage.ts b/backend/storage/db-storage.ts similarity index 85% rename from backend/db-storage.ts rename to backend/storage/db-storage.ts index f955bb6..eab1da0 100644 --- a/backend/db-storage.ts +++ b/backend/storage/db-storage.ts @@ -1,7 +1,7 @@ import { JSONValue } from "replicache"; -import { delEntry, getEntry, putEntry } from "./data"; -import { Executor } from "./pg"; -import { Version } from "./version"; +import { delEntry, getEntry, putEntry } from "../db/data"; +import { Executor } from "../db/pg"; +import { Version } from "../types/version"; import type { Storage } from "./storage"; import { ZodSchema } from "zod"; diff --git a/backend/entry-cache.text.ts b/backend/storage/entry-cache.text.ts similarity index 98% rename from backend/entry-cache.text.ts rename to backend/storage/entry-cache.text.ts index 5153830..04b0b0c 100644 --- a/backend/entry-cache.text.ts +++ b/backend/storage/entry-cache.text.ts @@ -1,4 +1,4 @@ -import { transact } from "./pg"; +import { transact } from "../db/pg"; import { expect } from "chai"; import { test } from "mocha"; import { EntryCache } from "./entry-cache"; diff --git a/backend/entry-cache.ts b/backend/storage/entry-cache.ts similarity index 100% rename from backend/entry-cache.ts rename to backend/storage/entry-cache.ts diff --git a/backend/mem-storage.ts b/backend/storage/mem-storage.ts similarity index 100% rename from backend/mem-storage.ts rename to backend/storage/mem-storage.ts diff --git a/backend/replicache-transaction.test.ts b/backend/storage/replicache-transaction.test.ts similarity index 89% rename from backend/replicache-transaction.test.ts rename to backend/storage/replicache-transaction.test.ts index 3b2bfa8..4d4a4ba 100644 --- a/backend/replicache-transaction.test.ts +++ b/backend/storage/replicache-transaction.test.ts @@ -1,11 +1,11 @@ import { ReplicacheTransaction } from "./replicache-transaction"; -import { transact, withExecutor } from "./pg"; +import { transact, withExecutor } from "../db/pg"; import { expect } from "chai"; import { setup, test } from "mocha"; import { EntryCache } from "./entry-cache"; import { DBStorage } from "./db-storage"; -import { createDatabase, getEntry } from "./data"; -import { UserValue, userValueKey, userValueSchema } from "./user-value"; +import { createDatabase, getEntry } from "../db/data"; +import { UserValue, userValueKey, userValueSchema } from "../types/user-value"; import { MemStorage } from "./mem-storage"; test("ReplicacheTransaction", async () => { diff --git a/backend/replicache-transaction.ts b/backend/storage/replicache-transaction.ts similarity index 91% rename from backend/replicache-transaction.ts rename to backend/storage/replicache-transaction.ts index 449d2ea..fed033b 100644 --- a/backend/replicache-transaction.ts +++ b/backend/storage/replicache-transaction.ts @@ -1,9 +1,9 @@ import { JSONValue, ScanResult, WriteTransaction } from "replicache"; -import { Version } from "./version"; +import { Version } from "../types/version"; import { EntryCache } from "./entry-cache"; -import { UserValue, userValueKey, userValueSchema } from "./user-value"; +import { UserValue, userValueKey, userValueSchema } from "../types/user-value"; import { JSONType } from "protocol/json"; -import { ClientID } from "./client-state"; +import { ClientID } from "../types/client-state"; /** * Implements Replicache's WriteTransaction in terms of EntryCache. diff --git a/backend/storage.ts b/backend/storage/storage.ts similarity index 100% rename from backend/storage.ts rename to backend/storage/storage.ts diff --git a/backend/client-record.ts b/backend/types/client-record.ts similarity index 94% rename from backend/client-record.ts rename to backend/types/client-record.ts index 944a352..5cff05a 100644 --- a/backend/client-record.ts +++ b/backend/types/client-record.ts @@ -1,7 +1,7 @@ import { nullableVersionSchema } from "./version"; import { z } from "zod"; import { ClientID } from "./client-state"; -import { Storage } from "./storage"; +import { Storage } from "../storage/storage"; export const clientRecordSchema = z.object({ lastMutationID: z.number(), diff --git a/backend/client-state.ts b/backend/types/client-state.ts similarity index 100% rename from backend/client-state.ts rename to backend/types/client-state.ts diff --git a/backend/room-state.ts b/backend/types/room-state.ts similarity index 100% rename from backend/room-state.ts rename to backend/types/room-state.ts diff --git a/backend/user-value.ts b/backend/types/user-value.ts similarity index 88% rename from backend/user-value.ts rename to backend/types/user-value.ts index 23c916f..dd98b4a 100644 --- a/backend/user-value.ts +++ b/backend/types/user-value.ts @@ -1,6 +1,6 @@ -import { jsonSchema } from "../protocol/json"; +import { jsonSchema } from "../../protocol/json"; import { versionSchema } from "./version"; -import { Storage } from "./storage"; +import { Storage } from "../storage/storage"; import { z } from "zod"; export const userValueSchema = z.object({ diff --git a/backend/version.ts b/backend/types/version.ts similarity index 100% rename from backend/version.ts rename to backend/types/version.ts diff --git a/backend/lock.test.ts b/backend/util/lock.test.ts similarity index 95% rename from backend/lock.test.ts rename to backend/util/lock.test.ts index 7f84272..6c97cfd 100644 --- a/backend/lock.test.ts +++ b/backend/util/lock.test.ts @@ -1,6 +1,6 @@ import { expect } from "chai"; import { test } from "mocha"; -import { resolver } from "../frontend/resolver"; +import { resolver } from "../../frontend/resolver"; import { Lock } from "./lock"; import { sleep } from "./test-utils"; diff --git a/backend/lock.ts b/backend/util/lock.ts similarity index 92% rename from backend/lock.ts rename to backend/util/lock.ts index e0afa87..08e84e3 100644 --- a/backend/lock.ts +++ b/backend/util/lock.ts @@ -1,4 +1,4 @@ -import { resolver } from "../frontend/resolver"; +import { resolver } from "../../frontend/resolver"; // This is lifted from Replicache. Perhaps should be refactored into shared repo? // Same with resolver.ts. diff --git a/backend/socket.ts b/backend/util/socket.ts similarity index 79% rename from backend/socket.ts rename to backend/util/socket.ts index f731673..2939f05 100644 --- a/backend/socket.ts +++ b/backend/util/socket.ts @@ -1,5 +1,5 @@ import { Downstream } from "protocol/down"; -import { Socket } from "./client-state"; +import { Socket } from "../types/client-state"; export function sendError(ws: Socket, body: string) { const message: Downstream = ["error", body]; diff --git a/backend/test-utils.ts b/backend/util/test-utils.ts similarity index 87% rename from backend/test-utils.ts rename to backend/util/test-utils.ts index 573e582..ccdfad7 100644 --- a/backend/test-utils.ts +++ b/backend/util/test-utils.ts @@ -1,8 +1,8 @@ import { JSONType } from "protocol/json"; import { Mutation } from "protocol/push"; -import { ClientID, ClientState, Socket } from "./client-state"; -import { RoomID, RoomMap, RoomState } from "./room-state"; -import { NullableVersion } from "./version"; +import { ClientID, ClientState, Socket } from "../types/client-state"; +import { RoomID, RoomMap, RoomState } from "../types/room-state"; +import { NullableVersion } from "../types/version"; export function roomMap(...rooms: [RoomID, RoomState][]): RoomMap { return new Map(rooms); diff --git a/pages/api/init.ts b/pages/api/init.ts index dba576a..72282a3 100644 --- a/pages/api/init.ts +++ b/pages/api/init.ts @@ -1,4 +1,4 @@ -import { createDatabase } from "../../backend/data"; +import { createDatabase } from "../../backend/db/data"; import type { NextApiRequest, NextApiResponse } from "next"; export default async (req: NextApiRequest, res: NextApiResponse) => { diff --git a/pages/d/[id].tsx b/pages/d/[id].tsx index bc0c1e3..21d4b31 100644 --- a/pages/d/[id].tsx +++ b/pages/d/[id].tsx @@ -7,7 +7,6 @@ import { randUserInfo } from "../../frontend/client-state"; import { randomShape } from "../../frontend/shape"; import { PushMessage, PushBody } from "../../protocol/push"; import { resolver } from "frontend/resolver"; -import { nanoid } from "nanoid"; import { pokeMessageSchema } from "protocol/poke"; export default function Home() { @@ -31,7 +30,6 @@ export default function Home() { pusher: async (req) => { const ws = await socket; const pushBody = (await req.json()) as PushBody; - pushBody.id = nanoid(); const msg: PushMessage = ["push", pushBody]; ws.send(JSON.stringify(msg)); return { diff --git a/protocol/poke.ts b/protocol/poke.ts index 747a0a7..591cf84 100644 --- a/protocol/poke.ts +++ b/protocol/poke.ts @@ -1,4 +1,4 @@ -import { nullableVersionSchema, versionSchema } from "backend/version"; +import { nullableVersionSchema, versionSchema } from "backend/types/version"; import { z } from "zod"; import { jsonSchema } from "./json";