@delali/narsil 0.3.1-next.185 → 0.3.1-next.193

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 (170) hide show
  1. package/README.md +1 -0
  2. package/dist/adapters/filesystem.mjs +2 -2
  3. package/dist/adapters/gcs.mjs +2 -2
  4. package/dist/adapters/indexeddb.mjs +1 -1
  5. package/dist/adapters/s3.mjs +2 -2
  6. package/dist/browser/chunk-4E5APHTM.mjs +1459 -0
  7. package/dist/browser/{chunk-4ZICWS4V.mjs → chunk-QAOVAYAG.mjs} +1 -1
  8. package/dist/browser/{chunk-CPSHS46N.mjs → chunk-UQG4HUGQ.mjs} +82 -50
  9. package/dist/browser/{chunk-2Q45HI5H.mjs → chunk-YINZFPLT.mjs} +7 -0
  10. package/dist/browser/{module-XVLHJ7T7.mjs → module-ZZTNG5JZ.mjs} +26 -3
  11. package/dist/browser/{optional-writes-ZTBHYQMN.mjs → optional-writes-J5BHKEQL.mjs} +21 -8
  12. package/dist/browser/{storage-leaf-4XMCITJ6.mjs → storage-leaf-N3JMQ6CF.mjs} +14569 -13747
  13. package/dist/{capabilities-DAHXphRn.d.ts → capabilities-CLnQHIqV.d.ts} +51 -9
  14. package/dist/{chunk-KLVMK3R5.mjs → chunk-2CKDCC65.mjs} +1 -1
  15. package/dist/{chunk-CODGHJVW.mjs → chunk-2HVQYOOD.mjs} +40 -12
  16. package/dist/{chunk-M5I64AQW.mjs → chunk-3N5GUXBI.mjs} +33 -33
  17. package/dist/{chunk-DU23J5JG.mjs → chunk-5CIHQIA4.mjs} +6 -6
  18. package/dist/{react-native/chunks/chunk-ZUFOYZ5P.mjs → chunk-62X472AC.mjs} +1 -1
  19. package/dist/{chunk-QJGDB3WR.mjs → chunk-6GHX35ZM.mjs} +2 -2
  20. package/dist/{chunk-G3XRHAJA.mjs → chunk-6MC6BCXK.mjs} +3 -3
  21. package/dist/{chunk-X5NHWOXT.mjs → chunk-7P5YWDRQ.mjs} +2 -2
  22. package/dist/{chunk-MHBG33R5.mjs → chunk-7X4IIREW.mjs} +76 -46
  23. package/dist/{chunk-F4OOBPKA.mjs → chunk-AOYQTX6W.mjs} +1 -1
  24. package/dist/{chunk-D6FYUTFN.mjs → chunk-ASZLNVWW.mjs} +1 -1
  25. package/dist/{chunk-LR6JJVML.mjs → chunk-D2EJUM4A.mjs} +764 -699
  26. package/dist/{chunk-FETZYCH7.mjs → chunk-F6F4G322.mjs} +26 -1
  27. package/dist/{chunk-2YWWHMVC.mjs → chunk-FGOGT273.mjs} +2 -2
  28. package/dist/{chunk-5BUEBIXR.mjs → chunk-GLXLSPLO.mjs} +18 -5
  29. package/dist/{chunk-IE3Q62QD.mjs → chunk-GR5GMFJJ.mjs} +1 -1
  30. package/dist/{chunk-Y7QUFIGV.mjs → chunk-GXED6R3Q.mjs} +2 -2
  31. package/dist/{chunk-BVZWWMOU.mjs → chunk-HHYQMGPW.mjs} +2 -2
  32. package/dist/{chunk-SL53DHVE.mjs → chunk-HSA3H3VW.mjs} +121 -68
  33. package/dist/{chunk-YM2HI5N5.mjs → chunk-IL5DRCNU.mjs} +2 -2
  34. package/dist/{chunk-JDG6CCXM.mjs → chunk-JENWA4WX.mjs} +2 -2
  35. package/dist/{chunk-2GONI5HL.mjs → chunk-JXINPXRS.mjs} +5 -4
  36. package/dist/chunk-K5GNK5GE.mjs +602 -0
  37. package/dist/{chunk-WLFOLAQI.mjs → chunk-KBU3ZFJN.mjs} +2 -2
  38. package/dist/{chunk-EDEE7ETQ.mjs → chunk-KOA6T2IC.mjs} +3 -3
  39. package/dist/{chunk-N3M4NPML.mjs → chunk-M2Z5WVWW.mjs} +1 -1
  40. package/dist/{chunk-EWGIF6FQ.mjs → chunk-NJJN4FMM.mjs} +1 -1
  41. package/dist/{chunk-C7YQHCSO.mjs → chunk-P4F4UWLC.mjs} +1 -1
  42. package/dist/{chunk-VYSWVQCE.mjs → chunk-PAPQGC6I.mjs} +2 -2
  43. package/dist/{chunk-I2WJCRIH.mjs → chunk-R2PJKSNL.mjs} +1 -1
  44. package/dist/{chunk-6SZQETSC.mjs → chunk-RORNUNXW.mjs} +5 -2
  45. package/dist/{chunk-FBQYC5OX.mjs → chunk-SB5HCIIO.mjs} +16 -13
  46. package/dist/{chunk-CYCVPTTR.mjs → chunk-SCZI7COG.mjs} +2 -2
  47. package/dist/{chunk-O337MDRJ.mjs → chunk-SHES4VP5.mjs} +38 -13
  48. package/dist/{chunk-H4QYQ5DE.mjs → chunk-SIIDIGKI.mjs} +2 -2
  49. package/dist/{chunk-A3OAFUZI.mjs → chunk-SM2SYVIX.mjs} +2 -2
  50. package/dist/{chunk-II6XF6M4.mjs → chunk-SMLUZGSI.mjs} +9 -8
  51. package/dist/{chunk-2KCCQK4R.mjs → chunk-TSE2VWPX.mjs} +2 -2
  52. package/dist/{chunk-WSIU6YXQ.mjs → chunk-YPEJ6ODL.mjs} +1732 -743
  53. package/dist/{chunk-7WXGUTWK.mjs → chunk-Z6SIJH5O.mjs} +17 -6
  54. package/dist/{chunk-Q3ZZH5Z7.mjs → chunk-ZKOQR7EA.mjs} +7 -0
  55. package/dist/{chunk-CKEOTSF5.mjs → chunk-ZS2YUB2L.mjs} +6 -3
  56. package/dist/{chunk-FCQMZDRI.mjs → chunk-ZVZYJIIP.mjs} +3 -3
  57. package/dist/{client-DpqtT2XG.d.ts → client-uB0kp2yT.d.ts} +88 -5
  58. package/dist/client.d.ts +7 -7
  59. package/dist/client.mjs +3 -1265
  60. package/dist/{config-D9WR3MhO.d.ts → config-BvHQ-C7U.d.ts} +3 -3
  61. package/dist/{config-C687-reW.d.ts → config-PqgNY1H7.d.ts} +1 -1
  62. package/dist/{core-leaf-NEUM7PU3.mjs → core-leaf-TAR3GJVE.mjs} +3 -3
  63. package/dist/{direct-executor-37IH3ISS.mjs → direct-executor-WSZALYCV.mjs} +8 -8
  64. package/dist/distribution/coordinator/etcd.d.ts +1 -1
  65. package/dist/distribution/coordinator/etcd.mjs +35 -15
  66. package/dist/distribution/coordinator/in-memory.d.ts +1 -1
  67. package/dist/distribution/coordinator/in-memory.mjs +16 -6
  68. package/dist/distribution/transport/grpc.mjs +2 -2
  69. package/dist/distribution/transport/tcp.mjs +2 -2
  70. package/dist/distribution.d.ts +70 -9
  71. package/dist/distribution.mjs +1695 -680
  72. package/dist/{durability-config-NWU2G2SJ.mjs → durability-config-VWS2K24E.mjs} +1 -1
  73. package/dist/{react-native/chunks/embed-4LBSQD7U.mjs → embed-4PHHKGBK.mjs} +1 -1
  74. package/dist/embeddings/openai.mjs +2 -2
  75. package/dist/{engine-De6JzTW8.d.ts → engine-qjAxJSKk.d.ts} +206 -7
  76. package/dist/{index-CBUl4z3E.d.ts → index-BaQIAnk0.d.ts} +137 -111
  77. package/dist/index.browser.mjs +3 -3
  78. package/dist/index.d.ts +4 -4
  79. package/dist/index.mjs +8 -8
  80. package/dist/index.react-native.mjs +9 -9
  81. package/dist/{long-query-thread-DIPJBMV7.mjs → long-query-thread-N2DNPEA3.mjs} +21 -21
  82. package/dist/{module-Z3CL4GJ4.mjs → module-UQFIJQMG.mjs} +33 -13
  83. package/dist/{msgpack-leaf-7KBYOVTF.mjs → msgpack-leaf-H5ZYW7S7.mjs} +4 -4
  84. package/dist/{optional-reads-ENKMVQ4Q.mjs → optional-reads-5HFMF2QZ.mjs} +2 -2
  85. package/dist/{optional-writes-YIVJMQ4D.mjs → optional-writes-CGDBJBHQ.mjs} +31 -18
  86. package/dist/{paged-partition-archive-IZ6U7DVN.mjs → paged-partition-archive-UK44CVXG.mjs} +20 -20
  87. package/dist/partial-words.browser.mjs +3 -3
  88. package/dist/partial-words.mjs +8 -8
  89. package/dist/pattern.browser.mjs +2 -2
  90. package/dist/pattern.mjs +3 -3
  91. package/dist/persistence/durability/checkpoint-worker.d.ts +7 -0
  92. package/dist/persistence/durability/checkpoint-worker.mjs +18 -18
  93. package/dist/{phrase-module-ZY3TLTQA.mjs → phrase-module-EFESWOCR.mjs} +2 -2
  94. package/dist/{query-actions-7EOKT3WD.mjs → query-actions-7BL3YZDW.mjs} +8 -8
  95. package/dist/query-suggestions/merge-worker.mjs +11 -11
  96. package/dist/query-suggestions.browser.mjs +3 -3
  97. package/dist/query-suggestions.d.ts +1 -1
  98. package/dist/query-suggestions.mjs +9 -9
  99. package/dist/react-native/chunks/{chunk-TQ26BKBH.mjs → chunk-43R4GCM7.mjs} +7 -0
  100. package/dist/react-native/chunks/{chunk-CBU4M3EU.mjs → chunk-63DZ26X5.mjs} +277 -75
  101. package/dist/react-native/chunks/{chunk-BN6FUB7N.mjs → chunk-AN4HKLB4.mjs} +1 -1
  102. package/dist/react-native/chunks/{chunk-T5HF3FW3.mjs → chunk-BNFGA7TY.mjs} +3 -3
  103. package/dist/react-native/chunks/{chunk-PVMEHBQO.mjs → chunk-CW2ZFXSE.mjs} +1 -1
  104. package/dist/react-native/chunks/{chunk-NLEFMPY3.mjs → chunk-G434H5WG.mjs} +15 -4
  105. package/dist/react-native/chunks/{chunk-SK3DPAI4.mjs → chunk-HTBY4GON.mjs} +2 -2
  106. package/dist/react-native/chunks/{chunk-ZQPSXN6C.mjs → chunk-IP3QLETQ.mjs} +5 -5
  107. package/dist/react-native/chunks/{chunk-7TPO4FLE.mjs → chunk-LOKGE4OQ.mjs} +3 -3
  108. package/dist/react-native/chunks/{chunk-ZDDJVR7X.mjs → chunk-MRH36RN3.mjs} +2 -2
  109. package/dist/react-native/chunks/{chunk-YOTP2FPE.mjs → chunk-UO44MA5M.mjs} +1 -1
  110. package/dist/{chunk-4JBTALUC.mjs → react-native/chunks/chunk-UYAZLGEM.mjs} +1 -1
  111. package/dist/react-native/chunks/{chunk-WFQ6YQZA.mjs → chunk-UYKIV2KT.mjs} +1 -1
  112. package/dist/react-native/chunks/{chunk-IEY4DARA.mjs → chunk-XHYOQ4K4.mjs} +2 -2
  113. package/dist/react-native/chunks/{chunk-OZ3XEM6D.mjs → chunk-ZUJCUPVS.mjs} +1 -1
  114. package/dist/react-native/chunks/{core-leaf-TPBRRWVN.mjs → core-leaf-5PVST5PR.mjs} +4 -4
  115. package/dist/react-native/chunks/{durability-config-RVTWSXPH.mjs → durability-config-FCSD4MXY.mjs} +1 -1
  116. package/dist/{embed-2QMYCD2Q.mjs → react-native/chunks/embed-HA2I7RCF.mjs} +1 -1
  117. package/dist/react-native/chunks/{gzip.react-native-DLO4WW27.mjs → gzip.react-native-CVM4NQG2.mjs} +2 -2
  118. package/dist/react-native/chunks/{module-COLNJNML.mjs → module-QRCYB7QV.mjs} +32 -9
  119. package/dist/react-native/chunks/msgpack-leaf-JMNGSTRW.mjs +7 -0
  120. package/dist/react-native/chunks/{optional-reads-74HYI4DO.mjs → optional-reads-AFPO2FEB.mjs} +3 -3
  121. package/dist/react-native/chunks/{optional-writes-QETIM6BI.mjs → optional-writes-GC6JWMAF.mjs} +31 -18
  122. package/dist/react-native/chunks/{phrase-module-UC4YGEBB.mjs → phrase-module-IGYBK7XH.mjs} +3 -3
  123. package/dist/react-native/chunks/{resolve-vector-text-FU7D4QP5.mjs → resolve-vector-text-GE2CRJKI.mjs} +1 -1
  124. package/dist/react-native/chunks/{restore-queue-3E2MJIJS.mjs → restore-queue-2ACONS4W.mjs} +12 -12
  125. package/dist/react-native/chunks/{segment-codec-leaf-IE4DXUZV.mjs → segment-codec-leaf-SSIWXVUG.mjs} +10 -10
  126. package/dist/react-native/chunks/{segment-merge-leaf-62LQ5WZB.mjs → segment-merge-leaf-I2WDFE6O.mjs} +11 -11
  127. package/dist/react-native/chunks/{storage-leaf-LNOLKWZB.mjs → storage-leaf-YXXMG4VR.mjs} +14727 -13915
  128. package/dist/react-native/chunks/{store-leaf-Y7CBOPFZ.mjs → store-leaf-FS7A4PZA.mjs} +11 -11
  129. package/dist/react-native/chunks/{suggestions-leaf-PSUGL6MI.mjs → suggestions-leaf-PSFCT5EX.mjs} +11 -11
  130. package/dist/react-native/chunks/{worker-copies-MW5O5U3D.mjs → worker-copies-QRMISMEL.mjs} +10 -10
  131. package/dist/react-native/partial-words.mjs +9 -9
  132. package/dist/react-native/pattern.mjs +5 -5
  133. package/dist/react-native/query-suggestions.mjs +9 -9
  134. package/dist/react-native/sync.mjs +433 -51
  135. package/dist/react-native/synonyms.mjs +9 -9
  136. package/dist/react.d.ts +8 -8
  137. package/dist/react.mjs +7 -0
  138. package/dist/{rerank-KTV5L7MZ.mjs → rerank-4GFC26FD.mjs} +8 -8
  139. package/dist/rerankers/http.mjs +2 -2
  140. package/dist/{resolve-vector-text-SD6BEMKZ.mjs → resolve-vector-text-OD4J3WHF.mjs} +2 -2
  141. package/dist/{restore-queue-ES7TDJ5V.mjs → restore-queue-E4EIUYVV.mjs} +11 -11
  142. package/dist/{segment-codec-leaf-DO75VKEV.mjs → segment-codec-leaf-MP6DILVE.mjs} +9 -9
  143. package/dist/{segment-merge-leaf-JGND6LG5.mjs → segment-merge-leaf-ED2NMUC5.mjs} +9 -9
  144. package/dist/server.d.ts +13 -9
  145. package/dist/server.mjs +875 -348
  146. package/dist/{storage-leaf-ZI3H2WJY.mjs → storage-leaf-HQFO37W7.mjs} +5234 -5835
  147. package/dist/{store-leaf-YIH36Y5J.mjs → store-leaf-PL2G7RM5.mjs} +10 -10
  148. package/dist/{suggestions-leaf-FP5HSTFP.mjs → suggestions-leaf-I3MEHACP.mjs} +12 -12
  149. package/dist/sync/filesystem.d.ts +2 -2
  150. package/dist/sync/filesystem.mjs +16 -16
  151. package/dist/sync/indexeddb.browser.mjs +1 -1
  152. package/dist/sync/indexeddb.d.ts +2 -2
  153. package/dist/sync/indexeddb.mjs +3 -3
  154. package/dist/sync.browser.mjs +35 -1108
  155. package/dist/sync.d.ts +3 -3
  156. package/dist/sync.mjs +430 -51
  157. package/dist/synonyms.browser.mjs +3 -3
  158. package/dist/synonyms.mjs +8 -8
  159. package/dist/{tasks-X2IJQc8J.d.ts → tasks-C65V7lIr.d.ts} +3 -3
  160. package/dist/thread-LTFI3RK5.mjs +31 -0
  161. package/dist/{types-D0YasjoX.d.ts → types-CpCg-Gq-.d.ts} +1 -1
  162. package/dist/vector/search-worker.mjs +5 -5
  163. package/dist/{worker-copies-WYPRCNB7.mjs → worker-copies-P7YWXEP5.mjs} +11 -11
  164. package/dist/workers/entry.mjs +14 -14
  165. package/openapi.json +144 -0
  166. package/package.json +5 -7
  167. package/dist/chunk-URVH6BLO.mjs +0 -217
  168. package/dist/react-native/chunks/msgpack-leaf-GTKLF6GJ.mjs +0 -7
  169. package/dist/thread-NQRNW3IH.mjs +0 -31
  170. /package/dist/{chunk-CV3Q2EKO.mjs → chunk-S5QMWECL.mjs} +0 -0
