@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,44 @@
1
+ import { isPlainObject, MAX_NESTING_DEPTH } from './chunk-L5ORED7V.mjs';
2
+
3
+ // src/workers/vector-code.ts
4
+ var VECTOR_TYPE_NAME = "vector";
5
+ var loaded = null;
6
+ var loading = null;
7
+ function loadedVectorCode() {
8
+ return loaded;
9
+ }
10
+ function requireLoadedVectorCode() {
11
+ if (loaded === null) throw new Error("The engine reached vector code before it loaded that code");
12
+ return loaded;
13
+ }
14
+ function loadVectorCode() {
15
+ if (loaded !== null) return Promise.resolve(loaded);
16
+ if (loading === null) {
17
+ loading = import('./module-3S65MVCM.mjs').then(
18
+ (code) => {
19
+ loaded = code;
20
+ return code;
21
+ },
22
+ (err) => {
23
+ loading = null;
24
+ throw err;
25
+ }
26
+ );
27
+ }
28
+ return loading;
29
+ }
30
+ function mayDeclareVectorField(schema, depth) {
31
+ if (!isPlainObject(schema) || depth > MAX_NESTING_DEPTH + 1) return false;
32
+ for (const type of Object.values(schema)) {
33
+ if (typeof type === "string" ? type.includes(VECTOR_TYPE_NAME) : mayDeclareVectorField(type, depth + 1)) {
34
+ return true;
35
+ }
36
+ }
37
+ return false;
38
+ }
39
+ function loadVectorCodeFor(schema) {
40
+ if (loaded !== null || !mayDeclareVectorField(schema, 1)) return null;
41
+ return loadVectorCode();
42
+ }
43
+
44
+ export { loadVectorCode, loadVectorCodeFor, loadedVectorCode, requireLoadedVectorCode };
@@ -0,0 +1,617 @@
1
+ import { nodeThreadId } from './chunk-C7SLA6CM.mjs';
2
+ import { getFs, getPath } from './chunk-HLPRHOSO.mjs';
3
+ import { DIRECTORY_LOCK_ATTEMPTS, DIRECTORY_LOCK_SETTLE_MS, DIRECTORY_LOCK_PROCESS_START_TOLERANCE_MS } from './chunk-J5QP52HA.mjs';
4
+ import { compareCodePoints } from './chunk-YTJCGKM4.mjs';
5
+ import { NarsilError, ErrorCodes } from './chunk-BWJVNZPR.mjs';
6
+ import { openSync, close, readFileSync } from 'fs';
7
+ import { rename, readFile, writeFile, unlink, realpath, readdir, readlink } from 'fs/promises';
8
+ import { endianness } from 'os';
9
+ import { dirname } from 'path';
10
+
11
+ var AUXV_CLOCK_TICKS_TYPE = 17n;
12
+ var AUXV_END_TYPE = 0n;
13
+ var THIRTY_TWO_BIT_ARCHITECTURES = /* @__PURE__ */ new Set(["arm", "ia32", "mips", "mipsel", "ppc"]);
14
+ var FIRST_STAT_FIELD_AFTER_COMMAND = 3;
15
+ var STAT_STATE_FIELD = 3;
16
+ var STAT_START_TIME_FIELD = 22;
17
+ var EXITED_STATES = /* @__PURE__ */ new Set(["Z", "X", "x"]);
18
+ var UNKNOWN = { kind: "unknown" };
19
+ var clockTicksPerSecond = null;
20
+ function errnoOf(err) {
21
+ return err?.code;
22
+ }
23
+ function readWord(bytes, offset, wordBytes, littleEndian) {
24
+ if (wordBytes === 8) return littleEndian ? bytes.readBigUInt64LE(offset) : bytes.readBigUInt64BE(offset);
25
+ return BigInt(littleEndian ? bytes.readUInt32LE(offset) : bytes.readUInt32BE(offset));
26
+ }
27
+ async function readClockTicksPerSecond() {
28
+ const auxv = await readFile("/proc/self/auxv");
29
+ const wordBytes = THIRTY_TWO_BIT_ARCHITECTURES.has(process.arch) ? 4 : 8;
30
+ const littleEndian = endianness() === "LE";
31
+ for (let offset = 0; offset + 2 * wordBytes <= auxv.length; offset += 2 * wordBytes) {
32
+ const type = readWord(auxv, offset, wordBytes, littleEndian);
33
+ if (type === AUXV_END_TYPE) return null;
34
+ if (type === AUXV_CLOCK_TICKS_TYPE) {
35
+ const ticks = Number(readWord(auxv, offset + wordBytes, wordBytes, littleEndian));
36
+ return ticks > 0 ? ticks : null;
37
+ }
38
+ }
39
+ return null;
40
+ }
41
+ function clockTicks() {
42
+ clockTicksPerSecond ??= readClockTicksPerSecond().catch(() => null);
43
+ return clockTicksPerSecond;
44
+ }
45
+ function threadGroupOf(status) {
46
+ const leader = /^Tgid:\s*(\d+)\s*$/m.exec(status)?.[1];
47
+ return leader === void 0 ? null : Number(leader);
48
+ }
49
+ function statField(fieldsAfterCommand, field) {
50
+ return fieldsAfterCommand[field - FIRST_STAT_FIELD_AFTER_COMMAND];
51
+ }
52
+ function signalReaches(pid) {
53
+ try {
54
+ process.kill(pid, 0);
55
+ return true;
56
+ } catch (err) {
57
+ return errnoOf(err) === "EPERM";
58
+ }
59
+ }
60
+ async function identifyProcess(pid) {
61
+ try {
62
+ const [status, stat, ticksPerSecond] = await Promise.all([
63
+ readFile(`/proc/${pid}/status`, "utf8"),
64
+ readFile(`/proc/${pid}/stat`, "utf8"),
65
+ clockTicks()
66
+ ]);
67
+ const leader = threadGroupOf(status);
68
+ if (leader === null) return UNKNOWN;
69
+ if (leader !== pid) return { kind: "thread" };
70
+ const commandEnd = stat.lastIndexOf(")");
71
+ if (commandEnd < 0) return UNKNOWN;
72
+ const fieldsAfterCommand = stat.slice(commandEnd + 1).trim().split(/\s+/);
73
+ const state = statField(fieldsAfterCommand, STAT_STATE_FIELD);
74
+ if (state !== void 0 && EXITED_STATES.has(state)) return { kind: "exited" };
75
+ const startTicks = Number(statField(fieldsAfterCommand, STAT_START_TIME_FIELD));
76
+ const uptimeSeconds = Number.parseFloat(readFileSync("/proc/uptime", "utf8"));
77
+ const nowMs = Date.now();
78
+ if (ticksPerSecond === null || !Number.isFinite(startTicks) || !Number.isFinite(uptimeSeconds)) return UNKNOWN;
79
+ return { kind: "process", startMs: Math.round(nowMs - (uptimeSeconds - startTicks / ticksPerSecond) * 1e3) };
80
+ } catch {
81
+ return UNKNOWN;
82
+ }
83
+ }
84
+ async function descriptorTargets(pid) {
85
+ const descriptorDirectory = `/proc/${pid}/fd`;
86
+ let descriptors;
87
+ try {
88
+ descriptors = await readdir(descriptorDirectory);
89
+ } catch {
90
+ return null;
91
+ }
92
+ const targets = [];
93
+ for (const descriptor of descriptors) {
94
+ try {
95
+ targets.push(await readlink(`${descriptorDirectory}/${descriptor}`));
96
+ } catch (err) {
97
+ if (errnoOf(err) !== "ENOENT") return null;
98
+ }
99
+ }
100
+ return targets;
101
+ }
102
+ async function holdsFileInDirectoryOf(pid, lockPath) {
103
+ const targets = await descriptorTargets(pid);
104
+ if (targets === null) return false;
105
+ const directory = await realpath(dirname(lockPath)).catch(() => null);
106
+ if (directory === null) return false;
107
+ const inside = directory.endsWith("/") ? directory : `${directory}/`;
108
+ return targets.some((target) => target === directory || target.startsWith(inside));
109
+ }
110
+ async function holderIsLive(holder, self, lockPath) {
111
+ if (holder.pid === self.pid) {
112
+ if (holder.processStartMs === null || self.processStartMs === null) return false;
113
+ if (Math.abs(holder.processStartMs - self.processStartMs) <= DIRECTORY_LOCK_PROCESS_START_TOLERANCE_MS) return true;
114
+ return holdsFileInDirectoryOf(holder.pid, lockPath);
115
+ }
116
+ if (!signalReaches(holder.pid)) return false;
117
+ const found = await identifyProcess(holder.pid);
118
+ if (found.kind === "thread" || found.kind === "exited") return false;
119
+ if (found.kind === "unknown") return signalReaches(holder.pid);
120
+ if (holder.processStartMs === null) return true;
121
+ if (found.startMs - holder.processStartMs <= DIRECTORY_LOCK_PROCESS_START_TOLERANCE_MS) return true;
122
+ return holdsFileInDirectoryOf(holder.pid, lockPath);
123
+ }
124
+ function holdDirectoryOpen(directory) {
125
+ if (process.platform !== "linux") return () => void 0;
126
+ const descriptor = openSync(directory, "r");
127
+ return () => close(descriptor, () => void 0);
128
+ }
129
+ async function removeStaleLock(lockPath, asidePath, isStale) {
130
+ try {
131
+ await rename(lockPath, asidePath);
132
+ } catch (err) {
133
+ if (errnoOf(err) === "ENOENT") return;
134
+ throw err;
135
+ }
136
+ try {
137
+ const moved = await readFile(asidePath, "utf8");
138
+ if (!isStale(moved)) {
139
+ await writeFile(lockPath, moved, { flag: "wx" }).catch((err) => {
140
+ if (errnoOf(err) !== "EEXIST") throw err;
141
+ });
142
+ }
143
+ } finally {
144
+ await unlink(asidePath).catch(() => void 0);
145
+ }
146
+ }
147
+
148
+ // src/persistence/durability/directory-lock.ts
149
+ var LOCK_FILE_NAME = ".narsil.lock";
150
+ var HARD_LINK_UNSUPPORTED_CODES = /* @__PURE__ */ new Set(["EPERM", "ENOTSUP", "EOPNOTSUPP", "ENOSYS"]);
151
+ var lockedDirectories = /* @__PURE__ */ new Set();
152
+ function errnoOf2(err) {
153
+ return err?.code;
154
+ }
155
+ function integerOrNull(text) {
156
+ const value = Number.parseInt(text ?? "", 10);
157
+ return Number.isSafeInteger(value) ? value : null;
158
+ }
159
+ function parseHolder(text) {
160
+ const [pidText, threadText, startText, nonceText] = text.split("\n");
161
+ const pid = integerOrNull(pidText);
162
+ if (pid === null || pid <= 0) return null;
163
+ const nonce = nonceText?.trim() ?? "";
164
+ return {
165
+ pid,
166
+ threadId: integerOrNull(threadText),
167
+ processStartMs: integerOrNull(startText),
168
+ nonce: nonce.length > 0 ? nonce : null
169
+ };
170
+ }
171
+ function formatHolder(holder) {
172
+ return `${holder.pid}
173
+ ${holder.threadId ?? ""}
174
+ ${holder.processStartMs ?? ""}
175
+ ${holder.nonce ?? ""}
176
+ `;
177
+ }
178
+ async function readHolder(fs, lockPath) {
179
+ try {
180
+ return parseHolder(await fs.readFile(lockPath, "utf8")) ?? "unreadable";
181
+ } catch (err) {
182
+ if (errnoOf2(err) === "ENOENT") return "absent";
183
+ throw err;
184
+ }
185
+ }
186
+ async function settledHolder(fs, lockPath) {
187
+ const first = await readHolder(fs, lockPath);
188
+ if (first !== "unreadable") return first;
189
+ await new Promise((resolve) => setTimeout(resolve, DIRECTORY_LOCK_SETTLE_MS));
190
+ return readHolder(fs, lockPath);
191
+ }
192
+ function sameReading(first, second) {
193
+ if (typeof first !== "object" || typeof second !== "object") return first === second;
194
+ return formatHolder(first) === formatHolder(second);
195
+ }
196
+ function heldElsewhere(root, holder, lockPath) {
197
+ return new NarsilError(
198
+ ErrorCodes.CONFIG_INVALID,
199
+ `The durability directory "${root}" is in use by ${holder}, and only one engine may write a write-ahead log directory. Point this engine at another directory, or shut the other engine down first. Where no engine uses the directory, delete "${lockPath}"`,
200
+ { directory: root }
201
+ );
202
+ }
203
+ function holderLabel(holder, self) {
204
+ if (holder.pid !== self.pid) return `process ${holder.pid}`;
205
+ return holder.threadId === self.threadId ? "another copy of Narsil in this thread" : "another thread of this process";
206
+ }
207
+ async function createLockFileExclusively(fs, lockPath, content) {
208
+ try {
209
+ await fs.writeFile(lockPath, content, { flag: "wx" });
210
+ return true;
211
+ } catch (err) {
212
+ if (errnoOf2(err) === "EEXIST") return false;
213
+ throw err;
214
+ }
215
+ }
216
+ async function createLockFile(fs, lockPath, draftPath, content) {
217
+ await fs.writeFile(draftPath, content, { flag: "w" });
218
+ try {
219
+ await fs.link(draftPath, lockPath);
220
+ return true;
221
+ } catch (err) {
222
+ const code = errnoOf2(err);
223
+ if (code === "EEXIST") return false;
224
+ if (code !== void 0 && HARD_LINK_UNSUPPORTED_CODES.has(code)) {
225
+ return createLockFileExclusively(fs, lockPath, content);
226
+ }
227
+ throw err;
228
+ } finally {
229
+ await fs.unlink(draftPath).catch(() => void 0);
230
+ }
231
+ }
232
+ async function acquire(fs, root, lockPath, self) {
233
+ const draftPath = `${lockPath}.${self.pid}.${self.threadId ?? 0}`;
234
+ const content = formatHolder(self);
235
+ for (let attempt = 0; attempt < DIRECTORY_LOCK_ATTEMPTS; attempt++) {
236
+ if (await createLockFile(fs, lockPath, draftPath, content)) return;
237
+ const holder = await settledHolder(fs, lockPath);
238
+ if (typeof holder === "object" && await holderIsLive(holder, self, lockPath)) {
239
+ throw heldElsewhere(root, holderLabel(holder, self), lockPath);
240
+ }
241
+ await removeStaleLock(
242
+ lockPath,
243
+ `${draftPath}.aside`,
244
+ (text) => sameReading(holder, parseHolder(text) ?? "unreadable")
245
+ );
246
+ }
247
+ throw heldElsewhere(root, "another process", lockPath);
248
+ }
249
+ async function describeSelf() {
250
+ return {
251
+ pid: process.pid,
252
+ threadId: await nodeThreadId(),
253
+ processStartMs: Math.round(Date.now() - process.uptime() * 1e3),
254
+ nonce: globalThis.crypto.randomUUID()
255
+ };
256
+ }
257
+ async function lockDirectory(root) {
258
+ const fs = await getFs();
259
+ const pathMod = await getPath();
260
+ const resolvedRoot = pathMod.resolve(root);
261
+ const lockPath = pathMod.join(resolvedRoot, LOCK_FILE_NAME);
262
+ const self = await describeSelf();
263
+ async function holds() {
264
+ const holder = await readHolder(fs, lockPath);
265
+ return typeof holder === "object" && holder.nonce === self.nonce;
266
+ }
267
+ async function removeOwnLockFile() {
268
+ if (await holds().catch(() => false)) await fs.unlink(lockPath).catch(() => void 0);
269
+ }
270
+ let directoryKey = null;
271
+ let closeDirectory = () => void 0;
272
+ try {
273
+ await fs.mkdir(resolvedRoot, { recursive: true });
274
+ const stat = await fs.stat(resolvedRoot, { bigint: true });
275
+ const key = `${stat.dev}:${stat.ino}`;
276
+ if (lockedDirectories.has(key)) throw heldElsewhere(root, "another engine in this process", lockPath);
277
+ lockedDirectories.add(key);
278
+ directoryKey = key;
279
+ await acquire(fs, root, lockPath, self);
280
+ closeDirectory = holdDirectoryOpen(resolvedRoot);
281
+ } catch (err) {
282
+ if (directoryKey !== null) lockedDirectories.delete(directoryKey);
283
+ await removeOwnLockFile();
284
+ if (err instanceof NarsilError) throw err;
285
+ throw new NarsilError(
286
+ ErrorCodes.PERSISTENCE_SAVE_FAILED,
287
+ `The engine cannot lock the durability directory "${root}"`,
288
+ { directory: root, cause: err instanceof Error ? err.message : String(err) }
289
+ );
290
+ }
291
+ const heldKey = directoryKey;
292
+ let released = false;
293
+ return {
294
+ holds,
295
+ async release() {
296
+ if (released) return;
297
+ released = true;
298
+ lockedDirectories.delete(heldKey);
299
+ await removeOwnLockFile();
300
+ closeDirectory();
301
+ }
302
+ };
303
+ }
304
+
305
+ // src/persistence/durability/durable-filesystem.ts
306
+ function validateRelativeKey(key) {
307
+ if (key.length === 0) {
308
+ throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: empty");
309
+ }
310
+ if (key.includes("\0")) {
311
+ throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: null byte detected", { key });
312
+ }
313
+ }
314
+ function wrapFsyncError(err, key) {
315
+ throw new NarsilError(
316
+ ErrorCodes.PERSISTENCE_FSYNC_FAILED,
317
+ `fsync failed for "${key}"; the write is not acknowledged and the node must recover from the durable log`,
318
+ { key, cause: err instanceof Error ? err.message : String(err) }
319
+ );
320
+ }
321
+ async function openReadWriteCreating(filePath, fs) {
322
+ try {
323
+ return await fs.open(filePath, "r+");
324
+ } catch (err) {
325
+ if (err.code !== "ENOENT") {
326
+ throw err;
327
+ }
328
+ }
329
+ try {
330
+ return await fs.open(filePath, "wx+");
331
+ } catch (err) {
332
+ if (err.code !== "EEXIST") {
333
+ throw err;
334
+ }
335
+ return fs.open(filePath, "r+");
336
+ }
337
+ }
338
+ async function writeFully(handle, buffer, key) {
339
+ let offset = 0;
340
+ while (offset < buffer.length) {
341
+ const { bytesWritten } = await handle.write(buffer, offset, buffer.length - offset);
342
+ if (bytesWritten <= 0) {
343
+ throw new NarsilError(
344
+ ErrorCodes.PERSISTENCE_SAVE_FAILED,
345
+ `Write to "${key}" made no progress; the file would be truncated and the write is not acknowledged`,
346
+ { key, written: offset, total: buffer.length }
347
+ );
348
+ }
349
+ offset += bytesWritten;
350
+ }
351
+ }
352
+ async function fsyncDirectory(dirPath, fs) {
353
+ let handle = null;
354
+ try {
355
+ handle = await fs.open(dirPath, "r");
356
+ await handle.sync();
357
+ } catch (err) {
358
+ const code = err.code;
359
+ if (code === "EISDIR" || code === "EPERM" || code === "EINVAL" || code === "ENOTSUP") {
360
+ return;
361
+ }
362
+ wrapFsyncError(err, dirPath);
363
+ } finally {
364
+ if (handle !== null) {
365
+ await handle.close().catch(() => void 0);
366
+ }
367
+ }
368
+ }
369
+ function createDurableDirectory(root) {
370
+ if (root.trim().length === 0) {
371
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "Durable directory requires a non-empty root path");
372
+ }
373
+ async function resolve(key) {
374
+ validateRelativeKey(key);
375
+ const pathMod = await getPath();
376
+ const resolvedBase = pathMod.resolve(root);
377
+ const resolvedPath = pathMod.resolve(root, key);
378
+ if (resolvedPath !== resolvedBase && !resolvedPath.startsWith(resolvedBase + pathMod.sep)) {
379
+ throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: path traversal detected", { key });
380
+ }
381
+ return { path: resolvedPath, resolvedBase, pathMod };
382
+ }
383
+ async function ensureDir(filePath, pathMod, fs) {
384
+ await fs.mkdir(pathMod.dirname(filePath), { recursive: true });
385
+ }
386
+ return {
387
+ get root() {
388
+ return root;
389
+ },
390
+ lock() {
391
+ return lockDirectory(root);
392
+ },
393
+ async identity() {
394
+ const fs = await getFs();
395
+ const pathMod = await getPath();
396
+ try {
397
+ const stat = await fs.stat(pathMod.resolve(root), { bigint: true });
398
+ return `${stat.dev}:${stat.ino}`;
399
+ } catch (err) {
400
+ if (err.code === "ENOENT") return null;
401
+ throw err;
402
+ }
403
+ },
404
+ async appendHandle(key) {
405
+ const { path: filePath, pathMod } = await resolve(key);
406
+ const fs = await getFs();
407
+ await ensureDir(filePath, pathMod, fs);
408
+ const handle = await fs.open(filePath, "a");
409
+ return {
410
+ async append(bytes) {
411
+ await writeFully(handle, bytes, key);
412
+ },
413
+ async sync() {
414
+ try {
415
+ await handle.sync();
416
+ } catch (err) {
417
+ wrapFsyncError(err, key);
418
+ }
419
+ },
420
+ async truncate(byteLength) {
421
+ await handle.truncate(byteLength);
422
+ try {
423
+ await handle.sync();
424
+ } catch (err) {
425
+ wrapFsyncError(err, key);
426
+ }
427
+ },
428
+ async size() {
429
+ const stat = await handle.stat();
430
+ return stat.size;
431
+ },
432
+ async close() {
433
+ await handle.close();
434
+ }
435
+ };
436
+ },
437
+ async markerHandle(key) {
438
+ const { path: filePath, pathMod } = await resolve(key);
439
+ const fs = await getFs();
440
+ await ensureDir(filePath, pathMod, fs);
441
+ const handle = await openReadWriteCreating(filePath, fs);
442
+ return {
443
+ async read() {
444
+ const stat = await handle.stat();
445
+ if (stat.size === 0) {
446
+ return new Uint8Array(0);
447
+ }
448
+ const buffer = Buffer.alloc(stat.size);
449
+ await handle.read(buffer, 0, stat.size, 0);
450
+ return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
451
+ },
452
+ async writeSlot(offset, slot) {
453
+ await handle.write(slot, 0, slot.length, offset);
454
+ try {
455
+ await handle.sync();
456
+ } catch (err) {
457
+ wrapFsyncError(err, key);
458
+ }
459
+ },
460
+ async close() {
461
+ await handle.close();
462
+ }
463
+ };
464
+ },
465
+ async syncDirectoryOf(key) {
466
+ const { path: filePath, pathMod } = await resolve(key);
467
+ const fs = await getFs();
468
+ await fsyncDirectory(pathMod.dirname(filePath), fs);
469
+ },
470
+ async atomicWrite(key, data) {
471
+ const { path: filePath, pathMod } = await resolve(key);
472
+ const fs = await getFs();
473
+ await ensureDir(filePath, pathMod, fs);
474
+ const dir = pathMod.dirname(filePath);
475
+ const tempName = `${pathMod.basename(filePath)}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
476
+ const tempPath = pathMod.join(dir, tempName);
477
+ const buffers = Array.isArray(data) ? data : [data];
478
+ let handle = null;
479
+ try {
480
+ handle = await fs.open(tempPath, "w");
481
+ for (const buffer of buffers) {
482
+ await writeFully(handle, buffer, tempPath);
483
+ }
484
+ try {
485
+ await handle.sync();
486
+ } catch (err) {
487
+ wrapFsyncError(err, tempPath);
488
+ }
489
+ await handle.close();
490
+ handle = null;
491
+ await fs.rename(tempPath, filePath);
492
+ await fsyncDirectory(dir, fs);
493
+ } catch (err) {
494
+ if (handle !== null) {
495
+ await handle.close().catch(() => void 0);
496
+ }
497
+ await fs.unlink(tempPath).catch(() => void 0);
498
+ throw err;
499
+ }
500
+ },
501
+ async read(key) {
502
+ const { path: filePath } = await resolve(key);
503
+ const fs = await getFs();
504
+ try {
505
+ const data = await fs.readFile(filePath);
506
+ return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
507
+ } catch (err) {
508
+ if (err.code === "ENOENT") {
509
+ return null;
510
+ }
511
+ throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Failed to read "${key}"`, {
512
+ key,
513
+ cause: err instanceof Error ? err.message : String(err)
514
+ });
515
+ }
516
+ },
517
+ async readRange(key, offset, length) {
518
+ const { path: filePath } = await resolve(key);
519
+ const fs = await getFs();
520
+ let handle = null;
521
+ try {
522
+ handle = await fs.open(filePath, "r");
523
+ const bytes = new Uint8Array(length);
524
+ let filled = 0;
525
+ while (filled < length) {
526
+ const { bytesRead } = await handle.read(bytes, filled, length - filled, offset + filled);
527
+ if (bytesRead === 0) break;
528
+ filled += bytesRead;
529
+ }
530
+ if (filled < length) {
531
+ throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `"${key}" ends before the bytes a read asked for`, {
532
+ key,
533
+ offset,
534
+ length
535
+ });
536
+ }
537
+ return bytes;
538
+ } finally {
539
+ await handle?.close();
540
+ }
541
+ },
542
+ async byteLength(key) {
543
+ const { path: filePath } = await resolve(key);
544
+ const fs = await getFs();
545
+ try {
546
+ return (await fs.stat(filePath)).size;
547
+ } catch (err) {
548
+ if (err.code === "ENOENT") {
549
+ return null;
550
+ }
551
+ throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Failed to read the length of "${key}"`, {
552
+ key,
553
+ cause: err instanceof Error ? err.message : String(err)
554
+ });
555
+ }
556
+ },
557
+ async remove(key) {
558
+ const { path: filePath, pathMod } = await resolve(key);
559
+ const fs = await getFs();
560
+ try {
561
+ await fs.unlink(filePath);
562
+ } catch (err) {
563
+ if (err.code === "ENOENT") {
564
+ return;
565
+ }
566
+ throw new NarsilError(ErrorCodes.PERSISTENCE_DELETE_FAILED, `Failed to delete "${key}"`, {
567
+ key,
568
+ cause: err instanceof Error ? err.message : String(err)
569
+ });
570
+ }
571
+ await fsyncDirectory(pathMod.dirname(filePath), fs);
572
+ },
573
+ async move(from, to) {
574
+ const source = await resolve(from);
575
+ const target = await resolve(to);
576
+ const fs = await getFs();
577
+ await ensureDir(target.path, target.pathMod, fs);
578
+ await fs.rename(source.path, target.path);
579
+ await fsyncDirectory(target.pathMod.dirname(target.path), fs);
580
+ await fsyncDirectory(source.pathMod.dirname(source.path), fs);
581
+ },
582
+ async list(prefix) {
583
+ const fs = await getFs();
584
+ const pathMod = await getPath();
585
+ const resolvedBase = pathMod.resolve(root);
586
+ const all = await listRecursive(resolvedBase, resolvedBase, pathMod, fs);
587
+ return all.filter((entry) => entry.startsWith(prefix)).sort(compareCodePoints);
588
+ },
589
+ async pathOf(key) {
590
+ const { path: filePath } = await resolve(key);
591
+ return filePath;
592
+ }
593
+ };
594
+ }
595
+ async function listRecursive(dir, base, pathMod, fs) {
596
+ const results = [];
597
+ let entries;
598
+ try {
599
+ entries = await fs.readdir(dir, { withFileTypes: true });
600
+ } catch (err) {
601
+ if (err.code === "ENOENT") {
602
+ return results;
603
+ }
604
+ throw err;
605
+ }
606
+ for (const entry of entries) {
607
+ const full = pathMod.join(dir, entry.name);
608
+ if (entry.isDirectory()) {
609
+ results.push(...await listRecursive(full, base, pathMod, fs));
610
+ } else {
611
+ results.push(pathMod.relative(base, full).split(pathMod.sep).join("/"));
612
+ }
613
+ }
614
+ return results;
615
+ }
616
+
617
+ export { createDurableDirectory };