@delali/narsil 0.3.1-next.99 → 0.4.1-next.211

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 +30 -11
  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 +404 -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,1459 @@
1
+ import { NarsilError, ErrorCodes, loadMessagePack, ClientErrorCodes, ServerErrorCodes, describeError } from './chunk-YINZFPLT.mjs';
2
+
3
+ // src/json-encoding.ts
4
+ function toPlainNumbers(key, value) {
5
+ if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
6
+ return Array.from(value);
7
+ }
8
+ return value;
9
+ }
10
+ function encodeJson(value) {
11
+ return JSON.stringify(value, toPlainNumbers);
12
+ }
13
+
14
+ // src/client/constants.ts
15
+ var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
16
+ var NO_TIMEOUT = 0;
17
+ var DEFAULT_TASK_POLL_INTERVAL_MS = 250;
18
+ var DEFAULT_URL_SKIP_MS = 1e3;
19
+ var DEFAULT_MAX_URL_SKIP_MS = 3e4;
20
+ var MAX_VECTOR_FIELD_PATH_SEGMENTS = 64;
21
+ var MAX_CHANGE_DOCUMENT_DEPTH = 64;
22
+
23
+ // src/client/paths.ts
24
+ function indexPath(indexName) {
25
+ return `/indexes/${encodeURIComponent(indexName)}`;
26
+ }
27
+ function documentPath(indexName, docId) {
28
+ return `${indexPath(indexName)}/documents/${encodeURIComponent(docId)}`;
29
+ }
30
+ function socketUrlOf(base, path) {
31
+ const location = globalThis.location?.href;
32
+ let parsed;
33
+ try {
34
+ parsed = location === void 0 ? new URL(base) : new URL(base, location);
35
+ } catch {
36
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, `The client cannot resolve "${base}" to a server`, { url: base });
37
+ }
38
+ parsed.protocol = parsed.protocol === "https:" ? "wss:" : "ws:";
39
+ parsed.search = "";
40
+ parsed.hash = "";
41
+ return `${parsed.toString().replace(/\/+$/, "")}${path}`;
42
+ }
43
+ function backupPath(backupName) {
44
+ return `/backups/${encodeURIComponent(backupName)}`;
45
+ }
46
+ function taskPath(taskId) {
47
+ return `/tasks/${encodeURIComponent(taskId)}`;
48
+ }
49
+
50
+ // src/client/response-shape.ts
51
+ function isRecord(value) {
52
+ return typeof value === "object" && value !== null && !Array.isArray(value);
53
+ }
54
+ function malformed(path, expected) {
55
+ return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, `The answer from ${path} holds no ${expected}`, {
56
+ route: path
57
+ });
58
+ }
59
+ function readArray(payload, key, path) {
60
+ if (!isRecord(payload) || !Array.isArray(payload[key])) throw malformed(path, `"${key}" array`);
61
+ return payload[key];
62
+ }
63
+ function readNumber(payload, key, path) {
64
+ if (!isRecord(payload) || typeof payload[key] !== "number") throw malformed(path, `"${key}" number`);
65
+ return payload[key];
66
+ }
67
+ function readBoolean(payload, key, path) {
68
+ if (!isRecord(payload) || typeof payload[key] !== "boolean") throw malformed(path, `"${key}" flag`);
69
+ return payload[key];
70
+ }
71
+ function readString(payload, key, path) {
72
+ if (!isRecord(payload) || typeof payload[key] !== "string") throw malformed(path, `"${key}" string`);
73
+ return payload[key];
74
+ }
75
+ function readObject(payload, key, path) {
76
+ if (!isRecord(payload) || !isRecord(payload[key])) throw malformed(path, `"${key}" object`);
77
+ return payload[key];
78
+ }
79
+ function readBody(payload, path) {
80
+ if (!isRecord(payload)) throw malformed(path, "JSON object");
81
+ return payload;
82
+ }
83
+
84
+ // src/client/admin.ts
85
+ function createAdminOperations(transport) {
86
+ async function postJson(path, body, options) {
87
+ const payload = await transport.json({
88
+ method: "POST",
89
+ path,
90
+ body: encodeJson(body),
91
+ contentType: "application/json",
92
+ options
93
+ });
94
+ return readBody(payload, path);
95
+ }
96
+ return {
97
+ async checkpoint(indexName, options) {
98
+ await transport.json({ method: "POST", path: `${indexPath(indexName)}/_checkpoint`, options });
99
+ },
100
+ snapshot(indexName, options) {
101
+ const { queryHistory, ...request } = options ?? {};
102
+ return transport.stream({
103
+ method: "GET",
104
+ path: `${indexPath(indexName)}/snapshot`,
105
+ ...queryHistory === true ? { query: { queryHistory: "true" } } : {},
106
+ defaultTimeoutMs: NO_TIMEOUT,
107
+ options: request
108
+ });
109
+ },
110
+ async restore(indexName, data, options) {
111
+ const path = `${indexPath(indexName)}/restore`;
112
+ const payload = await transport.json({
113
+ method: "POST",
114
+ path,
115
+ body: data,
116
+ contentType: "application/octet-stream",
117
+ defaultTimeoutMs: NO_TIMEOUT,
118
+ options
119
+ });
120
+ return readBody(payload, path);
121
+ },
122
+ exportDocuments(indexName, options) {
123
+ return transport.stream({
124
+ method: "GET",
125
+ path: `${indexPath(indexName)}/export`,
126
+ defaultTimeoutMs: NO_TIMEOUT,
127
+ options
128
+ });
129
+ },
130
+ async importDocumentExport(indexName, data, options) {
131
+ const path = `${indexPath(indexName)}/import`;
132
+ const payload = await transport.json({
133
+ method: "POST",
134
+ path,
135
+ body: data,
136
+ contentType: "application/x-ndjson",
137
+ defaultTimeoutMs: NO_TIMEOUT,
138
+ options
139
+ });
140
+ return readBody(payload, path);
141
+ },
142
+ async vectorMaintenanceStatus(indexName, options) {
143
+ const path = `${indexPath(indexName)}/vector-maintenance`;
144
+ const payload = await transport.json({ method: "GET", path, options });
145
+ return readArray(payload, "fields", path);
146
+ },
147
+ async compactVectors(indexName, fieldName, options) {
148
+ await postJson(
149
+ `${indexPath(indexName)}/vectors/_compact`,
150
+ fieldName === void 0 ? {} : { field: fieldName },
151
+ options
152
+ );
153
+ },
154
+ optimizeVectors(indexName, fieldName, options) {
155
+ return postJson(
156
+ `${indexPath(indexName)}/vectors/_optimize`,
157
+ fieldName === void 0 ? {} : { field: fieldName },
158
+ options
159
+ );
160
+ },
161
+ rebalance(indexName, targetPartitionCount, options) {
162
+ return postJson(`${indexPath(indexName)}/_rebalance`, { targetPartitionCount }, options);
163
+ },
164
+ async updatePartitionConfig(indexName, partitionConfig, options) {
165
+ await postJson(`${indexPath(indexName)}/partition-config`, partitionConfig, options);
166
+ },
167
+ async updateSynonyms(indexName, synonyms, options) {
168
+ await postJson(`${indexPath(indexName)}/synonyms`, { synonyms }, options);
169
+ },
170
+ async updateBM25(indexName, bm25, options) {
171
+ await postJson(`${indexPath(indexName)}/bm25`, bm25, options);
172
+ },
173
+ async getSynonyms(indexName, options) {
174
+ const path = `${indexPath(indexName)}/synonyms`;
175
+ const body = await readBody(
176
+ await transport.json({ method: "GET", path, options }),
177
+ path
178
+ );
179
+ return body.synonyms;
180
+ },
181
+ async rebuildAnalysis(indexName, options) {
182
+ const path = `${indexPath(indexName)}/_rebuild-analysis`;
183
+ return readBody(await transport.json({ method: "POST", path, options }), path);
184
+ },
185
+ async changeAnalysis(indexName, change, options) {
186
+ const path = `${indexPath(indexName)}/_change-analysis`;
187
+ return postJson(path, change, options);
188
+ },
189
+ async getMemoryStats(options) {
190
+ const path = "/stats/memory";
191
+ return readBody(await transport.json({ method: "GET", path, options }), path);
192
+ }
193
+ };
194
+ }
195
+
196
+ // src/client/backups.ts
197
+ function createBackupOperations(transport) {
198
+ async function postTask(path, body, options) {
199
+ const payload = await transport.json({
200
+ method: "POST",
201
+ path,
202
+ body: encodeJson(body),
203
+ contentType: "application/json",
204
+ options
205
+ });
206
+ return readBody(payload, path);
207
+ }
208
+ return {
209
+ createBackup(indexName, backupName, options) {
210
+ const { queryHistory, ...request } = options ?? {};
211
+ const body = { backup: backupName, ...queryHistory === true ? { queryHistory } : {} };
212
+ return postTask(`${indexPath(indexName)}/backups`, body, request);
213
+ },
214
+ async listBackups(options) {
215
+ const path = "/backups";
216
+ const payload = await transport.json({ method: "GET", path, defaultTimeoutMs: NO_TIMEOUT, options });
217
+ return readArray(payload, "backups", path);
218
+ },
219
+ restoreBackup(backupName, restore, options) {
220
+ return postTask(`${backupPath(backupName)}/_restore`, restore, options);
221
+ },
222
+ async deleteBackup(backupName, options) {
223
+ const path = backupPath(backupName);
224
+ return readBody(await transport.json({ method: "DELETE", path, options }), path);
225
+ }
226
+ };
227
+ }
228
+
229
+ // src/client/bulk.ts
230
+ function toNdjson(source) {
231
+ if (typeof source === "string") return source;
232
+ if (Array.isArray(source)) return source.map((document) => encodeJson(document)).join("\n");
233
+ return source;
234
+ }
235
+ function toBatchResult(payload, path) {
236
+ const failed = readArray(payload, "failed", path);
237
+ return {
238
+ succeeded: readArray(payload, "succeeded", path),
239
+ failed: failed.map((entry) => {
240
+ const record2 = readBody(entry, path);
241
+ const docId = typeof record2.docId === "string" ? record2.docId : "";
242
+ const error = readBody(record2.error, path);
243
+ const code = typeof error.code === "string" ? error.code : ServerErrorCodes.INTERNAL_ERROR;
244
+ const message = typeof error.message === "string" ? error.message : "The server refused the document";
245
+ const details = typeof error.details === "object" && error.details !== null ? error.details : void 0;
246
+ return { docId, error: new NarsilError(code, message, details) };
247
+ })
248
+ };
249
+ }
250
+ function createBulkOperations(transport) {
251
+ function batch(indexName, body, options) {
252
+ const path = `${indexPath(indexName)}/documents/_batch`;
253
+ return transport.json({ method: "POST", path, body: encodeJson(body), contentType: "application/json", options }).then((payload) => toBatchResult(payload, path));
254
+ }
255
+ function importBody(indexName, source, asynchronous, options) {
256
+ return {
257
+ method: "POST",
258
+ path: `${indexPath(indexName)}/documents/_import`,
259
+ ...asynchronous ? { query: { async: "true" } } : {},
260
+ body: toNdjson(source),
261
+ contentType: "application/x-ndjson",
262
+ defaultTimeoutMs: NO_TIMEOUT,
263
+ options
264
+ };
265
+ }
266
+ return {
267
+ insertBatch(indexName, documents, insertOptions, options) {
268
+ return batch(
269
+ indexName,
270
+ { action: "insert", documents, ...insertOptions === void 0 ? {} : { options: insertOptions } },
271
+ options
272
+ );
273
+ },
274
+ updateBatch(indexName, updates, writeOptions, options) {
275
+ return batch(
276
+ indexName,
277
+ { action: "update", updates, ...writeOptions === void 0 ? {} : { options: writeOptions } },
278
+ options
279
+ );
280
+ },
281
+ removeBatch(indexName, docIds, writeOptions, options) {
282
+ return batch(
283
+ indexName,
284
+ { action: "delete", docIds, ...writeOptions === void 0 ? {} : { options: writeOptions } },
285
+ options
286
+ );
287
+ },
288
+ async getMultiple(indexName, docIds, options) {
289
+ const path = `${indexPath(indexName)}/documents/_multi-get`;
290
+ const payload = await transport.json({
291
+ method: "POST",
292
+ path,
293
+ body: encodeJson({ docIds }),
294
+ contentType: "application/json",
295
+ repeatable: true,
296
+ options
297
+ });
298
+ const documents = readObject(payload, "documents", path);
299
+ return new Map(Object.entries(documents));
300
+ },
301
+ async listDocuments(indexName, params, options) {
302
+ const path = `${indexPath(indexName)}/documents/_list`;
303
+ const payload = await transport.json({
304
+ method: "POST",
305
+ path,
306
+ body: encodeJson(params ?? {}),
307
+ contentType: "application/json",
308
+ repeatable: true,
309
+ options
310
+ });
311
+ return readBody(payload, path);
312
+ },
313
+ async importDocuments(indexName, source, options) {
314
+ const spec = importBody(indexName, source, false, options);
315
+ return readBody(await transport.json(spec), spec.path);
316
+ },
317
+ async startImport(indexName, source, options) {
318
+ const spec = importBody(indexName, source, true, options);
319
+ return readBody(await transport.json(spec), spec.path);
320
+ }
321
+ };
322
+ }
323
+
324
+ // src/changes/wire.ts
325
+ var CHANGES_SUBPROTOCOL = "narsil.changes.v1";
326
+
327
+ // src/client/change-feed-messages.ts
328
+ var CHANGE_TYPES = /* @__PURE__ */ new Set(["put", "insert", "update", "remove"]);
329
+ var RESYNC_REASONS = /* @__PURE__ */ new Set(["epoch", "history", "ahead"]);
330
+ var EPOCH = /^[0-9a-f]{32}$/;
331
+ var FLOAT32_BYTES = 4;
332
+ function malformed2(detail) {
333
+ return new NarsilError(
334
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
335
+ `The change feed sent a malformed message: ${detail}`
336
+ );
337
+ }
338
+ function isRecord2(value) {
339
+ if (value === null || typeof value !== "object") return false;
340
+ const prototype = Object.getPrototypeOf(value);
341
+ return prototype === Object.prototype || prototype === null;
342
+ }
343
+ function isCount(value) {
344
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
345
+ }
346
+ function positionsOf(raw, partitionCount, field) {
347
+ if (!Array.isArray(raw) || raw.length !== partitionCount) throw malformed2(`${field} must hold each partition once`);
348
+ const seen = /* @__PURE__ */ new Set();
349
+ const positions = [];
350
+ for (const item of raw) {
351
+ if (!isRecord2(item) || !isCount(item.partitionId) || !isCount(item.seqNo)) throw malformed2(`${field} is invalid`);
352
+ if (item.partitionId >= partitionCount || seen.has(item.partitionId)) {
353
+ throw malformed2(`${field} must hold each partition once`);
354
+ }
355
+ seen.add(item.partitionId);
356
+ positions.push({ partitionId: item.partitionId, seqNo: item.seqNo });
357
+ }
358
+ return positions;
359
+ }
360
+ function vectorOf(bytes) {
361
+ if (bytes.byteLength % FLOAT32_BYTES !== 0) throw malformed2("a vector has a length that is not whole floats");
362
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
363
+ const vector = new Float32Array(bytes.byteLength / FLOAT32_BYTES);
364
+ for (let at = 0; at < vector.length; at++) vector[at] = view.getFloat32(at * FLOAT32_BYTES, true);
365
+ return vector;
366
+ }
367
+ function restoreVectorAt(document, path) {
368
+ let holder = document;
369
+ for (let at = 0; at < path.length - 1; at++) {
370
+ const segment = path[at];
371
+ if (segment === void 0 || !Object.hasOwn(holder, segment)) return;
372
+ const next = holder[segment];
373
+ if (!isRecord2(next)) return;
374
+ holder = next;
375
+ }
376
+ const leaf = path.at(-1);
377
+ if (leaf === void 0 || !Object.hasOwn(holder, leaf)) return;
378
+ const value = holder[leaf];
379
+ if (!(value instanceof Uint8Array)) return;
380
+ Object.defineProperty(holder, leaf, { value: vectorOf(value), enumerable: true, writable: true, configurable: true });
381
+ }
382
+ function requireShallow(document) {
383
+ const pending = [{ value: document, depth: 0 }];
384
+ for (let next = pending.pop(); next !== void 0; next = pending.pop()) {
385
+ const { value, depth } = next;
386
+ if (depth > MAX_CHANGE_DOCUMENT_DEPTH) throw malformed2("a document nests too deeply");
387
+ const children = Array.isArray(value) ? value : isRecord2(value) ? Object.values(value) : null;
388
+ if (children === null) continue;
389
+ for (const child of children) {
390
+ if (Array.isArray(child) || isRecord2(child)) pending.push({ value: child, depth: depth + 1 });
391
+ }
392
+ }
393
+ }
394
+ function documentOf(raw, field, vectorFields) {
395
+ if (!isRecord2(raw)) throw malformed2(`${field} must be a map`);
396
+ requireShallow(raw);
397
+ for (const path of vectorFields) restoreVectorAt(raw, path);
398
+ return raw;
399
+ }
400
+ function changeOf(raw, vectorFields) {
401
+ if (!isRecord2(raw) || !isCount(raw.seqNo) || typeof raw.documentId !== "string")
402
+ throw malformed2("a change is invalid");
403
+ if (typeof raw.type !== "string" || !CHANGE_TYPES.has(raw.type)) throw malformed2("a change has an unknown type");
404
+ const change = { seqNo: raw.seqNo, type: raw.type, documentId: raw.documentId };
405
+ if (raw.document !== void 0 && raw.document !== null) {
406
+ change.document = documentOf(raw.document, "document", vectorFields);
407
+ }
408
+ if (raw.previousDocument !== void 0 && raw.previousDocument !== null) {
409
+ change.previousDocument = documentOf(raw.previousDocument, "previousDocument", vectorFields);
410
+ }
411
+ return change;
412
+ }
413
+ function vectorFieldsOf(raw) {
414
+ if (!Array.isArray(raw)) throw malformed2("vectorFields must be a list of paths");
415
+ const paths = [];
416
+ for (const item of raw) {
417
+ if (typeof item !== "string") throw malformed2("vectorFields must be a list of paths");
418
+ const path = item.split(".");
419
+ if (path.length > MAX_VECTOR_FIELD_PATH_SEGMENTS || path.includes("")) {
420
+ throw malformed2(`the vector field path ${item} is invalid`);
421
+ }
422
+ paths.push(path);
423
+ }
424
+ return paths;
425
+ }
426
+ function resyncOf(raw) {
427
+ if (typeof raw.reason !== "string" || !RESYNC_REASONS.has(raw.reason)) throw malformed2("the resync reason is unknown");
428
+ return { type: "resync", reason: raw.reason };
429
+ }
430
+ function firstEventOf(raw) {
431
+ if (!isRecord2(raw)) throw malformed2("a message must be a map");
432
+ if (raw.type === "resync") return { event: resyncOf(raw), vectorFields: [] };
433
+ if (raw.type !== "subscribed") throw malformed2("the first message must be subscribed or resync");
434
+ const { epoch, partitionCount, bytesBehind, windowBytes, previousDocument } = raw;
435
+ if (typeof epoch !== "string" || !EPOCH.test(epoch)) throw malformed2("the epoch is invalid");
436
+ if (!isCount(partitionCount) || partitionCount === 0) throw malformed2("the partition count is invalid");
437
+ if (!isCount(bytesBehind) || !isCount(windowBytes) || windowBytes === 0) throw malformed2("a byte count is invalid");
438
+ if (typeof previousDocument !== "boolean") throw malformed2("previousDocument must be a boolean");
439
+ const event = {
440
+ type: "subscribed",
441
+ epoch,
442
+ partitionCount,
443
+ positions: positionsOf(raw.positions, partitionCount, "positions"),
444
+ heads: positionsOf(raw.heads, partitionCount, "heads"),
445
+ bytesBehind,
446
+ previousDocument
447
+ };
448
+ return { event, vectorFields: vectorFieldsOf(raw.vectorFields) };
449
+ }
450
+ function laterEventOf(raw, partitionCount, vectorFields) {
451
+ if (!isRecord2(raw)) throw malformed2("a message must be a map");
452
+ if (raw.type === "resync") return resyncOf(raw);
453
+ if (raw.type !== "changes") throw malformed2("the message type is unknown");
454
+ const { partitionId, scannedSeqNo, changes } = raw;
455
+ if (!isCount(partitionId) || partitionId >= partitionCount) throw malformed2("the partition is unknown");
456
+ if (!isCount(scannedSeqNo) || !Array.isArray(changes)) throw malformed2("the changes are invalid");
457
+ const decoded = changes.map((change) => changeOf(change, vectorFields));
458
+ if (decoded.some((change) => change.seqNo > scannedSeqNo)) throw malformed2("a change lies above scannedSeqNo");
459
+ return { type: "changes", partitionId, changes: decoded, scannedSeqNo };
460
+ }
461
+
462
+ // src/client/change-feed.ts
463
+ var CLOSE_NORMAL = 1e3;
464
+ var CLOSE_CREDENTIAL_EXPIRED = 4401;
465
+ var CLOSE_SETTING = 4409;
466
+ var CLOSE_RESYNC = 4410;
467
+ var CLOSE_POLICY = 1008;
468
+ function closedError(code, reason) {
469
+ if (code === CLOSE_SETTING && reason.length > 0) {
470
+ return new NarsilError(reason, `The server refused the subscription with ${reason}`, { closeCode: code });
471
+ }
472
+ if (code === CLOSE_RESYNC) {
473
+ return new NarsilError(ErrorCodes.CHANGES_NOT_ENABLED, "The index has change capture off", { closeCode: code });
474
+ }
475
+ if (code === CLOSE_POLICY) {
476
+ return new NarsilError(ErrorCodes.CONFIG_INVALID, `The server refused the subscription: ${reason}`, {
477
+ closeCode: code
478
+ });
479
+ }
480
+ const why = code === CLOSE_CREDENTIAL_EXPIRED ? "the credential expired" : `the server closed it with code ${code}`;
481
+ return new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, `The change feed ended because ${why}`, {
482
+ closeCode: code,
483
+ reason
484
+ });
485
+ }
486
+ function subscribeMessage(options) {
487
+ const { epoch, positions, filter } = options;
488
+ if (epoch === void 0 !== (positions === void 0)) {
489
+ throw new NarsilError(ErrorCodes.CHANGES_INVALID_POSITIONS, "Pass epoch and positions together, or leave both out");
490
+ }
491
+ return { type: "subscribe", epoch, positions, filter };
492
+ }
493
+ function openChangeSubscription(url, options) {
494
+ if (typeof WebSocket === "undefined") {
495
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The change feed needs a runtime with a global WebSocket");
496
+ }
497
+ const subscribe = subscribeMessage(options);
498
+ const queue = [];
499
+ let failure = null;
500
+ let ended = false;
501
+ let iterating = false;
502
+ let wake = null;
503
+ let socket = null;
504
+ let codec = null;
505
+ let partitionCount = 0;
506
+ let vectorFields = [];
507
+ const saved = [];
508
+ const notify = () => {
509
+ const waiting = wake;
510
+ wake = null;
511
+ waiting?.();
512
+ };
513
+ function finish(error) {
514
+ if (ended) return;
515
+ ended = true;
516
+ failure = error;
517
+ options.signal?.removeEventListener("abort", close);
518
+ if (socket !== null && socket.readyState <= WebSocket.OPEN) socket.close(CLOSE_NORMAL, "subscriber closed");
519
+ notify();
520
+ }
521
+ function close() {
522
+ finish(null);
523
+ }
524
+ function receive(data, code) {
525
+ if (!(data instanceof ArrayBuffer)) {
526
+ finish(new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, "The change feed sent a text frame"));
527
+ return;
528
+ }
529
+ try {
530
+ const raw = code.decode(new Uint8Array(data));
531
+ let event;
532
+ if (partitionCount === 0) {
533
+ const first = firstEventOf(raw);
534
+ event = first.event;
535
+ vectorFields = first.vectorFields;
536
+ } else {
537
+ event = laterEventOf(raw, partitionCount, vectorFields);
538
+ }
539
+ if (event.type === "subscribed") {
540
+ partitionCount = event.partitionCount;
541
+ saved.push(...new Array(partitionCount).fill(0));
542
+ for (const position of event.positions) saved[position.partitionId] = position.seqNo;
543
+ }
544
+ queue.push(event);
545
+ if (event.type === "resync") {
546
+ finish(null);
547
+ return;
548
+ }
549
+ } catch (error) {
550
+ finish(
551
+ error instanceof NarsilError ? error : new NarsilError(
552
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
553
+ "The change feed sent a message that is not MessagePack",
554
+ { cause: error instanceof Error ? error.message : String(error) }
555
+ )
556
+ );
557
+ return;
558
+ }
559
+ notify();
560
+ }
561
+ function connect(code) {
562
+ codec = code;
563
+ if (ended) return;
564
+ const protocols = options.credential === void 0 ? [CHANGES_SUBPROTOCOL] : [CHANGES_SUBPROTOCOL, options.credential];
565
+ let opened;
566
+ try {
567
+ opened = new WebSocket(url, protocols);
568
+ } catch (error) {
569
+ finish(
570
+ new NarsilError(ErrorCodes.CONFIG_INVALID, "The change feed could not start a WebSocket connection", {
571
+ cause: error instanceof Error ? error.message : String(error)
572
+ })
573
+ );
574
+ return;
575
+ }
576
+ socket = opened;
577
+ opened.binaryType = "arraybuffer";
578
+ opened.onopen = () => {
579
+ if (opened.protocol !== CHANGES_SUBPROTOCOL) {
580
+ finish(
581
+ new NarsilError(
582
+ ClientErrorCodes.CLIENT_INVALID_RESPONSE,
583
+ `The server answered the change feed with the subprotocol "${opened.protocol}"`
584
+ )
585
+ );
586
+ return;
587
+ }
588
+ opened.send(code.encode(subscribe, { ignoreUndefined: true }));
589
+ };
590
+ opened.onmessage = (event) => {
591
+ if (!ended) receive(event.data, code);
592
+ };
593
+ opened.onerror = () => {
594
+ if (!ended) finish(new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, `The change feed at ${url} failed`));
595
+ };
596
+ opened.onclose = (event) => {
597
+ if (!ended) finish(closedError(event.code, event.reason));
598
+ };
599
+ }
600
+ if (options.signal?.aborted === true) ended = true;
601
+ else options.signal?.addEventListener("abort", close);
602
+ if (!ended) {
603
+ loadMessagePack().then(
604
+ connect,
605
+ (error) => finish(
606
+ error instanceof NarsilError ? error : new NarsilError(ClientErrorCodes.CLIENT_UNEXPECTED_ERROR, String(error))
607
+ )
608
+ );
609
+ }
610
+ async function* events() {
611
+ try {
612
+ while (true) {
613
+ const next = queue.shift();
614
+ if (next !== void 0) {
615
+ yield next;
616
+ continue;
617
+ }
618
+ if (ended) {
619
+ if (failure !== null) throw failure;
620
+ return;
621
+ }
622
+ await new Promise((resolve) => {
623
+ wake = resolve;
624
+ });
625
+ }
626
+ } finally {
627
+ close();
628
+ }
629
+ }
630
+ return {
631
+ [Symbol.asyncIterator]() {
632
+ if (iterating) {
633
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "A change subscription returns its events to one loop alone");
634
+ }
635
+ iterating = true;
636
+ return events();
637
+ },
638
+ ack(positions) {
639
+ for (const { partitionId, seqNo } of positions) {
640
+ if (!Number.isSafeInteger(partitionId) || partitionId < 0 || !Number.isSafeInteger(seqNo) || seqNo < 0) {
641
+ throw new NarsilError(
642
+ ErrorCodes.CHANGES_INVALID_POSITIONS,
643
+ "Each acknowledged position needs whole numbers",
644
+ {
645
+ partitionId,
646
+ seqNo
647
+ }
648
+ );
649
+ }
650
+ if (saved.length > 0 && partitionId >= saved.length) {
651
+ throw new NarsilError(ErrorCodes.CHANGES_INVALID_POSITIONS, "The index has no such partition", {
652
+ partitionId,
653
+ partitionCount
654
+ });
655
+ }
656
+ }
657
+ if (ended || socket === null || codec === null || socket.readyState !== WebSocket.OPEN) return;
658
+ if (saved.length === 0) return;
659
+ for (const { partitionId, seqNo } of positions) saved[partitionId] = Math.max(saved[partitionId] ?? 0, seqNo);
660
+ const every = saved.map((seqNo, partitionId) => ({ partitionId, seqNo }));
661
+ socket.send(codec.encode({ type: "ack", positions: every }));
662
+ },
663
+ close
664
+ };
665
+ }
666
+
667
+ // src/client/changes.ts
668
+ function createChangesClientOperations(transport) {
669
+ return {
670
+ async getChanges(indexName, options) {
671
+ const path = `${indexPath(indexName)}/changes`;
672
+ return readBody(await transport.json({ method: "GET", path, options }), path);
673
+ },
674
+ async updateChanges(indexName, setting, options) {
675
+ const path = `${indexPath(indexName)}/changes`;
676
+ await transport.json({ method: "PUT", path, body: encodeJson(setting), contentType: "application/json", options });
677
+ },
678
+ followChanges(indexName, options = {}) {
679
+ return openChangeSubscription(transport.socketUrl(`${indexPath(indexName)}/changes/feed`), options);
680
+ }
681
+ };
682
+ }
683
+
684
+ // src/client/documents.ts
685
+ function createDocumentOperations(transport) {
686
+ return {
687
+ async insert(indexName, document, docId, insertOptions, options) {
688
+ const path = `${indexPath(indexName)}/documents`;
689
+ const payload = await transport.json({
690
+ method: "POST",
691
+ path,
692
+ body: encodeJson({
693
+ document,
694
+ ...docId === void 0 ? {} : { id: docId },
695
+ ...insertOptions === void 0 ? {} : { options: insertOptions }
696
+ }),
697
+ contentType: "application/json",
698
+ options
699
+ });
700
+ return readString(payload, "id", path);
701
+ },
702
+ async get(indexName, docId, options) {
703
+ const path = documentPath(indexName, docId);
704
+ const payload = await transport.jsonOrNull({ method: "GET", path, options }, ErrorCodes.DOC_NOT_FOUND);
705
+ if (payload === null) return void 0;
706
+ return readObject(payload, "document", path);
707
+ },
708
+ async has(indexName, docId, options) {
709
+ const path = `${documentPath(indexName, docId)}/_exists`;
710
+ return readBoolean(await transport.json({ method: "GET", path, options }), "exists", path);
711
+ },
712
+ async put(indexName, docId, document, writeOptions, options) {
713
+ const path = documentPath(indexName, docId);
714
+ const payload = await transport.json({
715
+ method: "PUT",
716
+ path,
717
+ body: encodeJson({ document, ...writeOptions === void 0 ? {} : { options: writeOptions } }),
718
+ contentType: "application/json",
719
+ options
720
+ });
721
+ return { id: readString(payload, "id", path), created: readBoolean(payload, "created", path) };
722
+ },
723
+ async update(indexName, docId, document, writeOptions, options) {
724
+ await transport.json({
725
+ method: "PATCH",
726
+ path: documentPath(indexName, docId),
727
+ body: encodeJson({ document, ...writeOptions === void 0 ? {} : { options: writeOptions } }),
728
+ contentType: "application/json",
729
+ options
730
+ });
731
+ },
732
+ async remove(indexName, docId, writeOptions, options) {
733
+ await transport.json({
734
+ method: "DELETE",
735
+ path: documentPath(indexName, docId),
736
+ ...writeOptions?.wait === true ? { query: { wait: "true" } } : {},
737
+ options
738
+ });
739
+ },
740
+ async waitForWrites(indexName, options) {
741
+ await transport.json({ method: "POST", path: `${indexPath(indexName)}/_wait-for-writes`, options });
742
+ },
743
+ async countDocuments(indexName, options) {
744
+ const path = `${indexPath(indexName)}/count`;
745
+ return readNumber(await transport.json({ method: "GET", path, options }), "count", path);
746
+ }
747
+ };
748
+ }
749
+
750
+ // src/client/failover.ts
751
+ var UNSENT_CONNECTION_CODES = /* @__PURE__ */ new Set(["ECONNREFUSED", "ENOTFOUND", "EAI_AGAIN", "ConnectionRefused"]);
752
+ var MAX_CAUSE_DEPTH = 4;
753
+ var SKIP_DOUBLING_CEILING = 30;
754
+ var RETRIED_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
755
+ function readSkip(value, fallback, field) {
756
+ if (value === void 0) return fallback;
757
+ if (!Number.isSafeInteger(value) || value < 0) {
758
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, `"${field}" must be a whole number of milliseconds, 0 or more`, {
759
+ [field]: value
760
+ });
761
+ }
762
+ return value;
763
+ }
764
+ function createUrlPool(bases, options) {
765
+ const skipMs = readSkip(options.urlSkipMs, DEFAULT_URL_SKIP_MS, "urlSkipMs");
766
+ const maxSkipMs = readSkip(options.maxUrlSkipMs, Math.max(DEFAULT_MAX_URL_SKIP_MS, skipMs), "maxUrlSkipMs");
767
+ if (maxSkipMs < skipMs) {
768
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, '"maxUrlSkipMs" must be at least "urlSkipMs"', {
769
+ urlSkipMs: skipMs,
770
+ maxUrlSkipMs: maxSkipMs
771
+ });
772
+ }
773
+ const entries = bases.map((base, position) => ({ base, position, refusals: 0, skipUntil: 0 }));
774
+ let nextStart = 0;
775
+ return {
776
+ order(now) {
777
+ const rotation = [...entries.slice(nextStart), ...entries.slice(0, nextStart)];
778
+ const open = rotation.filter((entry) => entry.skipUntil <= now);
779
+ const skipped = rotation.filter((entry) => entry.skipUntil > now).sort((a, b) => a.skipUntil - b.skipUntil);
780
+ return [...open, ...skipped];
781
+ },
782
+ took(entry) {
783
+ nextStart = (entry.position + 1) % entries.length;
784
+ },
785
+ answered(entry) {
786
+ entry.refusals = 0;
787
+ entry.skipUntil = 0;
788
+ },
789
+ refused(entry, now) {
790
+ entry.refusals += 1;
791
+ const doublings = Math.min(entry.refusals - 1, SKIP_DOUBLING_CEILING);
792
+ entry.skipUntil = now + Math.min(skipMs * 2 ** doublings, maxSkipMs);
793
+ }
794
+ };
795
+ }
796
+ function failedBeforeSending(error, depth = 0) {
797
+ if (depth > MAX_CAUSE_DEPTH || typeof error !== "object" || error === null) return false;
798
+ const { code, cause, errors } = error;
799
+ if (typeof code === "string" && UNSENT_CONNECTION_CODES.has(code)) return true;
800
+ if (failedBeforeSending(cause, depth + 1)) return true;
801
+ return Array.isArray(errors) && errors.length > 0 && errors.every((entry) => failedBeforeSending(entry, depth + 1));
802
+ }
803
+ function answersUnavailable(status) {
804
+ return RETRIED_STATUSES.has(status);
805
+ }
806
+ function movesOnAfterAnswer(status, code, repeatable) {
807
+ if (!answersUnavailable(status)) return false;
808
+ return repeatable || code === ErrorCodes.NODE_NOT_READY;
809
+ }
810
+ function earnsExtraTry(failure) {
811
+ return failure.code === ClientErrorCodes.CLIENT_CONNECTION_FAILED;
812
+ }
813
+ function movesOnAfterFailure(failure, sentNothing, repeatable) {
814
+ if (failure.code === ClientErrorCodes.CLIENT_REQUEST_TIMEOUT) return repeatable;
815
+ if (failure.code === ClientErrorCodes.CLIENT_CONNECTION_FAILED) return repeatable || sentNothing;
816
+ return false;
817
+ }
818
+
819
+ // src/client/http.ts
820
+ function isRecord3(value) {
821
+ return typeof value === "object" && value !== null && !Array.isArray(value);
822
+ }
823
+ function unrefTimer(timer) {
824
+ const unrefable = timer;
825
+ unrefable.unref?.();
826
+ }
827
+ function startDeadline(timeoutMs, caller) {
828
+ const controller = new AbortController();
829
+ let expired = false;
830
+ const abortWithCaller = () => controller.abort();
831
+ if (caller?.aborted) controller.abort();
832
+ else caller?.addEventListener("abort", abortWithCaller);
833
+ let timer;
834
+ if (timeoutMs > 0 && !controller.signal.aborted) {
835
+ timer = setTimeout(() => {
836
+ expired = true;
837
+ controller.abort();
838
+ }, timeoutMs);
839
+ unrefTimer(timer);
840
+ }
841
+ return {
842
+ signal: controller.signal,
843
+ expired: () => expired,
844
+ release: () => {
845
+ if (timer !== void 0) clearTimeout(timer);
846
+ caller?.removeEventListener("abort", abortWithCaller);
847
+ }
848
+ };
849
+ }
850
+ var ResponseTooLargeError = class extends Error {
851
+ constructor(limitBytes) {
852
+ super(`The answer passed the ${limitBytes} byte ceiling this client reads`);
853
+ this.limitBytes = limitBytes;
854
+ }
855
+ };
856
+ async function readStreamCapped(stream, limitBytes) {
857
+ const reader = stream.getReader();
858
+ const chunks = [];
859
+ let total = 0;
860
+ try {
861
+ for (; ; ) {
862
+ const { done, value } = await reader.read();
863
+ if (done) break;
864
+ total += value.byteLength;
865
+ if (total > limitBytes) throw new ResponseTooLargeError(limitBytes);
866
+ chunks.push(value);
867
+ }
868
+ } finally {
869
+ reader.releaseLock();
870
+ if (total > limitBytes) await stream.cancel().catch(() => void 0);
871
+ }
872
+ const body = new Uint8Array(total);
873
+ let offset = 0;
874
+ for (const chunk of chunks) {
875
+ body.set(chunk, offset);
876
+ offset += chunk.byteLength;
877
+ }
878
+ return body;
879
+ }
880
+ async function readAnswer(response, limitBytes) {
881
+ const stream = response.body;
882
+ if (limitBytes > 0 && stream !== null && stream !== void 0) {
883
+ return new TextDecoder().decode(await readStreamCapped(stream, limitBytes));
884
+ }
885
+ const text = await response.text();
886
+ if (limitBytes > 0 && text.length > limitBytes) throw new ResponseTooLargeError(limitBytes);
887
+ return text;
888
+ }
889
+ function invalidResponse(message, details) {
890
+ return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, message, details);
891
+ }
892
+ function errorFromBody(status, payload, url) {
893
+ const envelope = isRecord3(payload) && isRecord3(payload.error) ? payload.error : void 0;
894
+ if (envelope === void 0 || typeof envelope.code !== "string" || typeof envelope.message !== "string") {
895
+ return invalidResponse(`The server answered HTTP ${status} without a Narsil error envelope`, { url, status });
896
+ }
897
+ const details = isRecord3(envelope.details) ? envelope.details : {};
898
+ return new NarsilError(envelope.code, envelope.message, { ...details, status });
899
+ }
900
+ function isHttpUrl(value) {
901
+ try {
902
+ const parsed = new URL(value);
903
+ return parsed.protocol === "http:" || parsed.protocol === "https:";
904
+ } catch {
905
+ return false;
906
+ }
907
+ }
908
+ function normaliseBase(url) {
909
+ const trimmed = url.trim();
910
+ if (trimmed.length === 0) {
911
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, 'The client needs a server address, and "url" is empty');
912
+ }
913
+ if (!trimmed.startsWith("/") && !isHttpUrl(trimmed)) {
914
+ throw new NarsilError(
915
+ ErrorCodes.CONFIG_INVALID,
916
+ `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`,
917
+ { url: trimmed }
918
+ );
919
+ }
920
+ return trimmed.replace(/\/+$/, "");
921
+ }
922
+ function normaliseBases(url) {
923
+ if (typeof url === "string") return [normaliseBase(url)];
924
+ if (!Array.isArray(url) || url.length === 0) {
925
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, 'The client needs a server address, and "url" lists none');
926
+ }
927
+ return url.map((entry) => {
928
+ if (typeof entry !== "string") {
929
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, 'Every server address in "url" must be a string');
930
+ }
931
+ return normaliseBase(entry);
932
+ });
933
+ }
934
+ function translateFailure(err, url, timeoutMs, expired, caller) {
935
+ if (err instanceof ResponseTooLargeError) {
936
+ return invalidResponse(`The server at ${url} answered more than this client reads: ${err.message}`, {
937
+ url,
938
+ maxResponseBytes: err.limitBytes
939
+ });
940
+ }
941
+ if (expired) {
942
+ return new NarsilError(
943
+ ClientErrorCodes.CLIENT_REQUEST_TIMEOUT,
944
+ `The server at ${url} did not answer within ${timeoutMs} ms`,
945
+ { url, timeoutMs }
946
+ );
947
+ }
948
+ if (caller?.aborted === true) {
949
+ return new NarsilError(ClientErrorCodes.CLIENT_REQUEST_ABORTED, `The caller cancelled the request to ${url}`, {
950
+ url
951
+ });
952
+ }
953
+ return new NarsilError(
954
+ ClientErrorCodes.CLIENT_CONNECTION_FAILED,
955
+ `The client could not reach ${url}: ${describeError(err)}`,
956
+ { url }
957
+ );
958
+ }
959
+ function envelopeCodeOf(text) {
960
+ try {
961
+ const payload = JSON.parse(text);
962
+ return isRecord3(payload) && isRecord3(payload.error) && typeof payload.error.code === "string" ? payload.error.code : void 0;
963
+ } catch {
964
+ return void 0;
965
+ }
966
+ }
967
+ function withQuery(path, query) {
968
+ if (query === void 0) return path;
969
+ const params = new URLSearchParams();
970
+ for (const [key, value] of Object.entries(query)) {
971
+ if (value !== void 0) params.set(key, value);
972
+ }
973
+ const encoded = params.toString();
974
+ return encoded.length === 0 ? path : `${path}?${encoded}`;
975
+ }
976
+ function createTransport(options) {
977
+ const pool = createUrlPool(normaliseBases(options.url), options);
978
+ const send = options.fetch ?? ((input, init) => fetch(input, init));
979
+ const baseHeaders = { ...options.headers };
980
+ if (options.apiKey !== void 0 && options.apiKey.length > 0) {
981
+ baseHeaders.authorization = `Bearer ${options.apiKey}`;
982
+ }
983
+ function resolveTimeout(spec) {
984
+ const perCall = spec.options?.timeoutMs;
985
+ if (perCall !== void 0) return perCall;
986
+ if (options.timeoutMs !== void 0) return options.timeoutMs;
987
+ return spec.defaultTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
988
+ }
989
+ function resolveResponseCeiling(spec) {
990
+ const perCall = spec.options?.maxResponseBytes;
991
+ if (perCall !== void 0) return perCall;
992
+ return options.maxResponseBytes ?? 0;
993
+ }
994
+ function recordAttempt(entry, attempt, repeatable) {
995
+ if (attempt.kind === "answer") {
996
+ if (!answersUnavailable(attempt.status)) {
997
+ pool.answered(entry);
998
+ return false;
999
+ }
1000
+ return movesOnAfterAnswer(attempt.status, envelopeCodeOf(attempt.text), repeatable);
1001
+ }
1002
+ if (attempt.error.code === ClientErrorCodes.CLIENT_CONNECTION_FAILED) pool.refused(entry, Date.now());
1003
+ return movesOnAfterFailure(attempt.error, attempt.sentNothing, repeatable);
1004
+ }
1005
+ function outcomeOf(attempt) {
1006
+ if (attempt === null) throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The client holds no server address");
1007
+ if (attempt.kind === "failure") throw attempt.error;
1008
+ return attempt;
1009
+ }
1010
+ async function exchange(spec, keepsStream = false) {
1011
+ const repeatable = spec.repeatable === true || spec.method === "GET";
1012
+ const replayable = !(spec.body instanceof ReadableStream);
1013
+ const tryOn = async (entry2) => {
1014
+ pool.took(entry2);
1015
+ return exchangeOnce(entry2.base, spec, keepsStream);
1016
+ };
1017
+ let triesOnceMore = repeatable && replayable;
1018
+ let last = null;
1019
+ for (const entry2 of pool.order(Date.now())) {
1020
+ last = await tryOn(entry2);
1021
+ const moves = recordAttempt(entry2, last, repeatable);
1022
+ if (!replayable || !moves) return outcomeOf(last);
1023
+ if (last.kind === "failure" && !earnsExtraTry(last.error)) triesOnceMore = false;
1024
+ }
1025
+ const [entry] = pool.order(Date.now());
1026
+ if (triesOnceMore && entry !== void 0) {
1027
+ last = await tryOn(entry);
1028
+ recordAttempt(entry, last, repeatable);
1029
+ }
1030
+ return outcomeOf(last);
1031
+ }
1032
+ async function exchangeOnce(base, spec, keepsStream) {
1033
+ const url = `${base}${withQuery(spec.path, spec.query)}`;
1034
+ const headers = { ...baseHeaders, ...spec.headers, ...spec.options?.headers };
1035
+ if (spec.contentType !== void 0) headers["content-type"] = spec.contentType;
1036
+ const timeoutMs = resolveTimeout(spec);
1037
+ const deadline = startDeadline(timeoutMs, spec.options?.signal);
1038
+ let handedOver = false;
1039
+ try {
1040
+ const init = { method: spec.method, headers, signal: deadline.signal };
1041
+ if (spec.body !== void 0) init.body = spec.body;
1042
+ if (spec.body instanceof ReadableStream) init.duplex = "half";
1043
+ const response = await send(url, init);
1044
+ if (keepsStream && response.ok && response.body !== null) {
1045
+ handedOver = true;
1046
+ const stream = response.body.pipeThrough(
1047
+ new TransformStream({ flush: () => deadline.release() })
1048
+ );
1049
+ return { kind: "answer", status: response.status, url, text: "", stream, headers: response.headers };
1050
+ }
1051
+ const text = await readAnswer(response, resolveResponseCeiling(spec));
1052
+ return { kind: "answer", status: response.status, url, stream: null, text, headers: response.headers };
1053
+ } catch (err) {
1054
+ const error = translateFailure(err, url, timeoutMs, deadline.expired(), spec.options?.signal);
1055
+ const sentNothing = error.code === ClientErrorCodes.CLIENT_CONNECTION_FAILED && failedBeforeSending(err);
1056
+ return { kind: "failure", error, sentNothing };
1057
+ } finally {
1058
+ if (!handedOver) deadline.release();
1059
+ }
1060
+ }
1061
+ function parse(status, text, path) {
1062
+ if (text.length === 0) return void 0;
1063
+ try {
1064
+ return JSON.parse(text);
1065
+ } catch {
1066
+ throw invalidResponse(`The server answered HTTP ${status} with a body that is not JSON`, { url: path, status });
1067
+ }
1068
+ }
1069
+ async function json(spec) {
1070
+ const { status, text, url } = await exchange(spec);
1071
+ const payload = parse(status, text, url);
1072
+ if (status >= 200 && status < 300) return payload;
1073
+ throw errorFromBody(status, payload, url);
1074
+ }
1075
+ return {
1076
+ json,
1077
+ async jsonOrNull(spec, absentCode) {
1078
+ try {
1079
+ return await json(spec);
1080
+ } catch (err) {
1081
+ if (err instanceof NarsilError && err.code === absentCode) return null;
1082
+ throw err;
1083
+ }
1084
+ },
1085
+ async stream(spec) {
1086
+ const { status, text, stream, url } = await exchange(spec, true);
1087
+ if (stream !== null) return stream;
1088
+ throw errorFromBody(status, parse(status, text, url), url);
1089
+ },
1090
+ async download(spec) {
1091
+ const { status, text, stream, headers, url } = await exchange(spec, true);
1092
+ if (stream !== null && headers !== null) return { status, headers, stream };
1093
+ throw errorFromBody(status, parse(status, text, url), url);
1094
+ },
1095
+ async probe(spec) {
1096
+ const { status } = await exchange(spec);
1097
+ return status;
1098
+ },
1099
+ socketUrl(path) {
1100
+ const [entry] = pool.order(Date.now());
1101
+ if (entry === void 0) throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The client holds no server address");
1102
+ pool.took(entry);
1103
+ return socketUrlOf(entry.base, path);
1104
+ }
1105
+ };
1106
+ }
1107
+
1108
+ // src/client/indexes.ts
1109
+ function createIndexOperations(transport) {
1110
+ return {
1111
+ async createIndex(name, config, options) {
1112
+ await transport.json({
1113
+ method: "POST",
1114
+ path: "/indexes",
1115
+ body: encodeJson({ name, config }),
1116
+ contentType: "application/json",
1117
+ options
1118
+ });
1119
+ },
1120
+ async listIndexes(options) {
1121
+ const path = "/indexes";
1122
+ return readArray(await transport.json({ method: "GET", path, options }), "indexes", path);
1123
+ },
1124
+ async dropIndex(name, options) {
1125
+ await transport.json({ method: "DELETE", path: indexPath(name), options });
1126
+ },
1127
+ async getStats(indexName, options) {
1128
+ const path = `${indexPath(indexName)}/stats`;
1129
+ return readBody(await transport.json({ method: "GET", path, options }), path);
1130
+ },
1131
+ async getPartitionStats(indexName, options) {
1132
+ const path = `${indexPath(indexName)}/partitions`;
1133
+ const payload = await transport.json({ method: "GET", path, options });
1134
+ return readArray(payload, "partitions", path);
1135
+ },
1136
+ async clear(indexName, options) {
1137
+ await transport.json({ method: "POST", path: `${indexPath(indexName)}/_clear`, options });
1138
+ },
1139
+ async open(indexName, options) {
1140
+ await transport.json({ method: "POST", path: `${indexPath(indexName)}/_open`, options });
1141
+ },
1142
+ async close(indexName, options) {
1143
+ await transport.json({ method: "POST", path: `${indexPath(indexName)}/_close`, options });
1144
+ }
1145
+ };
1146
+ }
1147
+
1148
+ // src/client/query-suggestions.ts
1149
+ function createQuerySuggestionOperations(transport) {
1150
+ const pathOf = (indexName, route = "") => `${indexPath(indexName)}/query-suggestions${route}`;
1151
+ function send(method, path, body, options) {
1152
+ return transport.json({
1153
+ method,
1154
+ path,
1155
+ ...body === void 0 ? {} : { body: encodeJson(body), contentType: "application/json" },
1156
+ options
1157
+ });
1158
+ }
1159
+ return {
1160
+ async updateQuerySuggestions(indexName, setting, options) {
1161
+ await send("PUT", pathOf(indexName), setting, options);
1162
+ },
1163
+ async querySuggestionsStatus(indexName, options) {
1164
+ const path = pathOf(indexName);
1165
+ return readBody(await send("GET", path, void 0, options), path);
1166
+ },
1167
+ async recordQuery(indexName, query, options) {
1168
+ const path = pathOf(indexName, "/record");
1169
+ return readBody(await send("POST", path, { query }, options), path);
1170
+ },
1171
+ async buildQuerySuggestions(indexName, options) {
1172
+ const path = pathOf(indexName, "/build");
1173
+ return readBody(await send("POST", path, void 0, options), path);
1174
+ },
1175
+ async previewQuerySuggestions(indexName, limit, options) {
1176
+ const path = pathOf(indexName, "/preview");
1177
+ const payload = await transport.json({
1178
+ method: "GET",
1179
+ path,
1180
+ ...limit === void 0 ? {} : { query: { limit: String(limit) } },
1181
+ options
1182
+ });
1183
+ return readBody(payload, path);
1184
+ },
1185
+ async readQueryBans(indexName, options) {
1186
+ const path = pathOf(indexName, "/bans");
1187
+ return readArray(await send("GET", path, void 0, options), "bans", path);
1188
+ },
1189
+ async replaceQueryBans(indexName, bans, options) {
1190
+ const path = pathOf(indexName, "/bans");
1191
+ return readArray(await send("PUT", path, { bans }, options), "bans", path);
1192
+ },
1193
+ async removeQuery(indexName, query, options) {
1194
+ const path = pathOf(indexName, "/remove");
1195
+ return readNumber(await send("POST", path, { query }, options), "suggestionIndexes", path);
1196
+ },
1197
+ async addManualSuggestion(indexName, suggestion, options) {
1198
+ const path = pathOf(indexName, "/manual");
1199
+ return readString(await send("POST", path, suggestion, options), "indexName", path);
1200
+ }
1201
+ };
1202
+ }
1203
+
1204
+ // src/score-encoding.ts
1205
+ function decodeScore(score) {
1206
+ if (typeof score === "number") return Number.isFinite(score) ? score : void 0;
1207
+ if (score === "NaN") return Number.NaN;
1208
+ if (score === "Infinity") return Number.POSITIVE_INFINITY;
1209
+ if (score === "-Infinity") return Number.NEGATIVE_INFINITY;
1210
+ return void 0;
1211
+ }
1212
+
1213
+ // src/client/query-response.ts
1214
+ function record(value) {
1215
+ return typeof value === "object" && value !== null && !Array.isArray(value);
1216
+ }
1217
+ function invalid(path) {
1218
+ throw new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, "The search response holds an invalid hit score", {
1219
+ route: path
1220
+ });
1221
+ }
1222
+ function decodeHits(hits, path) {
1223
+ if (!Array.isArray(hits)) invalid(path);
1224
+ for (const hit of hits) {
1225
+ if (!record(hit)) invalid(path);
1226
+ if (!Object.hasOwn(hit, "score")) continue;
1227
+ const score = decodeScore(hit.score);
1228
+ if (score === void 0) invalid(path);
1229
+ hit.score = score;
1230
+ }
1231
+ }
1232
+ function decodeQueryResult(payload, path) {
1233
+ if (!record(payload)) invalid(path);
1234
+ decodeHits(payload.hits, path);
1235
+ if (payload.groups !== void 0) {
1236
+ if (!Array.isArray(payload.groups)) invalid(path);
1237
+ for (const group of payload.groups) {
1238
+ if (!record(group)) invalid(path);
1239
+ decodeHits(group.hits, path);
1240
+ }
1241
+ }
1242
+ return payload;
1243
+ }
1244
+
1245
+ // src/client/search.ts
1246
+ function createSearchOperations(transport) {
1247
+ async function post(path, params, options) {
1248
+ const payload = await transport.json({
1249
+ method: "POST",
1250
+ path,
1251
+ body: encodeJson(params),
1252
+ contentType: "application/json",
1253
+ repeatable: true,
1254
+ options
1255
+ });
1256
+ return readBody(payload, path);
1257
+ }
1258
+ return {
1259
+ async query(indexName, params, options) {
1260
+ const path = `${indexPath(indexName)}/search`;
1261
+ return decodeQueryResult(await post(path, params, options), path);
1262
+ },
1263
+ preflight(indexName, params, options) {
1264
+ return post(`${indexPath(indexName)}/search/preflight`, params, options);
1265
+ },
1266
+ suggest(indexName, params, options) {
1267
+ return post(`${indexPath(indexName)}/suggest`, params, options);
1268
+ }
1269
+ };
1270
+ }
1271
+
1272
+ // src/client/server-info.ts
1273
+ function createServerOperations(transport) {
1274
+ let announced = null;
1275
+ async function readCapabilities(options) {
1276
+ const path = "/capabilities";
1277
+ const payload = await transport.jsonOrNull({ method: "GET", path, options }, ServerErrorCodes.NOT_FOUND);
1278
+ if (payload === null) return /* @__PURE__ */ new Set();
1279
+ return new Set(readArray(payload, "capabilities", path));
1280
+ }
1281
+ function load(options) {
1282
+ if (announced === null) {
1283
+ announced = readCapabilities(options).catch((err) => {
1284
+ announced = null;
1285
+ throw err;
1286
+ });
1287
+ }
1288
+ return announced;
1289
+ }
1290
+ return {
1291
+ async version(options) {
1292
+ const path = "/version";
1293
+ return readBody(await transport.json({ method: "GET", path, options }), path);
1294
+ },
1295
+ async capabilities(options) {
1296
+ return [...await load(options)];
1297
+ },
1298
+ async supports(capability, options) {
1299
+ return (await load(options)).has(capability);
1300
+ },
1301
+ async isAlive(options) {
1302
+ return await transport.probe({ method: "GET", path: "/livez", options }) === 200;
1303
+ },
1304
+ async isReady(options) {
1305
+ return await transport.probe({ method: "GET", path: "/readyz", options }) === 200;
1306
+ }
1307
+ };
1308
+ }
1309
+
1310
+ // src/client/sync.ts
1311
+ var ENTITY_TAG = /^"([0-9a-f]{64})"$/;
1312
+ var CONTENT_RANGE = /^bytes (\d+)-(\d+)\/(\d+)$/;
1313
+ function invalidDownload(message, path) {
1314
+ return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, message, { url: path });
1315
+ }
1316
+ function createSyncSettingOperations(transport) {
1317
+ return {
1318
+ async getSync(indexName, options) {
1319
+ const path = `${indexPath(indexName)}/sync`;
1320
+ return readBody(await transport.json({ method: "GET", path, options }), path);
1321
+ },
1322
+ async updateSync(indexName, setting, options) {
1323
+ const path = `${indexPath(indexName)}/sync`;
1324
+ await transport.json({ method: "PUT", path, body: encodeJson(setting), contentType: "application/json", options });
1325
+ },
1326
+ async syncSnapshot(indexName, resume, options) {
1327
+ const path = `${indexPath(indexName)}/sync/snapshot`;
1328
+ const headers = resume === void 0 || resume.offset <= 0 ? {} : { range: `bytes=${resume.offset}-`, "if-range": `"${resume.entityTag}"` };
1329
+ const answer = await transport.download({ method: "GET", path, headers, defaultTimeoutMs: NO_TIMEOUT, options });
1330
+ const entityTag = ENTITY_TAG.exec(answer.headers.get("etag") ?? "")?.[1];
1331
+ if (entityTag === void 0) {
1332
+ await answer.stream.cancel();
1333
+ throw invalidDownload("The server sent the snapshot without an entity tag", path);
1334
+ }
1335
+ if (answer.status === 206) {
1336
+ const range = CONTENT_RANGE.exec(answer.headers.get("content-range") ?? "");
1337
+ if (range === null || Number(range[1]) !== resume?.offset) {
1338
+ await answer.stream.cancel();
1339
+ throw invalidDownload("The server sent a byte range other than the one that the client asked for", path);
1340
+ }
1341
+ return { entityTag, offset: Number(range[1]), totalBytes: Number(range[3]), stream: answer.stream };
1342
+ }
1343
+ const length = Number(answer.headers.get("content-length") ?? Number.NaN);
1344
+ if (!Number.isSafeInteger(length)) {
1345
+ await answer.stream.cancel();
1346
+ throw invalidDownload("The server sent the snapshot without its length", path);
1347
+ }
1348
+ return { entityTag, offset: 0, totalBytes: length, stream: answer.stream };
1349
+ }
1350
+ };
1351
+ }
1352
+
1353
+ // src/client/tasks.ts
1354
+ function isTerminal(record2) {
1355
+ return record2.status === "succeeded" || record2.status === "failed" || record2.status === "cancelled";
1356
+ }
1357
+ function progressKey(record2) {
1358
+ const progress = record2.progress;
1359
+ if (progress === void 0) return record2.status;
1360
+ return `${record2.status}:${progress.bytesProcessed}:${progress.indexed}:${progress.failed}`;
1361
+ }
1362
+ function sleep(milliseconds, signal) {
1363
+ return new Promise((resolve) => {
1364
+ const timer = setTimeout(() => {
1365
+ signal?.removeEventListener("abort", stop);
1366
+ resolve();
1367
+ }, milliseconds);
1368
+ function stop() {
1369
+ clearTimeout(timer);
1370
+ resolve();
1371
+ }
1372
+ signal?.addEventListener("abort", stop, { once: true });
1373
+ });
1374
+ }
1375
+ function listQuery(query) {
1376
+ return {
1377
+ indexName: query?.indexName,
1378
+ type: query?.type === void 0 || query.type.length === 0 ? void 0 : query.type.join(","),
1379
+ status: query?.status === void 0 || query.status.length === 0 ? void 0 : query.status.join(","),
1380
+ from: query?.from === void 0 ? void 0 : String(query.from),
1381
+ limit: query?.limit === void 0 ? void 0 : String(query.limit)
1382
+ };
1383
+ }
1384
+ function createTaskOperations(transport) {
1385
+ const operations = {
1386
+ async getTask(taskId, options) {
1387
+ const path = taskPath(taskId);
1388
+ const payload = await transport.jsonOrNull({ method: "GET", path, options }, ServerErrorCodes.TASK_NOT_FOUND);
1389
+ return payload === null ? null : readBody(payload, path);
1390
+ },
1391
+ async listTasks(query, options) {
1392
+ const path = "/tasks";
1393
+ const payload = await transport.json({ method: "GET", path, query: listQuery(query), options });
1394
+ return readBody(payload, path);
1395
+ },
1396
+ async cancelTask(taskId, options) {
1397
+ const path = `${taskPath(taskId)}/_cancel`;
1398
+ return readBody(await transport.json({ method: "POST", path, options }), path);
1399
+ },
1400
+ async waitForTask(taskId, options) {
1401
+ const interval = options?.pollIntervalMs ?? DEFAULT_TASK_POLL_INTERVAL_MS;
1402
+ const waitTimeoutMs = options?.waitTimeoutMs ?? 0;
1403
+ const startedAt = Date.now();
1404
+ let reported = null;
1405
+ for (; ; ) {
1406
+ if (options?.signal?.aborted === true) {
1407
+ throw new NarsilError(
1408
+ ClientErrorCodes.CLIENT_REQUEST_ABORTED,
1409
+ `The caller stopped waiting for task "${taskId}"`,
1410
+ { taskId }
1411
+ );
1412
+ }
1413
+ const record2 = await operations.getTask(taskId, options);
1414
+ if (record2 === null) {
1415
+ throw new NarsilError(
1416
+ ServerErrorCodes.TASK_NOT_FOUND,
1417
+ `The server no longer holds task "${taskId}", so its outcome is unknown`,
1418
+ { taskId }
1419
+ );
1420
+ }
1421
+ const key = progressKey(record2);
1422
+ if (key !== reported) {
1423
+ reported = key;
1424
+ options?.onProgress?.(record2);
1425
+ }
1426
+ if (isTerminal(record2)) return record2;
1427
+ if (waitTimeoutMs > 0 && Date.now() - startedAt >= waitTimeoutMs) {
1428
+ throw new NarsilError(
1429
+ ClientErrorCodes.CLIENT_TASK_TIMEOUT,
1430
+ `Task "${taskId}" was still ${record2.status} after ${waitTimeoutMs} ms, and it keeps running`,
1431
+ { taskId, waitTimeoutMs, status: record2.status }
1432
+ );
1433
+ }
1434
+ await sleep(interval, options?.signal);
1435
+ }
1436
+ }
1437
+ };
1438
+ return operations;
1439
+ }
1440
+
1441
+ // src/client/client.ts
1442
+ function createNarsilClient(options) {
1443
+ const transport = createTransport(options);
1444
+ return {
1445
+ ...createIndexOperations(transport),
1446
+ ...createDocumentOperations(transport),
1447
+ ...createBulkOperations(transport),
1448
+ ...createSearchOperations(transport),
1449
+ ...createTaskOperations(transport),
1450
+ ...createAdminOperations(transport),
1451
+ ...createBackupOperations(transport),
1452
+ ...createServerOperations(transport),
1453
+ ...createSyncSettingOperations(transport),
1454
+ ...createChangesClientOperations(transport),
1455
+ ...createQuerySuggestionOperations(transport)
1456
+ };
1457
+ }
1458
+
1459
+ export { createNarsilClient };