package/dist/sync.mjs CHANGED
@@ -1,21 +1,21 @@
1
- import { SYNC_CLOSE_RESYNC, SYNC_CLOSE_CREDENTIAL_EXPIRED, encodeJson, SYNC_SUBPROTOCOL, decodeScore } from './chunk-FETZYCH7.mjs';
1
+ import { SYNC_CLOSE_RESYNC, SYNC_CLOSE_CREDENTIAL_EXPIRED, encodeJson, SYNC_SUBPROTOCOL, CHANGES_SUBPROTOCOL, decodeScore } from './chunk-F6F4G322.mjs';
2
2
  import { createSerial, KEPT_ENTRY_FLUSH_IDLE_MS, KEPT_ENTRY_OVERHEAD_BYTES, DEFAULT_REPLAY_LIMIT_OF_SNAPSHOT, BACKOFF_DOUBLINGS_LIMIT, KEPT_ENTRY_FLUSH_BYTES, UPLOAD_BATCH_WRITES, RETRY_MAX_DELAY_MS, UPLOAD_RETRY_DELAY_MS, DOWNLOAD_SAVE_BYTES, SNAPSHOT_MEMORY_PER_ARCHIVE_BYTE, SNAPSHOT_RETRY_DELAY_MS, RECONNECT_MIN_DELAY_MS } from './chunk-6MSZ543Y.mjs';
