@delali/narsil 0.3.0 → 0.3.1-next.101

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-DiGlB6Sr.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-DffQlmRq.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
@@ -1,13 +1,24 @@
1
- import { MAX_NODE_ID_LENGTH, MAX_WATCHERS } from '../../chunk-ORBLD3EL.mjs';
2
- import { compareCodePoints } from '../../chunk-YMZCMH5M.mjs';
3
- import { NarsilError, ErrorCodes } from '../../chunk-USKFU7J5.mjs';
4
- import { decode, encode } from '@msgpack/msgpack';
1
+ import { MAX_NODE_ID_LENGTH, CONTROLLER_LEASE_KEY, MAX_WATCHERS } from '../../chunk-44W3CBNG.mjs';
2
+ import '../../chunk-R6A773WK.mjs';
3
+ import { compareCodePoints } from '../../chunk-YTJCGKM4.mjs';
4
+ import { NarsilError, ErrorCodes, describeError } from '../../chunk-BWJVNZPR.mjs';
5
+ import { encode, decode } from '@msgpack/msgpack';
6
+
7
+ // src/distribution/coordinator/etcd/constants.ts
8
+ var DEFAULT_ETCD_MAX_REQUEST_BYTES = 1572864;
9
+ var MAX_ETCD_REQUEST_BYTES = 2147483647;
10
+ var ETCD_REQUEST_FRAMING_BYTES = 1024;
11
+ var ETCD_MAX_RESPONSE_BYTES = 2147483647;
12
+ var RECONNECT_FIRST_DELAY_MS = 100;
13
+ var RECONNECT_LONGEST_DELAY_MS = 5e3;
14
+ var ETCD_START_PROBE_TIMEOUT_MS = 1e4;
5
15
 
6
16
  // src/distribution/coordinator/etcd/types.ts
7
17
  var DEFAULT_ETCD_CONFIG = {
8
18
  endpoints: ["http://localhost:2379"],
9
19
  keyPrefix: "_narsil",
10
- nodeHeartbeatTtlSeconds: 30
20
+ nodeHeartbeatTtlSeconds: 30,
21
+ maxRequestBytes: DEFAULT_ETCD_MAX_REQUEST_BYTES
11
22
  };
12
23
  var ETCD_KEY_NODES = "nodes";
13
24
  var ETCD_KEY_ALLOCATION = "allocation";
@@ -38,6 +49,42 @@ function extractSuffix(fullKey, prefix) {
38
49
  return fullKey.slice(prefix.length + 1);
39
50
  }
40
51
 
52
+ // src/distribution/coordinator/etcd/lease-validity.ts
53
+ var MILLISECONDS_PER_SECOND = 1e3;
54
+ var validities = /* @__PURE__ */ new WeakMap();
55
+ function recordLeaseRenewal(lease, sentAt, ttlSeconds) {
56
+ if (!Number.isFinite(ttlSeconds) || ttlSeconds <= 0) return;
57
+ const ttlMs = ttlSeconds * MILLISECONDS_PER_SECOND;
58
+ const heldUntil = sentAt + ttlMs;
59
+ const current = validities.get(lease);
60
+ if (current !== void 0 && current.heldUntil >= heldUntil) return;
61
+ validities.set(lease, { heldUntil, ttlMs });
62
+ }
63
+ function followLeaseRenewals(lease) {
64
+ const unanswered = [];
65
+ lease.on("keepaliveFired", () => {
66
+ unanswered.push(performance.now());
67
+ });
68
+ lease.on("keepaliveSucceeded", (response) => {
69
+ const sentAt = unanswered.shift();
70
+ if (sentAt !== void 0) recordLeaseRenewal(lease, sentAt, Number(response.TTL));
71
+ });
72
+ lease.on("keepaliveEstablished", () => {
73
+ unanswered.length = 0;
74
+ });
75
+ lease.on("keepaliveFailed", () => {
76
+ unanswered.length = 0;
77
+ });
78
+ lease.once("lost", () => {
79
+ validities.delete(lease);
80
+ });
81
+ }
82
+ function leaseHeldFor(lease) {
83
+ const validity = validities.get(lease);
84
+ if (validity === void 0 || lease.revoked()) return null;
85
+ return { remainingMs: validity.heldUntil - performance.now(), ttlMs: validity.ttlMs };
86
+ }
87
+
41
88
  // src/distribution/coordinator/etcd/leases.ts
