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