@trestleinc/replicate 1.1.0 → 1.1.2-preview.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.
Files changed (91) hide show
  1. package/README.md +446 -260
  2. package/dist/client/index.d.ts +311 -19
  3. package/dist/client/index.js +4027 -0
  4. package/dist/component/_generated/api.d.ts +13 -17
  5. package/dist/component/_generated/api.js +24 -4
  6. package/dist/component/_generated/component.d.ts +79 -77
  7. package/dist/component/_generated/component.js +1 -0
  8. package/dist/component/_generated/dataModel.d.ts +12 -15
  9. package/dist/component/_generated/dataModel.js +1 -0
  10. package/dist/component/_generated/server.d.ts +19 -22
  11. package/dist/component/_generated/server.js +65 -1
  12. package/dist/component/_virtual/rolldown_runtime.js +18 -0
  13. package/dist/component/convex.config.d.ts +6 -2
  14. package/dist/component/convex.config.js +7 -3
  15. package/dist/component/logger.d.ts +10 -6
  16. package/dist/component/logger.js +25 -28
  17. package/dist/component/public.d.ts +70 -61
  18. package/dist/component/public.js +311 -295
  19. package/dist/component/schema.d.ts +53 -45
  20. package/dist/component/schema.js +26 -32
  21. package/dist/component/shared/types.d.ts +9 -0
  22. package/dist/component/shared/types.js +15 -0
  23. package/dist/server/index.d.ts +134 -13
  24. package/dist/server/index.js +368 -0
  25. package/dist/shared/index.d.ts +27 -3
  26. package/dist/shared/index.js +1 -2
  27. package/package.json +34 -29
  28. package/src/client/collection.ts +339 -306
  29. package/src/client/errors.ts +9 -9
  30. package/src/client/index.ts +13 -32
  31. package/src/client/logger.ts +2 -2
  32. package/src/client/merge.ts +37 -34
  33. package/src/client/persistence/custom.ts +84 -0
  34. package/src/client/persistence/index.ts +9 -46
  35. package/src/client/persistence/indexeddb.ts +111 -84
  36. package/src/client/persistence/memory.ts +3 -3
  37. package/src/client/persistence/sqlite/browser.ts +168 -0
  38. package/src/client/persistence/sqlite/native.ts +29 -0
  39. package/src/client/persistence/sqlite/schema.ts +124 -0
  40. package/src/client/persistence/types.ts +32 -28
  41. package/src/client/prose-schema.ts +55 -0
  42. package/src/client/prose.ts +28 -25
  43. package/src/client/replicate.ts +5 -5
  44. package/src/client/services/cursor.ts +109 -0
  45. package/src/component/_generated/component.ts +31 -29
  46. package/src/component/convex.config.ts +2 -2
  47. package/src/component/logger.ts +7 -7
  48. package/src/component/public.ts +225 -237
  49. package/src/component/schema.ts +18 -15
  50. package/src/server/builder.ts +20 -7
  51. package/src/server/index.ts +3 -5
  52. package/src/server/schema.ts +5 -5
  53. package/src/server/storage.ts +113 -59
  54. package/src/shared/index.ts +5 -5
  55. package/src/shared/types.ts +51 -14
  56. package/dist/client/collection.d.ts +0 -96
  57. package/dist/client/errors.d.ts +0 -59
  58. package/dist/client/logger.d.ts +0 -2
  59. package/dist/client/merge.d.ts +0 -77
  60. package/dist/client/persistence/adapters/index.d.ts +0 -8
  61. package/dist/client/persistence/adapters/opsqlite.d.ts +0 -46
  62. package/dist/client/persistence/adapters/sqljs.d.ts +0 -83
  63. package/dist/client/persistence/index.d.ts +0 -49
  64. package/dist/client/persistence/indexeddb.d.ts +0 -17
  65. package/dist/client/persistence/memory.d.ts +0 -16
  66. package/dist/client/persistence/sqlite-browser.d.ts +0 -51
  67. package/dist/client/persistence/sqlite-level.d.ts +0 -63
  68. package/dist/client/persistence/sqlite-rn.d.ts +0 -36
  69. package/dist/client/persistence/sqlite.d.ts +0 -47
  70. package/dist/client/persistence/types.d.ts +0 -42
  71. package/dist/client/prose.d.ts +0 -56
  72. package/dist/client/replicate.d.ts +0 -40
  73. package/dist/client/services/checkpoint.d.ts +0 -18
  74. package/dist/client/services/reconciliation.d.ts +0 -24
  75. package/dist/index.js +0 -1620
  76. package/dist/server/builder.d.ts +0 -94
  77. package/dist/server/schema.d.ts +0 -27
  78. package/dist/server/storage.d.ts +0 -80
  79. package/dist/server.js +0 -281
  80. package/dist/shared/types.d.ts +0 -50
  81. package/dist/shared/types.js +0 -6
  82. package/dist/shared.js +0 -6
  83. package/src/client/persistence/adapters/index.ts +0 -8
  84. package/src/client/persistence/adapters/opsqlite.ts +0 -54
  85. package/src/client/persistence/adapters/sqljs.ts +0 -128
  86. package/src/client/persistence/sqlite-browser.ts +0 -107
  87. package/src/client/persistence/sqlite-level.ts +0 -407
  88. package/src/client/persistence/sqlite-rn.ts +0 -44
  89. package/src/client/persistence/sqlite.ts +0 -161
  90. package/src/client/services/checkpoint.ts +0 -86
  91. package/src/client/services/reconciliation.ts +0 -108
