@delali/narsil 0.3.0 → 0.3.1-next.102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/README.md +9 -8
  2. package/dist/adapters/filesystem.d.ts +1 -1
  3. package/dist/adapters/filesystem.mjs +1 -1
  4. package/dist/adapters/indexeddb.d.ts +1 -1
  5. package/dist/adapters/indexeddb.mjs +1 -1
  6. package/dist/adapters/memory.d.ts +1 -1
  7. package/dist/{adapters-79OIa4xm.d.ts → adapters-BQFQ4PRz.d.ts} +27 -1
  8. package/dist/browser/chunk-BDOSMEEP.mjs +493 -0
  9. package/dist/browser/chunk-EJDUFWSO.mjs +602 -0
  10. package/dist/browser/chunk-HOZW3PKP.mjs +9 -0
  11. package/dist/browser/chunk-JOVZ6HMT.mjs +4358 -0
  12. package/dist/browser/chunk-JPTQZWJQ.mjs +10 -0
  13. package/dist/browser/chunk-KPJMDPHG.mjs +24558 -0
  14. package/dist/browser/chunk-PGIM3HIM.mjs +2743 -0
  15. package/dist/browser/chunk-RHG4O77A.mjs +206 -0
  16. package/dist/browser/chunk-RIJSLY2I.mjs +360 -0
  17. package/dist/browser/durability-wiring-SRUYT3VV.mjs +3875 -0
  18. package/dist/browser/module-CBB2ETYP.mjs +3111 -0
  19. package/dist/browser/release-76MXRPGV.mjs +33 -0
  20. package/dist/browser/replace-RURXTGVG.mjs +57 -0
  21. package/dist/browser/restore-queue-EEZQO6RF.mjs +35 -0
  22. package/dist/browser/runtime-7ZAHCHWR.mjs +557 -0
  23. package/dist/browser/snapshot-OAAA5UJ7.mjs +1210 -0
  24. package/dist/browser/write-7D6VIK6O.mjs +208 -0
  25. package/dist/{capabilities-DSsOGz6F.d.ts → capabilities-ByQRMOx-.d.ts} +68 -7
  26. package/dist/chunk-2RIRGOUL.mjs +113 -0
  27. package/dist/chunk-37CD3OKS.mjs +87 -0
  28. package/dist/chunk-44W3CBNG.mjs +7 -0
  29. package/dist/chunk-46D6JQIG.mjs +426 -0
  30. package/dist/{chunk-VMRR3DQU.mjs → chunk-5JNA5SKS.mjs} +546 -35
  31. package/dist/chunk-747OHOUA.mjs +11 -0
  32. package/dist/chunk-75XIEF6H.mjs +126 -0
  33. package/dist/chunk-7X5JST5L.mjs +114 -0
  34. package/dist/chunk-BOFBLJFZ.mjs +209 -0
  35. package/dist/{chunk-USKFU7J5.mjs → chunk-BWJVNZPR.mjs} +8 -1
  36. package/dist/chunk-C7SLA6CM.mjs +19 -0
  37. package/dist/{chunk-POWOTUKJ.mjs → chunk-CQUMNIIG.mjs} +3 -6
  38. package/dist/chunk-CVXLLEQ3.mjs +1011 -0
  39. package/dist/chunk-DJHHF2L5.mjs +17 -0
  40. package/dist/chunk-E74YLAIG.mjs +24 -0
  41. package/dist/{chunk-UXNWQZST.mjs → chunk-EMRMGYRE.mjs} +236 -18
  42. package/dist/chunk-EWZLIYHV.mjs +57 -0
  43. package/dist/chunk-GBEEG5AN.mjs +35 -0
  44. package/dist/chunk-GES7U2PM.mjs +68 -0
  45. package/dist/chunk-HJSN7UYG.mjs +10 -0
  46. package/dist/chunk-HLPRHOSO.mjs +17 -0
  47. package/dist/chunk-HMFNFVP7.mjs +14 -0
  48. package/dist/chunk-J5QP52HA.mjs +1751 -0
  49. package/dist/{chunk-YNVIUGF5.mjs → chunk-JZVARCRZ.mjs} +3 -28
  50. package/dist/chunk-KOKM4PQN.mjs +52 -0
  51. package/dist/{chunk-B2TFROFX.mjs → chunk-KWTW6VTG.mjs} +465 -250
  52. package/dist/chunk-KZ3NX2P7.mjs +21 -0
  53. package/dist/chunk-L5ORED7V.mjs +25 -0
  54. package/dist/{chunk-THYRHHO2.mjs → chunk-LGAMKHA2.mjs} +2009 -2351
  55. package/dist/chunk-LKOB5COA.mjs +117 -0
  56. package/dist/chunk-LPQOEU5M.mjs +96 -0
  57. package/dist/{chunk-3LHGNRYE.mjs → chunk-LXWDQLTW.mjs} +7 -2
  58. package/dist/{chunk-L6TNPLAR.mjs → chunk-OEYI3KRB.mjs} +270 -108
  59. package/dist/chunk-OM362BRO.mjs +948 -0
  60. package/dist/chunk-OZDFA4WC.mjs +67 -0
  61. package/dist/chunk-P36MUFEJ.mjs +10 -0
  62. package/dist/chunk-PC2G76VW.mjs +2540 -0
  63. package/dist/{chunk-GCM3JFVA.mjs → chunk-PNEYIHA4.mjs} +11 -2
  64. package/dist/chunk-PNFKUCG3.mjs +1130 -0
  65. package/dist/chunk-Q6XWVUZX.mjs +44 -0
  66. package/dist/chunk-R27EMH5M.mjs +617 -0
  67. package/dist/{chunk-YMZCMH5M.mjs → chunk-R6A773WK.mjs} +19 -24
  68. package/dist/chunk-RF7PLG43.mjs +106 -0
  69. package/dist/chunk-RFBLQLLZ.mjs +16 -0
  70. package/dist/{chunk-Y3L5MDRV.mjs → chunk-RFVUDM5E.mjs} +17 -2
  71. package/dist/chunk-S7FL6UAH.mjs +521 -0
  72. package/dist/chunk-S7GNDRPS.mjs +651 -0
  73. package/dist/chunk-SP3NM3ZG.mjs +3537 -0
  74. package/dist/chunk-U476IXKQ.mjs +10 -0
  75. package/dist/chunk-UA3AUGNV.mjs +462 -0
  76. package/dist/chunk-UTG5YNXV.mjs +507 -0
  77. package/dist/chunk-VI4R6W25.mjs +8484 -0
  78. package/dist/chunk-WQBCBKY3.mjs +8 -0
  79. package/dist/{chunk-24RXHMEL.mjs → chunk-WVCOCKBU.mjs} +3794 -7570
  80. package/dist/chunk-X5CV2E2B.mjs +1 -0
  81. package/dist/chunk-XADZPPZI.mjs +614 -0
  82. package/dist/chunk-YEEQHT4V.mjs +212 -0
  83. package/dist/chunk-YTJCGKM4.mjs +56 -0
  84. package/dist/chunk-YYQIODMF.mjs +275 -0
  85. package/dist/chunk-ZI34TVOA.mjs +144 -0
  86. package/dist/{chunk-OOSIDHA3.mjs → chunk-ZKWYWW4Y.mjs} +1 -1
  87. package/dist/chunk-ZM7KU4YW.mjs +35 -0
  88. package/dist/{client-BQK6Tf6A.d.ts → client-BOStAkc5.d.ts} +160 -37
  89. package/dist/client.d.ts +11 -10
  90. package/dist/client.mjs +209 -45
  91. package/dist/{config-DmXULPa-.d.ts → config-Cjvzbs_j.d.ts} +51 -3
  92. package/dist/config-H6SGzbPH.d.ts +421 -0
  93. package/dist/direct-executor-ZD64PPEZ.mjs +24 -0
  94. package/dist/distribution/coordinator/etcd.d.ts +79 -18
  95. package/dist/distribution/coordinator/etcd.mjs +553 -109
  96. package/dist/distribution/coordinator/in-memory.d.ts +4 -3
  97. package/dist/distribution/coordinator/in-memory.mjs +22 -3
  98. package/dist/distribution/transport/grpc.d.ts +2 -2
  99. package/dist/distribution/transport/grpc.mjs +79 -14
  100. package/dist/distribution/transport/in-memory.d.ts +2 -2
  101. package/dist/distribution/transport/in-memory.mjs +12 -7
  102. package/dist/distribution/transport/tcp.d.ts +2 -2
  103. package/dist/distribution/transport/tcp.mjs +50 -28
  104. package/dist/distribution.d.ts +144 -23
  105. package/dist/distribution.mjs +14479 -8173
  106. package/dist/durability-wiring-FEUBR5BT.mjs +3330 -0
  107. package/dist/embeddings/openai.d.ts +2 -2
  108. package/dist/embeddings/openai.mjs +22 -133
  109. package/dist/{engine-CIOyFozK.d.ts → engine-rC0vjjY-.d.ts} +165 -141
  110. package/dist/{memory-yFaVn2xZ.d.ts → events-DqOIg5xa.d.ts} +645 -136
  111. package/dist/{types-DAK7mnUZ.d.ts → index-Bkdt8nDw.d.ts} +182 -45
  112. package/dist/index.browser.mjs +4 -32180
  113. package/dist/index.d.ts +91 -20
  114. package/dist/index.mjs +33 -16
  115. package/dist/invalidation/broadcast-channel.d.ts +1 -1
  116. package/dist/invalidation/filesystem.d.ts +1 -1
  117. package/dist/invalidation/filesystem.mjs +2 -1
  118. package/dist/invalidation/noop.d.ts +1 -1
  119. package/dist/module-3S65MVCM.mjs +2151 -0
  120. package/dist/partial-words-DTIHO8G4.d.ts +19 -0
  121. package/dist/partial-words.d.ts +22 -0
  122. package/dist/partial-words.mjs +17 -0
  123. package/dist/pattern-Cledccbl.d.ts +18 -0
  124. package/dist/pattern.d.ts +22 -0
  125. package/dist/pattern.mjs +12 -0
  126. package/dist/persistence/durability/checkpoint-worker.d.ts +119 -72
  127. package/dist/persistence/durability/checkpoint-worker.mjs +51 -18
  128. package/dist/react.d.ts +279 -183
  129. package/dist/react.mjs +201 -48
  130. package/dist/release-BH3DKW3T.mjs +33 -0
  131. package/dist/replace-CEO5KJJ3.mjs +57 -0
  132. package/dist/rerank-K7C4WZFG.mjs +60 -0
  133. package/dist/rerankers/http.d.ts +50 -0
  134. package/dist/rerankers/http.mjs +104 -0
  135. package/dist/restore-queue-KQHA7ZV2.mjs +35 -0
  136. package/dist/runtime-6Y4ZYXOG.mjs +177 -0
  137. package/dist/{schema-d5Afu03A.d.ts → schema-4W_7tk1f.d.ts} +81 -11
  138. package/dist/server.d.ts +13 -12
  139. package/dist/server.mjs +611 -80
  140. package/dist/snapshot-QGZPODM5.mjs +799 -0
  141. package/dist/synonyms-CpOAHPa-.d.ts +52 -0
  142. package/dist/synonyms.d.ts +22 -0
  143. package/dist/synonyms.mjs +12 -0
  144. package/dist/{tasks-CH2Dod8w.d.ts → tasks-CwPTz-tP.d.ts} +10 -3
  145. package/dist/thread-N3IUGUS3.mjs +697 -0
  146. package/dist/{types-BQDlIvfS.d.ts → types-2qoitTXg.d.ts} +1 -1
  147. package/dist/{types-D5OwhtsQ.d.ts → types-Cz5okLml.d.ts} +2 -2
  148. package/dist/vector/search-worker.d.ts +46 -4
  149. package/dist/vector/search-worker.mjs +21 -11
  150. package/dist/workers/entry.mjs +9 -6
  151. package/dist/write-U5RI7MAX.mjs +241 -0
  152. package/package.json +55 -9
  153. package/dist/chunk-3L4U6EJQ.mjs +0 -49
  154. package/dist/chunk-IFBKLBGB.mjs +0 -7
  155. package/dist/chunk-ORBLD3EL.mjs +0 -6
  156. package/dist/chunk-U7KYW4BA.mjs +0 -135
  157. package/dist/chunk-ZJ4CSAB2.mjs +0 -13
  158. package/dist/chunk-ZNZBXMP5.mjs +0 -2117
  159. package/dist/chunk-ZZT3EAUC.mjs +0 -11168
  160. package/dist/config-BhldZgaK.d.ts +0 -194
  161. package/dist/direct-executor-ALAVE7FC.mjs +0 -12
  162. package/dist/serialization/crc32-worker.d.ts +0 -20
  163. package/dist/serialization/crc32-worker.mjs +0 -62
  164. package/dist/simd-DW5AtJ6B.d.ts +0 -41
  165. package/dist/thread-DYHEQRVY.mjs +0 -402
