@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,2070 @@
1
+ // src/query-suggestions/store-host.browser.ts
2
+ var CHECKSUM_YIELD_CHUNK_BYTES;
3
+ var ErrorCodes;
4
+ var GROUP_PATTERN_SOURCE;
5
+ var MAX_FILE_BYTES;
6
+ var NATIVE_CRC32_MIN_BYTES;
7
+ var NarsilError;
8
+ var VERSION;
9
+ var dropEngineIndex;
10
+ var emitWriteEventAfter;
11
+ var flattenSchema;
12
+ var loadGzip;
13
+ var loadSuggestionsCode;
14
+ var encode;
15
+ function bindSuggestionStoreHost(host, msgpack) {
16
+ ({
17
+ CHECKSUM_YIELD_CHUNK_BYTES,
18
+ ErrorCodes,
19
+ GROUP_PATTERN_SOURCE,
20
+ MAX_FILE_BYTES,
21
+ NATIVE_CRC32_MIN_BYTES,
22
+ NarsilError,
23
+ VERSION,
24
+ dropEngineIndex,
25
+ emitWriteEventAfter,
26
+ flattenSchema,
27
+ loadGzip,
28
+ loadSuggestionsCode
29
+ } = host);
30
+ ({ encode } = msgpack);
31
+ }
32
+
33
+ // src/engine/sync/serial.ts
34
+ var chainsByRegistry = /* @__PURE__ */ new WeakMap();
35
+ async function oneSyncChangeAtATime(registry, indexName, action) {
36
+ let chains = chainsByRegistry.get(registry);
37
+ if (chains === void 0) {
38
+ chains = /* @__PURE__ */ new Map();
39
+ chainsByRegistry.set(registry, chains);
40
+ }
41
+ const running = chains;
42
+ const current = (running.get(indexName) ?? Promise.resolve()).catch(() => void 0).then(action);
43
+ running.set(indexName, current);
44
+ try {
45
+ return await current;
46
+ } finally {
47
+ if (running.get(indexName) === current) running.delete(indexName);
48
+ }
49
+ }
50
+
51
+ // src/serialization/crc32.ts
52
+ var IEEE_POLYNOMIAL = 3988292384;
53
+ var ALL_BITS = 4294967295;
54
+ var cachedTable = null;
55
+ function buildTable() {
56
+ const table = new Uint32Array(256);
57
+ for (let i = 0; i < 256; i++) {
58
+ let crc = i;
59
+ for (let bit = 0; bit < 8; bit++) {
60
+ crc = crc & 1 ? crc >>> 1 ^ IEEE_POLYNOMIAL : crc >>> 1;
61
+ }
62
+ table[i] = crc;
63
+ }
64
+ return table;
65
+ }
66
+ function getTable() {
67
+ if (cachedTable === null) {
68
+ cachedTable = buildTable();
69
+ }
70
+ return cachedTable;
71
+ }
72
+ function crc32(data) {
73
+ return crc32Final(crc32Update(crc32Init(), data));
74
+ }
75
+ function crc32Init() {
76
+ return ALL_BITS;
77
+ }
78
+ function crc32Update(state, data) {
79
+ const table = getTable();
80
+ let crc = state;
81
+ for (let i = 0; i < data.length; i++) {
82
+ crc = crc >>> 8 ^ table[(crc ^ data[i]) & 255];
83
+ }
84
+ return crc;
85
+ }
86
+ function crc32Final(state) {
87
+ return (state ^ ALL_BITS) >>> 0;
88
+ }
89
+
90
+ // src/serialization/chunk-checksum.ts
91
+ function yieldToEventLoop() {
92
+ if (typeof setImmediate === "function") {
93
+ return new Promise((resolve) => setImmediate(resolve));
94
+ }
95
+ return new Promise((resolve) => setTimeout(resolve, 0));
96
+ }
97
+ async function advanceChecksum(start, payload) {
98
+ let state = start;
99
+ for (let offset = 0; offset < payload.length; offset += CHECKSUM_YIELD_CHUNK_BYTES) {
100
+ const end = Math.min(offset + CHECKSUM_YIELD_CHUNK_BYTES, payload.length);
101
+ state = crc32Update(state, payload.subarray(offset, end));
102
+ if (end < payload.length) {
103
+ await yieldToEventLoop();
104
+ }
105
+ }
106
+ return state;
107
+ }
108
+ async function checksumOfChunks(chunks) {
109
+ let state = crc32Init();
110
+ for (const chunk of chunks) state = await advanceChecksum(state, chunk);
111
+ return crc32Final(state);
112
+ }
113
+
114
+ // src/serialization/header.ts
115
+ var MAGIC_BYTES = new Uint8Array([78, 82, 83, 76]);
116
+ var HEADER_SIZE = 32;
117
+ var MAX_PAYLOAD_LENGTH = 4294967295;
118
+ var COMPRESSION_ALGORITHMS = {
119
+ 0: "none",
120
+ 1: "gzip",
121
+ 2: "lz4",
122
+ 3: "zstd"
123
+ };
124
+ var COMPRESSION_TO_BITS = {
125
+ none: 0,
126
+ gzip: 1,
127
+ lz4: 2,
128
+ zstd: 3
129
+ };
130
+ function encodeFlags(flags) {
131
+ let bits = 0;
132
+ if (flags.compressionEnabled) bits |= 1;
133
+ bits |= (COMPRESSION_TO_BITS[flags.compressionAlgorithm] & 3) << 1;
134
+ if (flags.checksumPresent) bits |= 1 << 3;
135
+ if (flags.encryptionEnabled) bits |= 1 << 4;
136
+ return bits;
137
+ }
138
+ function decodeFlags(value) {
139
+ const compressionEnabled = (value & 1) === 1;
140
+ const algorithmBits = value >> 1 & 3;
141
+ const checksumPresent = (value >> 3 & 1) === 1;
142
+ const encryptionEnabled = (value >> 4 & 1) === 1;
143
+ return {
144
+ compressionEnabled,
145
+ compressionAlgorithm: COMPRESSION_ALGORITHMS[algorithmBits] ?? "none",
146
+ checksumPresent,
147
+ encryptionEnabled
148
+ };
149
+ }
150
+ function writeHeader(header) {
151
+ if (header.payloadLength > MAX_PAYLOAD_LENGTH) {
152
+ throw new NarsilError(
153
+ ErrorCodes.PERSISTENCE_SAVE_FAILED,
154
+ `A payload of ${header.payloadLength} bytes exceeds the ${MAX_PAYLOAD_LENGTH} bytes the envelope header can describe`,
155
+ { payloadLength: header.payloadLength, maxPayloadLength: MAX_PAYLOAD_LENGTH }
156
+ );
157
+ }
158
+ const buffer = new Uint8Array(HEADER_SIZE);
159
+ const view = new DataView(buffer.buffer);
160
+ buffer.set(MAGIC_BYTES, 0);
161
+ buffer[4] = header.envelopeFormatVersion & 255;
162
+ buffer[5] = header.engineVersionMajor & 255;
163
+ buffer[6] = header.engineVersionMinor & 255;
164
+ buffer[7] = header.engineVersionPatch & 255;
165
+ view.setUint32(8, header.payloadLength, false);
166
+ view.setUint16(12, encodeFlags(header.flags), false);
167
+ view.setUint32(14, header.checksum, false);
168
+ for (let i = 18; i < HEADER_SIZE; i++) {
169
+ buffer[i] = 0;
170
+ }
171
+ return buffer;
172
+ }
173
+ function hasNrslMagic(data) {
174
+ return data.length >= MAGIC_BYTES.length && data[0] === MAGIC_BYTES[0] && data[1] === MAGIC_BYTES[1] && data[2] === MAGIC_BYTES[2] && data[3] === MAGIC_BYTES[3];
175
+ }
176
+ function readHeader(data) {
177
+ if (data.length < HEADER_SIZE) {
178
+ throw new NarsilError(
179
+ ErrorCodes.ENVELOPE_INVALID_MAGIC,
180
+ `Expected at least ${HEADER_SIZE} bytes, received ${data.length}`
181
+ );
182
+ }
183
+ if (data[0] !== MAGIC_BYTES[0] || data[1] !== MAGIC_BYTES[1] || data[2] !== MAGIC_BYTES[2] || data[3] !== MAGIC_BYTES[3]) {
184
+ throw new NarsilError(ErrorCodes.ENVELOPE_INVALID_MAGIC, "File does not begin with NRSL magic bytes");
185
+ }
186
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
187
+ const envelopeFormatVersion = data[4];
188
+ const engineVersionMajor = data[5];
189
+ const engineVersionMinor = data[6];
190
+ const engineVersionPatch = data[7];
191
+ const payloadLength = view.getUint32(8, false);
192
+ const flagBits = view.getUint16(12, false);
193
+ const checksum = view.getUint32(14, false);
194
+ const reserved = data.slice(18, HEADER_SIZE);
195
+ return {
196
+ magic: "NRSL",
197
+ envelopeFormatVersion,
198
+ engineVersionMajor,
199
+ engineVersionMinor,
200
+ engineVersionPatch,
201
+ payloadLength,
202
+ flags: decodeFlags(flagBits),
203
+ checksum,
204
+ reserved
205
+ };
206
+ }
207
+ function payloadByteLength(chunks) {
208
+ let total = 0;
209
+ for (const chunk of chunks) total += chunk.byteLength;
210
+ return total;
211
+ }
212
+ function requireWithinFileLimit(byteLength, what) {
213
+ if (byteLength <= MAX_FILE_BYTES) return;
214
+ throw new NarsilError(
215
+ ErrorCodes.PERSISTENCE_SAVE_FAILED,
216
+ `The ${what} would take ${byteLength} bytes, above the file size limit of ${MAX_FILE_BYTES}`,
217
+ { bytes: byteLength, maximum: MAX_FILE_BYTES }
218
+ );
219
+ }
220
+ function packEnvelopeWithEngineVersion(payloadChunks, envelopeFormatVersion, what, checksum, engineVersion) {
221
+ const payloadLength = payloadByteLength(payloadChunks);
222
+ requireWithinFileLimit(HEADER_SIZE + payloadLength, what);
223
+ const [major, minor, patch] = engineVersion;
224
+ const header = writeHeader({
225
+ envelopeFormatVersion,
226
+ engineVersionMajor: major,
227
+ engineVersionMinor: minor,
228
+ engineVersionPatch: patch,
229
+ payloadLength,
230
+ flags: { compressionEnabled: false, compressionAlgorithm: "none", checksumPresent: true, encryptionEnabled: false },
231
+ checksum});
232
+ return { chunks: [header, ...payloadChunks], byteLength: HEADER_SIZE + payloadLength, checksum };
233
+ }
234
+ function loadFailed(message, details) {
235
+ throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, message, details);
236
+ }
237
+ async function gunzipWithinLimit(compressed, what) {
238
+ const { gunzip } = await loadGzip();
239
+ try {
240
+ return await gunzip(compressed, MAX_FILE_BYTES);
241
+ } catch (error) {
242
+ if (error?.code === "EFBIG") {
243
+ loadFailed(`The ${what} inflates past the file size limit of ${MAX_FILE_BYTES} bytes`);
244
+ }
245
+ loadFailed(`The ${what} holds a gzip payload that fails to inflate`, {
246
+ cause: error instanceof Error ? error.message : String(error)
247
+ });
248
+ }
249
+ }
250
+ async function openChecksummedEnvelope(data, expected) {
251
+ const { envelopeFormatVersion, what } = expected;
252
+ const header = readHeader(data);
253
+ if (header.envelopeFormatVersion !== envelopeFormatVersion) {
254
+ throw new NarsilError(
255
+ ErrorCodes.ENVELOPE_VERSION_MISMATCH,
256
+ `The ${what} holds envelope format v${header.envelopeFormatVersion}, while this build reads it as v${envelopeFormatVersion}`,
257
+ { version: header.envelopeFormatVersion, expected: envelopeFormatVersion }
258
+ );
259
+ }
260
+ if (header.flags.encryptionEnabled) {
261
+ throw new NarsilError(
262
+ ErrorCodes.ENVELOPE_VERSION_MISMATCH,
263
+ `The ${what} is encrypted, which this build cannot read`
264
+ );
265
+ }
266
+ if (header.flags.compressionEnabled && !expected.allowsCompression) {
267
+ loadFailed(`The ${what} is compressed, while its format stores it raw`);
268
+ }
269
+ if (header.flags.compressionEnabled && header.flags.compressionAlgorithm !== "gzip") {
270
+ throw new NarsilError(
271
+ ErrorCodes.ENVELOPE_VERSION_MISMATCH,
272
+ `The ${what} uses ${header.flags.compressionAlgorithm} compression, which this build cannot read`
273
+ );
274
+ }
275
+ if (!header.flags.checksumPresent) loadFailed(`The ${what} carries no checksum, while its format requires one`);
276
+ if (data.byteLength !== HEADER_SIZE + header.payloadLength) {
277
+ loadFailed(
278
+ `The ${what} holds ${data.byteLength} bytes where its header gives ${HEADER_SIZE + header.payloadLength}`,
279
+ { bytes: data.byteLength, expected: HEADER_SIZE + header.payloadLength }
280
+ );
281
+ }
282
+ const payload = data.subarray(HEADER_SIZE);
283
+ const computed = await checksumOfChunks([payload]);
284
+ if (computed !== header.checksum) {
285
+ throw new NarsilError(
286
+ ErrorCodes.PERSISTENCE_CRC_MISMATCH,
287
+ `The checksum of the ${what} does not match its payload`,
288
+ {
289
+ expected: header.checksum,
290
+ computed
291
+ }
292
+ );
293
+ }
294
+ if (header.flags.compressionEnabled) return { header, payload: await gunzipWithinLimit(payload, what) };
295
+ return { header, payload };
296
+ }
297
+
298
+ // src/serialization/envelope-file.ts
299
+ function engineVersionParts() {
300
+ const parts = VERSION.split(".");
301
+ return [
302
+ Number.parseInt(parts[0] ?? "0", 10),
303
+ Number.parseInt(parts[1] ?? "0", 10),
304
+ Number.parseInt(parts[2] ?? "0", 10)
305
+ ];
306
+ }
307
+ async function packChecksummedEnvelope(payloadChunks, envelopeFormatVersion, what) {
308
+ requireWithinFileLimit(HEADER_SIZE + payloadByteLength(payloadChunks), what);
309
+ return packEnvelopeWithChecksum(payloadChunks, envelopeFormatVersion, what, await checksumOfChunks(payloadChunks));
310
+ }
311
+ function packEnvelopeWithChecksum(payloadChunks, envelopeFormatVersion, what, checksum) {
312
+ return packEnvelopeWithEngineVersion(payloadChunks, envelopeFormatVersion, what, checksum, engineVersionParts());
313
+ }
314
+
315
+ // src/query-suggestions/attached.ts
316
+ var runtimes = /* @__PURE__ */ new WeakMap();
317
+ function attachRuntime(core, runtime) {
318
+ runtimes.set(core, runtime);
319
+ }
320
+ function attachedRuntimeOf(core) {
321
+ return runtimes.get(core);
322
+ }
323
+
324
+ // src/query-suggestions/constants.ts
325
+ var QUERY_HISTORY_FOLDER = "_query-counts";
326
+ var RESTORED_HISTORY_FOLDER = "restored";
327
+ var RESTORE_STAGING_INFIX = ".restoring-";
328
+ var MAX_TIMER_DELAY_MS = 2147483647;
329
+ var DEFAULT_PREVIEW_LIMIT = 100;
330
+ var MAX_PREVIEW_LIMIT = 1e3;
331
+ var QUERY_HISTORY_ARCHIVE_PREFIX = "query-history/";
332
+ var MAX_HISTORY_FILE_BYTES = 16777216;
333
+ var MAX_INDEX_UUID_LENGTH = 255;
334
+ var KEY_FINGERPRINT_LABEL = "narsil.query-suggestions.key";
335
+ var MIN_KEY_BYTES = 32;
336
+ var SCRAMBLED_ID_BYTES = 16;
337
+ var KEY_FINGERPRINT_BYTES = 16;
338
+ var DEFAULT_MIN_PEOPLE = 4;
339
+ var MIN_MIN_PEOPLE = 2;
340
+ var MAX_MIN_PEOPLE = 4096;
341
+ var DEFAULT_MIN_RESULTS = 5;
342
+ var MIN_MIN_RESULTS = 1;
343
+ var MAX_MIN_RESULTS = 1e4;
344
+ var DEFAULT_RETENTION_DAYS = 30;
345
+ var MIN_RETENTION_DAYS = 1;
346
+ var MAX_RETENTION_DAYS = 365;
347
+ var DEFAULT_MAX_PHRASES_PER_DAY = 1e4;
348
+ var MAX_MAX_PHRASES_PER_DAY = 16384;
349
+ var DEFAULT_MAX_PHRASES_PER_PERSON = 100;
350
+ var MAX_MAX_PHRASES_PER_PERSON = 1e3;
351
+ var DEFAULT_MAX_PEOPLE_PER_DAY = 1e5;
352
+ var MAX_MAX_PEOPLE_PER_DAY = 1e5;
353
+ var DEFAULT_MAX_GROUPS = 100;
354
+ var MAX_MAX_GROUPS = 1e3;
355
+ var DEFAULT_MIN_LETTERS = 4;
356
+ var DEFAULT_MAX_LENGTH = 100;
357
+ var MAX_MAX_LENGTH = 150;
358
+ var DEFAULT_MAX_WORDS = 8;
359
+ var MAX_MAX_WORDS = 50;
360
+ var DEFAULT_MAX_DIGITS = 5;
361
+ var DEFAULT_MAX_MERGE_PHRASES = 1e5;
362
+ var MAX_MAX_MERGE_PHRASES = 5e5;
363
+ var DEFAULT_MERGE_MEMORY_BYTES = 134217728;
364
+ var MIN_MERGE_MEMORY_BYTES = 16777216;
365
+ var MAX_MERGE_MEMORY_BYTES = 1073741824;
366
+ var DEFAULT_SAVE_INTERVAL_MS = 3e5;
367
+ var MIN_SAVE_INTERVAL_MS = 1e3;
368
+ var MAX_SAVE_INTERVAL_MS = 36e5;
369
+ var DEFAULT_RESULT_CHECKS_PER_SECOND = 20;
370
+ var MAX_RESULT_CHECKS_PER_SECOND = 1e3;
371
+ var DEFAULT_SCHEDULE = "0 3 * * *";
372
+ var SCHEDULE_OFF = "off";
373
+ var DEFAULT_TIME_ZONE = "UTC";
374
+ var MAX_PERSON_PAIRS_PER_DAY = 1e7;
375
+ var MAX_FILE_PEOPLE_PER_DAY = 65536;
376
+ var MAX_MERGE_PEOPLE = 2097152;
377
+ var MAX_PEOPLE_PER_PHRASE_IN_FILE = 4096;
378
+ var MAX_FIELDS = 32;
379
+ var MAX_SETTING_BYTES = 16384;
380
+ var MAX_BANNED_PHRASES = 1e3;
381
+ var MAX_BAN_LIST_BYTES = 262144;
382
+ var MAX_BANNED_PHRASE_CODE_POINTS = 150;
383
+ var MAX_SOURCE_INDEX_NAME_LENGTH = 210;
384
+ var MAX_PERSON_UTF16_UNITS = 256;
385
+ var RAW_PHRASE_UTF16_UNITS_PER_CODE_POINT = 8;
386
+ var PAIR_BLOCK_SLOTS = 16;
387
+ var PAIR_PAGE_BLOCKS = 16384;
388
+ var PERSON_TABLE_MIN_SLOTS = 1024;
389
+ var MIN_PHRASE_TABLE_CAPACITY = 64;
390
+ var MAX_MANUAL_SUGGESTION_COUNT = 4294967295;
391
+ var BYTES_PER_MIB = 1048576;
392
+ var MS_PER_SECOND = 1e3;
393
+ var DAY_MS = 864e5;
394
+ var HISTORY_SWEEP_INTERVAL_MS = 36e5;
395
+ var SCHEDULE_LOOKAHEAD_DAYS = 2922;
396
+ var MAX_SCHEDULE_LENGTH = 256;
397
+ var BUILD_WRITE_BATCH = 500;
398
+ var BUILD_LIST_PAGE = 1e3;
399
+
400
+ // src/query-suggestions/phrase.ts
401
+ var APOSTROPHE_VARIANTS = /[\u2018\u2019\u02bc]/g;
402
+ var HYPHEN_VARIANTS = /[\u2010\u2011]/g;
403
+ var WHITE_SPACE_RUNS = /\p{White_Space}+/gu;
404
+ var EDGE_SPACES = /^ | $/g;
405
+ var ALLOWED_PHRASE = /^[\p{L}\p{M}\p{Nd} '-]+$/u;
406
+ var LETTER = /\p{L}/gu;
407
+ var DIGIT = /\p{Nd}/gu;
408
+ var groupPattern = null;
409
+ function countMatches(text, pattern) {
410
+ pattern.lastIndex = 0;
411
+ let count = 0;
412
+ while (pattern.exec(text) !== null) count++;
413
+ return count;
414
+ }
415
+ function codePointCount(text, stopAfter) {
416
+ let count = 0;
417
+ for (const _ of text) {
418
+ count++;
419
+ if (count > stopAfter) break;
420
+ }
421
+ return count;
422
+ }
423
+ function normalisePhrase(raw, maxLength) {
424
+ if (raw.length === 0 || raw.length > maxLength * RAW_PHRASE_UTF16_UNITS_PER_CODE_POINT) return null;
425
+ return raw.normalize("NFKC").toLowerCase().replace(APOSTROPHE_VARIANTS, "'").replace(HYPHEN_VARIANTS, "-").replace(WHITE_SPACE_RUNS, " ").replace(EDGE_SPACES, "");
426
+ }
427
+ function phraseFits(phrase, limits) {
428
+ if (phrase.length === 0 || phrase.length > limits.maxLength * 2) return false;
429
+ if (phrase.startsWith(" ") || phrase.endsWith(" ") || phrase.includes(" ")) return false;
430
+ if (!ALLOWED_PHRASE.test(phrase)) return false;
431
+ if (codePointCount(phrase, limits.maxLength) > limits.maxLength) return false;
432
+ if (phrase.split(" ").length > limits.maxWords) return false;
433
+ if (countMatches(phrase, DIGIT) > limits.maxDigits) return false;
434
+ return countMatches(phrase, LETTER) >= limits.minLetters;
435
+ }
436
+ function isNormalPhrase(phrase, limits) {
437
+ return phraseFits(phrase, limits) && normalisePhrase(phrase, limits.maxLength) === phrase;
438
+ }
439
+ function acceptedPhrase(raw, limits) {
440
+ const phrase = normalisePhrase(raw, limits.maxLength);
441
+ if (phrase === null || !phraseFits(phrase, limits)) return null;
442
+ return phrase;
443
+ }
444
+ function isGroup(value) {
445
+ groupPattern ??= new RegExp(GROUP_PATTERN_SOURCE);
446
+ return typeof value === "string" && groupPattern.test(value);
447
+ }
448
+ function isPerson(value) {
449
+ return typeof value === "string" && value.length > 0 && value.length <= MAX_PERSON_UTF16_UNITS;
450
+ }
451
+
452
+ // src/query-suggestions/schedule.ts
453
+ var FIELD_RANGES = [
454
+ { min: 0, max: 59 },
455
+ { min: 0, max: 23 },
456
+ { min: 1, max: 31 },
457
+ { min: 1, max: 12 },
458
+ { min: 0, max: 6 }
459
+ ];
460
+ var NUMBER = /^\d{1,2}$/;
461
+ function parseNumber(text, range) {
462
+ if (!NUMBER.test(text)) return null;
463
+ const value = Number(text);
464
+ return value >= range.min && value <= range.max ? value : null;
465
+ }
466
+ function parseField(text, range) {
467
+ const allowed = new Array(range.max + 1).fill(false);
468
+ for (const part of text.split(",")) {
469
+ const [base, stepText, ...extra] = part.split("/");
470
+ if (base === void 0 || extra.length > 0) return null;
471
+ let step = 1;
472
+ if (stepText !== void 0) {
473
+ if (!NUMBER.test(stepText) || Number(stepText) < 1) return null;
474
+ step = Number(stepText);
475
+ }
476
+ let start;
477
+ let end;
478
+ if (base === "*") {
479
+ start = range.min;
480
+ end = range.max;
481
+ } else {
482
+ const bounds = base.split("-");
483
+ if (bounds.length > 2) return null;
484
+ const first = parseNumber(bounds[0] ?? "", range);
485
+ if (first === null) return null;
486
+ start = first;
487
+ if (bounds.length === 2) {
488
+ const last = parseNumber(bounds[1] ?? "", range);
489
+ if (last === null || last < first) return null;
490
+ end = last;
491
+ } else end = stepText === void 0 ? first : range.max;
492
+ }
493
+ for (let value = start; value <= end; value += step) allowed[value] = true;
494
+ }
495
+ return allowed;
496
+ }
497
+ function parseSchedule(expression) {
498
+ if (expression.length > MAX_SCHEDULE_LENGTH) return null;
499
+ const fields = expression.trim().split(/\s+/);
500
+ if (fields.length !== FIELD_RANGES.length) return null;
501
+ const parsed = [];
502
+ for (let i = 0; i < fields.length; i++) {
503
+ const field = parseField(fields[i] ?? "", FIELD_RANGES[i]);
504
+ if (field === null) return null;
505
+ parsed.push(field);
506
+ }
507
+ const [minutes, hours, daysOfMonth, months, daysOfWeek] = parsed;
508
+ return {
509
+ minutes,
510
+ hours,
511
+ daysOfMonth,
512
+ months,
513
+ daysOfWeek,
514
+ dayOfMonthAny: fields[2] === "*",
515
+ dayOfWeekAny: fields[4] === "*"
516
+ };
517
+ }
518
+ var formatters = /* @__PURE__ */ new Map();
519
+ function formatterOf(timeZone) {
520
+ let formatter = formatters.get(timeZone);
521
+ if (formatter === void 0) {
522
+ formatter = new Intl.DateTimeFormat("en-US", {
523
+ timeZone,
524
+ hourCycle: "h23",
525
+ year: "numeric",
526
+ month: "numeric",
527
+ day: "numeric",
528
+ hour: "numeric",
529
+ minute: "numeric"
530
+ });
531
+ formatters.set(timeZone, formatter);
532
+ }
533
+ return formatter;
534
+ }
535
+ function wallTimeAt(instant, timeZone) {
536
+ const parts = {};
537
+ for (const part of formatterOf(timeZone).formatToParts(instant)) {
538
+ if (part.type !== "literal") parts[part.type] = Number(part.value);
539
+ }
540
+ return { year: parts.year, month: parts.month, day: parts.day, hour: parts.hour, minute: parts.minute };
541
+ }
542
+ function wallAsUtc(wall) {
543
+ return Date.UTC(wall.year, wall.month - 1, wall.day, wall.hour, wall.minute);
544
+ }
545
+ function instantOf(wall, timeZone) {
546
+ const target = wallAsUtc(wall);
547
+ let guess = target;
548
+ for (let attempt = 0; attempt < 3; attempt++) {
549
+ const offset = wallAsUtc(wallTimeAt(guess, timeZone)) - guess;
550
+ guess = target - offset;
551
+ }
552
+ const landed = wallTimeAt(guess, timeZone);
553
+ return wallAsUtc(landed) === target ? guess : null;
554
+ }
555
+ function dayMatches(schedule, dayStartUtc) {
556
+ const date = new Date(dayStartUtc);
557
+ if (!schedule.months[date.getUTCMonth() + 1]) return false;
558
+ const monthDay = schedule.daysOfMonth[date.getUTCDate()] === true;
559
+ const weekDay = schedule.daysOfWeek[date.getUTCDay()] === true;
560
+ if (!schedule.dayOfMonthAny && !schedule.dayOfWeekAny) return monthDay || weekDay;
561
+ return monthDay && weekDay;
562
+ }
563
+ function matchesOfDay(schedule, dayStartUtc, timeZone) {
564
+ const date = new Date(dayStartUtc);
565
+ const instants = [];
566
+ for (let hour = 0; hour < 24; hour++) {
567
+ if (!schedule.hours[hour]) continue;
568
+ for (let minute = 0; minute < 60; minute++) {
569
+ if (!schedule.minutes[minute]) continue;
570
+ const wall = { year: date.getUTCFullYear(), month: date.getUTCMonth() + 1, day: date.getUTCDate(), hour, minute };
571
+ const instant = instantOf(wall, timeZone);
572
+ if (instant !== null) instants.push(instant);
573
+ }
574
+ }
575
+ return instants;
576
+ }
577
+ function localDayStart(instant, timeZone) {
578
+ const wall = wallTimeAt(instant, timeZone);
579
+ return Date.UTC(wall.year, wall.month - 1, wall.day);
580
+ }
581
+ function nextMatchAfter(schedule, timeZone, after) {
582
+ const firstDay = localDayStart(after, timeZone);
583
+ for (let day = 0; day <= SCHEDULE_LOOKAHEAD_DAYS; day++) {
584
+ const dayStart = firstDay + day * DAY_MS;
585
+ if (!dayMatches(schedule, dayStart)) continue;
586
+ for (const instant of matchesOfDay(schedule, dayStart, timeZone)) {
587
+ if (instant > after) return instant;
588
+ }
589
+ }
590
+ return null;
591
+ }
592
+ function latestMatchAtOrBefore(schedule, timeZone, at) {
593
+ const firstDay = localDayStart(at, timeZone);
594
+ for (let day = 0; day <= SCHEDULE_LOOKAHEAD_DAYS; day++) {
595
+ const dayStart = firstDay - day * DAY_MS;
596
+ if (!dayMatches(schedule, dayStart)) continue;
597
+ const instants = matchesOfDay(schedule, dayStart, timeZone);
598
+ for (let i = instants.length - 1; i >= 0; i--) {
599
+ if (instants[i] <= at) return instants[i];
600
+ }
601
+ }
602
+ return null;
603
+ }
604
+
605
+ // src/query-suggestions/setting.ts
606
+ var RANGES = [
607
+ {
608
+ key: "minPeople",
609
+ wire: "min_people",
610
+ fallback: DEFAULT_MIN_PEOPLE,
611
+ min: MIN_MIN_PEOPLE,
612
+ max: MAX_MIN_PEOPLE
613
+ },
614
+ {
615
+ key: "minResults",
616
+ wire: "min_results",
617
+ fallback: DEFAULT_MIN_RESULTS,
618
+ min: MIN_MIN_RESULTS,
619
+ max: MAX_MIN_RESULTS
620
+ },
621
+ {
622
+ key: "retentionDays",
623
+ wire: "retention_days",
624
+ fallback: DEFAULT_RETENTION_DAYS,
625
+ min: MIN_RETENTION_DAYS,
626
+ max: MAX_RETENTION_DAYS
627
+ },
628
+ {
629
+ key: "maxPhrasesPerDay",
630
+ wire: "max_phrases_per_day",
631
+ fallback: DEFAULT_MAX_PHRASES_PER_DAY,
632
+ min: 1,
633
+ max: MAX_MAX_PHRASES_PER_DAY
634
+ },
635
+ {
636
+ key: "maxPhrasesPerPerson",
637
+ wire: "max_phrases_per_person",
638
+ fallback: DEFAULT_MAX_PHRASES_PER_PERSON,
639
+ min: 1,
640
+ max: MAX_MAX_PHRASES_PER_PERSON
641
+ },
642
+ {
643
+ key: "maxPeoplePerDay",
644
+ wire: "max_people_per_day",
645
+ fallback: DEFAULT_MAX_PEOPLE_PER_DAY,
646
+ min: 1,
647
+ max: MAX_MAX_PEOPLE_PER_DAY
648
+ },
649
+ { key: "maxGroups", wire: "max_groups", fallback: DEFAULT_MAX_GROUPS, min: 1, max: MAX_MAX_GROUPS },
650
+ { key: "maxLength", wire: "max_length", fallback: DEFAULT_MAX_LENGTH, min: 1, max: MAX_MAX_LENGTH },
651
+ { key: "minLetters", wire: "min_letters", fallback: DEFAULT_MIN_LETTERS, min: 1, max: "maxLength" },
652
+ { key: "maxWords", wire: "max_words", fallback: DEFAULT_MAX_WORDS, min: 1, max: MAX_MAX_WORDS },
653
+ { key: "maxDigits", wire: "max_digits", fallback: DEFAULT_MAX_DIGITS, min: 0, max: "maxLength" },
654
+ {
655
+ key: "maxMergePhrases",
656
+ wire: "max_merge_phrases",
657
+ fallback: DEFAULT_MAX_MERGE_PHRASES,
658
+ min: 1,
659
+ max: MAX_MAX_MERGE_PHRASES
660
+ },
661
+ {
662
+ key: "mergeMemoryBytes",
663
+ wire: "merge_memory_bytes",
664
+ fallback: DEFAULT_MERGE_MEMORY_BYTES,
665
+ min: MIN_MERGE_MEMORY_BYTES,
666
+ max: MAX_MERGE_MEMORY_BYTES
667
+ },
668
+ {
669
+ key: "saveIntervalMs",
670
+ wire: "save_interval_ms",
671
+ fallback: DEFAULT_SAVE_INTERVAL_MS,
672
+ min: MIN_SAVE_INTERVAL_MS,
673
+ max: MAX_SAVE_INTERVAL_MS
674
+ },
675
+ {
676
+ key: "resultChecksPerSecond",
677
+ wire: "result_checks_per_second",
678
+ fallback: DEFAULT_RESULT_CHECKS_PER_SECOND,
679
+ min: 1,
680
+ max: MAX_RESULT_CHECKS_PER_SECOND
681
+ }
682
+ ];
683
+ var SETTING_KEYS = /* @__PURE__ */ new Set(["enabled", "fields", "schedule", "timeZone", ...RANGES.map((range) => range.key)]);
684
+ function invalidSetting(message, details = {}) {
685
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, message, details);
686
+ }
687
+ function isWholeNumber(value) {
688
+ return typeof value === "number" && Number.isSafeInteger(value);
689
+ }
690
+ function knowsTimeZone(timeZone) {
691
+ try {
692
+ new Intl.DateTimeFormat("en-US", { timeZone });
693
+ return true;
694
+ } catch {
695
+ return false;
696
+ }
697
+ }
698
+ function resolveSetting(setting) {
699
+ const resolved = {
700
+ enabled: setting.enabled,
701
+ fields: [...setting.fields],
702
+ schedule: setting.schedule ?? DEFAULT_SCHEDULE,
703
+ timeZone: setting.timeZone ?? DEFAULT_TIME_ZONE
704
+ };
705
+ for (const range of RANGES) resolved[range.key] = setting[range.key] ?? range.fallback;
706
+ return resolved;
707
+ }
708
+ function requireProducts(resolved) {
709
+ if (resolved.maxPeoplePerDay * resolved.maxPhrasesPerPerson > MAX_PERSON_PAIRS_PER_DAY) {
710
+ invalidSetting(`maxPeoplePerDay times maxPhrasesPerPerson must stay at or below ${MAX_PERSON_PAIRS_PER_DAY}`);
711
+ }
712
+ if (resolved.maxPhrasesPerDay * resolved.minPeople > MAX_FILE_PEOPLE_PER_DAY) {
713
+ invalidSetting(`maxPhrasesPerDay times minPeople must stay at or below ${MAX_FILE_PEOPLE_PER_DAY}`);
714
+ }
715
+ if (resolved.maxMergePhrases * resolved.minPeople > MAX_MERGE_PEOPLE) {
716
+ invalidSetting(`maxMergePhrases times minPeople must stay at or below ${MAX_MERGE_PEOPLE}`);
717
+ }
718
+ }
719
+ function validateSetting(raw) {
720
+ if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
721
+ invalidSetting("The query suggestions setting must be an object");
722
+ }
723
+ const input = raw;
724
+ for (const key of Object.keys(input)) {
725
+ if (!SETTING_KEYS.has(key)) invalidSetting(`The query suggestions setting holds an unknown key "${key}"`, { key });
726
+ }
727
+ if (typeof input.enabled !== "boolean") invalidSetting('The query suggestions setting needs a boolean "enabled"');
728
+ const fields = input.fields;
729
+ if (!Array.isArray(fields) || fields.length === 0 || fields.length > MAX_FIELDS) {
730
+ invalidSetting(`The query suggestions setting needs from 1 to ${MAX_FIELDS} fields`);
731
+ }
732
+ const seen = /* @__PURE__ */ new Set();
733
+ for (const field of fields) {
734
+ if (typeof field !== "string" || field.length === 0 || seen.has(field)) {
735
+ invalidSetting("Each field of the query suggestions setting must be a distinct, non-empty path");
736
+ }
737
+ seen.add(field);
738
+ }
739
+ const setting = { enabled: input.enabled, fields: [...seen] };
740
+ for (const range of RANGES) {
741
+ const value = input[range.key];
742
+ if (value === void 0) continue;
743
+ if (!isWholeNumber(value)) invalidSetting(`${range.key} must be a whole number`, { key: range.key });
744
+ setting[range.key] = value;
745
+ }
746
+ const resolved = resolveSetting(setting);
747
+ for (const range of RANGES) {
748
+ const value = resolved[range.key];
749
+ const max = typeof range.max === "number" ? range.max : resolved[range.max];
750
+ if (value < range.min || value > max) {
751
+ invalidSetting(`${range.key} must be from ${range.min} to ${max}`, { key: range.key, value });
752
+ }
753
+ }
754
+ requireProducts(resolved);
755
+ if (input.schedule !== void 0) {
756
+ if (typeof input.schedule !== "string") invalidSetting("schedule must be a string");
757
+ if (input.schedule !== SCHEDULE_OFF && parseSchedule(input.schedule) === null) {
758
+ invalidSetting('schedule must be "off" or a cron expression of five fields', { schedule: input.schedule });
759
+ }
760
+ setting.schedule = input.schedule;
761
+ }
762
+ if (input.timeZone !== void 0) {
763
+ if (typeof input.timeZone !== "string" || !knowsTimeZone(input.timeZone)) {
764
+ invalidSetting("timeZone must name an IANA time zone", { timeZone: String(input.timeZone) });
765
+ }
766
+ setting.timeZone = input.timeZone;
767
+ }
768
+ if (encode(settingToWire(setting)).byteLength > MAX_SETTING_BYTES) {
769
+ invalidSetting(`The query suggestions setting must encode to at most ${MAX_SETTING_BYTES} bytes`);
770
+ }
771
+ return setting;
772
+ }
773
+ function requireFieldsInSchema(setting, schema) {
774
+ const flat = flattenSchema(schema);
775
+ for (const field of setting.fields) {
776
+ const type = flat[field];
777
+ if (type !== "string" && type !== "string[]") {
778
+ invalidSetting(`Field "${field}" must be a string or string[] field of the schema`, { field });
779
+ }
780
+ }
781
+ }
782
+ function settingToWire(setting) {
783
+ const wire = { enabled: setting.enabled, fields: [...setting.fields] };
784
+ for (const range of RANGES) {
785
+ const value = setting[range.key];
786
+ if (value !== void 0) wire[range.wire] = value;
787
+ }
788
+ if (setting.schedule !== void 0) wire.schedule = setting.schedule;
789
+ if (setting.timeZone !== void 0) wire.time_zone = setting.timeZone;
790
+ return wire;
791
+ }
792
+ function settingFromWire(raw) {
793
+ if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
794
+ invalidSetting("The stored query suggestions setting is not a map");
795
+ }
796
+ const wire = raw;
797
+ const camel = {};
798
+ const byWire = new Map([
799
+ ["enabled", "enabled"],
800
+ ["fields", "fields"],
801
+ ["schedule", "schedule"],
802
+ ["time_zone", "timeZone"],
803
+ ...RANGES.map((range) => [range.wire, range.key])
804
+ ]);
805
+ for (const [key, value] of Object.entries(wire)) {
806
+ const camelKey = byWire.get(key);
807
+ if (camelKey === void 0) invalidSetting(`The stored query suggestions setting holds an unknown key "${key}"`);
808
+ camel[camelKey] = value;
809
+ }
810
+ return validateSetting(camel);
811
+ }
812
+ function validateSource(raw) {
813
+ if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
814
+ invalidSetting("The suggestions index marker must be a map");
815
+ }
816
+ const { index, group, ...rest } = raw;
817
+ if (Object.keys(rest).length > 0) invalidSetting("The suggestions index marker holds an unknown key");
818
+ if (typeof index !== "string" || index.length === 0 || index.length > MAX_SOURCE_INDEX_NAME_LENGTH) {
819
+ invalidSetting("The suggestions index marker needs the name of its source index");
820
+ }
821
+ if (!isGroup(group)) {
822
+ invalidSetting("The suggestions index marker needs a valid group");
823
+ }
824
+ return { index, group };
825
+ }
826
+
827
+ // src/query-suggestions/bans.ts
828
+ var BAN_LIMITS = {
829
+ minLetters: 0,
830
+ maxLength: MAX_BANNED_PHRASE_CODE_POINTS,
831
+ maxWords: MAX_BANNED_PHRASE_CODE_POINTS,
832
+ maxDigits: MAX_BANNED_PHRASE_CODE_POINTS
833
+ };
834
+ function requireWithinLimits(phrases) {
835
+ if (phrases.length > MAX_BANNED_PHRASES) {
836
+ invalidSetting(`A ban list holds at most ${MAX_BANNED_PHRASES} phrases`, { phrases: phrases.length });
837
+ }
838
+ if (encode(phrases).byteLength > MAX_BAN_LIST_BYTES) {
839
+ invalidSetting(`A ban list must encode to at most ${MAX_BAN_LIST_BYTES} bytes`);
840
+ }
841
+ }
842
+ function normaliseBan(raw) {
843
+ if (typeof raw !== "string") invalidSetting("Each banned phrase must be a string");
844
+ const phrase = acceptedPhrase(raw, BAN_LIMITS);
845
+ if (phrase === null) {
846
+ invalidSetting(
847
+ `A banned phrase must hold up to ${MAX_BANNED_PHRASE_CODE_POINTS} letters, digits, spaces, hyphens, and apostrophes`
848
+ );
849
+ }
850
+ return phrase;
851
+ }
852
+ function banListFromInput(raw) {
853
+ if (!Array.isArray(raw)) invalidSetting("A ban list must be an array of phrases");
854
+ if (raw.length > MAX_BANNED_PHRASES) {
855
+ invalidSetting(`A ban list holds at most ${MAX_BANNED_PHRASES} phrases`, { phrases: raw.length });
856
+ }
857
+ const phrases = [...new Set(raw.map(normaliseBan))];
858
+ requireWithinLimits(phrases);
859
+ return phrases;
860
+ }
861
+ function storedBanList(raw) {
862
+ if (!Array.isArray(raw)) invalidSetting("The stored ban list is not an array");
863
+ const seen = /* @__PURE__ */ new Set();
864
+ for (const phrase of raw) {
865
+ if (typeof phrase !== "string" || !isNormalPhrase(phrase, BAN_LIMITS) || seen.has(phrase)) {
866
+ invalidSetting("The stored ban list holds a phrase that is not in normal form, or one twice");
867
+ }
868
+ seen.add(phrase);
869
+ }
870
+ const phrases = [...seen];
871
+ requireWithinLimits(phrases);
872
+ return phrases;
873
+ }
874
+ function unionOfBans(first, second) {
875
+ const union = [.../* @__PURE__ */ new Set([...first, ...second])];
876
+ requireWithinLimits(union);
877
+ return union;
878
+ }
879
+
880
+ // src/query-suggestions/msgpack-cursor.ts
881
+ var MalformedPayload = class extends Error {
882
+ };
883
+ function utf8ByteLength(text) {
884
+ let bytes = 0;
885
+ for (let i = 0; i < text.length; i++) {
886
+ const unit = text.charCodeAt(i);
887
+ if (unit < 128) bytes += 1;
888
+ else if (unit < 2048) bytes += 2;
889
+ else if (unit >= 55296 && unit <= 56319 && i + 1 < text.length) {
890
+ const next = text.charCodeAt(i + 1);
891
+ if (next >= 56320 && next <= 57343) {
892
+ bytes += 4;
893
+ i++;
894
+ } else bytes += 3;
895
+ } else bytes += 3;
896
+ }
897
+ return bytes;
898
+ }
899
+ function headerBytes(length, fixLimit) {
900
+ if (length <= fixLimit) return 1;
901
+ if (length <= 255) return 2;
902
+ if (length <= 65535) return 3;
903
+ return 5;
904
+ }
905
+ function stringSize(text) {
906
+ const length = utf8ByteLength(text);
907
+ return headerBytes(length, 31) + length;
908
+ }
909
+ function binarySize(length) {
910
+ return (length <= 255 ? 2 : length <= 65535 ? 3 : 5) + length;
911
+ }
912
+ function containerHeaderSize(length) {
913
+ if (length <= 15) return 1;
914
+ return length <= 65535 ? 3 : 5;
915
+ }
916
+ function uintSize(value) {
917
+ if (value < 128) return 1;
918
+ if (value <= 255) return 2;
919
+ if (value <= 65535) return 3;
920
+ if (value <= 4294967295) return 5;
921
+ return 9;
922
+ }
923
+ var ExactWriter = class {
924
+ bytes;
925
+ view;
926
+ position;
927
+ encoder = new TextEncoder();
928
+ constructor(bytes, start) {
929
+ this.bytes = bytes;
930
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
931
+ this.position = start;
932
+ }
933
+ get offset() {
934
+ return this.position;
935
+ }
936
+ skip(length) {
937
+ this.position += length;
938
+ }
939
+ map(length) {
940
+ this.container(length, 128, 222, 223);
941
+ }
942
+ array(length) {
943
+ this.container(length, 144, 220, 221);
944
+ }
945
+ string(text) {
946
+ const length = utf8ByteLength(text);
947
+ if (length <= 31) this.bytes[this.position++] = 160 | length;
948
+ else if (length <= 255) {
949
+ this.bytes[this.position++] = 217;
950
+ this.bytes[this.position++] = length;
951
+ } else if (length <= 65535) {
952
+ this.bytes[this.position++] = 218;
953
+ this.view.setUint16(this.position, length);
954
+ this.position += 2;
955
+ } else {
956
+ this.bytes[this.position++] = 219;
957
+ this.view.setUint32(this.position, length);
958
+ this.position += 4;
959
+ }
960
+ const written = this.encoder.encodeInto(text, this.bytes.subarray(this.position, this.position + length)).written;
961
+ if (written !== length) throw new MalformedPayload("A string took a different number of bytes than measured");
962
+ this.position += length;
963
+ }
964
+ binary(source, offset, length) {
965
+ if (length <= 255) {
966
+ this.bytes[this.position++] = 196;
967
+ this.bytes[this.position++] = length;
968
+ } else if (length <= 65535) {
969
+ this.bytes[this.position++] = 197;
970
+ this.view.setUint16(this.position, length);
971
+ this.position += 2;
972
+ } else {
973
+ this.bytes[this.position++] = 198;
974
+ this.view.setUint32(this.position, length);
975
+ this.position += 4;
976
+ }
977
+ this.bytes.set(source.subarray(offset, offset + length), this.position);
978
+ this.position += length;
979
+ }
980
+ uint(value) {
981
+ if (value < 128) this.bytes[this.position++] = value;
982
+ else if (value <= 255) {
983
+ this.bytes[this.position++] = 204;
984
+ this.bytes[this.position++] = value;
985
+ } else if (value <= 65535) {
986
+ this.bytes[this.position++] = 205;
987
+ this.view.setUint16(this.position, value);
988
+ this.position += 2;
989
+ } else if (value <= 4294967295) {
990
+ this.bytes[this.position++] = 206;
991
+ this.view.setUint32(this.position, value);
992
+ this.position += 4;
993
+ } else {
994
+ this.bytes[this.position++] = 207;
995
+ this.view.setBigUint64(this.position, BigInt(value));
996
+ this.position += 8;
997
+ }
998
+ }
999
+ container(length, fix, sixteen, thirtyTwo) {
1000
+ if (length <= 15) this.bytes[this.position++] = fix | length;
1001
+ else if (length <= 65535) {
1002
+ this.bytes[this.position++] = sixteen;
1003
+ this.view.setUint16(this.position, length);
1004
+ this.position += 2;
1005
+ } else {
1006
+ this.bytes[this.position++] = thirtyTwo;
1007
+ this.view.setUint32(this.position, length);
1008
+ this.position += 4;
1009
+ }
1010
+ }
1011
+ };
1012
+ var StrictReader = class {
1013
+ bytes;
1014
+ view;
1015
+ position;
1016
+ decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
1017
+ constructor(bytes, start) {
1018
+ this.bytes = bytes;
1019
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1020
+ this.position = start;
1021
+ }
1022
+ get offset() {
1023
+ return this.position;
1024
+ }
1025
+ get atEnd() {
1026
+ return this.position === this.bytes.byteLength;
1027
+ }
1028
+ map() {
1029
+ return this.container(128, 222, 223, "a map");
1030
+ }
1031
+ array() {
1032
+ return this.container(144, 220, 221, "an array");
1033
+ }
1034
+ string() {
1035
+ const tag = this.byte();
1036
+ let length;
1037
+ if ((tag & 224) === 160) length = tag & 31;
1038
+ else if (tag === 217) length = this.byte();
1039
+ else if (tag === 218) length = this.uint16();
1040
+ else if (tag === 219) length = this.uint32();
1041
+ else throw new MalformedPayload("Expected a string");
1042
+ const end = this.take(length);
1043
+ try {
1044
+ return this.decoder.decode(this.bytes.subarray(end - length, end));
1045
+ } catch {
1046
+ throw new MalformedPayload("A string holds invalid UTF-8");
1047
+ }
1048
+ }
1049
+ binary() {
1050
+ const tag = this.byte();
1051
+ let length;
1052
+ if (tag === 196) length = this.byte();
1053
+ else if (tag === 197) length = this.uint16();
1054
+ else if (tag === 198) length = this.uint32();
1055
+ else throw new MalformedPayload("Expected bytes");
1056
+ const end = this.take(length);
1057
+ return { offset: end - length, length };
1058
+ }
1059
+ uint() {
1060
+ const tag = this.byte();
1061
+ if (tag < 128) return tag;
1062
+ if (tag === 204) return this.byte();
1063
+ if (tag === 205) return this.uint16();
1064
+ if (tag === 206) return this.uint32();
1065
+ if (tag === 207) {
1066
+ this.take(8);
1067
+ const value = this.view.getBigUint64(this.position - 8);
1068
+ if (value > BigInt(Number.MAX_SAFE_INTEGER)) throw new MalformedPayload("An integer is out of range");
1069
+ return Number(value);
1070
+ }
1071
+ throw new MalformedPayload("Expected an unsigned integer");
1072
+ }
1073
+ container(fix, sixteen, thirtyTwo, what) {
1074
+ const tag = this.byte();
1075
+ if ((tag & 240) === fix) return tag & 15;
1076
+ if (tag === sixteen) return this.uint16();
1077
+ if (tag === thirtyTwo) return this.uint32();
1078
+ throw new MalformedPayload(`Expected ${what}`);
1079
+ }
1080
+ take(length) {
1081
+ const end = this.position + length;
1082
+ if (end > this.bytes.byteLength) throw new MalformedPayload("The payload ends early");
1083
+ this.position = end;
1084
+ return end;
1085
+ }
1086
+ byte() {
1087
+ this.take(1);
1088
+ return this.bytes[this.position - 1];
1089
+ }
1090
+ uint16() {
1091
+ this.take(2);
1092
+ return this.view.getUint16(this.position - 2);
1093
+ }
1094
+ uint32() {
1095
+ this.take(4);
1096
+ return this.view.getUint32(this.position - 4);
1097
+ }
1098
+ };
1099
+
1100
+ // src/query-suggestions/counts-groups.ts
1101
+ var FILE_LIMITS = {
1102
+ minLetters: 1,
1103
+ maxLength: MAX_MAX_LENGTH,
1104
+ maxWords: MAX_MAX_WORDS,
1105
+ maxDigits: MAX_MAX_LENGTH
1106
+ };
1107
+ var KEY_PHRASE = "phrase";
1108
+ var KEY_COUNT = "count";
1109
+ var KEY_PEOPLE = "people";
1110
+ var MAX_UINT32 = 4294967295;
1111
+ function readPeople(reader) {
1112
+ const people = reader.array();
1113
+ if (people > MAX_PEOPLE_PER_PHRASE_IN_FILE) throw new MalformedPayload("A phrase lists too many people");
1114
+ const offsets = [];
1115
+ for (let i = 0; i < people; i++) {
1116
+ const { offset, length } = reader.binary();
1117
+ if (length !== SCRAMBLED_ID_BYTES) throw new MalformedPayload("A scrambled ID has the wrong length");
1118
+ offsets.push(offset);
1119
+ }
1120
+ return offsets;
1121
+ }
1122
+ function walkGroups(reader, payload, visitor, onGroup = null) {
1123
+ const groupCount = reader.map();
1124
+ const validating = visitor === null && onGroup === null;
1125
+ const seenGroups = validating ? /* @__PURE__ */ new Set() : null;
1126
+ for (let g = 0; g < groupCount; g++) {
1127
+ const group = reader.string();
1128
+ onGroup?.(group);
1129
+ if (seenGroups !== null) {
1130
+ if (!isGroup(group) || seenGroups.has(group)) throw new MalformedPayload("A group is invalid or repeated");
1131
+ seenGroups.add(group);
1132
+ }
1133
+ const phraseCount = reader.array();
1134
+ const seenPhrases = validating ? /* @__PURE__ */ new Set() : null;
1135
+ for (let p = 0; p < phraseCount; p++) {
1136
+ const fields = reader.map();
1137
+ let phrase = null;
1138
+ let count = null;
1139
+ let offsets = null;
1140
+ for (let f = 0; f < fields; f++) {
1141
+ const key = reader.string();
1142
+ if (key === KEY_PHRASE && phrase === null) phrase = reader.string();
1143
+ else if (key === KEY_COUNT && count === null) count = reader.uint();
1144
+ else if (key === KEY_PEOPLE && offsets === null) offsets = readPeople(reader);
1145
+ else throw new MalformedPayload(`A phrase holds an unexpected or repeated key "${key}"`);
1146
+ }
1147
+ if (phrase === null || count === null || offsets === null) throw new MalformedPayload("A phrase lacks a key");
1148
+ if (seenPhrases !== null) {
1149
+ if (!isNormalPhrase(phrase, FILE_LIMITS) || seenPhrases.has(phrase)) {
1150
+ throw new MalformedPayload("A phrase is invalid or repeated");
1151
+ }
1152
+ if (offsets.length > count || count > MAX_UINT32) {
1153
+ throw new MalformedPayload("A phrase lists more people than its count");
1154
+ }
1155
+ seenPhrases.add(phrase);
1156
+ }
1157
+ visitor?.(group, phrase, count, { source: payload, offsets });
1158
+ }
1159
+ }
1160
+ }
1161
+
1162
+ // src/query-suggestions/counts-file.ts
1163
+ var DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
1164
+ var WHAT = "query counts file";
1165
+ var QUERY_COUNTS_FILE_FORMAT_VERSION = 1;
1166
+ var KEY_DAY = "day";
1167
+ var KEY_FINGERPRINT = "keyFingerprint";
1168
+ var KEY_SAVED_AT = "savedAt";
1169
+ var KEY_INDEX_UUID = "indexUuid";
1170
+ var KEY_GROUPS = "groups";
1171
+ var KEY_PHRASE2 = "phrase";
1172
+ var KEY_COUNT2 = "count";
1173
+ var KEY_PEOPLE2 = "people";
1174
+ var REQUIRED_TOP_LEVEL_KEYS = 4;
1175
+ var PHRASE_KEYS = 3;
1176
+ function isDay(value) {
1177
+ if (!DAY_PATTERN.test(value)) return false;
1178
+ const parsed = /* @__PURE__ */ new Date(`${value}T00:00:00.000Z`);
1179
+ return !Number.isNaN(parsed.getTime()) && parsed.toISOString().slice(0, 10) === value;
1180
+ }
1181
+ function topLevelSize(header) {
1182
+ const keys = REQUIRED_TOP_LEVEL_KEYS + (header.indexUuid === null ? 0 : 1);
1183
+ return containerHeaderSize(keys) + stringSize(KEY_DAY) + stringSize(header.day) + stringSize(KEY_FINGERPRINT) + binarySize(header.fingerprint.byteLength) + stringSize(KEY_SAVED_AT) + uintSize(header.savedAt) + (header.indexUuid === null ? 0 : stringSize(KEY_INDEX_UUID) + stringSize(header.indexUuid)) + stringSize(KEY_GROUPS);
1184
+ }
1185
+ function writeTopLevel(writer, header) {
1186
+ writer.map(REQUIRED_TOP_LEVEL_KEYS + (header.indexUuid === null ? 0 : 1));
1187
+ writer.string(KEY_DAY);
1188
+ writer.string(header.day);
1189
+ writer.string(KEY_FINGERPRINT);
1190
+ writer.binary(header.fingerprint, 0, header.fingerprint.byteLength);
1191
+ writer.string(KEY_SAVED_AT);
1192
+ writer.uint(header.savedAt);
1193
+ if (header.indexUuid !== null) {
1194
+ writer.string(KEY_INDEX_UUID);
1195
+ writer.string(header.indexUuid);
1196
+ }
1197
+ writer.string(KEY_GROUPS);
1198
+ }
1199
+ function groupsSize(counts) {
1200
+ let size = containerHeaderSize(counts.groupTables.size);
1201
+ for (const [group, phrases] of counts.groupTables) {
1202
+ size += stringSize(group) + containerHeaderSize(phrases.size);
1203
+ for (const [phrase, number] of phrases) {
1204
+ const people = counts.peopleLengthOf(number);
1205
+ size += containerHeaderSize(PHRASE_KEYS) + stringSize(KEY_PHRASE2) + stringSize(phrase) + stringSize(KEY_COUNT2) + uintSize(counts.countOf(number)) + stringSize(KEY_PEOPLE2) + containerHeaderSize(people) + people * binarySize(SCRAMBLED_ID_BYTES);
1206
+ }
1207
+ }
1208
+ return size;
1209
+ }
1210
+ function writeGroups(writer, counts) {
1211
+ writer.map(counts.groupTables.size);
1212
+ const people = counts.peopleBytes;
1213
+ for (const [group, phrases] of counts.groupTables) {
1214
+ writer.string(group);
1215
+ writer.array(phrases.size);
1216
+ for (const [phrase, number] of phrases) {
1217
+ writer.map(PHRASE_KEYS);
1218
+ writer.string(KEY_PHRASE2);
1219
+ writer.string(phrase);
1220
+ writer.string(KEY_COUNT2);
1221
+ writer.uint(counts.countOf(number));
1222
+ writer.string(KEY_PEOPLE2);
1223
+ const length = counts.peopleLengthOf(number);
1224
+ const offset = counts.peopleOffsetOf(number);
1225
+ writer.array(length);
1226
+ for (let i = 0; i < length; i++) writer.binary(people, offset + i * SCRAMBLED_ID_BYTES, SCRAMBLED_ID_BYTES);
1227
+ }
1228
+ }
1229
+ }
1230
+ function sealed(bytes, writer) {
1231
+ if (writer.offset !== bytes.byteLength) throw new MalformedPayload("The query counts file took an unexpected size");
1232
+ if (bytes.byteLength > MAX_HISTORY_FILE_BYTES) {
1233
+ throw new MalformedPayload(`A query counts file must stay at or below ${MAX_HISTORY_FILE_BYTES} bytes`);
1234
+ }
1235
+ const payload = bytes.subarray(HEADER_SIZE);
1236
+ const packed = packEnvelopeWithChecksum([payload], QUERY_COUNTS_FILE_FORMAT_VERSION, WHAT, crc32(payload));
1237
+ bytes.set(packed.chunks[0], 0);
1238
+ return bytes;
1239
+ }
1240
+ function encodeCountsFile(counts, header) {
1241
+ const full = { ...header, day: counts.day };
1242
+ const bytes = new Uint8Array(HEADER_SIZE + topLevelSize(full) + groupsSize(counts));
1243
+ const writer = new ExactWriter(bytes, HEADER_SIZE);
1244
+ writeTopLevel(writer, full);
1245
+ writeGroups(writer, counts);
1246
+ return sealed(bytes, writer);
1247
+ }
1248
+ function rewrittenFile(header, groups) {
1249
+ const bytes = new Uint8Array(HEADER_SIZE + topLevelSize(header) + groups.byteLength);
1250
+ const writer = new ExactWriter(bytes, HEADER_SIZE);
1251
+ writeTopLevel(writer, header);
1252
+ bytes.set(groups, writer.offset);
1253
+ writer.skip(groups.byteLength);
1254
+ return sealed(bytes, writer);
1255
+ }
1256
+ function readTopLevel(payload) {
1257
+ const reader = new StrictReader(payload, 0);
1258
+ const keys = reader.map();
1259
+ let day = null;
1260
+ let fingerprint = null;
1261
+ let savedAt = null;
1262
+ let indexUuid = null;
1263
+ let groupsStart = null;
1264
+ let groupsEnd = 0;
1265
+ const seen = /* @__PURE__ */ new Set();
1266
+ for (let i = 0; i < keys; i++) {
1267
+ const key = reader.string();
1268
+ if (seen.has(key)) throw new MalformedPayload(`The file holds the key "${key}" twice`);
1269
+ seen.add(key);
1270
+ if (key === KEY_DAY) day = reader.string();
1271
+ else if (key === KEY_FINGERPRINT) {
1272
+ const { offset, length } = reader.binary();
1273
+ fingerprint = payload.subarray(offset, offset + length);
1274
+ } else if (key === KEY_SAVED_AT) savedAt = reader.uint();
1275
+ else if (key === KEY_INDEX_UUID) {
1276
+ indexUuid = reader.string();
1277
+ if (indexUuid.length === 0 || indexUuid.length > MAX_INDEX_UUID_LENGTH) {
1278
+ throw new MalformedPayload("The file holds an indexUuid of the wrong length");
1279
+ }
1280
+ } else if (key === KEY_GROUPS) {
1281
+ groupsStart = reader.offset;
1282
+ walkGroups(reader, payload, null);
1283
+ groupsEnd = reader.offset;
1284
+ } else throw new MalformedPayload(`The file holds an unexpected key "${key}"`);
1285
+ }
1286
+ if (!reader.atEnd) throw new MalformedPayload("The payload holds bytes after its map");
1287
+ if (day === null || fingerprint === null || savedAt === null || groupsStart === null) {
1288
+ throw new MalformedPayload("The file lacks a required key");
1289
+ }
1290
+ return { day, fingerprint, savedAt, indexUuid, groupsStart, groupsEnd };
1291
+ }
1292
+ async function openCountsFile(bytes, expectedDay, fingerprint) {
1293
+ if (bytes.byteLength > MAX_HISTORY_FILE_BYTES) {
1294
+ return { kind: "damaged", reason: `The file is longer than ${MAX_HISTORY_FILE_BYTES} bytes` };
1295
+ }
1296
+ let payload;
1297
+ try {
1298
+ payload = (await openChecksummedEnvelope(bytes, {
1299
+ envelopeFormatVersion: QUERY_COUNTS_FILE_FORMAT_VERSION,
1300
+ what: WHAT,
1301
+ allowsCompression: false
1302
+ })).payload;
1303
+ } catch (error) {
1304
+ return { kind: "damaged", reason: error instanceof Error ? error.message : String(error) };
1305
+ }
1306
+ let top;
1307
+ try {
1308
+ top = readTopLevel(payload);
1309
+ } catch (error) {
1310
+ if (error instanceof MalformedPayload) return { kind: "damaged", reason: error.message };
1311
+ throw error;
1312
+ }
1313
+ if (!isDay(top.day) || expectedDay !== null && top.day !== expectedDay) {
1314
+ return { kind: "damaged", reason: "The file holds a day that differs from its key" };
1315
+ }
1316
+ if (top.fingerprint.byteLength !== KEY_FINGERPRINT_BYTES) {
1317
+ return { kind: "damaged", reason: "The key fingerprint has the wrong length" };
1318
+ }
1319
+ if (!sameBytes(top.fingerprint, fingerprint)) return { kind: "other-key" };
1320
+ const { groupsStart, groupsEnd, ...header } = top;
1321
+ return {
1322
+ kind: "counts",
1323
+ ...header,
1324
+ visit(visitor) {
1325
+ walkGroups(new StrictReader(payload, groupsStart), payload, visitor);
1326
+ },
1327
+ groupNames() {
1328
+ const names = [];
1329
+ walkGroups(new StrictReader(payload, groupsStart), payload, null, (group) => names.push(group));
1330
+ return names;
1331
+ },
1332
+ withIndexUuid(indexUuid, savedAt) {
1333
+ return rewrittenFile({ ...header, indexUuid, savedAt }, payload.subarray(groupsStart, groupsEnd));
1334
+ }
1335
+ };
1336
+ }
1337
+ function sameBytes(left, right) {
1338
+ if (left.byteLength !== right.byteLength) return false;
1339
+ for (let i = 0; i < left.byteLength; i++) if (left[i] !== right[i]) return false;
1340
+ return true;
1341
+ }
1342
+
1343
+ // src/query-suggestions/day-counts.ts
1344
+ var EMPTY_SLOT = -1;
1345
+ var NO_BLOCK = -1;
1346
+ function nextPowerOfTwo(value) {
1347
+ let power = 1;
1348
+ while (power < value) power *= 2;
1349
+ return power;
1350
+ }
1351
+ function idsEqual(left, leftOffset, right, rightOffset) {
1352
+ for (let i = 0; i < SCRAMBLED_ID_BYTES; i++) {
1353
+ if (left[leftOffset + i] !== right[rightOffset + i]) return false;
1354
+ }
1355
+ return true;
1356
+ }
1357
+ function hashOfId(id, offset) {
1358
+ return (id[offset] | id[offset + 1] << 8 | id[offset + 2] << 16 | id[offset + 3] << 24) >>> 0;
1359
+ }
1360
+ var DayCounts = class {
1361
+ day;
1362
+ limits;
1363
+ groups = /* @__PURE__ */ new Map();
1364
+ phraseCount = 0;
1365
+ phraseTexts = [];
1366
+ phraseGroups = [];
1367
+ phraseCounts = new Uint32Array(0);
1368
+ phrasePeopleLengths = new Uint16Array(0);
1369
+ phrasePeople = new Uint8Array(0);
1370
+ personCount = 0;
1371
+ personIds = new Uint8Array(0);
1372
+ personPairs = new Uint16Array(0);
1373
+ personHeads = new Int32Array(0);
1374
+ personTails = new Int32Array(0);
1375
+ personSlots = new Int32Array(0);
1376
+ pairPages = [];
1377
+ linkPages = [];
1378
+ blockCount = 0;
1379
+ changed = false;
1380
+ constructor(day, limits) {
1381
+ this.day = day;
1382
+ this.limits = limits;
1383
+ }
1384
+ get phrases() {
1385
+ return this.phraseCount;
1386
+ }
1387
+ get people() {
1388
+ return this.personCount;
1389
+ }
1390
+ get groupNames() {
1391
+ return this.groups.keys();
1392
+ }
1393
+ get isChanged() {
1394
+ return this.changed;
1395
+ }
1396
+ markSaved() {
1397
+ this.changed = false;
1398
+ }
1399
+ markChanged() {
1400
+ this.changed = true;
1401
+ }
1402
+ get groupTables() {
1403
+ return this.groups;
1404
+ }
1405
+ get peopleBytes() {
1406
+ return this.phrasePeople;
1407
+ }
1408
+ countOf(phraseNumber) {
1409
+ return this.phraseCounts[phraseNumber];
1410
+ }
1411
+ peopleLengthOf(phraseNumber) {
1412
+ return this.phrasePeopleLengths[phraseNumber];
1413
+ }
1414
+ peopleOffsetOf(phraseNumber) {
1415
+ return phraseNumber * this.limits.minPeople * SCRAMBLED_ID_BYTES;
1416
+ }
1417
+ hasGroup(group) {
1418
+ return this.groups.has(group);
1419
+ }
1420
+ count(group, phrase, scrambledId, groupAllowed) {
1421
+ const groupPhrases = this.groups.get(group);
1422
+ const existingPhrase = groupPhrases?.get(phrase);
1423
+ const person = this.findPerson(scrambledId);
1424
+ if (person !== EMPTY_SLOT && existingPhrase !== void 0 && this.personHasPhrase(person, existingPhrase)) {
1425
+ return "repeat";
1426
+ }
1427
+ if (person !== EMPTY_SLOT && this.personPairs[person] >= this.limits.maxPhrasesPerPerson) return "person-limit";
1428
+ if (existingPhrase === void 0 && this.phraseCount >= this.limits.maxPhrasesPerDay) return "phrase-limit";
1429
+ if (person === EMPTY_SLOT && this.personCount >= this.limits.maxPeoplePerDay) return "people-limit";
1430
+ if (groupPhrases === void 0 && (!groupAllowed || this.groups.size >= this.limits.maxGroups)) {
1431
+ return "group-limit";
1432
+ }
1433
+ const phraseNumber = existingPhrase ?? this.addPhrase(group, phrase, groupPhrases);
1434
+ const personNumber = person === EMPTY_SLOT ? this.addPerson(scrambledId) : person;
1435
+ this.appendPair(personNumber, phraseNumber);
1436
+ this.phraseCounts[phraseNumber] += 1;
1437
+ this.addToPeople(phraseNumber, scrambledId, 0);
1438
+ this.changed = true;
1439
+ return "recorded";
1440
+ }
1441
+ restorePhrase(group, phrase, count, people, peopleCount) {
1442
+ const groupPhrases = this.groups.get(group);
1443
+ if (groupPhrases?.has(phrase) === true) return false;
1444
+ if (this.phraseCount >= this.limits.maxPhrasesPerDay) return false;
1445
+ if (groupPhrases === void 0 && this.groups.size >= this.limits.maxGroups) return false;
1446
+ const phraseNumber = this.addPhrase(group, phrase, groupPhrases);
1447
+ for (let i = 0; i < peopleCount; i++) this.addToPeople(phraseNumber, people, i * SCRAMBLED_ID_BYTES);
1448
+ this.phraseCounts[phraseNumber] = Math.max(count, this.phrasePeopleLengths[phraseNumber]);
1449
+ return true;
1450
+ }
1451
+ addPhrase(group, phrase, existing) {
1452
+ const groupPhrases = existing ?? /* @__PURE__ */ new Map();
1453
+ if (existing === void 0) this.groups.set(group, groupPhrases);
1454
+ if (this.phraseCount === this.phraseCounts.length) this.growPhrases();
1455
+ const number = this.phraseCount;
1456
+ this.phraseCount += 1;
1457
+ groupPhrases.set(phrase, number);
1458
+ this.phraseTexts.push(phrase);
1459
+ this.phraseGroups.push(group);
1460
+ return number;
1461
+ }
1462
+ growPhrases() {
1463
+ const capacity = Math.min(
1464
+ this.limits.maxPhrasesPerDay,
1465
+ Math.max(MIN_PHRASE_TABLE_CAPACITY, this.phraseCounts.length * 2)
1466
+ );
1467
+ const counts = new Uint32Array(capacity);
1468
+ counts.set(this.phraseCounts);
1469
+ this.phraseCounts = counts;
1470
+ const lengths = new Uint16Array(capacity);
1471
+ lengths.set(this.phrasePeopleLengths);
1472
+ this.phrasePeopleLengths = lengths;
1473
+ const people = new Uint8Array(capacity * this.limits.minPeople * SCRAMBLED_ID_BYTES);
1474
+ people.set(this.phrasePeople);
1475
+ this.phrasePeople = people;
1476
+ }
1477
+ addToPeople(phraseNumber, id, idOffset) {
1478
+ const length = this.phrasePeopleLengths[phraseNumber];
1479
+ if (length >= this.limits.minPeople) return;
1480
+ const base = phraseNumber * this.limits.minPeople * SCRAMBLED_ID_BYTES;
1481
+ for (let i = 0; i < length; i++) {
1482
+ if (idsEqual(this.phrasePeople, base + i * SCRAMBLED_ID_BYTES, id, idOffset)) return;
1483
+ }
1484
+ this.phrasePeople.set(id.subarray(idOffset, idOffset + SCRAMBLED_ID_BYTES), base + length * SCRAMBLED_ID_BYTES);
1485
+ this.phrasePeopleLengths[phraseNumber] = length + 1;
1486
+ }
1487
+ findPerson(id) {
1488
+ if (this.personSlots.length === 0) return EMPTY_SLOT;
1489
+ const mask = this.personSlots.length - 1;
1490
+ let slot = hashOfId(id, 0) & mask;
1491
+ for (; ; ) {
1492
+ const person = this.personSlots[slot];
1493
+ if (person === EMPTY_SLOT) return EMPTY_SLOT;
1494
+ if (idsEqual(this.personIds, person * SCRAMBLED_ID_BYTES, id, 0)) return person;
1495
+ slot = slot + 1 & mask;
1496
+ }
1497
+ }
1498
+ addPerson(id) {
1499
+ if (this.personCount === this.personPairs.length) this.growPeople();
1500
+ const person = this.personCount;
1501
+ this.personCount += 1;
1502
+ this.personIds.set(id.subarray(0, SCRAMBLED_ID_BYTES), person * SCRAMBLED_ID_BYTES);
1503
+ this.personHeads[person] = NO_BLOCK;
1504
+ this.personTails[person] = NO_BLOCK;
1505
+ this.insertSlot(person);
1506
+ return person;
1507
+ }
1508
+ insertSlot(person) {
1509
+ const mask = this.personSlots.length - 1;
1510
+ let slot = hashOfId(this.personIds, person * SCRAMBLED_ID_BYTES) & mask;
1511
+ while (this.personSlots[slot] !== EMPTY_SLOT) slot = slot + 1 & mask;
1512
+ this.personSlots[slot] = person;
1513
+ }
1514
+ growPeople() {
1515
+ const capacity = Math.min(
1516
+ this.limits.maxPeoplePerDay,
1517
+ Math.max(PERSON_TABLE_MIN_SLOTS / 2, this.personPairs.length * 2)
1518
+ );
1519
+ const ids = new Uint8Array(capacity * SCRAMBLED_ID_BYTES);
1520
+ ids.set(this.personIds);
1521
+ this.personIds = ids;
1522
+ const pairs = new Uint16Array(capacity);
1523
+ pairs.set(this.personPairs);
1524
+ this.personPairs = pairs;
1525
+ const heads = new Int32Array(capacity);
1526
+ heads.set(this.personHeads);
1527
+ this.personHeads = heads;
1528
+ const tails = new Int32Array(capacity);
1529
+ tails.set(this.personTails);
1530
+ this.personTails = tails;
1531
+ this.personSlots = new Int32Array(nextPowerOfTwo(capacity * 2)).fill(EMPTY_SLOT);
1532
+ for (let person = 0; person < this.personCount; person++) this.insertSlot(person);
1533
+ }
1534
+ personHasPhrase(person, phraseNumber) {
1535
+ let remaining = this.personPairs[person];
1536
+ let block = this.personHeads[person];
1537
+ while (block !== NO_BLOCK && remaining > 0) {
1538
+ const page = this.pairPages[Math.floor(block / PAIR_PAGE_BLOCKS)];
1539
+ const start = block % PAIR_PAGE_BLOCKS * PAIR_BLOCK_SLOTS;
1540
+ const used = Math.min(remaining, PAIR_BLOCK_SLOTS);
1541
+ for (let i = 0; i < used; i++) {
1542
+ if (page[start + i] === phraseNumber) return true;
1543
+ }
1544
+ remaining -= used;
1545
+ block = this.linkPages[Math.floor(block / PAIR_PAGE_BLOCKS)][block % PAIR_PAGE_BLOCKS];
1546
+ }
1547
+ return false;
1548
+ }
1549
+ appendPair(person, phraseNumber) {
1550
+ const pairs = this.personPairs[person];
1551
+ const slot = pairs % PAIR_BLOCK_SLOTS;
1552
+ let block = this.personTails[person];
1553
+ if (slot === 0) {
1554
+ const fresh = this.allocateBlock();
1555
+ if (block === NO_BLOCK) this.personHeads[person] = fresh;
1556
+ else this.linkPages[Math.floor(block / PAIR_PAGE_BLOCKS)][block % PAIR_PAGE_BLOCKS] = fresh;
1557
+ this.personTails[person] = fresh;
1558
+ block = fresh;
1559
+ }
1560
+ const page = this.pairPages[Math.floor(block / PAIR_PAGE_BLOCKS)];
1561
+ page[block % PAIR_PAGE_BLOCKS * PAIR_BLOCK_SLOTS + slot] = phraseNumber;
1562
+ this.personPairs[person] = pairs + 1;
1563
+ }
1564
+ allocateBlock() {
1565
+ const block = this.blockCount;
1566
+ if (block % PAIR_PAGE_BLOCKS === 0) {
1567
+ this.pairPages.push(new Uint16Array(PAIR_PAGE_BLOCKS * PAIR_BLOCK_SLOTS));
1568
+ this.linkPages.push(new Int32Array(PAIR_PAGE_BLOCKS).fill(NO_BLOCK));
1569
+ }
1570
+ this.blockCount += 1;
1571
+ return block;
1572
+ }
1573
+ };
1574
+
1575
+ // src/query-suggestions/history-files.ts
1576
+ var BUILD_RECORD_FILE = "build.nrsl";
1577
+ var DAY_FILE = /^(\d{4}-\d{2}-\d{2})\.nrsl$/;
1578
+ var RESTORED_FILE = /^(\d{4}-\d{2}-\d{2})-(\d{4})\.nrsl$/;
1579
+ function historyFolderOf(indexName) {
1580
+ return `${QUERY_HISTORY_FOLDER}/${indexName}/`;
1581
+ }
1582
+ function dayFileKey(indexName, day) {
1583
+ return `${historyFolderOf(indexName)}${day}.nrsl`;
1584
+ }
1585
+ function restoredFileKey(indexName, day, sequence) {
1586
+ return `${historyFolderOf(indexName)}${RESTORED_HISTORY_FOLDER}/${day}-${String(sequence).padStart(4, "0")}.nrsl`;
1587
+ }
1588
+ function buildRecordKey(indexName) {
1589
+ return `${historyFolderOf(indexName)}${BUILD_RECORD_FILE}`;
1590
+ }
1591
+ function historyFileOf(indexName, key) {
1592
+ const folder = historyFolderOf(indexName);
1593
+ if (!key.startsWith(folder)) return null;
1594
+ const rest = key.slice(folder.length);
1595
+ const own = DAY_FILE.exec(rest);
1596
+ if (own !== null) return { key, day: own[1], restored: false };
1597
+ const restoredPrefix = `${RESTORED_HISTORY_FOLDER}/`;
1598
+ if (!rest.startsWith(restoredPrefix)) return null;
1599
+ const restored = RESTORED_FILE.exec(rest.slice(restoredPrefix.length));
1600
+ return restored === null ? null : { key, day: restored[1], restored: true };
1601
+ }
1602
+ function directoryFiles(storage) {
1603
+ const { directory } = storage;
1604
+ return {
1605
+ listUnder: (prefix) => directory.list(prefix),
1606
+ async read(key) {
1607
+ const length = await directory.byteLength?.(key) ?? null;
1608
+ if (length !== null && length > MAX_HISTORY_FILE_BYTES) return "too-long";
1609
+ const bytes = await directory.read(key);
1610
+ if (bytes !== null && bytes.byteLength > MAX_HISTORY_FILE_BYTES) return "too-long";
1611
+ return bytes;
1612
+ },
1613
+ write: (key, bytes) => directory.atomicWrite(key, bytes),
1614
+ remove: (key) => directory.remove(key)
1615
+ };
1616
+ }
1617
+ function adapterFiles(storage) {
1618
+ const { adapter } = storage;
1619
+ return {
1620
+ listUnder: (prefix) => adapter.list(prefix),
1621
+ async read(key) {
1622
+ const bytes = await adapter.load(key);
1623
+ if (bytes !== null && bytes.byteLength > MAX_HISTORY_FILE_BYTES) return "too-long";
1624
+ return bytes;
1625
+ },
1626
+ write: (key, bytes) => adapter.save(key, bytes),
1627
+ remove: (key) => adapter.delete(key)
1628
+ };
1629
+ }
1630
+ function memoryFiles() {
1631
+ const files = /* @__PURE__ */ new Map();
1632
+ return {
1633
+ async listUnder(prefix) {
1634
+ return [...files.keys()].filter((key) => key.startsWith(prefix)).sort();
1635
+ },
1636
+ async read(key) {
1637
+ return files.get(key) ?? null;
1638
+ },
1639
+ async write(key, bytes) {
1640
+ files.set(key, bytes);
1641
+ },
1642
+ async remove(key) {
1643
+ files.delete(key);
1644
+ }
1645
+ };
1646
+ }
1647
+ function historyFilesOf(storage) {
1648
+ if (storage === null) return memoryFiles();
1649
+ return storage.kind === "directory" ? directoryFiles(storage) : adapterFiles(storage);
1650
+ }
1651
+ function listHistoryFolder(files, indexName) {
1652
+ return files.listUnder(historyFolderOf(indexName));
1653
+ }
1654
+ async function removeHistoryFolder(files, indexName) {
1655
+ for (const key of await listHistoryFolder(files, indexName)) await files.remove(key);
1656
+ }
1657
+ function restoreStagingPrefix() {
1658
+ return `${QUERY_HISTORY_FOLDER}/${RESTORE_STAGING_INFIX}${globalThis.crypto.randomUUID()}/`;
1659
+ }
1660
+ async function removeRestoreLeftovers(files) {
1661
+ for (const key of await files.listUnder(`${QUERY_HISTORY_FOLDER}/${RESTORE_STAGING_INFIX}`)) await files.remove(key);
1662
+ }
1663
+
1664
+ // src/query-suggestions/history-groups.ts
1665
+ var HistoryGroups = class {
1666
+ host;
1667
+ fingerprint;
1668
+ held = /* @__PURE__ */ new Map();
1669
+ constructor(host, fingerprint) {
1670
+ this.host = host;
1671
+ this.fingerprint = fingerprint;
1672
+ }
1673
+ async of(indexName, cutoff) {
1674
+ const held = this.held.get(indexName);
1675
+ if (held !== void 0 && held.cutoff <= cutoff) {
1676
+ if (held.cutoff < cutoff) {
1677
+ for (const [group, day] of held.newestDay) if (day < cutoff) held.newestDay.delete(group);
1678
+ held.cutoff = cutoff;
1679
+ }
1680
+ return held.newestDay.keys();
1681
+ }
1682
+ const newestDay = await this.load(indexName, cutoff);
1683
+ this.held.set(indexName, { cutoff, newestDay });
1684
+ return newestDay.keys();
1685
+ }
1686
+ note(indexName, day, groups) {
1687
+ const held = this.held.get(indexName);
1688
+ if (held === void 0 || day < held.cutoff) return;
1689
+ for (const group of groups) {
1690
+ const newest = held.newestDay.get(group);
1691
+ if (newest === void 0 || newest < day) held.newestDay.set(group, day);
1692
+ }
1693
+ }
1694
+ forget(indexName) {
1695
+ this.held.delete(indexName);
1696
+ }
1697
+ async load(indexName, cutoff) {
1698
+ const newestDay = /* @__PURE__ */ new Map();
1699
+ const expectedUuid = this.host.view(indexName)?.indexUuid ?? null;
1700
+ for (const key of await listHistoryFolder(this.host.files, indexName)) {
1701
+ const file = historyFileOf(indexName, key);
1702
+ if (file === null || file.day < cutoff) continue;
1703
+ const bytes = await this.host.files.read(key);
1704
+ if (!(bytes instanceof Uint8Array)) continue;
1705
+ const opened = await openCountsFile(bytes, file.day, this.fingerprint);
1706
+ if (opened.kind !== "counts") continue;
1707
+ if (this.host.clustered && (opened.indexUuid === null || opened.indexUuid !== expectedUuid)) continue;
1708
+ for (const group of opened.groupNames()) {
1709
+ const newest = newestDay.get(group);
1710
+ if (newest === void 0 || newest < file.day) newestDay.set(group, file.day);
1711
+ }
1712
+ }
1713
+ return newestDay;
1714
+ }
1715
+ };
1716
+
1717
+ // src/query-suggestions/day-store.ts
1718
+ function dayOf(instant) {
1719
+ return new Date(instant).toISOString().slice(0, 10);
1720
+ }
1721
+ function limitsOf(setting) {
1722
+ return {
1723
+ minPeople: setting.minPeople,
1724
+ maxPhrasesPerDay: setting.maxPhrasesPerDay,
1725
+ maxPhrasesPerPerson: setting.maxPhrasesPerPerson,
1726
+ maxPeoplePerDay: setting.maxPeoplePerDay,
1727
+ maxGroups: setting.maxGroups
1728
+ };
1729
+ }
1730
+ function sameLimits(left, right) {
1731
+ return left.minPeople === right.minPeople && left.maxPhrasesPerDay === right.maxPhrasesPerDay && left.maxPhrasesPerPerson === right.maxPhrasesPerPerson && left.maxPeoplePerDay === right.maxPeoplePerDay && left.maxGroups === right.maxGroups;
1732
+ }
1733
+ var DayStore = class {
1734
+ host;
1735
+ scrambler;
1736
+ history;
1737
+ days = /* @__PURE__ */ new Map();
1738
+ unsavedDays = /* @__PURE__ */ new Map();
1739
+ turns = /* @__PURE__ */ new Map();
1740
+ warnedLimits = /* @__PURE__ */ new Map();
1741
+ generations = /* @__PURE__ */ new Map();
1742
+ constructor(host, scrambler) {
1743
+ this.host = host;
1744
+ this.scrambler = scrambler;
1745
+ this.history = new HistoryGroups(host, scrambler.fingerprint);
1746
+ }
1747
+ hasUnsaved(indexName) {
1748
+ return this.days.get(indexName)?.isChanged === true || this.unsavedDays.has(indexName);
1749
+ }
1750
+ indexes() {
1751
+ return [.../* @__PURE__ */ new Set([...this.days.keys(), ...this.unsavedDays.keys()])];
1752
+ }
1753
+ inTurn(indexName, work) {
1754
+ const previous = this.turns.get(indexName) ?? Promise.resolve();
1755
+ const next = previous.then(work, work);
1756
+ const settled = next.catch(() => void 0);
1757
+ this.turns.set(indexName, settled);
1758
+ void settled.then(() => {
1759
+ if (this.turns.get(indexName) === settled) this.turns.delete(indexName);
1760
+ });
1761
+ return next;
1762
+ }
1763
+ count(indexName, setting, now, request) {
1764
+ return this.inTurn(indexName, async () => {
1765
+ if (!request.stillEnabled()) return null;
1766
+ if (request.isBanned()) return "banned";
1767
+ const counts = await this.currentDay(indexName, setting, now);
1768
+ const groupAllowed = counts.hasGroup(request.group) || request.groupAllowed(await this.knownGroups(indexName, counts, dayOf(now - setting.retentionDays * DAY_MS)));
1769
+ const outcome = counts.count(request.group, request.phrase, this.scrambler.scramble(request.person), groupAllowed);
1770
+ if (outcome === "phrase-limit" || outcome === "people-limit") this.warnLimit(indexName, counts.day, outcome);
1771
+ return outcome;
1772
+ });
1773
+ }
1774
+ save(indexName) {
1775
+ return this.inTurn(indexName, () => this.saveNow(indexName));
1776
+ }
1777
+ retire(indexName, today) {
1778
+ return this.inTurn(indexName, async () => {
1779
+ await this.saveNow(indexName);
1780
+ const counts = this.days.get(indexName);
1781
+ if (counts === void 0 || counts.day >= today || counts.isChanged) return;
1782
+ this.history.note(indexName, counts.day, counts.groupNames);
1783
+ this.days.delete(indexName);
1784
+ });
1785
+ }
1786
+ forgetHistoryGroups(indexName) {
1787
+ return this.inTurn(indexName, async () => this.history.forget(indexName));
1788
+ }
1789
+ discard(indexName) {
1790
+ return this.inTurn(indexName, async () => {
1791
+ this.days.delete(indexName);
1792
+ this.unsavedDays.delete(indexName);
1793
+ this.history.forget(indexName);
1794
+ this.warnedLimits.delete(indexName);
1795
+ this.generations.set(indexName, (this.generations.get(indexName) ?? 0) + 1);
1796
+ });
1797
+ }
1798
+ async knownGroups(indexName, counts, cutoff) {
1799
+ const known = new Set(counts.groupNames);
1800
+ for (const unsaved of this.unsavedDays.get(indexName) ?? []) {
1801
+ for (const group of unsaved.groupNames) known.add(group);
1802
+ }
1803
+ for (const group of await this.history.of(indexName, cutoff)) known.add(group);
1804
+ return known;
1805
+ }
1806
+ warnLimit(indexName, day, reason) {
1807
+ const mark = `${day}:${reason}`;
1808
+ const warned = this.warnedLimits.get(indexName) ?? "";
1809
+ if (warned.split("|").includes(mark)) return;
1810
+ this.warnedLimits.set(indexName, warned === "" ? mark : `${warned}|${mark}`);
1811
+ this.host.emit("querySuggestionsWarning", { indexName, reason });
1812
+ }
1813
+ async currentDay(indexName, setting, now) {
1814
+ const day = dayOf(now);
1815
+ const limits = limitsOf(setting);
1816
+ const current = this.days.get(indexName);
1817
+ if (current !== void 0 && current.day === day && sameLimits(current.limits, limits)) return current;
1818
+ if (current !== void 0) {
1819
+ await this.saveNow(indexName);
1820
+ if (current.isChanged) {
1821
+ if (current.day === day) return current;
1822
+ this.unsavedDays.set(indexName, [...this.unsavedDays.get(indexName) ?? [], current]);
1823
+ }
1824
+ this.history.note(indexName, current.day, current.groupNames);
1825
+ this.days.delete(indexName);
1826
+ }
1827
+ const loaded = await this.load(indexName, day, limits);
1828
+ this.days.set(indexName, loaded);
1829
+ return loaded;
1830
+ }
1831
+ async load(indexName, day, limits) {
1832
+ const counts = new DayCounts(day, limits);
1833
+ const key = dayFileKey(indexName, day);
1834
+ const bytes = await this.host.files.read(key);
1835
+ if (bytes === null) return counts;
1836
+ if (bytes === "too-long") {
1837
+ this.host.emit("querySuggestionsWarning", { indexName, reason: "damaged-file", file: key });
1838
+ return counts;
1839
+ }
1840
+ const opened = await openCountsFile(bytes, day, this.scrambler.fingerprint);
1841
+ if (opened.kind !== "counts") {
1842
+ const reason = opened.kind === "other-key" ? "other-key" : "damaged-file";
1843
+ this.host.emit("querySuggestionsWarning", { indexName, reason, file: key });
1844
+ return counts;
1845
+ }
1846
+ if (this.host.clustered && opened.indexUuid !== (this.host.view(indexName)?.indexUuid ?? null)) return counts;
1847
+ opened.visit((group, phrase, count, people) => {
1848
+ const ids = new Uint8Array(people.offsets.length * SCRAMBLED_ID_BYTES);
1849
+ people.offsets.forEach((offset, i) => {
1850
+ ids.set(people.source.subarray(offset, offset + SCRAMBLED_ID_BYTES), i * SCRAMBLED_ID_BYTES);
1851
+ });
1852
+ counts.restorePhrase(group, phrase, count, ids, people.offsets.length);
1853
+ });
1854
+ return counts;
1855
+ }
1856
+ async saveNow(indexName) {
1857
+ const unsaved = this.unsavedDays.get(indexName);
1858
+ if (unsaved !== void 0) {
1859
+ const stillUnsaved = [];
1860
+ for (const counts2 of unsaved) {
1861
+ await this.write(indexName, counts2);
1862
+ if (counts2.isChanged) stillUnsaved.push(counts2);
1863
+ }
1864
+ if (this.unsavedDays.get(indexName) === unsaved) {
1865
+ if (stillUnsaved.length === 0) this.unsavedDays.delete(indexName);
1866
+ else this.unsavedDays.set(indexName, stillUnsaved);
1867
+ }
1868
+ }
1869
+ const counts = this.days.get(indexName);
1870
+ if (counts !== void 0) await this.write(indexName, counts);
1871
+ }
1872
+ async write(indexName, counts) {
1873
+ if (!counts.isChanged) return;
1874
+ const generation = this.generations.get(indexName) ?? 0;
1875
+ const view = this.host.view(indexName);
1876
+ const bytes = encodeCountsFile(counts, {
1877
+ fingerprint: this.scrambler.fingerprint,
1878
+ savedAt: Date.now(),
1879
+ indexUuid: this.host.clustered ? view?.indexUuid ?? null : null
1880
+ });
1881
+ counts.markSaved();
1882
+ try {
1883
+ if ((this.generations.get(indexName) ?? 0) !== generation) return;
1884
+ await this.host.files.write(dayFileKey(indexName, counts.day), bytes);
1885
+ } catch (error) {
1886
+ counts.markChanged();
1887
+ this.host.emit("querySuggestionsWarning", {
1888
+ indexName,
1889
+ reason: "save-failed",
1890
+ message: error instanceof Error ? error.message : String(error)
1891
+ });
1892
+ }
1893
+ }
1894
+ };
1895
+
1896
+ // src/query-suggestions/engine-hooks.ts
1897
+ function suggestionIndexesOf(core, sourceName) {
1898
+ const names = [];
1899
+ for (const [name, entry] of core.indexRegistry) {
1900
+ if (name !== sourceName && entry.querySuggestions?.querySuggestionsSource?.index === sourceName) names.push(name);
1901
+ }
1902
+ return names;
1903
+ }
1904
+ async function dropSuggestionIndexesOf(core, sourceName) {
1905
+ if (core.mutationCtx.capacityScope === "partition") return;
1906
+ for (const name of suggestionIndexesOf(core, sourceName)) {
1907
+ if (!core.indexRegistry.has(name)) continue;
1908
+ await emitWriteEventAfter(core, name, dropEngineIndex(core, name));
1909
+ }
1910
+ }
1911
+ async function clearLeftoversOfName(core, name) {
1912
+ if (core.mutationCtx.capacityScope === "partition") return;
1913
+ await dropSuggestionIndexesOf(core, name);
1914
+ await core.durability?.manager.removeQueryHistory?.(name);
1915
+ }
1916
+ function hooksOf(core) {
1917
+ return {
1918
+ async beforeCreate(indexName) {
1919
+ await clearLeftoversOfName(core, indexName);
1920
+ if (core.indexRegistry.has(indexName)) {
1921
+ throw new NarsilError(ErrorCodes.INDEX_ALREADY_EXISTS, `Index "${indexName}" already exists`, { indexName });
1922
+ }
1923
+ },
1924
+ beforeDrop: (indexName) => dropSuggestionIndexesOf(core, indexName),
1925
+ async afterDrop(indexName) {
1926
+ await attachedRuntimeOf(core)?.onDrop(indexName);
1927
+ },
1928
+ async shutdown() {
1929
+ await attachedRuntimeOf(core)?.stop().catch(() => void 0);
1930
+ }
1931
+ };
1932
+ }
1933
+ function installSuggestionHooks(core) {
1934
+ core.querySuggestionHooks ??= hooksOf(core);
1935
+ }
1936
+ function holdsEnabledSetting(core) {
1937
+ for (const entry of core.indexRegistry.values()) {
1938
+ if (entry.querySuggestions?.querySuggestions?.enabled === true) return true;
1939
+ }
1940
+ return false;
1941
+ }
1942
+ async function startQuerySuggestions(core, engine, config) {
1943
+ installSuggestionHooks(core);
1944
+ if (config?.querySuggestions === void 0 && !holdsEnabledSetting(core)) return;
1945
+ try {
1946
+ const { localRuntimeOf } = await loadSuggestionsCode();
1947
+ await localRuntimeOf(core, engine, config);
1948
+ } catch (error) {
1949
+ await engine.shutdown().catch(() => void 0);
1950
+ throw error;
1951
+ }
1952
+ }
1953
+
1954
+ // src/query-suggestions/restored-history.ts
1955
+ async function collect(chunks) {
1956
+ const parts = [];
1957
+ let length = 0;
1958
+ for await (const chunk of chunks) {
1959
+ parts.push(chunk);
1960
+ length += chunk.byteLength;
1961
+ }
1962
+ const bytes = new Uint8Array(length);
1963
+ let offset = 0;
1964
+ for (const part of parts) {
1965
+ bytes.set(part, offset);
1966
+ offset += part.byteLength;
1967
+ }
1968
+ return bytes;
1969
+ }
1970
+ var RestoredHistory = class {
1971
+ runtime;
1972
+ indexName;
1973
+ cutoff;
1974
+ prefix = restoreStagingPrefix();
1975
+ staged = [];
1976
+ received = 0;
1977
+ constructor(runtime, indexName, setting) {
1978
+ this.runtime = runtime;
1979
+ this.indexName = indexName;
1980
+ this.cutoff = dayOf(Date.now() - resolveSetting(setting).retentionDays * DAY_MS);
1981
+ }
1982
+ async add(chunks) {
1983
+ const position = this.received;
1984
+ this.received += 1;
1985
+ const bytes = await collect(chunks);
1986
+ const scrambler = this.runtime.scrambler;
1987
+ if (scrambler === null) return;
1988
+ const opened = await openCountsFile(bytes, null, scrambler.fingerprint);
1989
+ if (opened.kind !== "counts") {
1990
+ this.runtime.host.emit("querySuggestionsWarning", {
1991
+ indexName: this.indexName,
1992
+ reason: opened.kind === "other-key" ? "other-key" : "damaged-file",
1993
+ file: `${QUERY_HISTORY_ARCHIVE_PREFIX}${position}`
1994
+ });
1995
+ return;
1996
+ }
1997
+ if (opened.day < this.cutoff) return;
1998
+ const key = `${this.prefix}${position}.nrsl`;
1999
+ const indexUuid = this.runtime.host.clustered ? this.runtime.host.view(this.indexName)?.indexUuid ?? null : null;
2000
+ await this.runtime.host.files.write(key, opened.withIndexUuid(indexUuid, opened.savedAt));
2001
+ this.staged.push({ key, day: opened.day });
2002
+ }
2003
+ async commit() {
2004
+ if (this.received === 0) return;
2005
+ const { files } = this.runtime.host;
2006
+ await this.runtime.discardHistory(this.indexName);
2007
+ const sequences = /* @__PURE__ */ new Map();
2008
+ const ordered = [...this.staged].sort((left, right) => left.day < right.day ? -1 : left.day > right.day ? 1 : 0);
2009
+ for (const file of ordered) {
2010
+ const bytes = await files.read(file.key);
2011
+ if (bytes instanceof Uint8Array) {
2012
+ const sequence = sequences.get(file.day) ?? 0;
2013
+ sequences.set(file.day, sequence + 1);
2014
+ await files.write(restoredFileKey(this.indexName, file.day, sequence), bytes);
2015
+ }
2016
+ await files.remove(file.key);
2017
+ }
2018
+ this.staged.length = 0;
2019
+ await this.runtime.days?.forgetHistoryGroups(this.indexName);
2020
+ }
2021
+ async abandon() {
2022
+ for (const file of this.staged) await this.runtime.host.files.remove(file.key);
2023
+ this.staged.length = 0;
2024
+ }
2025
+ };
2026
+
2027
+ // src/query-suggestions/stored-state.ts
2028
+ function isPresent(value) {
2029
+ return value !== void 0 && value !== null;
2030
+ }
2031
+ function storedStateOf(raw) {
2032
+ const state = {};
2033
+ if (isPresent(raw.querySuggestions)) state.querySuggestions = validateSetting(raw.querySuggestions);
2034
+ if (isPresent(raw.bannedQueries)) {
2035
+ const banned = storedBanList(raw.bannedQueries);
2036
+ if (banned.length > 0) state.bannedQueries = banned;
2037
+ }
2038
+ if (isPresent(raw.querySuggestionsSource)) state.querySuggestionsSource = validateSource(raw.querySuggestionsSource);
2039
+ return Object.keys(state).length === 0 ? null : state;
2040
+ }
2041
+ function storedStateToWire(state) {
2042
+ const wire = {};
2043
+ if (state.querySuggestions !== void 0) wire.query_suggestions = settingToWire(state.querySuggestions);
2044
+ if (state.bannedQueries !== void 0) wire.banned_queries = [...state.bannedQueries];
2045
+ if (state.querySuggestionsSource !== void 0) {
2046
+ wire.query_suggestions_source = {
2047
+ index: state.querySuggestionsSource.index,
2048
+ group: state.querySuggestionsSource.group
2049
+ };
2050
+ }
2051
+ return wire;
2052
+ }
2053
+ function storedStateFromWire(raw) {
2054
+ const state = storedStateOf({
2055
+ bannedQueries: raw.banned_queries,
2056
+ querySuggestionsSource: raw.query_suggestions_source
2057
+ });
2058
+ if (!isPresent(raw.query_suggestions)) return state;
2059
+ return { ...state, querySuggestions: settingFromWire(raw.query_suggestions) };
2060
+ }
2061
+ function withState(current, change) {
2062
+ const next = { ...current, ...change };
2063
+ for (const key of Object.keys(next)) {
2064
+ const value = next[key];
2065
+ if (value === void 0 || Array.isArray(value) && value.length === 0) delete next[key];
2066
+ }
2067
+ return Object.keys(next).length === 0 ? null : next;
2068
+ }
2069
+
2070
+ export { BUILD_LIST_PAGE, BUILD_WRITE_BATCH, BYTES_PER_MIB, DAY_MS, DEFAULT_PREVIEW_LIMIT, DayStore, HEADER_SIZE, HISTORY_SWEEP_INTERVAL_MS, KEY_FINGERPRINT_BYTES, KEY_FINGERPRINT_LABEL, MAX_HISTORY_FILE_BYTES, MAX_MANUAL_SUGGESTION_COUNT, MAX_PREVIEW_LIMIT, MAX_SOURCE_INDEX_NAME_LENGTH, MAX_TIMER_DELAY_MS, MIN_KEY_BYTES, MS_PER_SECOND, QUERY_HISTORY_ARCHIVE_PREFIX, QUERY_HISTORY_FOLDER, RestoredHistory, SCHEDULE_OFF, SCRAMBLED_ID_BYTES, acceptedPhrase, attachRuntime, attachedRuntimeOf, banListFromInput, bindSuggestionStoreHost, buildRecordKey, checksumOfChunks, clearLeftoversOfName, crc32, crc32Final, crc32Init, crc32Update, dayOf, engineVersionParts, hasNrslMagic, historyFileOf, historyFilesOf, installSuggestionHooks, invalidSetting, isGroup, isPerson, latestMatchAtOrBefore, listHistoryFolder, nextMatchAfter, normaliseBan, oneSyncChangeAtATime, openChecksummedEnvelope, openCountsFile, packChecksummedEnvelope, packEnvelopeWithChecksum, parseSchedule, readHeader, removeHistoryFolder, removeRestoreLeftovers, requireFieldsInSchema, requireWithinFileLimit, resolveSetting, sameBytes, startQuerySuggestions, storedStateFromWire, storedStateOf, storedStateToWire, unionOfBans, validateSetting, withState, writeHeader };