@delali/narsil 0.2.2 → 0.2.3-next.33

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 (66) hide show
  1. package/README.md +12 -9
  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-CVqI5NBt.d.ts → adapters-79OIa4xm.d.ts} +1 -1
  8. package/dist/brute-force-DbHYCTc3.d.ts +8 -0
  9. package/dist/chunk-BFIH7OV5.mjs +185 -0
  10. package/dist/chunk-C6VVLMRX.mjs +63 -0
  11. package/dist/{chunk-UNJZ7ZDB.mjs → chunk-EMI63EXP.mjs} +33 -2
  12. package/dist/chunk-MFFAJ2NR.mjs +135 -0
  13. package/dist/chunk-OBXS7OZM.mjs +376 -0
  14. package/dist/{chunk-ASI6AOW2.mjs → chunk-RO4EFWE6.mjs} +7833 -3524
  15. package/dist/{chunk-WLR5BFL3.mjs → chunk-TEAJO7CU.mjs} +3067 -2087
  16. package/dist/{chunk-S7RJJMHI.mjs → chunk-WDL632CN.mjs} +699 -222
  17. package/dist/{chunk-BKAI2WXI.mjs → chunk-WTONLDU3.mjs} +5 -0
  18. package/dist/client-DQHB_swQ.d.ts +793 -0
  19. package/dist/client.d.ts +6 -0
  20. package/dist/client.mjs +843 -0
  21. package/dist/{config-DXypXAfN.d.ts → config-ChZ7lfqc.d.ts} +3 -3
  22. package/dist/direct-executor-YARCDMJF.mjs +8 -0
  23. package/dist/distribution/coordinator/etcd.d.ts +3 -3
  24. package/dist/distribution/coordinator/etcd.mjs +471 -2
  25. package/dist/distribution/coordinator/in-memory.d.ts +3 -3
  26. package/dist/distribution/coordinator/in-memory.mjs +286 -1
  27. package/dist/distribution/transport/grpc.d.ts +45 -0
  28. package/dist/distribution/transport/grpc.mjs +586 -0
  29. package/dist/distribution/transport/in-memory.d.ts +118 -0
  30. package/dist/distribution/transport/in-memory.mjs +214 -0
  31. package/dist/distribution/transport/tcp.d.ts +27 -0
  32. package/dist/distribution/transport/tcp.mjs +595 -0
  33. package/dist/distribution.d.ts +241 -192
  34. package/dist/distribution.mjs +6620 -4795
  35. package/dist/embeddings/openai.d.ts +1 -1
  36. package/dist/embeddings/openai.mjs +1 -1
  37. package/dist/{engine-CO9Wnfie.d.ts → engine-DScGlT2Z.d.ts} +30 -3
  38. package/dist/index.browser.mjs +10035 -3914
  39. package/dist/index.d.ts +8 -8
  40. package/dist/index.mjs +8 -5
  41. package/dist/invalidation/broadcast-channel.d.ts +1 -1
  42. package/dist/invalidation/filesystem.d.ts +1 -1
  43. package/dist/invalidation/filesystem.mjs +3 -1
  44. package/dist/invalidation/noop.d.ts +1 -1
  45. package/dist/options-BORJg5XB.d.ts +234 -0
  46. package/dist/persistence/durability/checkpoint-worker.d.ts +4 -2
  47. package/dist/persistence/durability/checkpoint-worker.mjs +8 -5
  48. package/dist/react.d.ts +340 -0
  49. package/dist/react.mjs +687 -0
  50. package/dist/{schema-CE6S_aAr.d.ts → schema-emTQfEiX.d.ts} +21 -7
  51. package/dist/{search-DLBtiPdQ.d.ts → search-CQ-9bKxo.d.ts} +226 -14
  52. package/dist/server.d.ts +42 -356
  53. package/dist/server.mjs +493 -106
  54. package/dist/tasks-CnMLpfk-.d.ts +274 -0
  55. package/dist/types-BQDlIvfS.d.ts +34 -0
  56. package/dist/{types-BV3nAKTL.d.ts → types-Bfhhnh3G.d.ts} +23 -1
  57. package/dist/types-D5OwhtsQ.d.ts +97 -0
  58. package/dist/vector/hnsw-build-worker.d.ts +2 -7
  59. package/dist/vector/hnsw-build-worker.mjs +2 -1
  60. package/dist/vector/search-worker.d.ts +373 -0
  61. package/dist/vector/search-worker.mjs +279 -0
  62. package/dist/workers/entry.mjs +43 -25
  63. package/package.json +42 -2
  64. package/dist/chunk-FKK76OR7.mjs +0 -456
  65. package/dist/chunk-RQLXC2YS.mjs +0 -269
  66. package/dist/direct-executor-3W4QRA4E.mjs +0 -5
