Huginn/dist/single-file-messagepack-backing-store.js
2026-05-07 22:28:38 +01:00

130 lines
4.6 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SingleFileMessagePackBackingStore = void 0;
const promises_1 = require("node:fs/promises");
const node_path_1 = require("node:path");
const msgpack_1 = require("@msgpack/msgpack");
const zod_1 = require("zod");
const envelopeSchema = zod_1.z.object({
key: zod_1.z.string().min(1),
type: zod_1.z.string().min(1),
payload: zod_1.z.instanceof(Uint8Array),
storedAt: zod_1.z.string().min(1),
});
const envelopeArraySchema = zod_1.z.array(envelopeSchema);
const legacyEnvelopeArraySchema = zod_1.z.array(zod_1.z.object({
key: zod_1.z.string().min(1),
type: zod_1.z.string().min(1),
payload: zod_1.z.unknown(),
storedAt: zod_1.z.string().min(1),
}));
class SingleFileMessagePackBackingStore {
filePath;
#writeQueue = Promise.resolve();
constructor(filePath) {
this.filePath = (0, node_path_1.resolve)(filePath);
}
async pullAll() {
try {
const data = await (0, promises_1.readFile)(this.filePath);
const decoded = legacyEnvelopeArraySchema.parse((0, msgpack_1.decode)(data));
let repairedLegacyPayload = false;
const normalized = decoded.map((entry) => {
const payload = normalizePayload(entry.payload);
if (payload !== entry.payload) {
repairedLegacyPayload = true;
}
return {
...entry,
payload,
};
});
const parsed = envelopeArraySchema.parse(normalized);
if (repairedLegacyPayload) {
await this.#writeAll(parsed);
}
return parsed;
}
catch (error) {
const code = error.code;
if (code === "ENOENT") {
return [];
}
throw error;
}
}
async push(entry) {
await this.#enqueue(async () => {
const existing = await this.pullAll();
const filtered = existing.filter((candidate) => !(candidate.type === entry.type && candidate.key === entry.key));
filtered.push(entry);
await this.#writeAll(filtered);
});
}
async delete(entry) {
await this.#enqueue(async () => {
const existing = await this.pullAll();
const filtered = existing.filter((candidate) => !(candidate.type === entry.type && candidate.key === entry.key));
await this.#writeAll(filtered);
});
}
async pushAll(entries, options = {}) {
await this.#enqueue(async () => {
if (options.soft) {
try {
await (0, promises_1.readFile)(this.filePath);
return;
}
catch (error) {
const code = error.code;
if (code !== "ENOENT") {
throw error;
}
}
}
await this.#writeAll(entries);
});
}
async #enqueue(operation) {
let result;
const next = this.#writeQueue.then(async () => {
result = await operation();
});
this.#writeQueue = next.then(() => undefined, () => undefined);
await next;
return result;
}
async #writeAll(entries) {
await (0, promises_1.mkdir)((0, node_path_1.dirname)(this.filePath), { recursive: true });
const tempPath = `${this.filePath}.tmp-${process.pid}-${Date.now()}-${Math.random()
.toString(36)
.slice(2)}`;
try {
await (0, promises_1.writeFile)(tempPath, (0, msgpack_1.encode)(entries));
await (0, promises_1.rename)(tempPath, this.filePath);
}
catch (error) {
await (0, promises_1.rm)(tempPath, { force: true }).catch(() => undefined);
throw error;
}
}
}
exports.SingleFileMessagePackBackingStore = SingleFileMessagePackBackingStore;
function normalizePayload(payload) {
if (payload instanceof Uint8Array) {
return payload;
}
if (isObject(payload) &&
payload.type === "Buffer" &&
Array.isArray(payload.data) &&
payload.data.every((value) => Number.isInteger(value) && value >= 0 && value <= 255)) {
return Uint8Array.from(payload.data);
}
if (Array.isArray(payload) && payload.every((value) => Number.isInteger(value) && value >= 0 && value <= 255)) {
return Uint8Array.from(payload);
}
return (0, msgpack_1.encode)(payload);
}
function isObject(value) {
return typeof value === "object" && value !== null;
}