taladb 0.10.2 → 0.11.0

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.
package/dist/index.js CHANGED
@@ -30,57 +30,297 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- COVERAGE_COLLECTION: () => COVERAGE_COLLECTION,
34
- CoverageStore: () => CoverageStore,
35
- HttpSyncAdapter: () => HttpSyncAdapter,
36
- REPLICA_REVISION_FIELD: () => REPLICA_REVISION_FIELD,
37
- REPLICA_SCOPE_FIELD: () => REPLICA_SCOPE_FIELD,
38
- ReplicationCoordinator: () => ReplicationCoordinator,
39
33
  TalaDbValidationError: () => TalaDbValidationError,
40
34
  applySchema: () => applySchema,
41
- coverageKey: () => coverageKey,
42
- createRestSource: () => createRestSource,
35
+ createWebhookDispatcher: () => createWebhookDispatcher,
36
+ decorateCollection: () => decorateCollection,
43
37
  deriveDocId: () => deriveDocId,
44
- isAuthoritative: () => isAuthoritative,
45
38
  openDB: () => openDB,
46
- progress: () => progress,
47
- rowsApplied: () => rowsApplied,
48
- runMigrations: () => runMigrations
39
+ runMigrations: () => runMigrations,
40
+ validateWebhookConfig: () => validateWebhookConfig
49
41
  });
50
42
  module.exports = __toCommonJS(index_exports);
51
43
 
52
- // src/config.ts
53
- var ENDPOINT_FIELDS = [
44
+ // src/webhook.ts
45
+ var METHOD = {
46
+ insert: "POST",
47
+ update: "PUT",
48
+ delete: "DELETE"
49
+ };
50
+ var ENDPOINT_KEYS = [
54
51
  "endpoint",
55
52
  "insert_endpoint",
56
53
  "update_endpoint",
57
54
  "delete_endpoint"
58
55
  ];
59
- var LOCALHOST_HOSTS = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]", "::1"]);
60
- function isLocalhostUrl(url) {
56
+ var LOCALHOST = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]", "::1"]);
57
+ function isLocalhost(url) {
61
58
  try {
62
- return LOCALHOST_HOSTS.has(new URL(url).hostname);
59
+ return LOCALHOST.has(new URL(url).hostname);
63
60
  } catch {
64
61
  return false;
65
62
  }
66
63
  }
