@delali/narsil 0.3.1-next.185 → 0.3.1-next.193
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/adapters/filesystem.mjs +2 -2
- package/dist/adapters/gcs.mjs +2 -2
- package/dist/adapters/indexeddb.mjs +1 -1
- package/dist/adapters/s3.mjs +2 -2
- package/dist/browser/chunk-4E5APHTM.mjs +1459 -0
- package/dist/browser/{chunk-4ZICWS4V.mjs → chunk-QAOVAYAG.mjs} +1 -1
- package/dist/browser/{chunk-CPSHS46N.mjs → chunk-UQG4HUGQ.mjs} +82 -50
- package/dist/browser/{chunk-2Q45HI5H.mjs → chunk-YINZFPLT.mjs} +7 -0
- package/dist/browser/{module-XVLHJ7T7.mjs → module-ZZTNG5JZ.mjs} +26 -3
- package/dist/browser/{optional-writes-ZTBHYQMN.mjs → optional-writes-J5BHKEQL.mjs} +21 -8
- package/dist/browser/{storage-leaf-4XMCITJ6.mjs → storage-leaf-N3JMQ6CF.mjs} +14569 -13747
- package/dist/{capabilities-DAHXphRn.d.ts → capabilities-CLnQHIqV.d.ts} +51 -9
- package/dist/{chunk-KLVMK3R5.mjs → chunk-2CKDCC65.mjs} +1 -1
- package/dist/{chunk-CODGHJVW.mjs → chunk-2HVQYOOD.mjs} +40 -12
- package/dist/{chunk-M5I64AQW.mjs → chunk-3N5GUXBI.mjs} +33 -33
- package/dist/{chunk-DU23J5JG.mjs → chunk-5CIHQIA4.mjs} +6 -6
- package/dist/{react-native/chunks/chunk-ZUFOYZ5P.mjs → chunk-62X472AC.mjs} +1 -1
- package/dist/{chunk-QJGDB3WR.mjs → chunk-6GHX35ZM.mjs} +2 -2
- package/dist/{chunk-G3XRHAJA.mjs → chunk-6MC6BCXK.mjs} +3 -3
- package/dist/{chunk-X5NHWOXT.mjs → chunk-7P5YWDRQ.mjs} +2 -2
- package/dist/{chunk-MHBG33R5.mjs → chunk-7X4IIREW.mjs} +76 -46
- package/dist/{chunk-F4OOBPKA.mjs → chunk-AOYQTX6W.mjs} +1 -1
- package/dist/{chunk-D6FYUTFN.mjs → chunk-ASZLNVWW.mjs} +1 -1
- package/dist/{chunk-LR6JJVML.mjs → chunk-D2EJUM4A.mjs} +764 -699
- package/dist/{chunk-FETZYCH7.mjs → chunk-F6F4G322.mjs} +26 -1
- package/dist/{chunk-2YWWHMVC.mjs → chunk-FGOGT273.mjs} +2 -2
- package/dist/{chunk-5BUEBIXR.mjs → chunk-GLXLSPLO.mjs} +18 -5
- package/dist/{chunk-IE3Q62QD.mjs → chunk-GR5GMFJJ.mjs} +1 -1
- package/dist/{chunk-Y7QUFIGV.mjs → chunk-GXED6R3Q.mjs} +2 -2
- package/dist/{chunk-BVZWWMOU.mjs → chunk-HHYQMGPW.mjs} +2 -2
- package/dist/{chunk-SL53DHVE.mjs → chunk-HSA3H3VW.mjs} +121 -68
- package/dist/{chunk-YM2HI5N5.mjs → chunk-IL5DRCNU.mjs} +2 -2
- package/dist/{chunk-JDG6CCXM.mjs → chunk-JENWA4WX.mjs} +2 -2
- package/dist/{chunk-2GONI5HL.mjs → chunk-JXINPXRS.mjs} +5 -4
- package/dist/chunk-K5GNK5GE.mjs +602 -0
- package/dist/{chunk-WLFOLAQI.mjs → chunk-KBU3ZFJN.mjs} +2 -2
- package/dist/{chunk-EDEE7ETQ.mjs → chunk-KOA6T2IC.mjs} +3 -3
- package/dist/{chunk-N3M4NPML.mjs → chunk-M2Z5WVWW.mjs} +1 -1
- package/dist/{chunk-EWGIF6FQ.mjs → chunk-NJJN4FMM.mjs} +1 -1
- package/dist/{chunk-C7YQHCSO.mjs → chunk-P4F4UWLC.mjs} +1 -1
- package/dist/{chunk-VYSWVQCE.mjs → chunk-PAPQGC6I.mjs} +2 -2
- package/dist/{chunk-I2WJCRIH.mjs → chunk-R2PJKSNL.mjs} +1 -1
- package/dist/{chunk-6SZQETSC.mjs → chunk-RORNUNXW.mjs} +5 -2
- package/dist/{chunk-FBQYC5OX.mjs → chunk-SB5HCIIO.mjs} +16 -13
- package/dist/{chunk-CYCVPTTR.mjs → chunk-SCZI7COG.mjs} +2 -2
- package/dist/{chunk-O337MDRJ.mjs → chunk-SHES4VP5.mjs} +38 -13
- package/dist/{chunk-H4QYQ5DE.mjs → chunk-SIIDIGKI.mjs} +2 -2
- package/dist/{chunk-A3OAFUZI.mjs → chunk-SM2SYVIX.mjs} +2 -2
- package/dist/{chunk-II6XF6M4.mjs → chunk-SMLUZGSI.mjs} +9 -8
- package/dist/{chunk-2KCCQK4R.mjs → chunk-TSE2VWPX.mjs} +2 -2
- package/dist/{chunk-WSIU6YXQ.mjs → chunk-YPEJ6ODL.mjs} +1732 -743
- package/dist/{chunk-7WXGUTWK.mjs → chunk-Z6SIJH5O.mjs} +17 -6
- package/dist/{chunk-Q3ZZH5Z7.mjs → chunk-ZKOQR7EA.mjs} +7 -0
- package/dist/{chunk-CKEOTSF5.mjs → chunk-ZS2YUB2L.mjs} +6 -3
- package/dist/{chunk-FCQMZDRI.mjs → chunk-ZVZYJIIP.mjs} +3 -3
- package/dist/{client-DpqtT2XG.d.ts → client-uB0kp2yT.d.ts} +88 -5
- package/dist/client.d.ts +7 -7
- package/dist/client.mjs +3 -1265
- package/dist/{config-D9WR3MhO.d.ts → config-BvHQ-C7U.d.ts} +3 -3
- package/dist/{config-C687-reW.d.ts → config-PqgNY1H7.d.ts} +1 -1
- package/dist/{core-leaf-NEUM7PU3.mjs → core-leaf-TAR3GJVE.mjs} +3 -3
- package/dist/{direct-executor-37IH3ISS.mjs → direct-executor-WSZALYCV.mjs} +8 -8
- package/dist/distribution/coordinator/etcd.d.ts +1 -1
- package/dist/distribution/coordinator/etcd.mjs +35 -15
- package/dist/distribution/coordinator/in-memory.d.ts +1 -1
- package/dist/distribution/coordinator/in-memory.mjs +16 -6
- package/dist/distribution/transport/grpc.mjs +2 -2
- package/dist/distribution/transport/tcp.mjs +2 -2
- package/dist/distribution.d.ts +70 -9
- package/dist/distribution.mjs +1695 -680
- package/dist/{durability-config-NWU2G2SJ.mjs → durability-config-VWS2K24E.mjs} +1 -1
- package/dist/{react-native/chunks/embed-4LBSQD7U.mjs → embed-4PHHKGBK.mjs} +1 -1
- package/dist/embeddings/openai.mjs +2 -2
- package/dist/{engine-De6JzTW8.d.ts → engine-qjAxJSKk.d.ts} +206 -7
- package/dist/{index-CBUl4z3E.d.ts → index-BaQIAnk0.d.ts} +137 -111
- package/dist/index.browser.mjs +3 -3
- package/dist/index.d.ts +4 -4
- package/dist/index.mjs +8 -8
- package/dist/index.react-native.mjs +9 -9
- package/dist/{long-query-thread-DIPJBMV7.mjs → long-query-thread-N2DNPEA3.mjs} +21 -21
- package/dist/{module-Z3CL4GJ4.mjs → module-UQFIJQMG.mjs} +33 -13
- package/dist/{msgpack-leaf-7KBYOVTF.mjs → msgpack-leaf-H5ZYW7S7.mjs} +4 -4
- package/dist/{optional-reads-ENKMVQ4Q.mjs → optional-reads-5HFMF2QZ.mjs} +2 -2
- package/dist/{optional-writes-YIVJMQ4D.mjs → optional-writes-CGDBJBHQ.mjs} +31 -18
- package/dist/{paged-partition-archive-IZ6U7DVN.mjs → paged-partition-archive-UK44CVXG.mjs} +20 -20
- package/dist/partial-words.browser.mjs +3 -3
- package/dist/partial-words.mjs +8 -8
- package/dist/pattern.browser.mjs +2 -2
- package/dist/pattern.mjs +3 -3
- package/dist/persistence/durability/checkpoint-worker.d.ts +7 -0
- package/dist/persistence/durability/checkpoint-worker.mjs +18 -18
- package/dist/{phrase-module-ZY3TLTQA.mjs → phrase-module-EFESWOCR.mjs} +2 -2
- package/dist/{query-actions-7EOKT3WD.mjs → query-actions-7BL3YZDW.mjs} +8 -8
- package/dist/query-suggestions/merge-worker.mjs +11 -11
- package/dist/query-suggestions.browser.mjs +3 -3
- package/dist/query-suggestions.d.ts +1 -1
- package/dist/query-suggestions.mjs +9 -9
- package/dist/react-native/chunks/{chunk-TQ26BKBH.mjs → chunk-43R4GCM7.mjs} +7 -0
- package/dist/react-native/chunks/{chunk-CBU4M3EU.mjs → chunk-63DZ26X5.mjs} +277 -75
- package/dist/react-native/chunks/{chunk-BN6FUB7N.mjs → chunk-AN4HKLB4.mjs} +1 -1
- package/dist/react-native/chunks/{chunk-T5HF3FW3.mjs → chunk-BNFGA7TY.mjs} +3 -3
- package/dist/react-native/chunks/{chunk-PVMEHBQO.mjs → chunk-CW2ZFXSE.mjs} +1 -1
- package/dist/react-native/chunks/{chunk-NLEFMPY3.mjs → chunk-G434H5WG.mjs} +15 -4
- package/dist/react-native/chunks/{chunk-SK3DPAI4.mjs → chunk-HTBY4GON.mjs} +2 -2
- package/dist/react-native/chunks/{chunk-ZQPSXN6C.mjs → chunk-IP3QLETQ.mjs} +5 -5
- package/dist/react-native/chunks/{chunk-7TPO4FLE.mjs → chunk-LOKGE4OQ.mjs} +3 -3
- package/dist/react-native/chunks/{chunk-ZDDJVR7X.mjs → chunk-MRH36RN3.mjs} +2 -2
- package/dist/react-native/chunks/{chunk-YOTP2FPE.mjs → chunk-UO44MA5M.mjs} +1 -1
- package/dist/{chunk-4JBTALUC.mjs → react-native/chunks/chunk-UYAZLGEM.mjs} +1 -1
- package/dist/react-native/chunks/{chunk-WFQ6YQZA.mjs → chunk-UYKIV2KT.mjs} +1 -1
- package/dist/react-native/chunks/{chunk-IEY4DARA.mjs → chunk-XHYOQ4K4.mjs} +2 -2
- package/dist/react-native/chunks/{chunk-OZ3XEM6D.mjs → chunk-ZUJCUPVS.mjs} +1 -1
- package/dist/react-native/chunks/{core-leaf-TPBRRWVN.mjs → core-leaf-5PVST5PR.mjs} +4 -4
- package/dist/react-native/chunks/{durability-config-RVTWSXPH.mjs → durability-config-FCSD4MXY.mjs} +1 -1
- package/dist/{embed-2QMYCD2Q.mjs → react-native/chunks/embed-HA2I7RCF.mjs} +1 -1
- package/dist/react-native/chunks/{gzip.react-native-DLO4WW27.mjs → gzip.react-native-CVM4NQG2.mjs} +2 -2
- package/dist/react-native/chunks/{module-COLNJNML.mjs → module-QRCYB7QV.mjs} +32 -9
- package/dist/react-native/chunks/msgpack-leaf-JMNGSTRW.mjs +7 -0
- package/dist/react-native/chunks/{optional-reads-74HYI4DO.mjs → optional-reads-AFPO2FEB.mjs} +3 -3
- package/dist/react-native/chunks/{optional-writes-QETIM6BI.mjs → optional-writes-GC6JWMAF.mjs} +31 -18
- package/dist/react-native/chunks/{phrase-module-UC4YGEBB.mjs → phrase-module-IGYBK7XH.mjs} +3 -3
- package/dist/react-native/chunks/{resolve-vector-text-FU7D4QP5.mjs → resolve-vector-text-GE2CRJKI.mjs} +1 -1
- package/dist/react-native/chunks/{restore-queue-3E2MJIJS.mjs → restore-queue-2ACONS4W.mjs} +12 -12
- package/dist/react-native/chunks/{segment-codec-leaf-IE4DXUZV.mjs → segment-codec-leaf-SSIWXVUG.mjs} +10 -10
- package/dist/react-native/chunks/{segment-merge-leaf-62LQ5WZB.mjs → segment-merge-leaf-I2WDFE6O.mjs} +11 -11
- package/dist/react-native/chunks/{storage-leaf-LNOLKWZB.mjs → storage-leaf-YXXMG4VR.mjs} +14727 -13915
- package/dist/react-native/chunks/{store-leaf-Y7CBOPFZ.mjs → store-leaf-FS7A4PZA.mjs} +11 -11
- package/dist/react-native/chunks/{suggestions-leaf-PSUGL6MI.mjs → suggestions-leaf-PSFCT5EX.mjs} +11 -11
- package/dist/react-native/chunks/{worker-copies-MW5O5U3D.mjs → worker-copies-QRMISMEL.mjs} +10 -10
- package/dist/react-native/partial-words.mjs +9 -9
- package/dist/react-native/pattern.mjs +5 -5
- package/dist/react-native/query-suggestions.mjs +9 -9
- package/dist/react-native/sync.mjs +433 -51
- package/dist/react-native/synonyms.mjs +9 -9
- package/dist/react.d.ts +8 -8
- package/dist/react.mjs +7 -0
- package/dist/{rerank-KTV5L7MZ.mjs → rerank-4GFC26FD.mjs} +8 -8
- package/dist/rerankers/http.mjs +2 -2
- package/dist/{resolve-vector-text-SD6BEMKZ.mjs → resolve-vector-text-OD4J3WHF.mjs} +2 -2
- package/dist/{restore-queue-ES7TDJ5V.mjs → restore-queue-E4EIUYVV.mjs} +11 -11
- package/dist/{segment-codec-leaf-DO75VKEV.mjs → segment-codec-leaf-MP6DILVE.mjs} +9 -9
- package/dist/{segment-merge-leaf-JGND6LG5.mjs → segment-merge-leaf-ED2NMUC5.mjs} +9 -9
- package/dist/server.d.ts +13 -9
- package/dist/server.mjs +875 -348
- package/dist/{storage-leaf-ZI3H2WJY.mjs → storage-leaf-HQFO37W7.mjs} +5234 -5835
- package/dist/{store-leaf-YIH36Y5J.mjs → store-leaf-PL2G7RM5.mjs} +10 -10
- package/dist/{suggestions-leaf-FP5HSTFP.mjs → suggestions-leaf-I3MEHACP.mjs} +12 -12
- package/dist/sync/filesystem.d.ts +2 -2
- package/dist/sync/filesystem.mjs +16 -16
- package/dist/sync/indexeddb.browser.mjs +1 -1
- package/dist/sync/indexeddb.d.ts +2 -2
- package/dist/sync/indexeddb.mjs +3 -3
- package/dist/sync.browser.mjs +35 -1108
- package/dist/sync.d.ts +3 -3
- package/dist/sync.mjs +430 -51
- package/dist/synonyms.browser.mjs +3 -3
- package/dist/synonyms.mjs +8 -8
- package/dist/{tasks-X2IJQc8J.d.ts → tasks-C65V7lIr.d.ts} +3 -3
- package/dist/thread-LTFI3RK5.mjs +31 -0
- package/dist/{types-D0YasjoX.d.ts → types-CpCg-Gq-.d.ts} +1 -1
- package/dist/vector/search-worker.mjs +5 -5
- package/dist/{worker-copies-WYPRCNB7.mjs → worker-copies-P7YWXEP5.mjs} +11 -11
- package/dist/workers/entry.mjs +14 -14
- package/openapi.json +144 -0
- package/package.json +5 -7
- package/dist/chunk-URVH6BLO.mjs +0 -217
- package/dist/react-native/chunks/msgpack-leaf-GTKLF6GJ.mjs +0 -7
- package/dist/thread-NQRNW3IH.mjs +0 -31
- /package/dist/{chunk-CV3Q2EKO.mjs → chunk-S5QMWECL.mjs} +0 -0
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { isSyncEpoch, hasLanguage, hasTokenizer, hasStopWords, readHeapStatistics, loadStorageCode, getLanguage } from './chunks/chunk-
|
|
2
|
-
import './chunks/chunk-
|
|
3
|
-
import './chunks/chunk-
|
|
1
|
+
import { isSyncEpoch, hasLanguage, hasTokenizer, hasStopWords, readHeapStatistics, loadStorageCode, getLanguage } from './chunks/chunk-63DZ26X5.mjs';
|
|
2
|
+
import './chunks/chunk-AN4HKLB4.mjs';
|
|
3
|
+
import './chunks/chunk-G434H5WG.mjs';
|
|
4
4
|
import './chunks/chunk-LX62Y4B5.mjs';
|
|
5
|
-
import './chunks/chunk-
|
|
6
|
-
import './chunks/chunk-
|
|
7
|
-
import './chunks/chunk-
|
|
8
|
-
import './chunks/chunk-
|
|
9
|
-
import { loadMessagePack, loadedMessagePack, requireLoadedMessagePack, getNestedValue, extractVectorFieldsFromSchema } from './chunks/chunk-
|
|
5
|
+
import './chunks/chunk-HTBY4GON.mjs';
|
|
6
|
+
import './chunks/chunk-UYKIV2KT.mjs';
|
|
7
|
+
import './chunks/chunk-CW2ZFXSE.mjs';
|
|
8
|
+
import './chunks/chunk-UO44MA5M.mjs';
|
|
9
|
+
import { loadMessagePack, loadedMessagePack, requireLoadedMessagePack, getNestedValue, extractVectorFieldsFromSchema } from './chunks/chunk-MRH36RN3.mjs';
|
|
10
10
|
import './chunks/chunk-HRDMIQ63.mjs';
|
|
11
11
|
import './chunks/chunk-VUJCSQG5.mjs';
|
|
12
|
-
import { NarsilError, ErrorCodes, ClientErrorCodes, ServerErrorCodes, describeError } from './chunks/chunk-
|
|
12
|
+
import { NarsilError, ErrorCodes, ClientErrorCodes, ServerErrorCodes, describeError } from './chunks/chunk-43R4GCM7.mjs';
|
|
13
13
|
|
|
14
14
|
// src/json-encoding.ts
|
|
15
15
|
function toPlainNumbers(key, value) {
|
|
@@ -28,6 +28,8 @@ var NO_TIMEOUT = 0;
|
|
|
28
28
|
var DEFAULT_TASK_POLL_INTERVAL_MS = 250;
|
|
29
29
|
var DEFAULT_URL_SKIP_MS = 1e3;
|
|
30
30
|
var DEFAULT_MAX_URL_SKIP_MS = 3e4;
|
|
31
|
+
var MAX_VECTOR_FIELD_PATH_SEGMENTS = 64;
|
|
32
|
+
var MAX_CHANGE_DOCUMENT_DEPTH = 64;
|
|
31
33
|
|
|
32
34
|
// src/client/paths.ts
|
|
33
35
|
function indexPath(indexName) {
|
|
@@ -36,6 +38,19 @@ function indexPath(indexName) {
|
|
|
36
38
|
function documentPath(indexName, docId) {
|
|
37
39
|
return `${indexPath(indexName)}/documents/${encodeURIComponent(docId)}`;
|
|
38
40
|
}
|
|
41
|
+
function socketUrlOf(base, path) {
|
|
42
|
+
const location = globalThis.location?.href;
|
|
43
|
+
let parsed;
|
|
44
|
+
try {
|
|
45
|
+
parsed = location === void 0 ? new URL(base) : new URL(base, location);
|
|
46
|
+
} catch {
|
|
47
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, `The client cannot resolve "${base}" to a server`, { url: base });
|
|
48
|
+
}
|
|
49
|
+
parsed.protocol = parsed.protocol === "https:" ? "wss:" : "ws:";
|
|
50
|
+
parsed.search = "";
|
|
51
|
+
parsed.hash = "";
|
|
52
|
+
return `${parsed.toString().replace(/\/+$/, "")}${path}`;
|
|
53
|
+
}
|
|
39
54
|
function backupPath(backupName) {
|
|
40
55
|
return `/backups/${encodeURIComponent(backupName)}`;
|
|
41
56
|
}
|
|
@@ -317,6 +332,366 @@ function createBulkOperations(transport) {
|
|
|
317
332
|
};
|
|
318
333
|
}
|
|
319
334
|
|
|
335
|
+
// src/changes/wire.ts
|
|
336
|
+
var CHANGES_SUBPROTOCOL = "narsil.changes.v1";
|
|
337
|
+
|
|
338
|
+
// src/client/change-feed-messages.ts
|
|
339
|
+
var CHANGE_TYPES = /* @__PURE__ */ new Set(["put", "insert", "update", "remove"]);
|
|
340
|
+
var RESYNC_REASONS = /* @__PURE__ */ new Set(["epoch", "history", "ahead"]);
|
|
341
|
+
var EPOCH = /^[0-9a-f]{32}$/;
|
|
342
|
+
var FLOAT32_BYTES = 4;
|
|
343
|
+
function malformed2(detail) {
|
|
344
|
+
return new NarsilError(
|
|
345
|
+
ClientErrorCodes.CLIENT_INVALID_RESPONSE,
|
|
346
|
+
`The change feed sent a malformed message: ${detail}`
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
function isRecord2(value) {
|
|
350
|
+
if (value === null || typeof value !== "object") return false;
|
|
351
|
+
const prototype = Object.getPrototypeOf(value);
|
|
352
|
+
return prototype === Object.prototype || prototype === null;
|
|
353
|
+
}
|
|
354
|
+
function isCount(value) {
|
|
355
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
356
|
+
}
|
|
357
|
+
function positionsOf(raw, partitionCount, field) {
|
|
358
|
+
if (!Array.isArray(raw) || raw.length !== partitionCount) throw malformed2(`${field} must hold each partition once`);
|
|
359
|
+
const seen = /* @__PURE__ */ new Set();
|
|
360
|
+
const positions = [];
|
|
361
|
+
for (const item of raw) {
|
|
362
|
+
if (!isRecord2(item) || !isCount(item.partitionId) || !isCount(item.seqNo)) throw malformed2(`${field} is invalid`);
|
|
363
|
+
if (item.partitionId >= partitionCount || seen.has(item.partitionId)) {
|
|
364
|
+
throw malformed2(`${field} must hold each partition once`);
|
|
365
|
+
}
|
|
366
|
+
seen.add(item.partitionId);
|
|
367
|
+
positions.push({ partitionId: item.partitionId, seqNo: item.seqNo });
|
|
368
|
+
}
|
|
369
|
+
return positions;
|
|
370
|
+
}
|
|
371
|
+
function vectorOf(bytes) {
|
|
372
|
+
if (bytes.byteLength % FLOAT32_BYTES !== 0) throw malformed2("a vector has a length that is not whole floats");
|
|
373
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
374
|
+
const vector = new Float32Array(bytes.byteLength / FLOAT32_BYTES);
|
|
375
|
+
for (let at = 0; at < vector.length; at++) vector[at] = view.getFloat32(at * FLOAT32_BYTES, true);
|
|
376
|
+
return vector;
|
|
377
|
+
}
|
|
378
|
+
function restoreVectorAt(document, path) {
|
|
379
|
+
let holder = document;
|
|
380
|
+
for (let at = 0; at < path.length - 1; at++) {
|
|
381
|
+
const segment = path[at];
|
|
382
|
+
if (segment === void 0 || !Object.hasOwn(holder, segment)) return;
|
|
383
|
+
const next = holder[segment];
|
|
384
|
+
if (!isRecord2(next)) return;
|
|
385
|
+
holder = next;
|
|
386
|
+
}
|
|
387
|
+
const leaf = path.at(-1);
|
|
388
|
+
if (leaf === void 0 || !Object.hasOwn(holder, leaf)) return;
|
|
389
|
+
const value = holder[leaf];
|
|
390
|
+
if (!(value instanceof Uint8Array)) return;
|
|
391
|
+
Object.defineProperty(holder, leaf, { value: vectorOf(value), enumerable: true, writable: true, configurable: true });
|
|
392
|
+
}
|
|
393
|
+
function requireShallow(document) {
|
|
394
|
+
const pending = [{ value: document, depth: 0 }];
|
|
395
|
+
for (let next = pending.pop(); next !== void 0; next = pending.pop()) {
|
|
396
|
+
const { value, depth } = next;
|
|
397
|
+
if (depth > MAX_CHANGE_DOCUMENT_DEPTH) throw malformed2("a document nests too deeply");
|
|
398
|
+
const children = Array.isArray(value) ? value : isRecord2(value) ? Object.values(value) : null;
|
|
399
|
+
if (children === null) continue;
|
|
400
|
+
for (const child of children) {
|
|
401
|
+
if (Array.isArray(child) || isRecord2(child)) pending.push({ value: child, depth: depth + 1 });
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
function documentOf(raw, field, vectorFields) {
|
|
406
|
+
if (!isRecord2(raw)) throw malformed2(`${field} must be a map`);
|
|
407
|
+
requireShallow(raw);
|
|
408
|
+
for (const path of vectorFields) restoreVectorAt(raw, path);
|
|
409
|
+
return raw;
|
|
410
|
+
}
|
|
411
|
+
function changeOf(raw, vectorFields) {
|
|
412
|
+
if (!isRecord2(raw) || !isCount(raw.seqNo) || typeof raw.documentId !== "string")
|
|
413
|
+
throw malformed2("a change is invalid");
|
|
414
|
+
if (typeof raw.type !== "string" || !CHANGE_TYPES.has(raw.type)) throw malformed2("a change has an unknown type");
|
|
415
|
+
const change = { seqNo: raw.seqNo, type: raw.type, documentId: raw.documentId };
|
|
416
|
+
if (raw.document !== void 0 && raw.document !== null) {
|
|
417
|
+
change.document = documentOf(raw.document, "document", vectorFields);
|
|
418
|
+
}
|
|
419
|
+
if (raw.previousDocument !== void 0 && raw.previousDocument !== null) {
|
|
420
|
+
change.previousDocument = documentOf(raw.previousDocument, "previousDocument", vectorFields);
|
|
421
|
+
}
|
|
422
|
+
return change;
|
|
423
|
+
}
|
|
424
|
+
function vectorFieldsOf(raw) {
|
|
425
|
+
if (!Array.isArray(raw)) throw malformed2("vectorFields must be a list of paths");
|
|
426
|
+
const paths = [];
|
|
427
|
+
for (const item of raw) {
|
|
428
|
+
if (typeof item !== "string") throw malformed2("vectorFields must be a list of paths");
|
|
429
|
+
const path = item.split(".");
|
|
430
|
+
if (path.length > MAX_VECTOR_FIELD_PATH_SEGMENTS || path.includes("")) {
|
|
431
|
+
throw malformed2(`the vector field path ${item} is invalid`);
|
|
432
|
+
}
|
|
433
|
+
paths.push(path);
|
|
434
|
+
}
|
|
435
|
+
return paths;
|
|
436
|
+
}
|
|
437
|
+
function resyncOf(raw) {
|
|
438
|
+
if (typeof raw.reason !== "string" || !RESYNC_REASONS.has(raw.reason)) throw malformed2("the resync reason is unknown");
|
|
439
|
+
return { type: "resync", reason: raw.reason };
|
|
440
|
+
}
|
|
441
|
+
function firstEventOf(raw) {
|
|
442
|
+
if (!isRecord2(raw)) throw malformed2("a message must be a map");
|
|
443
|
+
if (raw.type === "resync") return { event: resyncOf(raw), vectorFields: [] };
|
|
444
|
+
if (raw.type !== "subscribed") throw malformed2("the first message must be subscribed or resync");
|
|
445
|
+
const { epoch, partitionCount, bytesBehind, windowBytes, previousDocument } = raw;
|
|
446
|
+
if (typeof epoch !== "string" || !EPOCH.test(epoch)) throw malformed2("the epoch is invalid");
|
|
447
|
+
if (!isCount(partitionCount) || partitionCount === 0) throw malformed2("the partition count is invalid");
|
|
448
|
+
if (!isCount(bytesBehind) || !isCount(windowBytes) || windowBytes === 0) throw malformed2("a byte count is invalid");
|
|
449
|
+
if (typeof previousDocument !== "boolean") throw malformed2("previousDocument must be a boolean");
|
|
450
|
+
const event = {
|
|
451
|
+
type: "subscribed",
|
|
452
|
+
epoch,
|
|
453
|
+
partitionCount,
|
|
454
|
+
positions: positionsOf(raw.positions, partitionCount, "positions"),
|
|
455
|
+
heads: positionsOf(raw.heads, partitionCount, "heads"),
|
|
456
|
+
bytesBehind,
|
|
457
|
+
previousDocument
|
|
458
|
+
};
|
|
459
|
+
return { event, vectorFields: vectorFieldsOf(raw.vectorFields) };
|
|
460
|
+
}
|
|
461
|
+
function laterEventOf(raw, partitionCount, vectorFields) {
|
|
462
|
+
if (!isRecord2(raw)) throw malformed2("a message must be a map");
|
|
463
|
+
if (raw.type === "resync") return resyncOf(raw);
|
|
464
|
+
if (raw.type !== "changes") throw malformed2("the message type is unknown");
|
|
465
|
+
const { partitionId, scannedSeqNo, changes } = raw;
|
|
466
|
+
if (!isCount(partitionId) || partitionId >= partitionCount) throw malformed2("the partition is unknown");
|
|
467
|
+
if (!isCount(scannedSeqNo) || !Array.isArray(changes)) throw malformed2("the changes are invalid");
|
|
468
|
+
const decoded = changes.map((change) => changeOf(change, vectorFields));
|
|
469
|
+
if (decoded.some((change) => change.seqNo > scannedSeqNo)) throw malformed2("a change lies above scannedSeqNo");
|
|
470
|
+
return { type: "changes", partitionId, changes: decoded, scannedSeqNo };
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// src/client/change-feed.ts
|
|
474
|
+
var CLOSE_NORMAL = 1e3;
|
|
475
|
+
var CLOSE_CREDENTIAL_EXPIRED = 4401;
|
|
476
|
+
var CLOSE_SETTING = 4409;
|
|
477
|
+
var CLOSE_RESYNC = 4410;
|
|
478
|
+
var CLOSE_POLICY = 1008;
|
|
479
|
+
function closedError(code, reason) {
|
|
480
|
+
if (code === CLOSE_SETTING && reason.length > 0) {
|
|
481
|
+
return new NarsilError(reason, `The server refused the subscription with ${reason}`, { closeCode: code });
|
|
482
|
+
}
|
|
483
|
+
if (code === CLOSE_RESYNC) {
|
|
484
|
+
return new NarsilError(ErrorCodes.CHANGES_NOT_ENABLED, "The index has change capture off", { closeCode: code });
|
|
485
|
+
}
|
|
486
|
+
if (code === CLOSE_POLICY) {
|
|
487
|
+
return new NarsilError(ErrorCodes.CONFIG_INVALID, `The server refused the subscription: ${reason}`, {
|
|
488
|
+
closeCode: code
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
const why = code === CLOSE_CREDENTIAL_EXPIRED ? "the credential expired" : `the server closed it with code ${code}`;
|
|
492
|
+
return new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, `The change feed ended because ${why}`, {
|
|
493
|
+
closeCode: code,
|
|
494
|
+
reason
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
function subscribeMessage(options) {
|
|
498
|
+
const { epoch, positions, filter } = options;
|
|
499
|
+
if (epoch === void 0 !== (positions === void 0)) {
|
|
500
|
+
throw new NarsilError(ErrorCodes.CHANGES_INVALID_POSITIONS, "Pass epoch and positions together, or leave both out");
|
|
501
|
+
}
|
|
502
|
+
return { type: "subscribe", epoch, positions, filter };
|
|
503
|
+
}
|
|
504
|
+
function openChangeSubscription(url, options) {
|
|
505
|
+
if (typeof WebSocket === "undefined") {
|
|
506
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The change feed needs a runtime with a global WebSocket");
|
|
507
|
+
}
|
|
508
|
+
const subscribe = subscribeMessage(options);
|
|
509
|
+
const queue = [];
|
|
510
|
+
let failure = null;
|
|
511
|
+
let ended = false;
|
|
512
|
+
let iterating = false;
|
|
513
|
+
let wake = null;
|
|
514
|
+
let socket = null;
|
|
515
|
+
let codec = null;
|
|
516
|
+
let partitionCount = 0;
|
|
517
|
+
let vectorFields = [];
|
|
518
|
+
const saved = [];
|
|
519
|
+
const notify = () => {
|
|
520
|
+
const waiting = wake;
|
|
521
|
+
wake = null;
|
|
522
|
+
waiting?.();
|
|
523
|
+
};
|
|
524
|
+
function finish(error) {
|
|
525
|
+
if (ended) return;
|
|
526
|
+
ended = true;
|
|
527
|
+
failure = error;
|
|
528
|
+
options.signal?.removeEventListener("abort", close);
|
|
529
|
+
if (socket !== null && socket.readyState <= WebSocket.OPEN) socket.close(CLOSE_NORMAL, "subscriber closed");
|
|
530
|
+
notify();
|
|
531
|
+
}
|
|
532
|
+
function close() {
|
|
533
|
+
finish(null);
|
|
534
|
+
}
|
|
535
|
+
function receive(data, code) {
|
|
536
|
+
if (!(data instanceof ArrayBuffer)) {
|
|
537
|
+
finish(new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, "The change feed sent a text frame"));
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
try {
|
|
541
|
+
const raw = code.decode(new Uint8Array(data));
|
|
542
|
+
let event;
|
|
543
|
+
if (partitionCount === 0) {
|
|
544
|
+
const first = firstEventOf(raw);
|
|
545
|
+
event = first.event;
|
|
546
|
+
vectorFields = first.vectorFields;
|
|
547
|
+
} else {
|
|
548
|
+
event = laterEventOf(raw, partitionCount, vectorFields);
|
|
549
|
+
}
|
|
550
|
+
if (event.type === "subscribed") {
|
|
551
|
+
partitionCount = event.partitionCount;
|
|
552
|
+
saved.push(...new Array(partitionCount).fill(0));
|
|
553
|
+
for (const position of event.positions) saved[position.partitionId] = position.seqNo;
|
|
554
|
+
}
|
|
555
|
+
queue.push(event);
|
|
556
|
+
if (event.type === "resync") {
|
|
557
|
+
finish(null);
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
} catch (error) {
|
|
561
|
+
finish(
|
|
562
|
+
error instanceof NarsilError ? error : new NarsilError(
|
|
563
|
+
ClientErrorCodes.CLIENT_INVALID_RESPONSE,
|
|
564
|
+
"The change feed sent a message that is not MessagePack",
|
|
565
|
+
{ cause: error instanceof Error ? error.message : String(error) }
|
|
566
|
+
)
|
|
567
|
+
);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
notify();
|
|
571
|
+
}
|
|
572
|
+
function connect(code) {
|
|
573
|
+
codec = code;
|
|
574
|
+
if (ended) return;
|
|
575
|
+
const protocols = options.credential === void 0 ? [CHANGES_SUBPROTOCOL] : [CHANGES_SUBPROTOCOL, options.credential];
|
|
576
|
+
let opened;
|
|
577
|
+
try {
|
|
578
|
+
opened = new WebSocket(url, protocols);
|
|
579
|
+
} catch (error) {
|
|
580
|
+
finish(
|
|
581
|
+
new NarsilError(ErrorCodes.CONFIG_INVALID, "The change feed could not start a WebSocket connection", {
|
|
582
|
+
cause: error instanceof Error ? error.message : String(error)
|
|
583
|
+
})
|
|
584
|
+
);
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
socket = opened;
|
|
588
|
+
opened.binaryType = "arraybuffer";
|
|
589
|
+
opened.onopen = () => {
|
|
590
|
+
if (opened.protocol !== CHANGES_SUBPROTOCOL) {
|
|
591
|
+
finish(
|
|
592
|
+
new NarsilError(
|
|
593
|
+
ClientErrorCodes.CLIENT_INVALID_RESPONSE,
|
|
594
|
+
`The server answered the change feed with the subprotocol "${opened.protocol}"`
|
|
595
|
+
)
|
|
596
|
+
);
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
opened.send(code.encode(subscribe, { ignoreUndefined: true }));
|
|
600
|
+
};
|
|
601
|
+
opened.onmessage = (event) => {
|
|
602
|
+
if (!ended) receive(event.data, code);
|
|
603
|
+
};
|
|
604
|
+
opened.onerror = () => {
|
|
605
|
+
if (!ended) finish(new NarsilError(ClientErrorCodes.CLIENT_CONNECTION_FAILED, `The change feed at ${url} failed`));
|
|
606
|
+
};
|
|
607
|
+
opened.onclose = (event) => {
|
|
608
|
+
if (!ended) finish(closedError(event.code, event.reason));
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
if (options.signal?.aborted === true) ended = true;
|
|
612
|
+
else options.signal?.addEventListener("abort", close);
|
|
613
|
+
if (!ended) {
|
|
614
|
+
loadMessagePack().then(
|
|
615
|
+
connect,
|
|
616
|
+
(error) => finish(
|
|
617
|
+
error instanceof NarsilError ? error : new NarsilError(ClientErrorCodes.CLIENT_UNEXPECTED_ERROR, String(error))
|
|
618
|
+
)
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
async function* events() {
|
|
622
|
+
try {
|
|
623
|
+
while (true) {
|
|
624
|
+
const next = queue.shift();
|
|
625
|
+
if (next !== void 0) {
|
|
626
|
+
yield next;
|
|
627
|
+
continue;
|
|
628
|
+
}
|
|
629
|
+
if (ended) {
|
|
630
|
+
if (failure !== null) throw failure;
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
await new Promise((resolve) => {
|
|
634
|
+
wake = resolve;
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
} finally {
|
|
638
|
+
close();
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
return {
|
|
642
|
+
[Symbol.asyncIterator]() {
|
|
643
|
+
if (iterating) {
|
|
644
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "A change subscription returns its events to one loop alone");
|
|
645
|
+
}
|
|
646
|
+
iterating = true;
|
|
647
|
+
return events();
|
|
648
|
+
},
|
|
649
|
+
ack(positions) {
|
|
650
|
+
for (const { partitionId, seqNo } of positions) {
|
|
651
|
+
if (!Number.isSafeInteger(partitionId) || partitionId < 0 || !Number.isSafeInteger(seqNo) || seqNo < 0) {
|
|
652
|
+
throw new NarsilError(
|
|
653
|
+
ErrorCodes.CHANGES_INVALID_POSITIONS,
|
|
654
|
+
"Each acknowledged position needs whole numbers",
|
|
655
|
+
{
|
|
656
|
+
partitionId,
|
|
657
|
+
seqNo
|
|
658
|
+
}
|
|
659
|
+
);
|
|
660
|
+
}
|
|
661
|
+
if (saved.length > 0 && partitionId >= saved.length) {
|
|
662
|
+
throw new NarsilError(ErrorCodes.CHANGES_INVALID_POSITIONS, "The index has no such partition", {
|
|
663
|
+
partitionId,
|
|
664
|
+
partitionCount
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
if (ended || socket === null || codec === null || socket.readyState !== WebSocket.OPEN) return;
|
|
669
|
+
if (saved.length === 0) return;
|
|
670
|
+
for (const { partitionId, seqNo } of positions) saved[partitionId] = Math.max(saved[partitionId] ?? 0, seqNo);
|
|
671
|
+
const every = saved.map((seqNo, partitionId) => ({ partitionId, seqNo }));
|
|
672
|
+
socket.send(codec.encode({ type: "ack", positions: every }));
|
|
673
|
+
},
|
|
674
|
+
close
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// src/client/changes.ts
|
|
679
|
+
function createChangesClientOperations(transport) {
|
|
680
|
+
return {
|
|
681
|
+
async getChanges(indexName, options) {
|
|
682
|
+
const path = `${indexPath(indexName)}/changes`;
|
|
683
|
+
return readBody(await transport.json({ method: "GET", path, options }), path);
|
|
684
|
+
},
|
|
685
|
+
async updateChanges(indexName, setting, options) {
|
|
686
|
+
const path = `${indexPath(indexName)}/changes`;
|
|
687
|
+
await transport.json({ method: "PUT", path, body: encodeJson(setting), contentType: "application/json", options });
|
|
688
|
+
},
|
|
689
|
+
followChanges(indexName, options = {}) {
|
|
690
|
+
return openChangeSubscription(transport.socketUrl(`${indexPath(indexName)}/changes/feed`), options);
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
|
|
320
695
|
// src/client/documents.ts
|
|
321
696
|
function createDocumentOperations(transport) {
|
|
322
697
|
return {
|
|
@@ -453,7 +828,7 @@ function movesOnAfterFailure(failure, sentNothing, repeatable) {
|
|
|
453
828
|
}
|
|
454
829
|
|
|
455
830
|
// src/client/http.ts
|
|
456
|
-
function
|
|
831
|
+
function isRecord3(value) {
|
|
457
832
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
458
833
|
}
|
|
459
834
|
function unrefTimer(timer) {
|
|
@@ -526,11 +901,11 @@ function invalidResponse(message, details) {
|
|
|
526
901
|
return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, message, details);
|
|
527
902
|
}
|
|
528
903
|
function errorFromBody(status, payload, url) {
|
|
529
|
-
const envelope =
|
|
904
|
+
const envelope = isRecord3(payload) && isRecord3(payload.error) ? payload.error : void 0;
|
|
530
905
|
if (envelope === void 0 || typeof envelope.code !== "string" || typeof envelope.message !== "string") {
|
|
531
906
|
return invalidResponse(`The server answered HTTP ${status} without a Narsil error envelope`, { url, status });
|
|
532
907
|
}
|
|
533
|
-
const details =
|
|
908
|
+
const details = isRecord3(envelope.details) ? envelope.details : {};
|
|
534
909
|
return new NarsilError(envelope.code, envelope.message, { ...details, status });
|
|
535
910
|
}
|
|
536
911
|
function isHttpUrl(value) {
|
|
@@ -595,7 +970,7 @@ function translateFailure(err, url, timeoutMs, expired, caller) {
|
|
|
595
970
|
function envelopeCodeOf(text) {
|
|
596
971
|
try {
|
|
597
972
|
const payload = JSON.parse(text);
|
|
598
|
-
return
|
|
973
|
+
return isRecord3(payload) && isRecord3(payload.error) && typeof payload.error.code === "string" ? payload.error.code : void 0;
|
|
599
974
|
} catch {
|
|
600
975
|
return void 0;
|
|
601
976
|
}
|
|
@@ -731,6 +1106,12 @@ function createTransport(options) {
|
|
|
731
1106
|
async probe(spec) {
|
|
732
1107
|
const { status } = await exchange(spec);
|
|
733
1108
|
return status;
|
|
1109
|
+
},
|
|
1110
|
+
socketUrl(path) {
|
|
1111
|
+
const [entry] = pool.order(Date.now());
|
|
1112
|
+
if (entry === void 0) throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The client holds no server address");
|
|
1113
|
+
pool.took(entry);
|
|
1114
|
+
return socketUrlOf(entry.base, path);
|
|
734
1115
|
}
|
|
735
1116
|
};
|
|
736
1117
|
}
|
|
@@ -1081,6 +1462,7 @@ function createNarsilClient(options) {
|
|
|
1081
1462
|
...createBackupOperations(transport),
|
|
1082
1463
|
...createServerOperations(transport),
|
|
1083
1464
|
...createSyncSettingOperations(transport),
|
|
1465
|
+
...createChangesClientOperations(transport),
|
|
1084
1466
|
...createQuerySuggestionOperations(transport)
|
|
1085
1467
|
};
|
|
1086
1468
|
}
|
|
@@ -1124,23 +1506,23 @@ function backoffDelayMs(attempt, minimumMs, maximumMs) {
|
|
|
1124
1506
|
}
|
|
1125
1507
|
|
|
1126
1508
|
// src/sync/device/messages.ts
|
|
1127
|
-
var
|
|
1128
|
-
function
|
|
1509
|
+
var RESYNC_REASONS2 = /* @__PURE__ */ new Set(["epoch", "history", "ahead"]);
|
|
1510
|
+
function isRecord4(value) {
|
|
1129
1511
|
return value !== null && typeof value === "object" && !Array.isArray(value) && !(value instanceof Uint8Array);
|
|
1130
1512
|
}
|
|
1131
|
-
function
|
|
1513
|
+
function isCount2(value) {
|
|
1132
1514
|
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
1133
1515
|
}
|
|
1134
|
-
function
|
|
1516
|
+
function malformed3(detail) {
|
|
1135
1517
|
return new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, `The sync feed sent a malformed message: ${detail}`);
|
|
1136
1518
|
}
|
|
1137
1519
|
function headsOf(raw) {
|
|
1138
|
-
if (!Array.isArray(raw)) throw
|
|
1520
|
+
if (!Array.isArray(raw)) throw malformed3("heads must be a list");
|
|
1139
1521
|
const seqNos = new Array(raw.length).fill(-1);
|
|
1140
1522
|
for (const item of raw) {
|
|
1141
|
-
if (!
|
|
1523
|
+
if (!isRecord4(item) || !isCount2(item.partitionId) || !isCount2(item.seqNo)) throw malformed3("a head is invalid");
|
|
1142
1524
|
if (item.partitionId >= seqNos.length || seqNos[item.partitionId] !== -1) {
|
|
1143
|
-
throw
|
|
1525
|
+
throw malformed3("heads must name each partition once");
|
|
1144
1526
|
}
|
|
1145
1527
|
seqNos[item.partitionId] = item.seqNo;
|
|
1146
1528
|
}
|
|
@@ -1150,22 +1532,22 @@ function stringList(value) {
|
|
|
1150
1532
|
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
1151
1533
|
}
|
|
1152
1534
|
function settingsOf(raw) {
|
|
1153
|
-
if (!
|
|
1535
|
+
if (!isRecord4(raw) || typeof raw.language !== "string") throw malformed3("settings need a language");
|
|
1154
1536
|
const optionalString2 = (key) => {
|
|
1155
1537
|
const value = raw[key];
|
|
1156
1538
|
if (value === void 0 || value === null) return void 0;
|
|
1157
|
-
if (typeof value !== "string") throw
|
|
1539
|
+
if (typeof value !== "string") throw malformed3(`settings.${key} must be a string`);
|
|
1158
1540
|
return value;
|
|
1159
1541
|
};
|
|
1160
1542
|
const tokenizer = optionalString2("tokenizer");
|
|
1161
1543
|
const stopWords = optionalString2("stopWords");
|
|
1162
1544
|
const analysisRevision = optionalString2("analysisRevision");
|
|
1163
1545
|
if (raw.stopWordList !== void 0 && raw.stopWordList !== null && !stringList(raw.stopWordList)) {
|
|
1164
|
-
throw
|
|
1546
|
+
throw malformed3("settings.stopWordList must be a list of strings");
|
|
1165
1547
|
}
|
|
1166
|
-
if (raw.bm25 !== void 0 && raw.bm25 !== null && !
|
|
1548
|
+
if (raw.bm25 !== void 0 && raw.bm25 !== null && !isRecord4(raw.bm25)) throw malformed3("settings.bm25 is invalid");
|
|
1167
1549
|
if (raw.synonyms !== void 0 && raw.synonyms !== null && !Array.isArray(raw.synonyms)) {
|
|
1168
|
-
throw
|
|
1550
|
+
throw malformed3("settings.synonyms must be a list");
|
|
1169
1551
|
}
|
|
1170
1552
|
return {
|
|
1171
1553
|
language: raw.language,
|
|
@@ -1173,26 +1555,26 @@ function settingsOf(raw) {
|
|
|
1173
1555
|
...stopWords !== void 0 ? { stopWords } : {},
|
|
1174
1556
|
...stringList(raw.stopWordList) ? { stopWordList: raw.stopWordList } : {},
|
|
1175
1557
|
...analysisRevision !== void 0 ? { analysisRevision } : {},
|
|
1176
|
-
...
|
|
1558
|
+
...isRecord4(raw.bm25) ? { bm25: raw.bm25 } : {},
|
|
1177
1559
|
...Array.isArray(raw.synonyms) ? { synonyms: raw.synonyms } : {}
|
|
1178
1560
|
};
|
|
1179
1561
|
}
|
|
1180
1562
|
function entriesOf(partitionId, raw) {
|
|
1181
|
-
if (!Array.isArray(raw)) throw
|
|
1563
|
+
if (!Array.isArray(raw)) throw malformed3("entries must be a list");
|
|
1182
1564
|
let previous = -1;
|
|
1183
1565
|
return raw.map((item) => {
|
|
1184
|
-
if (!
|
|
1185
|
-
throw
|
|
1566
|
+
if (!isRecord4(item) || !isCount2(item.seqNo) || typeof item.documentId !== "string") {
|
|
1567
|
+
throw malformed3("an entry is invalid");
|
|
1186
1568
|
}
|
|
1187
|
-
if (item.seqNo <= previous) throw
|
|
1569
|
+
if (item.seqNo <= previous) throw malformed3("entries must rise in sequence order");
|
|
1188
1570
|
if (item.partitionId !== void 0 && item.partitionId !== partitionId) {
|
|
1189
|
-
throw
|
|
1571
|
+
throw malformed3("an entry belongs to another partition");
|
|
1190
1572
|
}
|
|
1191
1573
|
previous = item.seqNo;
|
|
1192
1574
|
if (item.operation === "DELETE") {
|
|
1193
1575
|
return { partitionId, seqNo: item.seqNo, documentId: item.documentId, document: null };
|
|
1194
1576
|
}
|
|
1195
|
-
if (item.operation !== "INDEX" || !(item.document instanceof Uint8Array)) throw
|
|
1577
|
+
if (item.operation !== "INDEX" || !(item.document instanceof Uint8Array)) throw malformed3("an entry is invalid");
|
|
1196
1578
|
return { partitionId, seqNo: item.seqNo, documentId: item.documentId, document: item.document };
|
|
1197
1579
|
});
|
|
1198
1580
|
}
|
|
@@ -1202,17 +1584,17 @@ function decodeFeedMessage(bytes) {
|
|
|
1202
1584
|
try {
|
|
1203
1585
|
raw = decode(bytes);
|
|
1204
1586
|
} catch {
|
|
1205
|
-
throw
|
|
1587
|
+
throw malformed3("a frame is not MessagePack");
|
|
1206
1588
|
}
|
|
1207
|
-
if (!
|
|
1589
|
+
if (!isRecord4(raw)) throw malformed3("a frame is not a map");
|
|
1208
1590
|
switch (raw.type) {
|
|
1209
1591
|
case "subscribed": {
|
|
1210
|
-
if (!isSyncEpoch(raw.epoch) || !
|
|
1211
|
-
if (!
|
|
1212
|
-
throw
|
|
1592
|
+
if (!isSyncEpoch(raw.epoch) || !isCount2(raw.partitionCount)) throw malformed3("subscribed is invalid");
|
|
1593
|
+
if (!isCount2(raw.bytesBehind) || !isCount2(raw.snapshotBytes) || !isCount2(raw.windowBytes)) {
|
|
1594
|
+
throw malformed3("subscribed is invalid");
|
|
1213
1595
|
}
|
|
1214
1596
|
const heads = headsOf(raw.heads);
|
|
1215
|
-
if (heads.length !== raw.partitionCount) throw
|
|
1597
|
+
if (heads.length !== raw.partitionCount) throw malformed3("subscribed needs one head for each partition");
|
|
1216
1598
|
return {
|
|
1217
1599
|
type: "subscribed",
|
|
1218
1600
|
epoch: raw.epoch,
|
|
@@ -1225,26 +1607,26 @@ function decodeFeedMessage(bytes) {
|
|
|
1225
1607
|
};
|
|
1226
1608
|
}
|
|
1227
1609
|
case "entries":
|
|
1228
|
-
if (!
|
|
1610
|
+
if (!isCount2(raw.partitionId)) throw malformed3("entries need a partition");
|
|
1229
1611
|
return { type: "entries", partitionId: raw.partitionId, entries: entriesOf(raw.partitionId, raw.entries) };
|
|
1230
1612
|
case "settings":
|
|
1231
1613
|
return { type: "settings", settings: settingsOf(raw.settings) };
|
|
1232
1614
|
case "checkpoint":
|
|
1233
|
-
if (!
|
|
1615
|
+
if (!isCount2(raw.id)) throw malformed3("checkpoint needs an id");
|
|
1234
1616
|
return { type: "checkpoint", id: raw.id, heads: headsOf(raw.heads) };
|
|
1235
1617
|
case "resync":
|
|
1236
|
-
if (typeof raw.reason !== "string" || !
|
|
1618
|
+
if (typeof raw.reason !== "string" || !RESYNC_REASONS2.has(raw.reason)) throw malformed3("resync is invalid");
|
|
1237
1619
|
return { type: "resync", reason: raw.reason };
|
|
1238
1620
|
default:
|
|
1239
|
-
throw
|
|
1621
|
+
throw malformed3(`unknown type ${String(raw.type)}`);
|
|
1240
1622
|
}
|
|
1241
1623
|
}
|
|
1242
|
-
function
|
|
1624
|
+
function positionsOf2(seqNos) {
|
|
1243
1625
|
return seqNos.map((seqNo, partitionId) => ({ partitionId, seqNo }));
|
|
1244
1626
|
}
|
|
1245
1627
|
|
|
1246
1628
|
// src/sync/device/feed.ts
|
|
1247
|
-
var
|
|
1629
|
+
var CLOSE_NORMAL2 = 1e3;
|
|
1248
1630
|
function feedUrlOf(base, indexName) {
|
|
1249
1631
|
const location = globalThis.location?.href;
|
|
1250
1632
|
let parsed;
|
|
@@ -1281,7 +1663,7 @@ function openFeedConnection(url, credential, handlers) {
|
|
|
1281
1663
|
}
|
|
1282
1664
|
function closeSocket(reason) {
|
|
1283
1665
|
if (socket.readyState === WebSocket.CONNECTING || socket.readyState === WebSocket.OPEN) {
|
|
1284
|
-
socket.close(
|
|
1666
|
+
socket.close(CLOSE_NORMAL2, reason);
|
|
1285
1667
|
}
|
|
1286
1668
|
}
|
|
1287
1669
|
socket.onopen = () => {
|
|
@@ -1293,7 +1675,7 @@ function openFeedConnection(url, credential, handlers) {
|
|
|
1293
1675
|
)
|
|
1294
1676
|
);
|
|
1295
1677
|
closeSocket("subprotocol");
|
|
1296
|
-
finish(
|
|
1678
|
+
finish(CLOSE_NORMAL2);
|
|
1297
1679
|
return;
|
|
1298
1680
|
}
|
|
1299
1681
|
handlers.onOpen();
|
|
@@ -1303,7 +1685,7 @@ function openFeedConnection(url, credential, handlers) {
|
|
|
1303
1685
|
if (!(event.data instanceof ArrayBuffer)) {
|
|
1304
1686
|
handlers.onFailure(new NarsilError(ClientErrorCodes.CLIENT_INVALID_RESPONSE, "The feed sent a text frame"));
|
|
1305
1687
|
closeSocket("text frame");
|
|
1306
|
-
finish(
|
|
1688
|
+
finish(CLOSE_NORMAL2);
|
|
1307
1689
|
return;
|
|
1308
1690
|
}
|
|
1309
1691
|
let message;
|
|
@@ -1312,7 +1694,7 @@ function openFeedConnection(url, credential, handlers) {
|
|
|
1312
1694
|
} catch (error) {
|
|
1313
1695
|
handlers.onFailure(error);
|
|
1314
1696
|
closeSocket("malformed message");
|
|
1315
|
-
finish(
|
|
1697
|
+
finish(CLOSE_NORMAL2);
|
|
1316
1698
|
return;
|
|
1317
1699
|
}
|
|
1318
1700
|
handlers.onMessage(message);
|
|
@@ -1327,7 +1709,7 @@ function openFeedConnection(url, credential, handlers) {
|
|
|
1327
1709
|
},
|
|
1328
1710
|
close(reason) {
|
|
1329
1711
|
closeSocket(reason);
|
|
1330
|
-
finish(
|
|
1712
|
+
finish(CLOSE_NORMAL2);
|
|
1331
1713
|
}
|
|
1332
1714
|
};
|
|
1333
1715
|
}
|
|
@@ -1750,8 +2132,8 @@ function createSession(context, progress, state) {
|
|
|
1750
2132
|
try {
|
|
1751
2133
|
feed = openFeedConnection(feedUrl, credential, {
|
|
1752
2134
|
onOpen() {
|
|
1753
|
-
progress.setAcknowledge((seqNos) => feed?.send({ type: "ack", positions:
|
|
1754
|
-
feed?.send({ type: "subscribe", epoch: position.epoch, positions:
|
|
2135
|
+
progress.setAcknowledge((seqNos) => feed?.send({ type: "ack", positions: positionsOf2(seqNos) }));
|
|
2136
|
+
feed?.send({ type: "subscribe", epoch: position.epoch, positions: positionsOf2(progress.received()) });
|
|
1755
2137
|
},
|
|
1756
2138
|
onMessage(message) {
|
|
1757
2139
|
incoming = incoming.then(() => generation === mine ? handle(message) : void 0).catch((error) => {
|