3
- import { hasLanguage, hasTokenizer, hasStopWords, readHeapStatistics, loadStorageCode, getLanguage } from './chunk-SL53DHVE.mjs';
4
- import './chunk-KLVMK3R5.mjs';
5
- import './chunk-F4OOBPKA.mjs';
3
+ import { hasLanguage, hasTokenizer, hasStopWords, readHeapStatistics, loadStorageCode, getLanguage } from './chunk-HSA3H3VW.mjs';
4
+ import './chunk-2CKDCC65.mjs';
5
+ import './chunk-AOYQTX6W.mjs';
6
6
  import './chunk-GXCDWLQI.mjs';
7
7
  import { isSyncEpoch } from './chunk-BUMREPLU.mjs';
8
8
  import './chunk-7T4LAYT4.mjs';
9
- import './chunk-YM2HI5N5.mjs';
10
- import { loadMessagePack, loadedMessagePack, requireLoadedMessagePack, getNestedValue, extractVectorFieldsFromSchema } from './chunk-X5NHWOXT.mjs';
9
+ import './chunk-IL5DRCNU.mjs';
10
+ import { loadMessagePack, loadedMessagePack, requireLoadedMessagePack, getNestedValue, extractVectorFieldsFromSchema } from './chunk-7P5YWDRQ.mjs';
11
11
  import './chunk-U2BKYEC7.mjs';
