@securecoms/agent-bridge 0.27.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -0
- package/dist/bin.d.ts +1 -1
- package/dist/bin.js +3396 -380
- package/dist/bridge-agent.d.ts +182 -1
- package/dist/config.d.ts +7 -0
- package/dist/embedder-manifest.json +52 -0
- package/dist/embedder-worker.js +2898 -0
- package/dist/embedder.js +1574 -0
- package/dist/index.js +3201 -306
- package/dist/memory/consumer-recall.d.ts +21 -0
- package/dist/memory/embedder/client.d.ts +103 -0
- package/dist/memory/embedder/download.d.ts +28 -0
- package/dist/memory/embedder/embedder-main.d.ts +1 -0
- package/dist/memory/embedder/embedder-paths.d.ts +14 -0
- package/dist/memory/embedder/engine.d.ts +21 -0
- package/dist/memory/embedder/gc.d.ts +2 -0
- package/dist/memory/embedder/handshake.d.ts +7 -0
- package/dist/memory/embedder/heartbeats.d.ts +4 -0
- package/dist/memory/embedder/import-bundle.d.ts +6 -0
- package/dist/memory/embedder/inner-source.d.ts +2 -0
- package/dist/memory/embedder/layout.d.ts +36 -0
- package/dist/memory/embedder/locks.d.ts +17 -0
- package/dist/memory/embedder/manifest.d.ts +30 -0
- package/dist/memory/embedder/no-dial.d.ts +3 -0
- package/dist/memory/embedder/protocol.d.ts +55 -0
- package/dist/memory/embedder/run-dir.d.ts +11 -0
- package/dist/memory/embedder/scheduler.d.ts +24 -0
- package/dist/memory/embedder/server.d.ts +46 -0
- package/dist/memory/embedder/session.d.ts +17 -0
- package/dist/memory/embedder/spawn.d.ts +40 -0
- package/dist/memory/embedder/tokenizer.d.ts +41 -0
- package/dist/memory/embedder/types.d.ts +32 -0
- package/dist/memory/embedder/windows.d.ts +46 -0
- package/dist/memory/embedder/worker-body.d.ts +44 -0
- package/dist/memory/embedder/worker.d.ts +1 -0
- package/dist/memory/memory-config.d.ts +13 -0
- package/dist/memory/note-frames.d.ts +5 -2
- package/dist/memory/notes-book.d.ts +21 -3
- package/dist/memory/provider.d.ts +2 -0
- package/dist/memory/recall/call-heartbeat.d.ts +35 -0
- package/dist/memory/recall/membership-purge.d.ts +82 -0
- package/dist/memory/recall/rank.d.ts +15 -0
- package/dist/memory/recall/recall-book.d.ts +140 -0
- package/dist/memory/recall/reembed.d.ts +35 -0
- package/dist/memory-taps.d.ts +3 -2
- package/dist/ort-wasm-simd-threaded.wasm +0 -0
- package/dist/version.d.ts +1 -1
- package/dist/ws-server.d.ts +13 -1
- package/package.json +2 -1
package/dist/embedder.js
ADDED
|
@@ -0,0 +1,1574 @@
|
|
|
1
|
+
// src/memory/embedder/embedder-main.ts
|
|
2
|
+
import { Worker } from "node:worker_threads";
|
|
3
|
+
import { mkdirSync as mkdirSync3, readFileSync as readFileSync3, rmSync as rmSync3, writeFileSync as writeFileSync2 } from "node:fs";
|
|
4
|
+
import { homedir as homedir3 } from "node:os";
|
|
5
|
+
import { join as join4 } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
|
|
8
|
+
// src/memory/embedder/engine.ts
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
var HashMismatch = class extends Error {
|
|
11
|
+
};
|
|
12
|
+
function verifyBuffer(bytes2, sha256, size) {
|
|
13
|
+
if (bytes2.length !== size || createHash("sha256").update(bytes2).digest("hex") !== sha256) throw new HashMismatch("embedder file mismatch");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/memory/embedder/manifest.ts
|
|
17
|
+
var file = (modelId, name, sha256, size) => Object.freeze({ name, urlPath: `/models/embeddings/${modelId}/${sha256}/${name}`, sha256, size });
|
|
18
|
+
var MODEL_ID = "minilm-l6-v2-int8";
|
|
19
|
+
var EMBEDDING_MANIFEST = Object.freeze({
|
|
20
|
+
modelId: MODEL_ID,
|
|
21
|
+
dims: 384,
|
|
22
|
+
maxTokens: 256,
|
|
23
|
+
pooling: "mean",
|
|
24
|
+
normalize: true,
|
|
25
|
+
similarityThreshold: 0.19,
|
|
26
|
+
onnxInputs: Object.freeze(["input_ids", "attention_mask", "token_type_ids"]),
|
|
27
|
+
onnxOutput: "last_hidden_state",
|
|
28
|
+
wasmSha256: "ec8580a9d7b9476ceee52e10a7f94124e4dc71a019d666ed6d4726697c109a4d",
|
|
29
|
+
wasmSize: 13961845,
|
|
30
|
+
files: Object.freeze([
|
|
31
|
+
file(MODEL_ID, "model.onnx", "afdb6f1a0e45b715d0bb9b11772f032c399babd23bfc31fed1c170afc848bdb1", 22972370),
|
|
32
|
+
file(MODEL_ID, "vocab.txt", "07eced375cec144d27c900241f3e339478dec958f92fddbc551f295c992038a3", 231508),
|
|
33
|
+
file(MODEL_ID, "LICENSE", "d83f8f68a86e308e30d1ddf3d1b9984500f0c39ab8a1c7d274ab2799c21155b4", 9934),
|
|
34
|
+
file(MODEL_ID, "NOTICE", "99446995c7800bd9ce033f24c477b00daa50f06da85e441e903f4ce1e32c7507", 320)
|
|
35
|
+
])
|
|
36
|
+
});
|
|
37
|
+
var TOKENIZER_OPTIONS = Object.freeze({
|
|
38
|
+
lowercase: true,
|
|
39
|
+
stripAccents: true,
|
|
40
|
+
unkToken: "[UNK]",
|
|
41
|
+
unknownDropped: false,
|
|
42
|
+
addSpecialTokens: true,
|
|
43
|
+
maxInputCharsPerWord: 100
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// src/memory/embedder/layout.ts
|
|
47
|
+
import { homedir } from "node:os";
|
|
48
|
+
import { basename, dirname, join } from "node:path";
|
|
49
|
+
var defaultModelsRoot = (home = homedir()) => join(home, ".securecoms", "models", "embeddings");
|
|
50
|
+
var filePath = (root, modelId, f2) => join(root, modelId, f2.sha256, f2.name);
|
|
51
|
+
|
|
52
|
+
// src/memory/embedder/embedder-paths.ts
|
|
53
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
54
|
+
import { homedir as homedir2 } from "node:os";
|
|
55
|
+
import { join as join2 } from "node:path";
|
|
56
|
+
|
|
57
|
+
// src/memory/embedder/protocol.ts
|
|
58
|
+
import { StringDecoder } from "node:string_decoder";
|
|
59
|
+
var EMBEDDER_PROTOCOL = 1;
|
|
60
|
+
var MAX_TEXT_BYTES = 8192;
|
|
61
|
+
var MAX_LINE_BYTES = 69632;
|
|
62
|
+
function encodeVec64(v) {
|
|
63
|
+
const b = Buffer.alloc(v.length * 4);
|
|
64
|
+
for (let i = 0; i < v.length; i++) b.writeFloatLE(v[i], i * 4);
|
|
65
|
+
return b.toString("base64");
|
|
66
|
+
}
|
|
67
|
+
function lineReader(onLine, onOverflow, maxLine = MAX_LINE_BYTES) {
|
|
68
|
+
const dec = new StringDecoder("utf8");
|
|
69
|
+
let buf = "";
|
|
70
|
+
let dead = false;
|
|
71
|
+
const cap = () => typeof maxLine === "function" ? maxLine() : maxLine;
|
|
72
|
+
const overflow = () => {
|
|
73
|
+
dead = true;
|
|
74
|
+
buf = "";
|
|
75
|
+
onOverflow();
|
|
76
|
+
};
|
|
77
|
+
return (chunk) => {
|
|
78
|
+
if (dead) return;
|
|
79
|
+
buf += dec.write(chunk);
|
|
80
|
+
let i;
|
|
81
|
+
while ((i = buf.indexOf("\n")) >= 0) {
|
|
82
|
+
const line = buf.slice(0, i);
|
|
83
|
+
buf = buf.slice(i + 1);
|
|
84
|
+
if (Buffer.byteLength(line) > cap()) {
|
|
85
|
+
overflow();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (line) onLine(line);
|
|
89
|
+
}
|
|
90
|
+
if (Buffer.byteLength(buf) > cap()) overflow();
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/memory/embedder/embedder-paths.ts
|
|
95
|
+
function embedderKey(m) {
|
|
96
|
+
const model = m.files.find((f2) => f2.name === "model.onnx");
|
|
97
|
+
return createHash2("sha256").update(`${EMBEDDER_PROTOCOL}\0${m.modelId}\0${model.sha256}\0${m.wasmSha256}`).digest("hex").slice(0, 16);
|
|
98
|
+
}
|
|
99
|
+
var embedderRunDir = (home = homedir2()) => join2(home, ".securecoms", "run", "embedder");
|
|
100
|
+
var embedderPaths = (dir2, key2) => ({
|
|
101
|
+
sock: join2(dir2, `${key2}.sock`),
|
|
102
|
+
token: join2(dir2, `${key2}.token`),
|
|
103
|
+
lock: join2(dir2, `${key2}.spawn.lock`),
|
|
104
|
+
status: join2(dir2, `${key2}.status`),
|
|
105
|
+
pid: join2(dir2, `${key2}.pid`)
|
|
106
|
+
// E1 delta H1: the instance's lifetime lock
|
|
107
|
+
});
|
|
108
|
+
var SOCKET_PATH_MAX = 103;
|
|
109
|
+
var windowsRunDir = (env = process.env) => {
|
|
110
|
+
if (!env.LOCALAPPDATA) throw new Error("LOCALAPPDATA unset");
|
|
111
|
+
return join2(env.LOCALAPPDATA, "SecureComs", "run", "embedder");
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// src/memory/embedder/run-dir.ts
|
|
115
|
+
import { existsSync, lstatSync, mkdirSync } from "node:fs";
|
|
116
|
+
import { dirname as dirname2 } from "node:path";
|
|
117
|
+
var RunDirRefused = class extends Error {
|
|
118
|
+
};
|
|
119
|
+
function ensureRunDir(dir2, uid = process.getuid?.() ?? -1, lstat = lstatSync) {
|
|
120
|
+
const parent = dirname2(dir2);
|
|
121
|
+
if (!existsSync(parent)) mkdirSync(parent, { recursive: true, mode: 448 });
|
|
122
|
+
const ps = lstat(parent);
|
|
123
|
+
if (ps.isSymbolicLink() || !ps.isDirectory()) throw new RunDirRefused("run directory parent is not a real directory");
|
|
124
|
+
if (ps.uid !== uid) throw new RunDirRefused("run directory parent is not owned by this user");
|
|
125
|
+
if ((ps.mode & 18) !== 0) throw new RunDirRefused("run directory parent is group or world writable");
|
|
126
|
+
try {
|
|
127
|
+
mkdirSync(dir2, { mode: 448 });
|
|
128
|
+
} catch (e) {
|
|
129
|
+
if (e.code !== "EEXIST") throw e;
|
|
130
|
+
}
|
|
131
|
+
const st = lstat(dir2);
|
|
132
|
+
if (st.isSymbolicLink() || !st.isDirectory()) throw new RunDirRefused("run directory is not a real directory");
|
|
133
|
+
if (st.uid !== uid) throw new RunDirRefused("run directory is not owned by this user");
|
|
134
|
+
if ((st.mode & 511) !== 448) throw new RunDirRefused("run directory mode is not 0700");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/memory/embedder/server.ts
|
|
138
|
+
import { createServer, connect } from "node:net";
|
|
139
|
+
import { randomBytes as randomBytes4 } from "node:crypto";
|
|
140
|
+
|
|
141
|
+
// src/memory/embedder/handshake.ts
|
|
142
|
+
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
143
|
+
var PREAUTH_MAX_CONNECTIONS = 64;
|
|
144
|
+
var PREAUTH_MAX_LINE = 256;
|
|
145
|
+
var PREAUTH_TIMEOUT_MS = 250;
|
|
146
|
+
var PREAUTH_MAX_CONNECTIONS_TCP = 8;
|
|
147
|
+
var nonce = () => randomBytes(16).toString("hex");
|
|
148
|
+
var mac = (token, side, ns, nc, protocol) => createHmac("sha256", Buffer.from(token, "hex")).update(`${side}:${protocol}:${ns}:${nc}`).digest("hex");
|
|
149
|
+
var macClient = (token, ns, nc, protocol = EMBEDDER_PROTOCOL) => mac(token, "c", ns, nc, protocol);
|
|
150
|
+
var macServer = (token, ns, nc, protocol = EMBEDDER_PROTOCOL) => mac(token, "s", ns, nc, protocol);
|
|
151
|
+
function macEqual(a, b) {
|
|
152
|
+
if (!/^[0-9a-f]{64}$/.test(a) || !/^[0-9a-f]{64}$/.test(b)) return false;
|
|
153
|
+
return timingSafeEqual(Buffer.from(a, "hex"), Buffer.from(b, "hex"));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/memory/embedder/session.ts
|
|
157
|
+
import { hkdfSync, randomBytes as randomBytes2 } from "node:crypto";
|
|
158
|
+
|
|
159
|
+
// ../../node_modules/@noble/ciphers/esm/utils.js
|
|
160
|
+
function isBytes(a) {
|
|
161
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
162
|
+
}
|
|
163
|
+
function abool(b) {
|
|
164
|
+
if (typeof b !== "boolean")
|
|
165
|
+
throw new Error(`boolean expected, not ${b}`);
|
|
166
|
+
}
|
|
167
|
+
function anumber(n) {
|
|
168
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
169
|
+
throw new Error("positive integer expected, got " + n);
|
|
170
|
+
}
|
|
171
|
+
function abytes(b, ...lengths) {
|
|
172
|
+
if (!isBytes(b))
|
|
173
|
+
throw new Error("Uint8Array expected");
|
|
174
|
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
175
|
+
throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
176
|
+
}
|
|
177
|
+
function aexists(instance, checkFinished = true) {
|
|
178
|
+
if (instance.destroyed)
|
|
179
|
+
throw new Error("Hash instance has been destroyed");
|
|
180
|
+
if (checkFinished && instance.finished)
|
|
181
|
+
throw new Error("Hash#digest() has already been called");
|
|
182
|
+
}
|
|
183
|
+
function aoutput(out, instance) {
|
|
184
|
+
abytes(out);
|
|
185
|
+
const min = instance.outputLen;
|
|
186
|
+
if (out.length < min) {
|
|
187
|
+
throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function u32(arr) {
|
|
191
|
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
192
|
+
}
|
|
193
|
+
function clean(...arrays) {
|
|
194
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
195
|
+
arrays[i].fill(0);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function createView(arr) {
|
|
199
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
200
|
+
}
|
|
201
|
+
var isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
|
|
202
|
+
function utf8ToBytes(str) {
|
|
203
|
+
if (typeof str !== "string")
|
|
204
|
+
throw new Error("string expected");
|
|
205
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
206
|
+
}
|
|
207
|
+
function toBytes(data2) {
|
|
208
|
+
if (typeof data2 === "string")
|
|
209
|
+
data2 = utf8ToBytes(data2);
|
|
210
|
+
else if (isBytes(data2))
|
|
211
|
+
data2 = copyBytes(data2);
|
|
212
|
+
else
|
|
213
|
+
throw new Error("Uint8Array expected, got " + typeof data2);
|
|
214
|
+
return data2;
|
|
215
|
+
}
|
|
216
|
+
function checkOpts(defaults, opts) {
|
|
217
|
+
if (opts == null || typeof opts !== "object")
|
|
218
|
+
throw new Error("options must be defined");
|
|
219
|
+
const merged = Object.assign(defaults, opts);
|
|
220
|
+
return merged;
|
|
221
|
+
}
|
|
222
|
+
function equalBytes(a, b) {
|
|
223
|
+
if (a.length !== b.length)
|
|
224
|
+
return false;
|
|
225
|
+
let diff = 0;
|
|
226
|
+
for (let i = 0; i < a.length; i++)
|
|
227
|
+
diff |= a[i] ^ b[i];
|
|
228
|
+
return diff === 0;
|
|
229
|
+
}
|
|
230
|
+
var wrapCipher = /* @__NO_SIDE_EFFECTS__ */ (params, constructor) => {
|
|
231
|
+
function wrappedCipher(key2, ...args) {
|
|
232
|
+
abytes(key2);
|
|
233
|
+
if (!isLE)
|
|
234
|
+
throw new Error("Non little-endian hardware is not yet supported");
|
|
235
|
+
if (params.nonceLength !== void 0) {
|
|
236
|
+
const nonce2 = args[0];
|
|
237
|
+
if (!nonce2)
|
|
238
|
+
throw new Error("nonce / iv required");
|
|
239
|
+
if (params.varSizeNonce)
|
|
240
|
+
abytes(nonce2);
|
|
241
|
+
else
|
|
242
|
+
abytes(nonce2, params.nonceLength);
|
|
243
|
+
}
|
|
244
|
+
const tagl = params.tagLength;
|
|
245
|
+
if (tagl && args[1] !== void 0) {
|
|
246
|
+
abytes(args[1]);
|
|
247
|
+
}
|
|
248
|
+
const cipher = constructor(key2, ...args);
|
|
249
|
+
const checkOutput = (fnLength, output) => {
|
|
250
|
+
if (output !== void 0) {
|
|
251
|
+
if (fnLength !== 2)
|
|
252
|
+
throw new Error("cipher output not supported");
|
|
253
|
+
abytes(output);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
let called = false;
|
|
257
|
+
const wrCipher = {
|
|
258
|
+
encrypt(data2, output) {
|
|
259
|
+
if (called)
|
|
260
|
+
throw new Error("cannot encrypt() twice with same key + nonce");
|
|
261
|
+
called = true;
|
|
262
|
+
abytes(data2);
|
|
263
|
+
checkOutput(cipher.encrypt.length, output);
|
|
264
|
+
return cipher.encrypt(data2, output);
|
|
265
|
+
},
|
|
266
|
+
decrypt(data2, output) {
|
|
267
|
+
abytes(data2);
|
|
268
|
+
if (tagl && data2.length < tagl)
|
|
269
|
+
throw new Error("invalid ciphertext length: smaller than tagLength=" + tagl);
|
|
270
|
+
checkOutput(cipher.decrypt.length, output);
|
|
271
|
+
return cipher.decrypt(data2, output);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
return wrCipher;
|
|
275
|
+
}
|
|
276
|
+
Object.assign(wrappedCipher, params);
|
|
277
|
+
return wrappedCipher;
|
|
278
|
+
};
|
|
279
|
+
function getOutput(expectedLength, out, onlyAligned = true) {
|
|
280
|
+
if (out === void 0)
|
|
281
|
+
return new Uint8Array(expectedLength);
|
|
282
|
+
if (out.length !== expectedLength)
|
|
283
|
+
throw new Error("invalid output length, expected " + expectedLength + ", got: " + out.length);
|
|
284
|
+
if (onlyAligned && !isAligned32(out))
|
|
285
|
+
throw new Error("invalid output, must be aligned");
|
|
286
|
+
return out;
|
|
287
|
+
}
|
|
288
|
+
function setBigUint64(view, byteOffset, value, isLE2) {
|
|
289
|
+
if (typeof view.setBigUint64 === "function")
|
|
290
|
+
return view.setBigUint64(byteOffset, value, isLE2);
|
|
291
|
+
const _32n = BigInt(32);
|
|
292
|
+
const _u32_max = BigInt(4294967295);
|
|
293
|
+
const wh = Number(value >> _32n & _u32_max);
|
|
294
|
+
const wl = Number(value & _u32_max);
|
|
295
|
+
const h = isLE2 ? 4 : 0;
|
|
296
|
+
const l = isLE2 ? 0 : 4;
|
|
297
|
+
view.setUint32(byteOffset + h, wh, isLE2);
|
|
298
|
+
view.setUint32(byteOffset + l, wl, isLE2);
|
|
299
|
+
}
|
|
300
|
+
function u64Lengths(dataLength, aadLength, isLE2) {
|
|
301
|
+
abool(isLE2);
|
|
302
|
+
const num = new Uint8Array(16);
|
|
303
|
+
const view = createView(num);
|
|
304
|
+
setBigUint64(view, 0, BigInt(aadLength), isLE2);
|
|
305
|
+
setBigUint64(view, 8, BigInt(dataLength), isLE2);
|
|
306
|
+
return num;
|
|
307
|
+
}
|
|
308
|
+
function isAligned32(bytes2) {
|
|
309
|
+
return bytes2.byteOffset % 4 === 0;
|
|
310
|
+
}
|
|
311
|
+
function copyBytes(bytes2) {
|
|
312
|
+
return Uint8Array.from(bytes2);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ../../node_modules/@noble/ciphers/esm/_arx.js
|
|
316
|
+
var _utf8ToBytes = (str) => Uint8Array.from(str.split("").map((c) => c.charCodeAt(0)));
|
|
317
|
+
var sigma16 = _utf8ToBytes("expand 16-byte k");
|
|
318
|
+
var sigma32 = _utf8ToBytes("expand 32-byte k");
|
|
319
|
+
var sigma16_32 = u32(sigma16);
|
|
320
|
+
var sigma32_32 = u32(sigma32);
|
|
321
|
+
function rotl(a, b) {
|
|
322
|
+
return a << b | a >>> 32 - b;
|
|
323
|
+
}
|
|
324
|
+
function isAligned322(b) {
|
|
325
|
+
return b.byteOffset % 4 === 0;
|
|
326
|
+
}
|
|
327
|
+
var BLOCK_LEN = 64;
|
|
328
|
+
var BLOCK_LEN32 = 16;
|
|
329
|
+
var MAX_COUNTER = 2 ** 32 - 1;
|
|
330
|
+
var U32_EMPTY = new Uint32Array();
|
|
331
|
+
function runCipher(core, sigma, key2, nonce2, data2, output, counter, rounds) {
|
|
332
|
+
const len = data2.length;
|
|
333
|
+
const block = new Uint8Array(BLOCK_LEN);
|
|
334
|
+
const b32 = u32(block);
|
|
335
|
+
const isAligned = isAligned322(data2) && isAligned322(output);
|
|
336
|
+
const d32 = isAligned ? u32(data2) : U32_EMPTY;
|
|
337
|
+
const o32 = isAligned ? u32(output) : U32_EMPTY;
|
|
338
|
+
for (let pos = 0; pos < len; counter++) {
|
|
339
|
+
core(sigma, key2, nonce2, b32, counter, rounds);
|
|
340
|
+
if (counter >= MAX_COUNTER)
|
|
341
|
+
throw new Error("arx: counter overflow");
|
|
342
|
+
const take = Math.min(BLOCK_LEN, len - pos);
|
|
343
|
+
if (isAligned && take === BLOCK_LEN) {
|
|
344
|
+
const pos32 = pos / 4;
|
|
345
|
+
if (pos % 4 !== 0)
|
|
346
|
+
throw new Error("arx: invalid block position");
|
|
347
|
+
for (let j = 0, posj; j < BLOCK_LEN32; j++) {
|
|
348
|
+
posj = pos32 + j;
|
|
349
|
+
o32[posj] = d32[posj] ^ b32[j];
|
|
350
|
+
}
|
|
351
|
+
pos += BLOCK_LEN;
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
for (let j = 0, posj; j < take; j++) {
|
|
355
|
+
posj = pos + j;
|
|
356
|
+
output[posj] = data2[posj] ^ block[j];
|
|
357
|
+
}
|
|
358
|
+
pos += take;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
function createCipher(core, opts) {
|
|
362
|
+
const { allowShortKeys, extendNonceFn, counterLength, counterRight, rounds } = checkOpts({ allowShortKeys: false, counterLength: 8, counterRight: false, rounds: 20 }, opts);
|
|
363
|
+
if (typeof core !== "function")
|
|
364
|
+
throw new Error("core must be a function");
|
|
365
|
+
anumber(counterLength);
|
|
366
|
+
anumber(rounds);
|
|
367
|
+
abool(counterRight);
|
|
368
|
+
abool(allowShortKeys);
|
|
369
|
+
return (key2, nonce2, data2, output, counter = 0) => {
|
|
370
|
+
abytes(key2);
|
|
371
|
+
abytes(nonce2);
|
|
372
|
+
abytes(data2);
|
|
373
|
+
const len = data2.length;
|
|
374
|
+
if (output === void 0)
|
|
375
|
+
output = new Uint8Array(len);
|
|
376
|
+
abytes(output);
|
|
377
|
+
anumber(counter);
|
|
378
|
+
if (counter < 0 || counter >= MAX_COUNTER)
|
|
379
|
+
throw new Error("arx: counter overflow");
|
|
380
|
+
if (output.length < len)
|
|
381
|
+
throw new Error(`arx: output (${output.length}) is shorter than data (${len})`);
|
|
382
|
+
const toClean = [];
|
|
383
|
+
let l = key2.length;
|
|
384
|
+
let k;
|
|
385
|
+
let sigma;
|
|
386
|
+
if (l === 32) {
|
|
387
|
+
toClean.push(k = copyBytes(key2));
|
|
388
|
+
sigma = sigma32_32;
|
|
389
|
+
} else if (l === 16 && allowShortKeys) {
|
|
390
|
+
k = new Uint8Array(32);
|
|
391
|
+
k.set(key2);
|
|
392
|
+
k.set(key2, 16);
|
|
393
|
+
sigma = sigma16_32;
|
|
394
|
+
toClean.push(k);
|
|
395
|
+
} else {
|
|
396
|
+
throw new Error(`arx: invalid 32-byte key, got length=${l}`);
|
|
397
|
+
}
|
|
398
|
+
if (!isAligned322(nonce2))
|
|
399
|
+
toClean.push(nonce2 = copyBytes(nonce2));
|
|
400
|
+
const k32 = u32(k);
|
|
401
|
+
if (extendNonceFn) {
|
|
402
|
+
if (nonce2.length !== 24)
|
|
403
|
+
throw new Error(`arx: extended nonce must be 24 bytes`);
|
|
404
|
+
extendNonceFn(sigma, k32, u32(nonce2.subarray(0, 16)), k32);
|
|
405
|
+
nonce2 = nonce2.subarray(16);
|
|
406
|
+
}
|
|
407
|
+
const nonceNcLen = 16 - counterLength;
|
|
408
|
+
if (nonceNcLen !== nonce2.length)
|
|
409
|
+
throw new Error(`arx: nonce must be ${nonceNcLen} or 16 bytes`);
|
|
410
|
+
if (nonceNcLen !== 12) {
|
|
411
|
+
const nc = new Uint8Array(12);
|
|
412
|
+
nc.set(nonce2, counterRight ? 0 : 12 - nonce2.length);
|
|
413
|
+
nonce2 = nc;
|
|
414
|
+
toClean.push(nonce2);
|
|
415
|
+
}
|
|
416
|
+
const n32 = u32(nonce2);
|
|
417
|
+
runCipher(core, sigma, k32, n32, data2, output, counter, rounds);
|
|
418
|
+
clean(...toClean);
|
|
419
|
+
return output;
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// ../../node_modules/@noble/ciphers/esm/_poly1305.js
|
|
424
|
+
var u8to16 = (a, i) => a[i++] & 255 | (a[i++] & 255) << 8;
|
|
425
|
+
var Poly1305 = class {
|
|
426
|
+
constructor(key2) {
|
|
427
|
+
this.blockLen = 16;
|
|
428
|
+
this.outputLen = 16;
|
|
429
|
+
this.buffer = new Uint8Array(16);
|
|
430
|
+
this.r = new Uint16Array(10);
|
|
431
|
+
this.h = new Uint16Array(10);
|
|
432
|
+
this.pad = new Uint16Array(8);
|
|
433
|
+
this.pos = 0;
|
|
434
|
+
this.finished = false;
|
|
435
|
+
key2 = toBytes(key2);
|
|
436
|
+
abytes(key2, 32);
|
|
437
|
+
const t0 = u8to16(key2, 0);
|
|
438
|
+
const t1 = u8to16(key2, 2);
|
|
439
|
+
const t2 = u8to16(key2, 4);
|
|
440
|
+
const t3 = u8to16(key2, 6);
|
|
441
|
+
const t4 = u8to16(key2, 8);
|
|
442
|
+
const t5 = u8to16(key2, 10);
|
|
443
|
+
const t6 = u8to16(key2, 12);
|
|
444
|
+
const t7 = u8to16(key2, 14);
|
|
445
|
+
this.r[0] = t0 & 8191;
|
|
446
|
+
this.r[1] = (t0 >>> 13 | t1 << 3) & 8191;
|
|
447
|
+
this.r[2] = (t1 >>> 10 | t2 << 6) & 7939;
|
|
448
|
+
this.r[3] = (t2 >>> 7 | t3 << 9) & 8191;
|
|
449
|
+
this.r[4] = (t3 >>> 4 | t4 << 12) & 255;
|
|
450
|
+
this.r[5] = t4 >>> 1 & 8190;
|
|
451
|
+
this.r[6] = (t4 >>> 14 | t5 << 2) & 8191;
|
|
452
|
+
this.r[7] = (t5 >>> 11 | t6 << 5) & 8065;
|
|
453
|
+
this.r[8] = (t6 >>> 8 | t7 << 8) & 8191;
|
|
454
|
+
this.r[9] = t7 >>> 5 & 127;
|
|
455
|
+
for (let i = 0; i < 8; i++)
|
|
456
|
+
this.pad[i] = u8to16(key2, 16 + 2 * i);
|
|
457
|
+
}
|
|
458
|
+
process(data2, offset, isLast = false) {
|
|
459
|
+
const hibit = isLast ? 0 : 1 << 11;
|
|
460
|
+
const { h, r } = this;
|
|
461
|
+
const r0 = r[0];
|
|
462
|
+
const r1 = r[1];
|
|
463
|
+
const r2 = r[2];
|
|
464
|
+
const r3 = r[3];
|
|
465
|
+
const r4 = r[4];
|
|
466
|
+
const r5 = r[5];
|
|
467
|
+
const r6 = r[6];
|
|
468
|
+
const r7 = r[7];
|
|
469
|
+
const r8 = r[8];
|
|
470
|
+
const r9 = r[9];
|
|
471
|
+
const t0 = u8to16(data2, offset + 0);
|
|
472
|
+
const t1 = u8to16(data2, offset + 2);
|
|
473
|
+
const t2 = u8to16(data2, offset + 4);
|
|
474
|
+
const t3 = u8to16(data2, offset + 6);
|
|
475
|
+
const t4 = u8to16(data2, offset + 8);
|
|
476
|
+
const t5 = u8to16(data2, offset + 10);
|
|
477
|
+
const t6 = u8to16(data2, offset + 12);
|
|
478
|
+
const t7 = u8to16(data2, offset + 14);
|
|
479
|
+
let h0 = h[0] + (t0 & 8191);
|
|
480
|
+
let h1 = h[1] + ((t0 >>> 13 | t1 << 3) & 8191);
|
|
481
|
+
let h2 = h[2] + ((t1 >>> 10 | t2 << 6) & 8191);
|
|
482
|
+
let h3 = h[3] + ((t2 >>> 7 | t3 << 9) & 8191);
|
|
483
|
+
let h4 = h[4] + ((t3 >>> 4 | t4 << 12) & 8191);
|
|
484
|
+
let h5 = h[5] + (t4 >>> 1 & 8191);
|
|
485
|
+
let h6 = h[6] + ((t4 >>> 14 | t5 << 2) & 8191);
|
|
486
|
+
let h7 = h[7] + ((t5 >>> 11 | t6 << 5) & 8191);
|
|
487
|
+
let h8 = h[8] + ((t6 >>> 8 | t7 << 8) & 8191);
|
|
488
|
+
let h9 = h[9] + (t7 >>> 5 | hibit);
|
|
489
|
+
let c = 0;
|
|
490
|
+
let d0 = c + h0 * r0 + h1 * (5 * r9) + h2 * (5 * r8) + h3 * (5 * r7) + h4 * (5 * r6);
|
|
491
|
+
c = d0 >>> 13;
|
|
492
|
+
d0 &= 8191;
|
|
493
|
+
d0 += h5 * (5 * r5) + h6 * (5 * r4) + h7 * (5 * r3) + h8 * (5 * r2) + h9 * (5 * r1);
|
|
494
|
+
c += d0 >>> 13;
|
|
495
|
+
d0 &= 8191;
|
|
496
|
+
let d1 = c + h0 * r1 + h1 * r0 + h2 * (5 * r9) + h3 * (5 * r8) + h4 * (5 * r7);
|
|
497
|
+
c = d1 >>> 13;
|
|
498
|
+
d1 &= 8191;
|
|
499
|
+
d1 += h5 * (5 * r6) + h6 * (5 * r5) + h7 * (5 * r4) + h8 * (5 * r3) + h9 * (5 * r2);
|
|
500
|
+
c += d1 >>> 13;
|
|
501
|
+
d1 &= 8191;
|
|
502
|
+
let d2 = c + h0 * r2 + h1 * r1 + h2 * r0 + h3 * (5 * r9) + h4 * (5 * r8);
|
|
503
|
+
c = d2 >>> 13;
|
|
504
|
+
d2 &= 8191;
|
|
505
|
+
d2 += h5 * (5 * r7) + h6 * (5 * r6) + h7 * (5 * r5) + h8 * (5 * r4) + h9 * (5 * r3);
|
|
506
|
+
c += d2 >>> 13;
|
|
507
|
+
d2 &= 8191;
|
|
508
|
+
let d3 = c + h0 * r3 + h1 * r2 + h2 * r1 + h3 * r0 + h4 * (5 * r9);
|
|
509
|
+
c = d3 >>> 13;
|
|
510
|
+
d3 &= 8191;
|
|
511
|
+
d3 += h5 * (5 * r8) + h6 * (5 * r7) + h7 * (5 * r6) + h8 * (5 * r5) + h9 * (5 * r4);
|
|
512
|
+
c += d3 >>> 13;
|
|
513
|
+
d3 &= 8191;
|
|
514
|
+
let d4 = c + h0 * r4 + h1 * r3 + h2 * r2 + h3 * r1 + h4 * r0;
|
|
515
|
+
c = d4 >>> 13;
|
|
516
|
+
d4 &= 8191;
|
|
517
|
+
d4 += h5 * (5 * r9) + h6 * (5 * r8) + h7 * (5 * r7) + h8 * (5 * r6) + h9 * (5 * r5);
|
|
518
|
+
c += d4 >>> 13;
|
|
519
|
+
d4 &= 8191;
|
|
520
|
+
let d5 = c + h0 * r5 + h1 * r4 + h2 * r3 + h3 * r2 + h4 * r1;
|
|
521
|
+
c = d5 >>> 13;
|
|
522
|
+
d5 &= 8191;
|
|
523
|
+
d5 += h5 * r0 + h6 * (5 * r9) + h7 * (5 * r8) + h8 * (5 * r7) + h9 * (5 * r6);
|
|
524
|
+
c += d5 >>> 13;
|
|
525
|
+
d5 &= 8191;
|
|
526
|
+
let d6 = c + h0 * r6 + h1 * r5 + h2 * r4 + h3 * r3 + h4 * r2;
|
|
527
|
+
c = d6 >>> 13;
|
|
528
|
+
d6 &= 8191;
|
|
529
|
+
d6 += h5 * r1 + h6 * r0 + h7 * (5 * r9) + h8 * (5 * r8) + h9 * (5 * r7);
|
|
530
|
+
c += d6 >>> 13;
|
|
531
|
+
d6 &= 8191;
|
|
532
|
+
let d7 = c + h0 * r7 + h1 * r6 + h2 * r5 + h3 * r4 + h4 * r3;
|
|
533
|
+
c = d7 >>> 13;
|
|
534
|
+
d7 &= 8191;
|
|
535
|
+
d7 += h5 * r2 + h6 * r1 + h7 * r0 + h8 * (5 * r9) + h9 * (5 * r8);
|
|
536
|
+
c += d7 >>> 13;
|
|
537
|
+
d7 &= 8191;
|
|
538
|
+
let d8 = c + h0 * r8 + h1 * r7 + h2 * r6 + h3 * r5 + h4 * r4;
|
|
539
|
+
c = d8 >>> 13;
|
|
540
|
+
d8 &= 8191;
|
|
541
|
+
d8 += h5 * r3 + h6 * r2 + h7 * r1 + h8 * r0 + h9 * (5 * r9);
|
|
542
|
+
c += d8 >>> 13;
|
|
543
|
+
d8 &= 8191;
|
|
544
|
+
let d9 = c + h0 * r9 + h1 * r8 + h2 * r7 + h3 * r6 + h4 * r5;
|
|
545
|
+
c = d9 >>> 13;
|
|
546
|
+
d9 &= 8191;
|
|
547
|
+
d9 += h5 * r4 + h6 * r3 + h7 * r2 + h8 * r1 + h9 * r0;
|
|
548
|
+
c += d9 >>> 13;
|
|
549
|
+
d9 &= 8191;
|
|
550
|
+
c = (c << 2) + c | 0;
|
|
551
|
+
c = c + d0 | 0;
|
|
552
|
+
d0 = c & 8191;
|
|
553
|
+
c = c >>> 13;
|
|
554
|
+
d1 += c;
|
|
555
|
+
h[0] = d0;
|
|
556
|
+
h[1] = d1;
|
|
557
|
+
h[2] = d2;
|
|
558
|
+
h[3] = d3;
|
|
559
|
+
h[4] = d4;
|
|
560
|
+
h[5] = d5;
|
|
561
|
+
h[6] = d6;
|
|
562
|
+
h[7] = d7;
|
|
563
|
+
h[8] = d8;
|
|
564
|
+
h[9] = d9;
|
|
565
|
+
}
|
|
566
|
+
finalize() {
|
|
567
|
+
const { h, pad } = this;
|
|
568
|
+
const g = new Uint16Array(10);
|
|
569
|
+
let c = h[1] >>> 13;
|
|
570
|
+
h[1] &= 8191;
|
|
571
|
+
for (let i = 2; i < 10; i++) {
|
|
572
|
+
h[i] += c;
|
|
573
|
+
c = h[i] >>> 13;
|
|
574
|
+
h[i] &= 8191;
|
|
575
|
+
}
|
|
576
|
+
h[0] += c * 5;
|
|
577
|
+
c = h[0] >>> 13;
|
|
578
|
+
h[0] &= 8191;
|
|
579
|
+
h[1] += c;
|
|
580
|
+
c = h[1] >>> 13;
|
|
581
|
+
h[1] &= 8191;
|
|
582
|
+
h[2] += c;
|
|
583
|
+
g[0] = h[0] + 5;
|
|
584
|
+
c = g[0] >>> 13;
|
|
585
|
+
g[0] &= 8191;
|
|
586
|
+
for (let i = 1; i < 10; i++) {
|
|
587
|
+
g[i] = h[i] + c;
|
|
588
|
+
c = g[i] >>> 13;
|
|
589
|
+
g[i] &= 8191;
|
|
590
|
+
}
|
|
591
|
+
g[9] -= 1 << 13;
|
|
592
|
+
let mask = (c ^ 1) - 1;
|
|
593
|
+
for (let i = 0; i < 10; i++)
|
|
594
|
+
g[i] &= mask;
|
|
595
|
+
mask = ~mask;
|
|
596
|
+
for (let i = 0; i < 10; i++)
|
|
597
|
+
h[i] = h[i] & mask | g[i];
|
|
598
|
+
h[0] = (h[0] | h[1] << 13) & 65535;
|
|
599
|
+
h[1] = (h[1] >>> 3 | h[2] << 10) & 65535;
|
|
600
|
+
h[2] = (h[2] >>> 6 | h[3] << 7) & 65535;
|
|
601
|
+
h[3] = (h[3] >>> 9 | h[4] << 4) & 65535;
|
|
602
|
+
h[4] = (h[4] >>> 12 | h[5] << 1 | h[6] << 14) & 65535;
|
|
603
|
+
h[5] = (h[6] >>> 2 | h[7] << 11) & 65535;
|
|
604
|
+
h[6] = (h[7] >>> 5 | h[8] << 8) & 65535;
|
|
605
|
+
h[7] = (h[8] >>> 8 | h[9] << 5) & 65535;
|
|
606
|
+
let f2 = h[0] + pad[0];
|
|
607
|
+
h[0] = f2 & 65535;
|
|
608
|
+
for (let i = 1; i < 8; i++) {
|
|
609
|
+
f2 = (h[i] + pad[i] | 0) + (f2 >>> 16) | 0;
|
|
610
|
+
h[i] = f2 & 65535;
|
|
611
|
+
}
|
|
612
|
+
clean(g);
|
|
613
|
+
}
|
|
614
|
+
update(data2) {
|
|
615
|
+
aexists(this);
|
|
616
|
+
data2 = toBytes(data2);
|
|
617
|
+
abytes(data2);
|
|
618
|
+
const { buffer, blockLen } = this;
|
|
619
|
+
const len = data2.length;
|
|
620
|
+
for (let pos = 0; pos < len; ) {
|
|
621
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
622
|
+
if (take === blockLen) {
|
|
623
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
624
|
+
this.process(data2, pos);
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
buffer.set(data2.subarray(pos, pos + take), this.pos);
|
|
628
|
+
this.pos += take;
|
|
629
|
+
pos += take;
|
|
630
|
+
if (this.pos === blockLen) {
|
|
631
|
+
this.process(buffer, 0, false);
|
|
632
|
+
this.pos = 0;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
return this;
|
|
636
|
+
}
|
|
637
|
+
destroy() {
|
|
638
|
+
clean(this.h, this.r, this.buffer, this.pad);
|
|
639
|
+
}
|
|
640
|
+
digestInto(out) {
|
|
641
|
+
aexists(this);
|
|
642
|
+
aoutput(out, this);
|
|
643
|
+
this.finished = true;
|
|
644
|
+
const { buffer, h } = this;
|
|
645
|
+
let { pos } = this;
|
|
646
|
+
if (pos) {
|
|
647
|
+
buffer[pos++] = 1;
|
|
648
|
+
for (; pos < 16; pos++)
|
|
649
|
+
buffer[pos] = 0;
|
|
650
|
+
this.process(buffer, 0, true);
|
|
651
|
+
}
|
|
652
|
+
this.finalize();
|
|
653
|
+
let opos = 0;
|
|
654
|
+
for (let i = 0; i < 8; i++) {
|
|
655
|
+
out[opos++] = h[i] >>> 0;
|
|
656
|
+
out[opos++] = h[i] >>> 8;
|
|
657
|
+
}
|
|
658
|
+
return out;
|
|
659
|
+
}
|
|
660
|
+
digest() {
|
|
661
|
+
const { buffer, outputLen } = this;
|
|
662
|
+
this.digestInto(buffer);
|
|
663
|
+
const res = buffer.slice(0, outputLen);
|
|
664
|
+
this.destroy();
|
|
665
|
+
return res;
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
function wrapConstructorWithKey(hashCons) {
|
|
669
|
+
const hashC = (msg, key2) => hashCons(key2).update(toBytes(msg)).digest();
|
|
670
|
+
const tmp = hashCons(new Uint8Array(32));
|
|
671
|
+
hashC.outputLen = tmp.outputLen;
|
|
672
|
+
hashC.blockLen = tmp.blockLen;
|
|
673
|
+
hashC.create = (key2) => hashCons(key2);
|
|
674
|
+
return hashC;
|
|
675
|
+
}
|
|
676
|
+
var poly1305 = wrapConstructorWithKey((key2) => new Poly1305(key2));
|
|
677
|
+
|
|
678
|
+
// ../../node_modules/@noble/ciphers/esm/chacha.js
|
|
679
|
+
function chachaCore(s, k, n, out, cnt, rounds = 20) {
|
|
680
|
+
let y00 = s[0], y01 = s[1], y02 = s[2], y03 = s[3], y04 = k[0], y05 = k[1], y06 = k[2], y07 = k[3], y08 = k[4], y09 = k[5], y10 = k[6], y11 = k[7], y12 = cnt, y13 = n[0], y14 = n[1], y15 = n[2];
|
|
681
|
+
let x00 = y00, x01 = y01, x02 = y02, x03 = y03, x04 = y04, x05 = y05, x06 = y06, x07 = y07, x08 = y08, x09 = y09, x10 = y10, x11 = y11, x12 = y12, x13 = y13, x14 = y14, x15 = y15;
|
|
682
|
+
for (let r = 0; r < rounds; r += 2) {
|
|
683
|
+
x00 = x00 + x04 | 0;
|
|
684
|
+
x12 = rotl(x12 ^ x00, 16);
|
|
685
|
+
x08 = x08 + x12 | 0;
|
|
686
|
+
x04 = rotl(x04 ^ x08, 12);
|
|
687
|
+
x00 = x00 + x04 | 0;
|
|
688
|
+
x12 = rotl(x12 ^ x00, 8);
|
|
689
|
+
x08 = x08 + x12 | 0;
|
|
690
|
+
x04 = rotl(x04 ^ x08, 7);
|
|
691
|
+
x01 = x01 + x05 | 0;
|
|
692
|
+
x13 = rotl(x13 ^ x01, 16);
|
|
693
|
+
x09 = x09 + x13 | 0;
|
|
694
|
+
x05 = rotl(x05 ^ x09, 12);
|
|
695
|
+
x01 = x01 + x05 | 0;
|
|
696
|
+
x13 = rotl(x13 ^ x01, 8);
|
|
697
|
+
x09 = x09 + x13 | 0;
|
|
698
|
+
x05 = rotl(x05 ^ x09, 7);
|
|
699
|
+
x02 = x02 + x06 | 0;
|
|
700
|
+
x14 = rotl(x14 ^ x02, 16);
|
|
701
|
+
x10 = x10 + x14 | 0;
|
|
702
|
+
x06 = rotl(x06 ^ x10, 12);
|
|
703
|
+
x02 = x02 + x06 | 0;
|
|
704
|
+
x14 = rotl(x14 ^ x02, 8);
|
|
705
|
+
x10 = x10 + x14 | 0;
|
|
706
|
+
x06 = rotl(x06 ^ x10, 7);
|
|
707
|
+
x03 = x03 + x07 | 0;
|
|
708
|
+
x15 = rotl(x15 ^ x03, 16);
|
|
709
|
+
x11 = x11 + x15 | 0;
|
|
710
|
+
x07 = rotl(x07 ^ x11, 12);
|
|
711
|
+
x03 = x03 + x07 | 0;
|
|
712
|
+
x15 = rotl(x15 ^ x03, 8);
|
|
713
|
+
x11 = x11 + x15 | 0;
|
|
714
|
+
x07 = rotl(x07 ^ x11, 7);
|
|
715
|
+
x00 = x00 + x05 | 0;
|
|
716
|
+
x15 = rotl(x15 ^ x00, 16);
|
|
717
|
+
x10 = x10 + x15 | 0;
|
|
718
|
+
x05 = rotl(x05 ^ x10, 12);
|
|
719
|
+
x00 = x00 + x05 | 0;
|
|
720
|
+
x15 = rotl(x15 ^ x00, 8);
|
|
721
|
+
x10 = x10 + x15 | 0;
|
|
722
|
+
x05 = rotl(x05 ^ x10, 7);
|
|
723
|
+
x01 = x01 + x06 | 0;
|
|
724
|
+
x12 = rotl(x12 ^ x01, 16);
|
|
725
|
+
x11 = x11 + x12 | 0;
|
|
726
|
+
x06 = rotl(x06 ^ x11, 12);
|
|
727
|
+
x01 = x01 + x06 | 0;
|
|
728
|
+
x12 = rotl(x12 ^ x01, 8);
|
|
729
|
+
x11 = x11 + x12 | 0;
|
|
730
|
+
x06 = rotl(x06 ^ x11, 7);
|
|
731
|
+
x02 = x02 + x07 | 0;
|
|
732
|
+
x13 = rotl(x13 ^ x02, 16);
|
|
733
|
+
x08 = x08 + x13 | 0;
|
|
734
|
+
x07 = rotl(x07 ^ x08, 12);
|
|
735
|
+
x02 = x02 + x07 | 0;
|
|
736
|
+
x13 = rotl(x13 ^ x02, 8);
|
|
737
|
+
x08 = x08 + x13 | 0;
|
|
738
|
+
x07 = rotl(x07 ^ x08, 7);
|
|
739
|
+
x03 = x03 + x04 | 0;
|
|
740
|
+
x14 = rotl(x14 ^ x03, 16);
|
|
741
|
+
x09 = x09 + x14 | 0;
|
|
742
|
+
x04 = rotl(x04 ^ x09, 12);
|
|
743
|
+
x03 = x03 + x04 | 0;
|
|
744
|
+
x14 = rotl(x14 ^ x03, 8);
|
|
745
|
+
x09 = x09 + x14 | 0;
|
|
746
|
+
x04 = rotl(x04 ^ x09, 7);
|
|
747
|
+
}
|
|
748
|
+
let oi = 0;
|
|
749
|
+
out[oi++] = y00 + x00 | 0;
|
|
750
|
+
out[oi++] = y01 + x01 | 0;
|
|
751
|
+
out[oi++] = y02 + x02 | 0;
|
|
752
|
+
out[oi++] = y03 + x03 | 0;
|
|
753
|
+
out[oi++] = y04 + x04 | 0;
|
|
754
|
+
out[oi++] = y05 + x05 | 0;
|
|
755
|
+
out[oi++] = y06 + x06 | 0;
|
|
756
|
+
out[oi++] = y07 + x07 | 0;
|
|
757
|
+
out[oi++] = y08 + x08 | 0;
|
|
758
|
+
out[oi++] = y09 + x09 | 0;
|
|
759
|
+
out[oi++] = y10 + x10 | 0;
|
|
760
|
+
out[oi++] = y11 + x11 | 0;
|
|
761
|
+
out[oi++] = y12 + x12 | 0;
|
|
762
|
+
out[oi++] = y13 + x13 | 0;
|
|
763
|
+
out[oi++] = y14 + x14 | 0;
|
|
764
|
+
out[oi++] = y15 + x15 | 0;
|
|
765
|
+
}
|
|
766
|
+
function hchacha(s, k, i, o32) {
|
|
767
|
+
let x00 = s[0], x01 = s[1], x02 = s[2], x03 = s[3], x04 = k[0], x05 = k[1], x06 = k[2], x07 = k[3], x08 = k[4], x09 = k[5], x10 = k[6], x11 = k[7], x12 = i[0], x13 = i[1], x14 = i[2], x15 = i[3];
|
|
768
|
+
for (let r = 0; r < 20; r += 2) {
|
|
769
|
+
x00 = x00 + x04 | 0;
|
|
770
|
+
x12 = rotl(x12 ^ x00, 16);
|
|
771
|
+
x08 = x08 + x12 | 0;
|
|
772
|
+
x04 = rotl(x04 ^ x08, 12);
|
|
773
|
+
x00 = x00 + x04 | 0;
|
|
774
|
+
x12 = rotl(x12 ^ x00, 8);
|
|
775
|
+
x08 = x08 + x12 | 0;
|
|
776
|
+
x04 = rotl(x04 ^ x08, 7);
|
|
777
|
+
x01 = x01 + x05 | 0;
|
|
778
|
+
x13 = rotl(x13 ^ x01, 16);
|
|
779
|
+
x09 = x09 + x13 | 0;
|
|
780
|
+
x05 = rotl(x05 ^ x09, 12);
|
|
781
|
+
x01 = x01 + x05 | 0;
|
|
782
|
+
x13 = rotl(x13 ^ x01, 8);
|
|
783
|
+
x09 = x09 + x13 | 0;
|
|
784
|
+
x05 = rotl(x05 ^ x09, 7);
|
|
785
|
+
x02 = x02 + x06 | 0;
|
|
786
|
+
x14 = rotl(x14 ^ x02, 16);
|
|
787
|
+
x10 = x10 + x14 | 0;
|
|
788
|
+
x06 = rotl(x06 ^ x10, 12);
|
|
789
|
+
x02 = x02 + x06 | 0;
|
|
790
|
+
x14 = rotl(x14 ^ x02, 8);
|
|
791
|
+
x10 = x10 + x14 | 0;
|
|
792
|
+
x06 = rotl(x06 ^ x10, 7);
|
|
793
|
+
x03 = x03 + x07 | 0;
|
|
794
|
+
x15 = rotl(x15 ^ x03, 16);
|
|
795
|
+
x11 = x11 + x15 | 0;
|
|
796
|
+
x07 = rotl(x07 ^ x11, 12);
|
|
797
|
+
x03 = x03 + x07 | 0;
|
|
798
|
+
x15 = rotl(x15 ^ x03, 8);
|
|
799
|
+
x11 = x11 + x15 | 0;
|
|
800
|
+
x07 = rotl(x07 ^ x11, 7);
|
|
801
|
+
x00 = x00 + x05 | 0;
|
|
802
|
+
x15 = rotl(x15 ^ x00, 16);
|
|
803
|
+
x10 = x10 + x15 | 0;
|
|
804
|
+
x05 = rotl(x05 ^ x10, 12);
|
|
805
|
+
x00 = x00 + x05 | 0;
|
|
806
|
+
x15 = rotl(x15 ^ x00, 8);
|
|
807
|
+
x10 = x10 + x15 | 0;
|
|
808
|
+
x05 = rotl(x05 ^ x10, 7);
|
|
809
|
+
x01 = x01 + x06 | 0;
|
|
810
|
+
x12 = rotl(x12 ^ x01, 16);
|
|
811
|
+
x11 = x11 + x12 | 0;
|
|
812
|
+
x06 = rotl(x06 ^ x11, 12);
|
|
813
|
+
x01 = x01 + x06 | 0;
|
|
814
|
+
x12 = rotl(x12 ^ x01, 8);
|
|
815
|
+
x11 = x11 + x12 | 0;
|
|
816
|
+
x06 = rotl(x06 ^ x11, 7);
|
|
817
|
+
x02 = x02 + x07 | 0;
|
|
818
|
+
x13 = rotl(x13 ^ x02, 16);
|
|
819
|
+
x08 = x08 + x13 | 0;
|
|
820
|
+
x07 = rotl(x07 ^ x08, 12);
|
|
821
|
+
x02 = x02 + x07 | 0;
|
|
822
|
+
x13 = rotl(x13 ^ x02, 8);
|
|
823
|
+
x08 = x08 + x13 | 0;
|
|
824
|
+
x07 = rotl(x07 ^ x08, 7);
|
|
825
|
+
x03 = x03 + x04 | 0;
|
|
826
|
+
x14 = rotl(x14 ^ x03, 16);
|
|
827
|
+
x09 = x09 + x14 | 0;
|
|
828
|
+
x04 = rotl(x04 ^ x09, 12);
|
|
829
|
+
x03 = x03 + x04 | 0;
|
|
830
|
+
x14 = rotl(x14 ^ x03, 8);
|
|
831
|
+
x09 = x09 + x14 | 0;
|
|
832
|
+
x04 = rotl(x04 ^ x09, 7);
|
|
833
|
+
}
|
|
834
|
+
let oi = 0;
|
|
835
|
+
o32[oi++] = x00;
|
|
836
|
+
o32[oi++] = x01;
|
|
837
|
+
o32[oi++] = x02;
|
|
838
|
+
o32[oi++] = x03;
|
|
839
|
+
o32[oi++] = x12;
|
|
840
|
+
o32[oi++] = x13;
|
|
841
|
+
o32[oi++] = x14;
|
|
842
|
+
o32[oi++] = x15;
|
|
843
|
+
}
|
|
844
|
+
var chacha20 = /* @__PURE__ */ createCipher(chachaCore, {
|
|
845
|
+
counterRight: false,
|
|
846
|
+
counterLength: 4,
|
|
847
|
+
allowShortKeys: false
|
|
848
|
+
});
|
|
849
|
+
var xchacha20 = /* @__PURE__ */ createCipher(chachaCore, {
|
|
850
|
+
counterRight: false,
|
|
851
|
+
counterLength: 8,
|
|
852
|
+
extendNonceFn: hchacha,
|
|
853
|
+
allowShortKeys: false
|
|
854
|
+
});
|
|
855
|
+
var ZEROS16 = /* @__PURE__ */ new Uint8Array(16);
|
|
856
|
+
var updatePadded = (h, msg) => {
|
|
857
|
+
h.update(msg);
|
|
858
|
+
const left = msg.length % 16;
|
|
859
|
+
if (left)
|
|
860
|
+
h.update(ZEROS16.subarray(left));
|
|
861
|
+
};
|
|
862
|
+
var ZEROS32 = /* @__PURE__ */ new Uint8Array(32);
|
|
863
|
+
function computeTag(fn, key2, nonce2, data2, AAD) {
|
|
864
|
+
const authKey = fn(key2, nonce2, ZEROS32);
|
|
865
|
+
const h = poly1305.create(authKey);
|
|
866
|
+
if (AAD)
|
|
867
|
+
updatePadded(h, AAD);
|
|
868
|
+
updatePadded(h, data2);
|
|
869
|
+
const num = u64Lengths(data2.length, AAD ? AAD.length : 0, true);
|
|
870
|
+
h.update(num);
|
|
871
|
+
const res = h.digest();
|
|
872
|
+
clean(authKey, num);
|
|
873
|
+
return res;
|
|
874
|
+
}
|
|
875
|
+
var _poly1305_aead = (xorStream) => (key2, nonce2, AAD) => {
|
|
876
|
+
const tagLength = 16;
|
|
877
|
+
return {
|
|
878
|
+
encrypt(plaintext, output) {
|
|
879
|
+
const plength = plaintext.length;
|
|
880
|
+
output = getOutput(plength + tagLength, output, false);
|
|
881
|
+
output.set(plaintext);
|
|
882
|
+
const oPlain = output.subarray(0, -tagLength);
|
|
883
|
+
xorStream(key2, nonce2, oPlain, oPlain, 1);
|
|
884
|
+
const tag = computeTag(xorStream, key2, nonce2, oPlain, AAD);
|
|
885
|
+
output.set(tag, plength);
|
|
886
|
+
clean(tag);
|
|
887
|
+
return output;
|
|
888
|
+
},
|
|
889
|
+
decrypt(ciphertext, output) {
|
|
890
|
+
output = getOutput(ciphertext.length - tagLength, output, false);
|
|
891
|
+
const data2 = ciphertext.subarray(0, -tagLength);
|
|
892
|
+
const passedTag = ciphertext.subarray(-tagLength);
|
|
893
|
+
const tag = computeTag(xorStream, key2, nonce2, data2, AAD);
|
|
894
|
+
if (!equalBytes(passedTag, tag))
|
|
895
|
+
throw new Error("invalid tag");
|
|
896
|
+
output.set(ciphertext.subarray(0, -tagLength));
|
|
897
|
+
xorStream(key2, nonce2, output, output, 1);
|
|
898
|
+
clean(tag);
|
|
899
|
+
return output;
|
|
900
|
+
}
|
|
901
|
+
};
|
|
902
|
+
};
|
|
903
|
+
var chacha20poly1305 = /* @__PURE__ */ wrapCipher({ blockSize: 64, nonceLength: 12, tagLength: 16 }, _poly1305_aead(chacha20));
|
|
904
|
+
var xchacha20poly1305 = /* @__PURE__ */ wrapCipher({ blockSize: 64, nonceLength: 24, tagLength: 16 }, _poly1305_aead(xchacha20));
|
|
905
|
+
|
|
906
|
+
// src/memory/embedder/session.ts
|
|
907
|
+
function deriveSessionKeys(token, ns, nc) {
|
|
908
|
+
const ikm = Buffer.from(token, "hex");
|
|
909
|
+
const salt = Buffer.from(ns + nc, "hex");
|
|
910
|
+
const k = (label) => new Uint8Array(hkdfSync("sha256", ikm, salt, `securecoms-embedder ${label} v1`, 32));
|
|
911
|
+
return { c2s: k("c2s"), s2c: k("s2c") };
|
|
912
|
+
}
|
|
913
|
+
var SealedChannel = class {
|
|
914
|
+
constructor(sendKey, recvKey, sendLabel, recvLabel) {
|
|
915
|
+
this.sendKey = sendKey;
|
|
916
|
+
this.recvKey = recvKey;
|
|
917
|
+
this.sendLabel = sendLabel;
|
|
918
|
+
this.recvLabel = recvLabel;
|
|
919
|
+
}
|
|
920
|
+
sendSeq = 0;
|
|
921
|
+
recvSeq = 0;
|
|
922
|
+
seal(frame) {
|
|
923
|
+
const nonce2 = randomBytes2(24);
|
|
924
|
+
const aad = new TextEncoder().encode(`${this.sendLabel}:${this.sendSeq++}`);
|
|
925
|
+
const ct = xchacha20poly1305(this.sendKey, nonce2, aad).encrypt(new TextEncoder().encode(JSON.stringify(frame)));
|
|
926
|
+
return Buffer.concat([nonce2, Buffer.from(ct)]).toString("base64");
|
|
927
|
+
}
|
|
928
|
+
/** Null on any failure (bad base64, wrong key, tampering, replay): the caller closes the connection. */
|
|
929
|
+
open(line) {
|
|
930
|
+
try {
|
|
931
|
+
const b = Buffer.from(line, "base64");
|
|
932
|
+
if (b.length < 24 + 16) return null;
|
|
933
|
+
const aad = new TextEncoder().encode(`${this.recvLabel}:${this.recvSeq}`);
|
|
934
|
+
const pt = xchacha20poly1305(this.recvKey, b.subarray(0, 24), aad).decrypt(b.subarray(24));
|
|
935
|
+
this.recvSeq++;
|
|
936
|
+
const f2 = JSON.parse(new TextDecoder().decode(pt));
|
|
937
|
+
return f2 && typeof f2 === "object" && !Array.isArray(f2) ? f2 : null;
|
|
938
|
+
} catch {
|
|
939
|
+
return null;
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
|
|
944
|
+
// src/memory/embedder/server.ts
|
|
945
|
+
import { chmodSync, existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync2, renameSync as renameSync2, rmSync as rmSync2, statSync, writeFileSync } from "node:fs";
|
|
946
|
+
|
|
947
|
+
// src/memory/embedder/locks.ts
|
|
948
|
+
import { closeSync, constants, fstatSync, linkSync, openSync, readFileSync, renameSync, rmSync, writeSync } from "node:fs";
|
|
949
|
+
import { randomBytes as randomBytes3 } from "node:crypto";
|
|
950
|
+
import { uptime } from "node:os";
|
|
951
|
+
var LOCK_STALE_MS = 1e4;
|
|
952
|
+
var BOOT_TOLERANCE_S = 60;
|
|
953
|
+
var alive = (pid) => {
|
|
954
|
+
try {
|
|
955
|
+
process.kill(pid, 0);
|
|
956
|
+
return true;
|
|
957
|
+
} catch (e) {
|
|
958
|
+
return e.code === "EPERM";
|
|
959
|
+
}
|
|
960
|
+
};
|
|
961
|
+
var PROCESS_LOCK_ENV = {
|
|
962
|
+
pid: process.pid,
|
|
963
|
+
alive,
|
|
964
|
+
bootTime: () => Math.round((Date.now() - uptime() * 1e3) / 1e3),
|
|
965
|
+
startTime: () => Math.round(Date.now() - process.uptime() * 1e3)
|
|
966
|
+
};
|
|
967
|
+
function inspect(path) {
|
|
968
|
+
let fd;
|
|
969
|
+
try {
|
|
970
|
+
fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
971
|
+
} catch {
|
|
972
|
+
return null;
|
|
973
|
+
}
|
|
974
|
+
try {
|
|
975
|
+
const st = fstatSync(fd, { bigint: true });
|
|
976
|
+
return { dev: st.dev, ino: st.ino, mtimeNs: st.mtimeNs, mtimeMs: Number(st.mtimeMs), text: readFileSync(fd, "utf8") };
|
|
977
|
+
} catch {
|
|
978
|
+
return null;
|
|
979
|
+
} finally {
|
|
980
|
+
closeSync(fd);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
var sameLock = (a, b) => a !== null && a.dev === b.dev && a.ino === b.ino && a.mtimeNs === b.mtimeNs && a.text === b.text;
|
|
984
|
+
function holderDead(text, env) {
|
|
985
|
+
const [pidS, bootS] = text.split(" ");
|
|
986
|
+
const pid = Number(pidS), boot = Number(bootS);
|
|
987
|
+
if (Number.isInteger(boot) && boot > 0 && Math.abs(boot - env.bootTime()) > BOOT_TOLERANCE_S) return true;
|
|
988
|
+
return !(Number.isInteger(pid) && pid > 0 && env.alive(pid));
|
|
989
|
+
}
|
|
990
|
+
function takeLock(path, now = Date.now, env = PROCESS_LOCK_ENV) {
|
|
991
|
+
const id = `${env.pid} ${env.bootTime()} ${env.startTime()} ${randomBytes3(8).toString("hex")}`;
|
|
992
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
993
|
+
try {
|
|
994
|
+
const fd = openSync(path, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY, 384);
|
|
995
|
+
try {
|
|
996
|
+
writeSync(fd, id);
|
|
997
|
+
} finally {
|
|
998
|
+
closeSync(fd);
|
|
999
|
+
}
|
|
1000
|
+
return { id };
|
|
1001
|
+
} catch (e) {
|
|
1002
|
+
if (e.code !== "EEXIST") throw e;
|
|
1003
|
+
}
|
|
1004
|
+
const seen = inspect(path);
|
|
1005
|
+
if (!seen) continue;
|
|
1006
|
+
if (!holderDead(seen.text, env) || now() - seen.mtimeMs < LOCK_STALE_MS) return null;
|
|
1007
|
+
env.beforeSteal?.();
|
|
1008
|
+
const aside = `${path}.stale.${randomBytes3(6).toString("hex")}`;
|
|
1009
|
+
try {
|
|
1010
|
+
renameSync(path, aside);
|
|
1011
|
+
} catch {
|
|
1012
|
+
continue;
|
|
1013
|
+
}
|
|
1014
|
+
if (!sameLock(inspect(aside), seen)) {
|
|
1015
|
+
try {
|
|
1016
|
+
linkSync(aside, path);
|
|
1017
|
+
} catch {
|
|
1018
|
+
}
|
|
1019
|
+
rmSync(aside, { force: true });
|
|
1020
|
+
return null;
|
|
1021
|
+
}
|
|
1022
|
+
rmSync(aside, { force: true });
|
|
1023
|
+
}
|
|
1024
|
+
return null;
|
|
1025
|
+
}
|
|
1026
|
+
function releaseLock(path, id) {
|
|
1027
|
+
try {
|
|
1028
|
+
if (readFileSync(path, "utf8") === id) rmSync(path, { force: true });
|
|
1029
|
+
} catch {
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
// src/service-log.ts
|
|
1034
|
+
var ENUM_VALUE = /^[a-z][a-z_]{0,31}$/;
|
|
1035
|
+
var ServiceLogEnum = class _ServiceLogEnum {
|
|
1036
|
+
constructor(value) {
|
|
1037
|
+
this.value = value;
|
|
1038
|
+
}
|
|
1039
|
+
static of(value, allowed) {
|
|
1040
|
+
return new _ServiceLogEnum(allowed.includes(value) && ENUM_VALUE.test(value) ? value : "invalid");
|
|
1041
|
+
}
|
|
1042
|
+
};
|
|
1043
|
+
var TOKEN = /^[a-z][a-z0-9_.-]{0,63}$/;
|
|
1044
|
+
var CLASS = /^[A-Za-z_$][A-Za-z0-9_$]{0,63}$/;
|
|
1045
|
+
function errorClass(error) {
|
|
1046
|
+
if (error === null) return "NullThrown";
|
|
1047
|
+
if (typeof error !== "object" && typeof error !== "function") {
|
|
1048
|
+
const primitive = {
|
|
1049
|
+
string: "StringThrown",
|
|
1050
|
+
number: "NumberThrown",
|
|
1051
|
+
boolean: "BooleanThrown",
|
|
1052
|
+
bigint: "BigIntThrown",
|
|
1053
|
+
symbol: "SymbolThrown",
|
|
1054
|
+
undefined: "UndefinedThrown"
|
|
1055
|
+
};
|
|
1056
|
+
return primitive[typeof error] ?? "UnknownThrown";
|
|
1057
|
+
}
|
|
1058
|
+
try {
|
|
1059
|
+
const name = Object.getPrototypeOf(error)?.constructor?.name;
|
|
1060
|
+
return typeof name === "string" && CLASS.test(name) ? name : "Error";
|
|
1061
|
+
} catch {
|
|
1062
|
+
return "Error";
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
function formatServiceLog(level, event, fields = {}, error) {
|
|
1066
|
+
const safeEvent = TOKEN.test(event) ? event : "invalid_event";
|
|
1067
|
+
const parts = [`[bridge] ${level} ${safeEvent}`];
|
|
1068
|
+
for (const [key2, value] of Object.entries(fields).sort(([a], [b]) => a.localeCompare(b))) {
|
|
1069
|
+
if (!TOKEN.test(key2)) continue;
|
|
1070
|
+
if (typeof value === "boolean") parts.push(`${key2}=${value}`);
|
|
1071
|
+
else if (typeof value === "number" && Number.isFinite(value)) parts.push(`${key2}=${value}`);
|
|
1072
|
+
else if (value instanceof ServiceLogEnum && ENUM_VALUE.test(value.value)) parts.push(`${key2}=${value.value}`);
|
|
1073
|
+
}
|
|
1074
|
+
if (error !== void 0) parts.push(`error_class=${errorClass(error)}`);
|
|
1075
|
+
return parts.join(" ");
|
|
1076
|
+
}
|
|
1077
|
+
function writeServiceLog(level, event, fields, error) {
|
|
1078
|
+
const line = formatServiceLog(level, event, fields, error);
|
|
1079
|
+
process.stderr.write(`${line}
|
|
1080
|
+
`);
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
// src/memory/embedder/scheduler.ts
|
|
1084
|
+
var MAX_QUERIES_PER_CONN = 4;
|
|
1085
|
+
var MAX_BACKGROUND_PER_CONN = 1;
|
|
1086
|
+
var MAX_CONNECTIONS = 32;
|
|
1087
|
+
var BACKGROUND_PER_SECOND = 16;
|
|
1088
|
+
var Scheduler = class {
|
|
1089
|
+
constructor(o) {
|
|
1090
|
+
this.o = o;
|
|
1091
|
+
}
|
|
1092
|
+
q = [/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map()];
|
|
1093
|
+
last = [-1, -1];
|
|
1094
|
+
// last connection served, per class
|
|
1095
|
+
bgStarts = [];
|
|
1096
|
+
enqueue(j) {
|
|
1097
|
+
const m = this.q[j.priority];
|
|
1098
|
+
const list = m.get(j.conn) ?? [];
|
|
1099
|
+
if (list.length >= (j.priority === 0 ? MAX_QUERIES_PER_CONN : MAX_BACKGROUND_PER_CONN)) return "busy";
|
|
1100
|
+
list.push(j);
|
|
1101
|
+
m.set(j.conn, list);
|
|
1102
|
+
return "ok";
|
|
1103
|
+
}
|
|
1104
|
+
cancel(conn, jobId) {
|
|
1105
|
+
for (const m of this.q) {
|
|
1106
|
+
const list = m.get(conn);
|
|
1107
|
+
if (!list) continue;
|
|
1108
|
+
const i = list.findIndex((j) => j.jobId === jobId);
|
|
1109
|
+
if (i >= 0) {
|
|
1110
|
+
list.splice(i, 1);
|
|
1111
|
+
return true;
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
return false;
|
|
1115
|
+
}
|
|
1116
|
+
dropConnection(conn) {
|
|
1117
|
+
for (const m of this.q) m.delete(conn);
|
|
1118
|
+
}
|
|
1119
|
+
size() {
|
|
1120
|
+
let n = 0;
|
|
1121
|
+
for (const m of this.q) for (const l of m.values()) n += l.length;
|
|
1122
|
+
return n;
|
|
1123
|
+
}
|
|
1124
|
+
take(p) {
|
|
1125
|
+
const conns = [...this.q[p].entries()].filter(([, l]) => l.length > 0).map(([c2]) => c2).sort((a, b) => a - b);
|
|
1126
|
+
if (conns.length === 0) return void 0;
|
|
1127
|
+
const c = conns.find((x) => x > this.last[p]) ?? conns[0];
|
|
1128
|
+
this.last[p] = c;
|
|
1129
|
+
return this.q[p].get(c).shift();
|
|
1130
|
+
}
|
|
1131
|
+
next() {
|
|
1132
|
+
const q = this.take(0);
|
|
1133
|
+
if (q) return q;
|
|
1134
|
+
if (this.o.backgroundHeld()) return void 0;
|
|
1135
|
+
const now = this.o.now();
|
|
1136
|
+
this.bgStarts = this.bgStarts.filter((t) => now - t < 1e3);
|
|
1137
|
+
if (this.bgStarts.length >= BACKGROUND_PER_SECOND) return void 0;
|
|
1138
|
+
const b = this.take(1);
|
|
1139
|
+
if (b) this.bgStarts.push(now);
|
|
1140
|
+
return b;
|
|
1141
|
+
}
|
|
1142
|
+
};
|
|
1143
|
+
|
|
1144
|
+
// src/memory/embedder/server.ts
|
|
1145
|
+
var AlreadyRunning = class extends Error {
|
|
1146
|
+
};
|
|
1147
|
+
var RESET_TIMEOUT_MS = 3e4;
|
|
1148
|
+
var MAX_PENDING_WRITE_BYTES = 1 << 20;
|
|
1149
|
+
var live = (sock) => new Promise((r) => {
|
|
1150
|
+
const c = connect(sock);
|
|
1151
|
+
c.once("connect", () => {
|
|
1152
|
+
c.destroy();
|
|
1153
|
+
r(true);
|
|
1154
|
+
});
|
|
1155
|
+
c.once("error", () => r(false));
|
|
1156
|
+
});
|
|
1157
|
+
var bindPath = (sock) => `${sock}.${randomBytes4(4).toString("hex")}`;
|
|
1158
|
+
var SocketPathTooLong = class extends Error {
|
|
1159
|
+
};
|
|
1160
|
+
async function listenUnix(server, sock) {
|
|
1161
|
+
const bound = bindPath(sock);
|
|
1162
|
+
await new Promise((res, rej) => {
|
|
1163
|
+
server.once("error", rej);
|
|
1164
|
+
server.listen(bound, () => {
|
|
1165
|
+
server.off("error", rej);
|
|
1166
|
+
res();
|
|
1167
|
+
});
|
|
1168
|
+
});
|
|
1169
|
+
chmodSync(bound, 384);
|
|
1170
|
+
renameSync2(bound, sock);
|
|
1171
|
+
}
|
|
1172
|
+
async function startEmbedderServer(o) {
|
|
1173
|
+
const log = o.log ?? writeServiceLog;
|
|
1174
|
+
const tcp = (o.transport ?? (process.platform === "win32" ? "tcp" : "unix")) === "tcp";
|
|
1175
|
+
if (!tcp && Buffer.byteLength(bindPath(embedderPaths(o.dir, o.key).sock)) > SOCKET_PATH_MAX) throw new SocketPathTooLong("embedder socket path too long");
|
|
1176
|
+
if (process.platform !== "win32") ensureRunDir(o.dir, o.uid);
|
|
1177
|
+
else mkdirSync2(o.dir, { recursive: true });
|
|
1178
|
+
const p = embedderPaths(o.dir, o.key);
|
|
1179
|
+
const lock = takeLock(p.pid, o.now);
|
|
1180
|
+
if (!lock) throw new AlreadyRunning("embedder already running");
|
|
1181
|
+
let published = "";
|
|
1182
|
+
const server = createServer();
|
|
1183
|
+
try {
|
|
1184
|
+
if (!tcp && existsSync2(p.sock)) {
|
|
1185
|
+
if (await live(p.sock)) throw new AlreadyRunning("embedder already running");
|
|
1186
|
+
rmSync2(p.sock, { force: true });
|
|
1187
|
+
}
|
|
1188
|
+
const token = randomBytes4(32).toString("hex");
|
|
1189
|
+
const publish = (extra = {}) => {
|
|
1190
|
+
rmSync2(`${p.token}.tmp`, { force: true });
|
|
1191
|
+
writeFileSync(`${p.token}.tmp`, JSON.stringify({ ...extra, token }), { mode: 384, flag: "wx" });
|
|
1192
|
+
renameSync2(`${p.token}.tmp`, p.token);
|
|
1193
|
+
published = readFileSync2(p.token, "utf8");
|
|
1194
|
+
};
|
|
1195
|
+
if (!tcp) publish();
|
|
1196
|
+
return await serve(o, log, p, lock, token, () => published, server, tcp, publish);
|
|
1197
|
+
} catch (e) {
|
|
1198
|
+
server.close();
|
|
1199
|
+
try {
|
|
1200
|
+
if (published && readFileSync2(p.token, "utf8") === published) rmSync2(p.token, { force: true });
|
|
1201
|
+
} catch {
|
|
1202
|
+
}
|
|
1203
|
+
releaseLock(p.pid, lock.id);
|
|
1204
|
+
throw e;
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
async function serve(o, log, p, lock, token, published, server, tcp, publish) {
|
|
1208
|
+
const sched = new Scheduler({ now: o.now ?? Date.now, backgroundHeld: o.backgroundHeld ?? (() => false) });
|
|
1209
|
+
const conns = /* @__PURE__ */ new Map();
|
|
1210
|
+
const all = /* @__PURE__ */ new Set();
|
|
1211
|
+
const chans = /* @__PURE__ */ new Map();
|
|
1212
|
+
let ordinal = 0;
|
|
1213
|
+
let running = null;
|
|
1214
|
+
let idleTimer;
|
|
1215
|
+
let retryTimer;
|
|
1216
|
+
let stopped = false;
|
|
1217
|
+
const touchIdle = () => {
|
|
1218
|
+
clearTimeout(idleTimer);
|
|
1219
|
+
idleTimer = setTimeout(() => void stop("idle"), o.idleMs ?? 30 * 6e4);
|
|
1220
|
+
};
|
|
1221
|
+
const send = (conn, f2) => {
|
|
1222
|
+
const s = conns.get(conn);
|
|
1223
|
+
const c = chans.get(conn);
|
|
1224
|
+
if (!s || !c || s.destroyed) return;
|
|
1225
|
+
s.write(c.seal(f2) + "\n");
|
|
1226
|
+
if (s.writableLength > MAX_PENDING_WRITE_BYTES) {
|
|
1227
|
+
log("warn", "embedder.slow_reader_closed");
|
|
1228
|
+
s.destroy();
|
|
1229
|
+
}
|
|
1230
|
+
};
|
|
1231
|
+
const pump = async () => {
|
|
1232
|
+
if (running || stopped) return;
|
|
1233
|
+
const job = sched.next();
|
|
1234
|
+
if (!job) {
|
|
1235
|
+
if (sched.size() > 0 && !retryTimer) retryTimer = setTimeout(() => {
|
|
1236
|
+
retryTimer = void 0;
|
|
1237
|
+
void pump();
|
|
1238
|
+
}, o.retryMs ?? 250);
|
|
1239
|
+
return;
|
|
1240
|
+
}
|
|
1241
|
+
running = { job, cancelled: false };
|
|
1242
|
+
const r = running;
|
|
1243
|
+
let timer;
|
|
1244
|
+
let resetFailed = false;
|
|
1245
|
+
try {
|
|
1246
|
+
const vec = await Promise.race([o.embed(job.text), new Promise((res) => {
|
|
1247
|
+
timer = setTimeout(() => res("timeout"), o.jobTimeoutMs ?? 2e3);
|
|
1248
|
+
})]);
|
|
1249
|
+
if (vec === "timeout") {
|
|
1250
|
+
log("warn", "embedder.worker_reset");
|
|
1251
|
+
let resetTimer;
|
|
1252
|
+
const ok = await Promise.race([
|
|
1253
|
+
o.resetInner().then(() => true, () => false),
|
|
1254
|
+
new Promise((res) => {
|
|
1255
|
+
resetTimer = setTimeout(() => res(false), o.resetTimeoutMs ?? RESET_TIMEOUT_MS);
|
|
1256
|
+
})
|
|
1257
|
+
]);
|
|
1258
|
+
clearTimeout(resetTimer);
|
|
1259
|
+
resetFailed = !ok;
|
|
1260
|
+
if (!r.cancelled) send(job.conn, { t: "error", job_id: job.jobId, code: "worker_reset" });
|
|
1261
|
+
} else if (!r.cancelled) send(job.conn, { t: "result", job_id: job.jobId, model_id: o.hello.model_id, vec: encodeVec64(vec) });
|
|
1262
|
+
} catch {
|
|
1263
|
+
if (!r.cancelled) send(job.conn, { t: "error", job_id: job.jobId, code: "failed" });
|
|
1264
|
+
} finally {
|
|
1265
|
+
clearTimeout(timer);
|
|
1266
|
+
running = null;
|
|
1267
|
+
}
|
|
1268
|
+
if (resetFailed) {
|
|
1269
|
+
log("error", "embedder.reset_failed");
|
|
1270
|
+
await stop("reset_failed");
|
|
1271
|
+
return;
|
|
1272
|
+
}
|
|
1273
|
+
void pump();
|
|
1274
|
+
};
|
|
1275
|
+
let open = 0;
|
|
1276
|
+
let preauth = 0;
|
|
1277
|
+
server.on("connection", (sock) => {
|
|
1278
|
+
if (stopped || open >= MAX_CONNECTIONS || preauth >= (tcp ? PREAUTH_MAX_CONNECTIONS_TCP : PREAUTH_MAX_CONNECTIONS)) {
|
|
1279
|
+
sock.destroy();
|
|
1280
|
+
return;
|
|
1281
|
+
}
|
|
1282
|
+
open++;
|
|
1283
|
+
preauth++;
|
|
1284
|
+
all.add(sock);
|
|
1285
|
+
let authed = false;
|
|
1286
|
+
const conn = ++ordinal;
|
|
1287
|
+
const ns = nonce();
|
|
1288
|
+
const helloTimer = setTimeout(() => sock.destroy(), o.helloTimeoutMs ?? PREAUTH_TIMEOUT_MS);
|
|
1289
|
+
sock.once("close", () => {
|
|
1290
|
+
open--;
|
|
1291
|
+
if (!authed) preauth--;
|
|
1292
|
+
all.delete(sock);
|
|
1293
|
+
clearTimeout(helloTimer);
|
|
1294
|
+
conns.delete(conn);
|
|
1295
|
+
chans.delete(conn);
|
|
1296
|
+
sched.dropConnection(conn);
|
|
1297
|
+
});
|
|
1298
|
+
sock.on("error", () => sock.destroy());
|
|
1299
|
+
sock.write(JSON.stringify({ t: "challenge", protocol: EMBEDDER_PROTOCOL, ns }) + "\n");
|
|
1300
|
+
sock.on("data", lineReader((line) => {
|
|
1301
|
+
if (sock.destroyed) return;
|
|
1302
|
+
let f2;
|
|
1303
|
+
const chan = chans.get(conn);
|
|
1304
|
+
if (chan) {
|
|
1305
|
+
f2 = chan.open(line);
|
|
1306
|
+
if (!f2) {
|
|
1307
|
+
log("warn", "embedder.frame_rejected");
|
|
1308
|
+
sock.destroy();
|
|
1309
|
+
return;
|
|
1310
|
+
}
|
|
1311
|
+
} else {
|
|
1312
|
+
try {
|
|
1313
|
+
f2 = JSON.parse(line);
|
|
1314
|
+
} catch {
|
|
1315
|
+
sock.destroy();
|
|
1316
|
+
return;
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
if (!f2 || typeof f2 !== "object") {
|
|
1320
|
+
sock.destroy();
|
|
1321
|
+
return;
|
|
1322
|
+
}
|
|
1323
|
+
if (!authed) {
|
|
1324
|
+
const nc = typeof f2.nc === "string" && /^[0-9a-f]{32}$/.test(f2.nc) ? f2.nc : null;
|
|
1325
|
+
if (f2.t !== "hello" || !nc || typeof f2.mac_c !== "string" || !macEqual(f2.mac_c, macClient(token, ns, nc))) {
|
|
1326
|
+
sock.destroy();
|
|
1327
|
+
return;
|
|
1328
|
+
}
|
|
1329
|
+
authed = true;
|
|
1330
|
+
preauth--;
|
|
1331
|
+
clearTimeout(helloTimer);
|
|
1332
|
+
conns.set(conn, sock);
|
|
1333
|
+
sock.write(JSON.stringify({ t: "hello", mac_s: macServer(token, ns, nc), ...o.hello }) + "\n");
|
|
1334
|
+
const k = deriveSessionKeys(token, ns, nc);
|
|
1335
|
+
chans.set(conn, new SealedChannel(k.s2c, k.c2s, "s2c", "c2s"));
|
|
1336
|
+
return;
|
|
1337
|
+
}
|
|
1338
|
+
if (f2.t === "cancel") {
|
|
1339
|
+
if (!Number.isSafeInteger(f2.job_id)) {
|
|
1340
|
+
sock.destroy();
|
|
1341
|
+
return;
|
|
1342
|
+
}
|
|
1343
|
+
const cid = f2.job_id;
|
|
1344
|
+
if (!sched.cancel(conn, cid) && running && running.job.conn === conn && running.job.jobId === cid) running.cancelled = true;
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
if (f2.t !== "job" || !Number.isSafeInteger(f2.job_id) || typeof f2.text !== "string" || f2.priority !== 0 && f2.priority !== 1) {
|
|
1348
|
+
sock.destroy();
|
|
1349
|
+
return;
|
|
1350
|
+
}
|
|
1351
|
+
const jobId = f2.job_id;
|
|
1352
|
+
touchIdle();
|
|
1353
|
+
if (Buffer.byteLength(f2.text) > MAX_TEXT_BYTES) {
|
|
1354
|
+
send(conn, { t: "error", job_id: jobId, code: "too_long" });
|
|
1355
|
+
return;
|
|
1356
|
+
}
|
|
1357
|
+
if (sched.enqueue({ conn, jobId, priority: f2.priority, text: f2.text }) === "busy") {
|
|
1358
|
+
send(conn, { t: "error", job_id: jobId, code: "busy" });
|
|
1359
|
+
return;
|
|
1360
|
+
}
|
|
1361
|
+
void pump();
|
|
1362
|
+
}, () => sock.destroy(), () => authed ? MAX_LINE_BYTES : PREAUTH_MAX_LINE));
|
|
1363
|
+
});
|
|
1364
|
+
if (tcp) {
|
|
1365
|
+
await new Promise((res, rej) => {
|
|
1366
|
+
server.once("error", rej);
|
|
1367
|
+
server.listen({ host: "127.0.0.1", port: 0 }, () => {
|
|
1368
|
+
server.off("error", rej);
|
|
1369
|
+
res();
|
|
1370
|
+
});
|
|
1371
|
+
});
|
|
1372
|
+
publish({ port: server.address().port });
|
|
1373
|
+
} else await listenUnix(server, p.sock);
|
|
1374
|
+
const ident = () => {
|
|
1375
|
+
const st = statSync(p.sock, { bigint: true });
|
|
1376
|
+
return `${st.dev}:${st.ino}:${st.birthtimeNs}:${st.ctimeNs}`;
|
|
1377
|
+
};
|
|
1378
|
+
const ours = tcp ? "" : ident();
|
|
1379
|
+
touchIdle();
|
|
1380
|
+
log("info", "embedder.listening");
|
|
1381
|
+
async function stop(reason) {
|
|
1382
|
+
if (stopped) return;
|
|
1383
|
+
stopped = true;
|
|
1384
|
+
clearTimeout(idleTimer);
|
|
1385
|
+
clearTimeout(retryTimer);
|
|
1386
|
+
for (const s of all) s.destroy();
|
|
1387
|
+
await new Promise((r) => server.close(() => r()));
|
|
1388
|
+
try {
|
|
1389
|
+
if (!tcp && ident() === ours) rmSync2(p.sock, { force: true });
|
|
1390
|
+
} catch {
|
|
1391
|
+
}
|
|
1392
|
+
try {
|
|
1393
|
+
if (readFileSync2(p.token, "utf8") === published()) rmSync2(p.token, { force: true });
|
|
1394
|
+
} catch {
|
|
1395
|
+
}
|
|
1396
|
+
releaseLock(p.pid, lock.id);
|
|
1397
|
+
log("info", "embedder.stopped");
|
|
1398
|
+
o.onExit?.(reason);
|
|
1399
|
+
}
|
|
1400
|
+
return { close: () => stop("closed"), connections: () => conns.size, address: () => server.address() };
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
// src/memory/embedder/inner-source.ts
|
|
1404
|
+
function innerWorkerUrl(source) {
|
|
1405
|
+
return new URL(`data:text/javascript;base64,${Buffer.from(source, "utf8").toString("base64")}`);
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
// src/memory/embedder/no-dial.ts
|
|
1409
|
+
import { createRequire, syncBuiltinESMExports } from "node:module";
|
|
1410
|
+
var NO_DIAL_MESSAGE = "outbound connections are forbidden in the embedder";
|
|
1411
|
+
function forbidOutbound() {
|
|
1412
|
+
const net = createRequire(import.meta.url)("node:net");
|
|
1413
|
+
const mark = Symbol.for("securecoms.embedder.noDial");
|
|
1414
|
+
if (net[mark]) return;
|
|
1415
|
+
const refuse = () => {
|
|
1416
|
+
throw new Error(NO_DIAL_MESSAGE);
|
|
1417
|
+
};
|
|
1418
|
+
net.Socket.prototype.connect = refuse;
|
|
1419
|
+
net.connect = refuse;
|
|
1420
|
+
net.createConnection = refuse;
|
|
1421
|
+
net[mark] = true;
|
|
1422
|
+
syncBuiltinESMExports();
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
// src/memory/embedder/heartbeats.ts
|
|
1426
|
+
import { lstatSync as lstatSync2, readdirSync } from "node:fs";
|
|
1427
|
+
import { join as join3 } from "node:path";
|
|
1428
|
+
var HEARTBEAT_FUTURE_SKEW_MS = 6e4;
|
|
1429
|
+
var HEARTBEAT_STALE_MS = 3e4;
|
|
1430
|
+
function heartbeatAgeFresh(ageMs, staleMs = HEARTBEAT_STALE_MS) {
|
|
1431
|
+
return ageMs >= -HEARTBEAT_FUTURE_SKEW_MS && ageMs <= staleMs;
|
|
1432
|
+
}
|
|
1433
|
+
function anyHeartbeatFresh(dir2, now, staleMs = HEARTBEAT_STALE_MS) {
|
|
1434
|
+
let names;
|
|
1435
|
+
try {
|
|
1436
|
+
names = readdirSync(dir2);
|
|
1437
|
+
} catch {
|
|
1438
|
+
return false;
|
|
1439
|
+
}
|
|
1440
|
+
return names.some((n) => {
|
|
1441
|
+
try {
|
|
1442
|
+
const st = lstatSync2(join3(dir2, n));
|
|
1443
|
+
return st.isFile() && heartbeatAgeFresh(now - st.mtimeMs, staleMs);
|
|
1444
|
+
} catch {
|
|
1445
|
+
return false;
|
|
1446
|
+
}
|
|
1447
|
+
});
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
// src/memory/embedder/embedder-main.ts
|
|
1451
|
+
globalThis.fetch = () => {
|
|
1452
|
+
throw new Error("network forbidden in the embedder");
|
|
1453
|
+
};
|
|
1454
|
+
var key = process.argv[process.argv.indexOf("--key") + 1];
|
|
1455
|
+
if (key !== embedderKey(EMBEDDING_MANIFEST)) process.exit(2);
|
|
1456
|
+
var dist = fileURLToPath(new URL(".", import.meta.url));
|
|
1457
|
+
var win = process.platform === "win32";
|
|
1458
|
+
var dir = win ? windowsRunDir() : embedderRunDir();
|
|
1459
|
+
try {
|
|
1460
|
+
if (win) mkdirSync3(dir, { recursive: true });
|
|
1461
|
+
else ensureRunDir(dir);
|
|
1462
|
+
} catch {
|
|
1463
|
+
process.exit(4);
|
|
1464
|
+
}
|
|
1465
|
+
var status = embedderPaths(dir, key).status;
|
|
1466
|
+
var writeStatus = (fatal) => {
|
|
1467
|
+
try {
|
|
1468
|
+
rmSync3(status, { force: true });
|
|
1469
|
+
writeFileSync2(status, JSON.stringify({ fatal }), { mode: 384, flag: "wx" });
|
|
1470
|
+
} catch {
|
|
1471
|
+
}
|
|
1472
|
+
};
|
|
1473
|
+
var f = (n) => EMBEDDING_MANIFEST.files.find((x) => x.name === n);
|
|
1474
|
+
var bytes;
|
|
1475
|
+
try {
|
|
1476
|
+
const read = (p, sha, size) => {
|
|
1477
|
+
const b = readFileSync3(p);
|
|
1478
|
+
verifyBuffer(b, sha, size);
|
|
1479
|
+
return b;
|
|
1480
|
+
};
|
|
1481
|
+
bytes = {
|
|
1482
|
+
model: read(filePath(defaultModelsRoot(), EMBEDDING_MANIFEST.modelId, f("model.onnx")), f("model.onnx").sha256, f("model.onnx").size),
|
|
1483
|
+
vocab: read(filePath(defaultModelsRoot(), EMBEDDING_MANIFEST.modelId, f("vocab.txt")), f("vocab.txt").sha256, f("vocab.txt").size),
|
|
1484
|
+
wasm: read(join4(dist, "ort-wasm-simd-threaded.wasm"), EMBEDDING_MANIFEST.wasmSha256, EMBEDDING_MANIFEST.wasmSize)
|
|
1485
|
+
};
|
|
1486
|
+
} catch (e) {
|
|
1487
|
+
writeStatus(e instanceof HashMismatch ? "verify_failed" : "load_failed");
|
|
1488
|
+
process.exit(3);
|
|
1489
|
+
}
|
|
1490
|
+
var data = {
|
|
1491
|
+
bytes,
|
|
1492
|
+
model: { sha256: f("model.onnx").sha256, size: f("model.onnx").size },
|
|
1493
|
+
vocab: { sha256: f("vocab.txt").sha256, size: f("vocab.txt").size },
|
|
1494
|
+
wasm: { sha256: EMBEDDING_MANIFEST.wasmSha256, size: EMBEDDING_MANIFEST.wasmSize },
|
|
1495
|
+
tokenizer: TOKENIZER_OPTIONS,
|
|
1496
|
+
maxTokens: EMBEDDING_MANIFEST.maxTokens,
|
|
1497
|
+
dims: EMBEDDING_MANIFEST.dims,
|
|
1498
|
+
pooling: EMBEDDING_MANIFEST.pooling,
|
|
1499
|
+
normalize: EMBEDDING_MANIFEST.normalize,
|
|
1500
|
+
inputs: EMBEDDING_MANIFEST.onnxInputs,
|
|
1501
|
+
output: EMBEDDING_MANIFEST.onnxOutput
|
|
1502
|
+
};
|
|
1503
|
+
var inner;
|
|
1504
|
+
var seq = 0;
|
|
1505
|
+
var waiting = /* @__PURE__ */ new Map();
|
|
1506
|
+
var failAll = (why) => {
|
|
1507
|
+
for (const w of waiting.values()) w.rej(new Error(why));
|
|
1508
|
+
waiting.clear();
|
|
1509
|
+
};
|
|
1510
|
+
var innerUrl = innerWorkerUrl(readFileSync3(join4(dist, "embedder-worker.js"), "utf8"));
|
|
1511
|
+
var spawnInner = () => new Promise((res, rej) => {
|
|
1512
|
+
const w = new Worker(innerUrl, { workerData: data });
|
|
1513
|
+
inner = w;
|
|
1514
|
+
w.once("message", (m) => m?.ready ? res() : rej(Object.assign(new Error("inner fatal"), { fatal: m?.fatal })));
|
|
1515
|
+
w.on("message", (m) => {
|
|
1516
|
+
const p = waiting.get(m?.job_id);
|
|
1517
|
+
if (!p) return;
|
|
1518
|
+
waiting.delete(m.job_id);
|
|
1519
|
+
if (m.vec instanceof Float32Array && m.vec.length === EMBEDDING_MANIFEST.dims) p.res(m.vec);
|
|
1520
|
+
else p.rej(new Error("failed"));
|
|
1521
|
+
});
|
|
1522
|
+
w.on("error", () => {
|
|
1523
|
+
if (inner === w) failAll("inner error");
|
|
1524
|
+
rej(new Error("inner error"));
|
|
1525
|
+
});
|
|
1526
|
+
w.on("exit", () => {
|
|
1527
|
+
if (inner === w) failAll("inner exit");
|
|
1528
|
+
});
|
|
1529
|
+
});
|
|
1530
|
+
try {
|
|
1531
|
+
await spawnInner();
|
|
1532
|
+
} catch (e) {
|
|
1533
|
+
writeStatus(e.fatal === "verify_failed" ? "verify_failed" : "load_failed");
|
|
1534
|
+
process.exit(3);
|
|
1535
|
+
}
|
|
1536
|
+
rmSync3(status, { force: true });
|
|
1537
|
+
var heartbeats = join4(homedir3(), ".securecoms", "run", "voice-call");
|
|
1538
|
+
var backgroundHeld = () => anyHeartbeatFresh(heartbeats, Date.now());
|
|
1539
|
+
try {
|
|
1540
|
+
const srv = await startEmbedderServer({
|
|
1541
|
+
dir,
|
|
1542
|
+
key,
|
|
1543
|
+
backgroundHeld,
|
|
1544
|
+
// 11b review L2: an inner worker that cannot be rebuilt is a load failure the spawner can read; idle and signals exit 0.
|
|
1545
|
+
onExit: (reason) => {
|
|
1546
|
+
if (reason === "reset_failed") {
|
|
1547
|
+
writeStatus("load_failed");
|
|
1548
|
+
process.exit(3);
|
|
1549
|
+
}
|
|
1550
|
+
process.exit(0);
|
|
1551
|
+
},
|
|
1552
|
+
hello: { protocol: EMBEDDER_PROTOCOL, model_id: EMBEDDING_MANIFEST.modelId, model_sha256: f("model.onnx").sha256, wasm_sha256: EMBEDDING_MANIFEST.wasmSha256, dims: EMBEDDING_MANIFEST.dims },
|
|
1553
|
+
embed: (text) => new Promise((res, rej) => {
|
|
1554
|
+
const id = ++seq;
|
|
1555
|
+
waiting.set(id, { res, rej });
|
|
1556
|
+
inner.postMessage({ job_id: id, text });
|
|
1557
|
+
}),
|
|
1558
|
+
resetInner: async () => {
|
|
1559
|
+
const old = inner;
|
|
1560
|
+
failAll("reset");
|
|
1561
|
+
await old.terminate();
|
|
1562
|
+
await spawnInner();
|
|
1563
|
+
}
|
|
1564
|
+
});
|
|
1565
|
+
forbidOutbound();
|
|
1566
|
+
for (const sig of ["SIGTERM", "SIGINT", "SIGHUP"]) process.once(sig, () => void srv.close());
|
|
1567
|
+
} catch (e) {
|
|
1568
|
+
process.exit(e instanceof AlreadyRunning ? 0 : e instanceof SocketPathTooLong ? 5 : 4);
|
|
1569
|
+
}
|
|
1570
|
+
/*! Bundled license information:
|
|
1571
|
+
|
|
1572
|
+
@noble/ciphers/esm/utils.js:
|
|
1573
|
+
(*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
|
|
1574
|
+
*/
|