refactor: dedupe canvas lowercase helpers

This commit is contained in:
Peter Steinberger
2026-04-07 22:31:21 +01:00
parent e0ad3e79e6
commit 5a020cf9a1
4 changed files with 14 additions and 8 deletions

View File

@@ -1,5 +1,8 @@
import { resolveProviderCacheTtlEligibility } from "../../plugins/provider-runtime.js";
import { normalizeLowercaseStringOrEmpty } from "../../shared/string-coerce.js";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalLowercaseString,
} from "../../shared/string-coerce.js";
import { isAnthropicFamilyCacheTtlEligible } from "./anthropic-family-cache-semantics.js";
import { isGooglePromptCacheEligible } from "./prompt-cache-retention.js";
@@ -46,7 +49,7 @@ export function isCacheTtlEligibleProvider(
}
function normalizeCacheTtlKey(value: string | undefined): string | undefined {
return value?.trim().toLowerCase();
return normalizeOptionalLowercaseString(value);
}
function matchesCacheTtlContext(

View File

@@ -3,6 +3,7 @@ import type { IncomingMessage, ServerResponse } from "node:http";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { detectMime } from "../media/mime.js";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
import { resolveFileWithinRoot } from "./file-resolver.js";
export const A2UI_PATH = "/__openclaw__/a2ui";
@@ -132,7 +133,7 @@ export function injectCanvasLiveReload(html: string): string {
</script>
`.trim();
const idx = html.toLowerCase().lastIndexOf("</body>");
const idx = normalizeLowercaseStringOrEmpty(html).lastIndexOf("</body>");
if (idx >= 0) {
return `${html.slice(0, idx)}\n${snippet}\n${html.slice(idx)}`;
}
@@ -180,7 +181,7 @@ export async function handleA2uiHttpRequest(
}
try {
const lower = result.realPath.toLowerCase();
const lower = normalizeLowercaseStringOrEmpty(result.realPath);
const mime =
lower.endsWith(".html") || lower.endsWith(".htm")
? "text/html"

View File

@@ -15,6 +15,7 @@ import { resolveStateDir } from "../config/paths.js";
import { isTruthyEnvValue } from "../infra/env.js";
import { detectMime } from "../media/mime.js";
import type { RuntimeEnv } from "../runtime.js";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
import { ensureDir, resolveUserPath } from "../utils.js";
import {
CANVAS_HOST_PATH,
@@ -393,7 +394,7 @@ export async function createCanvasHostHandler(
await handle.close().catch(() => {});
}
const lower = realPath.toLowerCase();
const lower = normalizeLowercaseStringOrEmpty(realPath);
const mime =
lower.endsWith(".html") || lower.endsWith(".htm")
? "text/html"
@@ -464,7 +465,7 @@ export async function startCanvasHost(opts: CanvasHostServerOpts): Promise<Canva
const bindHost = opts.listenHost?.trim() || "127.0.0.1";
const server: Server = http.createServer((req, res) => {
if (String(req.headers.upgrade ?? "").toLowerCase() === "websocket") {
if (normalizeLowercaseStringOrEmpty(req.headers.upgrade ?? "") === "websocket") {
return;
}
void (async () => {

View File

@@ -1,4 +1,5 @@
import type { ServerResponse } from "node:http";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
export function createMockServerResponse(): ServerResponse & { body?: string } {
const headers: Record<string, string> = {};
@@ -13,10 +14,10 @@ export function createMockServerResponse(): ServerResponse & { body?: string } {
headersSent: false,
statusCode: 200,
setHeader: (key: string, value: string) => {
headers[key.toLowerCase()] = value;
headers[normalizeLowercaseStringOrEmpty(key)] = value;
return res;
},
getHeader: (key: string) => headers[key.toLowerCase()],
getHeader: (key: string) => headers[normalizeLowercaseStringOrEmpty(key)],
end: (body?: string) => {
res.headersSent = true;
res.body = body;