12
- import './chunk-6SZQETSC.mjs';
12
+ import './chunk-RORNUNXW.mjs';
13
13
  import './chunk-UPDNNOTG.mjs';
14
14
  import './chunk-3ECIRFS5.mjs';
15
- import './chunk-D6FYUTFN.mjs';
15
+ import './chunk-ASZLNVWW.mjs';
16
16
  import './chunk-ZXX5GZEX.mjs';
17
17
  import './chunk-4PY4JYMY.mjs';
18
- import { NarsilError, ErrorCodes, ClientErrorCodes, ServerErrorCodes, describeError } from './chunk-Q3ZZH5Z7.mjs';
18
+ import { NarsilError, ErrorCodes, ClientErrorCodes, ServerErrorCodes, describeError } from './chunk-ZKOQR7EA.mjs';
19
19
  import './chunk-TGPKS2UM.mjs';
20
20
 
21
21
  // src/client/constants.ts
@@ -24,6 +24,8 @@ var NO_TIMEOUT = 0;
24
24
  var DEFAULT_TASK_POLL_INTERVAL_MS = 250;
25
25
  var DEFAULT_URL_SKIP_MS = 1e3;
26
26
  var DEFAULT_MAX_URL_SKIP_MS = 3e4;
27
+ var MAX_VECTOR_FIELD_PATH_SEGMENTS = 64;
28
+ var MAX_CHANGE_DOCUMENT_DEPTH = 64;
27
29
 
28
30
  // src/client/paths.ts
29
31
  function indexPath(indexName) {
@@ -32,6 +34,19 @@ function indexPath(indexName) {
32
34
  function documentPath(indexName, docId) {
33
35
  return `${indexPath(indexName)}/documents/${encodeURIComponent(docId)}`;
34
36
  }
37
+ function socketUrlOf(base, path) {
38
+ const location = globalThis.location?.href;
39
+ let parsed;
40
+ try {
41
+ parsed = location === void 0 ? new URL(base) : new URL(base, location);
42
+ } catch {
43
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, `The client cannot resolve "${base}" to a server`, { url: base });
44
+ }
45
+ parsed.protocol = parsed.protocol === "https:" ? "wss:" : "ws:";
46
+ parsed.search = "";
47
+ parsed.hash = "";
48
+ return `${parsed.toString().replace(/\/+$/, "")}${path}`;
49
+ }
35
50
  function backupPath(backupName) {
36
51
  return `/backups/${encodeURIComponent(backupName)}`;
37
52
  }
@@ -313,6 +328,363 @@ function createBulkOperations(transport) {
313
328
  };
314
329
  }
315
330
 
