@delali/narsil 0.3.1-next.98 → 0.4.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 (426) hide show
  1. package/NOTICE +45 -0
  2. package/README.md +27 -8
  3. package/dist/adapters/filesystem.d.ts +1 -1
  4. package/dist/adapters/filesystem.mjs +101 -13
  5. package/dist/adapters/gcs.d.ts +49 -0
  6. package/dist/adapters/gcs.mjs +213 -0
  7. package/dist/adapters/indexeddb.d.ts +1 -1
  8. package/dist/adapters/indexeddb.mjs +2 -7
  9. package/dist/adapters/memory.d.ts +1 -1
  10. package/dist/adapters/memory.mjs +5 -0
  11. package/dist/adapters/s3.d.ts +53 -0
  12. package/dist/adapters/s3.mjs +263 -0
  13. package/dist/{adapters-BQFQ4PRz.d.ts → adapters-pljAqmrC.d.ts} +33 -0
  14. package/dist/browser/chunk-4E5APHTM.mjs +1459 -0
  15. package/dist/browser/chunk-NDYADQ4Y.mjs +23426 -0
  16. package/dist/browser/chunk-QAOVAYAG.mjs +564 -0
  17. package/dist/browser/chunk-WC2DEXWU.mjs +29 -0
  18. package/dist/browser/chunk-YINZFPLT.mjs +1239 -0
  19. package/dist/browser/chunk-YJHGOJAC.mjs +15 -0
  20. package/dist/browser/{chunk-Z7QH2XHB.mjs → core-leaf-HWAXCEWX.mjs} +3987 -2103
  21. package/dist/browser/custom-token-results-R5BIX7SL.mjs +59 -0
  22. package/dist/browser/digest-leaf-64IPRPZD.mjs +151 -0
  23. package/dist/browser/durability-config-75NSFCQN.mjs +97 -0
  24. package/dist/browser/embed-GG6PS3EA.mjs +264 -0
  25. package/dist/browser/geo-index-GNHY4PUB.mjs +1484 -0
  26. package/dist/browser/gzip-JF3S6PPT.mjs +31 -0
  27. package/dist/browser/module-ZZTNG5JZ.mjs +5762 -0
  28. package/dist/browser/msgpack-leaf-44XNWSSY.mjs +302 -0
  29. package/dist/browser/optional-reads-XMVPG6HG.mjs +3230 -0
  30. package/dist/browser/optional-writes-76DWMRQH.mjs +3603 -0
  31. package/dist/browser/phrase-module-6UGFVW5J.mjs +617 -0
  32. package/dist/browser/resolve-vector-text-XRR2OVHE.mjs +85 -0
  33. package/dist/browser/restore-queue-SG5O735W.mjs +196 -0
  34. package/dist/browser/{runtime-NYE5TB6O.mjs → runtime-OMKE7DF4.mjs} +83 -25
  35. package/dist/browser/segment-codec-leaf-A427QJUX.mjs +554 -0
  36. package/dist/browser/segment-merge-leaf-7BZ54JPI.mjs +1636 -0
  37. package/dist/browser/storage-leaf-5UTR5K7J.mjs +19829 -0
  38. package/dist/browser/store-leaf-3L33V35P.mjs +2070 -0
  39. package/dist/browser/suggestions-leaf-UBNDVKBD.mjs +1430 -0
  40. package/dist/browser/worker-copies-JXSGYR5X.mjs +2951 -0
  41. package/dist/{capabilities-BTTIs96E.d.ts → capabilities-DMte5K9_.d.ts} +162 -18
  42. package/dist/chunk-2AFZE7Y3.mjs +72 -0
  43. package/dist/chunk-2CKDCC65.mjs +47 -0
  44. package/dist/chunk-3ECIRFS5.mjs +61 -0
  45. package/dist/chunk-44W3CBNG.mjs +7 -0
  46. package/dist/chunk-4H4RG2FR.mjs +13 -0
  47. package/dist/chunk-4PY4JYMY.mjs +11 -0
  48. package/dist/chunk-4ZPVAHWF.mjs +1289 -0
  49. package/dist/chunk-5EI5CXO6.mjs +197 -0
  50. package/dist/chunk-5F5MXWS5.mjs +283 -0
  51. package/dist/{chunk-OHP7QR33.mjs → chunk-5FLRXF73.mjs} +25 -4
  52. package/dist/chunk-6FOENO4E.mjs +273 -0
  53. package/dist/chunk-6GHX35ZM.mjs +108 -0
  54. package/dist/chunk-6MSZ543Y.mjs +32 -0
  55. package/dist/chunk-6N6HHBCQ.mjs +4596 -0
  56. package/dist/{chunk-LXPIXDUJ.mjs → chunk-7P5YWDRQ.mjs} +403 -169
  57. package/dist/{chunk-TGP5M5R7.mjs → chunk-7T4LAYT4.mjs} +1 -1
  58. package/dist/{chunk-GGSH6KBQ.mjs → chunk-ASZLNVWW.mjs} +23 -3
  59. package/dist/chunk-AVBPB3MI.mjs +602 -0
  60. package/dist/{chunk-UCDF35PU.mjs → chunk-AVGUEEQ3.mjs} +494 -245
  61. package/dist/chunk-BUMREPLU.mjs +15 -0
  62. package/dist/chunk-BY6NALRK.mjs +31921 -0
  63. package/dist/chunk-C4ZMXXTL.mjs +92 -0
  64. package/dist/{chunk-2WYMG3OZ.mjs → chunk-C74KPRR7.mjs} +274 -19
  65. package/dist/{chunk-RSRVGAIQ.mjs → chunk-D456ZFPQ.mjs} +3 -2
  66. package/dist/chunk-DXZ6JDWG.mjs +797 -0
  67. package/dist/chunk-FJXJZUWF.mjs +209 -0
  68. package/dist/chunk-FS3HPPSC.mjs +212 -0
  69. package/dist/chunk-FT4KLZXW.mjs +1164 -0
  70. package/dist/chunk-GR5GMFJJ.mjs +104 -0
  71. package/dist/chunk-GXCDWLQI.mjs +193 -0
  72. package/dist/chunk-GXED6R3Q.mjs +271 -0
  73. package/dist/chunk-HLPRHOSO.mjs +17 -0
  74. package/dist/chunk-IL5DRCNU.mjs +3041 -0
  75. package/dist/{chunk-P36MUFEJ.mjs → chunk-JDDXV6UF.mjs} +2 -1
  76. package/dist/chunk-K5HSUPYS.mjs +11 -0
  77. package/dist/chunk-KYIRXTCA.mjs +100 -0
  78. package/dist/chunk-LFXS6OF7.mjs +289 -0
  79. package/dist/chunk-LNGE7SLY.mjs +151 -0
  80. package/dist/chunk-M2Z5WVWW.mjs +194 -0
  81. package/dist/{chunk-45AVKHMJ.mjs → chunk-M3B7SGJB.mjs} +63 -50
  82. package/dist/chunk-M3OW2XSP.mjs +40 -0
  83. package/dist/chunk-MFPSQZ6P.mjs +127 -0
  84. package/dist/chunk-MRCJBWJZ.mjs +1292 -0
  85. package/dist/chunk-MSIN4NM3.mjs +25 -0
  86. package/dist/chunk-MW3CJVGP.mjs +219 -0
  87. package/dist/chunk-MYM6CAVK.mjs +76 -0
  88. package/dist/chunk-NJJN4FMM.mjs +84 -0
  89. package/dist/chunk-NM7I3BM7.mjs +59 -0
  90. package/dist/chunk-OIJ64LGT.mjs +19 -0
  91. package/dist/{chunk-K7D4ZTSW.mjs → chunk-P4F4UWLC.mjs} +1 -1
  92. package/dist/chunk-Q2AP2FSN.mjs +15 -0
  93. package/dist/chunk-R2PJKSNL.mjs +54 -0
  94. package/dist/{chunk-HXJBSHXF.mjs → chunk-R724DJSG.mjs} +5056 -2619
  95. package/dist/chunk-S2POFTLN.mjs +10 -0
  96. package/dist/chunk-S5QMWECL.mjs +7 -0
  97. package/dist/chunk-SLAC5PAB.mjs +5008 -0
  98. package/dist/chunk-T4HMSZCX.mjs +5 -0
  99. package/dist/chunk-TACP6VK6.mjs +111 -0
  100. package/dist/chunk-TGPKS2UM.mjs +125 -0
  101. package/dist/chunk-TKKXPKEO.mjs +979 -0
  102. package/dist/chunk-V6EUBAIT.mjs +19 -0
  103. package/dist/chunk-XL56L4IR.mjs +228 -0
  104. package/dist/chunk-XQIAMDAG.mjs +3076 -0
  105. package/dist/chunk-YTUZ4R22.mjs +1063 -0
  106. package/dist/{chunk-KV3AZFT3.mjs → chunk-ZKOQR7EA.mjs} +45 -1
  107. package/dist/chunk-ZPJFGGGI.mjs +638 -0
  108. package/dist/chunk-ZXX5GZEX.mjs +17 -0
  109. package/dist/{client-i9eLJrw5.d.ts → client-BKqtl0_V.d.ts} +442 -22
  110. package/dist/client.d.ts +11 -10
  111. package/dist/client.mjs +8 -898
  112. package/dist/config-Bnl7L-0H.d.ts +608 -0
  113. package/dist/{config-P__Diiy4.d.ts → config-CJ-jfI0a.d.ts} +119 -9
  114. package/dist/core-leaf-6JVHEVFB.mjs +8 -0
  115. package/dist/custom-token-results-NXHBVY7U.mjs +59 -0
  116. package/dist/digest-leaf-KXBGKYHZ.mjs +1 -0
  117. package/dist/direct-executor-V3WOYU2Y.mjs +17 -0
  118. package/dist/distribution/coordinator/etcd.d.ts +63 -7
  119. package/dist/distribution/coordinator/etcd.mjs +725 -81
  120. package/dist/distribution/coordinator/in-memory.d.ts +3 -3
  121. package/dist/distribution/coordinator/in-memory.mjs +65 -10
  122. package/dist/distribution/transport/grpc.d.ts +2 -2
  123. package/dist/distribution/transport/grpc.mjs +39 -10
  124. package/dist/distribution/transport/in-memory.d.ts +2 -2
  125. package/dist/distribution/transport/in-memory.mjs +9 -6
  126. package/dist/distribution/transport/tcp.d.ts +2 -2
  127. package/dist/distribution/transport/tcp.mjs +20 -15
  128. package/dist/distribution.d.ts +267 -56
  129. package/dist/distribution.mjs +23156 -11362
  130. package/dist/durability-config-VWS2K24E.mjs +96 -0
  131. package/dist/embed-4PHHKGBK.mjs +263 -0
  132. package/dist/embeddings/openai.d.ts +1 -1
  133. package/dist/embeddings/openai.mjs +2 -2
  134. package/dist/{memory-CUEDwr-F.d.ts → engine-qjAxJSKk.d.ts} +2254 -1123
  135. package/dist/geo-index-2ZMMWWSK.mjs +1484 -0
  136. package/dist/gzip-2UCZW2MM.mjs +31 -0
  137. package/dist/{index-lcdGLyJH.d.ts → index-B_vmxpgW.d.ts} +260 -102
  138. package/dist/index.browser.mjs +4 -4
  139. package/dist/index.d.ts +2133 -57
  140. package/dist/index.mjs +16 -32
  141. package/dist/index.react-native.mjs +12 -0
  142. package/dist/invalidation/broadcast-channel.d.ts +1 -1
  143. package/dist/invalidation/filesystem.d.ts +1 -1
  144. package/dist/invalidation/filesystem.mjs +1 -2
  145. package/dist/invalidation/noop.d.ts +1 -1
  146. package/dist/languages/albanian.mjs +1 -1
  147. package/dist/languages/amharic.mjs +1 -1
  148. package/dist/languages/arabic.mjs +1 -1
  149. package/dist/languages/armenian.mjs +1 -1
  150. package/dist/languages/azerbaijani.mjs +1 -1
  151. package/dist/languages/bambara.mjs +1 -1
  152. package/dist/languages/basque.mjs +1 -1
  153. package/dist/languages/belarusian.mjs +1 -1
  154. package/dist/languages/bengali.mjs +1 -1
  155. package/dist/languages/bosnian.mjs +1 -1
  156. package/dist/languages/breton.mjs +1 -1
  157. package/dist/languages/bulgarian.mjs +1 -1
  158. package/dist/languages/burmese.mjs +1 -1
  159. package/dist/languages/catalan.mjs +1 -1
  160. package/dist/languages/chinese.mjs +1 -1
  161. package/dist/languages/croatian.mjs +1 -1
  162. package/dist/languages/czech.mjs +1 -1
  163. package/dist/languages/dagbani.mjs +1 -1
  164. package/dist/languages/danish.mjs +1 -1
  165. package/dist/languages/dutch.mjs +1 -1
  166. package/dist/languages/english.mjs +1 -1
  167. package/dist/languages/esperanto.mjs +1 -1
  168. package/dist/languages/estonian.mjs +1 -1
  169. package/dist/languages/ewe.mjs +1 -1
  170. package/dist/languages/faroese.mjs +1 -1
  171. package/dist/languages/fijian.mjs +1 -1
  172. package/dist/languages/finnish.mjs +1 -1
  173. package/dist/languages/french.mjs +1 -1
  174. package/dist/languages/ga.mjs +1 -1
  175. package/dist/languages/galician.mjs +1 -1
  176. package/dist/languages/georgian.mjs +1 -1
  177. package/dist/languages/german.mjs +1 -1
  178. package/dist/languages/greek.mjs +1 -1
  179. package/dist/languages/guarani.mjs +1 -1
  180. package/dist/languages/gujarati.mjs +1 -1
  181. package/dist/languages/haitian-creole.mjs +1 -1
  182. package/dist/languages/hausa.mjs +1 -1
  183. package/dist/languages/hawaiian.mjs +1 -1
  184. package/dist/languages/hebrew.mjs +1 -1
  185. package/dist/languages/hindi.mjs +1 -1
  186. package/dist/languages/hungarian.mjs +1 -1
  187. package/dist/languages/icelandic.mjs +1 -1
  188. package/dist/languages/igbo.mjs +1 -1
  189. package/dist/languages/indonesian.mjs +1 -1
  190. package/dist/languages/irish.mjs +1 -1
  191. package/dist/languages/italian.mjs +1 -1
  192. package/dist/languages/japanese.mjs +1 -1
  193. package/dist/languages/kannada.mjs +1 -1
  194. package/dist/languages/kazakh.mjs +1 -1
  195. package/dist/languages/khmer.mjs +1 -1
  196. package/dist/languages/kinyarwanda.mjs +1 -1
  197. package/dist/languages/kirundi.mjs +1 -1
  198. package/dist/languages/korean.mjs +1 -1
  199. package/dist/languages/kurmanji.mjs +1 -1
  200. package/dist/languages/kyrgyz.mjs +1 -1
  201. package/dist/languages/lao.mjs +1 -1
  202. package/dist/languages/latin.mjs +1 -1
  203. package/dist/languages/latvian.mjs +1 -1
  204. package/dist/languages/lingala.mjs +1 -1
  205. package/dist/languages/lithuanian.mjs +1 -1
  206. package/dist/languages/luxembourgish.mjs +1 -1
  207. package/dist/languages/macedonian.mjs +1 -1
  208. package/dist/languages/malagasy.mjs +1 -1
  209. package/dist/languages/malay.mjs +1 -1
  210. package/dist/languages/malayalam.mjs +1 -1
  211. package/dist/languages/maltese.mjs +1 -1
  212. package/dist/languages/maori.mjs +1 -1
  213. package/dist/languages/marathi.mjs +1 -1
  214. package/dist/languages/nepali.mjs +1 -1
  215. package/dist/languages/norwegian.mjs +1 -1
  216. package/dist/languages/oromo.mjs +1 -1
  217. package/dist/languages/persian.mjs +1 -1
  218. package/dist/languages/polish.mjs +1 -1
  219. package/dist/languages/portuguese.mjs +1 -1
  220. package/dist/languages/punjabi.mjs +1 -1
  221. package/dist/languages/romanian.mjs +1 -1
  222. package/dist/languages/russian.mjs +1 -1
  223. package/dist/languages/samoan.mjs +1 -1
  224. package/dist/languages/sanskrit.mjs +1 -1
  225. package/dist/languages/scottish-gaelic.mjs +1 -1
  226. package/dist/languages/serbian.mjs +1 -1
  227. package/dist/languages/shona.mjs +1 -1
  228. package/dist/languages/sinhala.mjs +1 -1
  229. package/dist/languages/slovak.mjs +1 -1
  230. package/dist/languages/slovenian.mjs +1 -1
  231. package/dist/languages/sorani.mjs +1 -1
  232. package/dist/languages/spanish.mjs +1 -1
  233. package/dist/languages/swahili.mjs +1 -1
  234. package/dist/languages/swedish.mjs +1 -1
  235. package/dist/languages/tagalog.mjs +1 -1
  236. package/dist/languages/tamil.mjs +1 -1
  237. package/dist/languages/tatar.mjs +1 -1
  238. package/dist/languages/telugu.mjs +1 -1
  239. package/dist/languages/thai.mjs +1 -1
  240. package/dist/languages/tibetan.mjs +1 -1
  241. package/dist/languages/tigrinya.mjs +1 -1
  242. package/dist/languages/tongan.mjs +1 -1
  243. package/dist/languages/turkish.mjs +1 -1
  244. package/dist/languages/twi.mjs +1 -1
  245. package/dist/languages/ukrainian.mjs +1 -1
  246. package/dist/languages/urdu.mjs +1 -1
  247. package/dist/languages/vietnamese.mjs +1 -1
  248. package/dist/languages/welsh.mjs +1 -1
  249. package/dist/languages/wolof.mjs +1 -1
  250. package/dist/languages/xhosa.mjs +1 -1
  251. package/dist/languages/yoruba.mjs +1 -1
  252. package/dist/languages/zulu.mjs +1 -1
  253. package/dist/long-query-thread-UCRK7MSI.mjs +154 -0
  254. package/dist/module-5ZSLUQII.mjs +3729 -0
  255. package/dist/msgpack-leaf-H5ZYW7S7.mjs +9 -0
  256. package/dist/optional-reads-5HFMF2QZ.mjs +3373 -0
  257. package/dist/optional-writes-4UOQKETU.mjs +247 -0
  258. package/dist/paged-partition-archive-BBRLZCIF.mjs +253 -0
  259. package/dist/partial-words.browser.mjs +1650 -0
  260. package/dist/partial-words.mjs +17 -11
  261. package/dist/{chunk-ASY5PHYI.mjs → pattern.browser.mjs} +253 -311
  262. package/dist/pattern.mjs +6 -7
  263. package/dist/persistence/durability/checkpoint-worker.d.ts +93 -25
  264. package/dist/persistence/durability/checkpoint-worker.mjs +51 -41
  265. package/dist/phrase-module-EFESWOCR.mjs +635 -0
  266. package/dist/query-actions-HMJUVWGT.mjs +119 -0
  267. package/dist/query-suggestions/merge-worker.d.ts +55 -0
  268. package/dist/query-suggestions/merge-worker.mjs +53 -0
  269. package/dist/query-suggestions-CBYkrQ5T.d.ts +311 -0
  270. package/dist/query-suggestions.browser.mjs +47 -0
  271. package/dist/query-suggestions.d.ts +20 -0
  272. package/dist/query-suggestions.mjs +19 -0
  273. package/dist/react-native/chunks/chunk-3OEFSHVV.mjs +970 -0
  274. package/dist/{browser/chunk-RELZRJNR.mjs → react-native/chunks/chunk-43R4GCM7.mjs} +49 -50
  275. package/dist/react-native/chunks/chunk-AKRYHAVG.mjs +6729 -0
  276. package/dist/react-native/chunks/chunk-CW2ZFXSE.mjs +14 -0
  277. package/dist/react-native/chunks/chunk-G3WCDTOF.mjs +29640 -0
  278. package/dist/react-native/chunks/chunk-GXXHHA2T.mjs +212 -0
  279. package/dist/react-native/chunks/chunk-H3MDVPBO.mjs +151 -0
  280. package/dist/react-native/chunks/chunk-HQEVQEDJ.mjs +52 -0
  281. package/dist/react-native/chunks/chunk-HRDMIQ63.mjs +131 -0
  282. package/dist/react-native/chunks/chunk-HTBY4GON.mjs +565 -0
  283. package/dist/react-native/chunks/chunk-LX62Y4B5.mjs +61 -0
  284. package/dist/react-native/chunks/chunk-MRH36RN3.mjs +902 -0
  285. package/dist/react-native/chunks/chunk-Q32DDFKX.mjs +19 -0
  286. package/dist/react-native/chunks/chunk-UE4ANHSX.mjs +127 -0
  287. package/dist/react-native/chunks/chunk-UNZPWLVL.mjs +1063 -0
  288. package/dist/react-native/chunks/chunk-UO44MA5M.mjs +232 -0
  289. package/dist/react-native/chunks/chunk-UYKIV2KT.mjs +42 -0
  290. package/dist/react-native/chunks/chunk-VUJCSQG5.mjs +11 -0
  291. package/dist/react-native/chunks/chunk-WVHF4ZAK.mjs +802 -0
  292. package/dist/react-native/chunks/chunk-ZUJCUPVS.mjs +54 -0
  293. package/dist/react-native/chunks/core-leaf-S2KNBWDY.mjs +7 -0
  294. package/dist/react-native/chunks/custom-token-results-3OZEMRE7.mjs +59 -0
  295. package/dist/react-native/chunks/digest-leaf-X6OIFDIU.mjs +1 -0
  296. package/dist/react-native/chunks/durability-config-FCSD4MXY.mjs +96 -0
  297. package/dist/react-native/chunks/embed-HA2I7RCF.mjs +263 -0
  298. package/dist/react-native/chunks/geo-index-QEYKEVPH.mjs +1484 -0
  299. package/dist/react-native/chunks/gzip.react-native-CVM4NQG2.mjs +12 -0
  300. package/dist/react-native/chunks/module-FMBX75GP.mjs +5209 -0
  301. package/dist/react-native/chunks/msgpack-leaf-JMNGSTRW.mjs +7 -0
  302. package/dist/react-native/chunks/optional-reads-AFPO2FEB.mjs +3364 -0
  303. package/dist/react-native/chunks/optional-writes-XOH25XZI.mjs +296 -0
  304. package/dist/react-native/chunks/phrase-module-IGYBK7XH.mjs +636 -0
  305. package/dist/react-native/chunks/replace-IMZUVUOE.mjs +57 -0
  306. package/dist/react-native/chunks/resolve-vector-text-GE2CRJKI.mjs +84 -0
  307. package/dist/react-native/chunks/restore-queue-LLNMYB4Z.mjs +187 -0
  308. package/dist/react-native/chunks/runtime-UOHD2XUJ.mjs +615 -0
  309. package/dist/react-native/chunks/segment-codec-leaf-VR6O5GLC.mjs +13 -0
  310. package/dist/react-native/chunks/segment-merge-leaf-LLMWU4EW.mjs +14 -0
  311. package/dist/react-native/chunks/storage-leaf-JD7PIDO4.mjs +19633 -0
  312. package/dist/react-native/chunks/store-leaf-4UVCMYGN.mjs +15 -0
  313. package/dist/react-native/chunks/suggestions-leaf-J5VT5RHY.mjs +1321 -0
  314. package/dist/react-native/chunks/worker-copies-ZYHY7FKJ.mjs +2831 -0
  315. package/dist/react-native/partial-words.mjs +1658 -0
  316. package/dist/react-native/pattern.mjs +2570 -0
  317. package/dist/react-native/query-suggestions.mjs +55 -0
  318. package/dist/react-native/sync.mjs +2759 -0
  319. package/dist/react-native/synonyms.mjs +676 -0
  320. package/dist/react.d.ts +12 -8
  321. package/dist/react.mjs +45 -1
  322. package/dist/{rerank-NWTKK636.mjs → rerank-RI2PMNOD.mjs} +22 -12
  323. package/dist/rerankers/http.d.ts +1 -1
  324. package/dist/rerankers/http.mjs +2 -2
  325. package/dist/resolve-vector-text-OD4J3WHF.mjs +3 -0
  326. package/dist/restore-queue-PCKWDAKQ.mjs +192 -0
  327. package/dist/runtime-USQYKN3I.mjs +615 -0
  328. package/dist/{schema-4W_7tk1f.d.ts → schema-CzG9OUw5.d.ts} +70 -30
  329. package/dist/segment-codec-leaf-QJM7I6QG.mjs +18 -0
  330. package/dist/segment-merge-leaf-4BJGIJHY.mjs +18 -0
  331. package/dist/server.d.ts +348 -22
  332. package/dist/server.mjs +2211 -327
  333. package/dist/storage-leaf-GRRRI7UV.mjs +5962 -0
  334. package/dist/store-leaf-6VMMSVBE.mjs +20 -0
  335. package/dist/stored-values-BWIMpiT_.d.ts +25 -0
  336. package/dist/suggestions-leaf-4KPTMPTL.mjs +54 -0
  337. package/dist/sync/filesystem.d.ts +30 -0
  338. package/dist/sync/filesystem.mjs +288 -0
  339. package/dist/sync/indexeddb.browser.mjs +287 -0
  340. package/dist/sync/indexeddb.d.ts +30 -0
  341. package/dist/sync/indexeddb.mjs +242 -0
  342. package/dist/sync.browser.mjs +1271 -0
  343. package/dist/sync.d.ts +23 -0
  344. package/dist/sync.mjs +2712 -0
  345. package/dist/synonyms.browser.mjs +658 -0
  346. package/dist/synonyms.mjs +17 -7
  347. package/dist/tasks-65b6t6lk.d.ts +782 -0
  348. package/dist/thread-UJ3W5LVI.mjs +31 -0
  349. package/dist/{types-B3b68nfF.d.ts → types-Ck97nB3D.d.ts} +1 -1
  350. package/dist/types-CpCg-Gq-.d.ts +227 -0
  351. package/dist/{types-Cb9kiY5P.d.ts → types-DvncmvuQ.d.ts} +12 -1
  352. package/dist/vector/search-worker.d.ts +10 -2
  353. package/dist/vector/search-worker.mjs +11 -11
  354. package/dist/worker-copies-YSXZXIGC.mjs +2232 -0
  355. package/dist/workers/entry.mjs +75 -15
  356. package/openapi.json +6840 -0
  357. package/package.json +406 -19
  358. package/dist/browser/chunk-CN4I4FZU.mjs +0 -602
  359. package/dist/browser/chunk-HOZW3PKP.mjs +0 -9
  360. package/dist/browser/chunk-JPTQZWJQ.mjs +0 -10
  361. package/dist/browser/chunk-LD55ZGU3.mjs +0 -347
  362. package/dist/browser/chunk-NAFT3K6X.mjs +0 -24546
  363. package/dist/browser/chunk-NNG3EEB6.mjs +0 -2728
  364. package/dist/browser/chunk-QWES44AW.mjs +0 -473
  365. package/dist/browser/durability-wiring-PY3L7DES.mjs +0 -3679
  366. package/dist/browser/module-PDXYHT75.mjs +0 -2822
  367. package/dist/browser/release-76MXRPGV.mjs +0 -33
  368. package/dist/browser/restore-queue-DVIYJ7RU.mjs +0 -35
  369. package/dist/browser/snapshot-J57MZK6L.mjs +0 -1207
  370. package/dist/browser/write-L7FAQA47.mjs +0 -208
  371. package/dist/chunk-37CD3OKS.mjs +0 -87
  372. package/dist/chunk-5XNIRLFC.mjs +0 -96
  373. package/dist/chunk-7X5JST5L.mjs +0 -114
  374. package/dist/chunk-AV4IJKD2.mjs +0 -35
  375. package/dist/chunk-AY3AQ3UH.mjs +0 -73
  376. package/dist/chunk-BKB3XTMI.mjs +0 -707
  377. package/dist/chunk-C7SLA6CM.mjs +0 -19
  378. package/dist/chunk-COVCQ73F.mjs +0 -587
  379. package/dist/chunk-DJHHF2L5.mjs +0 -17
  380. package/dist/chunk-E74YLAIG.mjs +0 -24
  381. package/dist/chunk-FMWRCP2C.mjs +0 -57
  382. package/dist/chunk-GES7U2PM.mjs +0 -68
  383. package/dist/chunk-HJSN7UYG.mjs +0 -10
  384. package/dist/chunk-JFRNNBOM.mjs +0 -35
  385. package/dist/chunk-K25LZA54.mjs +0 -8484
  386. package/dist/chunk-KEHK2O6P.mjs +0 -13
  387. package/dist/chunk-KOKM4PQN.mjs +0 -52
  388. package/dist/chunk-KZ3NX2P7.mjs +0 -21
  389. package/dist/chunk-L3L5YFXC.mjs +0 -614
  390. package/dist/chunk-L5ORED7V.mjs +0 -25
  391. package/dist/chunk-LH4NZNJJ.mjs +0 -1731
  392. package/dist/chunk-LU2CE5AE.mjs +0 -507
  393. package/dist/chunk-LXWDQLTW.mjs +0 -30
  394. package/dist/chunk-MHOSCQCQ.mjs +0 -948
  395. package/dist/chunk-NKRBEGJZ.mjs +0 -1705
  396. package/dist/chunk-NTRMFARG.mjs +0 -462
  397. package/dist/chunk-NW4XDSP2.mjs +0 -212
  398. package/dist/chunk-OOEXLUGR.mjs +0 -117
  399. package/dist/chunk-ORBLD3EL.mjs +0 -6
  400. package/dist/chunk-OWNZYMRE.mjs +0 -3591
  401. package/dist/chunk-PPY2GSIB.mjs +0 -44
  402. package/dist/chunk-QY7GC2MW.mjs +0 -33
  403. package/dist/chunk-R6A773WK.mjs +0 -226
  404. package/dist/chunk-R6PR4WYZ.mjs +0 -68
  405. package/dist/chunk-R7EEKAJE.mjs +0 -593
  406. package/dist/chunk-RONEOEON.mjs +0 -426
  407. package/dist/chunk-VWULPWRJ.mjs +0 -7469
  408. package/dist/chunk-WCJKUGHB.mjs +0 -262
  409. package/dist/chunk-WQBCBKY3.mjs +0 -8
  410. package/dist/chunk-X5CV2E2B.mjs +0 -1
  411. package/dist/chunk-YA7XLE2R.mjs +0 -1256
  412. package/dist/chunk-YTJCGKM4.mjs +0 -56
  413. package/dist/chunk-ZG477IPV.mjs +0 -526
  414. package/dist/chunk-ZYNAMIYX.mjs +0 -113
  415. package/dist/config-BGB_fDtq.d.ts +0 -237
  416. package/dist/direct-executor-CXZ66ACP.mjs +0 -22
  417. package/dist/durability-wiring-QTHOGFMZ.mjs +0 -3229
  418. package/dist/engine-BGCEm9E_.d.ts +0 -577
  419. package/dist/module-3NONKBZX.mjs +0 -1860
  420. package/dist/release-BH3DKW3T.mjs +0 -33
  421. package/dist/restore-queue-JPRBUYII.mjs +0 -35
  422. package/dist/runtime-ZKYZFJJC.mjs +0 -177
  423. package/dist/snapshot-HOGXNO5W.mjs +0 -793
  424. package/dist/tasks-HG3rlxx6.d.ts +0 -302
  425. package/dist/thread-B657LQXK.mjs +0 -692
  426. package/dist/write-XLIGTXBU.mjs +0 -238