@@ -1 +1,286 @@
1
- export { createInMemoryCoordinator } from '../../chunk-RQLXC2YS.mjs';
1
+ import { compareCodePoints } from '../../chunk-BFIH7OV5.mjs';
2
+
3
+ // src/distribution/coordinator/in-memory.ts
4
+ var NODE_HEARTBEAT_PREFIX = "_narsil/node/";
5
+ function bytesEqual(a, b) {
6
+ if (a.byteLength !== b.byteLength) {
7
+ return false;
8
+ }
9
+ for (let i = 0; i < a.byteLength; i++) {
10
+ if (a[i] !== b[i]) {
11
+ return false;
12
+ }
13
+ }
14
+ return true;
15
+ }
16
+ function createInMemoryCoordinator() {
17
+ const nodes = /* @__PURE__ */ new Map();
18
+ const allocations = /* @__PURE__ */ new Map();
19
+ const partitionStates = /* @__PURE__ */ new Map();
20
+ const leases = /* @__PURE__ */ new Map();
21
+ const schemas = /* @__PURE__ */ new Map();
22
+ const kvStore = /* @__PURE__ */ new Map();
23
+ const nodeWatchers = /* @__PURE__ */ new Set();
24
+ const allocationWatchers = /* @__PURE__ */ new Set();
25
+ const schemaWatchers = /* @__PURE__ */ new Set();
26
+ let isShutdown = false;
27
+ function assertNotShutdown() {
28
+ if (isShutdown) {
29
+ throw new Error("Coordinator has been shut down");
30
+ }
31
+ }
32
+ function emitNodeEvent(event) {
33
+ for (const handler of nodeWatchers) {
34
+ try {
35
+ handler(event);
36
+ } catch (_) {
37
+ }
38
+ }
39
+ }
40
+ function emitAllocationEvent(event) {
41
+ for (const handler of allocationWatchers) {
42
+ try {
43
+ handler(event);
44
+ } catch (_) {
45
+ }
46
+ }
47
+ }
48
+ function emitSchemaEvent(event) {
49
+ for (const handler of schemaWatchers) {
50
+ try {
51
+ handler(event);
52
+ } catch (_) {
53
+ }
54
+ }
55
+ }
56
+ function clearLeaseEntry(key) {
57
+ const entry = leases.get(key);
58
+ if (entry !== void 0) {
59
+ clearTimeout(entry.timer);
60
+ leases.delete(key);
61
+ }
62
+ }
63
+ function createLeaseTimer(key, ttlMs) {
64
+ return setTimeout(() => {
65
+ leases.delete(key);
66
+ if (key.startsWith(NODE_HEARTBEAT_PREFIX)) {
67
+ const expiredNodeId = key.slice(NODE_HEARTBEAT_PREFIX.length);
68
+ nodes.delete(expiredNodeId);
69
+ emitNodeEvent({ type: "node_left", nodeId: expiredNodeId, registration: null });
70
+ }
71
+ }, ttlMs);
72
+ }
73
+ function isLeaseValid(key) {
74
+ const entry = leases.get(key);
75
+ if (entry === void 0) {
76
+ return false;
77
+ }
78
+ return entry.expiresAt > Date.now();
79
+ }
80
+ const coordinator = {
81
+ async registerNode(registration) {
82
+ assertNotShutdown();
83
+ nodes.set(registration.nodeId, registration);
84
+ const heartbeatKey = `${NODE_HEARTBEAT_PREFIX}${registration.nodeId}`;
85
+ const defaultTtlMs = 3e4;
86
+ clearLeaseEntry(heartbeatKey);
87
+ const timer = createLeaseTimer(heartbeatKey, defaultTtlMs);
88
+ leases.set(heartbeatKey, {
89
+ nodeId: registration.nodeId,
90
+ expiresAt: Date.now() + defaultTtlMs,
91
+ timer
92
+ });
93
+ emitNodeEvent({ type: "node_joined", nodeId: registration.nodeId, registration });
94
+ },
95
+ async deregisterNode(nodeId) {
96
+ assertNotShutdown();
97
+ const existed = nodes.delete(nodeId);
98
+ const heartbeatKey = `${NODE_HEARTBEAT_PREFIX}${nodeId}`;
99
+ clearLeaseEntry(heartbeatKey);
100
+ if (existed) {
101
+ emitNodeEvent({ type: "node_left", nodeId, registration: null });
102
+ }
103
+ },
104
+ async listNodes() {
105
+ assertNotShutdown();
106
+ return Array.from(nodes.values());
107
+ },
108
+ async watchNodes(handler) {
109
+ assertNotShutdown();
110
+ nodeWatchers.add(handler);
111
+ return () => {
112
+ nodeWatchers.delete(handler);
113
+ };
114
+ },
115
+ async getAllocation(indexName) {
116
+ assertNotShutdown();
117
+ return allocations.get(indexName) ?? null;
118
+ },
119
+ async putAllocation(indexName, table, expectedVersion) {
120
+ assertNotShutdown();
121
+ if (expectedVersion !== void 0) {
122
+ const current = allocations.get(indexName);
123
+ if (expectedVersion === null) {
124
+ if (current !== void 0) {
125
+ return false;
126
+ }
127
+ } else {
128
+ if (current === void 0 || current.version !== expectedVersion) {
129
+ return false;
130
+ }
131
+ }
132
+ }
133
+ allocations.set(indexName, table);
134
+ emitAllocationEvent({ indexName, table });
135
+ return true;
136
+ },
137
+ async deleteAllocation(indexName) {
138
+ assertNotShutdown();
139
+ allocations.delete(indexName);
140
+ },
141
+ async watchAllocation(handler) {
142
+ assertNotShutdown();
143
+ allocationWatchers.add(handler);
144
+ return () => {
145
+ allocationWatchers.delete(handler);
146
+ };
147
+ },
148
+ async getPartitionState(indexName, partitionId) {
149
+ assertNotShutdown();
150
+ const indexStates = partitionStates.get(indexName);
151
+ if (indexStates === void 0) {
152
+ return "UNASSIGNED";
153
+ }
154
+ return indexStates.get(partitionId) ?? "UNASSIGNED";
155
+ },
156
+ async putPartitionState(indexName, partitionId, state) {
157
+ assertNotShutdown();
158
+ let indexStates = partitionStates.get(indexName);
159
+ if (indexStates === void 0) {
160
+ indexStates = /* @__PURE__ */ new Map();
161
+ partitionStates.set(indexName, indexStates);
162
+ }
163
+ indexStates.set(partitionId, state);
164
+ },
165
+ async acquireLease(key, nodeId, ttlMs) {
166
+ assertNotShutdown();
167
+ if (isLeaseValid(key)) {
168
+ const entry = leases.get(key);
169
+ if (entry !== void 0 && entry.nodeId !== nodeId) {
170
+ return false;
171
+ }
172
+ if (entry !== void 0 && entry.nodeId === nodeId) {
173
+ clearTimeout(entry.timer);
174
+ const timer2 = createLeaseTimer(key, ttlMs);
175
+ entry.expiresAt = Date.now() + ttlMs;
176
+ entry.timer = timer2;
177
+ return true;
178
+ }
179
+ }
180
+ clearLeaseEntry(key);
181
+ const timer = createLeaseTimer(key, ttlMs);
182
+ leases.set(key, { nodeId, expiresAt: Date.now() + ttlMs, timer });
183
+ return true;
184
+ },
185
+ async renewLease(key, nodeId, ttlMs) {
186
+ assertNotShutdown();
187
+ const entry = leases.get(key);
188
+ if (entry === void 0 || entry.nodeId !== nodeId) {
189
+ return false;
190
+ }
191
+ if (!isLeaseValid(key)) {
192
+ leases.delete(key);
193
+ return false;
194
+ }
195
+ clearTimeout(entry.timer);
196
+ entry.timer = createLeaseTimer(key, ttlMs);
197
+ entry.expiresAt = Date.now() + ttlMs;
198
+ return true;
199
+ },
200
+ async releaseLease(key) {
201
+ assertNotShutdown();
202
+ clearLeaseEntry(key);
203
+ },
204
+ async get(key) {
205
+ assertNotShutdown();
206
+ const value = kvStore.get(key);
207
+ return value !== void 0 ? new Uint8Array(value) : null;
208
+ },
209
+ async compareAndSet(key, expected, value) {
210
+ assertNotShutdown();
211
+ const current = kvStore.get(key);
212
+ if (expected === null) {
213
+ if (current !== void 0) {
214
+ return false;
215
+ }
216
+ kvStore.set(key, new Uint8Array(value));
217
+ return true;
218
+ }
219
+ if (current === void 0) {
220
+ return false;
221
+ }
222
+ if (!bytesEqual(current, expected)) {
223
+ return false;
224
+ }
225
+ kvStore.set(key, new Uint8Array(value));
226
+ return true;
227
+ },
228
+ async getSchema(indexName) {
229
+ assertNotShutdown();
230
+ return schemas.get(indexName) ?? null;
231
+ },
232
+ async putSchema(indexName, schema) {
233
+ assertNotShutdown();
234
+ schemas.set(indexName, schema);
235
+ emitSchemaEvent({ type: "schema_created", indexName, schema });
236
+ },
237
+ async dropSchema(indexName) {
238
+ assertNotShutdown();
239
+ if (!schemas.delete(indexName)) {
240
+ return;
241
+ }
242
+ emitSchemaEvent({ type: "schema_dropped", indexName, schema: null });
243
+ },
244
+ async listSchemas() {
245
+ assertNotShutdown();
246
+ return Array.from(schemas.keys()).sort(compareCodePoints);
247
+ },
248
+ async watchSchemas(handler) {
249
+ assertNotShutdown();
250
+ schemaWatchers.add(handler);
251
+ return () => {
252
+ schemaWatchers.delete(handler);
253
+ };
254
+ },
255
+ async getLeaseHolder(key) {
256
+ assertNotShutdown();
257
+ if (!isLeaseValid(key)) {
258
+ clearLeaseEntry(key);
259
+ return null;
260
+ }
261
+ const entry = leases.get(key);
262
+ return entry?.nodeId ?? null;
263
+ },
264
+ async shutdown() {
265
+ if (isShutdown) {
266
+ return;
267
+ }
268
+ isShutdown = true;
269
+ for (const entry of leases.values()) {
270
+ clearTimeout(entry.timer);
271
+ }
272
+ leases.clear();
273
+ nodes.clear();
274
+ allocations.clear();
275
+ partitionStates.clear();
276
+ schemas.clear();
277
+ kvStore.clear();
278
+ nodeWatchers.clear();
279
+ allocationWatchers.clear();
280
+ schemaWatchers.clear();
281
+ }
282
+ };
283
+ return coordinator;
284
+ }
285
+
286
+ export { createInMemoryCoordinator };
@@ -0,0 +1,45 @@
1
+ import { T as TransportConfig, N as NodeTransport } from '../../types-D5OwhtsQ.js';
2
+ import { a as TlsConfig } from '../../types-BQDlIvfS.js';
3
+
4
+ /**
5
+ * How the gRPC transport listens and dials.
6
+ *
7
+ * @public
8
+ */
9
+ interface GrpcTransportConfig extends TransportConfig {
10
+ /** The transport listens on this host. */
11
+ host: string;
12
+ /** The transport listens on this port, and 0 asks the system for a free one. */
13
+ port: number;
14
+ /** Presenting this makes every connection mutually authenticated TLS. */
15
+ tls?: TlsConfig;
16
+ }
17
+
18
+ /**
19
+ * Builds the gRPC transport a cluster node reaches its peers through,
20
+ * implementing the `narsil.transport.v1.NodeTransport` service from the
21
+ * cross-language specification.
22
+ *
23
+ * Each call carries one MessagePack-serialised message in a protobuf
24
+ * envelope, so a node written in another language can join the same cluster
25
+ * through its own gRPC stack. The transport listens on `config.host` and
26
+ * `config.port`, and it dials a peer at the `host:port` address that peer
27
+ * registered with the coordinator. Passing `config.tls` makes every
28
+ * connection mutually authenticated: the server requires a client
29
+ * certificate and both sides verify against the configured authority.
30
+ *
31
+ * The `@grpc/grpc-js` package is an optional peer dependency, and calling
32
+ * this without it installed throws a `NarsilError` with the code
33
+ * `TRANSPORT_DEPENDENCY_MISSING`.
34
+ *
35
+ * @param nodeId - The node this transport belongs to, used in error reports.
36
+ * @param config - The listen address, the timeouts, and the TLS material.
37
+ * @returns The transport, with `getPort` for reading the bound port.
38
+ *
39
+ * @public
40
+ */
41
+ declare function createGrpcTransport(nodeId: string, config?: Partial<GrpcTransportConfig>): Promise<NodeTransport & {
42
+ getPort(): number;
43
+ }>;
44
+
45
+ export { type GrpcTransportConfig, TlsConfig, createGrpcTransport };