331
+ // src/client/change-feed-messages.ts
332
+ var CHANGE_TYPES = /* @__PURE__ */ new Set(["put", "insert", "update", "remove"]);
333
+ var RESYNC_REASONS = /* @__PURE__ */ new Set(["epoch", "history", "ahead"]);
334
+ var EPOCH = /^[0-9a-f]{32}$/;
335
+ var FLOAT32_BYTES = 4;
336
+ function malformed2(detail) {
337
+ return new NarsilError(
338
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
339
+ `The change feed sent a malformed message: ${detail}`
340
+ );
341
+ }
342
+ function isRecord2(value) {
343
+ if (value === null || typeof value !== "object") return false;
344
+ const prototype = Object.getPrototypeOf(value);
345
+ return prototype === Object.prototype || prototype === null;
346
+ }
347
+ function isCount(value) {
348
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
349
+ }
350
+ function positionsOf(raw, partitionCount, field) {
351
+ if (!Array.isArray(raw) || raw.length !== partitionCount) throw malformed2(`${field} must hold each partition once`);
352
+ const seen = /* @__PURE__ */ new Set();
353
+ const positions = [];
354
+ for (const item of raw) {
355
+ if (!isRecord2(item) || !isCount(item.partitionId) || !isCount(item.seqNo)) throw malformed2(`${field} is invalid`);
356
+ if (item.partitionId >= partitionCount || seen.has(item.partitionId)) {
357
+ throw malformed2(`${field} must hold each partition once`);
358
+ }
359
+ seen.add(item.partitionId);
360
+ positions.push({ partitionId: item.partitionId, seqNo: item.seqNo });
361
+ }
362
+ return positions;
363
+ }
364
+ function vectorOf(bytes) {
365
+ if (bytes.byteLength % FLOAT32_BYTES !== 0) throw malformed2("a vector has a length that is not whole floats");
366
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
367
+ const vector = new Float32Array(bytes.byteLength / FLOAT32_BYTES);
368
+ for (let at = 0; at < vector.length; at++) vector[at] = view.getFloat32(at * FLOAT32_BYTES, true);
369
+ return vector;
370
+ }
371
+ function restoreVectorAt(document, path) {
372
+ let holder = document;
373
+ for (let at = 0; at < path.length - 1; at++) {
374
+ const segment = path[at];
375
+ if (segment === void 0 || !Object.hasOwn(holder, segment)) return;
376
+ const next = holder[segment];
377
+ if (!isRecord2(next)) return;
378
+ holder = next;
379
+ }
380
+ const leaf = path.at(-1);
381
+ if (leaf === void 0 || !Object.hasOwn(holder, leaf)) return;
382
+ const value = holder[leaf];
383
+ if (!(value instanceof Uint8Array)) return;
384
+ Object.defineProperty(holder, leaf, { value: vectorOf(value), enumerable: true, writable: true, configurable: true });
385
+ }
386
+ function requireShallow(document) {
387
+ const pending = [{ value: document, depth: 0 }];
388
+ for (let next = pending.pop(); next !== void 0; next = pending.pop()) {
389
+ const { value, depth } = next;
390
+ if (depth > MAX_CHANGE_DOCUMENT_DEPTH) throw malformed2("a document nests too deeply");
391
+ const children = Array.isArray(value) ? value : isRecord2(value) ? Object.values(value) : null;
392
+ if (children === null) continue;
393
+ for (const child of children) {
394
+ if (Array.isArray(child) || isRecord2(child)) pending.push({ value: child, depth: depth + 1 });
395
+ }
396
+ }
397
+ }
398
+ function documentOf(raw, field, vectorFields) {
399
+ if (!isRecord2(raw)) throw malformed2(`${field} must be a map`);
400
+ requireShallow(raw);
401
+ for (const path of vectorFields) restoreVectorAt(raw, path);
402
+ return raw;
403
+ }
404
+ function changeOf(raw, vectorFields) {
405
+ if (!isRecord2(raw) || !isCount(raw.seqNo) || typeof raw.documentId !== "string")
406
+ throw malformed2("a change is invalid");
407
+ if (typeof raw.type !== "string" || !CHANGE_TYPES.has(raw.type)) throw malformed2("a change has an unknown type");
408
+ const change = { seqNo: raw.seqNo, type: raw.type, documentId: raw.documentId };
409
+ if (raw.document !== void 0 && raw.document !== null) {
410
+ change.document = documentOf(raw.document, "document", vectorFields);
411
+ }
412
+ if (raw.previousDocument !== void 0 && raw.previousDocument !== null) {
413
+ change.previousDocument = documentOf(raw.previousDocument, "previousDocument", vectorFields);
414
+ }
415
+ return change;
416
+ }
417
+ function vectorFieldsOf(raw) {
418
+ if (!Array.isArray(raw)) throw malformed2("vectorFields must be a list of paths");
419
+ const paths = [];
420
+ for (const item of raw) {
421
+ if (typeof item !== "string") throw malformed2("vectorFields must be a list of paths");
422
+ const path = item.split(".");
423
+ if (path.length > MAX_VECTOR_FIELD_PATH_SEGMENTS || path.includes("")) {
424
+ throw malformed2(`the vector field path ${item} is invalid`);
425
+ }
426
+ paths.push(path);
427
+ }
428
+ return paths;
429
+ }
430
+ function resyncOf(raw) {
431
+ if (typeof raw.reason !== "string" || !RESYNC_REASONS.has(raw.reason)) throw malformed2("the resync reason is unknown");
432
+ return { type: "resync", reason: raw.reason };
433
+ }
434
+ function firstEventOf(raw) {
435
+ if (!isRecord2(raw)) throw malformed2("a message must be a map");
436
+ if (raw.type === "resync") return { event: resyncOf(raw), vectorFields: [] };
437
+ if (raw.type !== "subscribed") throw malformed2("the first message must be subscribed or resync");
438
+ const { epoch, partitionCount, bytesBehind, windowBytes, previousDocument } = raw;
439
+ if (typeof epoch !== "string" || !EPOCH.test(epoch)) throw malformed2("the epoch is invalid");
440
+ if (!isCount(partitionCount) || partitionCount === 0) throw malformed2("the partition count is invalid");
441
+ if (!isCount(bytesBehind) || !isCount(windowBytes) || windowBytes === 0) throw malformed2("a byte count is invalid");
442
+ if (typeof previousDocument !== "boolean") throw malformed2("previousDocument must be a boolean");
443
+ const event = {
444
+ type: "subscribed",
445
+ epoch,
446
+ partitionCount,
447
+ positions: positionsOf(raw.positions, partitionCount, "positions"),
448
+ heads: positionsOf(raw.heads, partitionCount, "heads"),
449
+ bytesBehind,
450
+ previousDocument
451
+ };
452
+ return { event, vectorFields: vectorFieldsOf(raw.vectorFields) };
453
+ }
454
+ function laterEventOf(raw, partitionCount, vectorFields) {
455
+ if (!isRecord2(raw)) throw malformed2("a message must be a map");
456
+ if (raw.type === "resync") return resyncOf(raw);
457
+ if (raw.type !== "changes") throw malformed2("the message type is unknown");
458
+ const { partitionId, scannedSeqNo, changes } = raw;
459
+ if (!isCount(partitionId) || partitionId >= partitionCount) throw malformed2("the partition is unknown");
460
+ if (!isCount(scannedSeqNo) || !Array.isArray(changes)) throw malformed2("the changes are invalid");
461
+ const decoded = changes.map((change) => changeOf(change, vectorFields));
462
+ if (decoded.some((change) => change.seqNo > scannedSeqNo)) throw malformed2("a change lies above scannedSeqNo");
463
+ return { type: "changes", partitionId, changes: decoded, scannedSeqNo };
464
+ }
465
+
466
+ // src/client/change-feed.ts
467
+ var CLOSE_NORMAL = 1e3;
468
+ var CLOSE_CREDENTIAL_EXPIRED = 4401;
469
+ var CLOSE_SETTING = 4409;
470
+ var CLOSE_RESYNC = 4410;
471
+ var CLOSE_POLICY = 1008;
472
+ function closedError(code, reason) {
473
+ if (code === CLOSE_SETTING && reason.length > 0) {
474
+ return new NarsilError(reason, `The server refused the subscription with ${reason}`, { closeCode: code });
475
+ }
476
+ if (code === CLOSE_RESYNC) {
477
+ return new NarsilError(ErrorCodes.CHANGES_NOT_ENABLED, "The index has change capture off", { closeCode: code });
478
+ }
479
+ if (code === CLOSE_POLICY) {
480
+ return new NarsilError(ErrorCodes.CONFIG_INVALID, `The server refused the subscription: ${reason}`, {
481
+ closeCode: code
482
+ });
483
+ }
484
+ const why = code === CLOSE_CREDENTIAL_EXPIRED ? "the credential expired" : `the server closed it with code ${code}`;
485
+ return new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, `The change feed ended because ${why}`, {
486
+ closeCode: code,
487
+ reason
488
+ });
489
+ }
490
+ function subscribeMessage(options) {
491
+ const { epoch, positions, filter } = options;
492
+ if (epoch === void 0 !== (positions === void 0)) {
493
+ throw new NarsilError(ErrorCodes.CHANGES_INVALID_POSITIONS, "Pass epoch and positions together, or leave both out");
494
+ }
495
+ return { type: "subscribe", epoch, positions, filter };
496
+ }
497
+ function openChangeSubscription(url, options) {
498
+ if (typeof WebSocket === "undefined") {
499
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The change feed needs a runtime with a global WebSocket");
500
+ }
501
+ const subscribe = subscribeMessage(options);
502
+ const queue = [];
503
+ let failure = null;
504
+ let ended = false;
505
+ let iterating = false;
506
+ let wake = null;
507
+ let socket = null;
508
+ let codec = null;
509
+ let partitionCount = 0;
510
+ let vectorFields = [];
511
+ const saved = [];
512
+ const notify = () => {
513
+ const waiting = wake;
514
+ wake = null;
515
+ waiting?.();
516
+ };
517
+ function finish(error) {
518
+ if (ended) return;
519
+ ended = true;
520
+ failure = error;
521
+ options.signal?.removeEventListener("abort", close);
522
+ if (socket !== null && socket.readyState <= WebSocket.OPEN) socket.close(CLOSE_NORMAL, "subscriber closed");
523
+ notify();
524
+ }
525
+ function close() {
526
+ finish(null);
527
+ }
528
+ function receive(data, code) {
529
+ if (!(data instanceof ArrayBuffer)) {
530
+ finish(new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, "The change feed sent a text frame"));
531
+ return;
532
+ }
533
+ try {
534
+ const raw = code.decode(new Uint8Array(data));
535
+ let event;
536
+ if (partitionCount === 0) {
537
+ const first = firstEventOf(raw);
538
+ event = first.event;
539
+ vectorFields = first.vectorFields;
540
+ } else {
541
+ event = laterEventOf(raw, partitionCount, vectorFields);
542
+ }
543
+ if (event.type === "subscribed") {
544
+ partitionCount = event.partitionCount;
545
+ saved.push(...new Array(partitionCount).fill(0));
546
+ for (const position of event.positions) saved[position.partitionId] = position.seqNo;
547
+ }
548
+ queue.push(event);
549
+ if (event.type === "resync") {
550
+ finish(null);
551
+ return;
552
+ }
553
+ } catch (error) {
554
+ finish(
555
+ error instanceof NarsilError ? error : new NarsilError(
556
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
557
+ "The change feed sent a message that is not MessagePack",
558
+ { cause: error instanceof Error ? error.message : String(error) }
559
+ )
560
+ );
561
+ return;
562
+ }
563
+ notify();
564
+ }
565
+ function connect(code) {
566
+ codec = code;
567
+ if (ended) return;
568
+ const protocols = options.credential === void 0 ? [CHANGES_SUBPROTOCOL] : [CHANGES_SUBPROTOCOL, options.credential];
569
+ let opened;
570
+ try {
571
+ opened = new WebSocket(url, protocols);
572
+ } catch (error) {
573
+ finish(
574
+ new NarsilError(ErrorCodes.CONFIG_INVALID, "The change feed could not start a WebSocket connection", {
575
+ cause: error instanceof Error ? error.message : String(error)
576
+ })
577
+ );
578
+ return;
579
+ }
580
+ socket = opened;
581
+ opened.binaryType = "arraybuffer";
582
+ opened.onopen = () => {
583
+ if (opened.protocol !== CHANGES_SUBPROTOCOL) {
584
+ finish(
585
+ new NarsilError(
586
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
587
+ `The server answered the change feed with the subprotocol "${opened.protocol}"`
588
+ )
589
+ );
590
+ return;
591
+ }
592
+ opened.send(code.encode(subscribe, { ignoreUndefined: true }));
593
+ };
594
+ opened.onmessage = (event) => {
595
+ if (!ended) receive(event.data, code);
596
+ };
597
+ opened.onerror = () => {
598
+ if (!ended) finish(new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, `The change feed at ${url} failed`));
599
+ };
600
+ opened.onclose = (event) => {
601
+ if (!ended) finish(closedError(event.code, event.reason));
602
+ };
603
+ }
604
+ if (options.signal?.aborted === true) ended = true;
605
+ else options.signal?.addEventListener("abort", close);
606
+ if (!ended) {
607
+ loadMessagePack().then(
608
+ connect,
609
+ (error) => finish(
610
+ error instanceof NarsilError ? error : new NarsilError(ClientErrorCodes.CLIENT_UNEXPECTED_ERROR, String(error))
611
+ )
612
+ );
613
+ }
614
+ async function* events() {
615
+ try {
616
+ while (true) {
617
+ const next = queue.shift();
618
+ if (next !== void 0) {
619
+ yield next;
620
+ continue;
621
+ }
622
+ if (ended) {
623
+ if (failure !== null) throw failure;
624
+ return;
625
+ }
626
+ await new Promise((resolve) => {
627
+ wake = resolve;
628
+ });
629
+ }
630
+ } finally {
631
+ close();
632
+ }
633
+ }
634
+ return {
635
+ [Symbol.asyncIterator]() {
636
+ if (iterating) {
637
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "A change subscription returns its events to one loop alone");
638
+ }
639
+ iterating = true;
640
+ return events();
641
+ },
642
+ ack(positions) {
643
+ for (const { partitionId, seqNo } of positions) {
644
+ if (!Number.isSafeInteger(partitionId) || partitionId < 0 || !Number.isSafeInteger(seqNo) || seqNo < 0) {
645
+ throw new NarsilError(
646
+ ErrorCodes.CHANGES_INVALID_POSITIONS,
647
+ "Each acknowledged position needs whole numbers",
648
+ {
649
+ partitionId,
650
+ seqNo
651
+ }
652
+ );
653
+ }
654
+ if (saved.length > 0 && partitionId >= saved.length) {
655
+ throw new NarsilError(ErrorCodes.CHANGES_INVALID_POSITIONS, "The index has no such partition", {
656
+ partitionId,
657
+ partitionCount
658
+ });
659
+ }
660
+ }
661
+ if (ended || socket === null || codec === null || socket.readyState !== WebSocket.OPEN) return;
662
+ if (saved.length === 0) return;
663
+ for (const { partitionId, seqNo } of positions) saved[partitionId] = Math.max(saved[partitionId] ?? 0, seqNo);
664
+ const every = saved.map((seqNo, partitionId) => ({ partitionId, seqNo }));
665
+ socket.send(codec.encode({ type: "ack", positions: every }));
666
+ },
667
+ close
668
+ };
669
+ }
670
+
671
+ // src/client/changes.ts
672
+ function createChangesClientOperations(transport) {
673
+ return {
674
+ async getChanges(indexName, options) {
675
+ const path = `${indexPath(indexName)}/changes`;
676
+ return readBody(await transport.json({ method: "GET", path, options }), path);
677
+ },
678
+ async updateChanges(indexName, setting, options) {
679
+ const path = `${indexPath(indexName)}/changes`;
680
+ await transport.json({ method: "PUT", path, body: encodeJson(setting), contentType: "application/json", options });
681
+ },
682
+ followChanges(indexName, options = {}) {
683
+ return openChangeSubscription(transport.socketUrl(`${indexPath(indexName)}/changes/feed`), options);
684
+ }
685
+ };
686
+ }
687
+
316
688
  // src/client/documents.ts