@@ -0,0 +1,697 @@
1
+ import { loadUWebSockets, childAppOf, createRouteRunner, corsHeaderReader, createSharedBodyBudget, createSharedRequestGate, registerServerRoutes, sendRelayed, createDocumentReadHandlers, respondJson, SERVER_CAPABILITIES, versionReport, statusLine, sendError, serverRoutes, parseJson, validateSuggest, rejectInvalid, respondError, validateQuery, refuseRegex, withServerRerankWindow, awaitDepartureConfirmation, departureConfirmation, REQUEST_THREAD_DEPARTURE_DRAIN_MS, departureConfirmed, encodeJson } from './chunk-5JNA5SKS.mjs';
2
+ import { withRerankQueryText } from './chunk-KZ3NX2P7.mjs';
3
+ import './chunk-BOFBLJFZ.mjs';
4
+ import './chunk-DJHHF2L5.mjs';
5
+ import { executeListDocuments, executeSuggest, executePreflight, queryBindingOf, executeQuery } from './chunk-KWTW6VTG.mjs';
6
+ import { resolveProjection, projectionKeepsField, setNestedValue } from './chunk-VI4R6W25.mjs';
7
+ import './chunk-LPQOEU5M.mjs';
8
+ import './chunk-HJSN7UYG.mjs';
9
+ import './chunk-LXWDQLTW.mjs';
10
+ import './chunk-GBEEG5AN.mjs';
11
+ import './chunk-JZVARCRZ.mjs';
12
+ import './chunk-PC2G76VW.mjs';
13
+ import './chunk-WQBCBKY3.mjs';
14
+ import './chunk-OM362BRO.mjs';
15
+ import { extractVectorFieldsFromSchema } from './chunk-OEYI3KRB.mjs';
16
+ import './chunk-U476IXKQ.mjs';
17
+ import './chunk-E74YLAIG.mjs';
18
+ import './chunk-HMFNFVP7.mjs';
19
+ import './chunk-GES7U2PM.mjs';
20
+ import './chunk-LKOB5COA.mjs';
21
+ import './chunk-X5CV2E2B.mjs';
22
+ import './chunk-UTG5YNXV.mjs';
23
+ import './chunk-R6A773WK.mjs';
24
+ import './chunk-YTJCGKM4.mjs';
25
+ import './chunk-RFVUDM5E.mjs';
26
+ import './chunk-L5ORED7V.mjs';
27
+ import { ServerErrorCodes, ErrorCodes, NarsilError, isNarsilError } from './chunk-BWJVNZPR.mjs';
28
+
29
+ // src/server/request-threads/held-vectors.ts
30
+ function managerWithHeldVectors(manager, fieldPaths, vectorOf) {
31
+ function get(docId, projection) {
32
+ const document = manager.get(docId, projection);
33
+ if (document === void 0) return void 0;
34
+ for (const fieldPath of fieldPaths) {
35
+ if (projection !== void 0 && !projectionKeepsField(projection, fieldPath)) continue;
36
+ const vector = vectorOf(fieldPath, docId);
37
+ if (vector !== void 0)
38
+ setNestedValue(document, fieldPath, Float32Array.from(vector));
39
+ }
40
+ return document;
41
+ }
42
+ const reader = Object.create(manager);
43
+ reader.get = get;
44
+ return reader;
45
+ }
46
+
47
+ // src/server/request-threads/read-engine.ts
48
+ function createThreadReadEngine(options) {
49
+ const { executor, relay, searchHooks, patternWorkCap, maxRerankWindow } = options;
50
+ function vectorFieldsOf(context) {
51
+ return [...extractVectorFieldsFromSchema(context.config.schema).keys()];
52
+ }
53
+ function holdsEveryVectorField(indexName, context) {
54
+ return vectorFieldsOf(context).every((fieldPath) => executor.holdsVectorField(indexName, fieldPath));
55
+ }
56
+ function holdsEveryVectorFieldRead(indexName, context, params) {
57
+ if (params.document === false) return true;
58
+ const projection = resolveProjection(params.document === true ? void 0 : params.document);
59
+ return vectorFieldsOf(context).every(
60
+ (fieldPath) => !projectionKeepsField(projection, fieldPath) || executor.holdsVectorField(indexName, fieldPath)
61
+ );
62
+ }
63
+ function managerOf(indexName, context) {
64
+ const fieldPaths = vectorFieldsOf(context);
65
+ if (fieldPaths.length === 0) return context.manager;
66
+ return managerWithHeldVectors(
67
+ context.manager,
68
+ fieldPaths,
69
+ (fieldPath, docId) => executor.heldVectorOf(indexName, fieldPath, docId)
70
+ );
71
+ }
72
+ function heldContext(indexName) {
73
+ return executor.queryContextOf(indexName) ?? null;
74
+ }
75
+ function wholeDocumentsContext(indexName) {
76
+ const context = heldContext(indexName);
77
+ return context !== null && holdsEveryVectorField(indexName, context) ? context : null;
78
+ }
79
+ function requireAnswerable(indexName, context) {
80
+ if (context === null) {
81
+ throw new NarsilError(
82
+ ErrorCodes.INDEX_NOT_FOUND,
83
+ `This thread holds no copy of index "${indexName}" that answers this request yet`,
84
+ { indexName }
85
+ );
86
+ }
87
+ relay.touch(indexName);
88
+ return context;
89
+ }
90
+ function requireWholeDocuments(indexName) {
91
+ const context = requireAnswerable(indexName, wholeDocumentsContext(indexName));
92
+ return { context, manager: managerOf(indexName, context) };
93
+ }
94
+ function rerankRunnerFor(params) {
95
+ if (params.rerank === void 0) return void 0;
96
+ return {
97
+ maxWindow: maxRerankWindow,
98
+ score: (adapter, query, documents) => relay.rerank(adapter, query, documents)
99
+ };
100
+ }
101
+ async function resolvedParams(indexName, request) {
102
+ const params = withRerankQueryText(request);
103
+ if (params.vector?.text === void 0) return params;
104
+ const value = await relay.embed(indexName, params.vector);
105
+ const { text: _text, ...vector } = params.vector;
106
+ return { ...params, vector: { ...vector, value } };
107
+ }
108
+ function searchableContext(indexName, params) {
109
+ if (searchHooks) return null;
110
+ const context = executor.queryContextOf(indexName);
111
+ if (context === void 0) return null;
112
+ const field = params.vector?.field;
113
+ if (field === void 0) return context;
114
+ const vectorFields = extractVectorFieldsFromSchema(context.config.schema);
115
+ if (!vectorFields.has(field)) return context;
116
+ return context.vectorSearchers.has(field) && executor.holdsVectorField(indexName, field) ? context : null;
117
+ }
118
+ function answerableContext(indexName, params) {
119
+ const context = searchableContext(indexName, params);
120
+ return context !== null && holdsEveryVectorFieldRead(indexName, context, params) ? context : null;
121
+ }
122
+ return {
123
+ holdsCopyOf: (indexName) => heldContext(indexName) !== null,
124
+ holdsDocumentsOf: (indexName) => wholeDocumentsContext(indexName) !== null,
125
+ canAnswer: (indexName, params) => answerableContext(indexName, params) !== null,
126
+ canCount: (indexName, params) => searchableContext(indexName, params) !== null,
127
+ async query(indexName, params) {
128
+ const resolved = await resolvedParams(indexName, params);
129
+ const context = requireAnswerable(indexName, answerableContext(indexName, params));
130
+ const result = await executeQuery(resolved, {
131
+ manager: managerOf(indexName, context),
132
+ language: context.language,
133
+ config: context.config,
134
+ indexName,
135
+ cursorBinding: queryBindingOf(params),
136
+ vectorSearchers: context.vectorSearchers,
137
+ patternWorkCap,
138
+ rerank: rerankRunnerFor(params)
139
+ });
140
+ if (context.analysisStale) result.analysisStale = true;
141
+ return result;
142
+ },
143
+ async preflight(indexName, params) {
144
+ const resolved = await resolvedParams(indexName, params);
145
+ const context = requireAnswerable(indexName, searchableContext(indexName, params));
146
+ const result = await executePreflight(resolved, {
147
+ manager: context.manager,
148
+ language: context.language,
149
+ config: context.config,
150
+ indexName,
151
+ cursorBinding: queryBindingOf(params),
152
+ vectorSearchers: context.vectorSearchers,
153
+ patternWorkCap
154
+ });
155
+ if (context.analysisStale) result.analysisStale = true;
156
+ return result;
157
+ },
158
+ async suggest(indexName, params) {
159
+ const context = requireAnswerable(indexName, heldContext(indexName));
160
+ const result = executeSuggest(context.manager, context.language, params);
161
+ if (context.analysisStale) result.analysisStale = true;
162
+ return result;
163
+ },
164
+ async get(indexName, docId) {
165
+ return requireWholeDocuments(indexName).manager.get(docId);
166
+ },
167
+ async getMultiple(indexName, docIds) {
168
+ const { manager } = requireWholeDocuments(indexName);
169
+ const found = /* @__PURE__ */ new Map();
170
+ for (const docId of docIds) {
171
+ const document = manager.get(docId);
172
+ if (document !== void 0) found.set(docId, document);
173
+ }
174
+ return found;
175
+ },
176
+ async has(indexName, docId) {
177
+ return requireAnswerable(indexName, heldContext(indexName)).manager.has(docId);
178
+ },
179
+ async countDocuments(indexName) {
180
+ return requireAnswerable(indexName, heldContext(indexName)).manager.countDocuments();
181
+ },
182
+ async listDocuments(indexName, params) {
183
+ const { context, manager } = requireWholeDocuments(indexName);
184
+ return executeListDocuments(params ?? {}, {
185
+ manager,
186
+ schema: context.config.schema,
187
+ strict: context.config.strict,
188
+ patternWorkCap
189
+ });
190
+ }
191
+ };
192
+ }
193
+
194
+ // src/server/request-threads/relay-client.ts
195
+ function isRelayReply(message) {
196
+ if (typeof message !== "object" || message === null) return false;
197
+ const { type, requestId } = message;
198
+ return typeof requestId === "number" && (type === "response" || type === "authorized" || type === "embedded" || type === "reranked");
199
+ }
200
+ function isStreamPart(message) {
201
+ if (typeof message !== "object" || message === null) return false;
202
+ const { type, requestId } = message;
203
+ return typeof requestId === "number" && (type === "stream-start" || type === "stream-chunk" || type === "stream-end");
204
+ }
205
+ function writeStreamPart(port, writer, part) {
206
+ const { ctx } = writer;
207
+ if (part.type === "stream-end") {
208
+ if (!ctx.abort.aborted) {
209
+ ctx.res.cork(() => {
210
+ if (part.failed) ctx.res.close();
211
+ else ctx.res.end();
212
+ });
213
+ }
214
+ writer.finish();
215
+ return;
216
+ }
217
+ if (ctx.abort.aborted) return;
218
+ if (part.type === "stream-start") {
219
+ writer.started = true;
220
+ ctx.res.cork(() => {
221
+ ctx.res.writeStatus(statusLine(part.status));
222
+ for (const [key, value] of part.headers) ctx.res.writeHeader(key, value);
223
+ });
224
+ return;
225
+ }
226
+ let writable = true;
227
+ ctx.res.cork(() => {
228
+ writable = ctx.res.write(part.chunk);
229
+ });
230
+ const credit = () => port.postMessage({ type: "stream-credit", requestId: part.requestId });
231
+ if (writable) {
232
+ credit();
233
+ return;
234
+ }
235
+ ctx.res.onWritable(() => {
236
+ credit();
237
+ return true;
238
+ });
239
+ }
240
+ function relayedErrorOf(error, fallback) {
241
+ return error === null ? fallback : new NarsilError(error.code, error.message, error.details);
242
+ }
243
+ function portClosedError() {
244
+ return new NarsilError(ErrorCodes.WORKER_CRASHED, "The port to the main thread closed before it answered");
245
+ }
246
+ var SHUTTING_DOWN_DENIAL = {
247
+ allowed: false,
248
+ denial: { status: 503, code: ServerErrorCodes.INTERNAL_ERROR, message: "The server is shutting down" }
249
+ };
250
+ function relayedBody(rawBody) {
251
+ if (rawBody === null) return { bytes: null, moved: [] };
252
+ const memory = rawBody.buffer;
253
+ const ownsItsMemory = memory instanceof ArrayBuffer && rawBody.byteOffset === 0 && rawBody.byteLength === memory.byteLength;
254
+ if (!ownsItsMemory) return { bytes: new Uint8Array(rawBody), moved: [] };
255
+ return { bytes: new Uint8Array(memory), moved: [memory] };
256
+ }
257
+ function sendUnavailable(res) {
258
+ sendError(res, 503, ServerErrorCodes.INTERNAL_ERROR, "The server is shutting down");
259
+ }
260
+ function createRelayClient(port, touchIntervalMs) {
261
+ const pending = /* @__PURE__ */ new Map();
262
+ const touchedAt = /* @__PURE__ */ new Map();
263
+ let nextRequestId = 0;
264
+ let closed = false;
265
+ function failAll() {
266
+ closed = true;
267
+ const waiting = [...pending.values()];
268
+ pending.clear();
269
+ for (const entry of waiting) entry.reject(portClosedError());
270
+ }
271
+ const writers = /* @__PURE__ */ new Map();
272
+ port.on("message", (message) => {
273
+ if (isStreamPart(message)) {
274
+ const writer = writers.get(message.requestId);
275
+ if (writer !== void 0) writeStreamPart(port, writer, message);
276
+ return;
277
+ }
278
+ if (!isRelayReply(message)) return;
279
+ const waiting = pending.get(message.requestId);
280
+ if (waiting === void 0) return;
281
+ pending.delete(message.requestId);
282
+ writers.delete(message.requestId);
283
+ waiting.resolve(message);
284
+ });
285
+ port.on("close", failAll);
286
+ function send(build, moved = []) {
287
+ if (closed) return Promise.reject(portClosedError());
288
+ nextRequestId += 1;
289
+ const requestId = nextRequestId;
290
+ return new Promise((resolve, reject) => {
291
+ pending.set(requestId, { resolve, reject });
292
+ try {
293
+ port.postMessage(build(requestId), moved);
294
+ } catch (err) {
295
+ pending.delete(requestId);
296
+ reject(err instanceof Error ? err : new Error(String(err)));
297
+ }
298
+ });
299
+ }
300
+ return {
301
+ async authorize(context) {
302
+ let reply;
303
+ try {
304
+ reply = await send((requestId) => ({ type: "authorize", requestId, context }));
305
+ } catch {
306
+ return SHUTTING_DOWN_DENIAL;
307
+ }
308
+ if (reply.type !== "authorized") return { allowed: false, denial: null };
309
+ if (reply.hookError) return { allowed: false, denial: null };
310
+ if (reply.denial !== null) return { allowed: false, denial: reply.denial };
311
+ return { allowed: true };
312
+ },
313
+ async request(route, ctx) {
314
+ let reply;
315
+ const body = relayedBody(ctx.rawBody);
316
+ try {
317
+ reply = await new Promise((resolve, reject) => {
318
+ void send((requestId) => {
319
+ writers.set(requestId, {
320
+ ctx,
321
+ started: false,
322
+ finish: () => {
323
+ writers.delete(requestId);
324
+ pending.delete(requestId);
325
+ resolve(null);
326
+ }
327
+ });
328
+ ctx.abort.onAbort(() => port.postMessage({ type: "stream-cancel", requestId }));
329
+ return {
330
+ type: "request",
331
+ requestId,
332
+ route,
333
+ params: ctx.params,
334
+ query: ctx.query.toString(),
335
+ contentType: ctx.contentType,
336
+ rawBody: body.bytes,
337
+ spooledBody: ctx.spooledBody === null ? null : { path: ctx.spooledBody.path, bytes: ctx.spooledBody.bytes },
338
+ hookContext: ctx.hookContext
339
+ };
340
+ }, body.moved).then(resolve, reject);
341
+ });
342
+ } catch {
343
+ if (!ctx.abort.aborted) sendUnavailable(ctx.res);
344
+ return;
345
+ }
346
+ if (reply === null || reply.type !== "response" || ctx.abort.aborted) return;
347
+ sendRelayed(ctx.res, ctx.abort, reply.status, reply.headers, reply.body);
348
+ },
349
+ async embed(indexName, vector) {
350
+ const reply = await send((requestId) => ({ type: "embed", requestId, indexName, vector }));
351
+ if (reply.type === "embedded" && reply.value !== null) return reply.value;
352
+ throw relayedErrorOf(
353
+ reply.type === "embedded" ? reply.error : null,
354
+ new NarsilError(ErrorCodes.EMBEDDING_FAILED, "The main thread returned no vector")
355
+ );
356
+ },
357
+ async rerank(adapter, query, documents) {
358
+ const reply = await send((requestId) => ({ type: "rerank", requestId, adapter, query, documents }));
359
+ if (reply.type === "reranked" && reply.scores !== null) return reply.scores;
360
+ throw relayedErrorOf(
361
+ reply.type === "reranked" ? reply.error : null,
362
+ new NarsilError(ErrorCodes.RERANK_FAILED, "The main thread returned no scores")
363
+ );
364
+ },
365
+ touch(indexName) {
366
+ if (closed) return;
367
+ const now = Date.now();
368
+ const last = touchedAt.get(indexName);
369
+ if (last !== void 0 && now - last < touchIntervalMs) return;
370
+ touchedAt.set(indexName, now);
371
+ port.postMessage({ type: "touch", indexName });
372
+ },
373
+ announceDeparture() {
374
+ if (closed) return;
375
+ port.postMessage({ type: "leaving" });
376
+ },
377
+ close() {
378
+ failAll();
379
+ port.close();
380
+ }
381
+ };
382
+ }
383
+
384
+ // src/server/request-threads/thread-departure.ts
385
+ var DEPARTURE_REFUSAL = {
386
+ error: {
387
+ code: ErrorCodes.WORKER_CRASHED,
388
+ message: "The request thread that held this connection failed, so retry the request on a new connection"
389
+ }
390
+ };
391
+ var ThreadDepartureSignal = class extends Error {
392
+ };
393
+ var exitThread = process.exit.bind(process);
394
+ var endThread = threadEndOf(process);
395
+ var active = null;
396
+ var installed = false;
397
+ function threadEndOf(target) {
398
+ const found = Reflect.get(target, "reallyExit");
399
+ if (typeof found !== "function") return null;
400
+ return (code) => {
401
+ Reflect.apply(found, target, [code]);
402
+ };
403
+ }
404
+ function refuse(request) {
405
+ if (request.abort.aborted) return;
406
+ const payload = encodeJson(DEPARTURE_REFUSAL);
407
+ try {
408
+ request.res.cork(() => {
409
+ request.res.writeStatus(statusLine(503)).writeHeader("Content-Type", "application/json").writeHeader("Connection", "close").end(payload, true);
410
+ });
411
+ } catch {
412
+ }
413
+ }
414
+ function departedError() {
415
+ return new NarsilError(ErrorCodes.WORKER_CRASHED, "This request thread is leaving after a failure");
416
+ }
417
+ function crashWith(error) {
418
+ process.off("uncaughtException", leaveOnUncaughtError);
419
+ setImmediate(() => {
420
+ throw error;
421
+ });
422
+ }
423
+ function leaveOnUncaughtError(error) {
424
+ if (error instanceof ThreadDepartureSignal) return;
425
+ if (process.listenerCount("uncaughtException") > 1) return;
426
+ const leaving = active;
427
+ if (leaving === null) {
428
+ crashWith(error);
429
+ return;
430
+ }
431
+ leaving.begin();
432
+ leaving.finish(() => crashWith(error));
433
+ }
434
+ function exitAfterDeparture(code) {
435
+ const leaving = active;
436
+ if (leaving === null) return exitThread(code);
437
+ leaving.begin();
438
+ leaving.finish(() => exitThread(code));
439
+ throw new ThreadDepartureSignal("This request thread is leaving through process.exit");
440
+ }
441
+ function endAfterDeparture(end) {
442
+ return (code) => {
443
+ const leaving = active;
444
+ if (leaving === null) {
445
+ end(code);
446
+ return;
447
+ }
448
+ leaving.begin();
449
+ leaving.finish(() => end(code));
450
+ throw new ThreadDepartureSignal("This request thread is leaving through an exit that it held before it served");
451
+ };
452
+ }
453
+ function leaveAtExit() {
454
+ active?.leaveAtExit();
455
+ }
456
+ function install() {
457
+ if (installed) return;
458
+ installed = true;
459
+ process.exit = exitAfterDeparture;
460
+ if (endThread !== null) Reflect.set(process, "reallyExit", endAfterDeparture(endThread));
461
+ process.on("uncaughtException", leaveOnUncaughtError);
462
+ process.on("exit", leaveAtExit);
463
+ }
464
+ function watchForDeparture(options) {
465
+ const { relay, departure, close } = options;
466
+ const running = /* @__PURE__ */ new Set();
467
+ let departing = false;
468
+ let finishing = false;
469
+ function closeOnce() {
470
+ if (active === state) active = null;
471
+ close();
472
+ }
473
+ const state = {
474
+ begin() {
475
+ if (departing) return;
476
+ departing = true;
477
+ for (const request of running) refuse(request);
478
+ if (!departureConfirmed(departure)) relay.announceDeparture();
479
+ },
480
+ finish(exit) {
481
+ if (finishing) return;
482
+ finishing = true;
483
+ departureConfirmation(departure).then(() => new Promise((resolve) => setTimeout(resolve, REQUEST_THREAD_DEPARTURE_DRAIN_MS))).catch(() => void 0).then(() => {
484
+ closeOnce();
485
+ exit();
486
+ });
487
+ },
488
+ leaveAtExit() {
489
+ state.begin();
490
+ if (endThread !== null) return;
491
+ awaitDepartureConfirmation(departure);
492
+ closeOnce();
493
+ }
494
+ };
495
+ active = state;
496
+ install();
497
+ function refusingOnceDeparting(call) {
498
+ return (...args) => {
499
+ if (departing) throw departedError();
500
+ return call(...args);
501
+ };
502
+ }
503
+ return {
504
+ track(res, abort) {
505
+ const request = { res, abort };
506
+ if (departing) {
507
+ refuse(request);
508
+ return () => void 0;
509
+ }
510
+ running.add(request);
511
+ return () => {
512
+ running.delete(request);
513
+ };
514
+ },
515
+ guardRoute(handler) {
516
+ return async (ctx) => {
517
+ if (departing) {
518
+ refuse(ctx);
519
+ return;
520
+ }
521
+ try {
522
+ await handler(ctx);
523
+ } catch (err) {
524
+ if (!departing) throw err;
525
+ }
526
+ };
527
+ },
528
+ guardExecutor(executor) {
529
+ return {
530
+ getManager: refusingOnceDeparting((indexName) => executor.getManager(indexName)),
531
+ queryContextOf: refusingOnceDeparting((indexName) => executor.queryContextOf(indexName)),
532
+ holdsVectorField: refusingOnceDeparting(
533
+ (indexName, fieldName) => executor.holdsVectorField(indexName, fieldName)
534
+ ),
535
+ heldVectorOf: refusingOnceDeparting(
536
+ (indexName, fieldName, docId) => executor.heldVectorOf(indexName, fieldName, docId)
537
+ ),
538
+ createIndex: refusingOnceDeparting(
539
+ (indexName, config, language, analysisStale, synonyms) => executor.createIndex(indexName, config, language, analysisStale, synonyms)
540
+ ),
541
+ dropIndex: refusingOnceDeparting((indexName) => executor.dropIndex(indexName)),
542
+ listIndexes: refusingOnceDeparting(() => executor.listIndexes())
543
+ };
544
+ }
545
+ };
546
+ }
547
+ function leaveWithoutDeparture() {
548
+ active = null;
549
+ }
550
+
551
+ // src/server/request-threads/thread.ts
552
+ var servingApp = null;
553
+ function guardEveryRoute(handlers, departure) {
554
+ const guarded = { ...handlers };
555
+ for (const route of Object.keys(guarded)) {
556
+ guarded[route] = departure.guardRoute(handlers[route]);
557
+ }
558
+ return guarded;
559
+ }
560
+ function relayed(relay, route) {
561
+ return (ctx) => relay.request(route, ctx);
562
+ }
563
+ function onHeldIndex(local, handler, fallback) {
564
+ return (ctx) => local.holdsCopyOf(ctx.params[0]) ? handler(ctx) : fallback(ctx);
565
+ }
566
+ function onWholeDocuments(local, handler, fallback) {
567
+ return (ctx) => local.holdsDocumentsOf(ctx.params[0]) ? handler(ctx) : fallback(ctx);
568
+ }
569
+ function mainThreadAnswersInstead(err) {
570
+ return isNarsilError(err) && err.code === ErrorCodes.WORKER_BUSY;
571
+ }
572
+ function searchHandler(options) {
573
+ const { fallback, limits, allowRegex, staysOnThread, answer } = options;
574
+ return async (ctx) => {
575
+ const params = parseJson(ctx);
576
+ if (!params) return;
577
+ const failure = validateQuery(params, limits);
578
+ if (failure) {
579
+ rejectInvalid(ctx, failure);
580
+ return;
581
+ }
582
+ if (refuseRegex(ctx, params.filters, allowRegex)) return;
583
+ if (!staysOnThread(ctx.params[0], params)) {
584
+ await fallback(ctx);
585
+ return;
586
+ }
587
+ try {
588
+ respondJson(ctx, await answer(ctx.params[0], withServerRerankWindow(params, limits)));
589
+ } catch (err) {
590
+ if (staysOnThread(ctx.params[0], params) && !mainThreadAnswersInstead(err)) respondError(ctx, err);
591
+ else await fallback(ctx);
592
+ }
593
+ };
594
+ }
595
+ function suggestHandler(local, fallback) {
596
+ return async (ctx) => {
597
+ const params = parseJson(ctx);
598
+ if (!params) return;
599
+ const failure = validateSuggest(params);
600
+ if (failure) {
601
+ rejectInvalid(ctx, failure);
602
+ return;
603
+ }
604
+ if (!local.holdsCopyOf(ctx.params[0])) {
605
+ await fallback(ctx);
606
+ return;
607
+ }
608
+ try {
609
+ respondJson(ctx, await local.suggest(ctx.params[0], params));
610
+ } catch (err) {
611
+ respondError(ctx, err);
612
+ }
613
+ };
614
+ }
615
+ var THREAD_ONLY_ROUTES = /* @__PURE__ */ new Set(["livez", "version", "capabilities"]);
616
+ function isRelayedRoute(handler) {
617
+ return !THREAD_ONLY_ROUTES.has(handler);
618
+ }
619
+ function relayedHandlers(relay, limits) {
620
+ const forwarded = {};
621
+ for (const route of serverRoutes(limits)) {
622
+ if (isRelayedRoute(route.handler)) forwarded[route.handler] = relayed(relay, route.handler);
623
+ }
624
+ return forwarded;
625
+ }
626
+ function threadHandlers(local, relay, settings) {
627
+ const { allowRegex } = settings;
628
+ const reads = createDocumentReadHandlers({ engine: local, limits: settings.limits, allowRegex });
629
+ const forwarded = relayedHandlers(relay, settings.limits);
630
+ return {
631
+ ...forwarded,
632
+ livez: (ctx) => respondJson(ctx, { status: "ok" }),
633
+ version: (ctx) => respondJson(ctx, versionReport(settings.build)),
634
+ capabilities: (ctx) => respondJson(ctx, { capabilities: [...SERVER_CAPABILITIES] }),
635
+ search: searchHandler({
636
+ fallback: forwarded.search,
637
+ limits: settings.limits,
638
+ allowRegex,
639
+ staysOnThread: (name, params) => local.canAnswer(name, params),
640
+ answer: (name, params) => local.query(name, params)
641
+ }),
642
+ preflight: searchHandler({
643
+ fallback: forwarded.preflight,
644
+ limits: settings.limits,
645
+ allowRegex,
646
+ staysOnThread: (name, params) => local.canCount(name, params),
647
+ answer: (name, params) => local.preflight(name, params)
648
+ }),
649
+ suggest: suggestHandler(local, forwarded.suggest),
650
+ get: onWholeDocuments(local, reads.get, forwarded.get),
651
+ exists: onHeldIndex(local, reads.exists, forwarded.exists),
652
+ count: onHeldIndex(local, reads.count, forwarded.count),
653
+ list: onWholeDocuments(local, reads.list, forwarded.list),
654
+ multiGet: onWholeDocuments(local, reads.multiGet, forwarded.multiGet)
655
+ };
656
+ }
657
+ async function serveRequests(executor, settings) {
658
+ if (servingApp !== null) throw new Error("This worker already serves requests");
659
+ const uws = await loadUWebSockets();
660
+ const app = uws.App();
661
+ const child = childAppOf(app);
662
+ if (child === null) throw new Error("This uWebSockets.js build offers no child apps");
663
+ const relay = createRelayClient(settings.port, settings.touchIntervalMs);
664
+ const departure = watchForDeparture({
665
+ relay,
666
+ departure: new Int32Array(settings.departure),
667
+ close: closeRequestThread
668
+ });
669
+ const local = createThreadReadEngine({
670
+ executor: departure.guardExecutor(executor),
671
+ relay,
672
+ searchHooks: settings.searchHooks,
673
+ patternWorkCap: settings.patternWorkCap,
674
+ maxRerankWindow: settings.maxRerankWindow
675
+ });
676
+ const run = createRouteRunner({
677
+ authorize: settings.authorizes ? (context) => relay.authorize(context) : void 0,
678
+ gate: createSharedRequestGate(settings.gate, settings.maxConcurrentRequests, settings.gateSlot),
679
+ bodyBudget: createSharedBodyBudget(settings.bodyBudget, settings.limits.maxBodyBytesInFlight, settings.gateSlot),
680
+ corsHeaders: settings.cors ? corsHeaderReader(settings.cors) : void 0,
681
+ track: departure.track
682
+ });
683
+ const handlers = guardEveryRoute(threadHandlers(local, relay, settings), departure);
684
+ registerServerRoutes(app, run, handlers, settings.limits, settings.cors);
685
+ servingApp = { app: child, relay };
686
+ return { descriptor: child.getDescriptor() };
687
+ }
688
+ function closeRequestThread() {
689
+ leaveWithoutDeparture();
690
+ if (servingApp === null) return;
691
+ const { app, relay } = servingApp;
692
+ servingApp = null;
693
+ app.close();
694
+ relay.close();
695
+ }
696
+
697
+ export { closeRequestThread, serveRequests };
@@ -1,4 +1,4 @@
1
- import { T as TransportConfig } from './types-D5OwhtsQ.js';
1
+ import { T as TransportConfig } from './types-Cz5okLml.js';
2
2
 
3
3
  /**
4
4
  * The certificate material one node presents to its peers.