openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
132 lines (131 loc) • 4.3 kB
JavaScript
import { t as ARTIFACT_DOWNLOAD_PATH } from "./artifact-download-CXaDlLGk.mjs";
import { randomBytes } from "node:crypto";
//#region src/gateway/artifact-download-grants.ts
const DOWNLOAD_TTL_MS = 3e5;
const MAX_DOWNLOADS_PER_CONNECTION = 128;
const downloads = /* @__PURE__ */ new Map();
function removeDownload(e, t) {
const n = e.get(t);
if (n && e.delete(t)) {
n.retired = true;
if (n.readers === 0) n.release();
}
}
function canCreateArtifactDownload(e) {
return Boolean(e?.connId && e.connectionSignal?.aborted === false && !e.invalidated);
}
function pruneExpiredArtifactDownloads(e, t = downloads.keys()) {
for (const n of t) {
const t = downloads.get(n);
if (!t) continue;
for (const [r, i] of t) if (i.source.kind === "bytes" && !canCreateArtifactDownload(n) || i.expiresAt <= e) removeDownload(t, r);
if (t.size === 0 && !canCreateArtifactDownload(n)) downloads.delete(n);
}
}
/** Gateway teardown clears grants; the HTTP lifetime owner cancels active transfers. */
function clearArtifactDownloads() {
for (const e of downloads.values()) for (const t of e.keys()) removeDownload(e, t);
downloads.clear();
}
function createDownloadGrant(e) {
const { client: t } = e;
if (!canCreateArtifactDownload(t)) throw new Error("Artifact download connection is no longer available");
e.assertCurrent();
let n = downloads.get(t);
if (!n) {
n = /* @__PURE__ */ new Map();
downloads.set(t, n);
const e = n;
t.connectionSignal.addEventListener("abort", () => {
for (const [t, n] of e) if (n.source.kind === "bytes") removeDownload(e, t);
if (e.size === 0 && downloads.get(t) === e) downloads.delete(t);
}, { once: true });
}
const r = Date.now();
pruneExpiredArtifactDownloads(r, [t]);
for (const e of n.keys()) {
if (n.size < MAX_DOWNLOADS_PER_CONNECTION) break;
removeDownload(n, e);
}
const i = randomBytes(32).toString("base64url");
const a = r + DOWNLOAD_TTL_MS;
n.set(i, {
expiresAt: a,
source: e.source,
assertCurrent: e.assertCurrent,
release: e.release,
readers: 0,
retired: false
});
return {
locator: `${encodeURIComponent(t.connId)}/${i}`,
expiresAt: new Date(a).toISOString()
};
}
function createArtifactDownload(e) {
const t = createDownloadGrant({
...e,
source: {
kind: "bytes",
digest: e.prepared.digest,
image: e.prepared.artifact.type === "image",
read: e.read
}
});
return {
url: `${ARTIFACT_DOWNLOAD_PATH}${t.locator}`,
expiresAt: t.expiresAt
};
}
/** Keep the managed media adapter's thumbnail and playback routes on the same grant lifetime. */
function createManagedArtifactDownload(e) {
const t = createDownloadGrant({
...e,
source: {
kind: "managed",
path: e.path
}
});
return {
url: `${e.path}?${new URLSearchParams({ mediaTicket: t.locator })}`,
expiresAt: t.expiresAt
};
}
function resolveArtifactDownloadGrant(e, t) {
const [n, r, i] = t.split("/");
if (!r || i !== void 0) return;
for (const [t, i] of downloads) {
if (!t.connId || encodeURIComponent(t.connId) !== n) continue;
const a = i.get(r);
if (a && (a.source.kind === "managed" || e.has(t))) return {
client: t,
grant: a,
ticket: r
};
}
}
function assertArtifactDownloadGrantCurrent(e, t, n, r) {
const i = downloads.get(t);
if (r.source.kind === "bytes" && (!e.has(t) || !canCreateArtifactDownload(t)) || r.expiresAt <= Date.now() || i?.get(n) !== r) {
if (i?.get(n) === r) removeDownload(i, n);
throw new Error("Artifact download expired");
}
r.assertCurrent();
}
/** URL expiry closes admission, not an already admitted, backpressured transfer. */
function retainArtifactDownloadStream(e, t, n, r) {
assertArtifactDownloadGrantCurrent(e, t, n, r);
r.readers++;
return {
assertCurrent: () => {
if (r.source.kind === "bytes" && (!e.has(t) || !canCreateArtifactDownload(t))) throw new Error("Artifact download connection is no longer available");
r.assertCurrent();
},
release() {
r.readers--;
if (r.retired && r.readers === 0) r.release();
}
};
}
//#endregion
export { createManagedArtifactDownload as a, retainArtifactDownloadStream as c, createArtifactDownload as i, canCreateArtifactDownload as n, pruneExpiredArtifactDownloads as o, clearArtifactDownloads as r, resolveArtifactDownloadGrant as s, assertArtifactDownloadGrantCurrent as t };