@spooky-sync/core 1.0.0-beta.1 → 1.0.0-beta.11

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 (55) hide show
  1. package/AGENTS.md +4 -3
  2. package/dist/index.d.ts +170 -9
  3. package/dist/index.js +5398 -2968
  4. package/dist/rows-worker.d.ts +1 -0
  5. package/dist/rows-worker.js +2731 -0
  6. package/dist/sqlite-open.js +1 -1
  7. package/dist/sqlite-worker.js +1 -1
  8. package/dist/types.d.ts +131 -13
  9. package/package.json +9 -4
  10. package/scripts/check-worker-entries.mjs +15 -2
  11. package/skills/sp00ky-core/SKILL.md +2 -0
  12. package/src/client/bucket-handle.ts +21 -1
  13. package/src/client/eager-paint.test.ts +82 -0
  14. package/src/client/services.ts +4 -1
  15. package/src/client/sp00ky-client.test.ts +143 -1
  16. package/src/client/sp00ky-client.ts +158 -19
  17. package/src/index.ts +9 -0
  18. package/src/kernel/effects.ts +12 -2
  19. package/src/kernel/interpreter.test.ts +17 -0
  20. package/src/kernel/interpreter.ts +5 -2
  21. package/src/modules/channel/hub.test.ts +223 -0
  22. package/src/modules/channel/hub.ts +355 -0
  23. package/src/modules/channel/index.ts +358 -14
  24. package/src/modules/channel/room.test.ts +340 -0
  25. package/src/modules/channel/room.ts +367 -0
  26. package/src/modules/channel/rooms.fixture.ts +383 -0
  27. package/src/modules/channel/share.test.ts +535 -0
  28. package/src/modules/channel/webrtc.ts +2 -2
  29. package/src/modules/devtools/storage-info.ts +4 -3
  30. package/src/query/materialize.saga.test.ts +16 -0
  31. package/src/query/materialize.saga.ts +7 -26
  32. package/src/query/materialize.ts +79 -3
  33. package/src/query/register.saga.test.ts +61 -0
  34. package/src/query/register.saga.ts +39 -2
  35. package/src/services/database/cache-engine.ts +17 -3
  36. package/src/services/database/engine-factory.ts +7 -0
  37. package/src/services/database/index.ts +1 -0
  38. package/src/services/database/rows-cache-engine.test.ts +1584 -0
  39. package/src/services/database/rows-cache-engine.ts +1203 -0
  40. package/src/services/database/rows-exec.integration.test.ts +589 -0
  41. package/src/services/database/rows-exec.ts +1027 -0
  42. package/src/services/database/rows-prewarm.ts +251 -0
  43. package/src/services/database/rows-store.test.ts +635 -0
  44. package/src/services/database/rows-store.ts +1208 -0
  45. package/src/services/database/rows-transport.fixture.ts +151 -0
  46. package/src/services/database/rows-worker.ts +607 -0
  47. package/src/services/database/rows-yield.test.ts +141 -0
  48. package/src/services/database/rows-yield.ts +92 -0
  49. package/src/services/database/sqlite-cache-engine.test.ts +22 -1
  50. package/src/services/database/sqlite-cache-engine.ts +55 -34
  51. package/src/services/database/sqlite-transport.ts +7 -5
  52. package/src/services/tabs/support.ts +6 -4
  53. package/src/types.ts +41 -6
  54. package/tsdown.config.ts +42 -1
  55. package/vitest.config.ts +4 -0
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Test-only: run the row worker's ops in-process behind the engine's
3
+ * transport, in place of a real Worker. Requests and replies go through
4
+ * `structuredClone` with their transfer lists, so a test sees what a real
5
+ * `postMessage` would do to them: class instances lose their prototype and a
6
+ * transferred buffer is gone from the sender's side.
7
+ *
8
+ * The lifecycle messages (`open`, `close`, `diag`) are answered here the way
9
+ * `rows-worker.ts` answers them, minus OPFS and Web Locks: a bucket's files
10
+ * are `MemoryFile`s kept in `disks`, so a reopen finds what was written.
11
+ */
12
+ import type { SqliteTransport } from './sqlite-transport';
13
+ import { MemoryFile, RowStore } from './rows-store';
14
+ import { ROWS_PROTOCOL, RowsExec, handleReaderOp, handleRowsOp, visibleTables } from './rows-exec';
15
+
16
+ export type RowsDisks = Map<string, { a: MemoryFile; b: MemoryFile }>;
17
+
18
+ export interface RowsHost {
19
+ disks: RowsDisks;
20
+ /** Every message the engine sent, in order. */
21
+ calls: { type: string; payload: any }[];
22
+ /** The store behind the open bucket, for assertions on the file itself. */
23
+ store(): RowStore | null;
24
+ /** The protocol number `open` answers with; a test changes it to play an old worker. */
25
+ proto: number;
26
+ /** The worker's answers themselves, for a second way in (a follower's port). */
27
+ answer: RowsAnswer;
28
+ }
29
+
30
+ /** What `rows-worker.ts` answers, for a caller that is not an engine (a fake Worker). */
31
+ export type RowsAnswer = (type: string, payload: any) => Promise<{ result: object; transfer?: Transferable[] }>;
32
+
33
+ /**
34
+ * A `Worker` whose messages are answered in-process: what the real transport
35
+ * needs to run unchanged. `proto` plays a worker built from another release.
36
+ */
37
+ export class FakeRowsWorker {
38
+ onmessage: ((ev: { data: unknown }) => void) | null = null;
39
+ onerror: ((ev: { message: string }) => void) | null = null;
40
+ onmessageerror: (() => void) | null = null;
41
+ terminated = false;
42
+ posted: { type: string; id: number }[] = [];
43
+ private answer: RowsAnswer;
44
+ /** One message at a time, as the worker's own queue has it. */
45
+ private chain: Promise<unknown> = Promise.resolve();
46
+
47
+ constructor(disks: RowsDisks, private proto?: number) {
48
+ this.answer = rowsAnswers(disks, { proto: ROWS_PROTOCOL } as RowsHost);
49
+ }
50
+
51
+ postMessage(msg: { id: number; type: string; payload: unknown }, transfer?: Transferable[]): void {
52
+ this.posted.push({ type: msg.type, id: msg.id });
53
+ const payload = structuredClone(msg.payload, { transfer: transfer ?? [] });
54
+ this.chain = this.chain
55
+ .then(() => this.answer(msg.type, payload))
56
+ .then(
57
+ ({ result, transfer: back }) => {
58
+ const reply = structuredClone(result, { transfer: back ?? [] }) as Record<string, unknown>;
59
+ if (msg.type === 'open' && this.proto !== undefined) reply.proto = this.proto;
60
+ this.onmessage?.({ data: { id: msg.id, ok: true, wt: 0, ...reply } });
61
+ },
62
+ (error: unknown) => this.onmessage?.({ data: { id: msg.id, ok: false, error: String(error) } })
63
+ );
64
+ }
65
+
66
+ terminate(): void {
67
+ this.terminated = true;
68
+ }
69
+ }
70
+
71
+ export function inProcessRows(engine: unknown, disks: RowsDisks = new Map()): RowsHost {
72
+ const host = { disks, calls: [], store: () => null, proto: ROWS_PROTOCOL } as unknown as RowsHost;
73
+ const answer = rowsAnswers(disks, host);
74
+ host.answer = answer;
75
+
76
+ (engine as { createTransport: () => SqliteTransport }).createTransport = () => {
77
+ let closed = false;
78
+ return {
79
+ kind: 'worker',
80
+ get connected() {
81
+ return !closed;
82
+ },
83
+ call: async <T>(type: string, payload?: unknown, transfer?: Transferable[]) => {
84
+ const received = structuredClone(payload, { transfer: transfer ?? [] });
85
+ host.calls.push({ type, payload: received });
86
+ const { result, transfer: back } = await answer(type, received);
87
+ return structuredClone(result, { transfer: back ?? [] }) as T;
88
+ },
89
+ failAll() {},
90
+ close() {
91
+ closed = true;
92
+ },
93
+ } as SqliteTransport;
94
+ };
95
+ return host;
96
+ }
97
+
98
+ /** The worker's answers over `disks`. `host.store` is pointed at the open store. */
99
+ function rowsAnswers(disks: RowsDisks, host: RowsHost): RowsAnswer {
100
+ let store: RowStore | null = null;
101
+ let exec: RowsExec | null = null;
102
+ let openedAs: string | null = null;
103
+ let reading = false;
104
+ host.store = () => store;
105
+ return async (type, payload) => {
106
+ switch (type) {
107
+ case 'open': {
108
+ // As in the worker: a second open of the open bucket changes nothing.
109
+ const key = `${payload.dbName}${payload.reader ? ' (reader)' : ''}`;
110
+ if (!store || openedAs !== key) {
111
+ store?.checkpoint();
112
+ if (!disks.has(payload.dbName)) disks.set(payload.dbName, { a: new MemoryFile(), b: new MemoryFile() });
113
+ const files = disks.get(payload.dbName)!;
114
+ reading = payload.reader === true;
115
+ store = await RowStore.open({ open: (name) => files[name] }, { reader: reading });
116
+ exec = new RowsExec(store, reading);
117
+ openedAs = key;
118
+ }
119
+ return { result: { persisted: payload.useOpfs === true, proto: host.proto, workerUrl: 'https://app.test/assets/rows-worker.js' } };
120
+ }
121
+ case 'warm':
122
+ exec?.warm();
123
+ return { result: {} };
124
+ case 'close':
125
+ case 'shutdown':
126
+ store?.checkpoint();
127
+ store = null;
128
+ exec = null;
129
+ openedAs = null;
130
+ return { result: {} };
131
+ case 'relock':
132
+ return { result: {} };
133
+ case 'diag': {
134
+ if (!store) throw new Error('rows: store not open');
135
+ const open = store;
136
+ return {
137
+ result: {
138
+ stats: open.stats(),
139
+ tableCounts: payload?.tableCounts ? visibleTables(open).map((table) => ({ table, rows: open.count(table) })) : undefined,
140
+ },
141
+ };
142
+ }
143
+ default: {
144
+ if (!store || !exec) throw new Error('rows: store not open');
145
+ const reply = reading ? await handleReaderOp(exec, store, type, payload) : await handleRowsOp(exec, store, type, payload);
146
+ if (!reply) throw new Error(`rows worker: unknown message ${type}`);
147
+ return reply;
148
+ }
149
+ }
150
+ };
151
+ }