openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
80 lines (79 loc) • 2.37 kB
JavaScript
import fs from "node:fs";
import { createHash } from "node:crypto";
import { parentPort, workerData } from "node:worker_threads";
//#region src/infra/package-update-integrity-hasher.worker.ts
const cancel = workerData?.cancel;
if (!parentPort || !(cancel instanceof Int32Array)) throw new Error("Package file hasher requires its worker cancellation buffer");
const cancellation = cancel;
const port = parentPort;
const buffer = Buffer.allocUnsafe(65536);
const fields = [
"dev",
"ino",
"mode",
"uid",
"gid",
"nlink",
"size",
"mtimeNs",
"ctimeNs"
];
const unchanged = (e, t) => e.ino !== 0n && fields.every((n) => e[n] === t[n]);
const cancelled = /* @__PURE__ */ new Error("Package file hashing cancelled");
function admit() {
if (Atomics.load(cancellation, 0)) throw cancelled;
}
function hash$1(e) {
admit();
const t = fs.openSync(e.file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK);
try {
admit();
if (!unchanged(e, fs.fstatSync(t, { bigint: true }))) throw new Error("Package rollback file changed before reading");
const n = createHash("sha256");
const r = Number(e.size);
let i = 0;
while (i < r) {
admit();
const e = fs.readSync(t, buffer, 0, Math.min(buffer.length, r - i), i);
if (e === 0) throw new Error("Package rollback file changed while reading");
i += e;
n.update(buffer.subarray(0, e));
}
admit();
if (!unchanged(e, fs.fstatSync(t, { bigint: true }))) throw new Error("Package rollback file changed while reading");
return { digest: n.digest("hex") };
} finally {
try {
fs.closeSync(t);
} catch {}
}
}
function serializeError(e) {
const field = (t) => typeof e === "object" && e !== null ? Reflect.get(e, t) : void 0;
const t = field("code");
const n = field("errno");
const r = field("syscall");
const i = field("path");
const a = { message: e instanceof Error ? e.message : String(e) };
if (typeof t === "string") a.code = t;
if (typeof n === "number") a.errno = n;
if (typeof r === "string") a.syscall = r;
if (typeof i === "string") a.path = i;
return a;
}
port.on("message", (e) => {
if (e === "close") {
port.close();
return;
}
const t = e.map((e) => {
try {
return hash$1(e);
} catch (e) {
return e === cancelled ? { cancelled: true } : { error: serializeError(e) };
}
});
port.postMessage(t);
});
//#endregion
export {};