Files
codewhale/web/middleware.ts
T
Hunter Bown f8a3c6619e fix(npm): map openharmony platform to linux binaries (#1072)
Node's `os.platform()` returns `openharmony` on HarmonyPC and on
OpenHarmony's Linux ABI-compatible userspace. The npm wrapper's
platform-asset matrix only covered `linux` / `darwin` / `win32`,
so `npm i -g deepseek-tui` aborted on those hosts with

    Unsupported platform: openharmony. Supported platforms: …

even though the existing Linux x64 / arm64 binaries run unchanged
on that environment (OpenHarmony is Linux-ABI-compatible at the
ELF level).

Added a `PLATFORM_ALIASES = { openharmony: "linux" }` indirection
that resolves the raw platform name through the alias map before
the `ASSET_MATRIX` lookup. Genuinely unsupported platforms still
report the raw `os.platform()` value in the error so OS-mismatch
bug reports stay diagnostic.

Four pure-JS regression tests pin the behaviour:

- openharmony x64 → linux x64 binaries
- openharmony arm64 → linux arm64 binaries
- known platforms unchanged by the alias map
- freebsd still reports `Unsupported platform: freebsd`

Harvested from PR #1499 by @CrepuscularIRIS

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 01:39:44 -05:00

65 lines
2.1 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { locales, defaultLocale } from "@/lib/i18n/config";
const COOKIE = "NEXT_LOCALE";
const SECURITY_HEADERS: Record<string, string> = {
"X-Frame-Options": "DENY",
"X-Content-Type-Options": "nosniff",
"Referrer-Policy": "strict-origin-when-cross-origin",
"Permissions-Policy": "camera=(), microphone=(), geolocation=(), interest-cohort=()",
"Strict-Transport-Security": "max-age=63072000; includeSubDomains; preload",
};
function applySecurityHeaders(res: NextResponse): NextResponse {
for (const [k, v] of Object.entries(SECURITY_HEADERS)) res.headers.set(k, v);
return res;
}
function detectLocale(req: NextRequest): string {
// 1. Cookie
const cookie = req.cookies.get(COOKIE)?.value;
if (cookie && locales.includes(cookie as typeof locales[number])) return cookie;
// 2. Accept-Language header
const accept = req.headers.get("accept-language") ?? "";
if (/^zh/i.test(accept.split(",")[0])) return "zh";
return defaultLocale;
}
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
// Skip API routes, static files, _next (but still apply security headers).
if (
pathname.startsWith("/api/") ||
pathname.startsWith("/_next/") ||
pathname.includes(".")
) {
return applySecurityHeaders(NextResponse.next());
}
// Check if locale is already in path
const seg = pathname.split("/")[1];
if (locales.includes(seg as typeof locales[number])) {
const res = NextResponse.next();
res.cookies.set(COOKIE, seg, { path: "/", maxAge: 60 * 60 * 24 * 365 });
return applySecurityHeaders(res);
}
// Redirect bare paths to detected locale
const locale = detectLocale(req);
const url = req.nextUrl.clone();
url.pathname = `/${locale}${pathname}`;
const res = NextResponse.redirect(url);
res.cookies.set(COOKIE, locale, { path: "/", maxAge: 60 * 60 * 24 * 365 });
return applySecurityHeaders(res);
}
export const config = {
// Match everything so security headers apply globally; the function
// bypasses redirect/locale logic for /_next, /api, and dotted paths.
matcher: ["/:path*"],
};