@@ -0,0 +1,2759 @@
1
+ import { isSyncEpoch, hasLanguage, hasTokenizer, hasStopWords, readHeapStatistics, loadStorageCode, getLanguage } from './chunks/chunk-G3WCDTOF.mjs';
2
+ import './chunks/chunk-HQEVQEDJ.mjs';
3
+ import './chunks/chunk-AKRYHAVG.mjs';
4
+ import './chunks/chunk-LX62Y4B5.mjs';
5
+ import './chunks/chunk-HTBY4GON.mjs';
6
+ import './chunks/chunk-UYKIV2KT.mjs';
7
+ import './chunks/chunk-CW2ZFXSE.mjs';
8
+ import './chunks/chunk-UO44MA5M.mjs';
9
+ import { loadMessagePack, loadedMessagePack, requireLoadedMessagePack, getNestedValue, extractVectorFieldsFromSchema } from './chunks/chunk-MRH36RN3.mjs';
10
+ import './chunks/chunk-HRDMIQ63.mjs';
11
+ import './chunks/chunk-VUJCSQG5.mjs';
12
+ import { NarsilError, ErrorCodes, ClientErrorCodes, ServerErrorCodes, describeError } from './chunks/chunk-43R4GCM7.mjs';
13
+
14
+ // src/json-encoding.ts
15
+ function toPlainNumbers(key, value) {
16
+ if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
17
+ return Array.from(value);
18
+ }
19
+ return value;
20
+ }
21
+ function encodeJson(value) {
22
+ return JSON.stringify(value, toPlainNumbers);
23
+ }
24
+
25
+ // src/client/constants.ts
26
+ var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
27
+ var NO_TIMEOUT = 0;
28
+ var DEFAULT_TASK_POLL_INTERVAL_MS = 250;
29
+ var DEFAULT_URL_SKIP_MS = 1e3;
30
+ var DEFAULT_MAX_URL_SKIP_MS = 3e4;
31
+ var MAX_VECTOR_FIELD_PATH_SEGMENTS = 64;
32
+ var MAX_CHANGE_DOCUMENT_DEPTH = 64;
33
+
34
+ // src/client/paths.ts
35
+ function indexPath(indexName) {
36
+ return `/indexes/${encodeURIComponent(indexName)}`;
37
+ }
38
+ function documentPath(indexName, docId) {
39
+ return `${indexPath(indexName)}/documents/${encodeURIComponent(docId)}`;
40
+ }
41
+ function socketUrlOf(base, path) {
42
+ const location = globalThis.location?.href;
43
+ let parsed;
44
+ try {
45
+ parsed = location === void 0 ? new URL(base) : new URL(base, location);
46
+ } catch {
47
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, `The client cannot resolve "${base}" to a server`, { url: base });
48
+ }
49
+ parsed.protocol = parsed.protocol === "https:" ? "wss:" : "ws:";
50
+ parsed.search = "";
51
+ parsed.hash = "";
52
+ return `${parsed.toString().replace(/\/+$/, "")}${path}`;
53
+ }
54
+ function backupPath(backupName) {
55
+ return `/backups/${encodeURIComponent(backupName)}`;
56
+ }
57
+ function taskPath(taskId) {
58
+ return `/tasks/${encodeURIComponent(taskId)}`;
59
+ }
60
+
61
+ // src/client/response-shape.ts
62
+ function isRecord(value) {
63
+ return typeof value === "object" && value !== null && !Array.isArray(value);
64
+ }
65
+ function malformed(path, expected) {
66
+ return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, `The answer from ${path} holds no ${expected}`, {
67
+ route: path
68
+ });
69
+ }
70
+ function readArray(payload, key, path) {
71
+ if (!isRecord(payload) || !Array.isArray(payload[key])) throw malformed(path, `"${key}" array`);
72
+ return payload[key];
73
+ }
74
+ function readNumber(payload, key, path) {
75
+ if (!isRecord(payload) || typeof payload[key] !== "number") throw malformed(path, `"${key}" number`);
76
+ return payload[key];
77
+ }
78
+ function readBoolean(payload, key, path) {
79
+ if (!isRecord(payload) || typeof payload[key] !== "boolean") throw malformed(path, `"${key}" flag`);
80
+ return payload[key];
81
+ }
82
+ function readString(payload, key, path) {
83
+ if (!isRecord(payload) || typeof payload[key] !== "string") throw malformed(path, `"${key}" string`);
84
+ return payload[key];
85
+ }
86
+ function readObject(payload, key, path) {
87
+ if (!isRecord(payload) || !isRecord(payload[key])) throw malformed(path, `"${key}" object`);
88
+ return payload[key];
89
+ }
90
+ function readBody(payload, path) {
91
+ if (!isRecord(payload)) throw malformed(path, "JSON object");
92
+ return payload;
93
+ }
94
+
95
+ // src/client/admin.ts
96
+ function createAdminOperations(transport) {
97
+ async function postJson(path, body, options) {
98
+ const payload = await transport.json({
99
+ method: "POST",
100
+ path,
101
+ body: encodeJson(body),
102
+ contentType: "application/json",
103
+ options
104
+ });
105
+ return readBody(payload, path);
106
+ }
107
+ return {
108
+ async checkpoint(indexName, options) {
109
+ await transport.json({ method: "POST", path: `${indexPath(indexName)}/_checkpoint`, options });
110
+ },
111
+ snapshot(indexName, options) {
112
+ const { queryHistory, ...request } = options ?? {};
113
+ return transport.stream({
114
+ method: "GET",
115
+ path: `${indexPath(indexName)}/snapshot`,
116
+ ...queryHistory === true ? { query: { queryHistory: "true" } } : {},
117
+ defaultTimeoutMs: NO_TIMEOUT,
118
+ options: request
119
+ });
120
+ },
121
+ async restore(indexName, data, options) {
122
+ const path = `${indexPath(indexName)}/restore`;
123
+ const payload = await transport.json({
124
+ method: "POST",
125
+ path,
126
+ body: data,
127
+ contentType: "application/octet-stream",
128
+ defaultTimeoutMs: NO_TIMEOUT,
129
+ options
130
+ });
131
+ return readBody(payload, path);
132
+ },
133
+ exportDocuments(indexName, options) {
134
+ return transport.stream({
135
+ method: "GET",
136
+ path: `${indexPath(indexName)}/export`,
137
+ defaultTimeoutMs: NO_TIMEOUT,
138
+ options
139
+ });
140
+ },
141
+ async importDocumentExport(indexName, data, options) {
142
+ const path = `${indexPath(indexName)}/import`;
143
+ const payload = await transport.json({
144
+ method: "POST",
145
+ path,
146
+ body: data,
147
+ contentType: "application/x-ndjson",
148
+ defaultTimeoutMs: NO_TIMEOUT,
149
+ options
150
+ });
151
+ return readBody(payload, path);
152
+ },
153
+ async vectorMaintenanceStatus(indexName, options) {
154
+ const path = `${indexPath(indexName)}/vector-maintenance`;
155
+ const payload = await transport.json({ method: "GET", path, options });
156
+ return readArray(payload, "fields", path);
157
+ },
158
+ async compactVectors(indexName, fieldName, options) {
159
+ await postJson(
160
+ `${indexPath(indexName)}/vectors/_compact`,
161
+ fieldName === void 0 ? {} : { field: fieldName },
162
+ options
163
+ );
164
+ },
165
+ optimizeVectors(indexName, fieldName, options) {
166
+ return postJson(
167
+ `${indexPath(indexName)}/vectors/_optimize`,
168
+ fieldName === void 0 ? {} : { field: fieldName },
169
+ options
170
+ );
171
+ },
172
+ rebalance(indexName, targetPartitionCount, options) {
173
+ return postJson(`${indexPath(indexName)}/_rebalance`, { targetPartitionCount }, options);
174
+ },
175
+ async updatePartitionConfig(indexName, partitionConfig, options) {
176
+ await postJson(`${indexPath(indexName)}/partition-config`, partitionConfig, options);
177
+ },
178
+ async updateSynonyms(indexName, synonyms, options) {
179
+ await postJson(`${indexPath(indexName)}/synonyms`, { synonyms }, options);
180
+ },
181
+ async updateBM25(indexName, bm25, options) {
182
+ await postJson(`${indexPath(indexName)}/bm25`, bm25, options);
183
+ },
184
+ async getSynonyms(indexName, options) {
185
+ const path = `${indexPath(indexName)}/synonyms`;
186
+ const body = await readBody(
187
+ await transport.json({ method: "GET", path, options }),
188
+ path
189
+ );
190
+ return body.synonyms;
191
+ },
192
+ async rebuildAnalysis(indexName, options) {
193
+ const path = `${indexPath(indexName)}/_rebuild-analysis`;
194
+ return readBody(await transport.json({ method: "POST", path, options }), path);
195
+ },
196
+ async changeAnalysis(indexName, change, options) {
197
+ const path = `${indexPath(indexName)}/_change-analysis`;
198
+ return postJson(path, change, options);
199
+ },
200
+ async getMemoryStats(options) {
201
+ const path = "/stats/memory";
202
+ return readBody(await transport.json({ method: "GET", path, options }), path);
203
+ }
204
+ };
205
+ }
206
+
207
+ // src/client/backups.ts
208
+ function createBackupOperations(transport) {
209
+ async function postTask(path, body, options) {
210
+ const payload = await transport.json({
211
+ method: "POST",
212
+ path,
213
+ body: encodeJson(body),
214
+ contentType: "application/json",
215
+ options
216
+ });
217
+ return readBody(payload, path);
218
+ }
219
+ return {
220
+ createBackup(indexName, backupName, options) {
221
+ const { queryHistory, ...request } = options ?? {};
222
+ const body = { backup: backupName, ...queryHistory === true ? { queryHistory } : {} };
223
+ return postTask(`${indexPath(indexName)}/backups`, body, request);
224
+ },
225
+ async listBackups(options) {
226
+ const path = "/backups";
227
+ const payload = await transport.json({ method: "GET", path, defaultTimeoutMs: NO_TIMEOUT, options });
228
+ return readArray(payload, "backups", path);
229
+ },
230
+ restoreBackup(backupName, restore, options) {
231
+ return postTask(`${backupPath(backupName)}/_restore`, restore, options);
232
+ },
233
+ async deleteBackup(backupName, options) {
234
+ const path = backupPath(backupName);
235
+ return readBody(await transport.json({ method: "DELETE", path, options }), path);
236
+ }
237
+ };
238
+ }
239
+
240
+ // src/client/bulk.ts
241
+ function toNdjson(source) {
242
+ if (typeof source === "string") return source;
243
+ if (Array.isArray(source)) return source.map((document) => encodeJson(document)).join("\n");
244
+ return source;
245
+ }
246
+ function toBatchResult(payload, path) {
247
+ const failed = readArray(payload, "failed", path);
248
+ return {
249
+ succeeded: readArray(payload, "succeeded", path),
250
+ failed: failed.map((entry) => {
251
+ const record2 = readBody(entry, path);
252
+ const docId = typeof record2.docId === "string" ? record2.docId : "";
253
+ const error = readBody(record2.error, path);
254
+ const code = typeof error.code === "string" ? error.code : ServerErrorCodes.INTERNAL_ERROR;
255
+ const message = typeof error.message === "string" ? error.message : "The server refused the document";
256
+ const details = typeof error.details === "object" && error.details !== null ? error.details : void 0;
257
+ return { docId, error: new NarsilError(code, message, details) };
258
+ })
259
+ };
260
+ }
261
+ function createBulkOperations(transport) {
262
+ function batch(indexName, body, options) {
263
+ const path = `${indexPath(indexName)}/documents/_batch`;
264
+ return transport.json({ method: "POST", path, body: encodeJson(body), contentType: "application/json", options }).then((payload) => toBatchResult(payload, path));
265
+ }
266
+ function importBody(indexName, source, asynchronous, options) {
267
+ return {
268
+ method: "POST",
269
+ path: `${indexPath(indexName)}/documents/_import`,
270
+ ...asynchronous ? { query: { async: "true" } } : {},
271
+ body: toNdjson(source),
272
+ contentType: "application/x-ndjson",
273
+ defaultTimeoutMs: NO_TIMEOUT,
274
+ options
275
+ };
276
+ }
277
+ return {
278
+ insertBatch(indexName, documents, insertOptions, options) {
279
+ return batch(
280
+ indexName,
281
+ { action: "insert", documents, ...insertOptions === void 0 ? {} : { options: insertOptions } },
282
+ options
283
+ );
284
+ },
285
+ updateBatch(indexName, updates, writeOptions, options) {
286
+ return batch(
287
+ indexName,
288
+ { action: "update", updates, ...writeOptions === void 0 ? {} : { options: writeOptions } },
289
+ options
290
+ );
291
+ },
292
+ removeBatch(indexName, docIds, writeOptions, options) {
293
+ return batch(
294
+ indexName,
295
+ { action: "delete", docIds, ...writeOptions === void 0 ? {} : { options: writeOptions } },
296
+ options
297
+ );
298
+ },
299
+ async getMultiple(indexName, docIds, options) {
300
+ const path = `${indexPath(indexName)}/documents/_multi-get`;
301
+ const payload = await transport.json({
302
+ method: "POST",
303
+ path,
304
+ body: encodeJson({ docIds }),
305
+ contentType: "application/json",
306
+ repeatable: true,
307
+ options
308
+ });
309
+ const documents = readObject(payload, "documents", path);
310
+ return new Map(Object.entries(documents));
311
+ },
312
+ async listDocuments(indexName, params, options) {
313
+ const path = `${indexPath(indexName)}/documents/_list`;
314
+ const payload = await transport.json({
315
+ method: "POST",
316
+ path,
317
+ body: encodeJson(params ?? {}),
318
+ contentType: "application/json",
319
+ repeatable: true,
320
+ options
321
+ });
322
+ return readBody(payload, path);
323
+ },
324
+ async importDocuments(indexName, source, options) {
325
+ const spec = importBody(indexName, source, false, options);
326
+ return readBody(await transport.json(spec), spec.path);
327
+ },
328
+ async startImport(indexName, source, options) {
329
+ const spec = importBody(indexName, source, true, options);
330
+ return readBody(await transport.json(spec), spec.path);
331
+ }
332
+ };
333
+ }
334
+
335
+ // src/changes/wire.ts
336
+ var CHANGES_SUBPROTOCOL = "narsil.changes.v1";
337
+
338
+ // src/client/change-feed-messages.ts
339
+ var CHANGE_TYPES = /* @__PURE__ */ new Set(["put", "insert", "update", "remove"]);
340
+ var RESYNC_REASONS = /* @__PURE__ */ new Set(["epoch", "history", "ahead"]);
341
+ var EPOCH = /^[0-9a-f]{32}$/;
342
+ var FLOAT32_BYTES = 4;
343
+ function malformed2(detail) {
344
+ return new NarsilError(
345
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
346
+ `The change feed sent a malformed message: ${detail}`
347
+ );
348
+ }
349
+ function isRecord2(value) {
350
+ if (value === null || typeof value !== "object") return false;
351
+ const prototype = Object.getPrototypeOf(value);
352
+ return prototype === Object.prototype || prototype === null;
353
+ }
354
+ function isCount(value) {
355
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
356
+ }
357
+ function positionsOf(raw, partitionCount, field) {
358
+ if (!Array.isArray(raw) || raw.length !== partitionCount) throw malformed2(`${field} must hold each partition once`);
359
+ const seen = /* @__PURE__ */ new Set();
360
+ const positions = [];
361
+ for (const item of raw) {
362
+ if (!isRecord2(item) || !isCount(item.partitionId) || !isCount(item.seqNo)) throw malformed2(`${field} is invalid`);
363
+ if (item.partitionId >= partitionCount || seen.has(item.partitionId)) {
364
+ throw malformed2(`${field} must hold each partition once`);
365
+ }
366
+ seen.add(item.partitionId);
367
+ positions.push({ partitionId: item.partitionId, seqNo: item.seqNo });
368
+ }
369
+ return positions;
370
+ }
371
+ function vectorOf(bytes) {
372
+ if (bytes.byteLength % FLOAT32_BYTES !== 0) throw malformed2("a vector has a length that is not whole floats");
373
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
374
+ const vector = new Float32Array(bytes.byteLength / FLOAT32_BYTES);
375
+ for (let at = 0; at < vector.length; at++) vector[at] = view.getFloat32(at * FLOAT32_BYTES, true);
376
+ return vector;
377
+ }
378
+ function restoreVectorAt(document, path) {
379
+ let holder = document;
380
+ for (let at = 0; at < path.length - 1; at++) {
381
+ const segment = path[at];
382
+ if (segment === void 0 || !Object.hasOwn(holder, segment)) return;
383
+ const next = holder[segment];
384
+ if (!isRecord2(next)) return;
385
+ holder = next;
386
+ }
387
+ const leaf = path.at(-1);
388
+ if (leaf === void 0 || !Object.hasOwn(holder, leaf)) return;
389
+ const value = holder[leaf];
390
+ if (!(value instanceof Uint8Array)) return;
391
+ Object.defineProperty(holder, leaf, { value: vectorOf(value), enumerable: true, writable: true, configurable: true });
392
+ }
393
+ function requireShallow(document) {
394
+ const pending = [{ value: document, depth: 0 }];
395
+ for (let next = pending.pop(); next !== void 0; next = pending.pop()) {
396
+ const { value, depth } = next;
397
+ if (depth > MAX_CHANGE_DOCUMENT_DEPTH) throw malformed2("a document nests too deeply");
398
+ const children = Array.isArray(value) ? value : isRecord2(value) ? Object.values(value) : null;
399
+ if (children === null) continue;
400
+ for (const child of children) {
401
+ if (Array.isArray(child) || isRecord2(child)) pending.push({ value: child, depth: depth + 1 });
402
+ }
403
+ }
404
+ }
405
+ function documentOf(raw, field, vectorFields) {
406
+ if (!isRecord2(raw)) throw malformed2(`${field} must be a map`);
407
+ requireShallow(raw);
408
+ for (const path of vectorFields) restoreVectorAt(raw, path);
409
+ return raw;
410
+ }
411
+ function changeOf(raw, vectorFields) {
412
+ if (!isRecord2(raw) || !isCount(raw.seqNo) || typeof raw.documentId !== "string")
413
+ throw malformed2("a change is invalid");
414
+ if (typeof raw.type !== "string" || !CHANGE_TYPES.has(raw.type)) throw malformed2("a change has an unknown type");
415
+ const change = { seqNo: raw.seqNo, type: raw.type, documentId: raw.documentId };
416
+ if (raw.document !== void 0 && raw.document !== null) {
417
+ change.document = documentOf(raw.document, "document", vectorFields);
418
+ }
419
+ if (raw.previousDocument !== void 0 && raw.previousDocument !== null) {
420
+ change.previousDocument = documentOf(raw.previousDocument, "previousDocument", vectorFields);
421
+ }
422
+ return change;
423
+ }
424
+ function vectorFieldsOf(raw) {
425
+ if (!Array.isArray(raw)) throw malformed2("vectorFields must be a list of paths");
426
+ const paths = [];
427
+ for (const item of raw) {
428
+ if (typeof item !== "string") throw malformed2("vectorFields must be a list of paths");
429
+ const path = item.split(".");
430
+ if (path.length > MAX_VECTOR_FIELD_PATH_SEGMENTS || path.includes("")) {
431
+ throw malformed2(`the vector field path ${item} is invalid`);
432
+ }
433
+ paths.push(path);
434
+ }
435
+ return paths;
436
+ }
437
+ function resyncOf(raw) {
438
+ if (typeof raw.reason !== "string" || !RESYNC_REASONS.has(raw.reason)) throw malformed2("the resync reason is unknown");
439
+ return { type: "resync", reason: raw.reason };
440
+ }
441
+ function firstEventOf(raw) {
442
+ if (!isRecord2(raw)) throw malformed2("a message must be a map");
443
+ if (raw.type === "resync") return { event: resyncOf(raw), vectorFields: [] };
444
+ if (raw.type !== "subscribed") throw malformed2("the first message must be subscribed or resync");
445
+ const { epoch, partitionCount, bytesBehind, windowBytes, previousDocument } = raw;
446
+ if (typeof epoch !== "string" || !EPOCH.test(epoch)) throw malformed2("the epoch is invalid");
447
+ if (!isCount(partitionCount) || partitionCount === 0) throw malformed2("the partition count is invalid");
448
+ if (!isCount(bytesBehind) || !isCount(windowBytes) || windowBytes === 0) throw malformed2("a byte count is invalid");
449
+ if (typeof previousDocument !== "boolean") throw malformed2("previousDocument must be a boolean");
450
+ const event = {
451
+ type: "subscribed",
452
+ epoch,
453
+ partitionCount,
454
+ positions: positionsOf(raw.positions, partitionCount, "positions"),
455
+ heads: positionsOf(raw.heads, partitionCount, "heads"),
456
+ bytesBehind,
457
+ previousDocument
458
+ };
459
+ return { event, vectorFields: vectorFieldsOf(raw.vectorFields) };
460
+ }
461
+ function laterEventOf(raw, partitionCount, vectorFields) {
462
+ if (!isRecord2(raw)) throw malformed2("a message must be a map");
463
+ if (raw.type === "resync") return resyncOf(raw);
464
+ if (raw.type !== "changes") throw malformed2("the message type is unknown");
465
+ const { partitionId, scannedSeqNo, changes } = raw;
466
+ if (!isCount(partitionId) || partitionId >= partitionCount) throw malformed2("the partition is unknown");
467
+ if (!isCount(scannedSeqNo) || !Array.isArray(changes)) throw malformed2("the changes are invalid");
468
+ const decoded = changes.map((change) => changeOf(change, vectorFields));
469
+ if (decoded.some((change) => change.seqNo > scannedSeqNo)) throw malformed2("a change lies above scannedSeqNo");
470
+ return { type: "changes", partitionId, changes: decoded, scannedSeqNo };
471
+ }
472
+
473
+ // src/client/change-feed.ts
474
+ var CLOSE_NORMAL = 1e3;
475
+ var CLOSE_CREDENTIAL_EXPIRED = 4401;
476
+ var CLOSE_SETTING = 4409;
477
+ var CLOSE_RESYNC = 4410;
478
+ var CLOSE_POLICY = 1008;
479
+ function closedError(code, reason) {
480
+ if (code === CLOSE_SETTING && reason.length > 0) {
481
+ return new NarsilError(reason, `The server refused the subscription with ${reason}`, { closeCode: code });
482
+ }
483
+ if (code === CLOSE_RESYNC) {
484
+ return new NarsilError(ErrorCodes.CHANGES_NOT_ENABLED, "The index has change capture off", { closeCode: code });
485
+ }
486
+ if (code === CLOSE_POLICY) {
487
+ return new NarsilError(ErrorCodes.CONFIG_INVALID, `The server refused the subscription: ${reason}`, {
488
+ closeCode: code
489
+ });
490
+ }
491
+ const why = code === CLOSE_CREDENTIAL_EXPIRED ? "the credential expired" : `the server closed it with code ${code}`;
492
+ return new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, `The change feed ended because ${why}`, {
493
+ closeCode: code,
494
+ reason
495
+ });
496
+ }
497
+ function subscribeMessage(options) {
498
+ const { epoch, positions, filter } = options;
499
+ if (epoch === void 0 !== (positions === void 0)) {
500
+ throw new NarsilError(ErrorCodes.CHANGES_INVALID_POSITIONS, "Pass epoch and positions together, or leave both out");
501
+ }
502
+ return { type: "subscribe", epoch, positions, filter };
503
+ }
504
+ function openChangeSubscription(url, options) {
505
+ if (typeof WebSocket === "undefined") {
506
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The change feed needs a runtime with a global WebSocket");
507
+ }
508
+ const subscribe = subscribeMessage(options);
509
+ const queue = [];
510
+ let failure = null;
511
+ let ended = false;
512
+ let iterating = false;
513
+ let wake = null;
514
+ let socket = null;
515
+ let codec = null;
516
+ let partitionCount = 0;
517
+ let vectorFields = [];
518
+ const saved = [];
519
+ const notify = () => {
520
+ const waiting = wake;
521
+ wake = null;
522
+ waiting?.();
523
+ };
524
+ function finish(error) {
525
+ if (ended) return;
526
+ ended = true;
527
+ failure = error;
528
+ options.signal?.removeEventListener("abort", close);
529
+ if (socket !== null && socket.readyState <= WebSocket.OPEN) socket.close(CLOSE_NORMAL, "subscriber closed");
530
+ notify();
531
+ }
532
+ function close() {
533
+ finish(null);
534
+ }
535
+ function receive(data, code) {
536
+ if (!(data instanceof ArrayBuffer)) {
537
+ finish(new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, "The change feed sent a text frame"));
538
+ return;
539
+ }
540
+ try {
541
+ const raw = code.decode(new Uint8Array(data));
542
+ let event;
543
+ if (partitionCount === 0) {
544
+ const first = firstEventOf(raw);
545
+ event = first.event;
546
+ vectorFields = first.vectorFields;
547
+ } else {
548
+ event = laterEventOf(raw, partitionCount, vectorFields);
549
+ }
550
+ if (event.type === "subscribed") {
551
+ partitionCount = event.partitionCount;
552
+ saved.push(...new Array(partitionCount).fill(0));
553
+ for (const position of event.positions) saved[position.partitionId] = position.seqNo;
554
+ }
555
+ queue.push(event);
556
+ if (event.type === "resync") {
557
+ finish(null);
558
+ return;
559
+ }
560
+ } catch (error) {
561
+ finish(
562
+ error instanceof NarsilError ? error : new NarsilError(
563
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
564
+ "The change feed sent a message that is not MessagePack",
565
+ { cause: error instanceof Error ? error.message : String(error) }
566
+ )
567
+ );
568
+ return;
569
+ }
570
+ notify();
571
+ }
572
+ function connect(code) {
573
+ codec = code;
574
+ if (ended) return;
575
+ const protocols = options.credential === void 0 ? [CHANGES_SUBPROTOCOL] : [CHANGES_SUBPROTOCOL, options.credential];
576
+ let opened;
577
+ try {
578
+ opened = new WebSocket(url, protocols);
579
+ } catch (error) {
580
+ finish(
581
+ new NarsilError(ErrorCodes.CONFIG_INVALID, "The change feed could not start a WebSocket connection", {
582
+ cause: error instanceof Error ? error.message : String(error)
583
+ })
584
+ );
585
+ return;
586
+ }
587
+ socket = opened;
588
+ opened.binaryType = "arraybuffer";
589
+ opened.onopen = () => {
590
+ if (opened.protocol !== CHANGES_SUBPROTOCOL) {
591
+ finish(
592
+ new NarsilError(
593
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
594
+ `The server answered the change feed with the subprotocol "${opened.protocol}"`
595
+ )
596
+ );
597
+ return;
598
+ }
599
+ opened.send(code.encode(subscribe, { ignoreUndefined: true }));
600
+ };
601
+ opened.onmessage = (event) => {
602
+ if (!ended) receive(event.data, code);
603
+ };
604
+ opened.onerror = () => {
605
+ if (!ended) finish(new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, `The change feed at ${url} failed`));
606
+ };
607
+ opened.onclose = (event) => {
608
+ if (!ended) finish(closedError(event.code, event.reason));
609
+ };
610
+ }
611
+ if (options.signal?.aborted === true) ended = true;
612
+ else options.signal?.addEventListener("abort", close);
613
+ if (!ended) {
614
+ loadMessagePack().then(
615
+ connect,
616
+ (error) => finish(
617
+ error instanceof NarsilError ? error : new NarsilError(ClientErrorCodes.CLIENT_UNEXPECTED_ERROR, String(error))
618
+ )
619
+ );
620
+ }
621
+ async function* events() {
622
+ try {
623
+ while (true) {
624
+ const next = queue.shift();
625
+ if (next !== void 0) {
626
+ yield next;
627
+ continue;
628
+ }
629
+ if (ended) {
630
+ if (failure !== null) throw failure;
631
+ return;
632
+ }
633
+ await new Promise((resolve) => {
634
+ wake = resolve;
635
+ });
636
+ }
637
+ } finally {
638
+ close();
639
+ }
640
+ }
641
+ return {
642
+ [Symbol.asyncIterator]() {
643
+ if (iterating) {
644
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "A change subscription returns its events to one loop alone");
645
+ }
646
+ iterating = true;
647
+ return events();
648
+ },
649
+ ack(positions) {
650
+ for (const { partitionId, seqNo } of positions) {
651
+ if (!Number.isSafeInteger(partitionId) || partitionId < 0 || !Number.isSafeInteger(seqNo) || seqNo < 0) {
652
+ throw new NarsilError(
653
+ ErrorCodes.CHANGES_INVALID_POSITIONS,
654
+ "Each acknowledged position needs whole numbers",
655
+ {
656
+ partitionId,
657
+ seqNo
658
+ }
659
+ );
660
+ }
661
+ if (saved.length > 0 && partitionId >= saved.length) {
662
+ throw new NarsilError(ErrorCodes.CHANGES_INVALID_POSITIONS, "The index has no such partition", {
663
+ partitionId,
664
+ partitionCount
665
+ });
666
+ }
667
+ }
668
+ if (ended || socket === null || codec === null || socket.readyState !== WebSocket.OPEN) return;
669
+ if (saved.length === 0) return;
670
+ for (const { partitionId, seqNo } of positions) saved[partitionId] = Math.max(saved[partitionId] ?? 0, seqNo);
671
+ const every = saved.map((seqNo, partitionId) => ({ partitionId, seqNo }));
672
+ socket.send(codec.encode({ type: "ack", positions: every }));
673
+ },
674
+ close
675
+ };
676
+ }
677
+
678
+ // src/client/changes.ts
679
+ function createChangesClientOperations(transport) {
680
+ return {
681
+ async getChanges(indexName, options) {
682
+ const path = `${indexPath(indexName)}/changes`;
683
+ return readBody(await transport.json({ method: "GET", path, options }), path);
684
+ },
685
+ async updateChanges(indexName, setting, options) {
686
+ const path = `${indexPath(indexName)}/changes`;
687
+ await transport.json({ method: "PUT", path, body: encodeJson(setting), contentType: "application/json", options });
688
+ },
689
+ followChanges(indexName, options = {}) {
690
+ return openChangeSubscription(transport.socketUrl(`${indexPath(indexName)}/changes/feed`), options);
691
+ }
692
+ };
693
+ }
694
+
695
+ // src/client/documents.ts
696
+ function createDocumentOperations(transport) {
697
+ return {
698
+ async insert(indexName, document, docId, insertOptions, options) {
699
+ const path = `${indexPath(indexName)}/documents`;
700
+ const payload = await transport.json({
701
+ method: "POST",
702
+ path,
703
+ body: encodeJson({
704
+ document,
705
+ ...docId === void 0 ? {} : { id: docId },
706
+ ...insertOptions === void 0 ? {} : { options: insertOptions }
707
+ }),
708
+ contentType: "application/json",
709
+ options
710
+ });
711
+ return readString(payload, "id", path);
712
+ },
713
+ async get(indexName, docId, options) {
714
+ const path = documentPath(indexName, docId);
715
+ const payload = await transport.jsonOrNull({ method: "GET", path, options }, ErrorCodes.DOC_NOT_FOUND);
716
+ if (payload === null) return void 0;
717
+ return readObject(payload, "document", path);
718
+ },
719
+ async has(indexName, docId, options) {
720
+ const path = `${documentPath(indexName, docId)}/_exists`;
721
+ return readBoolean(await transport.json({ method: "GET", path, options }), "exists", path);
722
+ },
723
+ async put(indexName, docId, document, writeOptions, options) {
724
+ const path = documentPath(indexName, docId);
725
+ const payload = await transport.json({
726
+ method: "PUT",
727
+ path,
728
+ body: encodeJson({ document, ...writeOptions === void 0 ? {} : { options: writeOptions } }),
729
+ contentType: "application/json",
730
+ options
731
+ });
732
+ return { id: readString(payload, "id", path), created: readBoolean(payload, "created", path) };
733
+ },
734
+ async update(indexName, docId, document, writeOptions, options) {
735
+ await transport.json({
736
+ method: "PATCH",
737
+ path: documentPath(indexName, docId),
738
+ body: encodeJson({ document, ...writeOptions === void 0 ? {} : { options: writeOptions } }),
739
+ contentType: "application/json",
740
+ options
741
+ });
742
+ },
743
+ async remove(indexName, docId, writeOptions, options) {
744
+ await transport.json({
745
+ method: "DELETE",
746
+ path: documentPath(indexName, docId),
747
+ ...writeOptions?.wait === true ? { query: { wait: "true" } } : {},
748
+ options
749
+ });
750
+ },
751
+ async waitForWrites(indexName, options) {
752
+ await transport.json({ method: "POST", path: `${indexPath(indexName)}/_wait-for-writes`, options });
753
+ },
754
+ async countDocuments(indexName, options) {
755
+ const path = `${indexPath(indexName)}/count`;
756
+ return readNumber(await transport.json({ method: "GET", path, options }), "count", path);
757
+ }
758
+ };
759
+ }
760
+
761
+ // src/client/failover.ts
762
+ var UNSENT_CONNECTION_CODES = /* @__PURE__ */ new Set(["ECONNREFUSED", "ENOTFOUND", "EAI_AGAIN", "ConnectionRefused"]);
763
+ var MAX_CAUSE_DEPTH = 4;
764
+ var SKIP_DOUBLING_CEILING = 30;
765
+ var RETRIED_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
766
+ function readSkip(value, fallback, field) {
767
+ if (value === void 0) return fallback;
768
+ if (!Number.isSafeInteger(value) || value < 0) {
769
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, `"${field}" must be a whole number of milliseconds, 0 or more`, {
770
+ [field]: value
771
+ });
772
+ }
773
+ return value;
774
+ }
775
+ function createUrlPool(bases, options) {
776
+ const skipMs = readSkip(options.urlSkipMs, DEFAULT_URL_SKIP_MS, "urlSkipMs");
777
+ const maxSkipMs = readSkip(options.maxUrlSkipMs, Math.max(DEFAULT_MAX_URL_SKIP_MS, skipMs), "maxUrlSkipMs");
778
+ if (maxSkipMs < skipMs) {
779
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, '"maxUrlSkipMs" must be at least "urlSkipMs"', {
780
+ urlSkipMs: skipMs,
781
+ maxUrlSkipMs: maxSkipMs
782
+ });
783
+ }
784
+ const entries = bases.map((base, position) => ({ base, position, refusals: 0, skipUntil: 0 }));
785
+ let nextStart = 0;
786
+ return {
787
+ order(now) {
788
+ const rotation = [...entries.slice(nextStart), ...entries.slice(0, nextStart)];
789
+ const open = rotation.filter((entry) => entry.skipUntil <= now);
790
+ const skipped = rotation.filter((entry) => entry.skipUntil > now).sort((a, b) => a.skipUntil - b.skipUntil);
791
+ return [...open, ...skipped];
792
+ },
793
+ took(entry) {
794
+ nextStart = (entry.position + 1) % entries.length;
795
+ },
796
+ answered(entry) {
797
+ entry.refusals = 0;
798
+ entry.skipUntil = 0;
799
+ },
800
+ refused(entry, now) {
801
+ entry.refusals += 1;
802
+ const doublings = Math.min(entry.refusals - 1, SKIP_DOUBLING_CEILING);
803
+ entry.skipUntil = now + Math.min(skipMs * 2 ** doublings, maxSkipMs);
804
+ }
805
+ };
806
+ }
807
+ function failedBeforeSending(error, depth = 0) {
808
+ if (depth > MAX_CAUSE_DEPTH || typeof error !== "object" || error === null) return false;
809
+ const { code, cause, errors } = error;
810
+ if (typeof code === "string" && UNSENT_CONNECTION_CODES.has(code)) return true;
811
+ if (failedBeforeSending(cause, depth + 1)) return true;
812
+ return Array.isArray(errors) && errors.length > 0 && errors.every((entry) => failedBeforeSending(entry, depth + 1));
813
+ }
814
+ function answersUnavailable(status) {
815
+ return RETRIED_STATUSES.has(status);
816
+ }
817
+ function movesOnAfterAnswer(status, code, repeatable) {
818
+ if (!answersUnavailable(status)) return false;
819
+ return repeatable || code === ErrorCodes.NODE_NOT_READY;
820
+ }
821
+ function earnsExtraTry(failure) {
822
+ return failure.code === ClientErrorCodes.CLIENT_CONNECTION_FAILED;
823
+ }
824
+ function movesOnAfterFailure(failure, sentNothing, repeatable) {
825
+ if (failure.code === ClientErrorCodes.CLIENT_REQUEST_TIMEOUT) return repeatable;
826
+ if (failure.code === ClientErrorCodes.CLIENT_CONNECTION_FAILED) return repeatable || sentNothing;
827
+ return false;
828
+ }
829
+
830
+ // src/client/http.ts
831
+ function isRecord3(value) {
832
+ return typeof value === "object" && value !== null && !Array.isArray(value);
833
+ }
834
+ function unrefTimer(timer) {
835
+ const unrefable = timer;
836
+ unrefable.unref?.();
837
+ }
838
+ function startDeadline(timeoutMs, caller) {
839
+ const controller = new AbortController();
840
+ let expired = false;
841
+ const abortWithCaller = () => controller.abort();
842
+ if (caller?.aborted) controller.abort();
843
+ else caller?.addEventListener("abort", abortWithCaller);
844
+ let timer;
845
+ if (timeoutMs > 0 && !controller.signal.aborted) {
846
+ timer = setTimeout(() => {
847
+ expired = true;
848
+ controller.abort();
849
+ }, timeoutMs);
850
+ unrefTimer(timer);
851
+ }
852
+ return {
853
+ signal: controller.signal,
854
+ expired: () => expired,
855
+ release: () => {
856
+ if (timer !== void 0) clearTimeout(timer);
857
+ caller?.removeEventListener("abort", abortWithCaller);
858
+ }
859
+ };
860
+ }
861
+ var ResponseTooLargeError = class extends Error {
862
+ constructor(limitBytes) {
863
+ super(`The answer passed the ${limitBytes} byte ceiling this client reads`);
864
+ this.limitBytes = limitBytes;
865
+ }
866
+ };
867
+ async function readStreamCapped(stream, limitBytes) {
868
+ const reader = stream.getReader();
869
+ const chunks = [];
870
+ let total = 0;
871
+ try {
872
+ for (; ; ) {
873
+ const { done, value } = await reader.read();
874
+ if (done) break;
875
+ total += value.byteLength;
876
+ if (total > limitBytes) throw new ResponseTooLargeError(limitBytes);
877
+ chunks.push(value);
878
+ }
879
+ } finally {
880
+ reader.releaseLock();
881
+ if (total > limitBytes) await stream.cancel().catch(() => void 0);
882
+ }
883
+ const body = new Uint8Array(total);
884
+ let offset = 0;
885
+ for (const chunk of chunks) {
886
+ body.set(chunk, offset);
887
+ offset += chunk.byteLength;
888
+ }
889
+ return body;
890
+ }
891
+ async function readAnswer(response, limitBytes) {
892
+ const stream = response.body;
893
+ if (limitBytes > 0 && stream !== null && stream !== void 0) {
894
+ return new TextDecoder().decode(await readStreamCapped(stream, limitBytes));
895
+ }
896
+ const text = await response.text();
897
+ if (limitBytes > 0 && text.length > limitBytes) throw new ResponseTooLargeError(limitBytes);
898
+ return text;
899
+ }
900
+ function invalidResponse(message, details) {
901
+ return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, message, details);
902
+ }
903
+ function errorFromBody(status, payload, url) {
904
+ const envelope = isRecord3(payload) && isRecord3(payload.error) ? payload.error : void 0;
905
+ if (envelope === void 0 || typeof envelope.code !== "string" || typeof envelope.message !== "string") {
906
+ return invalidResponse(`The server answered HTTP ${status} without a Narsil error envelope`, { url, status });
907
+ }
908
+ const details = isRecord3(envelope.details) ? envelope.details : {};
909
+ return new NarsilError(envelope.code, envelope.message, { ...details, status });
910
+ }
911
+ function isHttpUrl(value) {
912
+ try {
913
+ const parsed = new URL(value);
914
+ return parsed.protocol === "http:" || parsed.protocol === "https:";
915
+ } catch {
916
+ return false;
917
+ }
918
+ }
919
+ function normaliseBase(url) {
920
+ const trimmed = url.trim();
921
+ if (trimmed.length === 0) {
922
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, 'The client needs a server address, and "url" is empty');
923
+ }
924
+ if (!trimmed.startsWith("/") && !isHttpUrl(trimmed)) {
925
+ throw new NarsilError(
926
+ ErrorCodes.CONFIG_INVALID,
927
+ `The client cannot read "${trimmed}" as a server address; pass an http or https URL, or a path starting with "/" that a browser resolves against the page`,
928
+ { url: trimmed }
929
+ );
930
+ }
931
+ return trimmed.replace(/\/+$/, "");
932
+ }
933
+ function normaliseBases(url) {
934
+ if (typeof url === "string") return [normaliseBase(url)];
935
+ if (!Array.isArray(url) || url.length === 0) {
936
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, 'The client needs a server address, and "url" lists none');
937
+ }
938
+ return url.map((entry) => {
939
+ if (typeof entry !== "string") {
940
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, 'Every server address in "url" must be a string');
941
+ }
942
+ return normaliseBase(entry);
943
+ });
944
+ }
945
+ function translateFailure(err, url, timeoutMs, expired, caller) {
946
+ if (err instanceof ResponseTooLargeError) {
947
+ return invalidResponse(`The server at ${url} answered more than this client reads: ${err.message}`, {
948
+ url,
949
+ maxResponseBytes: err.limitBytes
950
+ });
951
+ }
952
+ if (expired) {
953
+ return new NarsilError(
954
+ ClientErrorCodes.CLIENT_REQUEST_TIMEOUT,
955
+ `The server at ${url} did not answer within ${timeoutMs} ms`,
956
+ { url, timeoutMs }
957
+ );
958
+ }
959
+ if (caller?.aborted === true) {
960
+ return new NarsilError(ClientErrorCodes.CLIENT_REQUEST_ABORTED, `The caller cancelled the request to ${url}`, {
961
+ url
962
+ });
963
+ }
964
+ return new NarsilError(
965
+ ClientErrorCodes.CLIENT_CONNECTION_FAILED,
966
+ `The client could not reach ${url}: ${describeError(err)}`,
967
+ { url }
968
+ );
969
+ }
970
+ function envelopeCodeOf(text) {
971
+ try {
972
+ const payload = JSON.parse(text);
973
+ return isRecord3(payload) && isRecord3(payload.error) && typeof payload.error.code === "string" ? payload.error.code : void 0;
974
+ } catch {
975
+ return void 0;
976
+ }
977
+ }
978
+ function withQuery(path, query) {
979
+ if (query === void 0) return path;
980
+ const params = new URLSearchParams();
981
+ for (const [key, value] of Object.entries(query)) {
982
+ if (value !== void 0) params.set(key, value);
983
+ }
984
+ const encoded = params.toString();
985
+ return encoded.length === 0 ? path : `${path}?${encoded}`;
986
+ }
987
+ function createTransport(options) {
988
+ const pool = createUrlPool(normaliseBases(options.url), options);
989
+ const send = options.fetch ?? ((input, init) => fetch(input, init));
990
+ const baseHeaders = { ...options.headers };
991
+ if (options.apiKey !== void 0 && options.apiKey.length > 0) {
992
+ baseHeaders.authorization = `Bearer ${options.apiKey}`;
993
+ }
994
+ function resolveTimeout(spec) {
995
+ const perCall = spec.options?.timeoutMs;
996
+ if (perCall !== void 0) return perCall;
997
+ if (options.timeoutMs !== void 0) return options.timeoutMs;
998
+ return spec.defaultTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
999
+ }
1000
+ function resolveResponseCeiling(spec) {
1001
+ const perCall = spec.options?.maxResponseBytes;
1002
+ if (perCall !== void 0) return perCall;
1003
+ return options.maxResponseBytes ?? 0;
1004
+ }
1005
+ function recordAttempt(entry, attempt, repeatable) {
1006
+ if (attempt.kind === "answer") {
1007
+ if (!answersUnavailable(attempt.status)) {
1008
+ pool.answered(entry);
1009
+ return false;
1010
+ }
1011
+ return movesOnAfterAnswer(attempt.status, envelopeCodeOf(attempt.text), repeatable);
1012
+ }
1013
+ if (attempt.error.code === ClientErrorCodes.CLIENT_CONNECTION_FAILED) pool.refused(entry, Date.now());
1014
+ return movesOnAfterFailure(attempt.error, attempt.sentNothing, repeatable);
1015
+ }
1016
+ function outcomeOf(attempt) {
1017
+ if (attempt === null) throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The client holds no server address");
1018
+ if (attempt.kind === "failure") throw attempt.error;
1019
+ return attempt;
1020
+ }
1021
+ async function exchange(spec, keepsStream = false) {
1022
+ const repeatable = spec.repeatable === true || spec.method === "GET";
1023
+ const replayable = !(spec.body instanceof ReadableStream);
1024
+ const tryOn = async (entry2) => {
1025
+ pool.took(entry2);
1026
+ return exchangeOnce(entry2.base, spec, keepsStream);
1027
+ };
1028
+ let triesOnceMore = repeatable && replayable;
1029
+ let last = null;
1030
+ for (const entry2 of pool.order(Date.now())) {
1031
+ last = await tryOn(entry2);
1032
+ const moves = recordAttempt(entry2, last, repeatable);
1033
+ if (!replayable || !moves) return outcomeOf(last);
1034
+ if (last.kind === "failure" && !earnsExtraTry(last.error)) triesOnceMore = false;
1035
+ }
1036
+ const [entry] = pool.order(Date.now());
1037
+ if (triesOnceMore && entry !== void 0) {
1038
+ last = await tryOn(entry);
1039
+ recordAttempt(entry, last, repeatable);
1040
+ }
1041
+ return outcomeOf(last);
1042
+ }
1043
+ async function exchangeOnce(base, spec, keepsStream) {
1044
+ const url = `${base}${withQuery(spec.path, spec.query)}`;
1045
+ const headers = { ...baseHeaders, ...spec.headers, ...spec.options?.headers };
1046
+ if (spec.contentType !== void 0) headers["content-type"] = spec.contentType;
1047
+ const timeoutMs = resolveTimeout(spec);
1048
+ const deadline = startDeadline(timeoutMs, spec.options?.signal);
1049
+ let handedOver = false;
1050
+ try {
1051
+ const init = { method: spec.method, headers, signal: deadline.signal };
1052
+ if (spec.body !== void 0) init.body = spec.body;
1053
+ if (spec.body instanceof ReadableStream) init.duplex = "half";
1054
+ const response = await send(url, init);
1055
+ if (keepsStream && response.ok && response.body !== null) {
1056
+ handedOver = true;
1057
+ const stream = response.body.pipeThrough(
1058
+ new TransformStream({ flush: () => deadline.release() })
1059
+ );
1060
+ return { kind: "answer", status: response.status, url, text: "", stream, headers: response.headers };
1061
+ }
1062
+ const text = await readAnswer(response, resolveResponseCeiling(spec));
1063
+ return { kind: "answer", status: response.status, url, stream: null, text, headers: response.headers };
1064
+ } catch (err) {
1065
+ const error = translateFailure(err, url, timeoutMs, deadline.expired(), spec.options?.signal);
1066
+ const sentNothing = error.code === ClientErrorCodes.CLIENT_CONNECTION_FAILED && failedBeforeSending(err);
1067
+ return { kind: "failure", error, sentNothing };
1068
+ } finally {
1069
+ if (!handedOver) deadline.release();
1070
+ }
1071
+ }
1072
+ function parse(status, text, path) {
1073
+ if (text.length === 0) return void 0;
1074
+ try {
1075
+ return JSON.parse(text);
1076
+ } catch {
1077
+ throw invalidResponse(`The server answered HTTP ${status} with a body that is not JSON`, { url: path, status });
1078
+ }
1079
+ }
1080
+ async function json(spec) {
1081
+ const { status, text, url } = await exchange(spec);
1082
+ const payload = parse(status, text, url);
1083
+ if (status >= 200 && status < 300) return payload;
1084
+ throw errorFromBody(status, payload, url);
1085
+ }
1086
+ return {
1087
+ json,
1088
+ async jsonOrNull(spec, absentCode) {
1089
+ try {
1090
+ return await json(spec);
1091
+ } catch (err) {
1092
+ if (err instanceof NarsilError && err.code === absentCode) return null;
1093
+ throw err;
1094
+ }
1095
+ },
1096
+ async stream(spec) {
1097
+ const { status, text, stream, url } = await exchange(spec, true);
1098
+ if (stream !== null) return stream;
1099
+ throw errorFromBody(status, parse(status, text, url), url);
1100
+ },
1101
+ async download(spec) {
1102
+ const { status, text, stream, headers, url } = await exchange(spec, true);
1103
+ if (stream !== null && headers !== null) return { status, headers, stream };
1104
+ throw errorFromBody(status, parse(status, text, url), url);
1105
+ },
1106
+ async probe(spec) {
1107
+ const { status } = await exchange(spec);
1108
+ return status;
1109
+ },
1110
+ socketUrl(path) {
1111
+ const [entry] = pool.order(Date.now());
1112
+ if (entry === void 0) throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The client holds no server address");
1113
+ pool.took(entry);
1114
+ return socketUrlOf(entry.base, path);
1115
+ }
1116
+ };
1117
+ }
1118
+
1119
+ // src/client/indexes.ts
1120
+ function createIndexOperations(transport) {
1121
+ return {
1122
+ async createIndex(name, config, options) {
1123
+ await transport.json({
1124
+ method: "POST",
1125
+ path: "/indexes",
1126
+ body: encodeJson({ name, config }),
1127
+ contentType: "application/json",
1128
+ options
1129
+ });
1130
+ },
1131
+ async listIndexes(options) {
1132
+ const path = "/indexes";
1133
+ return readArray(await transport.json({ method: "GET", path, options }), "indexes", path);
1134
+ },
1135
+ async dropIndex(name, options) {
1136
+ await transport.json({ method: "DELETE", path: indexPath(name), options });
1137
+ },
1138
+ async getStats(indexName, options) {
1139
+ const path = `${indexPath(indexName)}/stats`;
1140
+ return readBody(await transport.json({ method: "GET", path, options }), path);
1141
+ },
1142
+ async getPartitionStats(indexName, options) {
1143
+ const path = `${indexPath(indexName)}/partitions`;
1144
+ const payload = await transport.json({ method: "GET", path, options });
1145
+ return readArray(payload, "partitions", path);
1146
+ },
1147
+ async clear(indexName, options) {
1148
+ await transport.json({ method: "POST", path: `${indexPath(indexName)}/_clear`, options });
1149
+ },
1150
+ async open(indexName, options) {
1151
+ await transport.json({ method: "POST", path: `${indexPath(indexName)}/_open`, options });
1152
+ },
1153
+ async close(indexName, options) {
1154
+ await transport.json({ method: "POST", path: `${indexPath(indexName)}/_close`, options });
1155
+ }
1156
+ };
1157
+ }
1158
+
1159
+ // src/client/query-suggestions.ts
1160
+ function createQuerySuggestionOperations(transport) {
1161
+ const pathOf = (indexName, route = "") => `${indexPath(indexName)}/query-suggestions${route}`;
1162
+ function send(method, path, body, options) {
1163
+ return transport.json({
1164
+ method,
1165
+ path,
1166
+ ...body === void 0 ? {} : { body: encodeJson(body), contentType: "application/json" },
1167
+ options
1168
+ });
1169
+ }
1170
+ return {
1171
+ async updateQuerySuggestions(indexName, setting, options) {
1172
+ await send("PUT", pathOf(indexName), setting, options);
1173
+ },
1174
+ async querySuggestionsStatus(indexName, options) {
1175
+ const path = pathOf(indexName);
1176
+ return readBody(await send("GET", path, void 0, options), path);
1177
+ },
1178
+ async recordQuery(indexName, query, options) {
1179
+ const path = pathOf(indexName, "/record");
1180
+ return readBody(await send("POST", path, { query }, options), path);
1181
+ },
1182
+ async buildQuerySuggestions(indexName, options) {
1183
+ const path = pathOf(indexName, "/build");
1184
+ return readBody(await send("POST", path, void 0, options), path);
1185
+ },
1186
+ async previewQuerySuggestions(indexName, limit, options) {
1187
+ const path = pathOf(indexName, "/preview");
1188
+ const payload = await transport.json({
1189
+ method: "GET",
1190
+ path,
1191
+ ...limit === void 0 ? {} : { query: { limit: String(limit) } },
1192
+ options
1193
+ });
1194
+ return readBody(payload, path);
1195
+ },
1196
+ async readQueryBans(indexName, options) {
1197
+ const path = pathOf(indexName, "/bans");
1198
+ return readArray(await send("GET", path, void 0, options), "bans", path);
1199
+ },
1200
+ async replaceQueryBans(indexName, bans, options) {
1201
+ const path = pathOf(indexName, "/bans");
1202
+ return readArray(await send("PUT", path, { bans }, options), "bans", path);
1203
+ },
1204
+ async removeQuery(indexName, query, options) {
1205
+ const path = pathOf(indexName, "/remove");
1206
+ return readNumber(await send("POST", path, { query }, options), "suggestionIndexes", path);
1207
+ },
1208
+ async addManualSuggestion(indexName, suggestion, options) {
1209
+ const path = pathOf(indexName, "/manual");
1210
+ return readString(await send("POST", path, suggestion, options), "indexName", path);
1211
+ }
1212
+ };
1213
+ }
1214
+
1215
+ // src/score-encoding.ts
1216
+ function decodeScore(score) {
1217
+ if (typeof score === "number") return Number.isFinite(score) ? score : void 0;
1218
+ if (score === "NaN") return Number.NaN;
1219
+ if (score === "Infinity") return Number.POSITIVE_INFINITY;
1220
+ if (score === "-Infinity") return Number.NEGATIVE_INFINITY;
1221
+ return void 0;
1222
+ }
1223
+
1224
+ // src/client/query-response.ts
1225
+ function record(value) {
1226
+ return typeof value === "object" && value !== null && !Array.isArray(value);
1227
+ }
1228
+ function invalid(path) {
1229
+ throw new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, "The search response holds an invalid hit score", {
1230
+ route: path
1231
+ });
1232
+ }
1233
+ function decodeHits(hits, path) {
1234
+ if (!Array.isArray(hits)) invalid(path);
1235
+ for (const hit of hits) {
1236
+ if (!record(hit)) invalid(path);
1237
+ if (!Object.hasOwn(hit, "score")) continue;
1238
+ const score = decodeScore(hit.score);
1239
+ if (score === void 0) invalid(path);
1240
+ hit.score = score;
1241
+ }
1242
+ }
1243
+ function decodeQueryResult(payload, path) {
1244
+ if (!record(payload)) invalid(path);
1245
+ decodeHits(payload.hits, path);
1246
+ if (payload.groups !== void 0) {
1247
+ if (!Array.isArray(payload.groups)) invalid(path);
1248
+ for (const group of payload.groups) {
1249
+ if (!record(group)) invalid(path);
1250
+ decodeHits(group.hits, path);
1251
+ }
1252
+ }
1253
+ return payload;
1254
+ }
1255
+
1256
+ // src/client/search.ts
1257
+ function createSearchOperations(transport) {
1258
+ async function post(path, params, options) {
1259
+ const payload = await transport.json({
1260
+ method: "POST",
1261
+ path,
1262
+ body: encodeJson(params),
1263
+ contentType: "application/json",
1264
+ repeatable: true,
1265
+ options
1266
+ });
1267
+ return readBody(payload, path);
1268
+ }
1269
+ return {
1270
+ async query(indexName, params, options) {
1271
+ const path = `${indexPath(indexName)}/search`;
1272
+ return decodeQueryResult(await post(path, params, options), path);
1273
+ },
1274
+ preflight(indexName, params, options) {
1275
+ return post(`${indexPath(indexName)}/search/preflight`, params, options);
1276
+ },
1277
+ suggest(indexName, params, options) {
1278
+ return post(`${indexPath(indexName)}/suggest`, params, options);
1279
+ }
1280
+ };
1281
+ }
1282
+
1283
+ // src/client/server-info.ts
1284
+ function createServerOperations(transport) {
1285
+ let announced = null;
1286
+ async function readCapabilities(options) {
1287
+ const path = "/capabilities";
1288
+ const payload = await transport.jsonOrNull({ method: "GET", path, options }, ServerErrorCodes.NOT_FOUND);
1289
+ if (payload === null) return /* @__PURE__ */ new Set();
1290
+ return new Set(readArray(payload, "capabilities", path));
1291
+ }
1292
+ function load(options) {
1293
+ if (announced === null) {
1294
+ announced = readCapabilities(options).catch((err) => {
1295
+ announced = null;
1296
+ throw err;
1297
+ });
1298
+ }
1299
+ return announced;
1300
+ }
1301
+ return {
1302
+ async version(options) {
1303
+ const path = "/version";
1304
+ return readBody(await transport.json({ method: "GET", path, options }), path);
1305
+ },
1306
+ async capabilities(options) {
1307
+ return [...await load(options)];
1308
+ },
1309
+ async supports(capability, options) {
1310
+ return (await load(options)).has(capability);
1311
+ },
1312
+ async isAlive(options) {
1313
+ return await transport.probe({ method: "GET", path: "/livez", options }) === 200;
1314
+ },
1315
+ async isReady(options) {
1316
+ return await transport.probe({ method: "GET", path: "/readyz", options }) === 200;
1317
+ }
1318
+ };
1319
+ }
1320
+
1321
+ // src/client/sync.ts
1322
+ var ENTITY_TAG = /^"([0-9a-f]{64})"$/;
1323
+ var CONTENT_RANGE = /^bytes (\d+)-(\d+)\/(\d+)$/;
1324
+ function invalidDownload(message, path) {
1325
+ return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, message, { url: path });
1326
+ }
1327
+ function createSyncSettingOperations(transport) {
1328
+ return {
1329
+ async getSync(indexName, options) {
1330
+ const path = `${indexPath(indexName)}/sync`;
1331
+ return readBody(await transport.json({ method: "GET", path, options }), path);
1332
+ },
1333
+ async updateSync(indexName, setting, options) {
1334
+ const path = `${indexPath(indexName)}/sync`;
1335
+ await transport.json({ method: "PUT", path, body: encodeJson(setting), contentType: "application/json", options });
1336
+ },
1337
+ async syncSnapshot(indexName, resume, options) {
1338
+ const path = `${indexPath(indexName)}/sync/snapshot`;
1339
+ const headers = resume === void 0 || resume.offset <= 0 ? {} : { range: `bytes=${resume.offset}-`, "if-range": `"${resume.entityTag}"` };
1340
+ const answer = await transport.download({ method: "GET", path, headers, defaultTimeoutMs: NO_TIMEOUT, options });
1341
+ const entityTag = ENTITY_TAG.exec(answer.headers.get("etag") ?? "")?.[1];
1342
+ if (entityTag === void 0) {
1343
+ await answer.stream.cancel();
1344
+ throw invalidDownload("The server sent the snapshot without an entity tag", path);
1345
+ }
1346
+ if (answer.status === 206) {
1347
+ const range = CONTENT_RANGE.exec(answer.headers.get("content-range") ?? "");
1348
+ if (range === null || Number(range[1]) !== resume?.offset) {
1349
+ await answer.stream.cancel();
1350
+ throw invalidDownload("The server sent a byte range other than the one that the client asked for", path);
1351
+ }
1352
+ return { entityTag, offset: Number(range[1]), totalBytes: Number(range[3]), stream: answer.stream };
1353
+ }
1354
+ const length = Number(answer.headers.get("content-length") ?? Number.NaN);
1355
+ if (!Number.isSafeInteger(length)) {
1356
+ await answer.stream.cancel();
1357
+ throw invalidDownload("The server sent the snapshot without its length", path);
1358
+ }
1359
+ return { entityTag, offset: 0, totalBytes: length, stream: answer.stream };
1360
+ }
1361
+ };
1362
+ }
1363
+
1364
+ // src/client/tasks.ts
1365
+ function isTerminal(record2) {
1366
+ return record2.status === "succeeded" || record2.status === "failed" || record2.status === "cancelled";
1367
+ }
1368
+ function progressKey(record2) {
1369
+ const progress = record2.progress;
1370
+ if (progress === void 0) return record2.status;
1371
+ return `${record2.status}:${progress.bytesProcessed}:${progress.indexed}:${progress.failed}`;
1372
+ }
1373
+ function sleep(milliseconds, signal) {
1374
+ return new Promise((resolve) => {
1375
+ const timer = setTimeout(() => {
1376
+ signal?.removeEventListener("abort", stop);
1377
+ resolve();
1378
+ }, milliseconds);
1379
+ function stop() {
1380
+ clearTimeout(timer);
1381
+ resolve();
1382
+ }
1383
+ signal?.addEventListener("abort", stop, { once: true });
1384
+ });
1385
+ }
1386
+ function listQuery(query) {
1387
+ return {
1388
+ indexName: query?.indexName,
1389
+ type: query?.type === void 0 || query.type.length === 0 ? void 0 : query.type.join(","),
1390
+ status: query?.status === void 0 || query.status.length === 0 ? void 0 : query.status.join(","),
1391
+ from: query?.from === void 0 ? void 0 : String(query.from),
1392
+ limit: query?.limit === void 0 ? void 0 : String(query.limit)
1393
+ };
1394
+ }
1395
+ function createTaskOperations(transport) {
1396
+ const operations = {
1397
+ async getTask(taskId, options) {
1398
+ const path = taskPath(taskId);
1399
+ const payload = await transport.jsonOrNull({ method: "GET", path, options }, ServerErrorCodes.TASK_NOT_FOUND);
1400
+ return payload === null ? null : readBody(payload, path);
1401
+ },
1402
+ async listTasks(query, options) {
1403
+ const path = "/tasks";
1404
+ const payload = await transport.json({ method: "GET", path, query: listQuery(query), options });
1405
+ return readBody(payload, path);
1406
+ },
1407
+ async cancelTask(taskId, options) {
1408
+ const path = `${taskPath(taskId)}/_cancel`;
1409
+ return readBody(await transport.json({ method: "POST", path, options }), path);
1410
+ },
1411
+ async waitForTask(taskId, options) {
1412
+ const interval = options?.pollIntervalMs ?? DEFAULT_TASK_POLL_INTERVAL_MS;
1413
+ const waitTimeoutMs = options?.waitTimeoutMs ?? 0;
1414
+ const startedAt = Date.now();
1415
+ let reported = null;
1416
+ for (; ; ) {
1417
+ if (options?.signal?.aborted === true) {
1418
+ throw new NarsilError(
1419
+ ClientErrorCodes.CLIENT_REQUEST_ABORTED,
1420
+ `The caller stopped waiting for task "${taskId}"`,
1421
+ { taskId }
1422
+ );
1423
+ }
1424
+ const record2 = await operations.getTask(taskId, options);
1425
+ if (record2 === null) {
1426
+ throw new NarsilError(
1427
+ ServerErrorCodes.TASK_NOT_FOUND,
1428
+ `The server no longer holds task "${taskId}", so its outcome is unknown`,
1429
+ { taskId }
1430
+ );
1431
+ }
1432
+ const key = progressKey(record2);
1433
+ if (key !== reported) {
1434
+ reported = key;
1435
+ options?.onProgress?.(record2);
1436
+ }
1437
+ if (isTerminal(record2)) return record2;
1438
+ if (waitTimeoutMs > 0 && Date.now() - startedAt >= waitTimeoutMs) {
1439
+ throw new NarsilError(
1440
+ ClientErrorCodes.CLIENT_TASK_TIMEOUT,
1441
+ `Task "${taskId}" was still ${record2.status} after ${waitTimeoutMs} ms, and it keeps running`,
1442
+ { taskId, waitTimeoutMs, status: record2.status }
1443
+ );
1444
+ }
1445
+ await sleep(interval, options?.signal);
1446
+ }
1447
+ }
1448
+ };
1449
+ return operations;
1450
+ }
1451
+
1452
+ // src/client/client.ts
1453
+ function createNarsilClient(options) {
1454
+ const transport = createTransport(options);
1455
+ return {
1456
+ ...createIndexOperations(transport),
1457
+ ...createDocumentOperations(transport),
1458
+ ...createBulkOperations(transport),
1459
+ ...createSearchOperations(transport),
1460
+ ...createTaskOperations(transport),
1461
+ ...createAdminOperations(transport),
1462
+ ...createBackupOperations(transport),
1463
+ ...createServerOperations(transport),
1464
+ ...createSyncSettingOperations(transport),
1465
+ ...createChangesClientOperations(transport),
1466
+ ...createQuerySuggestionOperations(transport)
1467
+ };
1468
+ }
1469
+
1470
+ // src/sync/protocol.ts
1471
+ var SYNC_SUBPROTOCOL = "narsil.sync.v1";
1472
+ var SYNC_CLOSE_CREDENTIAL_EXPIRED = 4401;
1473
+ var SYNC_CLOSE_RESYNC = 4410;
1474
+
1475
+ // src/sync/device/constants.ts
1476
+ var RECONNECT_MIN_DELAY_MS = 500;
1477
+ var RETRY_MAX_DELAY_MS = 3e4;
1478
+ var BACKOFF_DOUBLINGS_LIMIT = 30;
1479
+ var DEFAULT_REPLAY_LIMIT_OF_SNAPSHOT = 1;
1480
+ var KEPT_ENTRY_FLUSH_BYTES = 4 * 1024 * 1024;
1481
+ var KEPT_ENTRY_FLUSH_IDLE_MS = 1e3;
1482
+ var UPLOAD_RETRY_DELAY_MS = 1e3;
1483
+ var SNAPSHOT_RETRY_DELAY_MS = 5e3;
1484
+ var SNAPSHOT_MEMORY_PER_ARCHIVE_BYTE = 3;
1485
+ var DOWNLOAD_SAVE_BYTES = 1024 * 1024;
1486
+ var UPLOAD_BATCH_WRITES = 100;
1487
+ var KEPT_ENTRY_OVERHEAD_BYTES = 32;
1488
+
1489
+ // src/sync/device/context.ts
1490
+ function createChanges() {
1491
+ let waiters = [];
1492
+ return {
1493
+ notify() {
1494
+ const woken = waiters;
1495
+ waiters = [];
1496
+ for (const wake of woken) wake();
1497
+ },
1498
+ next() {
1499
+ return new Promise((resolve) => waiters.push(resolve));
1500
+ }
1501
+ };
1502
+ }
1503
+ function backoffDelayMs(attempt, minimumMs, maximumMs) {
1504
+ const ceiling = Math.min(maximumMs, minimumMs * 2 ** Math.min(attempt, BACKOFF_DOUBLINGS_LIMIT));
1505
+ return Math.round(ceiling / 2 + Math.random() * ceiling / 2);
1506
+ }
1507
+
1508
+ // src/sync/device/messages.ts
1509
+ var RESYNC_REASONS2 = /* @__PURE__ */ new Set(["epoch", "history", "ahead"]);
1510
+ function isRecord4(value) {
1511
+ return value !== null && typeof value === "object" && !Array.isArray(value) && !(value instanceof Uint8Array);
1512
+ }
1513
+ function isCount2(value) {
1514
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
1515
+ }
1516
+ function malformed3(detail) {
1517
+ return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, `The sync feed sent a malformed message: ${detail}`);
1518
+ }
1519
+ function headsOf(raw) {
1520
+ if (!Array.isArray(raw)) throw malformed3("heads must be a list");
1521
+ const seqNos = new Array(raw.length).fill(-1);
1522
+ for (const item of raw) {
1523
+ if (!isRecord4(item) || !isCount2(item.partitionId) || !isCount2(item.seqNo)) throw malformed3("a head is invalid");
1524
+ if (item.partitionId >= seqNos.length || seqNos[item.partitionId] !== -1) {
1525
+ throw malformed3("heads must name each partition once");
1526
+ }
1527
+ seqNos[item.partitionId] = item.seqNo;
1528
+ }
1529
+ return seqNos;
1530
+ }
1531
+ function stringList(value) {
1532
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
1533
+ }
1534
+ function settingsOf(raw) {
1535
+ if (!isRecord4(raw) || typeof raw.language !== "string") throw malformed3("settings need a language");
1536
+ const optionalString2 = (key) => {
1537
+ const value = raw[key];
1538
+ if (value === void 0 || value === null) return void 0;
1539
+ if (typeof value !== "string") throw malformed3(`settings.${key} must be a string`);
1540
+ return value;
1541
+ };
1542
+ const tokenizer = optionalString2("tokenizer");
1543
+ const stopWords = optionalString2("stopWords");
1544
+ const analysisRevision = optionalString2("analysisRevision");
1545
+ if (raw.stopWordList !== void 0 && raw.stopWordList !== null && !stringList(raw.stopWordList)) {
1546
+ throw malformed3("settings.stopWordList must be a list of strings");
1547
+ }
1548
+ if (raw.bm25 !== void 0 && raw.bm25 !== null && !isRecord4(raw.bm25)) throw malformed3("settings.bm25 is invalid");
1549
+ if (raw.synonyms !== void 0 && raw.synonyms !== null && !Array.isArray(raw.synonyms)) {
1550
+ throw malformed3("settings.synonyms must be a list");
1551
+ }
1552
+ return {
1553
+ language: raw.language,
1554
+ ...tokenizer !== void 0 ? { tokenizer } : {},
1555
+ ...stopWords !== void 0 ? { stopWords } : {},
1556
+ ...stringList(raw.stopWordList) ? { stopWordList: raw.stopWordList } : {},
1557
+ ...analysisRevision !== void 0 ? { analysisRevision } : {},
1558
+ ...isRecord4(raw.bm25) ? { bm25: raw.bm25 } : {},
1559
+ ...Array.isArray(raw.synonyms) ? { synonyms: raw.synonyms } : {}
1560
+ };
1561
+ }
1562
+ function entriesOf(partitionId, raw) {
1563
+ if (!Array.isArray(raw)) throw malformed3("entries must be a list");
1564
+ let previous = -1;
1565
+ return raw.map((item) => {
1566
+ if (!isRecord4(item) || !isCount2(item.seqNo) || typeof item.documentId !== "string") {
1567
+ throw malformed3("an entry is invalid");
1568
+ }
1569
+ if (item.seqNo <= previous) throw malformed3("entries must rise in sequence order");
1570
+ if (item.partitionId !== void 0 && item.partitionId !== partitionId) {
1571
+ throw malformed3("an entry belongs to another partition");
1572
+ }
1573
+ previous = item.seqNo;
1574
+ if (item.operation === "DELETE") {
1575
+ return { partitionId, seqNo: item.seqNo, documentId: item.documentId, document: null };
1576
+ }
1577
+ if (item.operation !== "INDEX" || !(item.document instanceof Uint8Array)) throw malformed3("an entry is invalid");
1578
+ return { partitionId, seqNo: item.seqNo, documentId: item.documentId, document: item.document };
1579
+ });
1580
+ }
1581
+ function decodeFeedMessage(bytes) {
1582
+ const { decode } = requireLoadedMessagePack();
1583
+ let raw;
1584
+ try {
1585
+ raw = decode(bytes);
1586
+ } catch {
1587
+ throw malformed3("a frame is not MessagePack");
1588
+ }
1589
+ if (!isRecord4(raw)) throw malformed3("a frame is not a map");
1590
+ switch (raw.type) {
1591
+ case "subscribed": {
1592
+ if (!isSyncEpoch(raw.epoch) || !isCount2(raw.partitionCount)) throw malformed3("subscribed is invalid");
1593
+ if (!isCount2(raw.bytesBehind) || !isCount2(raw.snapshotBytes) || !isCount2(raw.windowBytes)) {
1594
+ throw malformed3("subscribed is invalid");
1595
+ }
1596
+ const heads = headsOf(raw.heads);
1597
+ if (heads.length !== raw.partitionCount) throw malformed3("subscribed needs one head for each partition");
1598
+ return {
1599
+ type: "subscribed",
1600
+ epoch: raw.epoch,
1601
+ partitionCount: raw.partitionCount,
1602
+ heads,
1603
+ bytesBehind: raw.bytesBehind,
1604
+ snapshotBytes: raw.snapshotBytes,
1605
+ windowBytes: raw.windowBytes,
1606
+ settings: settingsOf(raw.settings)
1607
+ };
1608
+ }
1609
+ case "entries":
1610
+ if (!isCount2(raw.partitionId)) throw malformed3("entries need a partition");
1611
+ return { type: "entries", partitionId: raw.partitionId, entries: entriesOf(raw.partitionId, raw.entries) };
1612
+ case "settings":
1613
+ return { type: "settings", settings: settingsOf(raw.settings) };
1614
+ case "checkpoint":
1615
+ if (!isCount2(raw.id)) throw malformed3("checkpoint needs an id");
1616
+ return { type: "checkpoint", id: raw.id, heads: headsOf(raw.heads) };
1617
+ case "resync":
1618
+ if (typeof raw.reason !== "string" || !RESYNC_REASONS2.has(raw.reason)) throw malformed3("resync is invalid");
1619
+ return { type: "resync", reason: raw.reason };
1620
+ default:
1621
+ throw malformed3(`unknown type ${String(raw.type)}`);
1622
+ }
1623
+ }
1624
+ function positionsOf2(seqNos) {
1625
+ return seqNos.map((seqNo, partitionId) => ({ partitionId, seqNo }));
1626
+ }
1627
+
1628
+ // src/sync/device/feed.ts
1629
+ var CLOSE_NORMAL2 = 1e3;
1630
+ function feedUrlOf(base, indexName) {
1631
+ const location = globalThis.location?.href;
1632
+ let parsed;
1633
+ try {
1634
+ parsed = location === void 0 ? new URL(base) : new URL(base, location);
1635
+ } catch {
1636
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, `The device cannot read "${base}" as a server address`, {
1637
+ url: base
1638
+ });
1639
+ }
1640
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
1641
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The device needs an http or https server address", { url: base });
1642
+ }
1643
+ parsed.protocol = parsed.protocol === "https:" ? "wss:" : "ws:";
1644
+ parsed.pathname = `${parsed.pathname.replace(/\/+$/, "")}/indexes/${encodeURIComponent(indexName)}/sync/feed`;
1645
+ parsed.search = "";
1646
+ parsed.hash = "";
1647
+ return parsed.toString();
1648
+ }
1649
+ function feedFailed(message) {
1650
+ return new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, message);
1651
+ }
1652
+ function openFeedConnection(url, credential, handlers) {
1653
+ if (typeof WebSocket === "undefined") {
1654
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "Device sync needs a runtime with a global WebSocket");
1655
+ }
1656
+ const socket = new WebSocket(url, credential === null ? [SYNC_SUBPROTOCOL] : [SYNC_SUBPROTOCOL, credential]);
1657
+ socket.binaryType = "arraybuffer";
1658
+ let closed = false;
1659
+ function finish(code) {
1660
+ if (closed) return;
1661
+ closed = true;
1662
+ handlers.onClose(code);
1663
+ }
1664
+ function closeSocket(reason) {
1665
+ if (socket.readyState === WebSocket.CONNECTING || socket.readyState === WebSocket.OPEN) {
1666
+ socket.close(CLOSE_NORMAL2, reason);
1667
+ }
1668
+ }
1669
+ socket.onopen = () => {
1670
+ if (socket.protocol !== SYNC_SUBPROTOCOL) {
1671
+ handlers.onFailure(
1672
+ new NarsilError(
1673
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
1674
+ `The server answered the feed with the subprotocol "${socket.protocol}"`
1675
+ )
1676
+ );
1677
+ closeSocket("subprotocol");
1678
+ finish(CLOSE_NORMAL2);
1679
+ return;
1680
+ }
1681
+ handlers.onOpen();
1682
+ };
1683
+ socket.onmessage = (event) => {
1684
+ if (closed) return;
1685
+ if (!(event.data instanceof ArrayBuffer)) {
1686
+ handlers.onFailure(new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, "The feed sent a text frame"));
1687
+ closeSocket("text frame");
1688
+ finish(CLOSE_NORMAL2);
1689
+ return;
1690
+ }
1691
+ let message;
1692
+ try {
1693
+ message = decodeFeedMessage(new Uint8Array(event.data));
1694
+ } catch (error) {
1695
+ handlers.onFailure(error);
1696
+ closeSocket("malformed message");
1697
+ finish(CLOSE_NORMAL2);
1698
+ return;
1699
+ }
1700
+ handlers.onMessage(message);
1701
+ };
1702
+ socket.onerror = () => {
1703
+ if (!closed) handlers.onFailure(feedFailed(`The feed at ${url} failed`));
1704
+ };
1705
+ socket.onclose = (event) => finish(event.code);
1706
+ return {
1707
+ send(message) {
1708
+ if (!closed && socket.readyState === WebSocket.OPEN) socket.send(requireLoadedMessagePack().encode(message));
1709
+ },
1710
+ close(reason) {
1711
+ closeSocket(reason);
1712
+ finish(CLOSE_NORMAL2);
1713
+ }
1714
+ };
1715
+ }
1716
+
1717
+ // src/sync/device/local-writes.ts
1718
+ var NO_VECTOR_FIELDS = /* @__PURE__ */ new Set();
1719
+ function vectorFieldPathsOf(engine, indexName) {
1720
+ if (!holdsIndex(engine, indexName)) return NO_VECTOR_FIELDS;
1721
+ return new Set(extractVectorFieldsFromSchema(engine.getStats(indexName).schema).keys());
1722
+ }
1723
+ function decodedDocument(bytes, vectorFieldPaths) {
1724
+ const code = requireLoadedMessagePack();
1725
+ const document = code.decode(bytes);
1726
+ code.restoreVectorFields(document, vectorFieldPaths);
1727
+ return document;
1728
+ }
1729
+ function documentOfEntry(engine, indexName, bytes) {
1730
+ return decodedDocument(bytes, vectorFieldPathsOf(engine, indexName));
1731
+ }
1732
+ function portableCopyOf(engine, indexName, document) {
1733
+ const code = requireLoadedMessagePack();
1734
+ const vectorFieldPaths = vectorFieldPathsOf(engine, indexName);
1735
+ const copy = decodedDocument(code.encodeDocument(document), vectorFieldPaths);
1736
+ for (const fieldPath of vectorFieldPaths) {
1737
+ const vector = getNestedValue(copy, fieldPath);
1738
+ if (vector instanceof Float32Array) code.setNestedValue(copy, fieldPath, Array.from(vector));
1739
+ }
1740
+ return copy;
1741
+ }
1742
+ function bytesEqual(left, right) {
1743
+ if (left.byteLength !== right.byteLength) return false;
1744
+ for (let at = 0; at < left.byteLength; at++) {
1745
+ if (left[at] !== right[at]) return false;
1746
+ }
1747
+ return true;
1748
+ }
1749
+ function holdsIndex(engine, indexName) {
1750
+ return engine.listIndexes().some((index) => index.name === indexName);
1751
+ }
1752
+ async function putLocally(engine, indexName, documentId, document) {
1753
+ if (await engine.has(indexName, documentId)) {
1754
+ await engine.update(indexName, documentId, document);
1755
+ } else {
1756
+ await engine.insert(indexName, document, documentId);
1757
+ }
1758
+ }
1759
+ async function removeLocally(engine, indexName, documentId) {
1760
+ try {
1761
+ await engine.remove(indexName, documentId);
1762
+ } catch (error) {
1763
+ if (!(error instanceof NarsilError) || error.code !== ErrorCodes.DOC_NOT_FOUND) throw error;
1764
+ }
1765
+ }
1766
+ async function holdsSameDocument(engine, indexName, documentId, encoded) {
1767
+ const held = await engine.get(indexName, documentId);
1768
+ if (encoded === null || held === void 0) return encoded === null && held === void 0;
1769
+ return bytesEqual(requireLoadedMessagePack().encodeDocument(held), encoded);
1770
+ }
1771
+ async function applyEntryLocally(engine, indexName, entry) {
1772
+ if (await holdsSameDocument(engine, indexName, entry.documentId, entry.document)) return;
1773
+ if (entry.document === null) {
1774
+ await removeLocally(engine, indexName, entry.documentId);
1775
+ } else {
1776
+ await putLocally(engine, indexName, entry.documentId, documentOfEntry(engine, indexName, entry.document));
1777
+ }
1778
+ }
1779
+ async function applyWriteLocally(engine, indexName, write) {
1780
+ if (write.document === null) {
1781
+ await removeLocally(engine, indexName, write.documentId);
1782
+ return;
1783
+ }
1784
+ const encoded = requireLoadedMessagePack().encodeDocument(write.document);
1785
+ if (await holdsSameDocument(engine, indexName, write.documentId, encoded)) return;
1786
+ await putLocally(engine, indexName, write.documentId, write.document);
1787
+ }
1788
+ function newestByDocument(entries) {
1789
+ const newest = /* @__PURE__ */ new Map();
1790
+ for (const entry of entries) {
1791
+ newest.delete(entry.documentId);
1792
+ newest.set(entry.documentId, entry);
1793
+ }
1794
+ return newest;
1795
+ }
1796
+
1797
+ // src/sync/device/progress.ts
1798
+ function entryBytes(entry) {
1799
+ return entry.documentId.length * 2 + (entry.document?.byteLength ?? 0) + KEPT_ENTRY_OVERHEAD_BYTES;
1800
+ }
1801
+ function createProgress(context) {
1802
+ const { engine, indexName, store, queue, work } = context;
1803
+ let position = null;
1804
+ let received = [];
1805
+ let unsaved = 0;
1806
+ let flushThreshold = KEPT_ENTRY_FLUSH_BYTES;
1807
+ let flushTimer = null;
1808
+ let acknowledge = null;
1809
+ const deferred = /* @__PURE__ */ new Map();
1810
+ function cancelFlush() {
1811
+ if (flushTimer !== null) clearTimeout(flushTimer);
1812
+ flushTimer = null;
1813
+ }
1814
+ async function saveWithinWork() {
1815
+ cancelFlush();
1816
+ if (position === null) return;
1817
+ if (unsaved === 0 && received.every((seqNo, partitionId) => seqNo === position?.seqNos[partitionId])) return;
1818
+ await context.makeDurable();
1819
+ const next = { epoch: position.epoch, seqNos: [...received] };
1820
+ await store.writePosition(next);
1821
+ position = next;
1822
+ await store.clearKeptEntries();
1823
+ unsaved = 0;
1824
+ acknowledge?.(next.seqNos);
1825
+ }
1826
+ function scheduleFlush() {
1827
+ if (context.stopped() || unsaved === 0) return;
1828
+ if (unsaved >= flushThreshold) {
1829
+ cancelFlush();
1830
+ work.run(saveWithinWork).catch(context.report);
1831
+ return;
1832
+ }
1833
+ if (flushTimer !== null) return;
1834
+ flushTimer = setTimeout(() => {
1835
+ flushTimer = null;
1836
+ work.run(saveWithinWork).catch(context.report);
1837
+ }, KEPT_ENTRY_FLUSH_IDLE_MS);
1838
+ }
1839
+ async function reapplyQueue() {
1840
+ if (!holdsIndex(engine, indexName)) return;
1841
+ for (const write of queue.ordered()) {
1842
+ try {
1843
+ await applyWriteLocally(engine, indexName, write);
1844
+ } catch (error) {
1845
+ await store.removeWrites([write.sequence]);
1846
+ queue.remove([write.sequence]);
1847
+ context.report(error);
1848
+ }
1849
+ }
1850
+ }
1851
+ return {
1852
+ position: () => position,
1853
+ received: () => received,
1854
+ recover: () => work.run(async () => {
1855
+ position = await store.readPosition();
1856
+ if (position !== null && !holdsIndex(engine, indexName)) {
1857
+ position = null;
1858
+ await store.writePosition(null);
1859
+ }
1860
+ queue.reset(await store.queuedWrites());
1861
+ const kept = await store.keptEntries();
1862
+ received = position === null ? [] : [...position.seqNos];
1863
+ const saved = position?.seqNos ?? [];
1864
+ const fresh = kept.filter((entry) => entry.seqNo > (saved[entry.partitionId] ?? Number.POSITIVE_INFINITY));
1865
+ for (const entry of newestByDocument(fresh).values()) {
1866
+ if (!queue.holdsDocument(entry.documentId)) await applyEntryLocally(engine, indexName, entry);
1867
+ }
1868
+ for (const entry of fresh) {
1869
+ received[entry.partitionId] = Math.max(received[entry.partitionId] ?? 0, entry.seqNo);
1870
+ }
1871
+ unsaved = fresh.length;
1872
+ if (fresh.length > 0) await saveWithinWork();
1873
+ else if (kept.length > 0) await store.clearKeptEntries();
1874
+ await reapplyQueue();
1875
+ }),
1876
+ async keep(partitionId, entries) {
1877
+ await work.run(async () => {
1878
+ const last = received[partitionId];
1879
+ if (last === void 0) return;
1880
+ const fresh = entries.filter((entry) => entry.seqNo > last);
1881
+ const newest = fresh[fresh.length - 1];
1882
+ if (newest === void 0) return;
1883
+ await store.keepEntries(fresh);
1884
+ for (const entry of fresh) {
1885
+ unsaved += entryBytes(entry);
1886
+ if (queue.holdsDocument(entry.documentId)) deferred.set(entry.documentId, entry);
1887
+ else await applyEntryLocally(engine, indexName, entry);
1888
+ }
1889
+ received[partitionId] = newest.seqNo;
1890
+ });
1891
+ scheduleFlush();
1892
+ },
1893
+ async settleUploaded(sequences) {
1894
+ await work.run(async () => {
1895
+ await store.removeWrites(sequences);
1896
+ queue.remove(sequences);
1897
+ for (const [documentId, entry] of deferred) {
1898
+ if (queue.holdsDocument(documentId)) continue;
1899
+ deferred.delete(documentId);
1900
+ await applyEntryLocally(engine, indexName, entry);
1901
+ }
1902
+ });
1903
+ scheduleFlush();
1904
+ },
1905
+ forgetDeferred(documentId) {
1906
+ deferred.delete(documentId);
1907
+ },
1908
+ async adopt(next) {
1909
+ cancelFlush();
1910
+ deferred.clear();
1911
+ await reapplyQueue();
1912
+ await context.makeDurable();
1913
+ await store.writePosition(next);
1914
+ position = { epoch: next.epoch, seqNos: [...next.seqNos] };
1915
+ received = [...next.seqNos];
1916
+ unsaved = 0;
1917
+ await store.clearDownload();
1918
+ },
1919
+ reapplyQueue,
1920
+ save: () => work.run(saveWithinWork),
1921
+ reached(heads) {
1922
+ return heads.length === received.length && heads.every((head, partitionId) => (received[partitionId] ?? -1) >= head);
1923
+ },
1924
+ setFlushThreshold(windowBytes) {
1925
+ flushThreshold = Math.max(1, Math.min(KEPT_ENTRY_FLUSH_BYTES, Math.floor(windowBytes / 2)));
1926
+ },
1927
+ setAcknowledge(next) {
1928
+ acknowledge = next;
1929
+ },
1930
+ cancelFlush,
1931
+ unsavedBytes: () => unsaved
1932
+ };
1933
+ }
1934
+
1935
+ // src/sync/device/queue.ts
1936
+ var QueueMirror = class {
1937
+ writes = /* @__PURE__ */ new Map();
1938
+ sequencesByDocument = /* @__PURE__ */ new Map();
1939
+ uploaded = /* @__PURE__ */ new Set();
1940
+ get size() {
1941
+ return this.writes.size;
1942
+ }
1943
+ reset(writes) {
1944
+ this.writes.clear();
1945
+ this.sequencesByDocument.clear();
1946
+ this.uploaded.clear();
1947
+ for (const write of [...writes].sort((a, b) => a.sequence - b.sequence)) this.add(write);
1948
+ }
1949
+ add(write) {
1950
+ this.writes.set(write.sequence, write);
1951
+ const sequences = this.sequencesByDocument.get(write.documentId);
1952
+ if (sequences === void 0) this.sequencesByDocument.set(write.documentId, [write.sequence]);
1953
+ else sequences.push(write.sequence);
1954
+ }
1955
+ remove(sequences) {
1956
+ for (const sequence of sequences) {
1957
+ const write = this.writes.get(sequence);
1958
+ if (write === void 0) continue;
1959
+ this.writes.delete(sequence);
1960
+ this.uploaded.delete(sequence);
1961
+ const remaining = (this.sequencesByDocument.get(write.documentId) ?? []).filter((held) => held !== sequence);
1962
+ if (remaining.length === 0) this.sequencesByDocument.delete(write.documentId);
1963
+ else this.sequencesByDocument.set(write.documentId, remaining);
1964
+ }
1965
+ }
1966
+ holdsDocument(documentId) {
1967
+ return this.sequencesByDocument.has(documentId);
1968
+ }
1969
+ ordered() {
1970
+ return [...this.writes.values()].sort((a, b) => a.sequence - b.sequence);
1971
+ }
1972
+ latestAwaitingUpload() {
1973
+ const latest = [];
1974
+ for (const sequences of this.sequencesByDocument.values()) {
1975
+ const newest = sequences[sequences.length - 1];
1976
+ if (newest === void 0 || this.uploaded.has(newest)) continue;
1977
+ const write = this.writes.get(newest);
1978
+ if (write !== void 0) latest.push(write);
1979
+ }
1980
+ return latest.sort((a, b) => a.sequence - b.sequence);
1981
+ }
1982
+ coveredBy(write) {
1983
+ const sequences = this.sequencesByDocument.get(write.documentId) ?? [];
1984
+ return sequences.filter((sequence) => sequence <= write.sequence && !this.uploaded.has(sequence));
1985
+ }
1986
+ hasLaterWrite(write) {
1987
+ const sequences = this.sequencesByDocument.get(write.documentId) ?? [];
1988
+ return (sequences[sequences.length - 1] ?? 0) > write.sequence;
1989
+ }
1990
+ markUploaded(sequences) {
1991
+ for (const sequence of sequences) {
1992
+ if (this.writes.has(sequence)) this.uploaded.add(sequence);
1993
+ }
1994
+ }
1995
+ forgetUploads() {
1996
+ this.uploaded.clear();
1997
+ }
1998
+ };
1999
+
2000
+ // src/sync/device/session.ts
2001
+ var MAX_CHECKPOINT_ID = 4294967295;
2002
+ function settingsKey(settings) {
2003
+ return JSON.stringify({ bm25: settings.bm25 ?? null, synonyms: settings.synonyms ?? [] });
2004
+ }
2005
+ function createSession(context, progress, state) {
2006
+ const { engine, indexName } = context;
2007
+ let feed = null;
2008
+ let generation = 0;
2009
+ let incoming = Promise.resolve();
2010
+ let hold = null;
2011
+ let nextCheckpointId = 1;
2012
+ let appliedSettings = null;
2013
+ const waiters = /* @__PURE__ */ new Map();
2014
+ let subscribedOnce = false;
2015
+ function sendCheckpoint(onHeads) {
2016
+ const id = nextCheckpointId;
2017
+ nextCheckpointId = id >= MAX_CHECKPOINT_ID ? 1 : id + 1;
2018
+ waiters.set(id, onHeads);
2019
+ feed?.send({ type: "checkpoint", id });
2020
+ }
2021
+ async function applySettings(settings) {
2022
+ state.settings = settings;
2023
+ const key = settingsKey(settings);
2024
+ if (key === appliedSettings) return;
2025
+ await context.work.run(async () => {
2026
+ await engine.updateBM25(indexName, settings.bm25 ?? {});
2027
+ await engine.updateSynonyms(indexName, settings.synonyms ?? []);
2028
+ });
2029
+ appliedSettings = key;
2030
+ }
2031
+ async function completeHold() {
2032
+ const done = hold;
2033
+ if (done === null || done.heads === null || !progress.reached(done.heads)) return;
2034
+ hold = null;
2035
+ await progress.settleUploaded(done.sequences);
2036
+ context.changes.notify();
2037
+ startHold();
2038
+ }
2039
+ function startHold() {
2040
+ if (hold !== null || !state.subscribed || feed === null || state.pendingHold.length === 0) return;
2041
+ const started = { checkpointId: 0, heads: null, sequences: state.pendingHold };
2042
+ state.pendingHold = [];
2043
+ hold = started;
2044
+ sendCheckpoint((heads) => {
2045
+ if (hold !== started || heads === null) return;
2046
+ started.heads = heads;
2047
+ completeHold().catch(context.report);
2048
+ });
2049
+ }
2050
+ function refuse(detail) {
2051
+ throw new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, `The sync feed broke its protocol: ${detail}`);
2052
+ }
2053
+ async function handle(message) {
2054
+ const position = progress.position();
2055
+ if (position === null) refuse("the device holds no copy");
2056
+ switch (message.type) {
2057
+ case "subscribed": {
2058
+ if (message.epoch !== position.epoch || message.partitionCount !== position.seqNos.length) {
2059
+ state.needsSnapshot = "required";
2060
+ feed?.close("epoch");
2061
+ return;
2062
+ }
2063
+ const behind = message.snapshotBytes > 0 && message.bytesBehind > replayLimit() * message.snapshotBytes;
2064
+ if (behind && !state.replayAnyway) {
2065
+ state.needsSnapshot = "behind";
2066
+ feed?.close("behind");
2067
+ return;
2068
+ }
2069
+ state.replayAnyway = false;
2070
+ progress.setFlushThreshold(message.windowBytes);
2071
+ await applySettings(message.settings);
2072
+ state.subscribed = true;
2073
+ subscribedOnce = true;
2074
+ context.changes.notify();
2075
+ startHold();
2076
+ return;
2077
+ }
2078
+ case "entries":
2079
+ if (!state.subscribed) refuse("entries arrived before subscribed");
2080
+ if (message.partitionId >= progress.received().length) refuse("entries name a partition that the index lacks");
2081
+ await progress.keep(message.partitionId, message.entries);
2082
+ await completeHold();
2083
+ context.changes.notify();
2084
+ return;
2085
+ case "settings":
2086
+ await applySettings(message.settings);
2087
+ return;
2088
+ case "checkpoint": {
2089
+ if (message.heads.length !== progress.received().length) refuse("heads name another partition count");
2090
+ const waiter = waiters.get(message.id);
2091
+ waiters.delete(message.id);
2092
+ waiter?.(message.heads);
2093
+ return;
2094
+ }
2095
+ case "resync":
2096
+ state.needsSnapshot = "required";
2097
+ return;
2098
+ }
2099
+ }
2100
+ function replayLimit() {
2101
+ return context.options.replayLimitOfSnapshot ?? DEFAULT_REPLAY_LIMIT_OF_SNAPSHOT;
2102
+ }
2103
+ function closed() {
2104
+ feed = null;
2105
+ generation += 1;
2106
+ state.subscribed = false;
2107
+ progress.setAcknowledge(null);
2108
+ if (hold !== null) {
2109
+ state.pendingHold = [...hold.sequences, ...state.pendingHold];
2110
+ hold = null;
2111
+ }
2112
+ for (const waiter of waiters.values()) waiter(null);
2113
+ waiters.clear();
2114
+ context.changes.notify();
2115
+ }
2116
+ return {
2117
+ async connect(feedUrl) {
2118
+ await incoming;
2119
+ const position = progress.position();
2120
+ if (position === null || context.stopped()) return { code: 0, subscribed: false };
2121
+ let credential;
2122
+ try {
2123
+ credential = await context.credential();
2124
+ } catch (error) {
2125
+ context.report(error);
2126
+ return { code: 0, subscribed: false };
2127
+ }
2128
+ if (context.stopped()) return { code: 0, subscribed: false };
2129
+ const mine = generation;
2130
+ subscribedOnce = false;
2131
+ const code = await new Promise((resolve) => {
2132
+ try {
2133
+ feed = openFeedConnection(feedUrl, credential, {
2134
+ onOpen() {
2135
+ progress.setAcknowledge((seqNos) => feed?.send({ type: "ack", positions: positionsOf2(seqNos) }));
2136
+ feed?.send({ type: "subscribe", epoch: position.epoch, positions: positionsOf2(progress.received()) });
2137
+ },
2138
+ onMessage(message) {
2139
+ incoming = incoming.then(() => generation === mine ? handle(message) : void 0).catch((error) => {
2140
+ context.report(error);
2141
+ if (generation === mine) feed?.close("device error");
2142
+ });
2143
+ },
2144
+ onFailure: context.report,
2145
+ onClose(code2) {
2146
+ closed();
2147
+ resolve(code2);
2148
+ }
2149
+ });
2150
+ } catch (error) {
2151
+ context.report(error);
2152
+ resolve(0);
2153
+ }
2154
+ });
2155
+ return { code, subscribed: subscribedOnce };
2156
+ },
2157
+ close(reason) {
2158
+ feed?.close(reason);
2159
+ },
2160
+ startHold,
2161
+ requestHeads() {
2162
+ if (feed === null || !state.subscribed) return Promise.resolve(null);
2163
+ return new Promise((resolve) => sendCheckpoint(resolve));
2164
+ },
2165
+ holding: () => hold !== null,
2166
+ forgetAppliedSettings() {
2167
+ appliedSettings = null;
2168
+ }
2169
+ };
2170
+ }
2171
+
2172
+ // src/sync/device/analysis.ts
2173
+ function requireAnalysisOnDevice(indexName, analysis) {
2174
+ const missing = [];
2175
+ if (!hasLanguage(analysis.language)) missing.push(`language "${analysis.language}"`);
2176
+ if (analysis.tokenizer !== void 0 && !hasTokenizer(analysis.tokenizer)) {
2177
+ missing.push(`tokenizer "${analysis.tokenizer}"`);
2178
+ }
2179
+ if (analysis.stopWords !== void 0 && !hasStopWords(analysis.stopWords)) {
2180
+ missing.push(`stop words "${analysis.stopWords}"`);
2181
+ }
2182
+ if (missing.length === 0) return;
2183
+ throw new NarsilError(
2184
+ ClientErrorCodes.SYNC_ANALYSIS_UNAVAILABLE,
2185
+ `Index "${indexName}" on the server analyses text with ${missing.join(", ")}, which this device has not registered, so the device keeps its copy. Register them and sync again`,
2186
+ { indexName, missing }
2187
+ );
2188
+ }
2189
+ function analysisIsStale(analysis) {
2190
+ if (analysis.analysisRevision === void 0 || !hasLanguage(analysis.language)) return false;
2191
+ return getLanguage(analysis.language).revision !== analysis.analysisRevision;
2192
+ }
2193
+
2194
+ // src/sync/device/archive-facts.ts
2195
+ var FactsComplete = class {
2196
+ };
2197
+ function unusable(detail) {
2198
+ return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, `The sync snapshot is unusable: ${detail}`);
2199
+ }
2200
+ function optionalString(value) {
2201
+ return typeof value === "string" ? value : void 0;
2202
+ }
2203
+ async function readArchiveFacts(source) {
2204
+ const { createByteReader, decodeIndexConfiguration, readSnapshotArchive } = await loadStorageCode();
2205
+ const reader = createByteReader(source);
2206
+ const read = { config: null, seqNos: null };
2207
+ try {
2208
+ await readSnapshotArchive(reader, {
2209
+ async config(bytes) {
2210
+ read.config = { ...await decodeIndexConfiguration(bytes) };
2211
+ },
2212
+ async manifest(manifest) {
2213
+ const found = new Array(manifest.checkpoint.length).fill(-1);
2214
+ for (const entry of manifest.checkpoint) {
2215
+ if (entry.partitionId >= found.length || found[entry.partitionId] !== -1) {
2216
+ throw unusable("its manifest names a partition twice or skips one");
2217
+ }
2218
+ found[entry.partitionId] = entry.lastSeqNo;
2219
+ }
2220
+ read.seqNos = found;
2221
+ throw new FactsComplete();
2222
+ },
2223
+ async file() {
2224
+ throw unusable("a file comes before its manifest");
2225
+ }
2226
+ });
2227
+ } catch (error) {
2228
+ if (!(error instanceof FactsComplete)) throw error;
2229
+ } finally {
2230
+ await reader.close();
2231
+ }
2232
+ const { config, seqNos } = read;
2233
+ if (config === null || seqNos === null) throw unusable("it holds no configuration or no manifest");
2234
+ if (!isSyncEpoch(config.syncEpoch)) throw unusable("its configuration holds no sync epoch");
2235
+ const language = optionalString(config.language);
2236
+ if (language === void 0) throw unusable("its configuration holds no language");
2237
+ const tokenizer = optionalString(config.tokenizer);
2238
+ const stopWords = optionalString(config.stopWords);
2239
+ const analysisRevision = optionalString(config.analysisRevision);
2240
+ return {
2241
+ epoch: config.syncEpoch,
2242
+ seqNos,
2243
+ analysis: {
2244
+ language,
2245
+ ...tokenizer !== void 0 ? { tokenizer } : {},
2246
+ ...stopWords !== void 0 ? { stopWords } : {},
2247
+ ...analysisRevision !== void 0 ? { analysisRevision } : {}
2248
+ }
2249
+ };
2250
+ }
2251
+
2252
+ // src/sync/device/snapshot.ts
2253
+ var RANGE_NOT_SATISFIABLE = 416;
2254
+ async function startDownload(context) {
2255
+ const saved = await context.store.readDownload();
2256
+ const resume = saved === null ? void 0 : { offset: saved.byteLength, entityTag: saved.entityTag };
2257
+ try {
2258
+ return await context.client.syncSnapshot(context.indexName, resume, await context.requestOptions());
2259
+ } catch (error) {
2260
+ if (resume === void 0 || !(error instanceof NarsilError) || error.details.status !== RANGE_NOT_SATISFIABLE) {
2261
+ throw error;
2262
+ }
2263
+ await context.store.clearDownload();
2264
+ return context.client.syncSnapshot(context.indexName, void 0, await context.requestOptions());
2265
+ }
2266
+ }
2267
+ async function mustDropOldCopy(context, archiveBytes) {
2268
+ if (!holdsIndex(context.engine, context.indexName)) return false;
2269
+ const heap = readHeapStatistics();
2270
+ const requiredBytes = archiveBytes * SNAPSHOT_MEMORY_PER_ARCHIVE_BYTE;
2271
+ if (heap === null || heap.usedBytes + requiredBytes <= heap.limitBytes) return false;
2272
+ if (context.appChoseLoad) return true;
2273
+ const choice = await context.chooseSnapshotLoad({ archiveBytes, requiredBytes, heapLimitBytes: heap.limitBytes });
2274
+ return choice === "keep" ? "keep" : true;
2275
+ }
2276
+ async function saveDownload(context, download) {
2277
+ const reader = download.stream.getReader();
2278
+ let offset = download.offset;
2279
+ let pending = [];
2280
+ let pendingBytes = 0;
2281
+ async function flush() {
2282
+ if (pendingBytes === 0) return;
2283
+ const batch = new Uint8Array(pendingBytes);
2284
+ let at = 0;
2285
+ for (const part of pending) {
2286
+ batch.set(part, at);
2287
+ at += part.byteLength;
2288
+ }
2289
+ await context.store.appendDownload(download.entityTag, offset, batch);
2290
+ offset += pendingBytes;
2291
+ pending = [];
2292
+ pendingBytes = 0;
2293
+ }
2294
+ try {
2295
+ if (download.offset === 0) await context.store.appendDownload(download.entityTag, 0, new Uint8Array(0));
2296
+ for (; ; ) {
2297
+ const { done, value } = await reader.read();
2298
+ if (done) break;
2299
+ if (offset + pendingBytes + value.byteLength > download.totalBytes) {
2300
+ throw new NarsilError(
2301
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
2302
+ "The server sent more bytes than the snapshot holds"
2303
+ );
2304
+ }
2305
+ pending.push(value);
2306
+ pendingBytes += value.byteLength;
2307
+ if (pendingBytes >= DOWNLOAD_SAVE_BYTES) await flush();
2308
+ }
2309
+ await flush();
2310
+ } catch (error) {
2311
+ await flush().catch(() => void 0);
2312
+ await reader.cancel().catch(() => void 0);
2313
+ throw error;
2314
+ } finally {
2315
+ reader.releaseLock();
2316
+ }
2317
+ }
2318
+ function completeDownload(saved, entityTag, totalBytes) {
2319
+ if (saved === null || saved.entityTag !== entityTag || saved.byteLength !== totalBytes) {
2320
+ throw new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, "The snapshot download ended early", {
2321
+ saved: saved?.byteLength ?? 0,
2322
+ totalBytes
2323
+ });
2324
+ }
2325
+ return saved;
2326
+ }
2327
+ async function downloadPublishedSnapshot(context) {
2328
+ if (context.settings !== null) requireAnalysisOnDevice(context.indexName, context.settings);
2329
+ const download = await startDownload(context);
2330
+ const dropOldCopy = await mustDropOldCopy(context, download.totalBytes);
2331
+ if (dropOldCopy === "keep") {
2332
+ await download.stream.cancel().catch(() => void 0);
2333
+ return { kind: "kept" };
2334
+ }
2335
+ await saveDownload(context, download);
2336
+ const saved = completeDownload(await context.store.readDownload(), download.entityTag, download.totalBytes);
2337
+ const facts = await readArchiveFacts(saved.chunks());
2338
+ requireAnalysisOnDevice(context.indexName, facts.analysis);
2339
+ return { kind: "downloaded", saved, facts, dropOldCopy };
2340
+ }
2341
+ async function restoreDownloadedSnapshot(context, downloaded) {
2342
+ await context.store.clearKeptEntries();
2343
+ if (downloaded.dropOldCopy && holdsIndex(context.engine, context.indexName)) {
2344
+ await context.engine.dropIndex(context.indexName);
2345
+ }
2346
+ await context.engine.restore(context.indexName, downloaded.saved.chunks());
2347
+ if (analysisIsStale(downloaded.facts.analysis)) await context.engine.rebuildAnalysis(context.indexName);
2348
+ return { epoch: downloaded.facts.epoch, seqNos: downloaded.facts.seqNos };
2349
+ }
2350
+
2351
+ // src/sync/device/stores/serial.ts
2352
+ function createSerial() {
2353
+ let tail = Promise.resolve();
2354
+ return {
2355
+ run(task) {
2356
+ const result = tail.then(task, task);
2357
+ tail = result.catch(() => void 0);
2358
+ return result;
2359
+ }
2360
+ };
2361
+ }
2362
+
2363
+ // src/sync/device/upload.ts
2364
+ var REFUSED_STATUSES = /* @__PURE__ */ new Set([400, 413, 422]);
2365
+ function refusedBy(error) {
2366
+ if (!(error instanceof NarsilError)) return false;
2367
+ const status = error.details.status;
2368
+ return typeof status === "number" && REFUSED_STATUSES.has(status);
2369
+ }
2370
+ async function sendOne(client, indexName, write, options) {
2371
+ if (write.document !== null) {
2372
+ await client.put(indexName, write.documentId, write.document, void 0, options);
2373
+ return;
2374
+ }
2375
+ try {
2376
+ await client.remove(indexName, write.documentId, void 0, options);
2377
+ } catch (error) {
2378
+ if (!(error instanceof NarsilError) || error.code !== ErrorCodes.DOC_NOT_FOUND) throw error;
2379
+ }
2380
+ }
2381
+ async function sendThroughServer(client, indexName, writes, options) {
2382
+ const outcomes = [];
2383
+ for (const write of writes) {
2384
+ try {
2385
+ await sendOne(client, indexName, write, await options());
2386
+ outcomes.push({ accepted: true });
2387
+ } catch (error) {
2388
+ if (!refusedBy(error)) return { outcomes, failure: error };
2389
+ outcomes.push({ accepted: false, error });
2390
+ }
2391
+ }
2392
+ return { outcomes, failure: null };
2393
+ }
2394
+ async function sendThroughBackend(upload, writes) {
2395
+ let outcomes;
2396
+ try {
2397
+ outcomes = await upload(writes);
2398
+ } catch (error) {
2399
+ return { outcomes: [], failure: error };
2400
+ }
2401
+ if (!Array.isArray(outcomes) || outcomes.length !== writes.length) {
2402
+ return {
2403
+ outcomes: [],
2404
+ failure: new NarsilError(
2405
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
2406
+ `The upload function returned ${Array.isArray(outcomes) ? outcomes.length : "no"} outcomes for ${writes.length} writes`
2407
+ )
2408
+ };
2409
+ }
2410
+ for (const outcome of outcomes) {
2411
+ const valid = outcome !== null && typeof outcome === "object" && (outcome.accepted === true || outcome.accepted === false && outcome.error instanceof NarsilError);
2412
+ if (!valid) {
2413
+ return {
2414
+ outcomes: [],
2415
+ failure: new NarsilError(
2416
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
2417
+ "Each outcome of the upload function must be { accepted: true } or { accepted: false, error }"
2418
+ )
2419
+ };
2420
+ }
2421
+ }
2422
+ return { outcomes, failure: null };
2423
+ }
2424
+
2425
+ // src/sync/device/uploader.ts
2426
+ function createUploader(context, progress, session, state) {
2427
+ const { engine, indexName, store, queue, work, client } = context;
2428
+ let running = null;
2429
+ let again = false;
2430
+ async function serverCopyOf(write, outcome) {
2431
+ if (outcome.serverDocument !== void 0) return outcome.serverDocument;
2432
+ return await client.get(indexName, write.documentId, await context.requestOptions()) ?? null;
2433
+ }
2434
+ async function refuse(write, outcome) {
2435
+ const serverDocument = await serverCopyOf(write, outcome);
2436
+ await work.run(async () => {
2437
+ const covered = queue.coveredBy(write);
2438
+ if (!queue.hasLaterWrite(write)) {
2439
+ if (serverDocument === null) await removeLocally(engine, indexName, write.documentId);
2440
+ else await putLocally(engine, indexName, write.documentId, serverDocument);
2441
+ }
2442
+ await store.removeWrites(covered);
2443
+ queue.remove(covered);
2444
+ progress.forgetDeferred(write.documentId);
2445
+ });
2446
+ try {
2447
+ context.options.onRefusedWrite?.({ write, error: outcome.error, serverDocument });
2448
+ } catch (error) {
2449
+ context.report(error);
2450
+ }
2451
+ }
2452
+ async function settle(batch, result) {
2453
+ let failure = result.failure;
2454
+ for (let at = 0; at < result.outcomes.length; at++) {
2455
+ const write = batch[at];
2456
+ const outcome = result.outcomes[at];
2457
+ if (write === void 0 || outcome === void 0) break;
2458
+ if (outcome.accepted) {
2459
+ const covered = queue.coveredBy(write);
2460
+ queue.markUploaded(covered);
2461
+ state.pendingHold.push(...covered);
2462
+ progress.forgetDeferred(write.documentId);
2463
+ continue;
2464
+ }
2465
+ try {
2466
+ await refuse(write, outcome);
2467
+ } catch (error) {
2468
+ failure ??= error;
2469
+ break;
2470
+ }
2471
+ }
2472
+ return failure;
2473
+ }
2474
+ async function drain() {
2475
+ let failures = 0;
2476
+ while (!context.stopped()) {
2477
+ again = false;
2478
+ const batch = queue.latestAwaitingUpload().slice(0, UPLOAD_BATCH_WRITES);
2479
+ if (batch.length === 0) return;
2480
+ const upload = context.options.upload;
2481
+ const result = upload === void 0 ? await sendThroughServer(client, indexName, batch, context.requestOptions) : await sendThroughBackend(upload, batch);
2482
+ const failure = await settle(batch, result);
2483
+ session.startHold();
2484
+ context.changes.notify();
2485
+ if (failure === null || failure === void 0) {
2486
+ failures = 0;
2487
+ continue;
2488
+ }
2489
+ context.report(failure);
2490
+ await context.pause(backoffDelayMs(failures, UPLOAD_RETRY_DELAY_MS, RETRY_MAX_DELAY_MS));
2491
+ failures += 1;
2492
+ }
2493
+ }
2494
+ function kick() {
2495
+ if (context.stopped()) return;
2496
+ if (running !== null) {
2497
+ again = true;
2498
+ return;
2499
+ }
2500
+ running = drain().catch(context.report).finally(() => {
2501
+ running = null;
2502
+ if (again) kick();
2503
+ });
2504
+ }
2505
+ return {
2506
+ kick,
2507
+ idle: () => running ?? Promise.resolve()
2508
+ };
2509
+ }
2510
+
2511
+ // src/sync/device/index.ts
2512
+ function invalid2(message) {
2513
+ return new NarsilError(ErrorCodes.CONFIG_INVALID, message);
2514
+ }
2515
+ function validated(options) {
2516
+ if (typeof options.indexName !== "string" || options.indexName.length === 0) {
2517
+ throw invalid2("Device sync needs an index name");
2518
+ }
2519
+ const limit = options.replayLimitOfSnapshot;
2520
+ if (limit !== void 0 && (!Number.isFinite(limit) || limit < 0)) {
2521
+ throw invalid2("replayLimitOfSnapshot must be a finite number at or above 0");
2522
+ }
2523
+ }
2524
+ function requireDocumentObject(document) {
2525
+ if (document === null || typeof document !== "object" || Array.isArray(document)) {
2526
+ throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, "A document must be an object");
2527
+ }
2528
+ }
2529
+ function requireDocumentId(documentId) {
2530
+ if (typeof documentId !== "string" || documentId.length === 0) {
2531
+ throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, "A document ID must be a non-empty string");
2532
+ }
2533
+ }
2534
+ function createDeviceSync(options) {
2535
+ validated(options);
2536
+ const { engine, indexName, store } = options;
2537
+ const feedUrl = feedUrlOf(options.url, indexName);
2538
+ const client = createNarsilClient({ url: options.url });
2539
+ const changes = createChanges();
2540
+ let stopped = true;
2541
+ let recovered = null;
2542
+ let loop = null;
2543
+ let appChoseLoad = false;
2544
+ let snapshotDeferred = false;
2545
+ let aborts = new AbortController();
2546
+ async function credential() {
2547
+ return options.credential === void 0 ? null : await options.credential();
2548
+ }
2549
+ const context = {
2550
+ options,
2551
+ engine,
2552
+ indexName,
2553
+ store,
2554
+ client,
2555
+ work: createSerial(),
2556
+ queue: new QueueMirror(),
2557
+ changes,
2558
+ report(error) {
2559
+ try {
2560
+ options.onError?.(error);
2561
+ } catch {
2562
+ }
2563
+ },
2564
+ credential,
2565
+ async requestOptions() {
2566
+ const value = await credential();
2567
+ const signal = aborts.signal;
2568
+ return value === null ? { signal } : { signal, headers: { authorization: `Bearer ${value}` } };
2569
+ },
2570
+ makeDurable: options.makeDurable ?? (() => engine.checkpoint(indexName)),
2571
+ stopped: () => stopped,
2572
+ pause(delayMs) {
2573
+ return new Promise((resolve) => {
2574
+ if (stopped) {
2575
+ resolve();
2576
+ return;
2577
+ }
2578
+ let done = false;
2579
+ const finish = () => {
2580
+ if (done) return;
2581
+ done = true;
2582
+ clearTimeout(timer);
2583
+ resolve();
2584
+ };
2585
+ const timer = setTimeout(finish, delayMs);
2586
+ const watch = () => {
2587
+ if (done) return;
2588
+ if (stopped) finish();
2589
+ else changes.next().then(watch);
2590
+ };
2591
+ changes.next().then(watch);
2592
+ });
2593
+ }
2594
+ };
2595
+ const progress = createProgress(context);
2596
+ const state = {
2597
+ subscribed: false,
2598
+ needsSnapshot: null,
2599
+ replayAnyway: false,
2600
+ pendingHold: [],
2601
+ settings: null
2602
+ };
2603
+ const session = createSession(context, progress, state);
2604
+ const uploader = createUploader(context, progress, session, state);
2605
+ async function loadSnapshot() {
2606
+ const snapshotContext = {
2607
+ engine,
2608
+ indexName,
2609
+ client,
2610
+ store,
2611
+ requestOptions: context.requestOptions,
2612
+ chooseSnapshotLoad: options.chooseSnapshotLoad ?? (async () => "load"),
2613
+ settings: state.settings,
2614
+ appChoseLoad
2615
+ };
2616
+ const downloaded = await downloadPublishedSnapshot(snapshotContext);
2617
+ if (downloaded.kind === "kept") return "kept";
2618
+ await context.work.run(async () => {
2619
+ const position = await restoreDownloadedSnapshot(snapshotContext, downloaded);
2620
+ session.forgetAppliedSettings();
2621
+ await progress.adopt(position);
2622
+ });
2623
+ return "loaded";
2624
+ }
2625
+ async function snapshotStep(attempt) {
2626
+ if (snapshotDeferred && !appChoseLoad) {
2627
+ await changes.next();
2628
+ return attempt;
2629
+ }
2630
+ try {
2631
+ const outcome = await loadSnapshot();
2632
+ if (outcome === "loaded") {
2633
+ state.needsSnapshot = null;
2634
+ appChoseLoad = false;
2635
+ snapshotDeferred = false;
2636
+ } else if (state.needsSnapshot === "behind" && progress.position() !== null) {
2637
+ state.needsSnapshot = null;
2638
+ state.replayAnyway = true;
2639
+ } else {
2640
+ snapshotDeferred = true;
2641
+ }
2642
+ changes.notify();
2643
+ return 0;
2644
+ } catch (error) {
2645
+ context.report(error);
2646
+ await context.pause(backoffDelayMs(attempt, SNAPSHOT_RETRY_DELAY_MS, RETRY_MAX_DELAY_MS));
2647
+ return attempt + 1;
2648
+ }
2649
+ }
2650
+ async function run() {
2651
+ let attempt = 0;
2652
+ while (!stopped) {
2653
+ if (progress.position() === null || state.needsSnapshot !== null) {
2654
+ attempt = await snapshotStep(attempt);
2655
+ continue;
2656
+ }
2657
+ const { code, subscribed } = await session.connect(feedUrl);
2658
+ if (stopped) return;
2659
+ if (subscribed) attempt = 0;
2660
+ if (code === SYNC_CLOSE_RESYNC) state.needsSnapshot ??= "required";
2661
+ if (code === SYNC_CLOSE_CREDENTIAL_EXPIRED || state.needsSnapshot !== null) continue;
2662
+ await context.pause(backoffDelayMs(attempt, RECONNECT_MIN_DELAY_MS, RETRY_MAX_DELAY_MS));
2663
+ attempt += 1;
2664
+ }
2665
+ }
2666
+ async function waitUntil(condition) {
2667
+ while (!condition()) {
2668
+ if (stopped) throw invalid2("Device sync stopped before it settled");
2669
+ await changes.next();
2670
+ }
2671
+ }
2672
+ function requireRecovered() {
2673
+ if (recovered === null) throw invalid2("Call start before writing through device sync");
2674
+ return recovered;
2675
+ }
2676
+ async function write(documentId, document) {
2677
+ requireDocumentId(documentId);
2678
+ if (document !== null) requireDocumentObject(document);
2679
+ if (loadedMessagePack() === null) await loadMessagePack();
2680
+ await requireRecovered();
2681
+ const copy = document === null ? null : portableCopyOf(engine, indexName, document);
2682
+ await context.work.run(async () => {
2683
+ const queued = await store.appendWrite({ documentId, document: copy });
2684
+ context.queue.add(queued);
2685
+ try {
2686
+ if (copy === null) await removeLocally(engine, indexName, documentId);
2687
+ else await putLocally(engine, indexName, documentId, copy);
2688
+ } catch (error) {
2689
+ await store.removeWrites([queued.sequence]);
2690
+ context.queue.remove([queued.sequence]);
2691
+ throw error;
2692
+ }
2693
+ });
2694
+ changes.notify();
2695
+ uploader.kick();
2696
+ }
2697
+ function quiet() {
2698
+ return state.subscribed && context.queue.size === 0 && state.pendingHold.length === 0 && !session.holding() && state.needsSnapshot === null;
2699
+ }
2700
+ return {
2701
+ put: (documentId, document) => write(documentId, document),
2702
+ remove: (documentId) => write(documentId, null),
2703
+ async start() {
2704
+ if (!stopped) return;
2705
+ stopped = false;
2706
+ aborts = new AbortController();
2707
+ recovered ??= loadMessagePack().then(() => progress.recover());
2708
+ try {
2709
+ await recovered;
2710
+ } catch (error) {
2711
+ recovered = null;
2712
+ stopped = true;
2713
+ throw error;
2714
+ }
2715
+ loop = run().catch(context.report);
2716
+ uploader.kick();
2717
+ },
2718
+ async stop() {
2719
+ if (stopped) return;
2720
+ stopped = true;
2721
+ aborts.abort();
2722
+ session.close("stopped");
2723
+ progress.cancelFlush();
2724
+ changes.notify();
2725
+ await Promise.allSettled([loop, uploader.idle()]);
2726
+ loop = null;
2727
+ if (progress.unsavedBytes() > 0) await progress.save().catch(context.report);
2728
+ },
2729
+ async loadSnapshot() {
2730
+ appChoseLoad = true;
2731
+ state.needsSnapshot ??= "required";
2732
+ session.close("snapshot");
2733
+ changes.notify();
2734
+ await waitUntil(() => !appChoseLoad && state.needsSnapshot === null);
2735
+ },
2736
+ async settled() {
2737
+ for (; ; ) {
2738
+ await waitUntil(quiet);
2739
+ const heads = await session.requestHeads();
2740
+ if (heads === null) continue;
2741
+ await waitUntil(() => progress.reached(heads) || !state.subscribed);
2742
+ if (!progress.reached(heads)) continue;
2743
+ await progress.save();
2744
+ if (quiet()) return;
2745
+ }
2746
+ },
2747
+ status() {
2748
+ const position = progress.position();
2749
+ return {
2750
+ connection: stopped ? "stopped" : state.subscribed ? "connected" : "connecting",
2751
+ queuedWrites: context.queue.size,
2752
+ position: position === null ? null : { epoch: position.epoch, seqNos: [...position.seqNos] },
2753
+ snapshotDeferred
2754
+ };
2755
+ }
2756
+ };
2757
+ }
2758
+
2759
+ export { createDeviceSync };