67
- function validateConfig(config) {
68
- const sync = config.sync;
69
- if (!sync) return;
70
- for (const key of ENDPOINT_FIELDS) {
71
- const url = sync[key];
72
- if (url !== void 0 && !url.startsWith("http://") && !url.startsWith("https://")) {
64
+ function validateWebhookConfig(config) {
65
+ for (const key of ENDPOINT_KEYS) {
66
+ const url = config[key];
67
+ if (url === void 0) continue;
68
+ if (!url.startsWith("http://") && !url.startsWith("https://")) {
73
69
  throw new Error(
74
- `TalaDB config: invalid endpoint URL "${url}" \u2014 must start with http:// or https://`
70
+ `TalaDB webhook: invalid ${key} "${url}" \u2014 must start with http:// or https://`
75
71
  );
76
72
  }
77
- if (url?.startsWith("http://") && !isLocalhostUrl(url)) {
73
+ if (url.startsWith("http://") && !isLocalhost(url)) {
78
74
  console.warn(
79
- `[TalaDB] sync endpoint "${url}" uses plaintext HTTP \u2014 use HTTPS in production to prevent changeset interception`
75
+ `[TalaDB] webhook ${key} "${url}" uses plaintext HTTP \u2014 document bodies will cross the network unencrypted. Use HTTPS in production.`
76
+ );
77
+ }
78
+ }
79
+ if (config.enabled && !config.endpoint) {
80
+ const perOp = ENDPOINT_KEYS.slice(1).every((k) => config[k] !== void 0);
81
+ if (!perOp) {
82
+ throw new Error(
83
+ "TalaDB webhook: `enabled: true` requires `endpoint` (or all three per-op endpoints)"
80
84
  );
81
85
  }
82
86
  }
83
87
  }
88
+ var DEFAULT_MAX_QUEUE = 512;
89
+ var DEFAULT_RETRIES = 3;
90
+ var BACKOFF_BASE_MS = 200;
91
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
92
+ function stripFields(doc, exclude) {
93
+ if (exclude.size === 0) return doc;
94
+ const out = {};
95
+ for (const [k, v] of Object.entries(doc)) {
96
+ if (!exclude.has(k)) out[k] = v;
97
+ }
98
+ return out;
99
+ }
100
+ function createWebhookDispatcher(config) {
101
+ if (!config?.enabled) return null;
102
+ validateWebhookConfig(config);
103
+ const fetchFn = config.fetch ?? globalThis.fetch?.bind(globalThis);
104
+ if (!fetchFn) {
105
+ throw new Error(
106
+ "TalaDB webhook: no global fetch available. Pass `fetch` in the webhook config."
107
+ );
108
+ }
109
+ const headers = { "content-type": "application/json", ...config.headers ?? {} };
110
+ const exclude = new Set(config.exclude_fields ?? []);
111
+ const only = config.collections ? new Set(config.collections) : null;
112
+ const maxQueue = config.max_queue ?? DEFAULT_MAX_QUEUE;
113
+ const retries = config.retries ?? DEFAULT_RETRIES;
114
+ const stats = { pending: 0, delivered: 0, failed: 0, dropped: 0 };
115
+ const chains = /* @__PURE__ */ new Map();
116
+ let eventSequence = 0;
117
+ function nextEventId() {
118
+ eventSequence++;
119
+ return `${Date.now().toString(36)}-${eventSequence.toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
120
+ }
121
+ function endpointFor(op) {
122
+ return config[`${op}_endpoint`] ?? config.endpoint;
123
+ }
124
+ function bodyFor(event, at, eventId) {
125
+ return JSON.stringify({
126
+ event_id: eventId,
127
+ collection: event.collection,
128
+ id: event.id,
129
+ timestamp: at,
130
+ document: event.document ? stripFields(event.document, exclude) : null
131
+ });
132
+ }
133
+ async function deliver(event, at, eventId) {
134
+ const url = endpointFor(event.op);
135
+ const init = {
136
+ method: METHOD[event.op],
137
+ headers: { ...headers, "Idempotency-Key": eventId },
138
+ body: bodyFor(event, at, eventId)
139
+ };
140
+ for (let attempt = 0; attempt <= retries; attempt++) {
141
+ try {
142
+ const res = await fetchFn(url, init);
143
+ if (res.ok) {
144
+ stats.delivered++;
145
+ return;
146
+ }
147
+ if (res.status >= 400 && res.status < 500) {
148
+ stats.failed++;
149
+ console.warn(
150
+ `[TalaDB] webhook ${event.op} ${event.collection}/${event.id} \u2192 ${res.status} ${res.statusText} (not retried)`
151
+ );
152
+ return;
153
+ }
154
+ } catch {
155
+ }
156
+ if (attempt < retries) await sleep(BACKOFF_BASE_MS * 2 ** attempt);
157
+ }
158
+ stats.failed++;
159
+ console.warn(
160
+ `[TalaDB] webhook ${event.op} ${event.collection}/${event.id} failed after ${retries + 1} attempts`
161
+ );
162
+ }
163
+ return {
164
+ reports(collection) {
165
+ if (collection.startsWith("_")) return false;
166
+ return only === null || only.has(collection);
167
+ },
168
+ emit(event) {
169
+ if (!this.reports(event.collection)) return;
170
+ if (stats.pending >= maxQueue) {
171
+ stats.dropped++;
172
+ console.warn(
173
+ `[TalaDB] webhook queue full (${maxQueue}) \u2014 dropped ${event.op} ${event.collection}/${event.id}. The endpoint is not keeping up.`
174
+ );
175
+ return;
176
+ }
177
+ stats.pending++;
178
+ const at = event.committedAt ?? Date.now();
179
+ const eventId = nextEventId();
180
+ const key = `${event.collection}\0${event.id}`;
181
+ const prior = chains.get(key) ?? Promise.resolve();
182
+ const next = prior.then(() => deliver(event, at, eventId)).finally(() => {
183
+ stats.pending--;
184
+ if (chains.get(key) === next) chains.delete(key);
185
+ });
186
+ chains.set(key, next);
187
+ },
188
+ async flush(timeoutMs = 5e3) {
189
+ const deadline = Date.now() + timeoutMs;
190
+ while (stats.pending > 0) {
191
+ const remaining = deadline - Date.now();
192
+ if (remaining <= 0) break;
193
+ let timer;
194
+ const expired = new Promise((resolve) => {
195
+ timer = setTimeout(resolve, remaining);
196
+ });
197
+ try {
198
+ await Promise.race([Promise.allSettled([...chains.values()]), expired]);
199
+ } finally {
200
+ clearTimeout(timer);
201
+ }
202
+ }
203
+ return stats.pending === 0;
204
+ },
205
+ stats() {
206
+ return { ...stats };
207
+ }
208
+ };
209
+ }
210
+ function byIds(ids) {
211
+ return ids.length === 1 ? { _id: ids[0] } : { _id: { $in: ids } };
212
+ }
213
+ function idFromFilter(filter) {
214
+ if (filter === null || typeof filter !== "object") return null;
215
+ const keys = Object.keys(filter);
216
+ if (keys.length !== 1 || keys[0] !== "_id") return null;
217
+ const id = filter._id;
218
+ return typeof id === "string" ? id : null;
219
+ }
220
+ function wrapCollectionWithWebhook(col, collection, webhook) {
221
+ if (!webhook.reports(collection)) return col;
222
+ async function idsFor(filter, limitOne) {
223
+ const fast = idFromFilter(filter);
224
+ if (fast !== null) return [fast];
225
+ if (limitOne) {
226
+ const doc = await col.findOne(filter);
227
+ return doc && typeof doc._id === "string" ? [doc._id] : [];
228
+ }
229
+ const docs = col.aggregate ? await col.aggregate([{ $match: filter }, { $project: { _id: 1 } }]) : await col.find(filter);
230
+ return docs.map((d) => d._id).filter((id) => typeof id === "string");
231
+ }
232
+ async function docsFor(filter, limitOne) {
233
+ if (limitOne) {
234
+ const doc = await col.findOne(filter);
235
+ return doc === null ? [] : [doc];
236
+ }
237
+ return col.find(filter);
238
+ }
239
+ async function emitPostImages(ids, op, committedAt) {
240
+ if (ids.length === 0) return;
241
+ const docs = await col.find(byIds(ids));
242
+ const byId = /* @__PURE__ */ new Map();
243
+ for (const doc of docs) {
244
+ if (typeof doc._id === "string") byId.set(doc._id, doc);
245
+ }
246
+ for (const id of ids) {
247
+ const doc = byId.get(id);
248
+ if (doc === void 0) {
249
+ webhook.emit({ op: "delete", collection, id, document: null, committedAt });
250
+ } else {
251
+ webhook.emit({ op, collection, id, document: doc, committedAt });
252
+ }
253
+ }
254
+ }
255
+ return {
256
+ ...col,
257
+ async insert(doc) {
258
+ const id = await col.insert(doc);
259
+ const committedAt = Date.now();
260
+ await emitPostImages([id], "insert", committedAt);
261
+ return id;
262
+ },
263
+ async insertMany(docs) {
264
+ const ids = await col.insertMany(docs);
265
+ const committedAt = Date.now();
266
+ await emitPostImages(ids, "insert", committedAt);
267
+ return ids;
268
+ },
269
+ async updateOne(filter, update) {
270
+ const ids = await idsFor(filter, true);
271
+ const changed = await col.updateOne(filter, update);
272
+ const committedAt = Date.now();
273
+ if (changed) await emitPostImages(ids, "update", committedAt);
274
+ return changed;
275
+ },
276
+ async updateMany(filter, update) {
277
+ const ids = await idsFor(filter, false);
278
+ const n = await col.updateMany(filter, update);
279
+ const committedAt = Date.now();
280
+ if (n > 0) await emitPostImages(ids, "update", committedAt);
281
+ return n;
282
+ },
283
+ async deleteOne(filter) {
284
+ const docs = await docsFor(filter, true);
285
+ const deleted = await col.deleteOne(filter);
286
+ const committedAt = Date.now();
287
+ if (deleted) {
288
+ for (const document of docs) {
289
+ webhook.emit({
290
+ op: "delete",
291
+ collection,
292
+ id: document._id,
293
+ document,
294
+ committedAt
295
+ });
296
+ }
297
+ }
298
+ return deleted;
299
+ },
300
+ async deleteMany(filter) {
301
+ const docs = await docsFor(filter, false);
302
+ const n = await col.deleteMany(filter);
303
+ const committedAt = Date.now();
304
+ if (n > 0) {
305
+ for (const document of docs) {
306
+ webhook.emit({
307
+ op: "delete",
308
+ collection,
309
+ id: document._id,
310
+ document,
311
+ committedAt
312
+ });
313
+ }
314
+ }
315
+ return n;
316
+ }
317
+ };
318
+ }
319
+
320
+ // src/config.ts
321
+ function validateConfig(config) {
322
+ if (config.webhook) validateWebhookConfig(config.webhook);
323
+ }
84
324
  async function loadConfig(configPath) {
85
325
  if (typeof process === "undefined" || typeof process.cwd !== "function") {
86
326
  return {};
@@ -129,146 +369,6 @@ async function loadConfig(configPath) {
129
369
  return {};
130
370
  }
131
371
 
132
- // src/sync.ts
133
- var CURSOR_COLLECTION = "__taladb_sync";
134
- async function resolveCollections(handle, options) {
135
- const base = options.collections ?? await handle.listCollectionNames();
136
- const excluded = new Set(options.exclude ?? []);
137
- return base.filter((c) => !excluded.has(c) && !c.startsWith("_"));
138
- }
139
- function unsupportedSync(runtime) {
140
- const err = () => new Error(
141
- `TalaDB sync is not yet available on the ${runtime} runtime (Node.js is supported today; browser and React Native are in progress). Track it on the roadmap.`
142
- );
143
- return {
144
- sync: () => Promise.reject(err()),
145
- exportChanges: () => Promise.reject(err()),
146
- importChanges: () => Promise.reject(err())
147
- };
148
- }
149
- async function readCursor(cursorCol, target) {
150
- const doc = await cursorCol.findOne({ target });
151
- return {
152
- pushMs: doc?.pushMs ?? 0,
153
- pullMs: doc?.pullMs ?? 0,
154
- pullCursor: doc?.pullCursor
155
- };
156
- }
157
- async function writeCursor(cursorCol, target, cursor) {
158
- const updated = await cursorCol.updateOne({ target }, { $set: { ...cursor } });
159
- if (!updated) {
160
- await cursorCol.insert({ target, ...cursor });
161
- }
162
- }
163
- function isCursorAdapter(adapter) {
164
- return typeof adapter.pullWithCursor === "function";
165
- }
166
- var MAX_PULL_PAGES = 1e4;
167
- async function runSync(handle, adapter, options, syncSchemas = {}) {
168
- const direction = options.direction ?? "both";
169
- const target = options.target ?? "default";
170
- const doPush = direction === "push" || direction === "both";
171
- const doPull = direction === "pull" || direction === "both";
172
- if (doPull && !adapter.pull && !isCursorAdapter(adapter)) {
173
- throw new Error(
174
- `sync direction '${direction}' requires adapter.pull() or adapter.pullWithCursor()`
175
- );
176
- }
177
- if (doPush && !adapter.push) {
178
- throw new Error(`sync direction '${direction}' requires adapter.push()`);
179
- }
180
- const collections = await resolveCollections(handle, options);
181
- const cursorCol = handle.collection(CURSOR_COLLECTION);
182
- const cursor = await readCursor(cursorCol, target);
183
- const local = doPush ? await handle.exportChanges(collections, 0) : "[]";
184
- const scopedSchemas = {};
185
- for (const c of collections) {
186
- if (syncSchemas[c]) scopedSchemas[c] = syncSchemas[c];
187
- }
188
- const useValidated = handle.importChangesValidated && Object.keys(scopedSchemas).length > 0;
189
- let pulled = 0;
190
- let skipped = 0;
191
- let quarantined = 0;
192
- let pullCursor = cursor.pullCursor;
193
- async function importOne(changeset) {
194
- if (!changeset || changeset === "[]") return;
195
- if (useValidated) {
196
- const report = await handle.importChangesValidated(changeset, JSON.stringify(scopedSchemas));
197
- pulled += report.applied;
198
- skipped += report.skipped;
199
- quarantined += report.quarantined;
200
- } else {
201
- pulled += await handle.importChanges(changeset);
202
- }
203
- }
204
- if (doPull) {
205
- if (isCursorAdapter(adapter)) {
206
- let pages = 0;
207
- for (; ; ) {
208
- const result = await adapter.pullWithCursor(pullCursor ?? null);
209
- await importOne(result.changeset);
210
- pullCursor = result.cursor;
211
- await writeCursor(cursorCol, target, { ...cursor, pullCursor });
212
- if (!result.hasMore) break;
213
- if (++pages >= MAX_PULL_PAGES) {
214
- throw new Error(
215
- `sync: origin returned hasMore after ${MAX_PULL_PAGES} pages for target '${target}' \u2014 it is probably not advancing its cursor.`
216
- );
217
- }
218
- }
219
- } else {
220
- await importOne(await adapter.pull(0));
221
- }
222
- }
223
- let pushed = 0;
224
- if (doPush && local !== "[]") {
225
- pushed = JSON.parse(local).length;
226
- await adapter.push(local);
227
- }
228
- await writeCursor(cursorCol, target, {
229
- pushMs: cursor.pushMs,
230
- pullMs: cursor.pullMs,
231
- ...pullCursor !== void 0 ? { pullCursor } : {}
232
- });
233
- return { pushed, pulled, skipped, quarantined, cursor: 0 };
234
- }
235
-
236
- // src/http-adapter.ts
237
- var HttpSyncAdapter = class {
238
- constructor(options) {
239
- this.endpoint = options.endpoint.replace(/\/$/, "");
240
- this.headers = options.headers ?? {};
241
- const f = options.fetch ?? globalThis.fetch?.bind(globalThis);
242
- if (!f) {
243
- throw new Error(
244
- "HttpSyncAdapter: no fetch available. Pass options.fetch on runtimes without a global fetch."
245
- );
246
- }
247
- this.fetchFn = f;
248
- this.pushPath = options.paths?.push ?? "/push";
249
- this.pullPath = options.paths?.pull ?? "/pull";
250
- }
251
- async push(changeset) {
252
- const res = await this.fetchFn(`${this.endpoint}${this.pushPath}`, {
253
- method: "POST",
254
- headers: { "content-type": "application/json", ...this.headers },
255
- body: changeset
256
- });
257
- if (!res.ok) {
258
- throw new Error(`HttpSyncAdapter push failed: ${res.status} ${res.statusText}`);
259
- }
260
- }
261
- async pull(sinceMs) {
262
- const url = `${this.endpoint}${this.pullPath}?since=${encodeURIComponent(String(sinceMs))}`;
263
- const res = await this.fetchFn(url, { method: "GET", headers: this.headers });
264
- if (!res.ok) {
265
- throw new Error(`HttpSyncAdapter pull failed: ${res.status} ${res.statusText}`);
266
- }
267
- const body = (await res.text()).trim();
268
- return body.length === 0 ? "[]" : body;
269
- }
270
- };
271
-
272
372
  // src/derive-id.ts
273
373
  var FNV1A128_OFFSET_BASIS = 0x6c62272e07bb014262b821756295c58dn;
274
374
  var FNV1A128_PRIME = 0x0000000001000000000000000000013bn;
@@ -292,455 +392,6 @@ function deriveDocId(collection, key) {
292
392
  return encodeUlid(hash);
293
393
  }
294
394
 
295
- // src/replication/coverage.ts
296
- var COVERAGE_COLLECTION = "__taladb_replica";
297
- function coverageKey(key) {
298
- return [
299
- key.origin,
300
- key.collection,
301
- key.scope,
302
- `p${key.projectionVersion}`,
303
- `s${key.schemaVersion}`
304
- ].map(encodeURIComponent).join("|");
305
- }
306
- var CoverageStore = class {
307
- constructor(db) {
308
- this.col = db.collection(COVERAGE_COLLECTION);
309
- }
310
- async read(key) {
311
- const doc = await this.col.findOne({ key: coverageKey(key) });
312
- if (!doc?.state) return { status: "empty" };
313
- try {
314
- return JSON.parse(doc.state);
315
- } catch {
316
- return { status: "empty" };
317
- }
318
- }
319
- async write(key, state) {
320
- const k = coverageKey(key);
321
- const state_json = JSON.stringify(state);
322
- await this.col.replaceManyWithIds(
323
- [{ _id: deriveDocId(COVERAGE_COLLECTION, k), key: k, state: state_json }],
324
- "local"
325
- );
326
- }
327
- /** Drop a scope's coverage, forcing a fresh bootstrap on next use. */
328
- async clear(key) {
329
- const k = coverageKey(key);
330
- await this.col.deleteManyWithIds([deriveDocId(COVERAGE_COLLECTION, k)], "local");
331
- }
332
- };
333
- function isAuthoritative(state) {
334
- return state.status === "complete";
335
- }
336
- function rowsApplied(state) {
337
- switch (state.status) {
338
- case "hydrating":
339
- case "complete":
340
- case "best-effort":
341
- return state.rowsApplied;
342
- default:
343
- return 0;
344
- }
345
- }
346
- function progress(state) {
347
- if (state.status === "complete") return 1;
348
- if (state.status !== "hydrating" || !state.total) return void 0;
349
- return Math.min(1, state.rowsApplied / state.total);
350
- }
351
-
352
- // src/replication/coordinator.ts
353
- var DEFAULT_PAGE_SIZE = 500;
354
- var defaultYield = () => new Promise((resolve) => setTimeout(resolve, 0));
355
- var MAX_BOOTSTRAP_PAGES = 1e5;
356
- var inflightByDatabase = /* @__PURE__ */ new WeakMap();
357
- var REPLICA_SCOPE_FIELD = "_replica_scope";
358
- var REPLICA_REVISION_FIELD = "_remote_rev";
359
- var ReplicationCoordinator = class {
360
- constructor(db, source, options = {}) {
361
- this.db = db;
362
- this.source = source;
363
- this.coverage = new CoverageStore(db);
364
- this.key = {
365
- origin: source.origin,
366
- collection: source.collection,
367
- scope: source.scope,
368
- projectionVersion: source.projectionVersion,
369
- schemaVersion: source.schemaVersion
370
- };
371
- this.pageSize = options.pageSize ?? DEFAULT_PAGE_SIZE;
372
- this.yieldFn = options.yieldFn ?? defaultYield;
373
- this.onProgress = options.onProgress;
374
- this.collectionOptions = options.collectionOptions;
375
- let shared = inflightByDatabase.get(db);
376
- if (!shared) {
377
- shared = /* @__PURE__ */ new Map();
378
- inflightByDatabase.set(db, shared);
379
- }
380
- this.inflight = shared;
381
- }
382
- get replicaScope() {
383
- return coverageKey(this.key);
384
- }
385
- get identityNamespace() {
386
- return `${this.source.origin}\0${this.source.scope}\0${this.source.collection}`;
387
- }
388
- getCoverage() {
389
- return this.coverage.read(this.key);
390
- }
391
- /** Whether a purely local read is authorized right now. */
392
- async isReady() {
393
- return isAuthoritative(await this.getCoverage());
394
- }
395
- /** Dedup by intent: identical concurrent work joins rather than duplicating. */
396
- dedup(key, run) {
397
- const existing = this.inflight.get(key);
398
- if (existing) return existing;
399
- const pass = run().finally(() => this.inflight.delete(key));
400
- this.inflight.set(key, pass);
401
- return pass;
402
- }
403
- /**
404
- * Write a batch of remote rows into the local collection.
405
- *
406
- * One commit for the whole batch, ids derived from the origin's primary key, and
407
- * `origin: 'remote'` so the rows can never replicate back out at the origin they
408
- * came from. This is the *only* write path in the coordinator — bootstrap, delta
409
- * and bridge all funnel through it, which is precisely why they converge instead
410
- * of conflicting.
411
- */
412
- async applyRows(rows) {
413
- if (rows.length === 0) return [];
414
- const col = this.db.collection(this.source.collection, this.collectionOptions);
415
- const docs = rows.map(
416
- (row) => {
417
- const revision = this.source.revisionOf(row);
418
- return {
419
- ...this.source.mapRow(row),
420
- _id: deriveDocId(this.identityNamespace, String(this.source.keyOf(row))),
421
- [REPLICA_SCOPE_FIELD]: this.replicaScope,
422
- [REPLICA_REVISION_FIELD]: revision
423
- };
424
- }
425
- );
426
- await col.replaceManyWithIds(docs, "remote");
427
- return docs.map((doc) => doc._id);
428
- }
429
- /**
430
- * Hydrate the scope: walk the origin page by page until the whole collection is
431
- * local, then mark it complete.
432
- *
433
- * Resumable and idempotent. If the walk is interrupted — a reload, a crash, a
434
- * dead network — the next call picks up from the last committed page, and
435
- * re-applying a page it already wrote is a no-op because the ids are derived.
436
- */
437
- hydrate() {
438
- return this.dedup(`${this.replicaScope}:hydrate`, () => this.runHydrate());
439
- }
440
- async runHydrate() {
441
- let state = await this.coverage.read(this.key);
442
- if (state.status === "complete") return state;
443
- let page = null;
444
- let snapshot = null;
445
- let rowsApplied2 = 0;
446
- let total;
447
- let deltaCursor;
448
- if (state.status === "hydrating") {
449
- page = state.nextPage;
450
- snapshot = state.snapshot;
451
- rowsApplied2 = state.rowsApplied;
452
- total = state.total;
453
- deltaCursor = state.deltaCursor;
454
- } else if (state.status === "error" && state.snapshot) {
455
- page = state.resumeFrom;
456
- snapshot = state.snapshot;
457
- rowsApplied2 = state.rowsApplied ?? 0;
458
- total = state.total;
459
- deltaCursor = state.deltaCursor;
460
- }
461
- let snapshotSupported = true;
462
- let pages = 0;
463
- try {
464
- for (; ; ) {
465
- const result = await this.source.bootstrap({ page, snapshot, limit: this.pageSize });
466
- if (snapshot !== null && result.snapshot !== void 0 && result.snapshot !== snapshot) {
467
- throw new Error(
468
- `replication: origin '${this.source.origin}' changed snapshot token mid-walk`
469
- );
470
- }
471
- if (snapshot === null && result.snapshot) snapshot = result.snapshot;
472
- if (result.snapshot === void 0 && page === null) snapshotSupported = false;
473
- if (result.deltaCursor && !deltaCursor) deltaCursor = result.deltaCursor;
474
- if (result.total !== void 0) total = result.total;
475
- rowsApplied2 += (await this.applyRows(result.rows)).length;
476
- page = result.nextPage;
477
- if (page !== null) {
478
- const next = {
479
- status: "hydrating",
480
- snapshot: snapshot ?? "",
481
- nextPage: page,
482
- rowsApplied: rowsApplied2,
483
- ...deltaCursor !== void 0 ? { deltaCursor } : {},
484
- ...total !== void 0 ? { total } : {}
485
- };
486
- await this.coverage.write(this.key, next);
487
- this.onProgress?.(next);
488
- if (++pages >= MAX_BOOTSTRAP_PAGES) {
489
- throw new Error(
490
- `replication: origin '${this.source.origin}' offered more than ${MAX_BOOTSTRAP_PAGES} bootstrap pages for '${this.source.collection}' \u2014 it is probably not advancing nextPage.`
491
- );
492
- }
493
- await this.yieldFn();
494
- continue;
495
- }
496
- if (snapshotSupported && this.source.delta && deltaCursor === void 0) {
497
- throw new Error(
498
- `replication: origin '${this.source.origin}' supports delta refresh but did not issue deltaCursor on the first bootstrap page`
499
- );
500
- }
501
- state = snapshotSupported ? {
502
- status: "complete",
503
- cursor: deltaCursor ?? "",
504
- completedAt: Date.now(),
505
- rowsApplied: rowsApplied2,
506
- ...total !== void 0 ? { total } : {}
507
- } : {
508
- // Every row the origin offered was applied — but without a snapshot
509
- // we cannot prove we saw a consistent view of it, so we must not
510
- // claim completeness. Reads keep going to the network.
511
- status: "best-effort",
512
- cursor: deltaCursor ?? "",
513
- reason: "the origin did not return a snapshot token, so a row that moved between pages during the walk may have been missed",
514
- rowsApplied: rowsApplied2,
515
- ...total !== void 0 ? { total } : {}
516
- };
517
- await this.coverage.write(this.key, state);
518
- this.onProgress?.(state);
519
- return state;
520
- }
521
- } catch (error) {
522
- const failed = {
523
- status: "error",
524
- resumeFrom: page ?? 0,
525
- ...snapshot ? { snapshot } : {},
526
- ...deltaCursor !== void 0 ? { deltaCursor } : {},
527
- rowsApplied: rowsApplied2,
528
- ...total !== void 0 ? { total } : {},
529
- error: error instanceof Error ? error.message : String(error)
530
- };
531
- await this.coverage.write(this.key, failed);
532
- this.onProgress?.(failed);
533
- throw error;
534
- }
535
- }
536
- /**
537
- * Apply incremental changes since the stored cursor.
538
- *
539
- * Deletions are applied by mapping the origin's primary keys through the same
540
- * `deriveDocId`, and are written with `origin: 'remote'` so they leave no
541
- * tombstone — the origin already knows it deleted these, and a tombstone would
542
- * push its own deletion back at it.
543
- */
544
- refresh() {
545
- return this.dedup(`${this.replicaScope}:refresh`, () => this.runRefresh());
546
- }
547
- async runRefresh() {
548
- const state = await this.coverage.read(this.key);
549
- if (state.status !== "complete") return state;
550
- if (!this.source.delta) return state;
551
- const col = this.db.collection(this.source.collection);
552
- let cursor = state.cursor;
553
- let rowsApplied2 = state.rowsApplied;
554
- for (; ; ) {
555
- const page = await this.source.delta(cursor);
556
- rowsApplied2 += (await this.applyRows(page.changed)).length;
557
- if (page.deleted.length > 0) {
558
- const ids = page.deleted.map(
559
- (k) => deriveDocId(this.identityNamespace, String(k))
560
- );
561
- await col.deleteManyWithIds(ids, "remote");
562
- }
563
- cursor = page.cursor;
564
- const next = { ...state, cursor, rowsApplied: rowsApplied2 };
565
- await this.coverage.write(this.key, next);
566
- if (!page.hasMore) {
567
- this.onProgress?.(next);
568
- return next;
569
- }
570
- await this.yieldFn();
571
- }
572
- }
573
- /**
574
- * Cold-start bridge: fetch exactly the rows one query needs, right now.
575
- *
576
- * Needed because a SPA or React Native app has no server render to paint behind
577
- * while the replica fills. The rows land in the same collection under the same
578
- * derived ids as the walk's, so this is not a cache — it is the replica, arriving
579
- * early.
580
- *
581
- * **Does not advance coverage.** These rows did not come from the bootstrap
582
- * snapshot and prove nothing about completeness; treating them as progress would
583
- * let a page-1 fetch masquerade as a hydrated catalog.
584
- */
585
- bridge(query) {
586
- if (!this.source.fetchQuery) return Promise.resolve({ count: 0, ids: [] });
587
- const key = `bridge:${coverageKey(this.key)}:${JSON.stringify(query)}`;
588
- return this.dedup(key, async () => {
589
- const rows = await this.source.fetchQuery(query);
590
- const ids = await this.applyRows(rows);
591
- return { count: ids.length, ids };
592
- });
593
- }
594
- /** Drop coverage and force a fresh bootstrap. Local rows are left alone. */
595
- async reset() {
596
- await this.coverage.clear(this.key);
597
- }
598
- };
599
-
600
- // src/replication/rest.ts
601
- function parseRows(body, endpoint) {
602
- if (Array.isArray(body)) return body;
603
- if (body && typeof body === "object") {
604
- const env = body;
605
- for (const field of ["data", "items", "rows"]) {
606
- const value = env[field];
607
- if (Array.isArray(value)) return value;
608
- }
609
- throw new Error(
610
- `taladb: could not find a row array in the response from ${endpoint}. Expected a bare array or a { data | items | rows } envelope, but got an object with keys: ${Object.keys(env).join(", ") || "(none)"}. Pass { parse } to extract them yourself.`
611
- );
612
- }
613
- throw new Error(
614
- `taladb: expected an array or object from ${endpoint}, got ${typeof body}.`
615
- );
616
- }
617
- function pick(body, names) {
618
- if (!body || typeof body !== "object") return void 0;
619
- const rec = body;
620
- for (const n of names) {
621
- if (rec[n] !== void 0) return rec[n];
622
- const meta = rec.meta;
623
- if (meta && meta[n] !== void 0) return meta[n];
624
- }
625
- return void 0;
626
- }
627
- function createRestSource(options) {
628
- const {
629
- endpoint,
630
- collection,
631
- origin = endpoint,
632
- scope = "global",
633
- projectionVersion = 1,
634
- schemaVersion = 1,
635
- key = "id",
636
- revision = "rev",
637
- mapRow,
638
- getAuth,
639
- paths,
640
- toParams,
641
- parse,
642
- pagination = "page"
643
- } = options;
644
- const doFetch = options.fetch ?? globalThis.fetch;
645
- async function get(path, params) {
646
- const url = new URL(path, globalThis.location?.origin ?? "http://localhost");
647
- for (const [k, v] of Object.entries(params)) url.searchParams.set(k, v);
648
- const headers = getAuth ? await getAuth() : void 0;
649
- const response = await doFetch(url.href, { headers });
650
- if (!response.ok) {
651
- throw new Error(
652
- `taladb: ${path} responded ${response.status} ${response.statusText}`
653
- );
654
- }
655
- return response.json();
656
- }
657
- const rowsFrom = (body) => parse ? parse(body) : parseRows(body, endpoint);
658
- return {
659
- origin,
660
- collection,
661
- scope,
662
- projectionVersion,
663
- schemaVersion,
664
- keyOf: (row) => {
665
- const value = row[key];
666
- if (value === void 0 || value === null) {
667
- throw new Error(
668
- `taladb: row from ${endpoint} has no '${key}' field to use as its primary key. Pass { key } to name the right one. Without a stable key, repeated fetches of the same row cannot be recognized as the same row.`
669
- );
670
- }
671
- return String(value);
672
- },
673
- revisionOf: (row) => {
674
- const value = typeof revision === "function" ? revision(row) : row[revision];
675
- if (value === void 0 || value === null) {
676
- throw new Error(
677
- `taladb: row from ${endpoint} has no authoritative revision. Pass { revision } to name the monotonic revision field.`
678
- );
679
- }
680
- const n = Number(value);
681
- if (!Number.isSafeInteger(n)) {
682
- throw new Error(`taladb: authoritative revision must be a safe integer, got ${String(value)}`);
683
- }
684
- return n;
685
- },
686
- mapRow: (row) => {
687
- if (mapRow) return mapRow(row);
688
- const { _id, ...rest } = row;
689
- void _id;
690
- return rest;
691
- },
692
- bootstrap: async (request) => {
693
- const params = { limit: String(request.limit) };
694
- if (request.page !== null) params.page = String(request.page);
695
- if (request.snapshot !== null) params.snapshot = request.snapshot;
696
- const body = await get(endpoint + (paths?.bootstrap ?? ""), params);
697
- const rows = rowsFrom(body);
698
- const nextPage = pick(body, ["nextPage", "next_page", "next"]);
699
- const snapshot = pick(body, ["snapshot"]);
700
- const deltaCursor = pick(body, ["deltaCursor", "delta_cursor", "cursor"]);
701
- const total = pick(body, ["total", "totalCount", "count"]);
702
- return {
703
- rows,
704
- // An origin that reports no explicit `nextPage` is treated as exhausted
705
- // once it returns a short page — the conventional REST behavior.
706
- nextPage: nextPage !== void 0 ? nextPage : rows.length < request.limit ? null : pagination === "offset" ? Number(request.page ?? 0) + request.limit : Number(request.page ?? 1) + 1,
707
- ...snapshot !== void 0 ? { snapshot } : {},
708
- ...deltaCursor !== void 0 ? { deltaCursor } : {},
709
- ...total !== void 0 ? { total } : {}
710
- };
711
- },
712
- ...options.delta === true || paths?.delta ? { delta: async (cursor) => {
713
- const body = await get(endpoint + (paths?.delta ?? ""), { since: cursor });
714
- const changed = rowsFrom(body);
715
- const deleted = pick(body, ["deleted", "removed"]) ?? [];
716
- const next = pick(body, ["cursor", "now", "nextCursor"]);
717
- return {
718
- changed,
719
- deleted: deleted.map(String),
720
- cursor: next ?? cursor,
721
- hasMore: Boolean(pick(body, ["hasMore", "has_more"]))
722
- };
723
- } } : {},
724
- fetchQuery: async (query) => {
725
- if (!toParams && Object.values(query.filter ?? {}).some((v) => typeof v === "object" && v !== null)) {
726
- throw new Error(
727
- "taladb: bridge filters with operators require RestSourceOptions.toParams; the default translator only supports scalar equality fields."
728
- );
729
- }
730
- const sortEntry = Object.entries(query.sort ?? {})[0];
731
- const params = toParams ? toParams(query) : {
732
- ...query.page !== void 0 ? { page: String(query.page) } : {},
733
- ...query.limit !== void 0 ? { limit: String(query.limit) } : {},
734
- ...Object.fromEntries(
735
- Object.entries(query.filter ?? {}).map(([k, v]) => [k, String(v)])
736
- ),
737
- ...sortEntry ? { sort: sortEntry[0], order: sortEntry[1] === -1 ? "desc" : "asc" } : {}
738
- };
739
- return rowsFrom(await get(endpoint, params));
740
- }
741
- };
742
- }
743
-
744
395
  // src/index.ts
745
396
  var import_meta = {};
746
397
  var TalaDbValidationError = class extends Error {
@@ -796,10 +447,7 @@ function applySchema(col, options) {
796
447
  const engineOwned = /* @__PURE__ */ new Set([
797
448
  "_id",
798
449
  "_v",
799
- "_changed_at",
800
- "_remote",
801
- "_remote_rev",
802
- "_replica_scope"
450
+ "_changed_at"
803
451
  ]);
804
452
  const downcastViews = /* @__PURE__ */ new WeakSet();
805
453
  function preserveFields(original, next, preserveUnknown, preserveVersion = true) {
@@ -862,10 +510,6 @@ function applySchema(col, options) {
862
510
  if (!stampVersion || doc._v !== void 0) return doc;
863
511
  return { ...doc, _v: targetVersion };
864
512
  }
865
- function stampDoc(doc) {
866
- if (!stampVersion || doc._v !== void 0) return doc;
867
- return { ...doc, _v: targetVersion };
868
- }
869
513
  function diffUpdate(original, migrated) {
870
514
  const $set = {};
871
515
  const $unset = {};
@@ -956,35 +600,6 @@ function applySchema(col, options) {
956
600
  if (schema) docs.forEach((doc, i) => parseWrite(doc, `insertMany[${i}]`));
957
601
  return col.insertMany(docs.map(stamp));
958
602
  } : col.insertMany.bind(col),
959
- // Rows arriving from a remote origin are validated like any other write. This
960
- // is the "parse, don't assert" boundary: the compile-time generic and the
961
- // runtime schema check have to be the same seam, or a malformed server
962
- // response walks straight into a typed collection.
963
- replaceManyWithIds: wrapWrites ? async (docs, origin) => {
964
- if (origin !== "remote") {
965
- docs.forEach((doc, i) => assertWritableDocument(doc, `replaceManyWithIds[${i}]`));
966
- if (targetVersion > 0) {
967
- throw new Error(
968
- "local replaceManyWithIds is disabled on versioned collections; use updateOne/updateMany so schema-version guards are atomic"
969
- );
970
- }
971
- }
972
- if (schema) docs.forEach((doc, i) => {
973
- const { _replica_scope, _remote_rev, ...schemaDoc } = doc;
974
- void _replica_scope;
975
- void _remote_rev;
976
- parseWrite(schemaDoc, `replaceManyWithIds[${i}]`);
977
- });
978
- return col.replaceManyWithIds(docs.map((d) => stampDoc(d)), origin);
979
- } : col.replaceManyWithIds.bind(col),
980
- deleteManyWithIds: stampVersion ? async (ids, origin) => {
981
- if (origin !== "remote") {
982
- throw new Error(
983
- "local deleteManyWithIds is disabled on versioned collections; use deleteOne/deleteMany so schema-version guards are atomic"
984
- );
985
- }
986
- return col.deleteManyWithIds(ids, origin);
987
- } : col.deleteManyWithIds.bind(col),
988
603
  updateOne: wrapWrites ? async (filter, update) => {
989
604
  assertSafeUpdate(update);
990
605
  return col.updateOne(writableFilter(filter), update);
@@ -1048,6 +663,10 @@ function applySchema(col, options) {
1048
663
  ) : col.subscribeAggregate.bind(col)
1049
664
  };
1050
665
  }
666
+ function decorateCollection(raw, name, opts, webhook) {
667
+ const reported = webhook ? wrapCollectionWithWebhook(raw, name, webhook) : raw;
668
+ return opts ? applySchema(reported, opts) : reported;
669
+ }
1051
670
  function detectPlatform() {
1052
671
  if (typeof navigator !== "undefined" && navigator.product === "ReactNative") {
1053
672
  return "react-native";
@@ -1133,7 +752,7 @@ function makePoller(findFn, callback, onError) {
1133
752
  active = false;
1134
753
  };
1135
754
  }
1136
- async function createBrowserDB(dbName, config, passphrase, migrations) {
755
+ async function createBrowserDB(dbName, webhook, config, passphrase, migrations) {
1137
756
  const workerUrl = new URL("@taladb/web/worker/taladb.worker.js", import_meta.url);
1138
757
  const worker = new Worker(workerUrl, { type: "module", name: "taladb" });
1139
758
  const proxy = new WorkerProxy(worker);
@@ -1158,9 +777,7 @@ async function createBrowserDB(dbName, config, passphrase, migrations) {
1158
777
  }
1159
778
  };
1160
779
  }
1161
- const syncSchemas = {};
1162
780
  function wrapCollection(name, opts) {
1163
- if (opts?.syncSchema) syncSchemas[name] = opts.syncSchema;
1164
781
  const s = JSON.stringify;
1165
782
  const wrapped = {
1166
783
  insert: (doc) => proxy.send("insert", { collection: name, docJson: s(doc) }),
@@ -1171,19 +788,6 @@ async function createBrowserDB(dbName, config, passphrase, migrations) {
1171
788
  });
1172
789
  return JSON.parse(json);
1173
790
  },
1174
- replaceManyWithIds: async (docs, origin = "local") => {
1175
- const json = await proxy.send("replaceManyWithIds", {
1176
- collection: name,
1177
- docsJson: s(docs),
1178
- origin
1179
- });
1180
- return JSON.parse(json);
1181
- },
1182
- deleteManyWithIds: (ids, origin = "local") => proxy.send("deleteManyWithIds", {
1183
- collection: name,
1184
- idsJson: s(ids),
1185
- origin
1186
- }),
1187
791
  find: async (filter) => {
1188
792
  const json = await proxy.send("find", {
1189
793
  collection: name,
@@ -1298,7 +902,7 @@ async function createBrowserDB(dbName, config, passphrase, migrations) {
1298
902
  onError
1299
903
  )
1300
904
  };
1301
- return opts ? applySchema(wrapped, opts) : wrapped;
905
+ return decorateCollection(wrapped, name, opts, webhook);
1302
906
  }
1303
907
  function nudgedPoller(collection, fetchJson, callback, onError) {
1304
908
  let active = true;
@@ -1367,8 +971,7 @@ async function createBrowserDB(dbName, config, passphrase, migrations) {
1367
971
  flush: async () => {
1368
972
  await proxy.send("flush");
1369
973
  },
1370
- syncStatus: async () => JSON.parse(await proxy.send("syncStatus")),
1371
- flushSync: (timeoutMs = 5e3) => proxy.send("flushSync", { timeoutMs }),
974
+ isPrimary: () => proxy.send("isPrimary"),
1372
975
  close: async () => {
1373
976
  channel?.close();
1374
977
  try {
@@ -1378,14 +981,7 @@ async function createBrowserDB(dbName, config, passphrase, migrations) {
1378
981
  proxy.abort(new Error("taladb worker closed"));
1379
982
  }
1380
983
  },
1381
- // All engine work (export scan, LWW merge) runs inside the worker, off the
1382
- // main thread — a sync pass never blocks rendering, whatever its size.
1383
- exportChanges: (collections, sinceMs) => proxy.send("exportChangeset", { collectionsJson: JSON.stringify(collections), sinceMs }),
1384
- importChanges: (changeset) => proxy.send("importChangeset", { changesetJson: changeset }),
1385
- importChangesValidated: async (changeset, schemasJson) => JSON.parse(await proxy.send("importChangesetValidated", { changesetJson: changeset, schemasJson })),
1386
- listCollectionNames: async () => JSON.parse(await proxy.send("listCollections")),
1387
- quarantined: async (collection) => JSON.parse(await proxy.send("quarantined", { collection })),
1388
- sync: (adapter, options) => runSync(handle, adapter, options, syncSchemas)
984
+ listCollectionNames: async () => JSON.parse(await proxy.send("listCollections"))
1389
985
  };
1390
986
  if (migrations?.length) {
1391
987
  await runMigrations(
@@ -1399,21 +995,17 @@ async function createBrowserDB(dbName, config, passphrase, migrations) {
1399
995
  }
1400
996
  return handle;
1401
997
  }
1402
- async function createNodeDB(dbName, config, passphrase, migrations) {
998
+ async function createNodeDB(dbName, webhook, config, passphrase, migrations) {
1403
999
  const native = await import("@taladb/node");
1404
1000
  const TalaDBNode = native.TalaDbNode ?? native.TalaDBNode;
1405
1001
  if (!TalaDBNode) throw new Error("@taladb/node loaded but exports no TalaDbNode class \u2014 rebuild the native module");
1406
1002
  const configJson = config !== void 0 ? JSON.stringify(config) : null;
1407
1003
  const db = TalaDBNode.open(dbName, configJson, passphrase ?? null);
1408
- const syncSchemas = {};
1409
1004
  function wrapCollection(name, opts) {
1410
- if (opts?.syncSchema) syncSchemas[name] = opts.syncSchema;
1411
1005
  const col = db.collection(name);
1412
1006
  const wrapped = {
1413
1007
  insert: async (doc) => col.insertAsync ? col.insertAsync(doc) : col.insert(doc),
1414
1008
  insertMany: async (docs) => col.insertManyAsync ? col.insertManyAsync(docs) : col.insertMany(docs),
1415
- replaceManyWithIds: async (docs, origin = "local") => col.replaceManyWithIdsAsync ? col.replaceManyWithIdsAsync(docs, origin) : col.replaceManyWithIds(docs, origin),
1416
- deleteManyWithIds: async (ids, origin = "local") => col.deleteManyWithIdsAsync ? col.deleteManyWithIdsAsync(ids, origin) : col.deleteManyWithIds(ids, origin),
1417
1009
  find: async (filter) => col.findAsync ? col.findAsync(filter ?? null) : col.find(filter ?? null),
1418
1010
  findOne: async (filter) => col.findOne(filter) ?? null,
1419
1011
  updateOne: async (filter, update) => col.updateOneAsync ? col.updateOneAsync(filter, update) : col.updateOne(filter, update),
@@ -1448,7 +1040,7 @@ async function createNodeDB(dbName, config, passphrase, migrations) {
1448
1040
  subscribe: (filter, callback, onError) => makePoller(async () => col.find(filter ?? null), callback, onError),
1449
1041
  subscribeAggregate: (pipeline, callback, onError) => makePoller(async () => wrapped.aggregate(pipeline), callback, onError)
1450
1042
  };
1451
- return opts ? applySchema(wrapped, opts) : wrapped;
1043
+ return decorateCollection(wrapped, name, opts, webhook);
1452
1044
  }
1453
1045
  const handle = {
1454
1046
  collection: (name, opts) => wrapCollection(name, opts),
@@ -1458,14 +1050,9 @@ async function createNodeDB(dbName, config, passphrase, migrations) {
1458
1050
  flush: db.flush ? async () => {
1459
1051
  db.flush();
1460
1052
  } : void 0,
1461
- exportChanges: async (collections, sinceMs) => db.exportChanges(sinceMs, collections),
1462
- importChanges: async (changeset) => db.importChanges(changeset),
1463
- // Feature-detected: only present when the loaded .node binary supports it,
1464
- // so older prebuilt binaries fall back to plain importChanges.
1465
- importChangesValidated: db.importChangesValidated ? async (changeset, schemasJson) => db.importChangesValidated(changeset, schemasJson) : void 0,
1466
- listCollectionNames: async () => db.listCollectionNames(),
1467
- quarantined: async (collection) => db.quarantined ? db.quarantined(collection) : [],
1468
- sync: (adapter, options) => runSync(handle, adapter, options, syncSchemas)
1053
+ // One process owns the file — there is no other tab to defer to.
1054
+ isPrimary: async () => true,
1055
+ listCollectionNames: async () => db.listCollectionNames()
1469
1056
  };
1470
1057
  if (migrations?.length) {
1471
1058
  if (typeof db.userVersion !== "function" || typeof db.setUserVersion !== "function") {
@@ -1480,7 +1067,7 @@ async function createNodeDB(dbName, config, passphrase, migrations) {
1480
1067
  }
1481
1068
  return handle;
1482
1069
  }
1483
- async function createNativeDB(_dbName, migrations) {
1070
+ async function createNativeDB(_dbName, webhook, migrations) {
1484
1071
  const maybeNative = globalThis.__TalaDB__;
1485
1072
  if (!maybeNative) {
1486
1073
  throw new Error(
@@ -1488,14 +1075,10 @@ async function createNativeDB(_dbName, migrations) {
1488
1075
  );
1489
1076
  }
1490
1077
  const native = maybeNative;
1491
- const syncSchemas = {};
1492
1078
  function wrapCollection(name, opts) {
1493
- if (opts?.syncSchema) syncSchemas[name] = opts.syncSchema;
1494
1079
  const wrapped = {
1495
1080
  insert: async (doc) => native.insert(name, doc),
1496
1081
  insertMany: async (docs) => native.insertMany(name, docs),
1497
- replaceManyWithIds: async (docs, origin = "local") => native.replaceManyWithIds(name, docs, origin),
1498
- deleteManyWithIds: async (ids, origin = "local") => native.deleteManyWithIds(name, ids, origin),
1499
1082
  find: async (filter) => native.find(name, filter ?? {}),
1500
1083
  findOne: async (filter) => native.findOne(name, filter ?? {}),
1501
1084
  updateOne: async (filter, update) => native.updateOne(name, filter, update),
@@ -1542,25 +1125,8 @@ async function createNativeDB(_dbName, migrations) {
1542
1125
  subscribe: (filter, callback, onError) => makePoller(async () => native.find(name, filter ?? {}), callback, onError),
1543
1126
  subscribeAggregate: (pipeline, callback, onError) => makePoller(async () => native.aggregate(name, pipeline), callback, onError)
1544
1127
  };
1545
- return opts ? applySchema(wrapped, opts) : wrapped;
1128
+ return decorateCollection(wrapped, name, opts, webhook);
1546
1129
  }
1547
- const syncSurface = typeof native.exportChanges === "function" && typeof native.importChanges === "function" && typeof native.listCollectionNames === "function" ? (() => {
1548
- const handle2 = {
1549
- collection: (name, opts) => wrapCollection(name, opts),
1550
- exportChanges: async (collections, sinceMs) => native.exportChanges(collections, sinceMs),
1551
- importChanges: async (changeset) => native.importChanges(changeset),
1552
- // Feature-detected: present on 0.9.2+ JSI HostObjects; when absent,
1553
- // runSync falls back to unvalidated importChanges.
1554
- importChangesValidated: native.importChangesValidated ? async (changeset, schemasJson) => native.importChangesValidated(changeset, schemasJson) : void 0,
1555
- listCollectionNames: async () => native.listCollectionNames(),
1556
- sync: (adapter, options) => runSync(handle2, adapter, options, syncSchemas)
1557
- };
1558
- return {
1559
- exportChanges: handle2.exportChanges,
1560
- importChanges: handle2.importChanges,
1561
- sync: handle2.sync
1562
- };
1563
- })() : unsupportedSync("react-native");
1564
1130
  const handle = {
1565
1131
  collection: (name, opts) => wrapCollection(name, opts),
1566
1132
  compact: async () => native.compact(),
@@ -1568,8 +1134,8 @@ async function createNativeDB(_dbName, migrations) {
1568
1134
  flush: native.flush ? async () => {
1569
1135
  native.flush();
1570
1136
  } : void 0,
1571
- quarantined: native.quarantined ? async (collection) => native.quarantined(collection) : void 0,
1572
- ...syncSurface
1137
+ // One process owns the file — there is no other tab to defer to.
1138
+ isPrimary: async () => true
1573
1139
  };
1574
1140
  if (migrations?.length) {
1575
1141
  if (typeof native.userVersion !== "function" || typeof native.setUserVersion !== "function") {
@@ -1621,36 +1187,45 @@ async function openDB(dbName = "taladb.db", options) {
1621
1187
  durability: { ...resolvedConfig?.durability, ...options.durability }
1622
1188
  };
1623
1189
  }
1190
+ const webhook = createWebhookDispatcher(options?.webhook ?? resolvedConfig?.webhook);
1624
1191
  const platform = detectPlatform();
1625
1192
  const migrations = options?.migrations;
1193
+ let db;
1626
1194
  switch (platform) {
1627
1195
  case "browser":
1628
- return createBrowserDB(dbName, resolvedConfig, options?.passphrase, migrations);
1196
+ db = await createBrowserDB(dbName, webhook, resolvedConfig, options?.passphrase, migrations);
1197
+ break;
1629
1198
  case "react-native":
1630
1199
  if (options?.passphrase !== void 0) {
1631
1200
  throw new Error("On React Native, pass the passphrase in the config JSON to TalaDBModule.initialize(); refusing to assume the already-open native database is encrypted");
1632
1201
  }
1633
- return createNativeDB(dbName, migrations);
1202
+ db = await createNativeDB(dbName, webhook, migrations);
1203
+ break;
1634
1204
  case "node":
1635
- return createNodeDB(dbName, resolvedConfig, options?.passphrase, migrations);
1205
+ db = await createNodeDB(dbName, webhook, resolvedConfig, options?.passphrase, migrations);
1206
+ break;
1636
1207
  }
1208
+ return webhook ? attachWebhook(db, webhook) : db;
1209
+ }
1210
+ function attachWebhook(db, webhook) {
1211
+ return {
1212
+ ...db,
1213
+ webhookStats: () => webhook.stats(),
1214
+ flushWebhook: (timeoutMs) => webhook.flush(timeoutMs),
1215
+ async close() {
1216
+ await webhook.flush();
1217
+ await db.close();
1218
+ }
1219
+ };
1637
1220
  }
1638
1221
  // Annotate the CommonJS export names for ESM import in node:
1639
1222
  0 && (module.exports = {
1640
- COVERAGE_COLLECTION,
1641
- CoverageStore,
1642
- HttpSyncAdapter,
1643
- REPLICA_REVISION_FIELD,
1644
- REPLICA_SCOPE_FIELD,
1645
- ReplicationCoordinator,
1646
1223
  TalaDbValidationError,
1647
1224
  applySchema,
1648
- coverageKey,
1649
- createRestSource,
1225
+ createWebhookDispatcher,
1226
+ decorateCollection,
1650
1227
  deriveDocId,
1651
- isAuthoritative,
1652
1228
  openDB,
1653
- progress,
1654
- rowsApplied,
1655
- runMigrations
1229
+ runMigrations,
1230
+ validateWebhookConfig
1656
1231
  });