openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
155 lines (154 loc) • 4.21 kB
JavaScript
import { n as matchesHttpIfNoneMatch, t as matchesHttpIfModifiedSince } from "./http-conditional-BnFeJ7I4.mjs";
import { createHash } from "node:crypto";
import { pipeline } from "node:stream/promises";
//#region src/gateway/http-byte-range.ts
function createImmutableFileValidators(e) {
return {
etag: `"${createHash("sha256").update(`${e.size}:${e.mtimeMs}`).digest("base64url")}"`,
mtimeMs: e.mtimeMs
};
}
function parseByteRange(e, t) {
const n = e.trim();
if (n.includes(",")) return "invalid";
const r = /^bytes=(\d*)-(\d*)$/i.exec(n);
if (!r || !r[1] && !r[2]) return "invalid";
const [, i = "", a = ""] = r;
const o = BigInt(t);
if (!i) {
const e = BigInt(a);
if (e === 0n || o === 0n) return "unsatisfiable";
const n = e >= o ? 0n : o - e;
return {
start: Number(n),
end: t - 1
};
}
const s = BigInt(i);
if (s >= o) return "unsatisfiable";
const c = a ? BigInt(a) : o - 1n;
if (c < s) return "unsatisfiable";
const l = c >= o ? o - 1n : c;
return {
start: Number(s),
end: Number(l)
};
}
function resolveByteResponse(e) {
const t = e.validators?.etag;
const n = e.nowMs ?? Date.now();
const r = e.validators ? Math.floor(Math.min(e.validators.mtimeMs, n) / 1e3) * 1e3 : void 0;
const i = r === void 0 ? void 0 : new Date(r).toUTCString();
const a = e.request?.headers;
const o = a?.["if-none-match"];
if ((e.method === "GET" || e.method === "HEAD") && (matchesHttpIfNoneMatch(o, t) || o === void 0 && r !== void 0 && matchesHttpIfModifiedSince(e.request, r, n))) return {
kind: "not-modified",
statusCode: 304,
etag: t,
lastModified: i
};
const s = {
kind: "full",
statusCode: 200,
contentLength: e.file.size,
etag: t,
lastModified: i
};
const c = a?.range;
if (e.method !== "GET" || typeof c !== "string") return s;
const l = a?.["if-range"];
if (l !== void 0 && l !== t && l !== i) return s;
const u = parseByteRange(c, e.file.size);
if (u === "invalid") return s;
if (u === "unsatisfiable") return {
kind: "unsatisfiable",
statusCode: 416,
contentLength: 0,
etag: t,
lastModified: i,
size: e.file.size
};
return {
kind: "partial",
statusCode: 206,
contentLength: u.end - u.start + 1,
etag: t,
lastModified: i,
range: u,
size: e.file.size
};
}
function writeByteHeaders(e, t) {
e.statusCode = t.statusCode;
e.setHeader("Accept-Ranges", "bytes");
if (t.etag !== void 0) e.setHeader("ETag", t.etag);
if (t.lastModified !== void 0) e.setHeader("Last-Modified", t.lastModified);
if (t.kind === "not-modified") return;
e.setHeader("Content-Length", String(t.contentLength));
if (t.kind === "partial") e.setHeader("Content-Range", `bytes ${t.range.start}-${t.range.end}/${t.size}`);
else if (t.kind === "unsatisfiable") e.setHeader("Content-Range", `bytes */${t.size}`);
}
function createGatewayByteStream(e, t, n) {
let r;
const i = new AbortController();
let a = false;
const close = async () => {
if (a) return;
a = true;
i.abort();
if (r) {
r.destroy();
return;
}
await t.close().catch(() => {});
};
const release = () => {
close();
};
e.once("close", release);
return {
close,
signal: i.signal,
async pipe(i, o, s, c) {
if (o === "HEAD" || i.kind === "not-modified" || i.contentLength === 0) {
await close();
s?.();
e.end();
return;
}
if (a || e.destroyed || e.writableEnded) {
await close();
return;
}
s?.();
r = t.createReadStream({
start: i.kind === "partial" ? i.range.start : 0,
end: i.kind === "partial" ? i.range.end : i.contentLength - 1,
autoClose: true
});
r.once("end", release).once("close", release);
r.once("error", () => {
release();
if (!e.destroyed && !e.writableEnded) {
if (e.headersSent) e.destroy();
else n();
}
});
if (c) try {
await pipeline(r, async function* (e) {
for await (const t of e) {
c.signal?.throwIfAborted();
c.assertCurrent();
yield t;
}
}, e, { signal: c.signal });
} catch {
await close();
e.destroy();
}
else r.pipe(e);
}
};
}
//#endregion
export { writeByteHeaders as i, createImmutableFileValidators as n, resolveByteResponse as r, createGatewayByteStream as t };