UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

120 lines (119 loc) • 3.76 kB
import { i as splitHttpHeaderValue } from "./http-header-value-BTQlMYRm.mjs"; //#region src/gateway/http-media-range.ts const HTTP_TOKEN_PATTERN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/u; const HTTP_QVALUE_PATTERN = /^(?:0(?:\.\d{0,3})?|1(?:\.0{0,3})?)$/u; const HTTP_OPTIONAL_WHITESPACE_PATTERN = /^[\t ]+|[\t ]+$/gu; function trimHttpOptionalWhitespace(e) { return e.replace(HTTP_OPTIONAL_WHITESPACE_PATTERN, ""); } function parseParameterValue(e) { if (HTTP_TOKEN_PATTERN.test(e)) return e; if (e.length < 2 || e[0] !== "\"" || e.at(-1) !== "\"") return null; let t = ""; let n = false; for (let r = 1; r < e.length - 1; r += 1) { const i = e[r]; if (n) { t += i; n = false; continue; } if (i === "\\") { n = true; continue; } if (i === "\"") return null; t += i; } return n ? null : t; } function parseMediaType(e, t) { const n = splitHttpHeaderValue(e, ";", "quoted-string"); if (!n) return null; const [r, i, ...a] = trimHttpOptionalWhitespace(n.shift() ?? "").toLowerCase().split("/"); if (a.length > 0 || !r || !i || !HTTP_TOKEN_PATTERN.test(r) || !HTTP_TOKEN_PATTERN.test(i)) return null; let o = 1; let s = false; const c = /* @__PURE__ */ new Map(); for (const e of n) { const n = trimHttpOptionalWhitespace(e); if (!n) continue; const r = n.indexOf("="); const i = (r < 0 ? n : n.slice(0, r)).toLowerCase(); if (!HTTP_TOKEN_PATTERN.test(i)) return null; if (r <= 0) return null; const a = n.slice(r + 1); if (!a) return null; if (i === "q") { if (!t || s || !HTTP_QVALUE_PATTERN.test(a)) return null; s = true; o = Number(a); continue; } const l = parseParameterValue(a); if (l === null || c.has(i)) return null; c.set(i, i === "charset" ? l.toLowerCase() : l); } return { type: r, subtype: i, parameters: c, quality: o }; } function matchesRepresentation(e, t, n) { const r = e.type === t.type && e.subtype === t.subtype; const i = n && e.subtype === "*" && (e.type === "*" || e.type === t.type); if (!r && !i) return false; for (const [n, r] of e.parameters) if (t.parameters.get(n) !== r) return false; return true; } /** Checks the quality of the most specific range matching the offered representation. */ function acceptsMediaType(e, t, n = true) { if (!e) return false; const r = parseMediaType(t, false); if (!r) return false; const i = splitHttpHeaderValue(e, ",", "quoted-string"); if (!i) return false; let a = -1; let o = -1; let s = 0; for (const e of i) { const t = parseMediaType(e, true); if (!t || !matchesRepresentation(t, r, n)) continue; const i = t.type === "*" ? 0 : t.subtype === "*" ? 1 : 2; const c = t.parameters.size; const l = i - a || c - o; if (l > 0) { a = i; o = c; s = t.quality; } else if (l === 0) s = Math.max(s, t.quality); } return s > 0; } /** Wildcards cannot opt callers into long-lived streaming responses. */ function hasExplicitAcceptableMediaRange(e, t) { return acceptsMediaType(e, t, false); } //#endregion //#region src/gateway/control-ui-http-utils.ts function isReadHttpMethod(e) { return e === "GET" || e === "HEAD"; } function acceptsControlUiHtmlResponse(e) { const t = e?.trim(); if (!t) return true; return acceptsMediaType(t, "text/html; charset=utf-8") || hasExplicitAcceptableMediaRange(t, "application/xhtml+xml"); } function respondPlainText(e, t, n) { e.statusCode = t; e.setHeader("Content-Type", "text/plain; charset=utf-8"); if (t !== 204) e.setHeader("Content-Length", String(Buffer.byteLength(n))); e.end(n); } function respondNotFound(e) { respondPlainText(e, 404, "Not Found"); } //#endregion export { hasExplicitAcceptableMediaRange as a, respondPlainText as i, isReadHttpMethod as n, respondNotFound as r, acceptsControlUiHtmlResponse as t };