317
689
  function createDocumentOperations(transport) {
318
690
  return {
@@ -449,7 +821,7 @@ function movesOnAfterFailure(failure, sentNothing, repeatable) {
449
821
  }
450
822
 
451
823
  // src/client/http.ts
452
- function isRecord2(value) {
824
+ function isRecord3(value) {
453
825
  return typeof value === "object" && value !== null && !Array.isArray(value);
454
826
  }
455
827
  function unrefTimer(timer) {
@@ -522,11 +894,11 @@ function invalidResponse(message, details) {
522
894
  return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, message, details);
523
895
  }
524
896
  function errorFromBody(status, payload, url) {
525
- const envelope = isRecord2(payload) && isRecord2(payload.error) ? payload.error : void 0;
897
+ const envelope = isRecord3(payload) && isRecord3(payload.error) ? payload.error : void 0;
526
898
  if (envelope === void 0 || typeof envelope.code !== "string" || typeof envelope.message !== "string") {
527
899
  return invalidResponse(`The server answered HTTP ${status} without a Narsil error envelope`, { url, status });
528
900
  }
529
- const details = isRecord2(envelope.details) ? envelope.details : {};
901
+ const details = isRecord3(envelope.details) ? envelope.details : {};
530
902
  return new NarsilError(envelope.code, envelope.message, { ...details, status });
531
903
  }
532
904
  function isHttpUrl(value) {
@@ -591,7 +963,7 @@ function translateFailure(err, url, timeoutMs, expired, caller) {
591
963
  function envelopeCodeOf(text) {
592
964
  try {
593
965
  const payload = JSON.parse(text);
594
- return isRecord2(payload) && isRecord2(payload.error) && typeof payload.error.code === "string" ? payload.error.code : void 0;
966
+ return isRecord3(payload) && isRecord3(payload.error) && typeof payload.error.code === "string" ? payload.error.code : void 0;
595
967
  } catch {
596
968
  return void 0;
597
969
  }
@@ -727,6 +1099,12 @@ function createTransport(options) {
727
1099
  async probe(spec) {
728
1100
  const { status } = await exchange(spec);
729
1101
  return status;
1102
+ },
1103
+ socketUrl(path) {
1104
+ const [entry] = pool.order(Date.now());
1105
+ if (entry === void 0) throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The client holds no server address");
1106
+ pool.took(entry);
1107
+ return socketUrlOf(entry.base, path);
730
1108
  }
731
1109
  };
732
1110
  }
@@ -1068,6 +1446,7 @@ function createNarsilClient(options) {
1068
1446
  ...createBackupOperations(transport),
1069
1447
  ...createServerOperations(transport),
1070
1448
  ...createSyncSettingOperations(transport),
1449
+ ...createChangesClientOperations(transport),
1071
1450
  ...createQuerySuggestionOperations(transport)
1072
1451
  };
1073
1452
  }
@@ -1092,23 +1471,23 @@ function backoffDelayMs(attempt, minimumMs, maximumMs) {
1092
1471
  }
1093
1472
 
1094
1473
  // src/sync/device/messages.ts
1095
- var RESYNC_REASONS = /* @__PURE__ */ new Set(["epoch", "history", "ahead"]);
1096
- function isRecord3(value) {
1474
+ var RESYNC_REASONS2 = /* @__PURE__ */ new Set(["epoch", "history", "ahead"]);
1475
+ function isRecord4(value) {
1097
1476
  return value !== null && typeof value === "object" && !Array.isArray(value) && !(value instanceof Uint8Array);
1098
1477
  }
1099
- function isCount(value) {
1478
+ function isCount2(value) {
1100
1479
  return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
1101
1480
  }
1102
- function malformed2(detail) {
1481
+ function malformed3(detail) {
1103
1482
  return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, `The sync feed sent a malformed message: ${detail}`);
1104
1483
  }
1105
1484
  function headsOf(raw) {
1106
- if (!Array.isArray(raw)) throw malformed2("heads must be a list");
1485
+ if (!Array.isArray(raw)) throw malformed3("heads must be a list");
1107
1486
  const seqNos = new Array(raw.length).fill(-1);
1108
1487
  for (const item of raw) {
1109
- if (!isRecord3(item) || !isCount(item.partitionId) || !isCount(item.seqNo)) throw malformed2("a head is invalid");
1488
+ if (!isRecord4(item) || !isCount2(item.partitionId) || !isCount2(item.seqNo)) throw malformed3("a head is invalid");
1110
1489
  if (item.partitionId >= seqNos.length || seqNos[item.partitionId] !== -1) {
1111
- throw malformed2("heads must name each partition once");
1490
+ throw malformed3("heads must name each partition once");
1112
1491
  }
1113
1492
  seqNos[item.partitionId] = item.seqNo;
1114
1493
  }
@@ -1118,22 +1497,22 @@ function stringList(value) {
1118
1497
  return Array.isArray(value) && value.every((item) => typeof item === "string");
1119
1498
  }
1120
1499
  function settingsOf(raw) {
1121
- if (!isRecord3(raw) || typeof raw.language !== "string") throw malformed2("settings need a language");
1500
+ if (!isRecord4(raw) || typeof raw.language !== "string") throw malformed3("settings need a language");
1122
1501
  const optionalString2 = (key) => {
1123
1502
  const value = raw[key];
1124
1503
  if (value === void 0 || value === null) return void 0;
1125
- if (typeof value !== "string") throw malformed2(`settings.${key} must be a string`);
1504
+ if (typeof value !== "string") throw malformed3(`settings.${key} must be a string`);
1126
1505
  return value;
1127
1506
  };
1128
1507
  const tokenizer = optionalString2("tokenizer");
1129
1508
  const stopWords = optionalString2("stopWords");
1130
1509
  const analysisRevision = optionalString2("analysisRevision");
1131
1510
  if (raw.stopWordList !== void 0 && raw.stopWordList !== null && !stringList(raw.stopWordList)) {
1132
- throw malformed2("settings.stopWordList must be a list of strings");
1511
+ throw malformed3("settings.stopWordList must be a list of strings");
1133
1512
  }
1134
- if (raw.bm25 !== void 0 && raw.bm25 !== null && !isRecord3(raw.bm25)) throw malformed2("settings.bm25 is invalid");
1513
+ if (raw.bm25 !== void 0 && raw.bm25 !== null && !isRecord4(raw.bm25)) throw malformed3("settings.bm25 is invalid");
1135
1514
  if (raw.synonyms !== void 0 && raw.synonyms !== null && !Array.isArray(raw.synonyms)) {
1136
- throw malformed2("settings.synonyms must be a list");
1515
+ throw malformed3("settings.synonyms must be a list");
1137
1516
  }
1138
1517
  return {
1139
1518
  language: raw.language,
@@ -1141,26 +1520,26 @@ function settingsOf(raw) {
1141
1520
  ...stopWords !== void 0 ? { stopWords } : {},
1142
1521
  ...stringList(raw.stopWordList) ? { stopWordList: raw.stopWordList } : {},
1143
1522
  ...analysisRevision !== void 0 ? { analysisRevision } : {},
1144
- ...isRecord3(raw.bm25) ? { bm25: raw.bm25 } : {},
1523
+ ...isRecord4(raw.bm25) ? { bm25: raw.bm25 } : {},
1145
1524
  ...Array.isArray(raw.synonyms) ? { synonyms: raw.synonyms } : {}
1146
1525
  };
1147
1526
  }
1148
1527
  function entriesOf(partitionId, raw) {
1149
- if (!Array.isArray(raw)) throw malformed2("entries must be a list");
1528
+ if (!Array.isArray(raw)) throw malformed3("entries must be a list");
1150
1529
  let previous = -1;
1151
1530
  return raw.map((item) => {
1152
- if (!isRecord3(item) || !isCount(item.seqNo) || typeof item.documentId !== "string") {
1153
- throw malformed2("an entry is invalid");
1531
+ if (!isRecord4(item) || !isCount2(item.seqNo) || typeof item.documentId !== "string") {
1532
+ throw malformed3("an entry is invalid");
1154
1533
  }
1155
- if (item.seqNo <= previous) throw malformed2("entries must rise in sequence order");
1534
+ if (item.seqNo <= previous) throw malformed3("entries must rise in sequence order");
1156
1535
  if (item.partitionId !== void 0 && item.partitionId !== partitionId) {
1157
- throw malformed2("an entry belongs to another partition");
1536
+ throw malformed3("an entry belongs to another partition");
1158
1537
  }
1159
1538
  previous = item.seqNo;
1160
1539
  if (item.operation === "DELETE") {
1161
1540
  return { partitionId, seqNo: item.seqNo, documentId: item.documentId, document: null };
1162
1541
  }
1163
- if (item.operation !== "INDEX" || !(item.document instanceof Uint8Array)) throw malformed2("an entry is invalid");
1542
+ if (item.operation !== "INDEX" || !(item.document instanceof Uint8Array)) throw malformed3("an entry is invalid");
1164
1543
  return { partitionId, seqNo: item.seqNo, documentId: item.documentId, document: item.document };
1165
1544
  });
1166
1545
  }
@@ -1170,17 +1549,17 @@ function decodeFeedMessage(bytes) {
1170
1549
  try {
1171
1550
  raw = decode(bytes);
1172
1551
  } catch {
1173
- throw malformed2("a frame is not MessagePack");
1552
+ throw malformed3("a frame is not MessagePack");
1174
1553
  }
1175
- if (!isRecord3(raw)) throw malformed2("a frame is not a map");
1554
+ if (!isRecord4(raw)) throw malformed3("a frame is not a map");
1176
1555
  switch (raw.type) {
1177
1556
  case "subscribed": {
1178
- if (!isSyncEpoch(raw.epoch) || !isCount(raw.partitionCount)) throw malformed2("subscribed is invalid");
1179
- if (!isCount(raw.bytesBehind) || !isCount(raw.snapshotBytes) || !isCount(raw.windowBytes)) {
1180
- throw malformed2("subscribed is invalid");
1557
+ if (!isSyncEpoch(raw.epoch) || !isCount2(raw.partitionCount)) throw malformed3("subscribed is invalid");
1558
+ if (!isCount2(raw.bytesBehind) || !isCount2(raw.snapshotBytes) || !isCount2(raw.windowBytes)) {
1559
+ throw malformed3("subscribed is invalid");
1181
1560
  }
1182
1561
  const heads = headsOf(raw.heads);
1183
- if (heads.length !== raw.partitionCount) throw malformed2("subscribed needs one head for each partition");
1562
+ if (heads.length !== raw.partitionCount) throw malformed3("subscribed needs one head for each partition");
1184
1563
  return {
1185
1564
  type: "subscribed",
1186
1565
  epoch: raw.epoch,
@@ -1193,26 +1572,26 @@ function decodeFeedMessage(bytes) {
1193
1572
  };
1194
1573
  }
1195
1574
  case "entries":
1196
- if (!isCount(raw.partitionId)) throw malformed2("entries need a partition");
1575
+ if (!isCount2(raw.partitionId)) throw malformed3("entries need a partition");
1197
1576
  return { type: "entries", partitionId: raw.partitionId, entries: entriesOf(raw.partitionId, raw.entries) };
1198
1577
  case "settings":
1199
1578
  return { type: "settings", settings: settingsOf(raw.settings) };
1200
1579
  case "checkpoint":
1201
- if (!isCount(raw.id)) throw malformed2("checkpoint needs an id");
1580
+ if (!isCount2(raw.id)) throw malformed3("checkpoint needs an id");
1202
1581
  return { type: "checkpoint", id: raw.id, heads: headsOf(raw.heads) };
1203
1582
  case "resync":
1204
- if (typeof raw.reason !== "string" || !RESYNC_REASONS.has(raw.reason)) throw malformed2("resync is invalid");
1583
+ if (typeof raw.reason !== "string" || !RESYNC_REASONS2.has(raw.reason)) throw malformed3("resync is invalid");
1205
1584
  return { type: "resync", reason: raw.reason };
1206
1585
  default:
1207
- throw malformed2(`unknown type ${String(raw.type)}`);
1586
+ throw malformed3(`unknown type ${String(raw.type)}`);
1208
1587
  }
1209
1588
  }
1210
- function positionsOf(seqNos) {
1589
+ function positionsOf2(seqNos) {
1211
1590
  return seqNos.map((seqNo, partitionId) => ({ partitionId, seqNo }));
1212
1591
  }
1213
1592
 
1214
1593
  // src/sync/device/feed.ts
1215
- var CLOSE_NORMAL = 1e3;
1594
+ var CLOSE_NORMAL2 = 1e3;
1216
1595
  function feedUrlOf(base, indexName) {
1217
1596
  const location = globalThis.location?.href;
1218
1597
  let parsed;
@@ -1249,7 +1628,7 @@ function openFeedConnection(url, credential, handlers) {
1249
1628
  }
1250
1629
  function closeSocket(reason) {
1251
1630
  if (socket.readyState === WebSocket.CONNECTING || socket.readyState === WebSocket.OPEN) {
1252
- socket.close(CLOSE_NORMAL, reason);
1631
+ socket.close(CLOSE_NORMAL2, reason);
1253
1632
  }
1254
1633
  }
1255
1634
  socket.onopen = () => {
@@ -1261,7 +1640,7 @@ function openFeedConnection(url, credential, handlers) {
1261
1640
  )
1262
1641
  );
1263
1642
  closeSocket("subprotocol");
1264
- finish(CLOSE_NORMAL);
1643
+ finish(CLOSE_NORMAL2);
1265
1644
  return;
1266
1645
  }
1267
1646
  handlers.onOpen();
@@ -1271,7 +1650,7 @@ function openFeedConnection(url, credential, handlers) {
1271
1650
  if (!(event.data instanceof ArrayBuffer)) {
1272
1651
  handlers.onFailure(new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, "The feed sent a text frame"));
1273
1652
  closeSocket("text frame");
1274
- finish(CLOSE_NORMAL);
1653
+ finish(CLOSE_NORMAL2);
1275
1654
  return;
1276
1655
  }
1277
1656
  let message;
@@ -1280,7 +1659,7 @@ function openFeedConnection(url, credential, handlers) {
1280
1659
  } catch (error) {
1281
1660
  handlers.onFailure(error);
1282
1661
  closeSocket("malformed message");
1283
- finish(CLOSE_NORMAL);
1662
+ finish(CLOSE_NORMAL2);
1284
1663
  return;
1285
1664
  }
1286
1665
  handlers.onMessage(message);
@@ -1295,7 +1674,7 @@ function openFeedConnection(url, credential, handlers) {
1295
1674
  },
1296
1675
  close(reason) {
1297
1676
  closeSocket(reason);
1298
- finish(CLOSE_NORMAL);
1677
+ finish(CLOSE_NORMAL2);
1299
1678
  }
1300
1679
  };
1301
1680
  }
@@ -1718,8 +2097,8 @@ function createSession(context, progress, state) {
1718
2097
  try {
1719
2098
  feed = openFeedConnection(feedUrl, credential, {
1720
2099
  onOpen() {
1721
- progress.setAcknowledge((seqNos) => feed?.send({ type: "ack", positions: positionsOf(seqNos) }));
1722
- feed?.send({ type: "subscribe", epoch: position.epoch, positions: positionsOf(progress.received()) });
2100
+ progress.setAcknowledge((seqNos) => feed?.send({ type: "ack", positions: positionsOf2(seqNos) }));
2101
+ feed?.send({ type: "subscribe", epoch: position.epoch, positions: positionsOf2(progress.received()) });
1723
2102
  },
1724
2103
  onMessage(message) {
1725
2104
  incoming = incoming.then(() => generation === mine ? handle(message) : void 0).catch((error) => {