durable-db 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +1052 -0
  2. package/dist/cli/bin.d.ts +1 -0
  3. package/dist/cli/bin.js +18 -0
  4. package/dist/cli/index.d.ts +330 -0
  5. package/dist/cli/index.js +9 -0
  6. package/dist/cli-Cr9686BF.js +630 -0
  7. package/dist/context/index.d.ts +2 -0
  8. package/dist/context/index.js +5 -0
  9. package/dist/context-BuIiIQ6K.js +103 -0
  10. package/dist/db/index.d.ts +3 -0
  11. package/dist/db/index.js +760 -0
  12. package/dist/debug-BZJbKybf.js +49 -0
  13. package/dist/defineDatabase-CONf4xFC.js +54 -0
  14. package/dist/defineDatabase-jBv90-Pk.d.ts +188 -0
  15. package/dist/generator-BUPP9scJ.js +180 -0
  16. package/dist/index-BFM5d0FP.d.ts +328 -0
  17. package/dist/index-CqVMaMYY.d.ts +65 -0
  18. package/dist/index.d.ts +6 -0
  19. package/dist/index.js +8 -0
  20. package/dist/migrations/generator.d.ts +101 -0
  21. package/dist/migrations/generator.js +5 -0
  22. package/dist/migrations/snapshot.d.ts +2 -0
  23. package/dist/migrations/snapshot.js +3 -0
  24. package/dist/parser-CbX54Flt.js +293 -0
  25. package/dist/protocol-g_plhPOY.js +35 -0
  26. package/dist/push-BgQ5cnfH.js +184 -0
  27. package/dist/push-BkkaslPd.js +8 -0
  28. package/dist/registry.d.ts +28 -0
  29. package/dist/registry.js +72 -0
  30. package/dist/schema-BOtrGnzM.js +8 -0
  31. package/dist/schema-CpzTOfZ6.d.ts +6 -0
  32. package/dist/schema.d.ts +2 -0
  33. package/dist/schema.js +3 -0
  34. package/dist/snapshot-C162qYoA.d.ts +125 -0
  35. package/dist/snapshot-D170nL7i.js +93 -0
  36. package/dist/state-BCcvNN-M.js +5 -0
  37. package/dist/state-FOEjpgJ6.js +203 -0
  38. package/dist/transport/protocol.d.ts +27 -0
  39. package/dist/transport/protocol.js +3 -0
  40. package/dist/transport/websocket.d.ts +31 -0
  41. package/dist/transport/websocket.js +3 -0
  42. package/dist/vite/durableDb.d.ts +20 -0
  43. package/dist/vite/durableDb.js +933 -0
  44. package/dist/websocket-Dx1fqwnF.js +104 -0
  45. package/package.json +114 -0
  46. package/virtual.d.ts +15 -0
