@delali/narsil 0.3.1-next.99 → 0.4.1-next.211
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +45 -0
- package/README.md +30 -11
- package/dist/adapters/filesystem.d.ts +1 -1
- package/dist/adapters/filesystem.mjs +101 -13
- package/dist/adapters/gcs.d.ts +49 -0
- package/dist/adapters/gcs.mjs +213 -0
- package/dist/adapters/indexeddb.d.ts +1 -1
- package/dist/adapters/indexeddb.mjs +2 -7
- package/dist/adapters/memory.d.ts +1 -1
- package/dist/adapters/memory.mjs +5 -0
- package/dist/adapters/s3.d.ts +53 -0
- package/dist/adapters/s3.mjs +263 -0
- package/dist/{adapters-BQFQ4PRz.d.ts → adapters-pljAqmrC.d.ts} +33 -0
- package/dist/browser/chunk-4E5APHTM.mjs +1459 -0
- package/dist/browser/chunk-NDYADQ4Y.mjs +23426 -0
- package/dist/browser/chunk-QAOVAYAG.mjs +564 -0
- package/dist/browser/chunk-WC2DEXWU.mjs +29 -0
- package/dist/browser/chunk-YINZFPLT.mjs +1239 -0
- package/dist/browser/chunk-YJHGOJAC.mjs +15 -0
- package/dist/browser/{chunk-Z7QH2XHB.mjs → core-leaf-HWAXCEWX.mjs} +3987 -2103
- package/dist/browser/custom-token-results-R5BIX7SL.mjs +59 -0
- package/dist/browser/digest-leaf-64IPRPZD.mjs +151 -0
- package/dist/browser/durability-config-75NSFCQN.mjs +97 -0
- package/dist/browser/embed-GG6PS3EA.mjs +264 -0
- package/dist/browser/geo-index-GNHY4PUB.mjs +1484 -0
- package/dist/browser/gzip-JF3S6PPT.mjs +31 -0
- package/dist/browser/module-ZZTNG5JZ.mjs +5762 -0
- package/dist/browser/msgpack-leaf-44XNWSSY.mjs +302 -0
- package/dist/browser/optional-reads-XMVPG6HG.mjs +3230 -0
- package/dist/browser/optional-writes-76DWMRQH.mjs +3603 -0
- package/dist/browser/phrase-module-6UGFVW5J.mjs +617 -0
- package/dist/browser/resolve-vector-text-XRR2OVHE.mjs +85 -0
- package/dist/browser/restore-queue-SG5O735W.mjs +196 -0
- package/dist/browser/{runtime-NYE5TB6O.mjs → runtime-OMKE7DF4.mjs} +83 -25
- package/dist/browser/segment-codec-leaf-A427QJUX.mjs +554 -0
- package/dist/browser/segment-merge-leaf-7BZ54JPI.mjs +1636 -0
- package/dist/browser/storage-leaf-5UTR5K7J.mjs +19829 -0
- package/dist/browser/store-leaf-3L33V35P.mjs +2070 -0
- package/dist/browser/suggestions-leaf-UBNDVKBD.mjs +1430 -0
- package/dist/browser/worker-copies-JXSGYR5X.mjs +2951 -0
- package/dist/{capabilities-BTTIs96E.d.ts → capabilities-DMte5K9_.d.ts} +162 -18
- package/dist/chunk-2AFZE7Y3.mjs +72 -0
- package/dist/chunk-2CKDCC65.mjs +47 -0
- package/dist/chunk-3ECIRFS5.mjs +61 -0
- package/dist/chunk-44W3CBNG.mjs +7 -0
- package/dist/chunk-4H4RG2FR.mjs +13 -0
- package/dist/chunk-4PY4JYMY.mjs +11 -0
- package/dist/chunk-4ZPVAHWF.mjs +1289 -0
- package/dist/chunk-5EI5CXO6.mjs +197 -0
- package/dist/chunk-5F5MXWS5.mjs +283 -0
- package/dist/{chunk-OHP7QR33.mjs → chunk-5FLRXF73.mjs} +25 -4
- package/dist/chunk-6FOENO4E.mjs +273 -0
- package/dist/chunk-6GHX35ZM.mjs +108 -0
- package/dist/chunk-6MSZ543Y.mjs +32 -0
- package/dist/chunk-6N6HHBCQ.mjs +4596 -0
- package/dist/{chunk-LXPIXDUJ.mjs → chunk-7P5YWDRQ.mjs} +403 -169
- package/dist/{chunk-TGP5M5R7.mjs → chunk-7T4LAYT4.mjs} +1 -1
- package/dist/{chunk-GGSH6KBQ.mjs → chunk-ASZLNVWW.mjs} +23 -3
- package/dist/chunk-AVBPB3MI.mjs +602 -0
- package/dist/{chunk-UCDF35PU.mjs → chunk-AVGUEEQ3.mjs} +494 -245
- package/dist/chunk-BUMREPLU.mjs +15 -0
- package/dist/chunk-BY6NALRK.mjs +31921 -0
- package/dist/chunk-C4ZMXXTL.mjs +92 -0
- package/dist/{chunk-2WYMG3OZ.mjs → chunk-C74KPRR7.mjs} +274 -19
- package/dist/{chunk-RSRVGAIQ.mjs → chunk-D456ZFPQ.mjs} +3 -2
- package/dist/chunk-DXZ6JDWG.mjs +797 -0
- package/dist/chunk-FJXJZUWF.mjs +209 -0
- package/dist/chunk-FS3HPPSC.mjs +212 -0
- package/dist/chunk-FT4KLZXW.mjs +1164 -0
- package/dist/chunk-GR5GMFJJ.mjs +104 -0
- package/dist/chunk-GXCDWLQI.mjs +193 -0
- package/dist/chunk-GXED6R3Q.mjs +271 -0
- package/dist/chunk-HLPRHOSO.mjs +17 -0
- package/dist/chunk-IL5DRCNU.mjs +3041 -0
- package/dist/{chunk-P36MUFEJ.mjs → chunk-JDDXV6UF.mjs} +2 -1
- package/dist/chunk-K5HSUPYS.mjs +11 -0
- package/dist/chunk-KYIRXTCA.mjs +100 -0
- package/dist/chunk-LFXS6OF7.mjs +289 -0
- package/dist/chunk-LNGE7SLY.mjs +151 -0
- package/dist/chunk-M2Z5WVWW.mjs +194 -0
- package/dist/{chunk-45AVKHMJ.mjs → chunk-M3B7SGJB.mjs} +63 -50
- package/dist/chunk-M3OW2XSP.mjs +40 -0
- package/dist/chunk-MFPSQZ6P.mjs +127 -0
- package/dist/chunk-MRCJBWJZ.mjs +1292 -0
- package/dist/chunk-MSIN4NM3.mjs +25 -0
- package/dist/chunk-MW3CJVGP.mjs +219 -0
- package/dist/chunk-MYM6CAVK.mjs +76 -0
- package/dist/chunk-NJJN4FMM.mjs +84 -0
- package/dist/chunk-NM7I3BM7.mjs +59 -0
- package/dist/chunk-OIJ64LGT.mjs +19 -0
- package/dist/{chunk-K7D4ZTSW.mjs → chunk-P4F4UWLC.mjs} +1 -1
- package/dist/chunk-Q2AP2FSN.mjs +15 -0
- package/dist/chunk-R2PJKSNL.mjs +54 -0
- package/dist/{chunk-HXJBSHXF.mjs → chunk-R724DJSG.mjs} +5056 -2619
- package/dist/chunk-S2POFTLN.mjs +10 -0
- package/dist/chunk-S5QMWECL.mjs +7 -0
- package/dist/chunk-SLAC5PAB.mjs +5008 -0
- package/dist/chunk-T4HMSZCX.mjs +5 -0
- package/dist/chunk-TACP6VK6.mjs +111 -0
- package/dist/chunk-TGPKS2UM.mjs +125 -0
- package/dist/chunk-TKKXPKEO.mjs +979 -0
- package/dist/chunk-V6EUBAIT.mjs +19 -0
- package/dist/chunk-XL56L4IR.mjs +228 -0
- package/dist/chunk-XQIAMDAG.mjs +3076 -0
- package/dist/chunk-YTUZ4R22.mjs +1063 -0
- package/dist/{chunk-KV3AZFT3.mjs → chunk-ZKOQR7EA.mjs} +45 -1
- package/dist/chunk-ZPJFGGGI.mjs +638 -0
- package/dist/chunk-ZXX5GZEX.mjs +17 -0
- package/dist/{client-i9eLJrw5.d.ts → client-BKqtl0_V.d.ts} +442 -22
- package/dist/client.d.ts +11 -10
- package/dist/client.mjs +8 -898
- package/dist/config-Bnl7L-0H.d.ts +608 -0
- package/dist/{config-P__Diiy4.d.ts → config-CJ-jfI0a.d.ts} +119 -9
- package/dist/core-leaf-6JVHEVFB.mjs +8 -0
- package/dist/custom-token-results-NXHBVY7U.mjs +59 -0
- package/dist/digest-leaf-KXBGKYHZ.mjs +1 -0
- package/dist/direct-executor-V3WOYU2Y.mjs +17 -0
- package/dist/distribution/coordinator/etcd.d.ts +63 -7
- package/dist/distribution/coordinator/etcd.mjs +725 -81
- package/dist/distribution/coordinator/in-memory.d.ts +3 -3
- package/dist/distribution/coordinator/in-memory.mjs +65 -10
- package/dist/distribution/transport/grpc.d.ts +2 -2
- package/dist/distribution/transport/grpc.mjs +39 -10
- package/dist/distribution/transport/in-memory.d.ts +2 -2
- package/dist/distribution/transport/in-memory.mjs +9 -6
- package/dist/distribution/transport/tcp.d.ts +2 -2
- package/dist/distribution/transport/tcp.mjs +20 -15
- package/dist/distribution.d.ts +267 -56
- package/dist/distribution.mjs +23156 -11362
- package/dist/durability-config-VWS2K24E.mjs +96 -0
- package/dist/embed-4PHHKGBK.mjs +263 -0
- package/dist/embeddings/openai.d.ts +1 -1
- package/dist/embeddings/openai.mjs +2 -2
- package/dist/{memory-CUEDwr-F.d.ts → engine-qjAxJSKk.d.ts} +2254 -1123
- package/dist/geo-index-2ZMMWWSK.mjs +1484 -0
- package/dist/gzip-2UCZW2MM.mjs +31 -0
- package/dist/{index-lcdGLyJH.d.ts → index-B_vmxpgW.d.ts} +260 -102
- package/dist/index.browser.mjs +4 -4
- package/dist/index.d.ts +2133 -57
- package/dist/index.mjs +16 -32
- package/dist/index.react-native.mjs +12 -0
- package/dist/invalidation/broadcast-channel.d.ts +1 -1
- package/dist/invalidation/filesystem.d.ts +1 -1
- package/dist/invalidation/filesystem.mjs +1 -2
- package/dist/invalidation/noop.d.ts +1 -1
- package/dist/languages/albanian.mjs +1 -1
- package/dist/languages/amharic.mjs +1 -1
- package/dist/languages/arabic.mjs +1 -1
- package/dist/languages/armenian.mjs +1 -1
- package/dist/languages/azerbaijani.mjs +1 -1
- package/dist/languages/bambara.mjs +1 -1
- package/dist/languages/basque.mjs +1 -1
- package/dist/languages/belarusian.mjs +1 -1
- package/dist/languages/bengali.mjs +1 -1
- package/dist/languages/bosnian.mjs +1 -1
- package/dist/languages/breton.mjs +1 -1
- package/dist/languages/bulgarian.mjs +1 -1
- package/dist/languages/burmese.mjs +1 -1
- package/dist/languages/catalan.mjs +1 -1
- package/dist/languages/chinese.mjs +1 -1
- package/dist/languages/croatian.mjs +1 -1
- package/dist/languages/czech.mjs +1 -1
- package/dist/languages/dagbani.mjs +1 -1
- package/dist/languages/danish.mjs +1 -1
- package/dist/languages/dutch.mjs +1 -1
- package/dist/languages/english.mjs +1 -1
- package/dist/languages/esperanto.mjs +1 -1
- package/dist/languages/estonian.mjs +1 -1
- package/dist/languages/ewe.mjs +1 -1
- package/dist/languages/faroese.mjs +1 -1
- package/dist/languages/fijian.mjs +1 -1
- package/dist/languages/finnish.mjs +1 -1
- package/dist/languages/french.mjs +1 -1
- package/dist/languages/ga.mjs +1 -1
- package/dist/languages/galician.mjs +1 -1
- package/dist/languages/georgian.mjs +1 -1
- package/dist/languages/german.mjs +1 -1
- package/dist/languages/greek.mjs +1 -1
- package/dist/languages/guarani.mjs +1 -1
- package/dist/languages/gujarati.mjs +1 -1
- package/dist/languages/haitian-creole.mjs +1 -1
- package/dist/languages/hausa.mjs +1 -1
- package/dist/languages/hawaiian.mjs +1 -1
- package/dist/languages/hebrew.mjs +1 -1
- package/dist/languages/hindi.mjs +1 -1
- package/dist/languages/hungarian.mjs +1 -1
- package/dist/languages/icelandic.mjs +1 -1
- package/dist/languages/igbo.mjs +1 -1
- package/dist/languages/indonesian.mjs +1 -1
- package/dist/languages/irish.mjs +1 -1
- package/dist/languages/italian.mjs +1 -1
- package/dist/languages/japanese.mjs +1 -1
- package/dist/languages/kannada.mjs +1 -1
- package/dist/languages/kazakh.mjs +1 -1
- package/dist/languages/khmer.mjs +1 -1
- package/dist/languages/kinyarwanda.mjs +1 -1
- package/dist/languages/kirundi.mjs +1 -1
- package/dist/languages/korean.mjs +1 -1
- package/dist/languages/kurmanji.mjs +1 -1
- package/dist/languages/kyrgyz.mjs +1 -1
- package/dist/languages/lao.mjs +1 -1
- package/dist/languages/latin.mjs +1 -1
- package/dist/languages/latvian.mjs +1 -1
- package/dist/languages/lingala.mjs +1 -1
- package/dist/languages/lithuanian.mjs +1 -1
- package/dist/languages/luxembourgish.mjs +1 -1
- package/dist/languages/macedonian.mjs +1 -1
- package/dist/languages/malagasy.mjs +1 -1
- package/dist/languages/malay.mjs +1 -1
- package/dist/languages/malayalam.mjs +1 -1
- package/dist/languages/maltese.mjs +1 -1
- package/dist/languages/maori.mjs +1 -1
- package/dist/languages/marathi.mjs +1 -1
- package/dist/languages/nepali.mjs +1 -1
- package/dist/languages/norwegian.mjs +1 -1
- package/dist/languages/oromo.mjs +1 -1
- package/dist/languages/persian.mjs +1 -1
- package/dist/languages/polish.mjs +1 -1
- package/dist/languages/portuguese.mjs +1 -1
- package/dist/languages/punjabi.mjs +1 -1
- package/dist/languages/romanian.mjs +1 -1
- package/dist/languages/russian.mjs +1 -1
- package/dist/languages/samoan.mjs +1 -1
- package/dist/languages/sanskrit.mjs +1 -1
- package/dist/languages/scottish-gaelic.mjs +1 -1
- package/dist/languages/serbian.mjs +1 -1
- package/dist/languages/shona.mjs +1 -1
- package/dist/languages/sinhala.mjs +1 -1
- package/dist/languages/slovak.mjs +1 -1
- package/dist/languages/slovenian.mjs +1 -1
- package/dist/languages/sorani.mjs +1 -1
- package/dist/languages/spanish.mjs +1 -1
- package/dist/languages/swahili.mjs +1 -1
- package/dist/languages/swedish.mjs +1 -1
- package/dist/languages/tagalog.mjs +1 -1
- package/dist/languages/tamil.mjs +1 -1
- package/dist/languages/tatar.mjs +1 -1
- package/dist/languages/telugu.mjs +1 -1
- package/dist/languages/thai.mjs +1 -1
- package/dist/languages/tibetan.mjs +1 -1
- package/dist/languages/tigrinya.mjs +1 -1
- package/dist/languages/tongan.mjs +1 -1
- package/dist/languages/turkish.mjs +1 -1
- package/dist/languages/twi.mjs +1 -1
- package/dist/languages/ukrainian.mjs +1 -1
- package/dist/languages/urdu.mjs +1 -1
- package/dist/languages/vietnamese.mjs +1 -1
- package/dist/languages/welsh.mjs +1 -1
- package/dist/languages/wolof.mjs +1 -1
- package/dist/languages/xhosa.mjs +1 -1
- package/dist/languages/yoruba.mjs +1 -1
- package/dist/languages/zulu.mjs +1 -1
- package/dist/long-query-thread-UCRK7MSI.mjs +154 -0
- package/dist/module-5ZSLUQII.mjs +3729 -0
- package/dist/msgpack-leaf-H5ZYW7S7.mjs +9 -0
- package/dist/optional-reads-5HFMF2QZ.mjs +3373 -0
- package/dist/optional-writes-4UOQKETU.mjs +247 -0
- package/dist/paged-partition-archive-BBRLZCIF.mjs +253 -0
- package/dist/partial-words.browser.mjs +1650 -0
- package/dist/partial-words.mjs +17 -11
- package/dist/{chunk-ASY5PHYI.mjs → pattern.browser.mjs} +253 -311
- package/dist/pattern.mjs +6 -7
- package/dist/persistence/durability/checkpoint-worker.d.ts +93 -25
- package/dist/persistence/durability/checkpoint-worker.mjs +51 -41
- package/dist/phrase-module-EFESWOCR.mjs +635 -0
- package/dist/query-actions-HMJUVWGT.mjs +119 -0
- package/dist/query-suggestions/merge-worker.d.ts +55 -0
- package/dist/query-suggestions/merge-worker.mjs +53 -0
- package/dist/query-suggestions-CBYkrQ5T.d.ts +311 -0
- package/dist/query-suggestions.browser.mjs +47 -0
- package/dist/query-suggestions.d.ts +20 -0
- package/dist/query-suggestions.mjs +19 -0
- package/dist/react-native/chunks/chunk-3OEFSHVV.mjs +970 -0
- package/dist/{browser/chunk-RELZRJNR.mjs → react-native/chunks/chunk-43R4GCM7.mjs} +49 -50
- package/dist/react-native/chunks/chunk-AKRYHAVG.mjs +6729 -0
- package/dist/react-native/chunks/chunk-CW2ZFXSE.mjs +14 -0
- package/dist/react-native/chunks/chunk-G3WCDTOF.mjs +29640 -0
- package/dist/react-native/chunks/chunk-GXXHHA2T.mjs +212 -0
- package/dist/react-native/chunks/chunk-H3MDVPBO.mjs +151 -0
- package/dist/react-native/chunks/chunk-HQEVQEDJ.mjs +52 -0
- package/dist/react-native/chunks/chunk-HRDMIQ63.mjs +131 -0
- package/dist/react-native/chunks/chunk-HTBY4GON.mjs +565 -0
- package/dist/react-native/chunks/chunk-LX62Y4B5.mjs +61 -0
- package/dist/react-native/chunks/chunk-MRH36RN3.mjs +902 -0
- package/dist/react-native/chunks/chunk-Q32DDFKX.mjs +19 -0
- package/dist/react-native/chunks/chunk-UE4ANHSX.mjs +127 -0
- package/dist/react-native/chunks/chunk-UNZPWLVL.mjs +1063 -0
- package/dist/react-native/chunks/chunk-UO44MA5M.mjs +232 -0
- package/dist/react-native/chunks/chunk-UYKIV2KT.mjs +42 -0
- package/dist/react-native/chunks/chunk-VUJCSQG5.mjs +11 -0
- package/dist/react-native/chunks/chunk-WVHF4ZAK.mjs +802 -0
- package/dist/react-native/chunks/chunk-ZUJCUPVS.mjs +54 -0
- package/dist/react-native/chunks/core-leaf-S2KNBWDY.mjs +7 -0
- package/dist/react-native/chunks/custom-token-results-3OZEMRE7.mjs +59 -0
- package/dist/react-native/chunks/digest-leaf-X6OIFDIU.mjs +1 -0
- package/dist/react-native/chunks/durability-config-FCSD4MXY.mjs +96 -0
- package/dist/react-native/chunks/embed-HA2I7RCF.mjs +263 -0
- package/dist/react-native/chunks/geo-index-QEYKEVPH.mjs +1484 -0
- package/dist/react-native/chunks/gzip.react-native-CVM4NQG2.mjs +12 -0
- package/dist/react-native/chunks/module-FMBX75GP.mjs +5209 -0
- package/dist/react-native/chunks/msgpack-leaf-JMNGSTRW.mjs +7 -0
- package/dist/react-native/chunks/optional-reads-AFPO2FEB.mjs +3364 -0
- package/dist/react-native/chunks/optional-writes-XOH25XZI.mjs +296 -0
- package/dist/react-native/chunks/phrase-module-IGYBK7XH.mjs +636 -0
- package/dist/react-native/chunks/replace-IMZUVUOE.mjs +57 -0
- package/dist/react-native/chunks/resolve-vector-text-GE2CRJKI.mjs +84 -0
- package/dist/react-native/chunks/restore-queue-LLNMYB4Z.mjs +187 -0
- package/dist/react-native/chunks/runtime-UOHD2XUJ.mjs +615 -0
- package/dist/react-native/chunks/segment-codec-leaf-VR6O5GLC.mjs +13 -0
- package/dist/react-native/chunks/segment-merge-leaf-LLMWU4EW.mjs +14 -0
- package/dist/react-native/chunks/storage-leaf-JD7PIDO4.mjs +19633 -0
- package/dist/react-native/chunks/store-leaf-4UVCMYGN.mjs +15 -0
- package/dist/react-native/chunks/suggestions-leaf-J5VT5RHY.mjs +1321 -0
- package/dist/react-native/chunks/worker-copies-ZYHY7FKJ.mjs +2831 -0
- package/dist/react-native/partial-words.mjs +1658 -0
- package/dist/react-native/pattern.mjs +2570 -0
- package/dist/react-native/query-suggestions.mjs +55 -0
- package/dist/react-native/sync.mjs +2759 -0
- package/dist/react-native/synonyms.mjs +676 -0
- package/dist/react.d.ts +12 -8
- package/dist/react.mjs +45 -1
- package/dist/{rerank-NWTKK636.mjs → rerank-RI2PMNOD.mjs} +22 -12
- package/dist/rerankers/http.d.ts +1 -1
- package/dist/rerankers/http.mjs +2 -2
- package/dist/resolve-vector-text-OD4J3WHF.mjs +3 -0
- package/dist/restore-queue-PCKWDAKQ.mjs +192 -0
- package/dist/runtime-USQYKN3I.mjs +615 -0
- package/dist/{schema-4W_7tk1f.d.ts → schema-CzG9OUw5.d.ts} +70 -30
- package/dist/segment-codec-leaf-QJM7I6QG.mjs +18 -0
- package/dist/segment-merge-leaf-4BJGIJHY.mjs +18 -0
- package/dist/server.d.ts +348 -22
- package/dist/server.mjs +2211 -327
- package/dist/storage-leaf-GRRRI7UV.mjs +5962 -0
- package/dist/store-leaf-6VMMSVBE.mjs +20 -0
- package/dist/stored-values-BWIMpiT_.d.ts +25 -0
- package/dist/suggestions-leaf-4KPTMPTL.mjs +54 -0
- package/dist/sync/filesystem.d.ts +30 -0
- package/dist/sync/filesystem.mjs +288 -0
- package/dist/sync/indexeddb.browser.mjs +287 -0
- package/dist/sync/indexeddb.d.ts +30 -0
- package/dist/sync/indexeddb.mjs +242 -0
- package/dist/sync.browser.mjs +1271 -0
- package/dist/sync.d.ts +23 -0
- package/dist/sync.mjs +2712 -0
- package/dist/synonyms.browser.mjs +658 -0
- package/dist/synonyms.mjs +17 -7
- package/dist/tasks-65b6t6lk.d.ts +782 -0
- package/dist/thread-UJ3W5LVI.mjs +31 -0
- package/dist/{types-B3b68nfF.d.ts → types-Ck97nB3D.d.ts} +1 -1
- package/dist/types-CpCg-Gq-.d.ts +227 -0
- package/dist/{types-Cb9kiY5P.d.ts → types-DvncmvuQ.d.ts} +12 -1
- package/dist/vector/search-worker.d.ts +10 -2
- package/dist/vector/search-worker.mjs +11 -11
- package/dist/worker-copies-YSXZXIGC.mjs +2232 -0
- package/dist/workers/entry.mjs +75 -15
- package/openapi.json +6840 -0
- package/package.json +404 -19
- package/dist/browser/chunk-CN4I4FZU.mjs +0 -602
- package/dist/browser/chunk-HOZW3PKP.mjs +0 -9
- package/dist/browser/chunk-JPTQZWJQ.mjs +0 -10
- package/dist/browser/chunk-LD55ZGU3.mjs +0 -347
- package/dist/browser/chunk-NAFT3K6X.mjs +0 -24546
- package/dist/browser/chunk-NNG3EEB6.mjs +0 -2728
- package/dist/browser/chunk-QWES44AW.mjs +0 -473
- package/dist/browser/durability-wiring-PY3L7DES.mjs +0 -3679
- package/dist/browser/module-PDXYHT75.mjs +0 -2822
- package/dist/browser/release-76MXRPGV.mjs +0 -33
- package/dist/browser/restore-queue-DVIYJ7RU.mjs +0 -35
- package/dist/browser/snapshot-J57MZK6L.mjs +0 -1207
- package/dist/browser/write-L7FAQA47.mjs +0 -208
- package/dist/chunk-37CD3OKS.mjs +0 -87
- package/dist/chunk-5XNIRLFC.mjs +0 -96
- package/dist/chunk-7X5JST5L.mjs +0 -114
- package/dist/chunk-AV4IJKD2.mjs +0 -35
- package/dist/chunk-AY3AQ3UH.mjs +0 -73
- package/dist/chunk-BKB3XTMI.mjs +0 -707
- package/dist/chunk-C7SLA6CM.mjs +0 -19
- package/dist/chunk-COVCQ73F.mjs +0 -587
- package/dist/chunk-DJHHF2L5.mjs +0 -17
- package/dist/chunk-E74YLAIG.mjs +0 -24
- package/dist/chunk-FMWRCP2C.mjs +0 -57
- package/dist/chunk-GES7U2PM.mjs +0 -68
- package/dist/chunk-HJSN7UYG.mjs +0 -10
- package/dist/chunk-JFRNNBOM.mjs +0 -35
- package/dist/chunk-K25LZA54.mjs +0 -8484
- package/dist/chunk-KEHK2O6P.mjs +0 -13
- package/dist/chunk-KOKM4PQN.mjs +0 -52
- package/dist/chunk-KZ3NX2P7.mjs +0 -21
- package/dist/chunk-L3L5YFXC.mjs +0 -614
- package/dist/chunk-L5ORED7V.mjs +0 -25
- package/dist/chunk-LH4NZNJJ.mjs +0 -1731
- package/dist/chunk-LU2CE5AE.mjs +0 -507
- package/dist/chunk-LXWDQLTW.mjs +0 -30
- package/dist/chunk-MHOSCQCQ.mjs +0 -948
- package/dist/chunk-NKRBEGJZ.mjs +0 -1705
- package/dist/chunk-NTRMFARG.mjs +0 -462
- package/dist/chunk-NW4XDSP2.mjs +0 -212
- package/dist/chunk-OOEXLUGR.mjs +0 -117
- package/dist/chunk-ORBLD3EL.mjs +0 -6
- package/dist/chunk-OWNZYMRE.mjs +0 -3591
- package/dist/chunk-PPY2GSIB.mjs +0 -44
- package/dist/chunk-QY7GC2MW.mjs +0 -33
- package/dist/chunk-R6A773WK.mjs +0 -226
- package/dist/chunk-R6PR4WYZ.mjs +0 -68
- package/dist/chunk-R7EEKAJE.mjs +0 -593
- package/dist/chunk-RONEOEON.mjs +0 -426
- package/dist/chunk-VWULPWRJ.mjs +0 -7469
- package/dist/chunk-WCJKUGHB.mjs +0 -262
- package/dist/chunk-WQBCBKY3.mjs +0 -8
- package/dist/chunk-X5CV2E2B.mjs +0 -1
- package/dist/chunk-YA7XLE2R.mjs +0 -1256
- package/dist/chunk-YTJCGKM4.mjs +0 -56
- package/dist/chunk-ZG477IPV.mjs +0 -526
- package/dist/chunk-ZYNAMIYX.mjs +0 -113
- package/dist/config-BGB_fDtq.d.ts +0 -237
- package/dist/direct-executor-CXZ66ACP.mjs +0 -22
- package/dist/durability-wiring-QTHOGFMZ.mjs +0 -3229
- package/dist/engine-BGCEm9E_.d.ts +0 -577
- package/dist/module-3NONKBZX.mjs +0 -1860
- package/dist/release-BH3DKW3T.mjs +0 -33
- package/dist/restore-queue-JPRBUYII.mjs +0 -35
- package/dist/runtime-ZKYZFJJC.mjs +0 -177
- package/dist/snapshot-HOGXNO5W.mjs +0 -793
- package/dist/tasks-HG3rlxx6.d.ts +0 -302
- package/dist/thread-B657LQXK.mjs +0 -692
- package/dist/write-XLIGTXBU.mjs +0 -238
|
@@ -0,0 +1,1271 @@
|
|
|
1
|
+
import { createSerial, KEPT_ENTRY_FLUSH_IDLE_MS, KEPT_ENTRY_OVERHEAD_BYTES, DEFAULT_REPLAY_LIMIT_OF_SNAPSHOT, BACKOFF_DOUBLINGS_LIMIT, KEPT_ENTRY_FLUSH_BYTES, UPLOAD_BATCH_WRITES, RETRY_MAX_DELAY_MS, UPLOAD_RETRY_DELAY_MS, DOWNLOAD_SAVE_BYTES, SNAPSHOT_MEMORY_PER_ARCHIVE_BYTE, SNAPSHOT_RETRY_DELAY_MS, RECONNECT_MIN_DELAY_MS } from './browser/chunk-WC2DEXWU.mjs';
|
|
2
|
+
import { hasLanguage, hasTokenizer, hasStopWords, readHeapStatistics, loadStorageCode, getLanguage } from './browser/chunk-NDYADQ4Y.mjs';
|
|
3
|
+
import './browser/chunk-QAOVAYAG.mjs';
|
|
4
|
+
import { isSyncEpoch } from './browser/chunk-YJHGOJAC.mjs';
|
|
5
|
+
import { createNarsilClient } from './browser/chunk-4E5APHTM.mjs';
|
|
6
|
+
import { loadMessagePack, loadedMessagePack, NarsilError, ErrorCodes, requireLoadedMessagePack, getNestedValue, ClientErrorCodes, extractVectorFieldsFromSchema } from './browser/chunk-YINZFPLT.mjs';
|
|
7
|
+
|
|
8
|
+
// src/sync/protocol.ts
|
|
9
|
+
var SYNC_SUBPROTOCOL = "narsil.sync.v1";
|
|
10
|
+
var SYNC_CLOSE_CREDENTIAL_EXPIRED = 4401;
|
|
11
|
+
var SYNC_CLOSE_RESYNC = 4410;
|
|
12
|
+
|
|
13
|
+
// src/sync/device/context.ts
|
|
14
|
+
function createChanges() {
|
|
15
|
+
let waiters = [];
|
|
16
|
+
return {
|
|
17
|
+
notify() {
|
|
18
|
+
const woken = waiters;
|
|
19
|
+
waiters = [];
|
|
20
|
+
for (const wake of woken) wake();
|
|
21
|
+
},
|
|
22
|
+
next() {
|
|
23
|
+
return new Promise((resolve) => waiters.push(resolve));
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function backoffDelayMs(attempt, minimumMs, maximumMs) {
|
|
28
|
+
const ceiling = Math.min(maximumMs, minimumMs * 2 ** Math.min(attempt, BACKOFF_DOUBLINGS_LIMIT));
|
|
29
|
+
return Math.round(ceiling / 2 + Math.random() * ceiling / 2);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/sync/device/messages.ts
|
|
33
|
+
var RESYNC_REASONS = /* @__PURE__ */ new Set(["epoch", "history", "ahead"]);
|
|
34
|
+
function isRecord(value) {
|
|
35
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) && !(value instanceof Uint8Array);
|
|
36
|
+
}
|
|
37
|
+
function isCount(value) {
|
|
38
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
39
|
+
}
|
|
40
|
+
function malformed(detail) {
|
|
41
|
+
return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, `The sync feed sent a malformed message: ${detail}`);
|
|
42
|
+
}
|
|
43
|
+
function headsOf(raw) {
|
|
44
|
+
if (!Array.isArray(raw)) throw malformed("heads must be a list");
|
|
45
|
+
const seqNos = new Array(raw.length).fill(-1);
|
|
46
|
+
for (const item of raw) {
|
|
47
|
+
if (!isRecord(item) || !isCount(item.partitionId) || !isCount(item.seqNo)) throw malformed("a head is invalid");
|
|
48
|
+
if (item.partitionId >= seqNos.length || seqNos[item.partitionId] !== -1) {
|
|
49
|
+
throw malformed("heads must name each partition once");
|
|
50
|
+
}
|
|
51
|
+
seqNos[item.partitionId] = item.seqNo;
|
|
52
|
+
}
|
|
53
|
+
return seqNos;
|
|
54
|
+
}
|
|
55
|
+
function stringList(value) {
|
|
56
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
57
|
+
}
|
|
58
|
+
function settingsOf(raw) {
|
|
59
|
+
if (!isRecord(raw) || typeof raw.language !== "string") throw malformed("settings need a language");
|
|
60
|
+
const optionalString2 = (key) => {
|
|
61
|
+
const value = raw[key];
|
|
62
|
+
if (value === void 0 || value === null) return void 0;
|
|
63
|
+
if (typeof value !== "string") throw malformed(`settings.${key} must be a string`);
|
|
64
|
+
return value;
|
|
65
|
+
};
|
|
66
|
+
const tokenizer = optionalString2("tokenizer");
|
|
67
|
+
const stopWords = optionalString2("stopWords");
|
|
68
|
+
const analysisRevision = optionalString2("analysisRevision");
|
|
69
|
+
if (raw.stopWordList !== void 0 && raw.stopWordList !== null && !stringList(raw.stopWordList)) {
|
|
70
|
+
throw malformed("settings.stopWordList must be a list of strings");
|
|
71
|
+
}
|
|
72
|
+
if (raw.bm25 !== void 0 && raw.bm25 !== null && !isRecord(raw.bm25)) throw malformed("settings.bm25 is invalid");
|
|
73
|
+
if (raw.synonyms !== void 0 && raw.synonyms !== null && !Array.isArray(raw.synonyms)) {
|
|
74
|
+
throw malformed("settings.synonyms must be a list");
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
language: raw.language,
|
|
78
|
+
...tokenizer !== void 0 ? { tokenizer } : {},
|
|
79
|
+
...stopWords !== void 0 ? { stopWords } : {},
|
|
80
|
+
...stringList(raw.stopWordList) ? { stopWordList: raw.stopWordList } : {},
|
|
81
|
+
...analysisRevision !== void 0 ? { analysisRevision } : {},
|
|
82
|
+
...isRecord(raw.bm25) ? { bm25: raw.bm25 } : {},
|
|
83
|
+
...Array.isArray(raw.synonyms) ? { synonyms: raw.synonyms } : {}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function entriesOf(partitionId, raw) {
|
|
87
|
+
if (!Array.isArray(raw)) throw malformed("entries must be a list");
|
|
88
|
+
let previous = -1;
|
|
89
|
+
return raw.map((item) => {
|
|
90
|
+
if (!isRecord(item) || !isCount(item.seqNo) || typeof item.documentId !== "string") {
|
|
91
|
+
throw malformed("an entry is invalid");
|
|
92
|
+
}
|
|
93
|
+
if (item.seqNo <= previous) throw malformed("entries must rise in sequence order");
|
|
94
|
+
if (item.partitionId !== void 0 && item.partitionId !== partitionId) {
|
|
95
|
+
throw malformed("an entry belongs to another partition");
|
|
96
|
+
}
|
|
97
|
+
previous = item.seqNo;
|
|
98
|
+
if (item.operation === "DELETE") {
|
|
99
|
+
return { partitionId, seqNo: item.seqNo, documentId: item.documentId, document: null };
|
|
100
|
+
}
|
|
101
|
+
if (item.operation !== "INDEX" || !(item.document instanceof Uint8Array)) throw malformed("an entry is invalid");
|
|
102
|
+
return { partitionId, seqNo: item.seqNo, documentId: item.documentId, document: item.document };
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
function decodeFeedMessage(bytes) {
|
|
106
|
+
const { decode } = requireLoadedMessagePack();
|
|
107
|
+
let raw;
|
|
108
|
+
try {
|
|
109
|
+
raw = decode(bytes);
|
|
110
|
+
} catch {
|
|
111
|
+
throw malformed("a frame is not MessagePack");
|
|
112
|
+
}
|
|
113
|
+
if (!isRecord(raw)) throw malformed("a frame is not a map");
|
|
114
|
+
switch (raw.type) {
|
|
115
|
+
case "subscribed": {
|
|
116
|
+
if (!isSyncEpoch(raw.epoch) || !isCount(raw.partitionCount)) throw malformed("subscribed is invalid");
|
|
117
|
+
if (!isCount(raw.bytesBehind) || !isCount(raw.snapshotBytes) || !isCount(raw.windowBytes)) {
|
|
118
|
+
throw malformed("subscribed is invalid");
|
|
119
|
+
}
|
|
120
|
+
const heads = headsOf(raw.heads);
|
|
121
|
+
if (heads.length !== raw.partitionCount) throw malformed("subscribed needs one head for each partition");
|
|
122
|
+
return {
|
|
123
|
+
type: "subscribed",
|
|
124
|
+
epoch: raw.epoch,
|
|
125
|
+
partitionCount: raw.partitionCount,
|
|
126
|
+
heads,
|
|
127
|
+
bytesBehind: raw.bytesBehind,
|
|
128
|
+
snapshotBytes: raw.snapshotBytes,
|
|
129
|
+
windowBytes: raw.windowBytes,
|
|
130
|
+
settings: settingsOf(raw.settings)
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
case "entries":
|
|
134
|
+
if (!isCount(raw.partitionId)) throw malformed("entries need a partition");
|
|
135
|
+
return { type: "entries", partitionId: raw.partitionId, entries: entriesOf(raw.partitionId, raw.entries) };
|
|
136
|
+
case "settings":
|
|
137
|
+
return { type: "settings", settings: settingsOf(raw.settings) };
|
|
138
|
+
case "checkpoint":
|
|
139
|
+
if (!isCount(raw.id)) throw malformed("checkpoint needs an id");
|
|
140
|
+
return { type: "checkpoint", id: raw.id, heads: headsOf(raw.heads) };
|
|
141
|
+
case "resync":
|
|
142
|
+
if (typeof raw.reason !== "string" || !RESYNC_REASONS.has(raw.reason)) throw malformed("resync is invalid");
|
|
143
|
+
return { type: "resync", reason: raw.reason };
|
|
144
|
+
default:
|
|
145
|
+
throw malformed(`unknown type ${String(raw.type)}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function positionsOf(seqNos) {
|
|
149
|
+
return seqNos.map((seqNo, partitionId) => ({ partitionId, seqNo }));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// src/sync/device/feed.ts
|
|
153
|
+
var CLOSE_NORMAL = 1e3;
|
|
154
|
+
function feedUrlOf(base, indexName) {
|
|
155
|
+
const location = globalThis.location?.href;
|
|
156
|
+
let parsed;
|
|
157
|
+
try {
|
|
158
|
+
parsed = location === void 0 ? new URL(base) : new URL(base, location);
|
|
159
|
+
} catch {
|
|
160
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, `The device cannot read "${base}" as a server address`, {
|
|
161
|
+
url: base
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
165
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The device needs an http or https server address", { url: base });
|
|
166
|
+
}
|
|
167
|
+
parsed.protocol = parsed.protocol === "https:" ? "wss:" : "ws:";
|
|
168
|
+
parsed.pathname = `${parsed.pathname.replace(/\/+$/, "")}/indexes/${encodeURIComponent(indexName)}/sync/feed`;
|
|
169
|
+
parsed.search = "";
|
|
170
|
+
parsed.hash = "";
|
|
171
|
+
return parsed.toString();
|
|
172
|
+
}
|
|
173
|
+
function feedFailed(message) {
|
|
174
|
+
return new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, message);
|
|
175
|
+
}
|
|
176
|
+
function openFeedConnection(url, credential, handlers) {
|
|
177
|
+
if (typeof WebSocket === "undefined") {
|
|
178
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "Device sync needs a runtime with a global WebSocket");
|
|
179
|
+
}
|
|
180
|
+
const socket = new WebSocket(url, credential === null ? [SYNC_SUBPROTOCOL] : [SYNC_SUBPROTOCOL, credential]);
|
|
181
|
+
socket.binaryType = "arraybuffer";
|
|
182
|
+
let closed = false;
|
|
183
|
+
function finish(code) {
|
|
184
|
+
if (closed) return;
|
|
185
|
+
closed = true;
|
|
186
|
+
handlers.onClose(code);
|
|
187
|
+
}
|
|
188
|
+
function closeSocket(reason) {
|
|
189
|
+
if (socket.readyState === WebSocket.CONNECTING || socket.readyState === WebSocket.OPEN) {
|
|
190
|
+
socket.close(CLOSE_NORMAL, reason);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
socket.onopen = () => {
|
|
194
|
+
if (socket.protocol !== SYNC_SUBPROTOCOL) {
|
|
195
|
+
handlers.onFailure(
|
|
196
|
+
new NarsilError(
|
|
197
|
+
ClientErrorCodes.CLIENT_INVALID_RESPONSE,
|
|
198
|
+
`The server answered the feed with the subprotocol "${socket.protocol}"`
|
|
199
|
+
)
|
|
200
|
+
);
|
|
201
|
+
closeSocket("subprotocol");
|
|
202
|
+
finish(CLOSE_NORMAL);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
handlers.onOpen();
|
|
206
|
+
};
|
|
207
|
+
socket.onmessage = (event) => {
|
|
208
|
+
if (closed) return;
|
|
209
|
+
if (!(event.data instanceof ArrayBuffer)) {
|
|
210
|
+
handlers.onFailure(new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, "The feed sent a text frame"));
|
|
211
|
+
closeSocket("text frame");
|
|
212
|
+
finish(CLOSE_NORMAL);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
let message;
|
|
216
|
+
try {
|
|
217
|
+
message = decodeFeedMessage(new Uint8Array(event.data));
|
|
218
|
+
} catch (error) {
|
|
219
|
+
handlers.onFailure(error);
|
|
220
|
+
closeSocket("malformed message");
|
|
221
|
+
finish(CLOSE_NORMAL);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
handlers.onMessage(message);
|
|
225
|
+
};
|
|
226
|
+
socket.onerror = () => {
|
|
227
|
+
if (!closed) handlers.onFailure(feedFailed(`The feed at ${url} failed`));
|
|
228
|
+
};
|
|
229
|
+
socket.onclose = (event) => finish(event.code);
|
|
230
|
+
return {
|
|
231
|
+
send(message) {
|
|
232
|
+
if (!closed && socket.readyState === WebSocket.OPEN) socket.send(requireLoadedMessagePack().encode(message));
|
|
233
|
+
},
|
|
234
|
+
close(reason) {
|
|
235
|
+
closeSocket(reason);
|
|
236
|
+
finish(CLOSE_NORMAL);
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// src/sync/device/local-writes.ts
|
|
242
|
+
var NO_VECTOR_FIELDS = /* @__PURE__ */ new Set();
|
|
243
|
+
function vectorFieldPathsOf(engine, indexName) {
|
|
244
|
+
if (!holdsIndex(engine, indexName)) return NO_VECTOR_FIELDS;
|
|
245
|
+
return new Set(extractVectorFieldsFromSchema(engine.getStats(indexName).schema).keys());
|
|
246
|
+
}
|
|
247
|
+
function decodedDocument(bytes, vectorFieldPaths) {
|
|
248
|
+
const code = requireLoadedMessagePack();
|
|
249
|
+
const document = code.decode(bytes);
|
|
250
|
+
code.restoreVectorFields(document, vectorFieldPaths);
|
|
251
|
+
return document;
|
|
252
|
+
}
|
|
253
|
+
function documentOfEntry(engine, indexName, bytes) {
|
|
254
|
+
return decodedDocument(bytes, vectorFieldPathsOf(engine, indexName));
|
|
255
|
+
}
|
|
256
|
+
function portableCopyOf(engine, indexName, document) {
|
|
257
|
+
const code = requireLoadedMessagePack();
|
|
258
|
+
const vectorFieldPaths = vectorFieldPathsOf(engine, indexName);
|
|
259
|
+
const copy = decodedDocument(code.encodeDocument(document), vectorFieldPaths);
|
|
260
|
+
for (const fieldPath of vectorFieldPaths) {
|
|
261
|
+
const vector = getNestedValue(copy, fieldPath);
|
|
262
|
+
if (vector instanceof Float32Array) code.setNestedValue(copy, fieldPath, Array.from(vector));
|
|
263
|
+
}
|
|
264
|
+
return copy;
|
|
265
|
+
}
|
|
266
|
+
function bytesEqual(left, right) {
|
|
267
|
+
if (left.byteLength !== right.byteLength) return false;
|
|
268
|
+
for (let at = 0; at < left.byteLength; at++) {
|
|
269
|
+
if (left[at] !== right[at]) return false;
|
|
270
|
+
}
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
function holdsIndex(engine, indexName) {
|
|
274
|
+
return engine.listIndexes().some((index) => index.name === indexName);
|
|
275
|
+
}
|
|
276
|
+
async function putLocally(engine, indexName, documentId, document) {
|
|
277
|
+
if (await engine.has(indexName, documentId)) {
|
|
278
|
+
await engine.update(indexName, documentId, document);
|
|
279
|
+
} else {
|
|
280
|
+
await engine.insert(indexName, document, documentId);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
async function removeLocally(engine, indexName, documentId) {
|
|
284
|
+
try {
|
|
285
|
+
await engine.remove(indexName, documentId);
|
|
286
|
+
} catch (error) {
|
|
287
|
+
if (!(error instanceof NarsilError) || error.code !== ErrorCodes.DOC_NOT_FOUND) throw error;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async function holdsSameDocument(engine, indexName, documentId, encoded) {
|
|
291
|
+
const held = await engine.get(indexName, documentId);
|
|
292
|
+
if (encoded === null || held === void 0) return encoded === null && held === void 0;
|
|
293
|
+
return bytesEqual(requireLoadedMessagePack().encodeDocument(held), encoded);
|
|
294
|
+
}
|
|
295
|
+
async function applyEntryLocally(engine, indexName, entry) {
|
|
296
|
+
if (await holdsSameDocument(engine, indexName, entry.documentId, entry.document)) return;
|
|
297
|
+
if (entry.document === null) {
|
|
298
|
+
await removeLocally(engine, indexName, entry.documentId);
|
|
299
|
+
} else {
|
|
300
|
+
await putLocally(engine, indexName, entry.documentId, documentOfEntry(engine, indexName, entry.document));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async function applyWriteLocally(engine, indexName, write) {
|
|
304
|
+
if (write.document === null) {
|
|
305
|
+
await removeLocally(engine, indexName, write.documentId);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const encoded = requireLoadedMessagePack().encodeDocument(write.document);
|
|
309
|
+
if (await holdsSameDocument(engine, indexName, write.documentId, encoded)) return;
|
|
310
|
+
await putLocally(engine, indexName, write.documentId, write.document);
|
|
311
|
+
}
|
|
312
|
+
function newestByDocument(entries) {
|
|
313
|
+
const newest = /* @__PURE__ */ new Map();
|
|
314
|
+
for (const entry of entries) {
|
|
315
|
+
newest.delete(entry.documentId);
|
|
316
|
+
newest.set(entry.documentId, entry);
|
|
317
|
+
}
|
|
318
|
+
return newest;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// src/sync/device/progress.ts
|
|
322
|
+
function entryBytes(entry) {
|
|
323
|
+
return entry.documentId.length * 2 + (entry.document?.byteLength ?? 0) + KEPT_ENTRY_OVERHEAD_BYTES;
|
|
324
|
+
}
|
|
325
|
+
function createProgress(context) {
|
|
326
|
+
const { engine, indexName, store, queue, work } = context;
|
|
327
|
+
let position = null;
|
|
328
|
+
let received = [];
|
|
329
|
+
let unsaved = 0;
|
|
330
|
+
let flushThreshold = KEPT_ENTRY_FLUSH_BYTES;
|
|
331
|
+
let flushTimer = null;
|
|
332
|
+
let acknowledge = null;
|
|
333
|
+
const deferred = /* @__PURE__ */ new Map();
|
|
334
|
+
function cancelFlush() {
|
|
335
|
+
if (flushTimer !== null) clearTimeout(flushTimer);
|
|
336
|
+
flushTimer = null;
|
|
337
|
+
}
|
|
338
|
+
async function saveWithinWork() {
|
|
339
|
+
cancelFlush();
|
|
340
|
+
if (position === null) return;
|
|
341
|
+
if (unsaved === 0 && received.every((seqNo, partitionId) => seqNo === position?.seqNos[partitionId])) return;
|
|
342
|
+
await context.makeDurable();
|
|
343
|
+
const next = { epoch: position.epoch, seqNos: [...received] };
|
|
344
|
+
await store.writePosition(next);
|
|
345
|
+
position = next;
|
|
346
|
+
await store.clearKeptEntries();
|
|
347
|
+
unsaved = 0;
|
|
348
|
+
acknowledge?.(next.seqNos);
|
|
349
|
+
}
|
|
350
|
+
function scheduleFlush() {
|
|
351
|
+
if (context.stopped() || unsaved === 0) return;
|
|
352
|
+
if (unsaved >= flushThreshold) {
|
|
353
|
+
cancelFlush();
|
|
354
|
+
work.run(saveWithinWork).catch(context.report);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (flushTimer !== null) return;
|
|
358
|
+
flushTimer = setTimeout(() => {
|
|
359
|
+
flushTimer = null;
|
|
360
|
+
work.run(saveWithinWork).catch(context.report);
|
|
361
|
+
}, KEPT_ENTRY_FLUSH_IDLE_MS);
|
|
362
|
+
}
|
|
363
|
+
async function reapplyQueue() {
|
|
364
|
+
if (!holdsIndex(engine, indexName)) return;
|
|
365
|
+
for (const write of queue.ordered()) {
|
|
366
|
+
try {
|
|
367
|
+
await applyWriteLocally(engine, indexName, write);
|
|
368
|
+
} catch (error) {
|
|
369
|
+
await store.removeWrites([write.sequence]);
|
|
370
|
+
queue.remove([write.sequence]);
|
|
371
|
+
context.report(error);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return {
|
|
376
|
+
position: () => position,
|
|
377
|
+
received: () => received,
|
|
378
|
+
recover: () => work.run(async () => {
|
|
379
|
+
position = await store.readPosition();
|
|
380
|
+
if (position !== null && !holdsIndex(engine, indexName)) {
|
|
381
|
+
position = null;
|
|
382
|
+
await store.writePosition(null);
|
|
383
|
+
}
|
|
384
|
+
queue.reset(await store.queuedWrites());
|
|
385
|
+
const kept = await store.keptEntries();
|
|
386
|
+
received = position === null ? [] : [...position.seqNos];
|
|
387
|
+
const saved = position?.seqNos ?? [];
|
|
388
|
+
const fresh = kept.filter((entry) => entry.seqNo > (saved[entry.partitionId] ?? Number.POSITIVE_INFINITY));
|
|
389
|
+
for (const entry of newestByDocument(fresh).values()) {
|
|
390
|
+
if (!queue.holdsDocument(entry.documentId)) await applyEntryLocally(engine, indexName, entry);
|
|
391
|
+
}
|
|
392
|
+
for (const entry of fresh) {
|
|
393
|
+
received[entry.partitionId] = Math.max(received[entry.partitionId] ?? 0, entry.seqNo);
|
|
394
|
+
}
|
|
395
|
+
unsaved = fresh.length;
|
|
396
|
+
if (fresh.length > 0) await saveWithinWork();
|
|
397
|
+
else if (kept.length > 0) await store.clearKeptEntries();
|
|
398
|
+
await reapplyQueue();
|
|
399
|
+
}),
|
|
400
|
+
async keep(partitionId, entries) {
|
|
401
|
+
await work.run(async () => {
|
|
402
|
+
const last = received[partitionId];
|
|
403
|
+
if (last === void 0) return;
|
|
404
|
+
const fresh = entries.filter((entry) => entry.seqNo > last);
|
|
405
|
+
const newest = fresh[fresh.length - 1];
|
|
406
|
+
if (newest === void 0) return;
|
|
407
|
+
await store.keepEntries(fresh);
|
|
408
|
+
for (const entry of fresh) {
|
|
409
|
+
unsaved += entryBytes(entry);
|
|
410
|
+
if (queue.holdsDocument(entry.documentId)) deferred.set(entry.documentId, entry);
|
|
411
|
+
else await applyEntryLocally(engine, indexName, entry);
|
|
412
|
+
}
|
|
413
|
+
received[partitionId] = newest.seqNo;
|
|
414
|
+
});
|
|
415
|
+
scheduleFlush();
|
|
416
|
+
},
|
|
417
|
+
async settleUploaded(sequences) {
|
|
418
|
+
await work.run(async () => {
|
|
419
|
+
await store.removeWrites(sequences);
|
|
420
|
+
queue.remove(sequences);
|
|
421
|
+
for (const [documentId, entry] of deferred) {
|
|
422
|
+
if (queue.holdsDocument(documentId)) continue;
|
|
423
|
+
deferred.delete(documentId);
|
|
424
|
+
await applyEntryLocally(engine, indexName, entry);
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
scheduleFlush();
|
|
428
|
+
},
|
|
429
|
+
forgetDeferred(documentId) {
|
|
430
|
+
deferred.delete(documentId);
|
|
431
|
+
},
|
|
432
|
+
async adopt(next) {
|
|
433
|
+
cancelFlush();
|
|
434
|
+
deferred.clear();
|
|
435
|
+
await reapplyQueue();
|
|
436
|
+
await context.makeDurable();
|
|
437
|
+
await store.writePosition(next);
|
|
438
|
+
position = { epoch: next.epoch, seqNos: [...next.seqNos] };
|
|
439
|
+
received = [...next.seqNos];
|
|
440
|
+
unsaved = 0;
|
|
441
|
+
await store.clearDownload();
|
|
442
|
+
},
|
|
443
|
+
reapplyQueue,
|
|
444
|
+
save: () => work.run(saveWithinWork),
|
|
445
|
+
reached(heads) {
|
|
446
|
+
return heads.length === received.length && heads.every((head, partitionId) => (received[partitionId] ?? -1) >= head);
|
|
447
|
+
},
|
|
448
|
+
setFlushThreshold(windowBytes) {
|
|
449
|
+
flushThreshold = Math.max(1, Math.min(KEPT_ENTRY_FLUSH_BYTES, Math.floor(windowBytes / 2)));
|
|
450
|
+
},
|
|
451
|
+
setAcknowledge(next) {
|
|
452
|
+
acknowledge = next;
|
|
453
|
+
},
|
|
454
|
+
cancelFlush,
|
|
455
|
+
unsavedBytes: () => unsaved
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// src/sync/device/queue.ts
|
|
460
|
+
var QueueMirror = class {
|
|
461
|
+
writes = /* @__PURE__ */ new Map();
|
|
462
|
+
sequencesByDocument = /* @__PURE__ */ new Map();
|
|
463
|
+
uploaded = /* @__PURE__ */ new Set();
|
|
464
|
+
get size() {
|
|
465
|
+
return this.writes.size;
|
|
466
|
+
}
|
|
467
|
+
reset(writes) {
|
|
468
|
+
this.writes.clear();
|
|
469
|
+
this.sequencesByDocument.clear();
|
|
470
|
+
this.uploaded.clear();
|
|
471
|
+
for (const write of [...writes].sort((a, b) => a.sequence - b.sequence)) this.add(write);
|
|
472
|
+
}
|
|
473
|
+
add(write) {
|
|
474
|
+
this.writes.set(write.sequence, write);
|
|
475
|
+
const sequences = this.sequencesByDocument.get(write.documentId);
|
|
476
|
+
if (sequences === void 0) this.sequencesByDocument.set(write.documentId, [write.sequence]);
|
|
477
|
+
else sequences.push(write.sequence);
|
|
478
|
+
}
|
|
479
|
+
remove(sequences) {
|
|
480
|
+
for (const sequence of sequences) {
|
|
481
|
+
const write = this.writes.get(sequence);
|
|
482
|
+
if (write === void 0) continue;
|
|
483
|
+
this.writes.delete(sequence);
|
|
484
|
+
this.uploaded.delete(sequence);
|
|
485
|
+
const remaining = (this.sequencesByDocument.get(write.documentId) ?? []).filter((held) => held !== sequence);
|
|
486
|
+
if (remaining.length === 0) this.sequencesByDocument.delete(write.documentId);
|
|
487
|
+
else this.sequencesByDocument.set(write.documentId, remaining);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
holdsDocument(documentId) {
|
|
491
|
+
return this.sequencesByDocument.has(documentId);
|
|
492
|
+
}
|
|
493
|
+
ordered() {
|
|
494
|
+
return [...this.writes.values()].sort((a, b) => a.sequence - b.sequence);
|
|
495
|
+
}
|
|
496
|
+
latestAwaitingUpload() {
|
|
497
|
+
const latest = [];
|
|
498
|
+
for (const sequences of this.sequencesByDocument.values()) {
|
|
499
|
+
const newest = sequences[sequences.length - 1];
|
|
500
|
+
if (newest === void 0 || this.uploaded.has(newest)) continue;
|
|
501
|
+
const write = this.writes.get(newest);
|
|
502
|
+
if (write !== void 0) latest.push(write);
|
|
503
|
+
}
|
|
504
|
+
return latest.sort((a, b) => a.sequence - b.sequence);
|
|
505
|
+
}
|
|
506
|
+
coveredBy(write) {
|
|
507
|
+
const sequences = this.sequencesByDocument.get(write.documentId) ?? [];
|
|
508
|
+
return sequences.filter((sequence) => sequence <= write.sequence && !this.uploaded.has(sequence));
|
|
509
|
+
}
|
|
510
|
+
hasLaterWrite(write) {
|
|
511
|
+
const sequences = this.sequencesByDocument.get(write.documentId) ?? [];
|
|
512
|
+
return (sequences[sequences.length - 1] ?? 0) > write.sequence;
|
|
513
|
+
}
|
|
514
|
+
markUploaded(sequences) {
|
|
515
|
+
for (const sequence of sequences) {
|
|
516
|
+
if (this.writes.has(sequence)) this.uploaded.add(sequence);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
forgetUploads() {
|
|
520
|
+
this.uploaded.clear();
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
|
|
524
|
+
// src/sync/device/session.ts
|
|
525
|
+
var MAX_CHECKPOINT_ID = 4294967295;
|
|
526
|
+
function settingsKey(settings) {
|
|
527
|
+
return JSON.stringify({ bm25: settings.bm25 ?? null, synonyms: settings.synonyms ?? [] });
|
|
528
|
+
}
|
|
529
|
+
function createSession(context, progress, state) {
|
|
530
|
+
const { engine, indexName } = context;
|
|
531
|
+
let feed = null;
|
|
532
|
+
let generation = 0;
|
|
533
|
+
let incoming = Promise.resolve();
|
|
534
|
+
let hold = null;
|
|
535
|
+
let nextCheckpointId = 1;
|
|
536
|
+
let appliedSettings = null;
|
|
537
|
+
const waiters = /* @__PURE__ */ new Map();
|
|
538
|
+
let subscribedOnce = false;
|
|
539
|
+
function sendCheckpoint(onHeads) {
|
|
540
|
+
const id = nextCheckpointId;
|
|
541
|
+
nextCheckpointId = id >= MAX_CHECKPOINT_ID ? 1 : id + 1;
|
|
542
|
+
waiters.set(id, onHeads);
|
|
543
|
+
feed?.send({ type: "checkpoint", id });
|
|
544
|
+
}
|
|
545
|
+
async function applySettings(settings) {
|
|
546
|
+
state.settings = settings;
|
|
547
|
+
const key = settingsKey(settings);
|
|
548
|
+
if (key === appliedSettings) return;
|
|
549
|
+
await context.work.run(async () => {
|
|
550
|
+
await engine.updateBM25(indexName, settings.bm25 ?? {});
|
|
551
|
+
await engine.updateSynonyms(indexName, settings.synonyms ?? []);
|
|
552
|
+
});
|
|
553
|
+
appliedSettings = key;
|
|
554
|
+
}
|
|
555
|
+
async function completeHold() {
|
|
556
|
+
const done = hold;
|
|
557
|
+
if (done === null || done.heads === null || !progress.reached(done.heads)) return;
|
|
558
|
+
hold = null;
|
|
559
|
+
await progress.settleUploaded(done.sequences);
|
|
560
|
+
context.changes.notify();
|
|
561
|
+
startHold();
|
|
562
|
+
}
|
|
563
|
+
function startHold() {
|
|
564
|
+
if (hold !== null || !state.subscribed || feed === null || state.pendingHold.length === 0) return;
|
|
565
|
+
const started = { checkpointId: 0, heads: null, sequences: state.pendingHold };
|
|
566
|
+
state.pendingHold = [];
|
|
567
|
+
hold = started;
|
|
568
|
+
sendCheckpoint((heads) => {
|
|
569
|
+
if (hold !== started || heads === null) return;
|
|
570
|
+
started.heads = heads;
|
|
571
|
+
completeHold().catch(context.report);
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
function refuse(detail) {
|
|
575
|
+
throw new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, `The sync feed broke its protocol: ${detail}`);
|
|
576
|
+
}
|
|
577
|
+
async function handle(message) {
|
|
578
|
+
const position = progress.position();
|
|
579
|
+
if (position === null) refuse("the device holds no copy");
|
|
580
|
+
switch (message.type) {
|
|
581
|
+
case "subscribed": {
|
|
582
|
+
if (message.epoch !== position.epoch || message.partitionCount !== position.seqNos.length) {
|
|
583
|
+
state.needsSnapshot = "required";
|
|
584
|
+
feed?.close("epoch");
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
const behind = message.snapshotBytes > 0 && message.bytesBehind > replayLimit() * message.snapshotBytes;
|
|
588
|
+
if (behind && !state.replayAnyway) {
|
|
589
|
+
state.needsSnapshot = "behind";
|
|
590
|
+
feed?.close("behind");
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
state.replayAnyway = false;
|
|
594
|
+
progress.setFlushThreshold(message.windowBytes);
|
|
595
|
+
await applySettings(message.settings);
|
|
596
|
+
state.subscribed = true;
|
|
597
|
+
subscribedOnce = true;
|
|
598
|
+
context.changes.notify();
|
|
599
|
+
startHold();
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
case "entries":
|
|
603
|
+
if (!state.subscribed) refuse("entries arrived before subscribed");
|
|
604
|
+
if (message.partitionId >= progress.received().length) refuse("entries name a partition that the index lacks");
|
|
605
|
+
await progress.keep(message.partitionId, message.entries);
|
|
606
|
+
await completeHold();
|
|
607
|
+
context.changes.notify();
|
|
608
|
+
return;
|
|
609
|
+
case "settings":
|
|
610
|
+
await applySettings(message.settings);
|
|
611
|
+
return;
|
|
612
|
+
case "checkpoint": {
|
|
613
|
+
if (message.heads.length !== progress.received().length) refuse("heads name another partition count");
|
|
614
|
+
const waiter = waiters.get(message.id);
|
|
615
|
+
waiters.delete(message.id);
|
|
616
|
+
waiter?.(message.heads);
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
case "resync":
|
|
620
|
+
state.needsSnapshot = "required";
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
function replayLimit() {
|
|
625
|
+
return context.options.replayLimitOfSnapshot ?? DEFAULT_REPLAY_LIMIT_OF_SNAPSHOT;
|
|
626
|
+
}
|
|
627
|
+
function closed() {
|
|
628
|
+
feed = null;
|
|
629
|
+
generation += 1;
|
|
630
|
+
state.subscribed = false;
|
|
631
|
+
progress.setAcknowledge(null);
|
|
632
|
+
if (hold !== null) {
|
|
633
|
+
state.pendingHold = [...hold.sequences, ...state.pendingHold];
|
|
634
|
+
hold = null;
|
|
635
|
+
}
|
|
636
|
+
for (const waiter of waiters.values()) waiter(null);
|
|
637
|
+
waiters.clear();
|
|
638
|
+
context.changes.notify();
|
|
639
|
+
}
|
|
640
|
+
return {
|
|
641
|
+
async connect(feedUrl) {
|
|
642
|
+
await incoming;
|
|
643
|
+
const position = progress.position();
|
|
644
|
+
if (position === null || context.stopped()) return { code: 0, subscribed: false };
|
|
645
|
+
let credential;
|
|
646
|
+
try {
|
|
647
|
+
credential = await context.credential();
|
|
648
|
+
} catch (error) {
|
|
649
|
+
context.report(error);
|
|
650
|
+
return { code: 0, subscribed: false };
|
|
651
|
+
}
|
|
652
|
+
if (context.stopped()) return { code: 0, subscribed: false };
|
|
653
|
+
const mine = generation;
|
|
654
|
+
subscribedOnce = false;
|
|
655
|
+
const code = await new Promise((resolve) => {
|
|
656
|
+
try {
|
|
657
|
+
feed = openFeedConnection(feedUrl, credential, {
|
|
658
|
+
onOpen() {
|
|
659
|
+
progress.setAcknowledge((seqNos) => feed?.send({ type: "ack", positions: positionsOf(seqNos) }));
|
|
660
|
+
feed?.send({ type: "subscribe", epoch: position.epoch, positions: positionsOf(progress.received()) });
|
|
661
|
+
},
|
|
662
|
+
onMessage(message) {
|
|
663
|
+
incoming = incoming.then(() => generation === mine ? handle(message) : void 0).catch((error) => {
|
|
664
|
+
context.report(error);
|
|
665
|
+
if (generation === mine) feed?.close("device error");
|
|
666
|
+
});
|
|
667
|
+
},
|
|
668
|
+
onFailure: context.report,
|
|
669
|
+
onClose(code2) {
|
|
670
|
+
closed();
|
|
671
|
+
resolve(code2);
|
|
672
|
+
}
|
|
673
|
+
});
|
|
674
|
+
} catch (error) {
|
|
675
|
+
context.report(error);
|
|
676
|
+
resolve(0);
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
return { code, subscribed: subscribedOnce };
|
|
680
|
+
},
|
|
681
|
+
close(reason) {
|
|
682
|
+
feed?.close(reason);
|
|
683
|
+
},
|
|
684
|
+
startHold,
|
|
685
|
+
requestHeads() {
|
|
686
|
+
if (feed === null || !state.subscribed) return Promise.resolve(null);
|
|
687
|
+
return new Promise((resolve) => sendCheckpoint(resolve));
|
|
688
|
+
},
|
|
689
|
+
holding: () => hold !== null,
|
|
690
|
+
forgetAppliedSettings() {
|
|
691
|
+
appliedSettings = null;
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// src/sync/device/analysis.ts
|
|
697
|
+
function requireAnalysisOnDevice(indexName, analysis) {
|
|
698
|
+
const missing = [];
|
|
699
|
+
if (!hasLanguage(analysis.language)) missing.push(`language "${analysis.language}"`);
|
|
700
|
+
if (analysis.tokenizer !== void 0 && !hasTokenizer(analysis.tokenizer)) {
|
|
701
|
+
missing.push(`tokenizer "${analysis.tokenizer}"`);
|
|
702
|
+
}
|
|
703
|
+
if (analysis.stopWords !== void 0 && !hasStopWords(analysis.stopWords)) {
|
|
704
|
+
missing.push(`stop words "${analysis.stopWords}"`);
|
|
705
|
+
}
|
|
706
|
+
if (missing.length === 0) return;
|
|
707
|
+
throw new NarsilError(
|
|
708
|
+
ClientErrorCodes.SYNC_ANALYSIS_UNAVAILABLE,
|
|
709
|
+
`Index "${indexName}" on the server analyses text with ${missing.join(", ")}, which this device has not registered, so the device keeps its copy. Register them and sync again`,
|
|
710
|
+
{ indexName, missing }
|
|
711
|
+
);
|
|
712
|
+
}
|
|
713
|
+
function analysisIsStale(analysis) {
|
|
714
|
+
if (analysis.analysisRevision === void 0 || !hasLanguage(analysis.language)) return false;
|
|
715
|
+
return getLanguage(analysis.language).revision !== analysis.analysisRevision;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// src/sync/device/archive-facts.ts
|
|
719
|
+
var FactsComplete = class {
|
|
720
|
+
};
|
|
721
|
+
function unusable(detail) {
|
|
722
|
+
return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, `The sync snapshot is unusable: ${detail}`);
|
|
723
|
+
}
|
|
724
|
+
function optionalString(value) {
|
|
725
|
+
return typeof value === "string" ? value : void 0;
|
|
726
|
+
}
|
|
727
|
+
async function readArchiveFacts(source) {
|
|
728
|
+
const { createByteReader, decodeIndexConfiguration, readSnapshotArchive } = await loadStorageCode();
|
|
729
|
+
const reader = createByteReader(source);
|
|
730
|
+
const read = { config: null, seqNos: null };
|
|
731
|
+
try {
|
|
732
|
+
await readSnapshotArchive(reader, {
|
|
733
|
+
async config(bytes) {
|
|
734
|
+
read.config = { ...await decodeIndexConfiguration(bytes) };
|
|
735
|
+
},
|
|
736
|
+
async manifest(manifest) {
|
|
737
|
+
const found = new Array(manifest.checkpoint.length).fill(-1);
|
|
738
|
+
for (const entry of manifest.checkpoint) {
|
|
739
|
+
if (entry.partitionId >= found.length || found[entry.partitionId] !== -1) {
|
|
740
|
+
throw unusable("its manifest names a partition twice or skips one");
|
|
741
|
+
}
|
|
742
|
+
found[entry.partitionId] = entry.lastSeqNo;
|
|
743
|
+
}
|
|
744
|
+
read.seqNos = found;
|
|
745
|
+
throw new FactsComplete();
|
|
746
|
+
},
|
|
747
|
+
async file() {
|
|
748
|
+
throw unusable("a file comes before its manifest");
|
|
749
|
+
}
|
|
750
|
+
});
|
|
751
|
+
} catch (error) {
|
|
752
|
+
if (!(error instanceof FactsComplete)) throw error;
|
|
753
|
+
} finally {
|
|
754
|
+
await reader.close();
|
|
755
|
+
}
|
|
756
|
+
const { config, seqNos } = read;
|
|
757
|
+
if (config === null || seqNos === null) throw unusable("it holds no configuration or no manifest");
|
|
758
|
+
if (!isSyncEpoch(config.syncEpoch)) throw unusable("its configuration holds no sync epoch");
|
|
759
|
+
const language = optionalString(config.language);
|
|
760
|
+
if (language === void 0) throw unusable("its configuration holds no language");
|
|
761
|
+
const tokenizer = optionalString(config.tokenizer);
|
|
762
|
+
const stopWords = optionalString(config.stopWords);
|
|
763
|
+
const analysisRevision = optionalString(config.analysisRevision);
|
|
764
|
+
return {
|
|
765
|
+
epoch: config.syncEpoch,
|
|
766
|
+
seqNos,
|
|
767
|
+
analysis: {
|
|
768
|
+
language,
|
|
769
|
+
...tokenizer !== void 0 ? { tokenizer } : {},
|
|
770
|
+
...stopWords !== void 0 ? { stopWords } : {},
|
|
771
|
+
...analysisRevision !== void 0 ? { analysisRevision } : {}
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
// src/sync/device/snapshot.ts
|
|
777
|
+
var RANGE_NOT_SATISFIABLE = 416;
|
|
778
|
+
async function startDownload(context) {
|
|
779
|
+
const saved = await context.store.readDownload();
|
|
780
|
+
const resume = saved === null ? void 0 : { offset: saved.byteLength, entityTag: saved.entityTag };
|
|
781
|
+
try {
|
|
782
|
+
return await context.client.syncSnapshot(context.indexName, resume, await context.requestOptions());
|
|
783
|
+
} catch (error) {
|
|
784
|
+
if (resume === void 0 || !(error instanceof NarsilError) || error.details.status !== RANGE_NOT_SATISFIABLE) {
|
|
785
|
+
throw error;
|
|
786
|
+
}
|
|
787
|
+
await context.store.clearDownload();
|
|
788
|
+
return context.client.syncSnapshot(context.indexName, void 0, await context.requestOptions());
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
async function mustDropOldCopy(context, archiveBytes) {
|
|
792
|
+
if (!holdsIndex(context.engine, context.indexName)) return false;
|
|
793
|
+
const heap = readHeapStatistics();
|
|
794
|
+
const requiredBytes = archiveBytes * SNAPSHOT_MEMORY_PER_ARCHIVE_BYTE;
|
|
795
|
+
if (heap === null || heap.usedBytes + requiredBytes <= heap.limitBytes) return false;
|
|
796
|
+
if (context.appChoseLoad) return true;
|
|
797
|
+
const choice = await context.chooseSnapshotLoad({ archiveBytes, requiredBytes, heapLimitBytes: heap.limitBytes });
|
|
798
|
+
return choice === "keep" ? "keep" : true;
|
|
799
|
+
}
|
|
800
|
+
async function saveDownload(context, download) {
|
|
801
|
+
const reader = download.stream.getReader();
|
|
802
|
+
let offset = download.offset;
|
|
803
|
+
let pending = [];
|
|
804
|
+
let pendingBytes = 0;
|
|
805
|
+
async function flush() {
|
|
806
|
+
if (pendingBytes === 0) return;
|
|
807
|
+
const batch = new Uint8Array(pendingBytes);
|
|
808
|
+
let at = 0;
|
|
809
|
+
for (const part of pending) {
|
|
810
|
+
batch.set(part, at);
|
|
811
|
+
at += part.byteLength;
|
|
812
|
+
}
|
|
813
|
+
await context.store.appendDownload(download.entityTag, offset, batch);
|
|
814
|
+
offset += pendingBytes;
|
|
815
|
+
pending = [];
|
|
816
|
+
pendingBytes = 0;
|
|
817
|
+
}
|
|
818
|
+
try {
|
|
819
|
+
if (download.offset === 0) await context.store.appendDownload(download.entityTag, 0, new Uint8Array(0));
|
|
820
|
+
for (; ; ) {
|
|
821
|
+
const { done, value } = await reader.read();
|
|
822
|
+
if (done) break;
|
|
823
|
+
if (offset + pendingBytes + value.byteLength > download.totalBytes) {
|
|
824
|
+
throw new NarsilError(
|
|
825
|
+
ClientErrorCodes.CLIENT_INVALID_RESPONSE,
|
|
826
|
+
"The server sent more bytes than the snapshot holds"
|
|
827
|
+
);
|
|
828
|
+
}
|
|
829
|
+
pending.push(value);
|
|
830
|
+
pendingBytes += value.byteLength;
|
|
831
|
+
if (pendingBytes >= DOWNLOAD_SAVE_BYTES) await flush();
|
|
832
|
+
}
|
|
833
|
+
await flush();
|
|
834
|
+
} catch (error) {
|
|
835
|
+
await flush().catch(() => void 0);
|
|
836
|
+
await reader.cancel().catch(() => void 0);
|
|
837
|
+
throw error;
|
|
838
|
+
} finally {
|
|
839
|
+
reader.releaseLock();
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
function completeDownload(saved, entityTag, totalBytes) {
|
|
843
|
+
if (saved === null || saved.entityTag !== entityTag || saved.byteLength !== totalBytes) {
|
|
844
|
+
throw new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, "The snapshot download ended early", {
|
|
845
|
+
saved: saved?.byteLength ?? 0,
|
|
846
|
+
totalBytes
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
return saved;
|
|
850
|
+
}
|
|
851
|
+
async function downloadPublishedSnapshot(context) {
|
|
852
|
+
if (context.settings !== null) requireAnalysisOnDevice(context.indexName, context.settings);
|
|
853
|
+
const download = await startDownload(context);
|
|
854
|
+
const dropOldCopy = await mustDropOldCopy(context, download.totalBytes);
|
|
855
|
+
if (dropOldCopy === "keep") {
|
|
856
|
+
await download.stream.cancel().catch(() => void 0);
|
|
857
|
+
return { kind: "kept" };
|
|
858
|
+
}
|
|
859
|
+
await saveDownload(context, download);
|
|
860
|
+
const saved = completeDownload(await context.store.readDownload(), download.entityTag, download.totalBytes);
|
|
861
|
+
const facts = await readArchiveFacts(saved.chunks());
|
|
862
|
+
requireAnalysisOnDevice(context.indexName, facts.analysis);
|
|
863
|
+
return { kind: "downloaded", saved, facts, dropOldCopy };
|
|
864
|
+
}
|
|
865
|
+
async function restoreDownloadedSnapshot(context, downloaded) {
|
|
866
|
+
await context.store.clearKeptEntries();
|
|
867
|
+
if (downloaded.dropOldCopy && holdsIndex(context.engine, context.indexName)) {
|
|
868
|
+
await context.engine.dropIndex(context.indexName);
|
|
869
|
+
}
|
|
870
|
+
await context.engine.restore(context.indexName, downloaded.saved.chunks());
|
|
871
|
+
if (analysisIsStale(downloaded.facts.analysis)) await context.engine.rebuildAnalysis(context.indexName);
|
|
872
|
+
return { epoch: downloaded.facts.epoch, seqNos: downloaded.facts.seqNos };
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// src/sync/device/upload.ts
|
|
876
|
+
var REFUSED_STATUSES = /* @__PURE__ */ new Set([400, 413, 422]);
|
|
877
|
+
function refusedBy(error) {
|
|
878
|
+
if (!(error instanceof NarsilError)) return false;
|
|
879
|
+
const status = error.details.status;
|
|
880
|
+
return typeof status === "number" && REFUSED_STATUSES.has(status);
|
|
881
|
+
}
|
|
882
|
+
async function sendOne(client, indexName, write, options) {
|
|
883
|
+
if (write.document !== null) {
|
|
884
|
+
await client.put(indexName, write.documentId, write.document, void 0, options);
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
try {
|
|
888
|
+
await client.remove(indexName, write.documentId, void 0, options);
|
|
889
|
+
} catch (error) {
|
|
890
|
+
if (!(error instanceof NarsilError) || error.code !== ErrorCodes.DOC_NOT_FOUND) throw error;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
async function sendThroughServer(client, indexName, writes, options) {
|
|
894
|
+
const outcomes = [];
|
|
895
|
+
for (const write of writes) {
|
|
896
|
+
try {
|
|
897
|
+
await sendOne(client, indexName, write, await options());
|
|
898
|
+
outcomes.push({ accepted: true });
|
|
899
|
+
} catch (error) {
|
|
900
|
+
if (!refusedBy(error)) return { outcomes, failure: error };
|
|
901
|
+
outcomes.push({ accepted: false, error });
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
return { outcomes, failure: null };
|
|
905
|
+
}
|
|
906
|
+
async function sendThroughBackend(upload, writes) {
|
|
907
|
+
let outcomes;
|
|
908
|
+
try {
|
|
909
|
+
outcomes = await upload(writes);
|
|
910
|
+
} catch (error) {
|
|
911
|
+
return { outcomes: [], failure: error };
|
|
912
|
+
}
|
|
913
|
+
if (!Array.isArray(outcomes) || outcomes.length !== writes.length) {
|
|
914
|
+
return {
|
|
915
|
+
outcomes: [],
|
|
916
|
+
failure: new NarsilError(
|
|
917
|
+
ClientErrorCodes.CLIENT_INVALID_RESPONSE,
|
|
918
|
+
`The upload function returned ${Array.isArray(outcomes) ? outcomes.length : "no"} outcomes for ${writes.length} writes`
|
|
919
|
+
)
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
for (const outcome of outcomes) {
|
|
923
|
+
const valid = outcome !== null && typeof outcome === "object" && (outcome.accepted === true || outcome.accepted === false && outcome.error instanceof NarsilError);
|
|
924
|
+
if (!valid) {
|
|
925
|
+
return {
|
|
926
|
+
outcomes: [],
|
|
927
|
+
failure: new NarsilError(
|
|
928
|
+
ClientErrorCodes.CLIENT_INVALID_RESPONSE,
|
|
929
|
+
"Each outcome of the upload function must be { accepted: true } or { accepted: false, error }"
|
|
930
|
+
)
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
return { outcomes, failure: null };
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
// src/sync/device/uploader.ts
|
|
938
|
+
function createUploader(context, progress, session, state) {
|
|
939
|
+
const { engine, indexName, store, queue, work, client } = context;
|
|
940
|
+
let running = null;
|
|
941
|
+
let again = false;
|
|
942
|
+
async function serverCopyOf(write, outcome) {
|
|
943
|
+
if (outcome.serverDocument !== void 0) return outcome.serverDocument;
|
|
944
|
+
return await client.get(indexName, write.documentId, await context.requestOptions()) ?? null;
|
|
945
|
+
}
|
|
946
|
+
async function refuse(write, outcome) {
|
|
947
|
+
const serverDocument = await serverCopyOf(write, outcome);
|
|
948
|
+
await work.run(async () => {
|
|
949
|
+
const covered = queue.coveredBy(write);
|
|
950
|
+
if (!queue.hasLaterWrite(write)) {
|
|
951
|
+
if (serverDocument === null) await removeLocally(engine, indexName, write.documentId);
|
|
952
|
+
else await putLocally(engine, indexName, write.documentId, serverDocument);
|
|
953
|
+
}
|
|
954
|
+
await store.removeWrites(covered);
|
|
955
|
+
queue.remove(covered);
|
|
956
|
+
progress.forgetDeferred(write.documentId);
|
|
957
|
+
});
|
|
958
|
+
try {
|
|
959
|
+
context.options.onRefusedWrite?.({ write, error: outcome.error, serverDocument });
|
|
960
|
+
} catch (error) {
|
|
961
|
+
context.report(error);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
async function settle(batch, result) {
|
|
965
|
+
let failure = result.failure;
|
|
966
|
+
for (let at = 0; at < result.outcomes.length; at++) {
|
|
967
|
+
const write = batch[at];
|
|
968
|
+
const outcome = result.outcomes[at];
|
|
969
|
+
if (write === void 0 || outcome === void 0) break;
|
|
970
|
+
if (outcome.accepted) {
|
|
971
|
+
const covered = queue.coveredBy(write);
|
|
972
|
+
queue.markUploaded(covered);
|
|
973
|
+
state.pendingHold.push(...covered);
|
|
974
|
+
progress.forgetDeferred(write.documentId);
|
|
975
|
+
continue;
|
|
976
|
+
}
|
|
977
|
+
try {
|
|
978
|
+
await refuse(write, outcome);
|
|
979
|
+
} catch (error) {
|
|
980
|
+
failure ??= error;
|
|
981
|
+
break;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
return failure;
|
|
985
|
+
}
|
|
986
|
+
async function drain() {
|
|
987
|
+
let failures = 0;
|
|
988
|
+
while (!context.stopped()) {
|
|
989
|
+
again = false;
|
|
990
|
+
const batch = queue.latestAwaitingUpload().slice(0, UPLOAD_BATCH_WRITES);
|
|
991
|
+
if (batch.length === 0) return;
|
|
992
|
+
const upload = context.options.upload;
|
|
993
|
+
const result = upload === void 0 ? await sendThroughServer(client, indexName, batch, context.requestOptions) : await sendThroughBackend(upload, batch);
|
|
994
|
+
const failure = await settle(batch, result);
|
|
995
|
+
session.startHold();
|
|
996
|
+
context.changes.notify();
|
|
997
|
+
if (failure === null || failure === void 0) {
|
|
998
|
+
failures = 0;
|
|
999
|
+
continue;
|
|
1000
|
+
}
|
|
1001
|
+
context.report(failure);
|
|
1002
|
+
await context.pause(backoffDelayMs(failures, UPLOAD_RETRY_DELAY_MS, RETRY_MAX_DELAY_MS));
|
|
1003
|
+
failures += 1;
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
function kick() {
|
|
1007
|
+
if (context.stopped()) return;
|
|
1008
|
+
if (running !== null) {
|
|
1009
|
+
again = true;
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
running = drain().catch(context.report).finally(() => {
|
|
1013
|
+
running = null;
|
|
1014
|
+
if (again) kick();
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
return {
|
|
1018
|
+
kick,
|
|
1019
|
+
idle: () => running ?? Promise.resolve()
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
// src/sync/device/index.ts
|
|
1024
|
+
function invalid(message) {
|
|
1025
|
+
return new NarsilError(ErrorCodes.CONFIG_INVALID, message);
|
|
1026
|
+
}
|
|
1027
|
+
function validated(options) {
|
|
1028
|
+
if (typeof options.indexName !== "string" || options.indexName.length === 0) {
|
|
1029
|
+
throw invalid("Device sync needs an index name");
|
|
1030
|
+
}
|
|
1031
|
+
const limit = options.replayLimitOfSnapshot;
|
|
1032
|
+
if (limit !== void 0 && (!Number.isFinite(limit) || limit < 0)) {
|
|
1033
|
+
throw invalid("replayLimitOfSnapshot must be a finite number at or above 0");
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
function requireDocumentObject(document) {
|
|
1037
|
+
if (document === null || typeof document !== "object" || Array.isArray(document)) {
|
|
1038
|
+
throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, "A document must be an object");
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
function requireDocumentId(documentId) {
|
|
1042
|
+
if (typeof documentId !== "string" || documentId.length === 0) {
|
|
1043
|
+
throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, "A document ID must be a non-empty string");
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
function createDeviceSync(options) {
|
|
1047
|
+
validated(options);
|
|
1048
|
+
const { engine, indexName, store } = options;
|
|
1049
|
+
const feedUrl = feedUrlOf(options.url, indexName);
|
|
1050
|
+
const client = createNarsilClient({ url: options.url });
|
|
1051
|
+
const changes = createChanges();
|
|
1052
|
+
let stopped = true;
|
|
1053
|
+
let recovered = null;
|
|
1054
|
+
let loop = null;
|
|
1055
|
+
let appChoseLoad = false;
|
|
1056
|
+
let snapshotDeferred = false;
|
|
1057
|
+
let aborts = new AbortController();
|
|
1058
|
+
async function credential() {
|
|
1059
|
+
return options.credential === void 0 ? null : await options.credential();
|
|
1060
|
+
}
|
|
1061
|
+
const context = {
|
|
1062
|
+
options,
|
|
1063
|
+
engine,
|
|
1064
|
+
indexName,
|
|
1065
|
+
store,
|
|
1066
|
+
client,
|
|
1067
|
+
work: createSerial(),
|
|
1068
|
+
queue: new QueueMirror(),
|
|
1069
|
+
changes,
|
|
1070
|
+
report(error) {
|
|
1071
|
+
try {
|
|
1072
|
+
options.onError?.(error);
|
|
1073
|
+
} catch {
|
|
1074
|
+
}
|
|
1075
|
+
},
|
|
1076
|
+
credential,
|
|
1077
|
+
async requestOptions() {
|
|
1078
|
+
const value = await credential();
|
|
1079
|
+
const signal = aborts.signal;
|
|
1080
|
+
return value === null ? { signal } : { signal, headers: { authorization: `Bearer ${value}` } };
|
|
1081
|
+
},
|
|
1082
|
+
makeDurable: options.makeDurable ?? (() => engine.checkpoint(indexName)),
|
|
1083
|
+
stopped: () => stopped,
|
|
1084
|
+
pause(delayMs) {
|
|
1085
|
+
return new Promise((resolve) => {
|
|
1086
|
+
if (stopped) {
|
|
1087
|
+
resolve();
|
|
1088
|
+
return;
|
|
1089
|
+
}
|
|
1090
|
+
let done = false;
|
|
1091
|
+
const finish = () => {
|
|
1092
|
+
if (done) return;
|
|
1093
|
+
done = true;
|
|
1094
|
+
clearTimeout(timer);
|
|
1095
|
+
resolve();
|
|
1096
|
+
};
|
|
1097
|
+
const timer = setTimeout(finish, delayMs);
|
|
1098
|
+
const watch = () => {
|
|
1099
|
+
if (done) return;
|
|
1100
|
+
if (stopped) finish();
|
|
1101
|
+
else changes.next().then(watch);
|
|
1102
|
+
};
|
|
1103
|
+
changes.next().then(watch);
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1106
|
+
};
|
|
1107
|
+
const progress = createProgress(context);
|
|
1108
|
+
const state = {
|
|
1109
|
+
subscribed: false,
|
|
1110
|
+
needsSnapshot: null,
|
|
1111
|
+
replayAnyway: false,
|
|
1112
|
+
pendingHold: [],
|
|
1113
|
+
settings: null
|
|
1114
|
+
};
|
|
1115
|
+
const session = createSession(context, progress, state);
|
|
1116
|
+
const uploader = createUploader(context, progress, session, state);
|
|
1117
|
+
async function loadSnapshot() {
|
|
1118
|
+
const snapshotContext = {
|
|
1119
|
+
engine,
|
|
1120
|
+
indexName,
|
|
1121
|
+
client,
|
|
1122
|
+
store,
|
|
1123
|
+
requestOptions: context.requestOptions,
|
|
1124
|
+
chooseSnapshotLoad: options.chooseSnapshotLoad ?? (async () => "load"),
|
|
1125
|
+
settings: state.settings,
|
|
1126
|
+
appChoseLoad
|
|
1127
|
+
};
|
|
1128
|
+
const downloaded = await downloadPublishedSnapshot(snapshotContext);
|
|
1129
|
+
if (downloaded.kind === "kept") return "kept";
|
|
1130
|
+
await context.work.run(async () => {
|
|
1131
|
+
const position = await restoreDownloadedSnapshot(snapshotContext, downloaded);
|
|
1132
|
+
session.forgetAppliedSettings();
|
|
1133
|
+
await progress.adopt(position);
|
|
1134
|
+
});
|
|
1135
|
+
return "loaded";
|
|
1136
|
+
}
|
|
1137
|
+
async function snapshotStep(attempt) {
|
|
1138
|
+
if (snapshotDeferred && !appChoseLoad) {
|
|
1139
|
+
await changes.next();
|
|
1140
|
+
return attempt;
|
|
1141
|
+
}
|
|
1142
|
+
try {
|
|
1143
|
+
const outcome = await loadSnapshot();
|
|
1144
|
+
if (outcome === "loaded") {
|
|
1145
|
+
state.needsSnapshot = null;
|
|
1146
|
+
appChoseLoad = false;
|
|
1147
|
+
snapshotDeferred = false;
|
|
1148
|
+
} else if (state.needsSnapshot === "behind" && progress.position() !== null) {
|
|
1149
|
+
state.needsSnapshot = null;
|
|
1150
|
+
state.replayAnyway = true;
|
|
1151
|
+
} else {
|
|
1152
|
+
snapshotDeferred = true;
|
|
1153
|
+
}
|
|
1154
|
+
changes.notify();
|
|
1155
|
+
return 0;
|
|
1156
|
+
} catch (error) {
|
|
1157
|
+
context.report(error);
|
|
1158
|
+
await context.pause(backoffDelayMs(attempt, SNAPSHOT_RETRY_DELAY_MS, RETRY_MAX_DELAY_MS));
|
|
1159
|
+
return attempt + 1;
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
async function run() {
|
|
1163
|
+
let attempt = 0;
|
|
1164
|
+
while (!stopped) {
|
|
1165
|
+
if (progress.position() === null || state.needsSnapshot !== null) {
|
|
1166
|
+
attempt = await snapshotStep(attempt);
|
|
1167
|
+
continue;
|
|
1168
|
+
}
|
|
1169
|
+
const { code, subscribed } = await session.connect(feedUrl);
|
|
1170
|
+
if (stopped) return;
|
|
1171
|
+
if (subscribed) attempt = 0;
|
|
1172
|
+
if (code === SYNC_CLOSE_RESYNC) state.needsSnapshot ??= "required";
|
|
1173
|
+
if (code === SYNC_CLOSE_CREDENTIAL_EXPIRED || state.needsSnapshot !== null) continue;
|
|
1174
|
+
await context.pause(backoffDelayMs(attempt, RECONNECT_MIN_DELAY_MS, RETRY_MAX_DELAY_MS));
|
|
1175
|
+
attempt += 1;
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
async function waitUntil(condition) {
|
|
1179
|
+
while (!condition()) {
|
|
1180
|
+
if (stopped) throw invalid("Device sync stopped before it settled");
|
|
1181
|
+
await changes.next();
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
function requireRecovered() {
|
|
1185
|
+
if (recovered === null) throw invalid("Call start before writing through device sync");
|
|
1186
|
+
return recovered;
|
|
1187
|
+
}
|
|
1188
|
+
async function write(documentId, document) {
|
|
1189
|
+
requireDocumentId(documentId);
|
|
1190
|
+
if (document !== null) requireDocumentObject(document);
|
|
1191
|
+
if (loadedMessagePack() === null) await loadMessagePack();
|
|
1192
|
+
await requireRecovered();
|
|
1193
|
+
const copy = document === null ? null : portableCopyOf(engine, indexName, document);
|
|
1194
|
+
await context.work.run(async () => {
|
|
1195
|
+
const queued = await store.appendWrite({ documentId, document: copy });
|
|
1196
|
+
context.queue.add(queued);
|
|
1197
|
+
try {
|
|
1198
|
+
if (copy === null) await removeLocally(engine, indexName, documentId);
|
|
1199
|
+
else await putLocally(engine, indexName, documentId, copy);
|
|
1200
|
+
} catch (error) {
|
|
1201
|
+
await store.removeWrites([queued.sequence]);
|
|
1202
|
+
context.queue.remove([queued.sequence]);
|
|
1203
|
+
throw error;
|
|
1204
|
+
}
|
|
1205
|
+
});
|
|
1206
|
+
changes.notify();
|
|
1207
|
+
uploader.kick();
|
|
1208
|
+
}
|
|
1209
|
+
function quiet() {
|
|
1210
|
+
return state.subscribed && context.queue.size === 0 && state.pendingHold.length === 0 && !session.holding() && state.needsSnapshot === null;
|
|
1211
|
+
}
|
|
1212
|
+
return {
|
|
1213
|
+
put: (documentId, document) => write(documentId, document),
|
|
1214
|
+
remove: (documentId) => write(documentId, null),
|
|
1215
|
+
async start() {
|
|
1216
|
+
if (!stopped) return;
|
|
1217
|
+
stopped = false;
|
|
1218
|
+
aborts = new AbortController();
|
|
1219
|
+
recovered ??= loadMessagePack().then(() => progress.recover());
|
|
1220
|
+
try {
|
|
1221
|
+
await recovered;
|
|
1222
|
+
} catch (error) {
|
|
1223
|
+
recovered = null;
|
|
1224
|
+
stopped = true;
|
|
1225
|
+
throw error;
|
|
1226
|
+
}
|
|
1227
|
+
loop = run().catch(context.report);
|
|
1228
|
+
uploader.kick();
|
|
1229
|
+
},
|
|
1230
|
+
async stop() {
|
|
1231
|
+
if (stopped) return;
|
|
1232
|
+
stopped = true;
|
|
1233
|
+
aborts.abort();
|
|
1234
|
+
session.close("stopped");
|
|
1235
|
+
progress.cancelFlush();
|
|
1236
|
+
changes.notify();
|
|
1237
|
+
await Promise.allSettled([loop, uploader.idle()]);
|
|
1238
|
+
loop = null;
|
|
1239
|
+
if (progress.unsavedBytes() > 0) await progress.save().catch(context.report);
|
|
1240
|
+
},
|
|
1241
|
+
async loadSnapshot() {
|
|
1242
|
+
appChoseLoad = true;
|
|
1243
|
+
state.needsSnapshot ??= "required";
|
|
1244
|
+
session.close("snapshot");
|
|
1245
|
+
changes.notify();
|
|
1246
|
+
await waitUntil(() => !appChoseLoad && state.needsSnapshot === null);
|
|
1247
|
+
},
|
|
1248
|
+
async settled() {
|
|
1249
|
+
for (; ; ) {
|
|
1250
|
+
await waitUntil(quiet);
|
|
1251
|
+
const heads = await session.requestHeads();
|
|
1252
|
+
if (heads === null) continue;
|
|
1253
|
+
await waitUntil(() => progress.reached(heads) || !state.subscribed);
|
|
1254
|
+
if (!progress.reached(heads)) continue;
|
|
1255
|
+
await progress.save();
|
|
1256
|
+
if (quiet()) return;
|
|
1257
|
+
}
|
|
1258
|
+
},
|
|
1259
|
+
status() {
|
|
1260
|
+
const position = progress.position();
|
|
1261
|
+
return {
|
|
1262
|
+
connection: stopped ? "stopped" : state.subscribed ? "connected" : "connecting",
|
|
1263
|
+
queuedWrites: context.queue.size,
|
|
1264
|
+
position: position === null ? null : { epoch: position.epoch, seqNos: [...position.seqNos] },
|
|
1265
|
+
snapshotDeferred
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
};
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
export { createDeviceSync };
|