@@ -0,0 +1,4027 @@
1
+ import * as Y from "yjs";
2
+ import { createMutex } from "lib0/mutex";
3
+ import { getFunctionName } from "convex/server";
4
+ import { createCollection } from "@tanstack/db";
5
+ import { Context, Data, Effect, Layer } from "effect";
6
+ import { getLogger } from "@logtape/logtape";
7
+
8
+ //#region src/client/logger.ts
9
+ function getLogger$1(category) {
10
+ return getLogger(["replicate", ...category]);
11
+ }
12
+
13
+ //#endregion
14
+ //#region src/client/errors.ts
15
+ var NetworkError = class extends Data.TaggedError("NetworkError") {};
16
+ var IDBError = class extends Data.TaggedError("IDBError") {};
17
+ var IDBWriteError = class extends Data.TaggedError("IDBWriteError") {};
18
+ var ReconciliationError = class extends Data.TaggedError("ReconciliationError") {};
19
+ var ProseError = class extends Data.TaggedError("ProseError") {};
20
+ var CollectionNotReadyError = class extends Data.TaggedError("CollectionNotReadyError") {};
21
+ /** Error that should not be retried (auth failures, validation errors) */
22
+ var NonRetriableError = class extends Error {
23
+ constructor(message) {
24
+ super(message);
25
+ this.name = "NonRetriableError";
26
+ }
27
+ };
28
+
29
+ //#endregion
30
+ //#region src/client/services/cursor.ts
31
+ var CursorService = class extends Context.Tag("CursorService")() {};
32
+ function generatePeerId() {
33
+ return crypto.randomUUID();
34
+ }
35
+ function createCursorLayer(kv) {
36
+ return Layer.succeed(CursorService, CursorService.of({
37
+ loadCursor: (collection$1) => Effect.gen(function* (_) {
38
+ const key = `cursor:${collection$1}`;
39
+ const stored = yield* _(Effect.tryPromise({
40
+ try: () => kv.get(key),
41
+ catch: (cause) => new IDBError({
42
+ operation: "get",
43
+ key,
44
+ cause
45
+ })
46
+ }));
47
+ if (stored !== void 0) {
48
+ yield* _(Effect.logDebug("Loaded cursor from storage", {
49
+ collection: collection$1,
50
+ cursor: stored
51
+ }));
52
+ return stored;
53
+ }
54
+ yield* _(Effect.logDebug("No stored cursor, using default", { collection: collection$1 }));
55
+ return 0;
56
+ }),
57
+ saveCursor: (collection$1, cursor) => Effect.gen(function* (_) {
58
+ const key = `cursor:${collection$1}`;
59
+ yield* _(Effect.tryPromise({
60
+ try: () => kv.set(key, cursor),
61
+ catch: (cause) => new IDBWriteError({
62
+ key,
63
+ value: cursor,
64
+ cause
65
+ })
66
+ }));
67
+ yield* _(Effect.logDebug("Cursor saved", {
68
+ collection: collection$1,
69
+ cursor
70
+ }));
71
+ }),
72
+ clearCursor: (collection$1) => Effect.gen(function* (_) {
73
+ const key = `cursor:${collection$1}`;
74
+ yield* _(Effect.tryPromise({
75
+ try: () => kv.del(key),
76
+ catch: (cause) => new IDBError({
77
+ operation: "delete",
78
+ key,
79
+ cause
80
+ })
81
+ }));
82
+ yield* _(Effect.logDebug("Cursor cleared", { collection: collection$1 }));
83
+ }),
84
+ loadPeerId: (collection$1) => Effect.gen(function* (_) {
85
+ const key = `peerId:${collection$1}`;
86
+ const stored = yield* _(Effect.tryPromise({
87
+ try: () => kv.get(key),
88
+ catch: (cause) => new IDBError({
89
+ operation: "get",
90
+ key,
91
+ cause
92
+ })
93
+ }));
94
+ if (stored) {
95
+ yield* _(Effect.logDebug("Loaded peerId from storage", {
96
+ collection: collection$1,
97
+ peerId: stored
98
+ }));
99
+ return stored;
100
+ }
101
+ const newPeerId = generatePeerId();
102
+ yield* _(Effect.tryPromise({
103
+ try: () => kv.set(key, newPeerId),
104
+ catch: (cause) => new IDBWriteError({
105
+ key,
106
+ value: newPeerId,
107
+ cause
108
+ })
109
+ }));
110
+ yield* _(Effect.logDebug("Generated new peerId", {
111
+ collection: collection$1,
112
+ peerId: newPeerId
113
+ }));
114
+ return newPeerId;
115
+ })
116
+ }));
117
+ }
118
+
119
+ //#endregion
120
+ //#region src/client/replicate.ts
121
+ /**
122
+ * Create bound replicate operations for a collection.
123
+ * Returns functions that are already tied to the collection's params.
124
+ * This is the proper way to handle multiple concurrent collections.
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * const ops = createReplicateOps<Task>(params);
129
+ * ops.replace(items); // Always targets THIS collection's TanStack DB
130
+ * ops.upsert([item]);
131
+ * ops.delete([item]);
132
+ * ```
133
+ */
134
+ function createReplicateOps(params) {
135
+ return {
136
+ insert(items) {
137
+ params.begin();
138
+ for (const item of items) params.write({
139
+ type: "insert",
140
+ value: item
141
+ });
142
+ params.commit();
143
+ },
144
+ delete(items) {
145
+ params.begin();
146
+ for (const item of items) params.write({
147
+ type: "delete",
148
+ value: item
149
+ });
150
+ params.commit();
151
+ },
152
+ upsert(items) {
153
+ params.begin();
154
+ for (const item of items) params.write({
155
+ type: "update",
156
+ value: item
157
+ });
158
+ params.commit();
159
+ },
160
+ replace(items) {
161
+ params.begin();
162
+ params.truncate();
163
+ for (const item of items) params.write({
164
+ type: "insert",
165
+ value: item
166
+ });
167
+ params.commit();
168
+ }
169
+ };
170
+ }
171
+
172
+ //#endregion
173
+ //#region src/client/merge.ts
174
+ /**
175
+ * Merge Helpers - Plain functions for Yjs CRDT operations
176
+ *
177
+ * Provides document creation, state encoding, and merge operations.
178
+ */
179
+ const logger$2 = getLogger$1(["replicate", "merge"]);
180
+ /**
181
+ * Apply a binary update to a Yjs document.
182
+ * Y.applyUpdateV2 is already atomic, no need for transaction wrapper.
183
+ */
184
+ function applyUpdate(doc, update, origin) {
185
+ Y.applyUpdateV2(doc, update, origin);
186
+ }
187
+ /**
188
+ * Execute a function within a Yjs transaction and capture the delta.
189
+ * Returns both the function result and a delta containing only the changes made.
190
+ */
191
+ function transactWithDelta(doc, fn, origin) {
192
+ const beforeVector = Y.encodeStateVector(doc);
193
+ return {
194
+ result: doc.transact(fn, origin),
195
+ delta: Y.encodeStateAsUpdateV2(doc, beforeVector)
196
+ };
197
+ }
198
+ /**
199
+ * Check if a value is a Yjs AbstractType by checking internal properties.
200
+ * All Yjs types (Y.Map, Y.Array, Y.Text, Y.XmlFragment, etc.) extend AbstractType
201
+ * and have these properties regardless of which module instance created them.
202
+ */
203
+ function isYjsAbstractType(value) {
204
+ if (value === null || typeof value !== "object") return false;
205
+ const v = value;
206
+ return "_map" in v && "_eH" in v && "doc" in v;
207
+ }
208
+ /**
209
+ * Check if a value is a Y.Map.
210
+ * Y.Map has keys() method which Y.XmlFragment does not.
211
+ */
212
+ function isYMap(value) {
213
+ if (!isYjsAbstractType(value)) return false;
214
+ const v = value;
215
+ return typeof v.keys === "function" && typeof v.get === "function";
216
+ }
217
+ /**
218
+ * Check if a value is a Y.Array (has toArray but not get - distinguishes from Y.Map).
219
+ */
220
+ function isYArray(value) {
221
+ if (!isYjsAbstractType(value)) return false;
222
+ const v = value;
223
+ return typeof v.toArray === "function" && typeof v.get !== "function";
224
+ }
225
+ /**
226
+ * Check if a value is a Y.XmlFragment or Y.XmlElement.
227
+ * XmlFragment has toArray() and get(index), but NOT keys() like Y.Map.
228
+ */
229
+ function isYXmlFragment(value) {
230
+ if (!isYjsAbstractType(value)) return false;
231
+ const v = value;
232
+ return typeof v.toArray === "function" && typeof v.keys !== "function";
233
+ }
234
+ /**
235
+ * Recursively serialize a Yjs value to plain JavaScript.
236
+ * Handles Y.Map, Y.Array, Y.XmlFragment without using instanceof.
237
+ */
238
+ function serialize(value) {
239
+ if (value === null || value === void 0) return value;
240
+ if (typeof value !== "object") return value;
241
+ if (isYXmlFragment(value)) return fragmentToJSON(value);
242
+ if (isYMap(value)) {
243
+ const result = {};
244
+ value.forEach((v, k) => {
245
+ result[k] = serialize(v);
246
+ });
247
+ return result;
248
+ }
249
+ if (isYArray(value)) return value.toArray().map(serialize);
250
+ return value;
251
+ }
252
+ /**
253
+ * Extract all items from a Y.Map as plain objects.
254
+ */
255
+ function extractItems(ymap) {
256
+ const items = [];
257
+ ymap.forEach((value) => {
258
+ if (isYMap(value)) items.push(serialize(value));
259
+ });
260
+ return items;
261
+ }
262
+ /**
263
+ * Extract a single item from a Y.Map by key.
264
+ */
265
+ function extractItem(ymap, key) {
266
+ const value = ymap.get(key);
267
+ if (isYMap(value)) return serialize(value);
268
+ return null;
269
+ }
270
+ /**
271
+ * Check if a value looks like ProseMirror/BlockNote JSON document.
272
+ * Used internally to auto-detect prose fields during insert/update.
273
+ */
274
+ function isDoc(value) {
275
+ return typeof value === "object" && value !== null && "type" in value && value.type === "doc";
276
+ }
277
+ /**
278
+ * Convert a Y.XmlFragment to ProseMirror-compatible JSON.
279
+ */
280
+ function fragmentToJSON(fragment) {
281
+ const content = [];
282
+ for (const child of fragment.toArray()) if (child instanceof Y.XmlElement) content.push(xmlElementToJSON(child));
283
+ else if (child instanceof Y.XmlText) {
284
+ const textContent = xmlTextToJSON(child);
285
+ if (textContent.length > 0) content.push({
286
+ type: "paragraph",
287
+ content: textContent
288
+ });
289
+ }
290
+ return {
291
+ type: "doc",
292
+ content: content.length > 0 ? content : [{ type: "paragraph" }]
293
+ };
294
+ }
295
+ function xmlElementToJSON(element) {
296
+ const result = { type: element.nodeName };
297
+ const attrs = element.getAttributes();
298
+ if (Object.keys(attrs).length > 0) result.attrs = attrs;
299
+ const content = [];
300
+ for (const child of element.toArray()) if (child instanceof Y.XmlElement) content.push(xmlElementToJSON(child));
301
+ else if (child instanceof Y.XmlText) content.push(...xmlTextToJSON(child));
302
+ if (content.length > 0) result.content = content;
303
+ return result;
304
+ }
305
+ function xmlTextToJSON(text) {
306
+ const result = [];
307
+ const delta = text.toDelta();
308
+ for (const op of delta) if (typeof op.insert === "string") {
309
+ const node = {
310
+ type: "text",
311
+ text: op.insert
312
+ };
313
+ if (op.attributes && Object.keys(op.attributes).length > 0) node.marks = Object.entries(op.attributes).map(([type, attrs]) => ({
314
+ type,
315
+ attrs: typeof attrs === "object" ? attrs : void 0
316
+ }));
317
+ result.push(node);
318
+ }
319
+ return result;
320
+ }
321
+ /**
322
+ * Initialize a Y.XmlFragment from ProseMirror-compatible JSON.
323
+ */
324
+ function fragmentFromJSON(fragment, json) {
325
+ if (!json.content) return;
326
+ for (const node of json.content) appendNodeToFragment(fragment, node);
327
+ }
328
+ /**
329
+ * Extract plain text from ProseMirror/BlockNote JSON content.
330
+ * Handles various content structures defensively for search and display.
331
+ */
332
+ function extract(content) {
333
+ if (!content || typeof content !== "object") return "";
334
+ const doc = content;
335
+ if (!doc.content || !Array.isArray(doc.content)) return "";
336
+ return doc.content.map((block) => {
337
+ if (!block.content || !Array.isArray(block.content)) return "";
338
+ return block.content.map((node) => node.text || "").join("");
339
+ }).join(" ");
340
+ }
341
+ function appendNodeToFragment(parent, node) {
342
+ if (node.type === "text") {
343
+ const text = new Y.XmlText();
344
+ if (node.text) {
345
+ const attrs = {};
346
+ if (node.marks) for (const mark of node.marks) attrs[mark.type] = mark.attrs ?? true;
347
+ text.insert(0, node.text, Object.keys(attrs).length > 0 ? attrs : void 0);
348
+ }
349
+ parent.insert(parent.length, [text]);
350
+ } else {
351
+ const element = new Y.XmlElement(node.type);
352
+ if (node.attrs) for (const [key, value] of Object.entries(node.attrs)) element.setAttribute(key, value);
353
+ if (node.content) for (const child of node.content) appendNodeToFragment(element, child);
354
+ parent.insert(parent.length, [element]);
355
+ }
356
+ }
357
+ /**
358
+ * Serialize any value, handling Yjs types specially.
359
+ * Uses our custom serialization system that works across module instances.
360
+ */
361
+ function serializeYMapValue(value) {
362
+ return serialize(value);
363
+ }
364
+ /**
365
+ * Get a Y.XmlFragment from a document's field.
366
+ * Returns null if the document or field doesn't exist, or if the field is not an XmlFragment.
367
+ */
368
+ function getFragmentFromYMap(ymap, documentId, field) {
369
+ const doc = ymap.get(documentId);
370
+ if (!isYMap(doc)) return null;
371
+ const fieldValue = doc.get(field);
372
+ if (isYXmlFragment(fieldValue)) return fieldValue;
373
+ return null;
374
+ }
375
+
376
+ //#endregion
377
+ //#region src/client/prose.ts
378
+ /**
379
+ * Prose Field Helpers - Document-level state management for rich text sync
380
+ *
381
+ * Manages Y.XmlFragment observation, debounced sync, and pending state.
382
+ * Uses document-level tracking to prevent race conditions.
383
+ */
384
+ /** Server origin - changes from server should not trigger local sync */
385
+ const SERVER_ORIGIN = "server";
386
+ const logger$1 = getLogger$1(["replicate", "prose"]);
387
+ const DEFAULT_DEBOUNCE_MS$1 = 1e3;
388
+ const applyingFromServer = /* @__PURE__ */ new Map();
389
+ const debounceTimers = /* @__PURE__ */ new Map();
390
+ const lastSyncedVectors = /* @__PURE__ */ new Map();
391
+ const pendingState = /* @__PURE__ */ new Map();
392
+ const pendingListeners = /* @__PURE__ */ new Map();
393
+ const fragmentObservers = /* @__PURE__ */ new Map();
394
+ const failedSyncQueue = /* @__PURE__ */ new Map();
395
+ /**
396
+ * Check if a document is currently applying server data.
397
+ * Used to prevent echo loops in onUpdate handlers.
398
+ */
399
+ function isApplyingFromServer(collection$1, documentId) {
400
+ const key = `${collection$1}:${documentId}`;
401
+ return applyingFromServer.get(key) ?? false;
402
+ }
403
+ /**
404
+ * Set whether a document is currently applying server data.
405
+ */
406
+ function setApplyingFromServer(collection$1, documentId, value) {
407
+ const key = `${collection$1}:${documentId}`;
408
+ if (value) applyingFromServer.set(key, true);
409
+ else applyingFromServer.delete(key);
410
+ }
411
+ /**
412
+ * Set pending state and notify listeners.
413
+ */
414
+ function setPendingInternal(key, value) {
415
+ if ((pendingState.get(key) ?? false) !== value) {
416
+ pendingState.set(key, value);
417
+ const listeners = pendingListeners.get(key);
418
+ if (listeners) for (const cb of listeners) try {
419
+ cb(value);
420
+ } catch (err) {
421
+ logger$1.error("Pending listener error", {
422
+ key,
423
+ error: String(err)
424
+ });
425
+ }
426
+ }
427
+ }
428
+ /**
429
+ * Get current pending state for a document.
430
+ */
431
+ function isPending(collection$1, documentId) {
432
+ return pendingState.get(`${collection$1}:${documentId}`) ?? false;
433
+ }
434
+ /**
435
+ * Subscribe to pending state changes for a document.
436
+ */
437
+ function subscribePending(collection$1, documentId, callback) {
438
+ const key = `${collection$1}:${documentId}`;
439
+ let listeners = pendingListeners.get(key);
440
+ if (!listeners) {
441
+ listeners = /* @__PURE__ */ new Set();
442
+ pendingListeners.set(key, listeners);
443
+ }
444
+ listeners.add(callback);
445
+ return () => {
446
+ listeners?.delete(callback);
447
+ if (listeners?.size === 0) pendingListeners.delete(key);
448
+ };
449
+ }
450
+ /**
451
+ * Cancel any pending debounced sync for a document.
452
+ * Called when receiving remote updates to avoid conflicts.
453
+ */
454
+ function cancelPending(collection$1, documentId) {
455
+ const key = `${collection$1}:${documentId}`;
456
+ const timer = debounceTimers.get(key);
457
+ if (timer) {
458
+ clearTimeout(timer);
459
+ debounceTimers.delete(key);
460
+ setPendingInternal(key, false);
461
+ logger$1.debug("Cancelled pending sync due to remote update", {
462
+ collection: collection$1,
463
+ documentId
464
+ });
465
+ }
466
+ }
467
+ /**
468
+ * Cancel all pending syncs for a collection.
469
+ * Called when receiving a snapshot that replaces all state.
470
+ */
471
+ function cancelAllPending(collection$1) {
472
+ const prefix = `${collection$1}:`;
473
+ for (const [key, timer] of debounceTimers) if (key.startsWith(prefix)) {
474
+ clearTimeout(timer);
475
+ debounceTimers.delete(key);
476
+ setPendingInternal(key, false);
477
+ }
478
+ logger$1.debug("Cancelled all pending syncs", { collection: collection$1 });
479
+ }
480
+ /**
481
+ * Set up observation for a prose field's Y.XmlFragment.
482
+ * Returns a cleanup function.
483
+ */
484
+ function observeFragment(config$1) {
485
+ const { collection: collection$1, documentId, field, fragment, ydoc, ymap, collectionRef, debounceMs = DEFAULT_DEBOUNCE_MS$1 } = config$1;
486
+ const key = `${collection$1}:${documentId}`;
487
+ const existingCleanup = fragmentObservers.get(key);
488
+ if (existingCleanup) {
489
+ logger$1.debug("Fragment already being observed", {
490
+ collection: collection$1,
491
+ documentId,
492
+ field
493
+ });
494
+ return existingCleanup;
495
+ }
496
+ const observerHandler = (_events, transaction) => {
497
+ if (transaction.origin === SERVER_ORIGIN) return;
498
+ const existing = debounceTimers.get(key);
499
+ if (existing) clearTimeout(existing);
500
+ setPendingInternal(key, true);
501
+ const timer = setTimeout(async () => {
502
+ debounceTimers.delete(key);
503
+ const itemYMap = ymap.get(documentId);
504
+ if (!itemYMap) {
505
+ logger$1.error("Document not found", {
506
+ collection: collection$1,
507
+ documentId
508
+ });
509
+ setPendingInternal(key, false);
510
+ return;
511
+ }
512
+ try {
513
+ const lastVector = lastSyncedVectors.get(key);
514
+ const delta = lastVector ? Y.encodeStateAsUpdateV2(ydoc, lastVector) : Y.encodeStateAsUpdateV2(ydoc);
515
+ if (delta.length <= 2) {
516
+ logger$1.debug("No changes to sync", {
517
+ collection: collection$1,
518
+ documentId
519
+ });
520
+ setPendingInternal(key, false);
521
+ return;
522
+ }
523
+ const crdtBytes = delta.buffer;
524
+ const currentVector = Y.encodeStateVector(ydoc);
525
+ logger$1.debug("Syncing prose delta", {
526
+ collection: collection$1,
527
+ documentId,
528
+ deltaSize: delta.byteLength
529
+ });
530
+ const materializedDoc = serializeYMapValue(itemYMap);
531
+ await collectionRef.update(documentId, { metadata: { contentSync: {
532
+ crdtBytes,
533
+ materializedDoc
534
+ } } }, (draft) => {
535
+ draft.updatedAt = Date.now();
536
+ }).isPersisted.promise;
537
+ lastSyncedVectors.set(key, currentVector);
538
+ failedSyncQueue.delete(key);
539
+ setPendingInternal(key, false);
540
+ logger$1.debug("Prose sync completed", {
541
+ collection: collection$1,
542
+ documentId
543
+ });
544
+ } catch (err) {
545
+ logger$1.error("Prose sync failed, queued for retry", {
546
+ collection: collection$1,
547
+ documentId,
548
+ error: String(err)
549
+ });
550
+ failedSyncQueue.set(key, true);
551
+ }
552
+ }, debounceMs);
553
+ debounceTimers.set(key, timer);
554
+ if (failedSyncQueue.has(key)) {
555
+ failedSyncQueue.delete(key);
556
+ logger$1.debug("Retrying failed sync", {
557
+ collection: collection$1,
558
+ documentId
559
+ });
560
+ }
561
+ };
562
+ fragment.observeDeep(observerHandler);
563
+ const cleanup$1 = () => {
564
+ fragment.unobserveDeep(observerHandler);
565
+ cancelPending(collection$1, documentId);
566
+ fragmentObservers.delete(key);
567
+ lastSyncedVectors.delete(key);
568
+ logger$1.debug("Fragment observer cleaned up", {
569
+ collection: collection$1,
570
+ documentId,
571
+ field
572
+ });
573
+ };
574
+ fragmentObservers.set(key, cleanup$1);
575
+ logger$1.debug("Fragment observer registered", {
576
+ collection: collection$1,
577
+ documentId,
578
+ field
579
+ });
580
+ return cleanup$1;
581
+ }
582
+ /**
583
+ * Clean up all prose state for a collection.
584
+ * Called when collection is destroyed.
585
+ */
586
+ function cleanup(collection$1) {
587
+ const prefix = `${collection$1}:`;
588
+ for (const [key, timer] of debounceTimers) if (key.startsWith(prefix)) {
589
+ clearTimeout(timer);
590
+ debounceTimers.delete(key);
591
+ }
592
+ for (const key of pendingState.keys()) if (key.startsWith(prefix)) pendingState.delete(key);
593
+ for (const key of pendingListeners.keys()) if (key.startsWith(prefix)) pendingListeners.delete(key);
594
+ for (const key of applyingFromServer.keys()) if (key.startsWith(prefix)) applyingFromServer.delete(key);
595
+ for (const key of lastSyncedVectors.keys()) if (key.startsWith(prefix)) lastSyncedVectors.delete(key);
596
+ for (const [key, cleanupFn] of fragmentObservers) if (key.startsWith(prefix)) {
597
+ cleanupFn();
598
+ fragmentObservers.delete(key);
599
+ }
600
+ for (const key of failedSyncQueue.keys()) if (key.startsWith(prefix)) failedSyncQueue.delete(key);
601
+ logger$1.debug("Prose cleanup complete", { collection: collection$1 });
602
+ }
603
+
604
+ //#endregion
605
+ //#region node_modules/zod/v4/core/core.js
606
+ /** A special constant with type `never` */
607
+ const NEVER = Object.freeze({ status: "aborted" });
608
+ function $constructor(name, initializer$2, params) {
609
+ function init(inst, def) {
610
+ if (!inst._zod) Object.defineProperty(inst, "_zod", {
611
+ value: {
612
+ def,
613
+ constr: _,
614
+ traits: /* @__PURE__ */ new Set()
615
+ },
616
+ enumerable: false
617
+ });
618
+ if (inst._zod.traits.has(name)) return;
619
+ inst._zod.traits.add(name);
620
+ initializer$2(inst, def);
621
+ const proto = _.prototype;
622
+ const keys = Object.keys(proto);
623
+ for (let i = 0; i < keys.length; i++) {
624
+ const k = keys[i];
625
+ if (!(k in inst)) inst[k] = proto[k].bind(inst);
626
+ }
627
+ }
628
+ const Parent = params?.Parent ?? Object;
629
+ class Definition extends Parent {}
630
+ Object.defineProperty(Definition, "name", { value: name });
631
+ function _(def) {
632
+ var _a$1;
633
+ const inst = params?.Parent ? new Definition() : this;
634
+ init(inst, def);
635
+ (_a$1 = inst._zod).deferred ?? (_a$1.deferred = []);
636
+ for (const fn of inst._zod.deferred) fn();
637
+ return inst;
638
+ }
639
+ Object.defineProperty(_, "init", { value: init });
640
+ Object.defineProperty(_, Symbol.hasInstance, { value: (inst) => {
641
+ if (params?.Parent && inst instanceof params.Parent) return true;
642
+ return inst?._zod?.traits?.has(name);
643
+ } });
644
+ Object.defineProperty(_, "name", { value: name });
645
+ return _;
646
+ }
647
+ var $ZodAsyncError = class extends Error {
648
+ constructor() {
649
+ super(`Encountered Promise during synchronous parse. Use .parseAsync() instead.`);
650
+ }
651
+ };
652
+ var $ZodEncodeError = class extends Error {
653
+ constructor(name) {
654
+ super(`Encountered unidirectional transform during encode: ${name}`);
655
+ this.name = "ZodEncodeError";
656
+ }
657
+ };
658
+ const globalConfig = {};
659
+ function config(newConfig) {
660
+ if (newConfig) Object.assign(globalConfig, newConfig);
661
+ return globalConfig;
662
+ }
663
+
664
+ //#endregion
665
+ //#region node_modules/zod/v4/core/util.js
666
+ function getEnumValues(entries) {
667
+ const numericValues = Object.values(entries).filter((v) => typeof v === "number");
668
+ return Object.entries(entries).filter(([k, _]) => numericValues.indexOf(+k) === -1).map(([_, v]) => v);
669
+ }
670
+ function jsonStringifyReplacer(_, value) {
671
+ if (typeof value === "bigint") return value.toString();
672
+ return value;
673
+ }
674
+ function cached(getter) {
675
+ return { get value() {
676
+ {
677
+ const value = getter();
678
+ Object.defineProperty(this, "value", { value });
679
+ return value;
680
+ }
681
+ throw new Error("cached value already set");
682
+ } };
683
+ }
684
+ function nullish(input) {
685
+ return input === null || input === void 0;
686
+ }
687
+ function cleanRegex(source) {
688
+ const start = source.startsWith("^") ? 1 : 0;
689
+ const end = source.endsWith("$") ? source.length - 1 : source.length;
690
+ return source.slice(start, end);
691
+ }
692
+ const EVALUATING = Symbol("evaluating");
693
+ function defineLazy(object, key, getter) {
694
+ let value = void 0;
695
+ Object.defineProperty(object, key, {
696
+ get() {
697
+ if (value === EVALUATING) return;
698
+ if (value === void 0) {
699
+ value = EVALUATING;
700
+ value = getter();
701
+ }
702
+ return value;
703
+ },
704
+ set(v) {
705
+ Object.defineProperty(object, key, { value: v });
706
+ },
707
+ configurable: true
708
+ });
709
+ }
710
+ function assignProp(target, prop, value) {
711
+ Object.defineProperty(target, prop, {
712
+ value,
713
+ writable: true,
714
+ enumerable: true,
715
+ configurable: true
716
+ });
717
+ }
718
+ function mergeDefs(...defs) {
719
+ const mergedDescriptors = {};
720
+ for (const def of defs) {
721
+ const descriptors = Object.getOwnPropertyDescriptors(def);
722
+ Object.assign(mergedDescriptors, descriptors);
723
+ }
724
+ return Object.defineProperties({}, mergedDescriptors);
725
+ }
726
+ function esc(str) {
727
+ return JSON.stringify(str);
728
+ }
729
+ const captureStackTrace = "captureStackTrace" in Error ? Error.captureStackTrace : (..._args) => {};
730
+ function isObject(data) {
731
+ return typeof data === "object" && data !== null && !Array.isArray(data);
732
+ }
733
+ const allowsEval = cached(() => {
734
+ if (typeof navigator !== "undefined" && navigator?.userAgent?.includes("Cloudflare")) return false;
735
+ try {
736
+ new Function("");
737
+ return true;
738
+ } catch (_) {
739
+ return false;
740
+ }
741
+ });
742
+ function isPlainObject(o) {
743
+ if (isObject(o) === false) return false;
744
+ const ctor = o.constructor;
745
+ if (ctor === void 0) return true;
746
+ if (typeof ctor !== "function") return true;
747
+ const prot = ctor.prototype;
748
+ if (isObject(prot) === false) return false;
749
+ if (Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf") === false) return false;
750
+ return true;
751
+ }
752
+ function shallowClone(o) {
753
+ if (isPlainObject(o)) return { ...o };
754
+ if (Array.isArray(o)) return [...o];
755
+ return o;
756
+ }
757
+ const propertyKeyTypes = new Set([
758
+ "string",
759
+ "number",
760
+ "symbol"
761
+ ]);
762
+ function escapeRegex(str) {
763
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
764
+ }
765
+ function clone(inst, def, params) {
766
+ const cl = new inst._zod.constr(def ?? inst._zod.def);
767
+ if (!def || params?.parent) cl._zod.parent = inst;
768
+ return cl;
769
+ }
770
+ function normalizeParams(_params) {
771
+ const params = _params;
772
+ if (!params) return {};
773
+ if (typeof params === "string") return { error: () => params };
774
+ if (params?.message !== void 0) {
775
+ if (params?.error !== void 0) throw new Error("Cannot specify both `message` and `error` params");
776
+ params.error = params.message;
777
+ }
778
+ delete params.message;
779
+ if (typeof params.error === "string") return {
780
+ ...params,
781
+ error: () => params.error
782
+ };
783
+ return params;
784
+ }
785
+ function optionalKeys(shape) {
786
+ return Object.keys(shape).filter((k) => {
787
+ return shape[k]._zod.optin === "optional" && shape[k]._zod.optout === "optional";
788
+ });
789
+ }
790
+ const NUMBER_FORMAT_RANGES = {
791
+ safeint: [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],
792
+ int32: [-2147483648, 2147483647],
793
+ uint32: [0, 4294967295],
794
+ float32: [-34028234663852886e22, 34028234663852886e22],
795
+ float64: [-Number.MAX_VALUE, Number.MAX_VALUE]
796
+ };
797
+ function pick(schema, mask) {
798
+ const currDef = schema._zod.def;
799
+ return clone(schema, mergeDefs(schema._zod.def, {
800
+ get shape() {
801
+ const newShape = {};
802
+ for (const key in mask) {
803
+ if (!(key in currDef.shape)) throw new Error(`Unrecognized key: "${key}"`);
804
+ if (!mask[key]) continue;
805
+ newShape[key] = currDef.shape[key];
806
+ }
807
+ assignProp(this, "shape", newShape);
808
+ return newShape;
809
+ },
810
+ checks: []
811
+ }));
812
+ }
813
+ function omit(schema, mask) {
814
+ const currDef = schema._zod.def;
815
+ return clone(schema, mergeDefs(schema._zod.def, {
816
+ get shape() {
817
+ const newShape = { ...schema._zod.def.shape };
818
+ for (const key in mask) {
819
+ if (!(key in currDef.shape)) throw new Error(`Unrecognized key: "${key}"`);
820
+ if (!mask[key]) continue;
821
+ delete newShape[key];
822
+ }
823
+ assignProp(this, "shape", newShape);
824
+ return newShape;
825
+ },
826
+ checks: []
827
+ }));
828
+ }
829
+ function extend(schema, shape) {
830
+ if (!isPlainObject(shape)) throw new Error("Invalid input to extend: expected a plain object");
831
+ const checks = schema._zod.def.checks;
832
+ if (checks && checks.length > 0) throw new Error("Object schemas containing refinements cannot be extended. Use `.safeExtend()` instead.");
833
+ return clone(schema, mergeDefs(schema._zod.def, {
834
+ get shape() {
835
+ const _shape = {
836
+ ...schema._zod.def.shape,
837
+ ...shape
838
+ };
839
+ assignProp(this, "shape", _shape);
840
+ return _shape;
841
+ },
842
+ checks: []
843
+ }));
844
+ }
845
+ function safeExtend(schema, shape) {
846
+ if (!isPlainObject(shape)) throw new Error("Invalid input to safeExtend: expected a plain object");
847
+ return clone(schema, {
848
+ ...schema._zod.def,
849
+ get shape() {
850
+ const _shape = {
851
+ ...schema._zod.def.shape,
852
+ ...shape
853
+ };
854
+ assignProp(this, "shape", _shape);
855
+ return _shape;
856
+ },
857
+ checks: schema._zod.def.checks
858
+ });
859
+ }
860
+ function merge(a, b) {
861
+ return clone(a, mergeDefs(a._zod.def, {
862
+ get shape() {
863
+ const _shape = {
864
+ ...a._zod.def.shape,
865
+ ...b._zod.def.shape
866
+ };
867
+ assignProp(this, "shape", _shape);
868
+ return _shape;
869
+ },
870
+ get catchall() {
871
+ return b._zod.def.catchall;
872
+ },
873
+ checks: []
874
+ }));
875
+ }
876
+ function partial(Class, schema, mask) {
877
+ return clone(schema, mergeDefs(schema._zod.def, {
878
+ get shape() {
879
+ const oldShape = schema._zod.def.shape;
880
+ const shape = { ...oldShape };
881
+ if (mask) for (const key in mask) {
882
+ if (!(key in oldShape)) throw new Error(`Unrecognized key: "${key}"`);
883
+ if (!mask[key]) continue;
884
+ shape[key] = Class ? new Class({
885
+ type: "optional",
886
+ innerType: oldShape[key]
887
+ }) : oldShape[key];
888
+ }
889
+ else for (const key in oldShape) shape[key] = Class ? new Class({
890
+ type: "optional",
891
+ innerType: oldShape[key]
892
+ }) : oldShape[key];
893
+ assignProp(this, "shape", shape);
894
+ return shape;
895
+ },
896
+ checks: []
897
+ }));
898
+ }
899
+ function required(Class, schema, mask) {
900
+ return clone(schema, mergeDefs(schema._zod.def, {
901
+ get shape() {
902
+ const oldShape = schema._zod.def.shape;
903
+ const shape = { ...oldShape };
904
+ if (mask) for (const key in mask) {
905
+ if (!(key in shape)) throw new Error(`Unrecognized key: "${key}"`);
906
+ if (!mask[key]) continue;
907
+ shape[key] = new Class({
908
+ type: "nonoptional",
909
+ innerType: oldShape[key]
910
+ });
911
+ }
912
+ else for (const key in oldShape) shape[key] = new Class({
913
+ type: "nonoptional",
914
+ innerType: oldShape[key]
915
+ });
916
+ assignProp(this, "shape", shape);
917
+ return shape;
918
+ },
919
+ checks: []
920
+ }));
921
+ }
922
+ function aborted(x, startIndex = 0) {
923
+ if (x.aborted === true) return true;
924
+ for (let i = startIndex; i < x.issues.length; i++) if (x.issues[i]?.continue !== true) return true;
925
+ return false;
926
+ }
927
+ function prefixIssues(path, issues) {
928
+ return issues.map((iss) => {
929
+ var _a$1;
930
+ (_a$1 = iss).path ?? (_a$1.path = []);
931
+ iss.path.unshift(path);
932
+ return iss;
933
+ });
934
+ }
935
+ function unwrapMessage(message) {
936
+ return typeof message === "string" ? message : message?.message;
937
+ }
938
+ function finalizeIssue(iss, ctx, config$1) {
939
+ const full = {
940
+ ...iss,
941
+ path: iss.path ?? []
942
+ };
943
+ if (!iss.message) full.message = unwrapMessage(iss.inst?._zod.def?.error?.(iss)) ?? unwrapMessage(ctx?.error?.(iss)) ?? unwrapMessage(config$1.customError?.(iss)) ?? unwrapMessage(config$1.localeError?.(iss)) ?? "Invalid input";
944
+ delete full.inst;
945
+ delete full.continue;
946
+ if (!ctx?.reportInput) delete full.input;
947
+ return full;
948
+ }
949
+ function getLengthableOrigin(input) {
950
+ if (Array.isArray(input)) return "array";
951
+ if (typeof input === "string") return "string";
952
+ return "unknown";
953
+ }
954
+ function issue(...args) {
955
+ const [iss, input, inst] = args;
956
+ if (typeof iss === "string") return {
957
+ message: iss,
958
+ code: "custom",
959
+ input,
960
+ inst
961
+ };
962
+ return { ...iss };
963
+ }
964
+
965
+ //#endregion
966
+ //#region node_modules/zod/v4/core/errors.js
967
+ const initializer$1 = (inst, def) => {
968
+ inst.name = "$ZodError";
969
+ Object.defineProperty(inst, "_zod", {
970
+ value: inst._zod,
971
+ enumerable: false
972
+ });
973
+ Object.defineProperty(inst, "issues", {
974
+ value: def,
975
+ enumerable: false
976
+ });
977
+ inst.message = JSON.stringify(def, jsonStringifyReplacer, 2);
978
+ Object.defineProperty(inst, "toString", {
979
+ value: () => inst.message,
980
+ enumerable: false
981
+ });
982
+ };
983
+ const $ZodError = $constructor("$ZodError", initializer$1);
984
+ const $ZodRealError = $constructor("$ZodError", initializer$1, { Parent: Error });
985
+ function flattenError(error, mapper = (issue$1) => issue$1.message) {
986
+ const fieldErrors = {};
987
+ const formErrors = [];
988
+ for (const sub of error.issues) if (sub.path.length > 0) {
989
+ fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
990
+ fieldErrors[sub.path[0]].push(mapper(sub));
991
+ } else formErrors.push(mapper(sub));
992
+ return {
993
+ formErrors,
994
+ fieldErrors
995
+ };
996
+ }
997
+ function formatError(error, mapper = (issue$1) => issue$1.message) {
998
+ const fieldErrors = { _errors: [] };
999
+ const processError = (error$1) => {
1000
+ for (const issue$1 of error$1.issues) if (issue$1.code === "invalid_union" && issue$1.errors.length) issue$1.errors.map((issues) => processError({ issues }));
1001
+ else if (issue$1.code === "invalid_key") processError({ issues: issue$1.issues });
1002
+ else if (issue$1.code === "invalid_element") processError({ issues: issue$1.issues });
1003
+ else if (issue$1.path.length === 0) fieldErrors._errors.push(mapper(issue$1));
1004
+ else {
1005
+ let curr = fieldErrors;
1006
+ let i = 0;
1007
+ while (i < issue$1.path.length) {
1008
+ const el = issue$1.path[i];
1009
+ if (!(i === issue$1.path.length - 1)) curr[el] = curr[el] || { _errors: [] };
1010
+ else {
1011
+ curr[el] = curr[el] || { _errors: [] };
1012
+ curr[el]._errors.push(mapper(issue$1));
1013
+ }
1014
+ curr = curr[el];
1015
+ i++;
1016
+ }
1017
+ }
1018
+ };
1019
+ processError(error);
1020
+ return fieldErrors;
1021
+ }
1022
+
1023
+ //#endregion
1024
+ //#region node_modules/zod/v4/core/parse.js
1025
+ const _parse = (_Err) => (schema, value, _ctx, _params) => {
1026
+ const ctx = _ctx ? Object.assign(_ctx, { async: false }) : { async: false };
1027
+ const result = schema._zod.run({
1028
+ value,
1029
+ issues: []
1030
+ }, ctx);
1031
+ if (result instanceof Promise) throw new $ZodAsyncError();
1032
+ if (result.issues.length) {
1033
+ const e = new (_params?.Err ?? _Err)(result.issues.map((iss) => finalizeIssue(iss, ctx, config())));
1034
+ captureStackTrace(e, _params?.callee);
1035
+ throw e;
1036
+ }
1037
+ return result.value;
1038
+ };
1039
+ const parse$1 = /* @__PURE__ */ _parse($ZodRealError);
1040
+ const _parseAsync = (_Err) => async (schema, value, _ctx, params) => {
1041
+ const ctx = _ctx ? Object.assign(_ctx, { async: true }) : { async: true };
1042
+ let result = schema._zod.run({
1043
+ value,
1044
+ issues: []
1045
+ }, ctx);
1046
+ if (result instanceof Promise) result = await result;
1047
+ if (result.issues.length) {
1048
+ const e = new (params?.Err ?? _Err)(result.issues.map((iss) => finalizeIssue(iss, ctx, config())));
1049
+ captureStackTrace(e, params?.callee);
1050
+ throw e;
1051
+ }
1052
+ return result.value;
1053
+ };
1054
+ const parseAsync$1 = /* @__PURE__ */ _parseAsync($ZodRealError);
1055
+ const _safeParse = (_Err) => (schema, value, _ctx) => {
1056
+ const ctx = _ctx ? {
1057
+ ..._ctx,
1058
+ async: false
1059
+ } : { async: false };
1060
+ const result = schema._zod.run({
1061
+ value,
1062
+ issues: []
1063
+ }, ctx);
1064
+ if (result instanceof Promise) throw new $ZodAsyncError();
1065
+ return result.issues.length ? {
1066
+ success: false,
1067
+ error: new (_Err ?? $ZodError)(result.issues.map((iss) => finalizeIssue(iss, ctx, config())))
1068
+ } : {
1069
+ success: true,
1070
+ data: result.value
1071
+ };
1072
+ };
1073
+ const safeParse$1 = /* @__PURE__ */ _safeParse($ZodRealError);
1074
+ const _safeParseAsync = (_Err) => async (schema, value, _ctx) => {
1075
+ const ctx = _ctx ? Object.assign(_ctx, { async: true }) : { async: true };
1076
+ let result = schema._zod.run({
1077
+ value,
1078
+ issues: []
1079
+ }, ctx);
1080
+ if (result instanceof Promise) result = await result;
1081
+ return result.issues.length ? {
1082
+ success: false,
1083
+ error: new _Err(result.issues.map((iss) => finalizeIssue(iss, ctx, config())))
1084
+ } : {
1085
+ success: true,
1086
+ data: result.value
1087
+ };
1088
+ };
1089
+ const safeParseAsync$1 = /* @__PURE__ */ _safeParseAsync($ZodRealError);
1090
+ const _encode = (_Err) => (schema, value, _ctx) => {
1091
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
1092
+ return _parse(_Err)(schema, value, ctx);
1093
+ };
1094
+ const encode$1 = /* @__PURE__ */ _encode($ZodRealError);
1095
+ const _decode = (_Err) => (schema, value, _ctx) => {
1096
+ return _parse(_Err)(schema, value, _ctx);
1097
+ };
1098
+ const decode$1 = /* @__PURE__ */ _decode($ZodRealError);
1099
+ const _encodeAsync = (_Err) => async (schema, value, _ctx) => {
1100
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
1101
+ return _parseAsync(_Err)(schema, value, ctx);
1102
+ };
1103
+ const encodeAsync$1 = /* @__PURE__ */ _encodeAsync($ZodRealError);
1104
+ const _decodeAsync = (_Err) => async (schema, value, _ctx) => {
1105
+ return _parseAsync(_Err)(schema, value, _ctx);
1106
+ };
1107
+ const decodeAsync$1 = /* @__PURE__ */ _decodeAsync($ZodRealError);
1108
+ const _safeEncode = (_Err) => (schema, value, _ctx) => {
1109
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
1110
+ return _safeParse(_Err)(schema, value, ctx);
1111
+ };
1112
+ const safeEncode$1 = /* @__PURE__ */ _safeEncode($ZodRealError);
1113
+ const _safeDecode = (_Err) => (schema, value, _ctx) => {
1114
+ return _safeParse(_Err)(schema, value, _ctx);
1115
+ };
1116
+ const safeDecode$1 = /* @__PURE__ */ _safeDecode($ZodRealError);
1117
+ const _safeEncodeAsync = (_Err) => async (schema, value, _ctx) => {
1118
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
1119
+ return _safeParseAsync(_Err)(schema, value, ctx);
1120
+ };
1121
+ const safeEncodeAsync$1 = /* @__PURE__ */ _safeEncodeAsync($ZodRealError);
1122
+ const _safeDecodeAsync = (_Err) => async (schema, value, _ctx) => {
1123
+ return _safeParseAsync(_Err)(schema, value, _ctx);
1124
+ };
1125
+ const safeDecodeAsync$1 = /* @__PURE__ */ _safeDecodeAsync($ZodRealError);
1126
+
1127
+ //#endregion
1128
+ //#region node_modules/zod/v4/core/checks.js
1129
+ const $ZodCheck = /* @__PURE__ */ $constructor("$ZodCheck", (inst, def) => {
1130
+ var _a$1;
1131
+ inst._zod ?? (inst._zod = {});
1132
+ inst._zod.def = def;
1133
+ (_a$1 = inst._zod).onattach ?? (_a$1.onattach = []);
1134
+ });
1135
+ const $ZodCheckMaxLength = /* @__PURE__ */ $constructor("$ZodCheckMaxLength", (inst, def) => {
1136
+ var _a$1;
1137
+ $ZodCheck.init(inst, def);
1138
+ (_a$1 = inst._zod.def).when ?? (_a$1.when = (payload) => {
1139
+ const val = payload.value;
1140
+ return !nullish(val) && val.length !== void 0;
1141
+ });
1142
+ inst._zod.onattach.push((inst$1) => {
1143
+ const curr = inst$1._zod.bag.maximum ?? Number.POSITIVE_INFINITY;
1144
+ if (def.maximum < curr) inst$1._zod.bag.maximum = def.maximum;
1145
+ });
1146
+ inst._zod.check = (payload) => {
1147
+ const input = payload.value;
1148
+ if (input.length <= def.maximum) return;
1149
+ const origin = getLengthableOrigin(input);
1150
+ payload.issues.push({
1151
+ origin,
1152
+ code: "too_big",
1153
+ maximum: def.maximum,
1154
+ inclusive: true,
1155
+ input,
1156
+ inst,
1157
+ continue: !def.abort
1158
+ });
1159
+ };
1160
+ });
1161
+ const $ZodCheckMinLength = /* @__PURE__ */ $constructor("$ZodCheckMinLength", (inst, def) => {
1162
+ var _a$1;
1163
+ $ZodCheck.init(inst, def);
1164
+ (_a$1 = inst._zod.def).when ?? (_a$1.when = (payload) => {
1165
+ const val = payload.value;
1166
+ return !nullish(val) && val.length !== void 0;
1167
+ });
1168
+ inst._zod.onattach.push((inst$1) => {
1169
+ const curr = inst$1._zod.bag.minimum ?? Number.NEGATIVE_INFINITY;
1170
+ if (def.minimum > curr) inst$1._zod.bag.minimum = def.minimum;
1171
+ });
1172
+ inst._zod.check = (payload) => {
1173
+ const input = payload.value;
1174
+ if (input.length >= def.minimum) return;
1175
+ const origin = getLengthableOrigin(input);
1176
+ payload.issues.push({
1177
+ origin,
1178
+ code: "too_small",
1179
+ minimum: def.minimum,
1180
+ inclusive: true,
1181
+ input,
1182
+ inst,
1183
+ continue: !def.abort
1184
+ });
1185
+ };
1186
+ });
1187
+ const $ZodCheckLengthEquals = /* @__PURE__ */ $constructor("$ZodCheckLengthEquals", (inst, def) => {
1188
+ var _a$1;
1189
+ $ZodCheck.init(inst, def);
1190
+ (_a$1 = inst._zod.def).when ?? (_a$1.when = (payload) => {
1191
+ const val = payload.value;
1192
+ return !nullish(val) && val.length !== void 0;
1193
+ });
1194
+ inst._zod.onattach.push((inst$1) => {
1195
+ const bag = inst$1._zod.bag;
1196
+ bag.minimum = def.length;
1197
+ bag.maximum = def.length;
1198
+ bag.length = def.length;
1199
+ });
1200
+ inst._zod.check = (payload) => {
1201
+ const input = payload.value;
1202
+ const length = input.length;
1203
+ if (length === def.length) return;
1204
+ const origin = getLengthableOrigin(input);
1205
+ const tooBig = length > def.length;
1206
+ payload.issues.push({
1207
+ origin,
1208
+ ...tooBig ? {
1209
+ code: "too_big",
1210
+ maximum: def.length
1211
+ } : {
1212
+ code: "too_small",
1213
+ minimum: def.length
1214
+ },
1215
+ inclusive: true,
1216
+ exact: true,
1217
+ input: payload.value,
1218
+ inst,
1219
+ continue: !def.abort
1220
+ });
1221
+ };
1222
+ });
1223
+ const $ZodCheckOverwrite = /* @__PURE__ */ $constructor("$ZodCheckOverwrite", (inst, def) => {
1224
+ $ZodCheck.init(inst, def);
1225
+ inst._zod.check = (payload) => {
1226
+ payload.value = def.tx(payload.value);
1227
+ };
1228
+ });
1229
+
1230
+ //#endregion
1231
+ //#region node_modules/zod/v4/core/doc.js
1232
+ var Doc = class {
1233
+ constructor(args = []) {
1234
+ this.content = [];
1235
+ this.indent = 0;
1236
+ if (this) this.args = args;
1237
+ }
1238
+ indented(fn) {
1239
+ this.indent += 1;
1240
+ fn(this);
1241
+ this.indent -= 1;
1242
+ }
1243
+ write(arg) {
1244
+ if (typeof arg === "function") {
1245
+ arg(this, { execution: "sync" });
1246
+ arg(this, { execution: "async" });
1247
+ return;
1248
+ }
1249
+ const lines = arg.split("\n").filter((x) => x);
1250
+ const minIndent = Math.min(...lines.map((x) => x.length - x.trimStart().length));
1251
+ const dedented = lines.map((x) => x.slice(minIndent)).map((x) => " ".repeat(this.indent * 2) + x);
1252
+ for (const line of dedented) this.content.push(line);
1253
+ }
1254
+ compile() {
1255
+ const F = Function;
1256
+ const args = this?.args;
1257
+ const lines = [...(this?.content ?? [``]).map((x) => ` ${x}`)];
1258
+ return new F(...args, lines.join("\n"));
1259
+ }
1260
+ };
1261
+
1262
+ //#endregion
1263
+ //#region node_modules/zod/v4/core/versions.js
1264
+ const version = {
1265
+ major: 4,
1266
+ minor: 2,
1267
+ patch: 1
1268
+ };
1269
+
1270
+ //#endregion
1271
+ //#region node_modules/zod/v4/core/schemas.js
1272
+ const $ZodType = /* @__PURE__ */ $constructor("$ZodType", (inst, def) => {
1273
+ var _a$1;
1274
+ inst ?? (inst = {});
1275
+ inst._zod.def = def;
1276
+ inst._zod.bag = inst._zod.bag || {};
1277
+ inst._zod.version = version;
1278
+ const checks = [...inst._zod.def.checks ?? []];
1279
+ if (inst._zod.traits.has("$ZodCheck")) checks.unshift(inst);
1280
+ for (const ch of checks) for (const fn of ch._zod.onattach) fn(inst);
1281
+ if (checks.length === 0) {
1282
+ (_a$1 = inst._zod).deferred ?? (_a$1.deferred = []);
1283
+ inst._zod.deferred?.push(() => {
1284
+ inst._zod.run = inst._zod.parse;
1285
+ });
1286
+ } else {
1287
+ const runChecks = (payload, checks$1, ctx) => {
1288
+ let isAborted = aborted(payload);
1289
+ let asyncResult;
1290
+ for (const ch of checks$1) {
1291
+ if (ch._zod.def.when) {
1292
+ if (!ch._zod.def.when(payload)) continue;
1293
+ } else if (isAborted) continue;
1294
+ const currLen = payload.issues.length;
1295
+ const _ = ch._zod.check(payload);
1296
+ if (_ instanceof Promise && ctx?.async === false) throw new $ZodAsyncError();
1297
+ if (asyncResult || _ instanceof Promise) asyncResult = (asyncResult ?? Promise.resolve()).then(async () => {
1298
+ await _;
1299
+ if (payload.issues.length === currLen) return;
1300
+ if (!isAborted) isAborted = aborted(payload, currLen);
1301
+ });
1302
+ else {
1303
+ if (payload.issues.length === currLen) continue;
1304
+ if (!isAborted) isAborted = aborted(payload, currLen);
1305
+ }
1306
+ }
1307
+ if (asyncResult) return asyncResult.then(() => {
1308
+ return payload;
1309
+ });
1310
+ return payload;
1311
+ };
1312
+ const handleCanaryResult = (canary, payload, ctx) => {
1313
+ if (aborted(canary)) {
1314
+ canary.aborted = true;
1315
+ return canary;
1316
+ }
1317
+ const checkResult = runChecks(payload, checks, ctx);
1318
+ if (checkResult instanceof Promise) {
1319
+ if (ctx.async === false) throw new $ZodAsyncError();
1320
+ return checkResult.then((checkResult$1) => inst._zod.parse(checkResult$1, ctx));
1321
+ }
1322
+ return inst._zod.parse(checkResult, ctx);
1323
+ };
1324
+ inst._zod.run = (payload, ctx) => {
1325
+ if (ctx.skipChecks) return inst._zod.parse(payload, ctx);
1326
+ if (ctx.direction === "backward") {
1327
+ const canary = inst._zod.parse({
1328
+ value: payload.value,
1329
+ issues: []
1330
+ }, {
1331
+ ...ctx,
1332
+ skipChecks: true
1333
+ });
1334
+ if (canary instanceof Promise) return canary.then((canary$1) => {
1335
+ return handleCanaryResult(canary$1, payload, ctx);
1336
+ });
1337
+ return handleCanaryResult(canary, payload, ctx);
1338
+ }
1339
+ const result = inst._zod.parse(payload, ctx);
1340
+ if (result instanceof Promise) {
1341
+ if (ctx.async === false) throw new $ZodAsyncError();
1342
+ return result.then((result$1) => runChecks(result$1, checks, ctx));
1343
+ }
1344
+ return runChecks(result, checks, ctx);
1345
+ };
1346
+ }
1347
+ inst["~standard"] = {
1348
+ validate: (value) => {
1349
+ try {
1350
+ const r = safeParse$1(inst, value);
1351
+ return r.success ? { value: r.data } : { issues: r.error?.issues };
1352
+ } catch (_) {
1353
+ return safeParseAsync$1(inst, value).then((r) => r.success ? { value: r.data } : { issues: r.error?.issues });
1354
+ }
1355
+ },
1356
+ vendor: "zod",
1357
+ version: 1
1358
+ };
1359
+ });
1360
+ const $ZodUnknown = /* @__PURE__ */ $constructor("$ZodUnknown", (inst, def) => {
1361
+ $ZodType.init(inst, def);
1362
+ inst._zod.parse = (payload) => payload;
1363
+ });
1364
+ const $ZodNever = /* @__PURE__ */ $constructor("$ZodNever", (inst, def) => {
1365
+ $ZodType.init(inst, def);
1366
+ inst._zod.parse = (payload, _ctx) => {
1367
+ payload.issues.push({
1368
+ expected: "never",
1369
+ code: "invalid_type",
1370
+ input: payload.value,
1371
+ inst
1372
+ });
1373
+ return payload;
1374
+ };
1375
+ });
1376
+ function handleArrayResult(result, final, index) {
1377
+ if (result.issues.length) final.issues.push(...prefixIssues(index, result.issues));
1378
+ final.value[index] = result.value;
1379
+ }
1380
+ const $ZodArray = /* @__PURE__ */ $constructor("$ZodArray", (inst, def) => {
1381
+ $ZodType.init(inst, def);
1382
+ inst._zod.parse = (payload, ctx) => {
1383
+ const input = payload.value;
1384
+ if (!Array.isArray(input)) {
1385
+ payload.issues.push({
1386
+ expected: "array",
1387
+ code: "invalid_type",
1388
+ input,
1389
+ inst
1390
+ });
1391
+ return payload;
1392
+ }
1393
+ payload.value = Array(input.length);
1394
+ const proms = [];
1395
+ for (let i = 0; i < input.length; i++) {
1396
+ const item = input[i];
1397
+ const result = def.element._zod.run({
1398
+ value: item,
1399
+ issues: []
1400
+ }, ctx);
1401
+ if (result instanceof Promise) proms.push(result.then((result$1) => handleArrayResult(result$1, payload, i)));
1402
+ else handleArrayResult(result, payload, i);
1403
+ }
1404
+ if (proms.length) return Promise.all(proms).then(() => payload);
1405
+ return payload;
1406
+ };
1407
+ });
1408
+ function handlePropertyResult(result, final, key, input) {
1409
+ if (result.issues.length) final.issues.push(...prefixIssues(key, result.issues));
1410
+ if (result.value === void 0) {
1411
+ if (key in input) final.value[key] = void 0;
1412
+ } else final.value[key] = result.value;
1413
+ }
1414
+ function normalizeDef(def) {
1415
+ const keys = Object.keys(def.shape);
1416
+ for (const k of keys) if (!def.shape?.[k]?._zod?.traits?.has("$ZodType")) throw new Error(`Invalid element at key "${k}": expected a Zod schema`);
1417
+ const okeys = optionalKeys(def.shape);
1418
+ return {
1419
+ ...def,
1420
+ keys,
1421
+ keySet: new Set(keys),
1422
+ numKeys: keys.length,
1423
+ optionalKeys: new Set(okeys)
1424
+ };
1425
+ }
1426
+ function handleCatchall(proms, input, payload, ctx, def, inst) {
1427
+ const unrecognized = [];
1428
+ const keySet = def.keySet;
1429
+ const _catchall = def.catchall._zod;
1430
+ const t = _catchall.def.type;
1431
+ for (const key in input) {
1432
+ if (keySet.has(key)) continue;
1433
+ if (t === "never") {
1434
+ unrecognized.push(key);
1435
+ continue;
1436
+ }
1437
+ const r = _catchall.run({
1438
+ value: input[key],
1439
+ issues: []
1440
+ }, ctx);
1441
+ if (r instanceof Promise) proms.push(r.then((r$1) => handlePropertyResult(r$1, payload, key, input)));
1442
+ else handlePropertyResult(r, payload, key, input);
1443
+ }
1444
+ if (unrecognized.length) payload.issues.push({
1445
+ code: "unrecognized_keys",
1446
+ keys: unrecognized,
1447
+ input,
1448
+ inst
1449
+ });
1450
+ if (!proms.length) return payload;
1451
+ return Promise.all(proms).then(() => {
1452
+ return payload;
1453
+ });
1454
+ }
1455
+ const $ZodObject = /* @__PURE__ */ $constructor("$ZodObject", (inst, def) => {
1456
+ $ZodType.init(inst, def);
1457
+ if (!Object.getOwnPropertyDescriptor(def, "shape")?.get) {
1458
+ const sh = def.shape;
1459
+ Object.defineProperty(def, "shape", { get: () => {
1460
+ const newSh = { ...sh };
1461
+ Object.defineProperty(def, "shape", { value: newSh });
1462
+ return newSh;
1463
+ } });
1464
+ }
1465
+ const _normalized = cached(() => normalizeDef(def));
1466
+ defineLazy(inst._zod, "propValues", () => {
1467
+ const shape = def.shape;
1468
+ const propValues = {};
1469
+ for (const key in shape) {
1470
+ const field = shape[key]._zod;
1471
+ if (field.values) {
1472
+ propValues[key] ?? (propValues[key] = /* @__PURE__ */ new Set());
1473
+ for (const v of field.values) propValues[key].add(v);
1474
+ }
1475
+ }
1476
+ return propValues;
1477
+ });
1478
+ const isObject$1 = isObject;
1479
+ const catchall = def.catchall;
1480
+ let value;
1481
+ inst._zod.parse = (payload, ctx) => {
1482
+ value ?? (value = _normalized.value);
1483
+ const input = payload.value;
1484
+ if (!isObject$1(input)) {
1485
+ payload.issues.push({
1486
+ expected: "object",
1487
+ code: "invalid_type",
1488
+ input,
1489
+ inst
1490
+ });
1491
+ return payload;
1492
+ }
1493
+ payload.value = {};
1494
+ const proms = [];
1495
+ const shape = value.shape;
1496
+ for (const key of value.keys) {
1497
+ const r = shape[key]._zod.run({
1498
+ value: input[key],
1499
+ issues: []
1500
+ }, ctx);
1501
+ if (r instanceof Promise) proms.push(r.then((r$1) => handlePropertyResult(r$1, payload, key, input)));
1502
+ else handlePropertyResult(r, payload, key, input);
1503
+ }
1504
+ if (!catchall) return proms.length ? Promise.all(proms).then(() => payload) : payload;
1505
+ return handleCatchall(proms, input, payload, ctx, _normalized.value, inst);
1506
+ };
1507
+ });
1508
+ const $ZodObjectJIT = /* @__PURE__ */ $constructor("$ZodObjectJIT", (inst, def) => {
1509
+ $ZodObject.init(inst, def);
1510
+ const superParse = inst._zod.parse;
1511
+ const _normalized = cached(() => normalizeDef(def));
1512
+ const generateFastpass = (shape) => {
1513
+ const doc = new Doc([
1514
+ "shape",
1515
+ "payload",
1516
+ "ctx"
1517
+ ]);
1518
+ const normalized = _normalized.value;
1519
+ const parseStr = (key) => {
1520
+ const k = esc(key);
1521
+ return `shape[${k}]._zod.run({ value: input[${k}], issues: [] }, ctx)`;
1522
+ };
1523
+ doc.write(`const input = payload.value;`);
1524
+ const ids = Object.create(null);
1525
+ let counter = 0;
1526
+ for (const key of normalized.keys) ids[key] = `key_${counter++}`;
1527
+ doc.write(`const newResult = {};`);
1528
+ for (const key of normalized.keys) {
1529
+ const id = ids[key];
1530
+ const k = esc(key);
1531
+ doc.write(`const ${id} = ${parseStr(key)};`);
1532
+ doc.write(`
1533
+ if (${id}.issues.length) {
1534
+ payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
1535
+ ...iss,
1536
+ path: iss.path ? [${k}, ...iss.path] : [${k}]
1537
+ })));
1538
+ }
1539
+
1540
+
1541
+ if (${id}.value === undefined) {
1542
+ if (${k} in input) {
1543
+ newResult[${k}] = undefined;
1544
+ }
1545
+ } else {
1546
+ newResult[${k}] = ${id}.value;
1547
+ }
1548
+
1549
+ `);
1550
+ }
1551
+ doc.write(`payload.value = newResult;`);
1552
+ doc.write(`return payload;`);
1553
+ const fn = doc.compile();
1554
+ return (payload, ctx) => fn(shape, payload, ctx);
1555
+ };
1556
+ let fastpass;
1557
+ const isObject$1 = isObject;
1558
+ const jit = !globalConfig.jitless;
1559
+ const allowsEval$1 = allowsEval;
1560
+ const fastEnabled = jit && allowsEval$1.value;
1561
+ const catchall = def.catchall;
1562
+ let value;
1563
+ inst._zod.parse = (payload, ctx) => {
1564
+ value ?? (value = _normalized.value);
1565
+ const input = payload.value;
1566
+ if (!isObject$1(input)) {
1567
+ payload.issues.push({
1568
+ expected: "object",
1569
+ code: "invalid_type",
1570
+ input,
1571
+ inst
1572
+ });
1573
+ return payload;
1574
+ }
1575
+ if (jit && fastEnabled && ctx?.async === false && ctx.jitless !== true) {
1576
+ if (!fastpass) fastpass = generateFastpass(def.shape);
1577
+ payload = fastpass(payload, ctx);
1578
+ if (!catchall) return payload;
1579
+ return handleCatchall([], input, payload, ctx, value, inst);
1580
+ }
1581
+ return superParse(payload, ctx);
1582
+ };
1583
+ });
1584
+ function handleUnionResults(results, final, inst, ctx) {
1585
+ for (const result of results) if (result.issues.length === 0) {
1586
+ final.value = result.value;
1587
+ return final;
1588
+ }
1589
+ const nonaborted = results.filter((r) => !aborted(r));
1590
+ if (nonaborted.length === 1) {
1591
+ final.value = nonaborted[0].value;
1592
+ return nonaborted[0];
1593
+ }
1594
+ final.issues.push({
1595
+ code: "invalid_union",
1596
+ input: final.value,
1597
+ inst,
1598
+ errors: results.map((result) => result.issues.map((iss) => finalizeIssue(iss, ctx, config())))
1599
+ });
1600
+ return final;
1601
+ }
1602
+ const $ZodUnion = /* @__PURE__ */ $constructor("$ZodUnion", (inst, def) => {
1603
+ $ZodType.init(inst, def);
1604
+ defineLazy(inst._zod, "optin", () => def.options.some((o) => o._zod.optin === "optional") ? "optional" : void 0);
1605
+ defineLazy(inst._zod, "optout", () => def.options.some((o) => o._zod.optout === "optional") ? "optional" : void 0);
1606
+ defineLazy(inst._zod, "values", () => {
1607
+ if (def.options.every((o) => o._zod.values)) return new Set(def.options.flatMap((option) => Array.from(option._zod.values)));
1608
+ });
1609
+ defineLazy(inst._zod, "pattern", () => {
1610
+ if (def.options.every((o) => o._zod.pattern)) {
1611
+ const patterns = def.options.map((o) => o._zod.pattern);
1612
+ return /* @__PURE__ */ new RegExp(`^(${patterns.map((p) => cleanRegex(p.source)).join("|")})$`);
1613
+ }
1614
+ });
1615
+ const single = def.options.length === 1;
1616
+ const first = def.options[0]._zod.run;
1617
+ inst._zod.parse = (payload, ctx) => {
1618
+ if (single) return first(payload, ctx);
1619
+ let async = false;
1620
+ const results = [];
1621
+ for (const option of def.options) {
1622
+ const result = option._zod.run({
1623
+ value: payload.value,
1624
+ issues: []
1625
+ }, ctx);
1626
+ if (result instanceof Promise) {
1627
+ results.push(result);
1628
+ async = true;
1629
+ } else {
1630
+ if (result.issues.length === 0) return result;
1631
+ results.push(result);
1632
+ }
1633
+ }
1634
+ if (!async) return handleUnionResults(results, payload, inst, ctx);
1635
+ return Promise.all(results).then((results$1) => {
1636
+ return handleUnionResults(results$1, payload, inst, ctx);
1637
+ });
1638
+ };
1639
+ });
1640
+ const $ZodIntersection = /* @__PURE__ */ $constructor("$ZodIntersection", (inst, def) => {
1641
+ $ZodType.init(inst, def);
1642
+ inst._zod.parse = (payload, ctx) => {
1643
+ const input = payload.value;
1644
+ const left = def.left._zod.run({
1645
+ value: input,
1646
+ issues: []
1647
+ }, ctx);
1648
+ const right = def.right._zod.run({
1649
+ value: input,
1650
+ issues: []
1651
+ }, ctx);
1652
+ if (left instanceof Promise || right instanceof Promise) return Promise.all([left, right]).then(([left$1, right$1]) => {
1653
+ return handleIntersectionResults(payload, left$1, right$1);
1654
+ });
1655
+ return handleIntersectionResults(payload, left, right);
1656
+ };
1657
+ });
1658
+ function mergeValues(a, b) {
1659
+ if (a === b) return {
1660
+ valid: true,
1661
+ data: a
1662
+ };
1663
+ if (a instanceof Date && b instanceof Date && +a === +b) return {
1664
+ valid: true,
1665
+ data: a
1666
+ };
1667
+ if (isPlainObject(a) && isPlainObject(b)) {
1668
+ const bKeys = Object.keys(b);
1669
+ const sharedKeys = Object.keys(a).filter((key) => bKeys.indexOf(key) !== -1);
1670
+ const newObj = {
1671
+ ...a,
1672
+ ...b
1673
+ };
1674
+ for (const key of sharedKeys) {
1675
+ const sharedValue = mergeValues(a[key], b[key]);
1676
+ if (!sharedValue.valid) return {
1677
+ valid: false,
1678
+ mergeErrorPath: [key, ...sharedValue.mergeErrorPath]
1679
+ };
1680
+ newObj[key] = sharedValue.data;
1681
+ }
1682
+ return {
1683
+ valid: true,
1684
+ data: newObj
1685
+ };
1686
+ }
1687
+ if (Array.isArray(a) && Array.isArray(b)) {
1688
+ if (a.length !== b.length) return {
1689
+ valid: false,
1690
+ mergeErrorPath: []
1691
+ };
1692
+ const newArray = [];
1693
+ for (let index = 0; index < a.length; index++) {
1694
+ const itemA = a[index];
1695
+ const itemB = b[index];
1696
+ const sharedValue = mergeValues(itemA, itemB);
1697
+ if (!sharedValue.valid) return {
1698
+ valid: false,
1699
+ mergeErrorPath: [index, ...sharedValue.mergeErrorPath]
1700
+ };
1701
+ newArray.push(sharedValue.data);
1702
+ }
1703
+ return {
1704
+ valid: true,
1705
+ data: newArray
1706
+ };
1707
+ }
1708
+ return {
1709
+ valid: false,
1710
+ mergeErrorPath: []
1711
+ };
1712
+ }
1713
+ function handleIntersectionResults(result, left, right) {
1714
+ if (left.issues.length) result.issues.push(...left.issues);
1715
+ if (right.issues.length) result.issues.push(...right.issues);
1716
+ if (aborted(result)) return result;
1717
+ const merged = mergeValues(left.value, right.value);
1718
+ if (!merged.valid) throw new Error(`Unmergable intersection. Error path: ${JSON.stringify(merged.mergeErrorPath)}`);
1719
+ result.value = merged.data;
1720
+ return result;
1721
+ }
1722
+ const $ZodEnum = /* @__PURE__ */ $constructor("$ZodEnum", (inst, def) => {
1723
+ $ZodType.init(inst, def);
1724
+ const values = getEnumValues(def.entries);
1725
+ const valuesSet = new Set(values);
1726
+ inst._zod.values = valuesSet;
1727
+ inst._zod.pattern = /* @__PURE__ */ new RegExp(`^(${values.filter((k) => propertyKeyTypes.has(typeof k)).map((o) => typeof o === "string" ? escapeRegex(o) : o.toString()).join("|")})$`);
1728
+ inst._zod.parse = (payload, _ctx) => {
1729
+ const input = payload.value;
1730
+ if (valuesSet.has(input)) return payload;
1731
+ payload.issues.push({
1732
+ code: "invalid_value",
1733
+ values,
1734
+ input,
1735
+ inst
1736
+ });
1737
+ return payload;
1738
+ };
1739
+ });
1740
+ const $ZodTransform = /* @__PURE__ */ $constructor("$ZodTransform", (inst, def) => {
1741
+ $ZodType.init(inst, def);
1742
+ inst._zod.parse = (payload, ctx) => {
1743
+ if (ctx.direction === "backward") throw new $ZodEncodeError(inst.constructor.name);
1744
+ const _out = def.transform(payload.value, payload);
1745
+ if (ctx.async) return (_out instanceof Promise ? _out : Promise.resolve(_out)).then((output) => {
1746
+ payload.value = output;
1747
+ return payload;
1748
+ });
1749
+ if (_out instanceof Promise) throw new $ZodAsyncError();
1750
+ payload.value = _out;
1751
+ return payload;
1752
+ };
1753
+ });
1754
+ function handleOptionalResult(result, input) {
1755
+ if (result.issues.length && input === void 0) return {
1756
+ issues: [],
1757
+ value: void 0
1758
+ };
1759
+ return result;
1760
+ }
1761
+ const $ZodOptional = /* @__PURE__ */ $constructor("$ZodOptional", (inst, def) => {
1762
+ $ZodType.init(inst, def);
1763
+ inst._zod.optin = "optional";
1764
+ inst._zod.optout = "optional";
1765
+ defineLazy(inst._zod, "values", () => {
1766
+ return def.innerType._zod.values ? new Set([...def.innerType._zod.values, void 0]) : void 0;
1767
+ });
1768
+ defineLazy(inst._zod, "pattern", () => {
1769
+ const pattern = def.innerType._zod.pattern;
1770
+ return pattern ? /* @__PURE__ */ new RegExp(`^(${cleanRegex(pattern.source)})?$`) : void 0;
1771
+ });
1772
+ inst._zod.parse = (payload, ctx) => {
1773
+ if (def.innerType._zod.optin === "optional") {
1774
+ const result = def.innerType._zod.run(payload, ctx);
1775
+ if (result instanceof Promise) return result.then((r) => handleOptionalResult(r, payload.value));
1776
+ return handleOptionalResult(result, payload.value);
1777
+ }
1778
+ if (payload.value === void 0) return payload;
1779
+ return def.innerType._zod.run(payload, ctx);
1780
+ };
1781
+ });
1782
+ const $ZodNullable = /* @__PURE__ */ $constructor("$ZodNullable", (inst, def) => {
1783
+ $ZodType.init(inst, def);
1784
+ defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
1785
+ defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
1786
+ defineLazy(inst._zod, "pattern", () => {
1787
+ const pattern = def.innerType._zod.pattern;
1788
+ return pattern ? /* @__PURE__ */ new RegExp(`^(${cleanRegex(pattern.source)}|null)$`) : void 0;
1789
+ });
1790
+ defineLazy(inst._zod, "values", () => {
1791
+ return def.innerType._zod.values ? new Set([...def.innerType._zod.values, null]) : void 0;
1792
+ });
1793
+ inst._zod.parse = (payload, ctx) => {
1794
+ if (payload.value === null) return payload;
1795
+ return def.innerType._zod.run(payload, ctx);
1796
+ };
1797
+ });
1798
+ const $ZodDefault = /* @__PURE__ */ $constructor("$ZodDefault", (inst, def) => {
1799
+ $ZodType.init(inst, def);
1800
+ inst._zod.optin = "optional";
1801
+ defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1802
+ inst._zod.parse = (payload, ctx) => {
1803
+ if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
1804
+ if (payload.value === void 0) {
1805
+ payload.value = def.defaultValue;
1806
+ /**
1807
+ * $ZodDefault returns the default value immediately in forward direction.
1808
+ * It doesn't pass the default value into the validator ("prefault"). There's no reason to pass the default value through validation. The validity of the default is enforced by TypeScript statically. Otherwise, it's the responsibility of the user to ensure the default is valid. In the case of pipes with divergent in/out types, you can specify the default on the `in` schema of your ZodPipe to set a "prefault" for the pipe. */
1809
+ return payload;
1810
+ }
1811
+ const result = def.innerType._zod.run(payload, ctx);
1812
+ if (result instanceof Promise) return result.then((result$1) => handleDefaultResult(result$1, def));
1813
+ return handleDefaultResult(result, def);
1814
+ };
1815
+ });
1816
+ function handleDefaultResult(payload, def) {
1817
+ if (payload.value === void 0) payload.value = def.defaultValue;
1818
+ return payload;
1819
+ }
1820
+ const $ZodPrefault = /* @__PURE__ */ $constructor("$ZodPrefault", (inst, def) => {
1821
+ $ZodType.init(inst, def);
1822
+ inst._zod.optin = "optional";
1823
+ defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1824
+ inst._zod.parse = (payload, ctx) => {
1825
+ if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
1826
+ if (payload.value === void 0) payload.value = def.defaultValue;
1827
+ return def.innerType._zod.run(payload, ctx);
1828
+ };
1829
+ });
1830
+ const $ZodNonOptional = /* @__PURE__ */ $constructor("$ZodNonOptional", (inst, def) => {
1831
+ $ZodType.init(inst, def);
1832
+ defineLazy(inst._zod, "values", () => {
1833
+ const v = def.innerType._zod.values;
1834
+ return v ? new Set([...v].filter((x) => x !== void 0)) : void 0;
1835
+ });
1836
+ inst._zod.parse = (payload, ctx) => {
1837
+ const result = def.innerType._zod.run(payload, ctx);
1838
+ if (result instanceof Promise) return result.then((result$1) => handleNonOptionalResult(result$1, inst));
1839
+ return handleNonOptionalResult(result, inst);
1840
+ };
1841
+ });
1842
+ function handleNonOptionalResult(payload, inst) {
1843
+ if (!payload.issues.length && payload.value === void 0) payload.issues.push({
1844
+ code: "invalid_type",
1845
+ expected: "nonoptional",
1846
+ input: payload.value,
1847
+ inst
1848
+ });
1849
+ return payload;
1850
+ }
1851
+ const $ZodCatch = /* @__PURE__ */ $constructor("$ZodCatch", (inst, def) => {
1852
+ $ZodType.init(inst, def);
1853
+ defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
1854
+ defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
1855
+ defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1856
+ inst._zod.parse = (payload, ctx) => {
1857
+ if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
1858
+ const result = def.innerType._zod.run(payload, ctx);
1859
+ if (result instanceof Promise) return result.then((result$1) => {
1860
+ payload.value = result$1.value;
1861
+ if (result$1.issues.length) {
1862
+ payload.value = def.catchValue({
1863
+ ...payload,
1864
+ error: { issues: result$1.issues.map((iss) => finalizeIssue(iss, ctx, config())) },
1865
+ input: payload.value
1866
+ });
1867
+ payload.issues = [];
1868
+ }
1869
+ return payload;
1870
+ });
1871
+ payload.value = result.value;
1872
+ if (result.issues.length) {
1873
+ payload.value = def.catchValue({
1874
+ ...payload,
1875
+ error: { issues: result.issues.map((iss) => finalizeIssue(iss, ctx, config())) },
1876
+ input: payload.value
1877
+ });
1878
+ payload.issues = [];
1879
+ }
1880
+ return payload;
1881
+ };
1882
+ });
1883
+ const $ZodPipe = /* @__PURE__ */ $constructor("$ZodPipe", (inst, def) => {
1884
+ $ZodType.init(inst, def);
1885
+ defineLazy(inst._zod, "values", () => def.in._zod.values);
1886
+ defineLazy(inst._zod, "optin", () => def.in._zod.optin);
1887
+ defineLazy(inst._zod, "optout", () => def.out._zod.optout);
1888
+ defineLazy(inst._zod, "propValues", () => def.in._zod.propValues);
1889
+ inst._zod.parse = (payload, ctx) => {
1890
+ if (ctx.direction === "backward") {
1891
+ const right = def.out._zod.run(payload, ctx);
1892
+ if (right instanceof Promise) return right.then((right$1) => handlePipeResult(right$1, def.in, ctx));
1893
+ return handlePipeResult(right, def.in, ctx);
1894
+ }
1895
+ const left = def.in._zod.run(payload, ctx);
1896
+ if (left instanceof Promise) return left.then((left$1) => handlePipeResult(left$1, def.out, ctx));
1897
+ return handlePipeResult(left, def.out, ctx);
1898
+ };
1899
+ });
1900
+ function handlePipeResult(left, next, ctx) {
1901
+ if (left.issues.length) {
1902
+ left.aborted = true;
1903
+ return left;
1904
+ }
1905
+ return next._zod.run({
1906
+ value: left.value,
1907
+ issues: left.issues
1908
+ }, ctx);
1909
+ }
1910
+ const $ZodReadonly = /* @__PURE__ */ $constructor("$ZodReadonly", (inst, def) => {
1911
+ $ZodType.init(inst, def);
1912
+ defineLazy(inst._zod, "propValues", () => def.innerType._zod.propValues);
1913
+ defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1914
+ defineLazy(inst._zod, "optin", () => def.innerType?._zod?.optin);
1915
+ defineLazy(inst._zod, "optout", () => def.innerType?._zod?.optout);
1916
+ inst._zod.parse = (payload, ctx) => {
1917
+ if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
1918
+ const result = def.innerType._zod.run(payload, ctx);
1919
+ if (result instanceof Promise) return result.then(handleReadonlyResult);
1920
+ return handleReadonlyResult(result);
1921
+ };
1922
+ });
1923
+ function handleReadonlyResult(payload) {
1924
+ payload.value = Object.freeze(payload.value);
1925
+ return payload;
1926
+ }
1927
+ const $ZodCustom = /* @__PURE__ */ $constructor("$ZodCustom", (inst, def) => {
1928
+ $ZodCheck.init(inst, def);
1929
+ $ZodType.init(inst, def);
1930
+ inst._zod.parse = (payload, _) => {
1931
+ return payload;
1932
+ };
1933
+ inst._zod.check = (payload) => {
1934
+ const input = payload.value;
1935
+ const r = def.fn(input);
1936
+ if (r instanceof Promise) return r.then((r$1) => handleRefineResult(r$1, payload, input, inst));
1937
+ handleRefineResult(r, payload, input, inst);
1938
+ };
1939
+ });
1940
+ function handleRefineResult(result, payload, input, inst) {
1941
+ if (!result) {
1942
+ const _iss = {
1943
+ code: "custom",
1944
+ input,
1945
+ inst,
1946
+ path: [...inst._zod.def.path ?? []],
1947
+ continue: !inst._zod.def.abort
1948
+ };
1949
+ if (inst._zod.def.params) _iss.params = inst._zod.def.params;
1950
+ payload.issues.push(issue(_iss));
1951
+ }
1952
+ }
1953
+
1954
+ //#endregion
1955
+ //#region node_modules/zod/v4/core/registries.js
1956
+ var _a;
1957
+ var $ZodRegistry = class {
1958
+ constructor() {
1959
+ this._map = /* @__PURE__ */ new WeakMap();
1960
+ this._idmap = /* @__PURE__ */ new Map();
1961
+ }
1962
+ add(schema, ..._meta) {
1963
+ const meta$2 = _meta[0];
1964
+ this._map.set(schema, meta$2);
1965
+ if (meta$2 && typeof meta$2 === "object" && "id" in meta$2) {
1966
+ if (this._idmap.has(meta$2.id)) throw new Error(`ID ${meta$2.id} already exists in the registry`);
1967
+ this._idmap.set(meta$2.id, schema);
1968
+ }
1969
+ return this;
1970
+ }
1971
+ clear() {
1972
+ this._map = /* @__PURE__ */ new WeakMap();
1973
+ this._idmap = /* @__PURE__ */ new Map();
1974
+ return this;
1975
+ }
1976
+ remove(schema) {
1977
+ const meta$2 = this._map.get(schema);
1978
+ if (meta$2 && typeof meta$2 === "object" && "id" in meta$2) this._idmap.delete(meta$2.id);
1979
+ this._map.delete(schema);
1980
+ return this;
1981
+ }
1982
+ get(schema) {
1983
+ const p = schema._zod.parent;
1984
+ if (p) {
1985
+ const pm = { ...this.get(p) ?? {} };
1986
+ delete pm.id;
1987
+ const f = {
1988
+ ...pm,
1989
+ ...this._map.get(schema)
1990
+ };
1991
+ return Object.keys(f).length ? f : void 0;
1992
+ }
1993
+ return this._map.get(schema);
1994
+ }
1995
+ has(schema) {
1996
+ return this._map.has(schema);
1997
+ }
1998
+ };
1999
+ function registry() {
2000
+ return new $ZodRegistry();
2001
+ }
2002
+ (_a = globalThis).__zod_globalRegistry ?? (_a.__zod_globalRegistry = registry());
2003
+ const globalRegistry = globalThis.__zod_globalRegistry;
2004
+
2005
+ //#endregion
2006
+ //#region node_modules/zod/v4/core/api.js
2007
+ function _unknown(Class) {
2008
+ return new Class({ type: "unknown" });
2009
+ }
2010
+ function _never(Class, params) {
2011
+ return new Class({
2012
+ type: "never",
2013
+ ...normalizeParams(params)
2014
+ });
2015
+ }
2016
+ function _maxLength(maximum, params) {
2017
+ return new $ZodCheckMaxLength({
2018
+ check: "max_length",
2019
+ ...normalizeParams(params),
2020
+ maximum
2021
+ });
2022
+ }
2023
+ function _minLength(minimum, params) {
2024
+ return new $ZodCheckMinLength({
2025
+ check: "min_length",
2026
+ ...normalizeParams(params),
2027
+ minimum
2028
+ });
2029
+ }
2030
+ function _length(length, params) {
2031
+ return new $ZodCheckLengthEquals({
2032
+ check: "length_equals",
2033
+ ...normalizeParams(params),
2034
+ length
2035
+ });
2036
+ }
2037
+ function _overwrite(tx) {
2038
+ return new $ZodCheckOverwrite({
2039
+ check: "overwrite",
2040
+ tx
2041
+ });
2042
+ }
2043
+ function _array(Class, element, params) {
2044
+ return new Class({
2045
+ type: "array",
2046
+ element,
2047
+ ...normalizeParams(params)
2048
+ });
2049
+ }
2050
+ function _custom(Class, fn, _params) {
2051
+ const norm = normalizeParams(_params);
2052
+ norm.abort ?? (norm.abort = true);
2053
+ return new Class({
2054
+ type: "custom",
2055
+ check: "custom",
2056
+ fn,
2057
+ ...norm
2058
+ });
2059
+ }
2060
+ function _refine(Class, fn, _params) {
2061
+ return new Class({
2062
+ type: "custom",
2063
+ check: "custom",
2064
+ fn,
2065
+ ...normalizeParams(_params)
2066
+ });
2067
+ }
2068
+ function _superRefine(fn) {
2069
+ const ch = _check((payload) => {
2070
+ payload.addIssue = (issue$1) => {
2071
+ if (typeof issue$1 === "string") payload.issues.push(issue(issue$1, payload.value, ch._zod.def));
2072
+ else {
2073
+ const _issue = issue$1;
2074
+ if (_issue.fatal) _issue.continue = false;
2075
+ _issue.code ?? (_issue.code = "custom");
2076
+ _issue.input ?? (_issue.input = payload.value);
2077
+ _issue.inst ?? (_issue.inst = ch);
2078
+ _issue.continue ?? (_issue.continue = !ch._zod.def.abort);
2079
+ payload.issues.push(issue(_issue));
2080
+ }
2081
+ };
2082
+ return fn(payload.value, payload);
2083
+ });
2084
+ return ch;
2085
+ }
2086
+ function _check(fn, params) {
2087
+ const ch = new $ZodCheck({
2088
+ check: "custom",
2089
+ ...normalizeParams(params)
2090
+ });
2091
+ ch._zod.check = fn;
2092
+ return ch;
2093
+ }
2094
+ function describe$1(description) {
2095
+ const ch = new $ZodCheck({ check: "describe" });
2096
+ ch._zod.onattach = [(inst) => {
2097
+ const existing = globalRegistry.get(inst) ?? {};
2098
+ globalRegistry.add(inst, {
2099
+ ...existing,
2100
+ description
2101
+ });
2102
+ }];
2103
+ ch._zod.check = () => {};
2104
+ return ch;
2105
+ }
2106
+ function meta$1(metadata) {
2107
+ const ch = new $ZodCheck({ check: "meta" });
2108
+ ch._zod.onattach = [(inst) => {
2109
+ const existing = globalRegistry.get(inst) ?? {};
2110
+ globalRegistry.add(inst, {
2111
+ ...existing,
2112
+ ...metadata
2113
+ });
2114
+ }];
2115
+ ch._zod.check = () => {};
2116
+ return ch;
2117
+ }
2118
+
2119
+ //#endregion
2120
+ //#region node_modules/zod/v4/core/to-json-schema.js
2121
+ function initializeContext(params) {
2122
+ let target = params?.target ?? "draft-2020-12";
2123
+ if (target === "draft-4") target = "draft-04";
2124
+ if (target === "draft-7") target = "draft-07";
2125
+ return {
2126
+ processors: params.processors ?? {},
2127
+ metadataRegistry: params?.metadata ?? globalRegistry,
2128
+ target,
2129
+ unrepresentable: params?.unrepresentable ?? "throw",
2130
+ override: params?.override ?? (() => {}),
2131
+ io: params?.io ?? "output",
2132
+ counter: 0,
2133
+ seen: /* @__PURE__ */ new Map(),
2134
+ cycles: params?.cycles ?? "ref",
2135
+ reused: params?.reused ?? "inline",
2136
+ external: params?.external ?? void 0
2137
+ };
2138
+ }
2139
+ function process(schema, ctx, _params = {
2140
+ path: [],
2141
+ schemaPath: []
2142
+ }) {
2143
+ var _a$1;
2144
+ const def = schema._zod.def;
2145
+ const seen = ctx.seen.get(schema);
2146
+ if (seen) {
2147
+ seen.count++;
2148
+ if (_params.schemaPath.includes(schema)) seen.cycle = _params.path;
2149
+ return seen.schema;
2150
+ }
2151
+ const result = {
2152
+ schema: {},
2153
+ count: 1,
2154
+ cycle: void 0,
2155
+ path: _params.path
2156
+ };
2157
+ ctx.seen.set(schema, result);
2158
+ const overrideSchema = schema._zod.toJSONSchema?.();
2159
+ if (overrideSchema) result.schema = overrideSchema;
2160
+ else {
2161
+ const params = {
2162
+ ..._params,
2163
+ schemaPath: [..._params.schemaPath, schema],
2164
+ path: _params.path
2165
+ };
2166
+ const parent = schema._zod.parent;
2167
+ if (parent) {
2168
+ result.ref = parent;
2169
+ process(parent, ctx, params);
2170
+ ctx.seen.get(parent).isParent = true;
2171
+ } else if (schema._zod.processJSONSchema) schema._zod.processJSONSchema(ctx, result.schema, params);
2172
+ else {
2173
+ const _json = result.schema;
2174
+ const processor = ctx.processors[def.type];
2175
+ if (!processor) throw new Error(`[toJSONSchema]: Non-representable type encountered: ${def.type}`);
2176
+ processor(schema, ctx, _json, params);
2177
+ }
2178
+ }
2179
+ const meta$2 = ctx.metadataRegistry.get(schema);
2180
+ if (meta$2) Object.assign(result.schema, meta$2);
2181
+ if (ctx.io === "input" && isTransforming(schema)) {
2182
+ delete result.schema.examples;
2183
+ delete result.schema.default;
2184
+ }
2185
+ if (ctx.io === "input" && result.schema._prefault) (_a$1 = result.schema).default ?? (_a$1.default = result.schema._prefault);
2186
+ delete result.schema._prefault;
2187
+ return ctx.seen.get(schema).schema;
2188
+ }
2189
+ function extractDefs(ctx, schema) {
2190
+ const root = ctx.seen.get(schema);
2191
+ if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
2192
+ const makeURI = (entry) => {
2193
+ const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
2194
+ if (ctx.external) {
2195
+ const externalId = ctx.external.registry.get(entry[0])?.id;
2196
+ const uriGenerator = ctx.external.uri ?? ((id$1) => id$1);
2197
+ if (externalId) return { ref: uriGenerator(externalId) };
2198
+ const id = entry[1].defId ?? entry[1].schema.id ?? `schema${ctx.counter++}`;
2199
+ entry[1].defId = id;
2200
+ return {
2201
+ defId: id,
2202
+ ref: `${uriGenerator("__shared")}#/${defsSegment}/${id}`
2203
+ };
2204
+ }
2205
+ if (entry[1] === root) return { ref: "#" };
2206
+ const defUriPrefix = `#/${defsSegment}/`;
2207
+ const defId = entry[1].schema.id ?? `__schema${ctx.counter++}`;
2208
+ return {
2209
+ defId,
2210
+ ref: defUriPrefix + defId
2211
+ };
2212
+ };
2213
+ const extractToDef = (entry) => {
2214
+ if (entry[1].schema.$ref) return;
2215
+ const seen = entry[1];
2216
+ const { ref, defId } = makeURI(entry);
2217
+ seen.def = { ...seen.schema };
2218
+ if (defId) seen.defId = defId;
2219
+ const schema$1 = seen.schema;
2220
+ for (const key in schema$1) delete schema$1[key];
2221
+ schema$1.$ref = ref;
2222
+ };
2223
+ if (ctx.cycles === "throw") for (const entry of ctx.seen.entries()) {
2224
+ const seen = entry[1];
2225
+ if (seen.cycle) throw new Error(`Cycle detected: #/${seen.cycle?.join("/")}/<root>
2226
+
2227
+ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.`);
2228
+ }
2229
+ for (const entry of ctx.seen.entries()) {
2230
+ const seen = entry[1];
2231
+ if (schema === entry[0]) {
2232
+ extractToDef(entry);
2233
+ continue;
2234
+ }
2235
+ if (ctx.external) {
2236
+ const ext = ctx.external.registry.get(entry[0])?.id;
2237
+ if (schema !== entry[0] && ext) {
2238
+ extractToDef(entry);
2239
+ continue;
2240
+ }
2241
+ }
2242
+ if (ctx.metadataRegistry.get(entry[0])?.id) {
2243
+ extractToDef(entry);
2244
+ continue;
2245
+ }
2246
+ if (seen.cycle) {
2247
+ extractToDef(entry);
2248
+ continue;
2249
+ }
2250
+ if (seen.count > 1) {
2251
+ if (ctx.reused === "ref") {
2252
+ extractToDef(entry);
2253
+ continue;
2254
+ }
2255
+ }
2256
+ }
2257
+ }
2258
+ function finalize(ctx, schema) {
2259
+ const root = ctx.seen.get(schema);
2260
+ if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
2261
+ const flattenRef = (zodSchema) => {
2262
+ const seen = ctx.seen.get(zodSchema);
2263
+ const schema$1 = seen.def ?? seen.schema;
2264
+ const _cached = { ...schema$1 };
2265
+ if (seen.ref === null) return;
2266
+ const ref = seen.ref;
2267
+ seen.ref = null;
2268
+ if (ref) {
2269
+ flattenRef(ref);
2270
+ const refSchema = ctx.seen.get(ref).schema;
2271
+ if (refSchema.$ref && (ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0")) {
2272
+ schema$1.allOf = schema$1.allOf ?? [];
2273
+ schema$1.allOf.push(refSchema);
2274
+ } else {
2275
+ Object.assign(schema$1, refSchema);
2276
+ Object.assign(schema$1, _cached);
2277
+ }
2278
+ }
2279
+ if (!seen.isParent) ctx.override({
2280
+ zodSchema,
2281
+ jsonSchema: schema$1,
2282
+ path: seen.path ?? []
2283
+ });
2284
+ };
2285
+ for (const entry of [...ctx.seen.entries()].reverse()) flattenRef(entry[0]);
2286
+ const result = {};
2287
+ if (ctx.target === "draft-2020-12") result.$schema = "https://json-schema.org/draft/2020-12/schema";
2288
+ else if (ctx.target === "draft-07") result.$schema = "http://json-schema.org/draft-07/schema#";
2289
+ else if (ctx.target === "draft-04") result.$schema = "http://json-schema.org/draft-04/schema#";
2290
+ else if (ctx.target === "openapi-3.0") {}
2291
+ if (ctx.external?.uri) {
2292
+ const id = ctx.external.registry.get(schema)?.id;
2293
+ if (!id) throw new Error("Schema is missing an `id` property");
2294
+ result.$id = ctx.external.uri(id);
2295
+ }
2296
+ Object.assign(result, root.def ?? root.schema);
2297
+ const defs = ctx.external?.defs ?? {};
2298
+ for (const entry of ctx.seen.entries()) {
2299
+ const seen = entry[1];
2300
+ if (seen.def && seen.defId) defs[seen.defId] = seen.def;
2301
+ }
2302
+ if (ctx.external) {} else if (Object.keys(defs).length > 0) if (ctx.target === "draft-2020-12") result.$defs = defs;
2303
+ else result.definitions = defs;
2304
+ try {
2305
+ const finalized = JSON.parse(JSON.stringify(result));
2306
+ Object.defineProperty(finalized, "~standard", {
2307
+ value: {
2308
+ ...schema["~standard"],
2309
+ jsonSchema: {
2310
+ input: createStandardJSONSchemaMethod(schema, "input"),
2311
+ output: createStandardJSONSchemaMethod(schema, "output")
2312
+ }
2313
+ },
2314
+ enumerable: false,
2315
+ writable: false
2316
+ });
2317
+ return finalized;
2318
+ } catch (_err) {
2319
+ throw new Error("Error converting schema to JSON.");
2320
+ }
2321
+ }
2322
+ function isTransforming(_schema, _ctx) {
2323
+ const ctx = _ctx ?? { seen: /* @__PURE__ */ new Set() };
2324
+ if (ctx.seen.has(_schema)) return false;
2325
+ ctx.seen.add(_schema);
2326
+ const def = _schema._zod.def;
2327
+ if (def.type === "transform") return true;
2328
+ if (def.type === "array") return isTransforming(def.element, ctx);
2329
+ if (def.type === "set") return isTransforming(def.valueType, ctx);
2330
+ if (def.type === "lazy") return isTransforming(def.getter(), ctx);
2331
+ if (def.type === "promise" || def.type === "optional" || def.type === "nonoptional" || def.type === "nullable" || def.type === "readonly" || def.type === "default" || def.type === "prefault") return isTransforming(def.innerType, ctx);
2332
+ if (def.type === "intersection") return isTransforming(def.left, ctx) || isTransforming(def.right, ctx);
2333
+ if (def.type === "record" || def.type === "map") return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
2334
+ if (def.type === "pipe") return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
2335
+ if (def.type === "object") {
2336
+ for (const key in def.shape) if (isTransforming(def.shape[key], ctx)) return true;
2337
+ return false;
2338
+ }
2339
+ if (def.type === "union") {
2340
+ for (const option of def.options) if (isTransforming(option, ctx)) return true;
2341
+ return false;
2342
+ }
2343
+ if (def.type === "tuple") {
2344
+ for (const item of def.items) if (isTransforming(item, ctx)) return true;
2345
+ if (def.rest && isTransforming(def.rest, ctx)) return true;
2346
+ return false;
2347
+ }
2348
+ return false;
2349
+ }
2350
+ /**
2351
+ * Creates a toJSONSchema method for a schema instance.
2352
+ * This encapsulates the logic of initializing context, processing, extracting defs, and finalizing.
2353
+ */
2354
+ const createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
2355
+ const ctx = initializeContext({
2356
+ ...params,
2357
+ processors
2358
+ });
2359
+ process(schema, ctx);
2360
+ extractDefs(ctx, schema);
2361
+ return finalize(ctx, schema);
2362
+ };
2363
+ const createStandardJSONSchemaMethod = (schema, io) => (params) => {
2364
+ const { libraryOptions, target } = params ?? {};
2365
+ const ctx = initializeContext({
2366
+ ...libraryOptions ?? {},
2367
+ target,
2368
+ io,
2369
+ processors: {}
2370
+ });
2371
+ process(schema, ctx);
2372
+ extractDefs(ctx, schema);
2373
+ return finalize(ctx, schema);
2374
+ };
2375
+
2376
+ //#endregion
2377
+ //#region node_modules/zod/v4/core/json-schema-processors.js
2378
+ const neverProcessor = (_schema, _ctx, json, _params) => {
2379
+ json.not = {};
2380
+ };
2381
+ const unknownProcessor = (_schema, _ctx, _json, _params) => {};
2382
+ const enumProcessor = (schema, _ctx, json, _params) => {
2383
+ const def = schema._zod.def;
2384
+ const values = getEnumValues(def.entries);
2385
+ if (values.every((v) => typeof v === "number")) json.type = "number";
2386
+ if (values.every((v) => typeof v === "string")) json.type = "string";
2387
+ json.enum = values;
2388
+ };
2389
+ const customProcessor = (_schema, ctx, _json, _params) => {
2390
+ if (ctx.unrepresentable === "throw") throw new Error("Custom types cannot be represented in JSON Schema");
2391
+ };
2392
+ const transformProcessor = (_schema, ctx, _json, _params) => {
2393
+ if (ctx.unrepresentable === "throw") throw new Error("Transforms cannot be represented in JSON Schema");
2394
+ };
2395
+ const arrayProcessor = (schema, ctx, _json, params) => {
2396
+ const json = _json;
2397
+ const def = schema._zod.def;
2398
+ const { minimum, maximum } = schema._zod.bag;
2399
+ if (typeof minimum === "number") json.minItems = minimum;
2400
+ if (typeof maximum === "number") json.maxItems = maximum;
2401
+ json.type = "array";
2402
+ json.items = process(def.element, ctx, {
2403
+ ...params,
2404
+ path: [...params.path, "items"]
2405
+ });
2406
+ };
2407
+ const objectProcessor = (schema, ctx, _json, params) => {
2408
+ const json = _json;
2409
+ const def = schema._zod.def;
2410
+ json.type = "object";
2411
+ json.properties = {};
2412
+ const shape = def.shape;
2413
+ for (const key in shape) json.properties[key] = process(shape[key], ctx, {
2414
+ ...params,
2415
+ path: [
2416
+ ...params.path,
2417
+ "properties",
2418
+ key
2419
+ ]
2420
+ });
2421
+ const allKeys = new Set(Object.keys(shape));
2422
+ const requiredKeys = new Set([...allKeys].filter((key) => {
2423
+ const v = def.shape[key]._zod;
2424
+ if (ctx.io === "input") return v.optin === void 0;
2425
+ else return v.optout === void 0;
2426
+ }));
2427
+ if (requiredKeys.size > 0) json.required = Array.from(requiredKeys);
2428
+ if (def.catchall?._zod.def.type === "never") json.additionalProperties = false;
2429
+ else if (!def.catchall) {
2430
+ if (ctx.io === "output") json.additionalProperties = false;
2431
+ } else if (def.catchall) json.additionalProperties = process(def.catchall, ctx, {
2432
+ ...params,
2433
+ path: [...params.path, "additionalProperties"]
2434
+ });
2435
+ };
2436
+ const unionProcessor = (schema, ctx, json, params) => {
2437
+ const def = schema._zod.def;
2438
+ const isExclusive = def.inclusive === false;
2439
+ const options = def.options.map((x, i) => process(x, ctx, {
2440
+ ...params,
2441
+ path: [
2442
+ ...params.path,
2443
+ isExclusive ? "oneOf" : "anyOf",
2444
+ i
2445
+ ]
2446
+ }));
2447
+ if (isExclusive) json.oneOf = options;
2448
+ else json.anyOf = options;
2449
+ };
2450
+ const intersectionProcessor = (schema, ctx, json, params) => {
2451
+ const def = schema._zod.def;
2452
+ const a = process(def.left, ctx, {
2453
+ ...params,
2454
+ path: [
2455
+ ...params.path,
2456
+ "allOf",
2457
+ 0
2458
+ ]
2459
+ });
2460
+ const b = process(def.right, ctx, {
2461
+ ...params,
2462
+ path: [
2463
+ ...params.path,
2464
+ "allOf",
2465
+ 1
2466
+ ]
2467
+ });
2468
+ const isSimpleIntersection = (val) => "allOf" in val && Object.keys(val).length === 1;
2469
+ json.allOf = [...isSimpleIntersection(a) ? a.allOf : [a], ...isSimpleIntersection(b) ? b.allOf : [b]];
2470
+ };
2471
+ const nullableProcessor = (schema, ctx, json, params) => {
2472
+ const def = schema._zod.def;
2473
+ const inner = process(def.innerType, ctx, params);
2474
+ const seen = ctx.seen.get(schema);
2475
+ if (ctx.target === "openapi-3.0") {
2476
+ seen.ref = def.innerType;
2477
+ json.nullable = true;
2478
+ } else json.anyOf = [inner, { type: "null" }];
2479
+ };
2480
+ const nonoptionalProcessor = (schema, ctx, _json, params) => {
2481
+ const def = schema._zod.def;
2482
+ process(def.innerType, ctx, params);
2483
+ const seen = ctx.seen.get(schema);
2484
+ seen.ref = def.innerType;
2485
+ };
2486
+ const defaultProcessor = (schema, ctx, json, params) => {
2487
+ const def = schema._zod.def;
2488
+ process(def.innerType, ctx, params);
2489
+ const seen = ctx.seen.get(schema);
2490
+ seen.ref = def.innerType;
2491
+ json.default = JSON.parse(JSON.stringify(def.defaultValue));
2492
+ };
2493
+ const prefaultProcessor = (schema, ctx, json, params) => {
2494
+ const def = schema._zod.def;
2495
+ process(def.innerType, ctx, params);
2496
+ const seen = ctx.seen.get(schema);
2497
+ seen.ref = def.innerType;
2498
+ if (ctx.io === "input") json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
2499
+ };
2500
+ const catchProcessor = (schema, ctx, json, params) => {
2501
+ const def = schema._zod.def;
2502
+ process(def.innerType, ctx, params);
2503
+ const seen = ctx.seen.get(schema);
2504
+ seen.ref = def.innerType;
2505
+ let catchValue;
2506
+ try {
2507
+ catchValue = def.catchValue(void 0);
2508
+ } catch {
2509
+ throw new Error("Dynamic catch values are not supported in JSON Schema");
2510
+ }
2511
+ json.default = catchValue;
2512
+ };
2513
+ const pipeProcessor = (schema, ctx, _json, params) => {
2514
+ const def = schema._zod.def;
2515
+ const innerType = ctx.io === "input" ? def.in._zod.def.type === "transform" ? def.out : def.in : def.out;
2516
+ process(innerType, ctx, params);
2517
+ const seen = ctx.seen.get(schema);
2518
+ seen.ref = innerType;
2519
+ };
2520
+ const readonlyProcessor = (schema, ctx, json, params) => {
2521
+ const def = schema._zod.def;
2522
+ process(def.innerType, ctx, params);
2523
+ const seen = ctx.seen.get(schema);
2524
+ seen.ref = def.innerType;
2525
+ json.readOnly = true;
2526
+ };
2527
+ const optionalProcessor = (schema, ctx, _json, params) => {
2528
+ const def = schema._zod.def;
2529
+ process(def.innerType, ctx, params);
2530
+ const seen = ctx.seen.get(schema);
2531
+ seen.ref = def.innerType;
2532
+ };
2533
+
2534
+ //#endregion
2535
+ //#region node_modules/zod/v4/classic/errors.js
2536
+ const initializer = (inst, issues) => {
2537
+ $ZodError.init(inst, issues);
2538
+ inst.name = "ZodError";
2539
+ Object.defineProperties(inst, {
2540
+ format: { value: (mapper) => formatError(inst, mapper) },
2541
+ flatten: { value: (mapper) => flattenError(inst, mapper) },
2542
+ addIssue: { value: (issue$1) => {
2543
+ inst.issues.push(issue$1);
2544
+ inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
2545
+ } },
2546
+ addIssues: { value: (issues$1) => {
2547
+ inst.issues.push(...issues$1);
2548
+ inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
2549
+ } },
2550
+ isEmpty: { get() {
2551
+ return inst.issues.length === 0;
2552
+ } }
2553
+ });
2554
+ };
2555
+ const ZodError = $constructor("ZodError", initializer);
2556
+ const ZodRealError = $constructor("ZodError", initializer, { Parent: Error });
2557
+
2558
+ //#endregion
2559
+ //#region node_modules/zod/v4/classic/parse.js
2560
+ const parse = /* @__PURE__ */ _parse(ZodRealError);
2561
+ const parseAsync = /* @__PURE__ */ _parseAsync(ZodRealError);
2562
+ const safeParse = /* @__PURE__ */ _safeParse(ZodRealError);
2563
+ const safeParseAsync = /* @__PURE__ */ _safeParseAsync(ZodRealError);
2564
+ const encode = /* @__PURE__ */ _encode(ZodRealError);
2565
+ const decode = /* @__PURE__ */ _decode(ZodRealError);
2566
+ const encodeAsync = /* @__PURE__ */ _encodeAsync(ZodRealError);
2567
+ const decodeAsync = /* @__PURE__ */ _decodeAsync(ZodRealError);
2568
+ const safeEncode = /* @__PURE__ */ _safeEncode(ZodRealError);
2569
+ const safeDecode = /* @__PURE__ */ _safeDecode(ZodRealError);
2570
+ const safeEncodeAsync = /* @__PURE__ */ _safeEncodeAsync(ZodRealError);
2571
+ const safeDecodeAsync = /* @__PURE__ */ _safeDecodeAsync(ZodRealError);
2572
+
2573
+ //#endregion
2574
+ //#region node_modules/zod/v4/classic/schemas.js
2575
+ const ZodType = /* @__PURE__ */ $constructor("ZodType", (inst, def) => {
2576
+ $ZodType.init(inst, def);
2577
+ Object.assign(inst["~standard"], { jsonSchema: {
2578
+ input: createStandardJSONSchemaMethod(inst, "input"),
2579
+ output: createStandardJSONSchemaMethod(inst, "output")
2580
+ } });
2581
+ inst.toJSONSchema = createToJSONSchemaMethod(inst, {});
2582
+ inst.def = def;
2583
+ inst.type = def.type;
2584
+ Object.defineProperty(inst, "_def", { value: def });
2585
+ inst.check = (...checks) => {
2586
+ return inst.clone(mergeDefs(def, { checks: [...def.checks ?? [], ...checks.map((ch) => typeof ch === "function" ? { _zod: {
2587
+ check: ch,
2588
+ def: { check: "custom" },
2589
+ onattach: []
2590
+ } } : ch)] }));
2591
+ };
2592
+ inst.clone = (def$1, params) => clone(inst, def$1, params);
2593
+ inst.brand = () => inst;
2594
+ inst.register = ((reg, meta$2) => {
2595
+ reg.add(inst, meta$2);
2596
+ return inst;
2597
+ });
2598
+ inst.parse = (data, params) => parse(inst, data, params, { callee: inst.parse });
2599
+ inst.safeParse = (data, params) => safeParse(inst, data, params);
2600
+ inst.parseAsync = async (data, params) => parseAsync(inst, data, params, { callee: inst.parseAsync });
2601
+ inst.safeParseAsync = async (data, params) => safeParseAsync(inst, data, params);
2602
+ inst.spa = inst.safeParseAsync;
2603
+ inst.encode = (data, params) => encode(inst, data, params);
2604
+ inst.decode = (data, params) => decode(inst, data, params);
2605
+ inst.encodeAsync = async (data, params) => encodeAsync(inst, data, params);
2606
+ inst.decodeAsync = async (data, params) => decodeAsync(inst, data, params);
2607
+ inst.safeEncode = (data, params) => safeEncode(inst, data, params);
2608
+ inst.safeDecode = (data, params) => safeDecode(inst, data, params);
2609
+ inst.safeEncodeAsync = async (data, params) => safeEncodeAsync(inst, data, params);
2610
+ inst.safeDecodeAsync = async (data, params) => safeDecodeAsync(inst, data, params);
2611
+ inst.refine = (check, params) => inst.check(refine(check, params));
2612
+ inst.superRefine = (refinement) => inst.check(superRefine(refinement));
2613
+ inst.overwrite = (fn) => inst.check(_overwrite(fn));
2614
+ inst.optional = () => optional(inst);
2615
+ inst.nullable = () => nullable(inst);
2616
+ inst.nullish = () => optional(nullable(inst));
2617
+ inst.nonoptional = (params) => nonoptional(inst, params);
2618
+ inst.array = () => array(inst);
2619
+ inst.or = (arg) => union([inst, arg]);
2620
+ inst.and = (arg) => intersection(inst, arg);
2621
+ inst.transform = (tx) => pipe(inst, transform(tx));
2622
+ inst.default = (def$1) => _default(inst, def$1);
2623
+ inst.prefault = (def$1) => prefault(inst, def$1);
2624
+ inst.catch = (params) => _catch(inst, params);
2625
+ inst.pipe = (target) => pipe(inst, target);
2626
+ inst.readonly = () => readonly(inst);
2627
+ inst.describe = (description) => {
2628
+ const cl = inst.clone();
2629
+ globalRegistry.add(cl, { description });
2630
+ return cl;
2631
+ };
2632
+ Object.defineProperty(inst, "description", {
2633
+ get() {
2634
+ return globalRegistry.get(inst)?.description;
2635
+ },
2636
+ configurable: true
2637
+ });
2638
+ inst.meta = (...args) => {
2639
+ if (args.length === 0) return globalRegistry.get(inst);
2640
+ const cl = inst.clone();
2641
+ globalRegistry.add(cl, args[0]);
2642
+ return cl;
2643
+ };
2644
+ inst.isOptional = () => inst.safeParse(void 0).success;
2645
+ inst.isNullable = () => inst.safeParse(null).success;
2646
+ return inst;
2647
+ });
2648
+ const ZodUnknown = /* @__PURE__ */ $constructor("ZodUnknown", (inst, def) => {
2649
+ $ZodUnknown.init(inst, def);
2650
+ ZodType.init(inst, def);
2651
+ inst._zod.processJSONSchema = (ctx, json, params) => unknownProcessor(inst, ctx, json, params);
2652
+ });
2653
+ function unknown() {
2654
+ return _unknown(ZodUnknown);
2655
+ }
2656
+ const ZodNever = /* @__PURE__ */ $constructor("ZodNever", (inst, def) => {
2657
+ $ZodNever.init(inst, def);
2658
+ ZodType.init(inst, def);
2659
+ inst._zod.processJSONSchema = (ctx, json, params) => neverProcessor(inst, ctx, json, params);
2660
+ });
2661
+ function never(params) {
2662
+ return _never(ZodNever, params);
2663
+ }
2664
+ const ZodArray = /* @__PURE__ */ $constructor("ZodArray", (inst, def) => {
2665
+ $ZodArray.init(inst, def);
2666
+ ZodType.init(inst, def);
2667
+ inst._zod.processJSONSchema = (ctx, json, params) => arrayProcessor(inst, ctx, json, params);
2668
+ inst.element = def.element;
2669
+ inst.min = (minLength, params) => inst.check(_minLength(minLength, params));
2670
+ inst.nonempty = (params) => inst.check(_minLength(1, params));
2671
+ inst.max = (maxLength, params) => inst.check(_maxLength(maxLength, params));
2672
+ inst.length = (len, params) => inst.check(_length(len, params));
2673
+ inst.unwrap = () => inst.element;
2674
+ });
2675
+ function array(element, params) {
2676
+ return _array(ZodArray, element, params);
2677
+ }
2678
+ const ZodObject = /* @__PURE__ */ $constructor("ZodObject", (inst, def) => {
2679
+ $ZodObjectJIT.init(inst, def);
2680
+ ZodType.init(inst, def);
2681
+ inst._zod.processJSONSchema = (ctx, json, params) => objectProcessor(inst, ctx, json, params);
2682
+ defineLazy(inst, "shape", () => {
2683
+ return def.shape;
2684
+ });
2685
+ inst.keyof = () => _enum(Object.keys(inst._zod.def.shape));
2686
+ inst.catchall = (catchall) => inst.clone({
2687
+ ...inst._zod.def,
2688
+ catchall
2689
+ });
2690
+ inst.passthrough = () => inst.clone({
2691
+ ...inst._zod.def,
2692
+ catchall: unknown()
2693
+ });
2694
+ inst.loose = () => inst.clone({
2695
+ ...inst._zod.def,
2696
+ catchall: unknown()
2697
+ });
2698
+ inst.strict = () => inst.clone({
2699
+ ...inst._zod.def,
2700
+ catchall: never()
2701
+ });
2702
+ inst.strip = () => inst.clone({
2703
+ ...inst._zod.def,
2704
+ catchall: void 0
2705
+ });
2706
+ inst.extend = (incoming) => {
2707
+ return extend(inst, incoming);
2708
+ };
2709
+ inst.safeExtend = (incoming) => {
2710
+ return safeExtend(inst, incoming);
2711
+ };
2712
+ inst.merge = (other) => merge(inst, other);
2713
+ inst.pick = (mask) => pick(inst, mask);
2714
+ inst.omit = (mask) => omit(inst, mask);
2715
+ inst.partial = (...args) => partial(ZodOptional, inst, args[0]);
2716
+ inst.required = (...args) => required(ZodNonOptional, inst, args[0]);
2717
+ });
2718
+ const ZodUnion = /* @__PURE__ */ $constructor("ZodUnion", (inst, def) => {
2719
+ $ZodUnion.init(inst, def);
2720
+ ZodType.init(inst, def);
2721
+ inst._zod.processJSONSchema = (ctx, json, params) => unionProcessor(inst, ctx, json, params);
2722
+ inst.options = def.options;
2723
+ });
2724
+ function union(options, params) {
2725
+ return new ZodUnion({
2726
+ type: "union",
2727
+ options,
2728
+ ...normalizeParams(params)
2729
+ });
2730
+ }
2731
+ const ZodIntersection = /* @__PURE__ */ $constructor("ZodIntersection", (inst, def) => {
2732
+ $ZodIntersection.init(inst, def);
2733
+ ZodType.init(inst, def);
2734
+ inst._zod.processJSONSchema = (ctx, json, params) => intersectionProcessor(inst, ctx, json, params);
2735
+ });
2736
+ function intersection(left, right) {
2737
+ return new ZodIntersection({
2738
+ type: "intersection",
2739
+ left,
2740
+ right
2741
+ });
2742
+ }
2743
+ const ZodEnum = /* @__PURE__ */ $constructor("ZodEnum", (inst, def) => {
2744
+ $ZodEnum.init(inst, def);
2745
+ ZodType.init(inst, def);
2746
+ inst._zod.processJSONSchema = (ctx, json, params) => enumProcessor(inst, ctx, json, params);
2747
+ inst.enum = def.entries;
2748
+ inst.options = Object.values(def.entries);
2749
+ const keys = new Set(Object.keys(def.entries));
2750
+ inst.extract = (values, params) => {
2751
+ const newEntries = {};
2752
+ for (const value of values) if (keys.has(value)) newEntries[value] = def.entries[value];
2753
+ else throw new Error(`Key ${value} not found in enum`);
2754
+ return new ZodEnum({
2755
+ ...def,
2756
+ checks: [],
2757
+ ...normalizeParams(params),
2758
+ entries: newEntries
2759
+ });
2760
+ };
2761
+ inst.exclude = (values, params) => {
2762
+ const newEntries = { ...def.entries };
2763
+ for (const value of values) if (keys.has(value)) delete newEntries[value];
2764
+ else throw new Error(`Key ${value} not found in enum`);
2765
+ return new ZodEnum({
2766
+ ...def,
2767
+ checks: [],
2768
+ ...normalizeParams(params),
2769
+ entries: newEntries
2770
+ });
2771
+ };
2772
+ });
2773
+ function _enum(values, params) {
2774
+ return new ZodEnum({
2775
+ type: "enum",
2776
+ entries: Array.isArray(values) ? Object.fromEntries(values.map((v) => [v, v])) : values,
2777
+ ...normalizeParams(params)
2778
+ });
2779
+ }
2780
+ const ZodTransform = /* @__PURE__ */ $constructor("ZodTransform", (inst, def) => {
2781
+ $ZodTransform.init(inst, def);
2782
+ ZodType.init(inst, def);
2783
+ inst._zod.processJSONSchema = (ctx, json, params) => transformProcessor(inst, ctx, json, params);
2784
+ inst._zod.parse = (payload, _ctx) => {
2785
+ if (_ctx.direction === "backward") throw new $ZodEncodeError(inst.constructor.name);
2786
+ payload.addIssue = (issue$1) => {
2787
+ if (typeof issue$1 === "string") payload.issues.push(issue(issue$1, payload.value, def));
2788
+ else {
2789
+ const _issue = issue$1;
2790
+ if (_issue.fatal) _issue.continue = false;
2791
+ _issue.code ?? (_issue.code = "custom");
2792
+ _issue.input ?? (_issue.input = payload.value);
2793
+ _issue.inst ?? (_issue.inst = inst);
2794
+ payload.issues.push(issue(_issue));
2795
+ }
2796
+ };
2797
+ const output = def.transform(payload.value, payload);
2798
+ if (output instanceof Promise) return output.then((output$1) => {
2799
+ payload.value = output$1;
2800
+ return payload;
2801
+ });
2802
+ payload.value = output;
2803
+ return payload;
2804
+ };
2805
+ });
2806
+ function transform(fn) {
2807
+ return new ZodTransform({
2808
+ type: "transform",
2809
+ transform: fn
2810
+ });
2811
+ }
2812
+ const ZodOptional = /* @__PURE__ */ $constructor("ZodOptional", (inst, def) => {
2813
+ $ZodOptional.init(inst, def);
2814
+ ZodType.init(inst, def);
2815
+ inst._zod.processJSONSchema = (ctx, json, params) => optionalProcessor(inst, ctx, json, params);
2816
+ inst.unwrap = () => inst._zod.def.innerType;
2817
+ });
2818
+ function optional(innerType) {
2819
+ return new ZodOptional({
2820
+ type: "optional",
2821
+ innerType
2822
+ });
2823
+ }
2824
+ const ZodNullable = /* @__PURE__ */ $constructor("ZodNullable", (inst, def) => {
2825
+ $ZodNullable.init(inst, def);
2826
+ ZodType.init(inst, def);
2827
+ inst._zod.processJSONSchema = (ctx, json, params) => nullableProcessor(inst, ctx, json, params);
2828
+ inst.unwrap = () => inst._zod.def.innerType;
2829
+ });
2830
+ function nullable(innerType) {
2831
+ return new ZodNullable({
2832
+ type: "nullable",
2833
+ innerType
2834
+ });
2835
+ }
2836
+ const ZodDefault = /* @__PURE__ */ $constructor("ZodDefault", (inst, def) => {
2837
+ $ZodDefault.init(inst, def);
2838
+ ZodType.init(inst, def);
2839
+ inst._zod.processJSONSchema = (ctx, json, params) => defaultProcessor(inst, ctx, json, params);
2840
+ inst.unwrap = () => inst._zod.def.innerType;
2841
+ inst.removeDefault = inst.unwrap;
2842
+ });
2843
+ function _default(innerType, defaultValue) {
2844
+ return new ZodDefault({
2845
+ type: "default",
2846
+ innerType,
2847
+ get defaultValue() {
2848
+ return typeof defaultValue === "function" ? defaultValue() : shallowClone(defaultValue);
2849
+ }
2850
+ });
2851
+ }
2852
+ const ZodPrefault = /* @__PURE__ */ $constructor("ZodPrefault", (inst, def) => {
2853
+ $ZodPrefault.init(inst, def);
2854
+ ZodType.init(inst, def);
2855
+ inst._zod.processJSONSchema = (ctx, json, params) => prefaultProcessor(inst, ctx, json, params);
2856
+ inst.unwrap = () => inst._zod.def.innerType;
2857
+ });
2858
+ function prefault(innerType, defaultValue) {
2859
+ return new ZodPrefault({
2860
+ type: "prefault",
2861
+ innerType,
2862
+ get defaultValue() {
2863
+ return typeof defaultValue === "function" ? defaultValue() : shallowClone(defaultValue);
2864
+ }
2865
+ });
2866
+ }
2867
+ const ZodNonOptional = /* @__PURE__ */ $constructor("ZodNonOptional", (inst, def) => {
2868
+ $ZodNonOptional.init(inst, def);
2869
+ ZodType.init(inst, def);
2870
+ inst._zod.processJSONSchema = (ctx, json, params) => nonoptionalProcessor(inst, ctx, json, params);
2871
+ inst.unwrap = () => inst._zod.def.innerType;
2872
+ });
2873
+ function nonoptional(innerType, params) {
2874
+ return new ZodNonOptional({
2875
+ type: "nonoptional",
2876
+ innerType,
2877
+ ...normalizeParams(params)
2878
+ });
2879
+ }
2880
+ const ZodCatch = /* @__PURE__ */ $constructor("ZodCatch", (inst, def) => {
2881
+ $ZodCatch.init(inst, def);
2882
+ ZodType.init(inst, def);
2883
+ inst._zod.processJSONSchema = (ctx, json, params) => catchProcessor(inst, ctx, json, params);
2884
+ inst.unwrap = () => inst._zod.def.innerType;
2885
+ inst.removeCatch = inst.unwrap;
2886
+ });
2887
+ function _catch(innerType, catchValue) {
2888
+ return new ZodCatch({
2889
+ type: "catch",
2890
+ innerType,
2891
+ catchValue: typeof catchValue === "function" ? catchValue : () => catchValue
2892
+ });
2893
+ }
2894
+ const ZodPipe = /* @__PURE__ */ $constructor("ZodPipe", (inst, def) => {
2895
+ $ZodPipe.init(inst, def);
2896
+ ZodType.init(inst, def);
2897
+ inst._zod.processJSONSchema = (ctx, json, params) => pipeProcessor(inst, ctx, json, params);
2898
+ inst.in = def.in;
2899
+ inst.out = def.out;
2900
+ });
2901
+ function pipe(in_, out) {
2902
+ return new ZodPipe({
2903
+ type: "pipe",
2904
+ in: in_,
2905
+ out
2906
+ });
2907
+ }
2908
+ const ZodReadonly = /* @__PURE__ */ $constructor("ZodReadonly", (inst, def) => {
2909
+ $ZodReadonly.init(inst, def);
2910
+ ZodType.init(inst, def);
2911
+ inst._zod.processJSONSchema = (ctx, json, params) => readonlyProcessor(inst, ctx, json, params);
2912
+ inst.unwrap = () => inst._zod.def.innerType;
2913
+ });
2914
+ function readonly(innerType) {
2915
+ return new ZodReadonly({
2916
+ type: "readonly",
2917
+ innerType
2918
+ });
2919
+ }
2920
+ const ZodCustom = /* @__PURE__ */ $constructor("ZodCustom", (inst, def) => {
2921
+ $ZodCustom.init(inst, def);
2922
+ ZodType.init(inst, def);
2923
+ inst._zod.processJSONSchema = (ctx, json, params) => customProcessor(inst, ctx, json, params);
2924
+ });
2925
+ function custom(fn, _params) {
2926
+ return _custom(ZodCustom, fn ?? (() => true), _params);
2927
+ }
2928
+ function refine(fn, _params = {}) {
2929
+ return _refine(ZodCustom, fn, _params);
2930
+ }
2931
+ function superRefine(fn) {
2932
+ return _superRefine(fn);
2933
+ }
2934
+ const describe = describe$1;
2935
+ const meta = meta$1;
2936
+
2937
+ //#endregion
2938
+ //#region src/client/prose-schema.ts
2939
+ const PROSE_MARKER = Symbol.for("replicate:prose");
2940
+ function createProseSchema() {
2941
+ const schema = custom((val) => {
2942
+ if (val == null) return true;
2943
+ if (typeof val !== "object") return false;
2944
+ return val.type === "doc";
2945
+ }, { message: "Expected prose document with type \"doc\"" });
2946
+ Object.defineProperty(schema, PROSE_MARKER, {
2947
+ value: true,
2948
+ writable: false
2949
+ });
2950
+ return schema;
2951
+ }
2952
+ function emptyProse() {
2953
+ return {
2954
+ type: "doc",
2955
+ content: []
2956
+ };
2957
+ }
2958
+ function prose$1() {
2959
+ return createProseSchema();
2960
+ }
2961
+ prose$1.empty = emptyProse;
2962
+ function isProseSchema(schema) {
2963
+ return schema != null && typeof schema === "object" && PROSE_MARKER in schema && schema[PROSE_MARKER] === true;
2964
+ }
2965
+ function extractProseFields(schema) {
2966
+ const fields = [];
2967
+ for (const [key, fieldSchema] of Object.entries(schema.shape)) {
2968
+ let unwrapped = fieldSchema;
2969
+ while (unwrapped instanceof ZodOptional || unwrapped instanceof ZodNullable) unwrapped = unwrapped.unwrap();
2970
+ if (isProseSchema(unwrapped)) fields.push(key);
2971
+ }
2972
+ return fields;
2973
+ }
2974
+
2975
+ //#endregion
2976
+ //#region src/client/collection.ts
2977
+ /** Origin markers for Yjs transactions */
2978
+ var YjsOrigin = /* @__PURE__ */ function(YjsOrigin$1) {
2979
+ YjsOrigin$1["Local"] = "local";
2980
+ YjsOrigin$1["Fragment"] = "fragment";
2981
+ YjsOrigin$1["Server"] = "server";
2982
+ return YjsOrigin$1;
2983
+ }(YjsOrigin || {});
2984
+ const logger = getLogger$1(["replicate", "collection"]);
2985
+ function handleMutationError(error, operation, collection$1) {
2986
+ const httpError = error;
2987
+ logger.error(`${operation} failed`, {
2988
+ collection: collection$1,
2989
+ error: httpError?.message,
2990
+ status: httpError?.status
2991
+ });
2992
+ if (httpError?.status === 401 || httpError?.status === 403) throw new NonRetriableError("Authentication failed");
2993
+ if (httpError?.status === 422) throw new NonRetriableError("Validation error");
2994
+ throw error;
2995
+ }
2996
+ const cleanupFunctions = /* @__PURE__ */ new Map();
2997
+ const collectionDocs = /* @__PURE__ */ new Map();
2998
+ const collectionUndoConfig = /* @__PURE__ */ new Map();
2999
+ const DEFAULT_UNDO_CAPTURE_TIMEOUT = 500;
3000
+ const DEFAULT_DEBOUNCE_MS = 1e3;
3001
+ const collectionMutex = /* @__PURE__ */ new Map();
3002
+ const fragmentUndoManagers = /* @__PURE__ */ new Map();
3003
+ const debounceConfig = /* @__PURE__ */ new Map();
3004
+ const collectionRefs = /* @__PURE__ */ new Map();
3005
+ const serverStateVectors = /* @__PURE__ */ new Map();
3006
+ /**
3007
+ * Get or create mutex for a collection.
3008
+ */
3009
+ function getOrCreateMutex(collection$1) {
3010
+ let mux = collectionMutex.get(collection$1);
3011
+ if (!mux) {
3012
+ mux = createMutex();
3013
+ collectionMutex.set(collection$1, mux);
3014
+ }
3015
+ return mux;
3016
+ }
3017
+ /**
3018
+ * Get or create an UndoManager scoped to a fragment field.
3019
+ * This tracks only content edits, not document-level changes like title.
3020
+ */
3021
+ function getOrCreateFragmentUndoManager(collection$1, documentId, field, fragment) {
3022
+ const key = `${collection$1}:${documentId}:${field}`;
3023
+ let um = fragmentUndoManagers.get(key);
3024
+ if (um) return um;
3025
+ const config$1 = collectionUndoConfig.get(collection$1);
3026
+ um = new Y.UndoManager([fragment], {
3027
+ captureTimeout: config$1?.captureTimeout ?? DEFAULT_UNDO_CAPTURE_TIMEOUT,
3028
+ trackedOrigins: new Set([YjsOrigin.Fragment])
3029
+ });
3030
+ fragmentUndoManagers.set(key, um);
3031
+ return um;
3032
+ }
3033
+ function convexCollectionOptions(config$1) {
3034
+ const { schema, getKey, material, convexClient, api, undoCaptureTimeout = 500, persistence: persistence$1 } = config$1;
3035
+ const collection$1 = getFunctionName(api.stream).split(":")[0];
3036
+ if (!collection$1) throw new Error("Could not extract collection name from api.stream function reference");
3037
+ const proseFields = schema && schema instanceof ZodObject ? extractProseFields(schema) : [];
3038
+ const proseFieldSet = new Set(proseFields);
3039
+ const utils = { async prose(documentId, field) {
3040
+ const fieldStr = field;
3041
+ if (!proseFieldSet.has(fieldStr)) throw new ProseError({
3042
+ documentId,
3043
+ field: fieldStr,
3044
+ collection: collection$1
3045
+ });
3046
+ let docs = collectionDocs.get(collection$1);
3047
+ if (!docs) {
3048
+ await new Promise((resolve, reject) => {
3049
+ const maxWait = 1e4;
3050
+ const startTime = Date.now();
3051
+ const check = setInterval(() => {
3052
+ if (collectionDocs.has(collection$1)) {
3053
+ clearInterval(check);
3054
+ resolve();
3055
+ } else if (Date.now() - startTime > maxWait) {
3056
+ clearInterval(check);
3057
+ reject(new ProseError({
3058
+ documentId,
3059
+ field: fieldStr,
3060
+ collection: collection$1
3061
+ }));
3062
+ }
3063
+ }, 10);
3064
+ });
3065
+ docs = collectionDocs.get(collection$1);
3066
+ }
3067
+ if (!docs) throw new ProseError({
3068
+ documentId,
3069
+ field: fieldStr,
3070
+ collection: collection$1
3071
+ });
3072
+ const fragment = getFragmentFromYMap(docs.ymap, documentId, fieldStr);
3073
+ if (!fragment) throw new ProseError({
3074
+ documentId,
3075
+ field: fieldStr,
3076
+ collection: collection$1
3077
+ });
3078
+ const collectionRef = collectionRefs.get(collection$1);
3079
+ if (collectionRef) observeFragment({
3080
+ collection: collection$1,
3081
+ documentId,
3082
+ field: fieldStr,
3083
+ fragment,
3084
+ ydoc: docs.ydoc,
3085
+ ymap: docs.ymap,
3086
+ collectionRef,
3087
+ debounceMs: debounceConfig.get(collection$1) ?? DEFAULT_DEBOUNCE_MS
3088
+ });
3089
+ const undoManager = getOrCreateFragmentUndoManager(collection$1, documentId, fieldStr, fragment);
3090
+ return {
3091
+ fragment,
3092
+ provider: { awareness: null },
3093
+ get pending() {
3094
+ return isPending(collection$1, documentId);
3095
+ },
3096
+ onPendingChange(callback) {
3097
+ return subscribePending(collection$1, documentId, callback);
3098
+ },
3099
+ undo() {
3100
+ undoManager.undo();
3101
+ },
3102
+ redo() {
3103
+ undoManager.redo();
3104
+ },
3105
+ canUndo() {
3106
+ return undoManager.canUndo();
3107
+ },
3108
+ canRedo() {
3109
+ return undoManager.canRedo();
3110
+ }
3111
+ };
3112
+ } };
3113
+ const ydoc = new Y.Doc({ guid: collection$1 });
3114
+ const ymap = ydoc.getMap(collection$1);
3115
+ let docPersistence = null;
3116
+ collectionDocs.set(collection$1, {
3117
+ ydoc,
3118
+ ymap
3119
+ });
3120
+ let ops = null;
3121
+ const cursorLayer = createCursorLayer(persistence$1.kv);
3122
+ let resolvePersistenceReady;
3123
+ const persistenceReadyPromise = new Promise((resolve) => {
3124
+ resolvePersistenceReady = resolve;
3125
+ });
3126
+ let resolveOptimisticReady;
3127
+ const optimisticReadyPromise = new Promise((resolve) => {
3128
+ resolveOptimisticReady = resolve;
3129
+ });
3130
+ const recover = async () => {
3131
+ if (!api.recovery) {
3132
+ logger.debug("No recovery API configured", { collection: collection$1 });
3133
+ return 0;
3134
+ }
3135
+ try {
3136
+ const localStateVector = Y.encodeStateVector(ydoc);
3137
+ logger.debug("Starting recovery", {
3138
+ collection: collection$1,
3139
+ localVectorSize: localStateVector.byteLength
3140
+ });
3141
+ const response = await convexClient.query(api.recovery, { clientStateVector: localStateVector.buffer });
3142
+ if (response.diff) {
3143
+ getOrCreateMutex(collection$1)(() => {
3144
+ applyUpdate(ydoc, new Uint8Array(response.diff), YjsOrigin.Server);
3145
+ });
3146
+ logger.info("Recovery applied diff", {
3147
+ collection: collection$1,
3148
+ diffSize: response.diff.byteLength
3149
+ });
3150
+ }
3151
+ if (response.serverStateVector) serverStateVectors.set(collection$1, new Uint8Array(response.serverStateVector));
3152
+ const cursor = response.cursor ?? 0;
3153
+ await persistence$1.kv.set(`cursor:${collection$1}`, cursor);
3154
+ logger.info("Recovery complete", {
3155
+ collection: collection$1,
3156
+ cursor
3157
+ });
3158
+ return cursor;
3159
+ } catch (error) {
3160
+ logger.error("Recovery failed", {
3161
+ collection: collection$1,
3162
+ error: String(error)
3163
+ });
3164
+ return 0;
3165
+ }
3166
+ };
3167
+ const applyYjsInsert = (mutations) => {
3168
+ const { delta } = transactWithDelta(ydoc, () => {
3169
+ mutations.forEach((mut) => {
3170
+ const itemYMap = new Y.Map();
3171
+ ymap.set(String(mut.key), itemYMap);
3172
+ Object.entries(mut.modified).forEach(([k, v]) => {
3173
+ if (proseFieldSet.has(k) && isDoc(v)) {
3174
+ const fragment = new Y.XmlFragment();
3175
+ itemYMap.set(k, fragment);
3176
+ fragmentFromJSON(fragment, v);
3177
+ } else itemYMap.set(k, v);
3178
+ });
3179
+ });
3180
+ }, YjsOrigin.Local);
3181
+ return delta;
3182
+ };
3183
+ const applyYjsUpdate = (mutations) => {
3184
+ const { delta } = transactWithDelta(ydoc, () => {
3185
+ mutations.forEach((mut) => {
3186
+ const itemYMap = ymap.get(String(mut.key));
3187
+ if (itemYMap) {
3188
+ const modifiedFields = mut.modified;
3189
+ if (!modifiedFields) {
3190
+ logger.warn("mut.modified is null/undefined", {
3191
+ collection: collection$1,
3192
+ key: String(mut.key)
3193
+ });
3194
+ return;
3195
+ }
3196
+ Object.entries(modifiedFields).forEach(([k, v]) => {
3197
+ const existingValue = itemYMap.get(k);
3198
+ if (proseFieldSet.has(k)) {
3199
+ logger.debug("Skipping prose field in applyYjsUpdate", { field: k });
3200
+ return;
3201
+ }
3202
+ if (existingValue instanceof Y.XmlFragment) {
3203
+ logger.debug("Preserving live fragment field", { field: k });
3204
+ return;
3205
+ }
3206
+ itemYMap.set(k, v);
3207
+ });
3208
+ } else logger.error("Update attempted on non-existent item", {
3209
+ collection: collection$1,
3210
+ key: String(mut.key)
3211
+ });
3212
+ });
3213
+ }, YjsOrigin.Local);
3214
+ return delta;
3215
+ };
3216
+ const applyYjsDelete = (mutations) => {
3217
+ const { delta } = transactWithDelta(ydoc, () => {
3218
+ mutations.forEach((mut) => {
3219
+ ymap.delete(String(mut.key));
3220
+ });
3221
+ }, YjsOrigin.Local);
3222
+ return delta;
3223
+ };
3224
+ return {
3225
+ id: collection$1,
3226
+ getKey,
3227
+ schema,
3228
+ utils,
3229
+ onInsert: async ({ transaction }) => {
3230
+ const delta = applyYjsInsert(transaction.mutations);
3231
+ try {
3232
+ await Promise.all([persistenceReadyPromise, optimisticReadyPromise]);
3233
+ if (delta.length > 0) {
3234
+ const documentKey = String(transaction.mutations[0].key);
3235
+ const itemYMap = ymap.get(documentKey);
3236
+ const materializedDoc = itemYMap ? serializeYMapValue(itemYMap) : transaction.mutations[0].modified;
3237
+ await convexClient.mutation(api.insert, {
3238
+ documentId: documentKey,
3239
+ crdtBytes: delta.slice().buffer,
3240
+ materializedDoc
3241
+ });
3242
+ }
3243
+ } catch (error) {
3244
+ handleMutationError(error, "Insert", collection$1);
3245
+ }
3246
+ },
3247
+ onUpdate: async ({ transaction }) => {
3248
+ const mutation = transaction.mutations[0];
3249
+ const documentKey = String(mutation.key);
3250
+ if (isApplyingFromServer(collection$1, documentKey)) {
3251
+ logger.debug("Skipping onUpdate - data from server", {
3252
+ collection: collection$1,
3253
+ documentKey
3254
+ });
3255
+ return;
3256
+ }
3257
+ const metadata = mutation.metadata;
3258
+ const isContentSync = !!metadata?.contentSync;
3259
+ const delta = isContentSync ? null : applyYjsUpdate(transaction.mutations);
3260
+ try {
3261
+ await Promise.all([persistenceReadyPromise, optimisticReadyPromise]);
3262
+ if (isContentSync && metadata?.contentSync) {
3263
+ const { crdtBytes, materializedDoc } = metadata.contentSync;
3264
+ await convexClient.mutation(api.update, {
3265
+ documentId: documentKey,
3266
+ crdtBytes,
3267
+ materializedDoc
3268
+ });
3269
+ return;
3270
+ }
3271
+ if (delta && delta.length > 0) {
3272
+ const itemYMap = ymap.get(documentKey);
3273
+ const fullDoc = itemYMap ? serializeYMapValue(itemYMap) : mutation.modified;
3274
+ await convexClient.mutation(api.update, {
3275
+ documentId: documentKey,
3276
+ crdtBytes: delta.slice().buffer,
3277
+ materializedDoc: fullDoc
3278
+ });
3279
+ }
3280
+ } catch (error) {
3281
+ handleMutationError(error, "Update", collection$1);
3282
+ }
3283
+ },
3284
+ onDelete: async ({ transaction }) => {
3285
+ const delta = applyYjsDelete(transaction.mutations);
3286
+ try {
3287
+ await Promise.all([persistenceReadyPromise, optimisticReadyPromise]);
3288
+ const itemsToDelete = transaction.mutations.map((mut) => mut.original).filter((item) => item !== void 0 && Object.keys(item).length > 0);
3289
+ ops.delete(itemsToDelete);
3290
+ if (delta.length > 0) {
3291
+ const documentKey = String(transaction.mutations[0].key);
3292
+ await convexClient.mutation(api.remove, {
3293
+ documentId: documentKey,
3294
+ crdtBytes: delta.slice().buffer
3295
+ });
3296
+ }
3297
+ } catch (error) {
3298
+ handleMutationError(error, "Delete", collection$1);
3299
+ }
3300
+ },
3301
+ sync: {
3302
+ rowUpdateMode: "partial",
3303
+ sync: (params) => {
3304
+ const { markReady, collection: collectionInstance } = params;
3305
+ collectionRefs.set(collection$1, collectionInstance);
3306
+ const existingCleanup = cleanupFunctions.get(collection$1);
3307
+ if (existingCleanup) {
3308
+ existingCleanup();
3309
+ cleanupFunctions.delete(collection$1);
3310
+ }
3311
+ let subscription = null;
3312
+ const ssrDocuments = material?.documents;
3313
+ const ssrCursor = material?.cursor;
3314
+ const ssrCRDTBytes = material?.crdtBytes;
3315
+ const docs = ssrDocuments ? [...ssrDocuments] : [];
3316
+ (async () => {
3317
+ try {
3318
+ const trackedOrigins = new Set([YjsOrigin.Local]);
3319
+ collectionUndoConfig.set(collection$1, {
3320
+ captureTimeout: undoCaptureTimeout,
3321
+ trackedOrigins
3322
+ });
3323
+ docPersistence = persistence$1.createDocPersistence(collection$1, ydoc);
3324
+ docPersistence.whenSynced.then(() => {
3325
+ logger.debug("Persistence synced", { collection: collection$1 });
3326
+ resolvePersistenceReady?.();
3327
+ });
3328
+ await persistenceReadyPromise;
3329
+ logger.info("Persistence ready", {
3330
+ collection: collection$1,
3331
+ ymapSize: ymap.size
3332
+ });
3333
+ ops = createReplicateOps(params);
3334
+ resolveOptimisticReady?.();
3335
+ if (ssrCRDTBytes) applyUpdate(ydoc, new Uint8Array(ssrCRDTBytes), YjsOrigin.Server);
3336
+ const recoveryCursor = await recover();
3337
+ if (ymap.size > 0) {
3338
+ const items = extractItems(ymap);
3339
+ ops.replace(items);
3340
+ logger.info("Data loaded to TanStack DB", {
3341
+ collection: collection$1,
3342
+ itemCount: items.length
3343
+ });
3344
+ } else {
3345
+ ops.replace([]);
3346
+ logger.info("No data, cleared TanStack DB", { collection: collection$1 });
3347
+ }
3348
+ markReady();
3349
+ logger.info("Collection ready", {
3350
+ collection: collection$1,
3351
+ ymapSize: ymap.size
3352
+ });
3353
+ const peerId = await Effect.runPromise(Effect.gen(function* () {
3354
+ return yield* (yield* CursorService).loadPeerId(collection$1);
3355
+ }).pipe(Effect.provide(cursorLayer)));
3356
+ const cursor = ssrCursor ?? recoveryCursor;
3357
+ logger.info("Starting subscription", {
3358
+ collection: collection$1,
3359
+ cursor,
3360
+ peerId,
3361
+ source: ssrCursor !== void 0 ? "SSR" : "recovery"
3362
+ });
3363
+ const mux = getOrCreateMutex(collection$1);
3364
+ const handleSnapshotChange = (crdtBytes) => {
3365
+ cancelAllPending(collection$1);
3366
+ mux(() => {
3367
+ try {
3368
+ logger.debug("Applying snapshot", {
3369
+ collection: collection$1,
3370
+ bytesLength: crdtBytes.byteLength
3371
+ });
3372
+ applyUpdate(ydoc, new Uint8Array(crdtBytes), YjsOrigin.Server);
3373
+ const items = extractItems(ymap);
3374
+ logger.debug("Snapshot applied", {
3375
+ collection: collection$1,
3376
+ itemCount: items.length
3377
+ });
3378
+ ops.replace(items);
3379
+ } catch (error) {
3380
+ logger.error("Error applying snapshot", {
3381
+ collection: collection$1,
3382
+ error: String(error)
3383
+ });
3384
+ throw new Error(`Snapshot application failed: ${error}`);
3385
+ }
3386
+ });
3387
+ };
3388
+ const handleDeltaChange = (crdtBytes, documentId) => {
3389
+ if (documentId) {
3390
+ cancelPending(collection$1, documentId);
3391
+ setApplyingFromServer(collection$1, documentId, true);
3392
+ }
3393
+ mux(() => {
3394
+ try {
3395
+ logger.debug("Applying delta", {
3396
+ collection: collection$1,
3397
+ documentId,
3398
+ bytesLength: crdtBytes.byteLength
3399
+ });
3400
+ const itemBefore = documentId ? extractItem(ymap, documentId) : null;
3401
+ applyUpdate(ydoc, new Uint8Array(crdtBytes), YjsOrigin.Server);
3402
+ if (!documentId) {
3403
+ logger.debug("Delta applied (no documentId)", { collection: collection$1 });
3404
+ return;
3405
+ }
3406
+ const itemAfter = extractItem(ymap, documentId);
3407
+ if (itemAfter) {
3408
+ logger.debug("Upserting item after delta", {
3409
+ collection: collection$1,
3410
+ documentId
3411
+ });
3412
+ ops.upsert([itemAfter]);
3413
+ } else if (itemBefore) {
3414
+ logger.debug("Deleting item after delta", {
3415
+ collection: collection$1,
3416
+ documentId
3417
+ });
3418
+ ops.delete([itemBefore]);
3419
+ } else logger.debug("No change detected after delta", {
3420
+ collection: collection$1,
3421
+ documentId
3422
+ });
3423
+ } catch (error) {
3424
+ logger.error("Error applying delta", {
3425
+ collection: collection$1,
3426
+ documentId,
3427
+ error: String(error)
3428
+ });
3429
+ throw new Error(`Delta application failed for ${documentId}: ${error}`);
3430
+ } finally {
3431
+ if (documentId) setApplyingFromServer(collection$1, documentId, false);
3432
+ }
3433
+ });
3434
+ };
3435
+ const handleSubscriptionUpdate = async (response) => {
3436
+ try {
3437
+ if (!response || !Array.isArray(response.changes)) {
3438
+ logger.error("Invalid subscription response", { response });
3439
+ return;
3440
+ }
3441
+ const { changes, cursor: newCursor, compact: compactHint } = response;
3442
+ for (const change of changes) {
3443
+ const { operationType, crdtBytes, documentId } = change;
3444
+ if (!crdtBytes) {
3445
+ logger.warn("Skipping change with missing crdtBytes", { change });
3446
+ continue;
3447
+ }
3448
+ try {
3449
+ if (operationType === "snapshot") handleSnapshotChange(crdtBytes);
3450
+ else handleDeltaChange(crdtBytes, documentId);
3451
+ } catch (changeError) {
3452
+ logger.error("Failed to apply change", {
3453
+ operationType,
3454
+ documentId,
3455
+ error: String(changeError)
3456
+ });
3457
+ }
3458
+ }
3459
+ if (newCursor !== void 0) try {
3460
+ const key = `cursor:${collection$1}`;
3461
+ await persistence$1.kv.set(key, newCursor);
3462
+ logger.debug("Cursor saved", {
3463
+ collection: collection$1,
3464
+ cursor: newCursor
3465
+ });
3466
+ await convexClient.mutation(api.mark, {
3467
+ peerId,
3468
+ syncedSeq: newCursor
3469
+ });
3470
+ logger.debug("Ack sent", {
3471
+ collection: collection$1,
3472
+ peerId,
3473
+ syncedSeq: newCursor
3474
+ });
3475
+ } catch (ackError) {
3476
+ logger.error("Failed to save cursor or ack", {
3477
+ collection: collection$1,
3478
+ error: String(ackError)
3479
+ });
3480
+ }
3481
+ if (compactHint) try {
3482
+ const snapshot = Y.encodeStateAsUpdate(ydoc);
3483
+ const stateVector = Y.encodeStateVector(ydoc);
3484
+ await convexClient.mutation(api.compact, {
3485
+ documentId: compactHint,
3486
+ snapshotBytes: snapshot.buffer,
3487
+ stateVector: stateVector.buffer
3488
+ });
3489
+ logger.info("Compaction triggered", {
3490
+ collection: collection$1,
3491
+ documentId: compactHint
3492
+ });
3493
+ } catch (compactError) {
3494
+ logger.error("Compaction failed", {
3495
+ collection: collection$1,
3496
+ documentId: compactHint,
3497
+ error: String(compactError)
3498
+ });
3499
+ }
3500
+ } catch (error) {
3501
+ logger.error("Subscription handler error", {
3502
+ collection: collection$1,
3503
+ error: String(error)
3504
+ });
3505
+ }
3506
+ };
3507
+ logger.info("Establishing subscription", {
3508
+ collection: collection$1,
3509
+ cursor,
3510
+ limit: 1e3
3511
+ });
3512
+ subscription = convexClient.onUpdate(api.stream, {
3513
+ cursor,
3514
+ limit: 1e3
3515
+ }, (response) => {
3516
+ logger.debug("Subscription received update", {
3517
+ collection: collection$1,
3518
+ changesCount: response.changes?.length ?? 0,
3519
+ cursor: response.cursor,
3520
+ hasMore: response.hasMore
3521
+ });
3522
+ handleSubscriptionUpdate(response);
3523
+ });
3524
+ logger.info("Subscription established", { collection: collection$1 });
3525
+ } catch (error) {
3526
+ logger.error("Failed to set up collection", {
3527
+ error,
3528
+ collection: collection$1
3529
+ });
3530
+ markReady();
3531
+ }
3532
+ })();
3533
+ return {
3534
+ material: docs,
3535
+ cleanup: () => {
3536
+ subscription?.();
3537
+ cleanup(collection$1);
3538
+ const prefix = `${collection$1}:`;
3539
+ for (const [key, um] of fragmentUndoManagers) if (key.startsWith(prefix)) {
3540
+ um.destroy();
3541
+ fragmentUndoManagers.delete(key);
3542
+ }
3543
+ collectionMutex.delete(collection$1);
3544
+ debounceConfig.delete(collection$1);
3545
+ collectionRefs.delete(collection$1);
3546
+ collectionUndoConfig.delete(collection$1);
3547
+ collectionDocs.delete(collection$1);
3548
+ docPersistence?.destroy();
3549
+ ydoc?.destroy();
3550
+ cleanupFunctions.delete(collection$1);
3551
+ }
3552
+ };
3553
+ }
3554
+ }
3555
+ };
3556
+ }
3557
+ const collection = { create(options) {
3558
+ let persistence$1 = null;
3559
+ let resolvedConfig = null;
3560
+ let material;
3561
+ let instance = null;
3562
+ return {
3563
+ async init(mat) {
3564
+ if (!persistence$1) {
3565
+ persistence$1 = await options.persistence();
3566
+ resolvedConfig = options.config();
3567
+ material = mat;
3568
+ }
3569
+ },
3570
+ get() {
3571
+ if (!persistence$1 || !resolvedConfig) throw new Error("Call init() before get()");
3572
+ if (!instance) instance = createCollection(convexCollectionOptions({
3573
+ ...resolvedConfig,
3574
+ persistence: persistence$1,
3575
+ material
3576
+ }));
3577
+ return instance;
3578
+ }
3579
+ };
3580
+ } };
3581
+
3582
+ //#endregion
3583
+ //#region src/client/persistence/memory.ts
3584
+ /**
3585
+ * In-memory key-value store.
3586
+ */
3587
+ var MemoryKeyValueStore = class {
3588
+ store = /* @__PURE__ */ new Map();
3589
+ async get(key) {
3590
+ return this.store.get(key);
3591
+ }
3592
+ async set(key, value) {
3593
+ this.store.set(key, value);
3594
+ }
3595
+ async del(key) {
3596
+ this.store.delete(key);
3597
+ }
3598
+ };
3599
+ /**
3600
+ * No-op persistence provider for in-memory usage.
3601
+ *
3602
+ * The Y.Doc is kept in memory without persistence.
3603
+ */
3604
+ var MemoryPersistenceProvider = class {
3605
+ whenSynced = Promise.resolve();
3606
+ destroy() {}
3607
+ };
3608
+ /**
3609
+ * Create an in-memory persistence factory.
3610
+ *
3611
+ * Useful for testing where you don't want IndexedDB side effects.
3612
+ *
3613
+ * @example
3614
+ * ```typescript
3615
+ * // In tests
3616
+ * convexCollectionOptions<Task>({
3617
+ * // ... other options
3618
+ * persistence: memoryPersistence(),
3619
+ * });
3620
+ * ```
3621
+ */
3622
+ function memoryPersistence() {
3623
+ return {
3624
+ createDocPersistence: (_, __) => new MemoryPersistenceProvider(),
3625
+ kv: new MemoryKeyValueStore()
3626
+ };
3627
+ }
3628
+
3629
+ //#endregion
3630
+ //#region src/client/persistence/sqlite/schema.ts
3631
+ async function initSchema(executor) {
3632
+ await executor.execute(`
3633
+ CREATE TABLE IF NOT EXISTS snapshots (
3634
+ collection TEXT PRIMARY KEY,
3635
+ data BLOB NOT NULL,
3636
+ state_vector BLOB,
3637
+ seq INTEGER DEFAULT 0
3638
+ )
3639
+ `);
3640
+ await executor.execute(`
3641
+ CREATE TABLE IF NOT EXISTS updates (
3642
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
3643
+ collection TEXT NOT NULL,
3644
+ data BLOB NOT NULL
3645
+ )
3646
+ `);
3647
+ await executor.execute(`
3648
+ CREATE INDEX IF NOT EXISTS updates_collection_idx ON updates (collection)
3649
+ `);
3650
+ await executor.execute(`
3651
+ CREATE TABLE IF NOT EXISTS kv (
3652
+ key TEXT PRIMARY KEY,
3653
+ value TEXT NOT NULL
3654
+ )
3655
+ `);
3656
+ }
3657
+ var SqliteKeyValueStore = class {
3658
+ constructor(executor) {
3659
+ this.executor = executor;
3660
+ }
3661
+ async get(key) {
3662
+ const result = await this.executor.execute("SELECT value FROM kv WHERE key = ?", [key]);
3663
+ if (result.rows.length === 0) return void 0;
3664
+ return JSON.parse(result.rows[0].value);
3665
+ }
3666
+ async set(key, value) {
3667
+ await this.executor.execute("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)", [key, JSON.stringify(value)]);
3668
+ }
3669
+ async del(key) {
3670
+ await this.executor.execute("DELETE FROM kv WHERE key = ?", [key]);
3671
+ }
3672
+ };
3673
+ var SqlitePersistenceProvider = class {
3674
+ updateHandler;
3675
+ whenSynced;
3676
+ constructor(executor, collection$1, ydoc) {
3677
+ this.executor = executor;
3678
+ this.collection = collection$1;
3679
+ this.ydoc = ydoc;
3680
+ this.whenSynced = this.loadState();
3681
+ this.updateHandler = (update, origin) => {
3682
+ if (origin !== "sqlite") this.saveUpdate(update);
3683
+ };
3684
+ this.ydoc.on("update", this.updateHandler);
3685
+ }
3686
+ async loadState() {
3687
+ const snapshotResult = await this.executor.execute("SELECT data FROM snapshots WHERE collection = ?", [this.collection]);
3688
+ if (snapshotResult.rows.length > 0) {
3689
+ const raw = snapshotResult.rows[0].data;
3690
+ const snapshotData = raw instanceof Uint8Array ? raw : new Uint8Array(raw);
3691
+ Y.applyUpdate(this.ydoc, snapshotData, "sqlite");
3692
+ }
3693
+ const updatesResult = await this.executor.execute("SELECT data FROM updates WHERE collection = ? ORDER BY id ASC", [this.collection]);
3694
+ for (const row of updatesResult.rows) {
3695
+ const raw = row.data;
3696
+ const updateData = raw instanceof Uint8Array ? raw : new Uint8Array(raw);
3697
+ Y.applyUpdate(this.ydoc, updateData, "sqlite");
3698
+ }
3699
+ }
3700
+ async saveUpdate(update) {
3701
+ await this.executor.execute("INSERT INTO updates (collection, data) VALUES (?, ?)", [this.collection, update]);
3702
+ }
3703
+ destroy() {
3704
+ this.ydoc.off("update", this.updateHandler);
3705
+ }
3706
+ };
3707
+ function createPersistenceFromExecutor(executor) {
3708
+ return {
3709
+ createDocPersistence: (collection$1, ydoc) => new SqlitePersistenceProvider(executor, collection$1, ydoc),
3710
+ kv: new SqliteKeyValueStore(executor)
3711
+ };
3712
+ }
3713
+
3714
+ //#endregion
3715
+ //#region src/client/persistence/sqlite/browser.ts
3716
+ function hasOPFS() {
3717
+ return typeof navigator !== "undefined" && "storage" in navigator && "getDirectory" in navigator.storage;
3718
+ }
3719
+ async function loadFromOPFS(dbName) {
3720
+ try {
3721
+ const file = await (await (await navigator.storage.getDirectory()).getFileHandle(`${dbName}.sqlite`)).getFile();
3722
+ return new Uint8Array(await file.arrayBuffer());
3723
+ } catch {
3724
+ return null;
3725
+ }
3726
+ }
3727
+ async function saveToOPFS(dbName, data) {
3728
+ const writable = await (await (await navigator.storage.getDirectory()).getFileHandle(`${dbName}.sqlite`, { create: true })).createWritable();
3729
+ await writable.write(new Uint8Array(data));
3730
+ await writable.close();
3731
+ }
3732
+ const IDB_STORE = "sqlite-db";
3733
+ function openIDB(dbName) {
3734
+ return new Promise((resolve, reject) => {
3735
+ const request = indexedDB.open(`replicate-sqlite-${dbName}`, 1);
3736
+ request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("IndexedDB open failed"));
3737
+ request.onsuccess = () => resolve(request.result);
3738
+ request.onupgradeneeded = () => {
3739
+ request.result.createObjectStore(IDB_STORE);
3740
+ };
3741
+ });
3742
+ }
3743
+ async function loadFromIDB(dbName) {
3744
+ try {
3745
+ const db = await openIDB(dbName);
3746
+ return new Promise((resolve) => {
3747
+ const request = db.transaction(IDB_STORE, "readonly").objectStore(IDB_STORE).get("data");
3748
+ request.onsuccess = () => {
3749
+ db.close();
3750
+ resolve(request.result ?? null);
3751
+ };
3752
+ request.onerror = () => {
3753
+ db.close();
3754
+ resolve(null);
3755
+ };
3756
+ });
3757
+ } catch {
3758
+ return null;
3759
+ }
3760
+ }
3761
+ async function saveToIDB(dbName, data) {
3762
+ const db = await openIDB(dbName);
3763
+ return new Promise((resolve, reject) => {
3764
+ const request = db.transaction(IDB_STORE, "readwrite").objectStore(IDB_STORE).put(data, "data");
3765
+ request.onsuccess = () => {
3766
+ db.close();
3767
+ resolve();
3768
+ };
3769
+ request.onerror = () => {
3770
+ db.close();
3771
+ reject(request.error ?? /* @__PURE__ */ new Error("IndexedDB put failed"));
3772
+ };
3773
+ });
3774
+ }
3775
+ function createStorageBackend(dbName) {
3776
+ if (hasOPFS()) return {
3777
+ load: () => loadFromOPFS(dbName),
3778
+ save: (data) => saveToOPFS(dbName, data)
3779
+ };
3780
+ return {
3781
+ load: () => loadFromIDB(dbName),
3782
+ save: (data) => saveToIDB(dbName, data)
3783
+ };
3784
+ }
3785
+ var SqlJsExecutor = class {
3786
+ constructor(db, storage) {
3787
+ this.db = db;
3788
+ this.storage = storage;
3789
+ }
3790
+ async execute(sql, params) {
3791
+ const rows = [];
3792
+ const trimmed = sql.trim().toUpperCase();
3793
+ if (trimmed.startsWith("CREATE") || trimmed.startsWith("INSERT") || trimmed.startsWith("UPDATE") || trimmed.startsWith("DELETE") || trimmed.startsWith("BEGIN") || trimmed.startsWith("COMMIT") || trimmed.startsWith("ROLLBACK")) {
3794
+ this.db.run(sql, params);
3795
+ await this.storage.save(this.db.export());
3796
+ return { rows };
3797
+ }
3798
+ const stmt = this.db.prepare(sql);
3799
+ if (params?.length) stmt.bind(params);
3800
+ while (stmt.step()) rows.push(stmt.getAsObject());
3801
+ stmt.free();
3802
+ return { rows };
3803
+ }
3804
+ close() {
3805
+ this.db.close();
3806
+ }
3807
+ };
3808
+ async function createBrowserSqlitePersistence(SQL, dbName) {
3809
+ const storage = createStorageBackend(dbName);
3810
+ const existingData = await storage.load();
3811
+ const executor = new SqlJsExecutor(existingData ? new SQL.Database(existingData) : new SQL.Database(), storage);
3812
+ await initSchema(executor);
3813
+ return createPersistenceFromExecutor(executor);
3814
+ }
3815
+
3816
+ //#endregion
3817
+ //#region src/client/persistence/sqlite/native.ts
3818
+ var OPSqliteExecutor = class {
3819
+ constructor(db) {
3820
+ this.db = db;
3821
+ }
3822
+ async execute(sql, params) {
3823
+ return { rows: (await this.db.execute(sql, params)).rows || [] };
3824
+ }
3825
+ close() {
3826
+ this.db.close();
3827
+ }
3828
+ };
3829
+ async function createNativeSqlitePersistence(db, _dbName) {
3830
+ const executor = new OPSqliteExecutor(db);
3831
+ await initSchema(executor);
3832
+ return createPersistenceFromExecutor(executor);
3833
+ }
3834
+
3835
+ //#endregion
3836
+ //#region src/client/persistence/indexeddb.ts
3837
+ const UPDATES_STORE = "updates";
3838
+ const SNAPSHOTS_STORE = "snapshots";
3839
+ const KV_STORE = "kv";
3840
+ function openDatabase(dbName) {
3841
+ return new Promise((resolve, reject) => {
3842
+ const request = indexedDB.open(`replicate-${dbName}`, 1);
3843
+ request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("IndexedDB open failed"));
3844
+ request.onsuccess = () => resolve(request.result);
3845
+ request.onupgradeneeded = () => {
3846
+ const db = request.result;
3847
+ if (!db.objectStoreNames.contains(UPDATES_STORE)) db.createObjectStore(UPDATES_STORE, { autoIncrement: true });
3848
+ if (!db.objectStoreNames.contains(SNAPSHOTS_STORE)) db.createObjectStore(SNAPSHOTS_STORE);
3849
+ if (!db.objectStoreNames.contains(KV_STORE)) db.createObjectStore(KV_STORE);
3850
+ };
3851
+ });
3852
+ }
3853
+ var IDBKeyValueStore = class {
3854
+ constructor(db) {
3855
+ this.db = db;
3856
+ }
3857
+ get(key) {
3858
+ return new Promise((resolve, reject) => {
3859
+ const request = this.db.transaction(KV_STORE, "readonly").objectStore(KV_STORE).get(key);
3860
+ request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("IndexedDB get failed"));
3861
+ request.onsuccess = () => resolve(request.result);
3862
+ });
3863
+ }
3864
+ set(key, value) {
3865
+ return new Promise((resolve, reject) => {
3866
+ const request = this.db.transaction(KV_STORE, "readwrite").objectStore(KV_STORE).put(value, key);
3867
+ request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("IndexedDB set failed"));
3868
+ request.onsuccess = () => resolve();
3869
+ });
3870
+ }
3871
+ del(key) {
3872
+ return new Promise((resolve, reject) => {
3873
+ const request = this.db.transaction(KV_STORE, "readwrite").objectStore(KV_STORE).delete(key);
3874
+ request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("IndexedDB delete failed"));
3875
+ request.onsuccess = () => resolve();
3876
+ });
3877
+ }
3878
+ };
3879
+ var IDBPersistenceProvider = class {
3880
+ updateHandler;
3881
+ whenSynced;
3882
+ constructor(db, collection$1, ydoc) {
3883
+ this.db = db;
3884
+ this.collection = collection$1;
3885
+ this.ydoc = ydoc;
3886
+ this.whenSynced = this.loadState();
3887
+ this.updateHandler = (update, origin) => {
3888
+ if (origin !== "idb") this.saveUpdate(update);
3889
+ };
3890
+ this.ydoc.on("update", this.updateHandler);
3891
+ }
3892
+ loadState() {
3893
+ return new Promise((resolve, reject) => {
3894
+ const tx = this.db.transaction([SNAPSHOTS_STORE, UPDATES_STORE], "readonly");
3895
+ const snapshotRequest = tx.objectStore(SNAPSHOTS_STORE).get(this.collection);
3896
+ snapshotRequest.onsuccess = () => {
3897
+ if (snapshotRequest.result) Y.applyUpdate(this.ydoc, snapshotRequest.result, "idb");
3898
+ const updatesRequest = tx.objectStore(UPDATES_STORE).getAll();
3899
+ updatesRequest.onsuccess = () => {
3900
+ const updates = updatesRequest.result;
3901
+ for (const update of updates) Y.applyUpdate(this.ydoc, update, "idb");
3902
+ resolve();
3903
+ };
3904
+ updatesRequest.onerror = () => reject(updatesRequest.error ?? /* @__PURE__ */ new Error("IndexedDB updates load failed"));
3905
+ };
3906
+ snapshotRequest.onerror = () => reject(snapshotRequest.error ?? /* @__PURE__ */ new Error("IndexedDB snapshot load failed"));
3907
+ });
3908
+ }
3909
+ saveUpdate(update) {
3910
+ return new Promise((resolve, reject) => {
3911
+ const request = this.db.transaction(UPDATES_STORE, "readwrite").objectStore(UPDATES_STORE).add(update);
3912
+ request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("IndexedDB save update failed"));
3913
+ request.onsuccess = () => resolve();
3914
+ });
3915
+ }
3916
+ destroy() {
3917
+ this.ydoc.off("update", this.updateHandler);
3918
+ }
3919
+ };
3920
+ async function createIndexedDBPersistence(dbName) {
3921
+ const db = await openDatabase(dbName);
3922
+ return {
3923
+ createDocPersistence: (collection$1, ydoc) => new IDBPersistenceProvider(db, collection$1, ydoc),
3924
+ kv: new IDBKeyValueStore(db)
3925
+ };
3926
+ }
3927
+
3928
+ //#endregion
3929
+ //#region src/client/persistence/custom.ts
3930
+ const SNAPSHOT_PREFIX = "snapshot:";
3931
+ const UPDATE_PREFIX = "update:";
3932
+ const META_PREFIX = "meta:";
3933
+ var AdapterKeyValueStore = class {
3934
+ constructor(adapter) {
3935
+ this.adapter = adapter;
3936
+ }
3937
+ async get(key) {
3938
+ const data = await this.adapter.get(`${META_PREFIX}${key}`);
3939
+ if (!data) return void 0;
3940
+ return JSON.parse(new TextDecoder().decode(data));
3941
+ }
3942
+ async set(key, value) {
3943
+ await this.adapter.set(`${META_PREFIX}${key}`, new TextEncoder().encode(JSON.stringify(value)));
3944
+ }
3945
+ async del(key) {
3946
+ await this.adapter.delete(`${META_PREFIX}${key}`);
3947
+ }
3948
+ };
3949
+ var AdapterPersistenceProvider = class {
3950
+ updateHandler;
3951
+ updateCounter = 0;
3952
+ whenSynced;
3953
+ constructor(adapter, collection$1, ydoc) {
3954
+ this.adapter = adapter;
3955
+ this.collection = collection$1;
3956
+ this.ydoc = ydoc;
3957
+ this.whenSynced = this.loadState();
3958
+ this.updateHandler = (update, origin) => {
3959
+ if (origin !== "custom") this.saveUpdate(update);
3960
+ };
3961
+ this.ydoc.on("update", this.updateHandler);
3962
+ }
3963
+ async loadState() {
3964
+ const snapshotData = await this.adapter.get(`${SNAPSHOT_PREFIX}${this.collection}`);
3965
+ if (snapshotData) Y.applyUpdate(this.ydoc, snapshotData, "custom");
3966
+ const sortedKeys = (await this.adapter.keys(`${UPDATE_PREFIX}${this.collection}:`)).sort();
3967
+ for (const key of sortedKeys) {
3968
+ const updateData = await this.adapter.get(key);
3969
+ if (updateData) {
3970
+ Y.applyUpdate(this.ydoc, updateData, "custom");
3971
+ const seq = parseInt(key.split(":").pop() || "0", 10);
3972
+ if (seq > this.updateCounter) this.updateCounter = seq;
3973
+ }
3974
+ }
3975
+ }
3976
+ async saveUpdate(update) {
3977
+ this.updateCounter++;
3978
+ const paddedCounter = String(this.updateCounter).padStart(10, "0");
3979
+ await this.adapter.set(`${UPDATE_PREFIX}${this.collection}:${paddedCounter}`, update);
3980
+ }
3981
+ destroy() {
3982
+ this.ydoc.off("update", this.updateHandler);
3983
+ }
3984
+ };
3985
+ function createCustomPersistence(adapter) {
3986
+ return {
3987
+ createDocPersistence: (collection$1, ydoc) => new AdapterPersistenceProvider(adapter, collection$1, ydoc),
3988
+ kv: new AdapterKeyValueStore(adapter)
3989
+ };
3990
+ }
3991
+
3992
+ //#endregion
3993
+ //#region src/client/persistence/index.ts
3994
+ const persistence = {
3995
+ memory: memoryPersistence,
3996
+ sqlite: {
3997
+ browser: createBrowserSqlitePersistence,
3998
+ native: createNativeSqlitePersistence
3999
+ },
4000
+ indexeddb: createIndexedDBPersistence,
4001
+ custom: createCustomPersistence
4002
+ };
4003
+
4004
+ //#endregion
4005
+ //#region src/client/index.ts
4006
+ const errors = {
4007
+ Network: NetworkError,
4008
+ IDB: IDBError,
4009
+ IDBWrite: IDBWriteError,
4010
+ Reconciliation: ReconciliationError,
4011
+ Prose: ProseError,
4012
+ CollectionNotReady: CollectionNotReadyError,
4013
+ NonRetriable: NonRetriableError
4014
+ };
4015
+ function empty() {
4016
+ return {
4017
+ type: "doc",
4018
+ content: []
4019
+ };
4020
+ }
4021
+ const prose = Object.assign(prose$1, {
4022
+ extract,
4023
+ empty
4024
+ });
4025
+
4026
+ //#endregion
4027
+ export { collection, errors, persistence, prose };