@@ -0,0 +1,104 @@
1
+ import { n as decodeResponse, r as encodeRequest } from "./protocol-g_plhPOY.js";
2
+
3
+ //#region src/transport/websocket.ts
4
+ /** Default timeout (ms) after which a pending WebSocket request rejects. */
5
+ const DEFAULT_WS_REQUEST_TIMEOUT_MS = 3e4;
6
+ /**
7
+ * Client-side WebSocket transport for calling DO actions over WebSocket.
8
+ *
9
+ * Manages a single WebSocket connection to a Durable Object stub,
10
+ * with request-response correlation via message IDs.
11
+ */
12
+ var WebSocketTransport = class {
13
+ ws = null;
14
+ pending = /* @__PURE__ */ new Map();
15
+ connectPromise = null;
16
+ requestTimeoutMs;
17
+ constructor(stub, options = {}) {
18
+ this.stub = stub;
19
+ this.requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_WS_REQUEST_TIMEOUT_MS;
20
+ }
21
+ async call(action, args, instanceKey) {
22
+ await this.ensureConnected();
23
+ const id = crypto.randomUUID();
24
+ const message = encodeRequest({
25
+ id,
26
+ action,
27
+ args,
28
+ instanceKey
29
+ });
30
+ return new Promise((resolve, reject) => {
31
+ const timer = this.requestTimeoutMs > 0 && Number.isFinite(this.requestTimeoutMs) ? setTimeout(() => {
32
+ const entry = this.pending.get(id);
33
+ if (!entry) return;
34
+ this.pending.delete(id);
35
+ entry.reject(/* @__PURE__ */ new Error(`[db] WebSocket request '${action}' timed out after ${this.requestTimeoutMs}ms`));
36
+ }, this.requestTimeoutMs) : null;
37
+ this.pending.set(id, {
38
+ resolve,
39
+ reject,
40
+ timer
41
+ });
42
+ this.ws.send(message);
43
+ });
44
+ }
45
+ async ensureConnected() {
46
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) return;
47
+ if (this.connectPromise) return this.connectPromise;
48
+ this.connectPromise = this.connect();
49
+ try {
50
+ await this.connectPromise;
51
+ } finally {
52
+ this.connectPromise = null;
53
+ }
54
+ }
55
+ async connect() {
56
+ const ws = (await this.stub.fetch(new Request("https://do/ws", { headers: { Upgrade: "websocket" } }))).webSocket;
57
+ if (!ws) throw new Error("WebSocket upgrade failed: no webSocket on response");
58
+ ws.accept();
59
+ this.ws = ws;
60
+ ws.addEventListener("message", (event) => {
61
+ this.onMessage(event);
62
+ });
63
+ ws.addEventListener("close", () => {
64
+ this.onClose();
65
+ });
66
+ ws.addEventListener("error", () => {
67
+ this.onClose();
68
+ });
69
+ }
70
+ onMessage(event) {
71
+ const raw = typeof event.data === "string" ? event.data : String(event.data);
72
+ let response;
73
+ try {
74
+ response = decodeResponse(raw);
75
+ } catch (e) {
76
+ const message = e instanceof Error ? e.message : String(e);
77
+ console.warn(`[db] WebSocket received malformed message (ignored): ${message}. First 120 chars: ${raw.slice(0, 120)}`);
78
+ return;
79
+ }
80
+ const pending = this.pending.get(response.id);
81
+ if (!pending) return;
82
+ this.pending.delete(response.id);
83
+ if (pending.timer) clearTimeout(pending.timer);
84
+ if (response.ok) pending.resolve(response.result);
85
+ else pending.reject(new Error(response.error));
86
+ }
87
+ onClose() {
88
+ this.ws = null;
89
+ for (const [, entry] of this.pending) {
90
+ if (entry.timer) clearTimeout(entry.timer);
91
+ entry.reject(/* @__PURE__ */ new Error("WebSocket connection closed"));
92
+ }
93
+ this.pending.clear();
94
+ }
95
+ close() {
96
+ if (this.ws) {
97
+ this.ws.close(1e3, "Client closing");
98
+ this.ws = null;
99
+ }
100
+ }
101
+ };
102
+
103
+ //#endregion
104
+ export { WebSocketTransport as n, DEFAULT_WS_REQUEST_TIMEOUT_MS as t };
package/package.json ADDED
@@ -0,0 +1,114 @@
1
+ {
2
+ "name": "durable-db",
3
+ "version": "0.0.1",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/eagerpatch/durable-db.git"
7
+ },
8
+ "type": "module",
9
+ "packageManager": "pnpm@10.2.1",
10
+ "bin": {
11
+ "db": "./dist/cli/bin.js"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ },
18
+ "./db": {
19
+ "types": "./dist/db/index.d.ts",
20
+ "import": "./dist/db/index.js"
21
+ },
22
+ "./context": {
23
+ "types": "./dist/context/index.d.ts",
24
+ "import": "./dist/context/index.js"
25
+ },
26
+ "./migrations/snapshot": {
27
+ "types": "./dist/migrations/snapshot.d.ts",
28
+ "import": "./dist/migrations/snapshot.js"
29
+ },
30
+ "./migrations/generator": {
31
+ "types": "./dist/migrations/generator.d.ts",
32
+ "import": "./dist/migrations/generator.js"
33
+ },
34
+ "./vite": {
35
+ "types": "./dist/vite/durableDb.d.ts",
36
+ "import": "./dist/vite/durableDb.js"
37
+ },
38
+ "./registry": {
39
+ "types": "./dist/registry.d.ts",
40
+ "import": "./dist/registry.js"
41
+ },
42
+ "./transport/protocol": {
43
+ "types": "./dist/transport/protocol.d.ts",
44
+ "import": "./dist/transport/protocol.js"
45
+ },
46
+ "./transport/websocket": {
47
+ "types": "./dist/transport/websocket.d.ts",
48
+ "import": "./dist/transport/websocket.js"
49
+ },
50
+ "./schema": {
51
+ "types": "./dist/schema.d.ts",
52
+ "import": "./dist/schema.js"
53
+ },
54
+ "./cli": {
55
+ "types": "./dist/cli/index.d.ts",
56
+ "import": "./dist/cli/index.js"
57
+ },
58
+ "./virtual": {
59
+ "types": "./virtual.d.ts"
60
+ }
61
+ },
62
+ "files": [
63
+ "dist",
64
+ "virtual.d.ts"
65
+ ],
66
+ "publishConfig": {
67
+ "access": "public"
68
+ },
69
+ "scripts": {
70
+ "build": "tsdown",
71
+ "dev": "tsdown --watch",
72
+ "test": "vitest",
73
+ "test:run": "vitest run",
74
+ "changeset": "changeset"
75
+ },
76
+ "dependencies": {
77
+ "@babel/generator": "^7.0.0",
78
+ "@babel/parser": "^7.0.0",
79
+ "@babel/traverse": "^7.0.0",
80
+ "@babel/types": "^7.0.0",
81
+ "@outerbase/browsable-durable-object": "^0.1.0",
82
+ "arktype": "^2.1.28",
83
+ "commander": "^13.1.0",
84
+ "debug": "^4.4.3",
85
+ "drizzle-kit": "^0.30.0",
86
+ "drizzle-orm": "^0.38.0",
87
+ "esbuild": "^0.27.1",
88
+ "kysely": "^0.27.0",
89
+ "libsql": "^0.5.0",
90
+ "magic-string": "^0.30.21"
91
+ },
92
+ "peerDependencies": {
93
+ "vite": "^5.0.0 || ^6.0.0 || ^7.0.0"
94
+ },
95
+ "devDependencies": {
96
+ "@babel/generator": "^7.26.0",
97
+ "@babel/parser": "^7.26.0",
98
+ "@babel/traverse": "^7.26.0",
99
+ "@babel/types": "^7.26.0",
100
+ "@changesets/cli": "^2.29.8",
101
+ "@cloudflare/vitest-pool-workers": "^0.8.71",
102
+ "@cloudflare/workers-types": "^4.20241127.0",
103
+ "@types/babel__generator": "^7.6.8",
104
+ "@types/babel__traverse": "^7.20.6",
105
+ "@types/debug": "^4.1.12",
106
+ "@types/node": "^24.0.0",
107
+ "drizzle-kit": "^0.30.0",
108
+ "esbuild": "^0.27.1",
109
+ "tsdown": "^0.17.2",
110
+ "typescript": "^5.7.0",
111
+ "vite": "^7.0.0",
112
+ "vitest": "^2.1.0"
113
+ }
114
+ }
package/virtual.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ // Ambient module declarations for the virtual modules generated by the Vite plugin.
2
+
3
+ // Exports are DurableObject subclasses with names derived from database filenames
4
+ // (e.g. main.ts -> MainDatabaseDO).
5
+ declare module "virtual:durable-db/__durableObjects";
6
+
7
+ // Dev-epoch helpers baked in at build time. `applyDevEpoch` suffixes instance
8
+ // keys with the current dev epoch (identity in production builds).
9
+ declare module "virtual:durable-db/__devEpoch" {
10
+ export const devEpoch: string | null;
11
+ export function applyDevEpoch(key: string): string;
12
+ }
13
+
14
+ /** @deprecated Use "virtual:durable-db/__durableObjects" instead. */
15
+ declare module "virtual:eagerpatch/durable-db/__durableObjects";