42
89
  function payloadsMatch(left, right) {
43
90
  if (left === null || left.byteLength !== right.byteLength) {
@@ -52,9 +99,17 @@ function payloadsMatch(left, right) {
52
99
  }
53
100
  var LeaseManager = class {
54
101
  leases = /* @__PURE__ */ new Map();
102
+ holders = /* @__PURE__ */ new Map();
55
103
  keyLocks = /* @__PURE__ */ new Map();
56
104
  track(key, lease, nodeId, payload = null) {
57
105
  this.leases.set(key, { lease, nodeId, key, payload });
106
+ this.holders.set(key, lease);
107
+ }
108
+ holder(key) {
109
+ return this.holders.get(key);
110
+ }
111
+ forgetHolder(key) {
112
+ this.holders.delete(key);
58
113
  }
59
114
  get(key) {
60
115
  return this.leases.get(key);
@@ -101,6 +156,7 @@ var LeaseManager = class {
101
156
  }));
102
157
  }
103
158
  this.leases.clear();
159
+ this.holders.clear();
104
160
  await Promise.all(revokePromises);
105
161
  }
106
162
  entries() {
@@ -120,6 +176,124 @@ async function loadEtcd3Module() {
120
176
  );
121
177
  }
122
178
  }
179
+
180
+ // src/distribution/coordinator/etcd/prefix-watch.ts
181
+ function connectedOrDropped(watcher) {
182
+ return new Promise((resolve, reject) => {
183
+ const onConnected = () => {
184
+ detach();
185
+ resolve();
186
+ };
187
+ const onDropped = (error) => {
188
+ detach();
189
+ reject(error);
190
+ };
191
+ function detach() {
192
+ watcher.off("connected", onConnected);
193
+ watcher.off("disconnected", onDropped);
194
+ watcher.off("error", onDropped);
195
+ }
196
+ watcher.once("connected", onConnected);
197
+ watcher.once("disconnected", onDropped);
198
+ watcher.once("error", onDropped);
199
+ });
200
+ }
201
+ async function watchPrefix(context, prefix, handlers) {
202
+ const modRevisionByKey = /* @__PURE__ */ new Map();
203
+ let current = null;
204
+ let stopped = false;
205
+ let reopenTimer = null;
206
+ let reopenDelayMs = RECONNECT_FIRST_DELAY_MS;
207
+ function recordPut(kv) {
208
+ modRevisionByKey.set(kv.key.toString(), kv.mod_revision);
209
+ handlers.put(kv);
210
+ }
211
+ function recordDelete(fullKey) {
212
+ modRevisionByKey.delete(fullKey);
213
+ handlers.delete(fullKey);
214
+ }
215
+ async function listMissedChanges(reportMissedChanges) {
216
+ const listing = await context.client.getAll().prefix(prefix).exec();
217
+ const listedKeys = /* @__PURE__ */ new Set();
218
+ for (const kv of listing.kvs) {
219
+ const fullKey = kv.key.toString();
220
+ listedKeys.add(fullKey);
221
+ if (!reportMissedChanges) {
222
+ modRevisionByKey.set(fullKey, kv.mod_revision);
223
+ } else if (modRevisionByKey.get(fullKey) !== kv.mod_revision) {
224
+ recordPut(kv);
225
+ }
226
+ }
227
+ if (reportMissedChanges) {
228
+ for (const fullKey of [...modRevisionByKey.keys()]) {
229
+ if (!listedKeys.has(fullKey)) recordDelete(fullKey);
230
+ }
231
+ }
232
+ return String(BigInt(listing.header.revision) + 1n);
233
+ }
234
+ async function watchFrom(startRevision2) {
235
+ const watcher = context.client.watch().prefix(prefix).startRevision(startRevision2).watcher();
236
+ try {
237
+ await connectedOrDropped(watcher);
238
+ } catch (error) {
239
+ watcher.cancel().catch(() => {
240
+ });
241
+ throw error;
242
+ }
243
+ if (stopped || context.isShutdown()) {
244
+ watcher.cancel().catch(() => {
245
+ });
246
+ return;
247
+ }
248
+ watcher.on("put", recordPut);
249
+ watcher.on("delete", (kv) => recordDelete(kv.key.toString()));
250
+ watcher.on("error", () => reopenAfterFailure(watcher));
251
+ watcher.on("disconnected", () => reopenAfterFailure(watcher));
252
+ context.addWatcher(watcher);
253
+ current = watcher;
254
+ }
255
+ async function listThenWatch(reportMissedChanges) {
256
+ await watchFrom(await listMissedChanges(reportMissedChanges));
257
+ }
258
+ function scheduleReopen() {
259
+ if (stopped || context.isShutdown()) return;
260
+ const delayMs = reopenDelayMs;
261
+ reopenDelayMs = Math.min(reopenDelayMs * 2, RECONNECT_LONGEST_DELAY_MS);
262
+ reopenTimer = setTimeout(() => {
263
+ reopenTimer = null;
264
+ listThenWatch(true).then(
265
+ () => {
266
+ reopenDelayMs = RECONNECT_FIRST_DELAY_MS;
267
+ },
268
+ () => scheduleReopen()
269
+ );
270
+ }, delayMs);
271
+ reopenTimer.unref?.();
272
+ }
273
+ function reopenAfterFailure(failed) {
274
+ if (current !== failed) return;
275
+ current = null;
276
+ context.removeWatcher(failed);
277
+ scheduleReopen();
278
+ }
279
+ const startRevision = await listMissedChanges(false);
280
+ try {
281
+ await watchFrom(startRevision);
282
+ } catch (_) {
283
+ scheduleReopen();
284
+ }
285
+ return () => {
286
+ stopped = true;
287
+ if (reopenTimer !== null) {
288
+ clearTimeout(reopenTimer);
289
+ reopenTimer = null;
290
+ }
291
+ if (current !== null) {
292
+ context.removeWatcher(current);
293
+ current = null;
294
+ }
295
+ };
296
+ }
123
297
  function serializeAllocationTable(table) {
124
298
  const serializable = {
125
299
  indexName: table.indexName,
@@ -127,6 +301,8 @@ function serializeAllocationTable(table) {
127
301
  replicationFactor: table.replicationFactor,
128
302
  assignments: Array.from(table.assignments.entries())
129
303
  };
304
+ if (table.generation !== void 0) serializable.generation = table.generation;
305
+ if (table.indexUuid !== void 0) serializable.indexUuid = table.indexUuid;
130
306
  return new Uint8Array(encode(serializable));
131
307
  }
132
308
  function deserializeAllocationTable(data) {
@@ -140,12 +316,15 @@ function deserializeAllocationTable(data) {
140
316
  if (!Array.isArray(raw.assignments)) {
141
317
  throw new NarsilError(ErrorCodes.CONFIG_INVALID, "AllocationTable missing or invalid assignments field");
142
318
  }
143
- return {
319
+ const table = {
144
320
  indexName: raw.indexName,
145
321
  version: raw.version,
146
322
  replicationFactor: typeof raw.replicationFactor === "number" ? raw.replicationFactor : 0,
147
323
  assignments: normaliseAssignments(raw.assignments)
148
324
  };
325
+ if (typeof raw.generation === "string" && raw.generation.length > 0) table.generation = raw.generation;
326
+ if (typeof raw.indexUuid === "string" && raw.indexUuid.length > 0) table.indexUuid = raw.indexUuid;
327
+ return table;
149
328
  }
150
329
  function stringsOf(value) {
151
330
  if (!Array.isArray(value)) {
@@ -180,6 +359,12 @@ function normaliseAssignments(entries) {
180
359
  } else {
181
360
  delete normalised.lastHolders;
182
361
  }
362
+ const removingReplicas = stringsOf(candidate.removingReplicas);
363
+ if (removingReplicas.length > 0) {
364
+ normalised.removingReplicas = removingReplicas;
365
+ } else {
366
+ delete normalised.removingReplicas;
367
+ }
183
368
  assignments.set(partitionId, normalised);
184
369
  }
185
370
  return assignments;
@@ -211,10 +396,14 @@ function writeNodeRegistration(context, registration) {
211
396
  async function writeUnderLease(context, registration) {
212
397
  const payload = serializeNodeRegistration(registration);
213
398
  const data = Buffer.from(payload);
399
+ const write = { key: context.key, value: data, expected: null };
400
+ context.guard.requireFits(write);
214
401
  const tracked = context.leaseManager.getByNodeId(context.key, registration.nodeId);
215
402
  if (tracked !== void 0) {
216
403
  try {
217
- await tracked.lease.keepaliveOnce();
404
+ const sentAt = performance.now();
405
+ const renewal = await tracked.lease.keepaliveOnce();
406
+ recordLeaseRenewal(tracked.lease, sentAt, Number(renewal.TTL));
218
407
  if (context.leaseManager.holdsPayload(context.key, payload)) {
219
408
  return;
220
409
  }
@@ -225,15 +414,290 @@ async function writeUnderLease(context, registration) {
225
414
  context.leaseManager.remove(context.key);
226
415
  }
227
416
  }
228
- const lease = context.client.lease(context.ttlSeconds);
229
- await lease.grant();
230
- await lease.put(context.key).value(data).exec();
417
+ const lease = await grantFollowedLease(context);
418
+ try {
419
+ await context.guard.send(write, () => lease.put(context.key).value(data).exec());
420
+ } catch (error) {
421
+ await lease.revoke().catch(() => {
422
+ });
423
+ throw error;
424
+ }
231
425
  const existing = context.leaseManager.get(context.key);
232
- if (existing !== void 0) {
426
+ context.leaseManager.track(context.key, lease, registration.nodeId, payload);
427
+ registerAgainOnLoss(context, lease, registration.nodeId);
428
+ if (existing !== void 0 && existing.lease !== lease) {
233
429
  await existing.lease.revoke().catch(() => {
234
430
  });
235
431
  }
236
- context.leaseManager.track(context.key, lease, registration.nodeId, payload);
432
+ }
433
+ async function grantFollowedLease(context) {
434
+ const sentAt = performance.now();
435
+ const lease = context.client.lease(context.ttlSeconds);
436
+ followLeaseRenewals(lease);
437
+ await lease.grant();
438
+ recordLeaseRenewal(lease, sentAt, context.ttlSeconds);
439
+ return lease;
440
+ }
441
+ function registerAgainOnLoss(context, lease, nodeId) {
442
+ lease.once("lost", () => {
443
+ void restoreLostRegistration(context, lease, nodeId, RECONNECT_FIRST_DELAY_MS);
444
+ });
445
+ }
446
+ async function restoreLostRegistration(context, lost, nodeId, retryDelayMs) {
447
+ const settled = await context.leaseManager.runExclusively(context.key, async () => {
448
+ const tracked = context.leaseManager.get(context.key);
449
+ if (context.isShutdown() || tracked === void 0 || tracked.lease !== lost || tracked.payload === null) {
450
+ return true;
451
+ }
452
+ let lease = null;
453
+ try {
454
+ lease = await grantFollowedLease(context);
455
+ await lease.put(context.key).value(Buffer.from(tracked.payload)).exec();
456
+ } catch (_) {
457
+ await lease?.revoke().catch(() => {
458
+ });
459
+ return false;
460
+ }
461
+ context.leaseManager.track(context.key, lease, nodeId, tracked.payload);
462
+ registerAgainOnLoss(context, lease, nodeId);
463
+ return true;
464
+ });
465
+ if (settled) return;
466
+ const timer = setTimeout(() => {
467
+ void restoreLostRegistration(context, lost, nodeId, Math.min(retryDelayMs * 2, RECONNECT_LONGEST_DELAY_MS));
468
+ }, retryDelayMs);
469
+ timer.unref?.();
470
+ }
471
+
472
+ // src/distribution/coordinator/etcd/request-size.ts
473
+ var ETCD_REQUEST_TOO_LARGE = "etcdserver: request is too large";
474
+ var GRPC_MESSAGE_TOO_LARGE = "larger than max";
475
+ function requireEtcdMaxRequestBytes(maxRequestBytes) {
476
+ if (Number.isSafeInteger(maxRequestBytes) && maxRequestBytes > ETCD_REQUEST_FRAMING_BYTES && maxRequestBytes <= MAX_ETCD_REQUEST_BYTES) {
477
+ return;
478
+ }
479
+ throw new NarsilError(
480
+ ErrorCodes.CONFIG_INVALID,
481
+ `maxRequestBytes must be a whole number above ${ETCD_REQUEST_FRAMING_BYTES} and at most ${MAX_ETCD_REQUEST_BYTES}`,
482
+ { maxRequestBytes }
483
+ );
484
+ }
485
+ function requestBytesOf(write) {
486
+ const keyBytes = Buffer.byteLength(write.key);
487
+ return write.value.byteLength + (write.expected?.byteLength ?? 0) + 2 * keyBytes + ETCD_REQUEST_FRAMING_BYTES;
488
+ }
489
+ function refusedForSize(etcd3, error) {
490
+ if (error instanceof etcd3.GRPCInvalidArgumentError) return error.message.includes(ETCD_REQUEST_TOO_LARGE);
491
+ if (error instanceof etcd3.GRPCResourceExhastedError) return error.message.includes(GRPC_MESSAGE_TOO_LARGE);
492
+ return false;
493
+ }
494
+ function createEtcdWriteGuard(etcd3, maxRequestBytes) {
495
+ function requireFits(write) {
496
+ const bytes = requestBytesOf(write);
497
+ if (bytes <= maxRequestBytes) return bytes;
498
+ throw new NarsilError(
499
+ ErrorCodes.CONFIG_INVALID,
500
+ `The request to write "${write.key}" would be ${bytes} bytes, more than the maxRequestBytes of ${maxRequestBytes}. Raise maxRequestBytes in createEtcdCoordinator together with the --max-request-bytes value of etcd.`,
501
+ { key: write.key, bytes, maxRequestBytes }
502
+ );
503
+ }
504
+ return {
505
+ requireFits,
506
+ async send(write, perform) {
507
+ const bytes = requireFits(write);
508
+ try {
509
+ return await perform();
510
+ } catch (error) {
511
+ if (!refusedForSize(etcd3, error)) throw error;
512
+ throw new NarsilError(
513
+ ErrorCodes.CONFIG_INVALID,
514
+ `etcd reports the ${bytes}-byte request to write "${write.key}" as too large, so its --max-request-bytes value is below the maxRequestBytes of ${maxRequestBytes}. Set both to the same value.`,
515
+ { key: write.key, bytes, maxRequestBytes }
516
+ );
517
+ }
518
+ }
519
+ };
520
+ }
521
+
522
+ // src/distribution/coordinator/etcd/security.ts
523
+ var SECURE_SCHEME = "https://";
524
+ var LOOPBACK_IPV4 = /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
525
+ var LOOPBACK_NAMES = /* @__PURE__ */ new Set(["localhost", "[::1]", "::1"]);
526
+ function hostnameOf(endpoint) {
527
+ const withScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(endpoint) ? endpoint : `http://${endpoint}`;
528
+ try {
529
+ return new URL(withScheme).hostname.toLowerCase();
530
+ } catch (_) {
531
+ return null;
532
+ }
533
+ }
534
+ function isLoopback(endpoint) {
535
+ const hostname = hostnameOf(endpoint);
536
+ return hostname !== null && (LOOPBACK_NAMES.has(hostname) || LOOPBACK_IPV4.test(hostname));
537
+ }
538
+ function refuse(message, details = {}) {
539
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, message, details);
540
+ }
541
+ function validateTls(config) {
542
+ const tls = config.tls;
543
+ if (tls === void 0) {
544
+ return;
545
+ }
546
+ if (tls.cert === void 0 !== (tls.key === void 0)) {
547
+ refuse("etcd tls needs both cert and key to present a client certificate, or neither");
548
+ }
549
+ const plain = config.endpoints.filter((endpoint) => !endpoint.startsWith(SECURE_SCHEME));
550
+ if (plain.length > 0) {
551
+ refuse("Every etcd endpoint must start with https:// when tls is set", { endpoints: plain });
552
+ }
553
+ }
554
+ function validateAuth(config) {
555
+ const auth = config.auth;
556
+ if (auth === void 0) {
557
+ return;
558
+ }
559
+ if (typeof auth.username !== "string" || typeof auth.password !== "string" || auth.username.length === 0 || auth.password.length === 0) {
560
+ refuse("etcd auth needs a username and a password that are not empty");
561
+ }
562
+ const exposed = config.endpoints.filter((endpoint) => !endpoint.startsWith(SECURE_SCHEME) && !isLoopback(endpoint));
563
+ if (exposed.length > 0) {
564
+ refuse("etcd auth sends its password over https://, or over http:// to a loopback address alone", {
565
+ endpoints: exposed
566
+ });
567
+ }
568
+ }
569
+ function etcdSecurityOptions(config) {
570
+ validateTls(config);
571
+ validateAuth(config);
572
+ const options = {};
573
+ if (config.tls !== void 0) {
574
+ options.credentials = {
575
+ rootCertificate: Buffer.from(config.tls.ca),
576
+ ...config.tls.key === void 0 ? {} : { privateKey: Buffer.from(config.tls.key) },
577
+ ...config.tls.cert === void 0 ? {} : { certChain: Buffer.from(config.tls.cert) }
578
+ };
579
+ }
580
+ if (config.auth !== void 0) {
581
+ options.auth = { username: config.auth.username, password: config.auth.password };
582
+ }
583
+ return options;
584
+ }
585
+
586
+ // src/distribution/coordinator/etcd/start-probe.ts
587
+ function refusedCredentials(etcd3, error) {
588
+ return error instanceof etcd3.EtcdAuthenticationFailedError || error instanceof etcd3.EtcdInvalidAuthTokenError || error instanceof etcd3.EtcdPermissionDeniedError || error instanceof etcd3.EtcdUserNotFoundError;
589
+ }
590
+ function probeFailure(etcd3, error, endpoints) {
591
+ if (error instanceof NarsilError) {
592
+ return error;
593
+ }
594
+ if (refusedCredentials(etcd3, error)) {
595
+ return new NarsilError(ErrorCodes.COORDINATOR_AUTH_FAILED, "etcd refused the coordinator credentials", {
596
+ endpoints,
597
+ cause: describeError(error)
598
+ });
599
+ }
600
+ return new NarsilError(
601
+ ErrorCodes.COORDINATOR_UNAVAILABLE,
602
+ "The coordinator could not reach any etcd endpoint or complete a TLS handshake with one",
603
+ { endpoints, cause: describeError(error) }
604
+ );
605
+ }
606
+ async function probeEtcd(client, etcd3, probeKey, endpoints) {
607
+ let timer;
608
+ const timedOut = new Promise((_, reject) => {
609
+ timer = setTimeout(() => {
610
+ reject(
611
+ new NarsilError(
612
+ ErrorCodes.COORDINATOR_UNAVAILABLE,
613
+ `No etcd endpoint answered within ${ETCD_START_PROBE_TIMEOUT_MS} ms`,
614
+ { endpoints, timeoutMs: ETCD_START_PROBE_TIMEOUT_MS }
615
+ )
616
+ );
617
+ }, ETCD_START_PROBE_TIMEOUT_MS);
618
+ });
619
+ try {
620
+ await Promise.race([client.get(probeKey).exec(), timedOut]);
621
+ } catch (error) {
622
+ throw probeFailure(etcd3, error, endpoints);
623
+ } finally {
624
+ clearTimeout(timer);
625
+ }
626
+ }
627
+
628
+ // src/distribution/coordinator/etcd/stored-values.ts
629
+ function storedValueMethods(context) {
630
+ const { client, keys, guard, assertNotShutdown, watchPrefix: watchPrefix2 } = context;
631
+ return {
632
+ async get(key) {
633
+ assertNotShutdown();
634
+ const etcdKey = keys.generic(key);
635
+ const buf = await client.get(etcdKey).buffer();
636
+ if (buf === null) {
637
+ return null;
638
+ }
639
+ return new Uint8Array(buf);
640
+ },
641
+ async compareAndSet(key, expected, value) {
642
+ assertNotShutdown();
643
+ const etcdKey = keys.generic(key);
644
+ const newValue = Buffer.from(value);
645
+ return guard.send({ key: etcdKey, value: newValue, expected }, async () => {
646
+ if (expected === null) {
647
+ const txn2 = await client.if(etcdKey, "Create", "==", 0).then(client.put(etcdKey).value(newValue)).commit();
648
+ return txn2.succeeded;
649
+ }
650
+ const expectedBuf = Buffer.from(expected);
651
+ const txn = await client.if(etcdKey, "Value", "==", expectedBuf).then(client.put(etcdKey).value(newValue)).commit();
652
+ return txn.succeeded;
653
+ });
654
+ },
655
+ async delete(key) {
656
+ assertNotShutdown();
657
+ await client.delete().key(keys.generic(key)).exec();
658
+ },
659
+ async getSchema(indexName) {
660
+ assertNotShutdown();
661
+ const key = keys.schema(indexName);
662
+ const buf = await client.get(key).buffer();
663
+ if (buf === null) {
664
+ return null;
665
+ }
666
+ return deserializeSchema(buf);
667
+ },
668
+ async putSchema(indexName, schema) {
669
+ assertNotShutdown();
670
+ const key = keys.schema(indexName);
671
+ const value = serializeSchema(schema);
672
+ await guard.send({ key, value, expected: null }, () => client.put(key).value(value).exec());
673
+ },
674
+ async dropSchema(indexName) {
675
+ assertNotShutdown();
676
+ await client.delete().key(keys.schema(indexName)).exec();
677
+ },
678
+ async listSchemas() {
679
+ assertNotShutdown();
680
+ const prefix = keys.schemaPrefix();
681
+ const result = await client.getAll().prefix(prefix).keys();
682
+ return result.map((fullKey) => extractSuffix(fullKey, prefix)).sort(compareCodePoints);
683
+ },
684
+ async watchSchemas(handler) {
685
+ assertNotShutdown();
686
+ const prefix = keys.schemaPrefix();
687
+ return watchPrefix2(prefix, {
688
+ put(kv) {
689
+ try {
690
+ const schema = deserializeSchema(kv.value);
691
+ handler({ type: "schema_created", indexName: extractSuffix(kv.key.toString(), prefix), schema });
692
+ } catch (_) {
693
+ }
694
+ },
695
+ delete(fullKey) {
696
+ handler({ type: "schema_dropped", indexName: extractSuffix(fullKey, prefix), schema: null });
697
+ }
698
+ });
699
+ }
700
+ };
237
701
  }
238
702
 
239
703
  // src/distribution/coordinator/etcd/validation.ts
@@ -274,8 +738,22 @@ async function createEtcdCoordinator(config) {
274
738
  ...DEFAULT_ETCD_CONFIG,
275
739
  ...config
276
740
  };
741
+ requireEtcdMaxRequestBytes(resolvedConfig.maxRequestBytes);
742
+ const securityOptions = etcdSecurityOptions(resolvedConfig);
277
743
  const etcd3Module = await loadEtcd3Module();
278
- const client = new etcd3Module.Etcd3({ hosts: resolvedConfig.endpoints });
744
+ const client = new etcd3Module.Etcd3({
745
+ hosts: resolvedConfig.endpoints,
746
+ grpcOptions: { "grpc.max_receive_message_length": ETCD_MAX_RESPONSE_BYTES },
747
+ ...securityOptions
748
+ });
749
+ const keys = coordinatorKeys(resolvedConfig);
750
+ try {
751
+ await probeEtcd(client, etcd3Module, keys.nodePrefix(), resolvedConfig.endpoints);
752
+ } catch (error) {
753
+ client.close();
754
+ throw error;
755
+ }
756
+ const guard = createEtcdWriteGuard(etcd3Module, resolvedConfig.maxRequestBytes);
279
757
  const leaseManager = new LeaseManager();
280
758
  const watchers = /* @__PURE__ */ new Set();
281
759
  let isShutdown = false;
@@ -284,7 +762,6 @@ async function createEtcdCoordinator(config) {
284
762
  throw new NarsilError(ErrorCodes.CONFIG_INVALID, "Coordinator has been shut down");
285
763
  }
286
764
  }
287
- const keys = coordinatorKeys(resolvedConfig);
288
765
  function addWatcher(watcher) {
289
766
  if (watchers.size >= MAX_WATCHERS) {
290
767
  watcher.cancel().catch(() => {
@@ -300,6 +777,7 @@ async function createEtcdCoordinator(config) {
300
777
  });
301
778
  watchers.delete(watcher);
302
779
  }
780
+ const watchContext = { client, addWatcher, removeWatcher, isShutdown: () => isShutdown };
303
781
  const coordinator = {
304
782
  async registerNode(registration) {
305
783
  assertNotShutdown();
@@ -308,8 +786,10 @@ async function createEtcdCoordinator(config) {
308
786
  {
309
787
  client,
310
788
  leaseManager,
789
+ guard,
311
790
  key: keys.node(registration.nodeId),
312
- ttlSeconds: resolvedConfig.nodeHeartbeatTtlSeconds
791
+ ttlSeconds: resolvedConfig.nodeHeartbeatTtlSeconds,
792
+ isShutdown: () => isShutdown
313
793
  },
314
794
  registration
315
795
  );
@@ -319,12 +799,29 @@ async function createEtcdCoordinator(config) {
319
799
  validateNodeId(nodeId);
320
800
  const key = keys.node(nodeId);
321
801
  const entry = leaseManager.remove(key);
802
+ leaseManager.forgetHolder(key);
322
803
  if (entry !== void 0) {
323
804
  await entry.lease.revoke().catch(() => {
324
805
  });
325
806
  }
326
807
  await client.delete().key(key).exec();
327
808
  },
809
+ registrationLease(nodeId) {
810
+ if (isShutdown) return null;
811
+ const holder = leaseManager.holder(keys.node(nodeId));
812
+ return holder === void 0 ? null : leaseHeldFor(holder);
813
+ },
814
+ async confirmRegistrationLease(nodeId) {
815
+ if (isShutdown) return null;
816
+ const holder = leaseManager.holder(keys.node(nodeId));
817
+ if (holder === void 0) return null;
818
+ const sentAt = performance.now();
819
+ try {
820
+ recordLeaseRenewal(holder, sentAt, Number((await holder.keepaliveOnce()).TTL));
821
+ } catch (_) {
822
+ }
823
+ return leaseHeldFor(holder);
824
+ },
328
825
  async listNodes() {
329
826
  assertNotShutdown();
330
827
  const prefix = keys.nodePrefix();
@@ -341,21 +838,18 @@ async function createEtcdCoordinator(config) {
341
838
  async watchNodes(handler) {
342
839
  assertNotShutdown();
343
840
  const prefix = keys.nodePrefix();
344
- const watcher = await client.watch().prefix(prefix).create();
345
- addWatcher(watcher);
346
- watcher.on("put", (kv) => {
347
- try {
348
- const registration = deserializeNodeRegistration(kv.value);
349
- handler({ type: "node_joined", nodeId: registration.nodeId, registration });
350
- } catch (_) {
841
+ return watchPrefix(watchContext, prefix, {
842
+ put(kv) {
843
+ try {
844
+ const registration = deserializeNodeRegistration(kv.value);
845
+ handler({ type: "node_joined", nodeId: registration.nodeId, registration });
846
+ } catch (_) {
847
+ }
848
+ },
849
+ delete(fullKey) {
850
+ handler({ type: "node_left", nodeId: extractSuffix(fullKey, prefix), registration: null });
351
851
  }
352
852
  });
353
- watcher.on("delete", (kv) => {
354
- const fullKey = kv.key.toString();
355
- const nodeId = extractSuffix(fullKey, prefix);
356
- handler({ type: "node_left", nodeId, registration: null });
357
- });
358
- return () => removeWatcher(watcher);
359
853
  },
360
854
  async getAllocation(indexName) {
361
855
  assertNotShutdown();
@@ -366,17 +860,24 @@ async function createEtcdCoordinator(config) {
366
860
  }
367
861
  return deserializeAllocationTable(buf);
368
862
  },
369
- async putAllocation(indexName, table, expectedVersion) {
863
+ async putAllocation(indexName, table, expectedVersion, writerNodeId) {
370
864
  assertNotShutdown();
371
865
  const key = keys.allocation(indexName);
372
866
  const data = Buffer.from(serializeAllocationTable(table));
867
+ const write = { key, value: data, expected: null };
373
868
  if (expectedVersion === void 0) {
374
- await client.put(key).value(data).exec();
869
+ await guard.send(write, () => client.put(key).value(data).exec());
375
870
  return true;
376
871
  }
872
+ if (writerNodeId === void 0 || writerNodeId.length === 0) {
873
+ return false;
874
+ }
875
+ const controllerLeaseKey = keys.lease(CONTROLLER_LEASE_KEY);
377
876
  if (expectedVersion === null) {
378
- const txn2 = await client.if(key, "Create", "==", 0).then(client.put(key).value(data)).commit();
379
- return txn2.succeeded;
877
+ return guard.send(write, async () => {
878
+ const txn = await client.if(key, "Create", "==", 0).and(controllerLeaseKey, "Value", "==", writerNodeId).then(client.put(key).value(data)).commit();
879
+ return txn.succeeded;
880
+ });
380
881
  }
381
882
  const response = await client.get(key).exec();
382
883
  if (response.kvs.length === 0) {
@@ -387,8 +888,10 @@ async function createEtcdCoordinator(config) {
387
888
  if (currentTable.version !== expectedVersion) {
388
889
  return false;
389
890
  }
390
- const txn = await client.if(key, "Mod", "==", kv.mod_revision).then(client.put(key).value(data)).commit();
391
- return txn.succeeded;
891
+ return guard.send(write, async () => {
892
+ const txn = await client.if(key, "Mod", "==", kv.mod_revision).and(controllerLeaseKey, "Value", "==", writerNodeId).then(client.put(key).value(data)).commit();
893
+ return txn.succeeded;
894
+ });
392
895
  },
393
896
  async deleteAllocation(indexName) {
394
897
  assertNotShutdown();
@@ -396,17 +899,17 @@ async function createEtcdCoordinator(config) {
396
899
  },
397
900
  async watchAllocation(handler) {
398
901
  assertNotShutdown();
399
- const prefix = keys.allocationPrefix();
400
- const watcher = await client.watch().prefix(prefix).create();
401
- addWatcher(watcher);
402
- watcher.on("put", (kv) => {
403
- try {
404
- const table = deserializeAllocationTable(kv.value);
405
- handler({ indexName: table.indexName, table });
406
- } catch (_) {
902
+ return watchPrefix(watchContext, keys.allocationPrefix(), {
903
+ put(kv) {
904
+ try {
905
+ const table = deserializeAllocationTable(kv.value);
906
+ handler({ indexName: table.indexName, table });
907
+ } catch (_) {
908
+ }
909
+ },
910
+ delete() {
407
911
  }
408
912
  });
409
- return () => removeWatcher(watcher);
410
913
  },
411
914
  async getPartitionState(indexName, partitionId) {
412
915
  assertNotShutdown();
@@ -472,72 +975,13 @@ async function createEtcdCoordinator(config) {
472
975
  await entry.lease.revoke().catch(() => {
473
976
  });
474
977
  },
475
- async get(key) {
476
- assertNotShutdown();
477
- const etcdKey = keys.generic(key);
478
- const buf = await client.get(etcdKey).buffer();
479
- if (buf === null) {
480
- return null;
481
- }
482
- return new Uint8Array(buf);
483
- },
484
- async compareAndSet(key, expected, value) {
485
- assertNotShutdown();
486
- const etcdKey = keys.generic(key);
487
- const newValue = Buffer.from(value);
488
- if (expected === null) {
489
- const txn2 = await client.if(etcdKey, "Create", "==", 0).then(client.put(etcdKey).value(newValue)).commit();
490
- return txn2.succeeded;
491
- }
492
- const expectedBuf = Buffer.from(expected);
493
- const txn = await client.if(etcdKey, "Value", "==", expectedBuf).then(client.put(etcdKey).value(newValue)).commit();
494
- return txn.succeeded;
495
- },
496
- async getSchema(indexName) {
497
- assertNotShutdown();
498
- const key = keys.schema(indexName);
499
- const buf = await client.get(key).buffer();
500
- if (buf === null) {
501
- return null;
502
- }
503
- return deserializeSchema(buf);
504
- },
505
- async putSchema(indexName, schema) {
506
- assertNotShutdown();
507
- const key = keys.schema(indexName);
508
- await client.put(key).value(serializeSchema(schema)).exec();
509
- },
510
- async dropSchema(indexName) {
511
- assertNotShutdown();
512
- await client.delete().key(keys.schema(indexName)).exec();
513
- },
514
- async listSchemas() {
515
- assertNotShutdown();
516
- const prefix = keys.schemaPrefix();
517
- const result = await client.getAll().prefix(prefix).keys();
518
- return result.map((fullKey) => extractSuffix(fullKey, prefix)).sort(compareCodePoints);
519
- },
520
- async watchSchemas(handler) {
521
- assertNotShutdown();
522
- const prefix = keys.schemaPrefix();
523
- const watcher = await client.watch().prefix(prefix).create();
524
- addWatcher(watcher);
525
- watcher.on("put", (kv) => {
526
- try {
527
- const schema = deserializeSchema(kv.value);
528
- const fullKey = kv.key.toString();
529
- const indexName = extractSuffix(fullKey, prefix);
530
- handler({ type: "schema_created", indexName, schema });
531
- } catch (_) {
532
- }
533
- });
534
- watcher.on("delete", (kv) => {
535
- const fullKey = kv.key.toString();
536
- const indexName = extractSuffix(fullKey, prefix);
537
- handler({ type: "schema_dropped", indexName, schema: null });
538
- });
539
- return () => removeWatcher(watcher);
540
- },
978
+ ...storedValueMethods({
979
+ client,
980
+ keys,
981
+ guard,
982
+ assertNotShutdown,
983
+ watchPrefix: (prefix, handlers) => watchPrefix(watchContext, prefix, handlers)
984
+ }),
541
985
  async getLeaseHolder(key) {
542
986
  assertNotShutdown();
543
987
  const etcdKey = keys.lease(key);