- Bootstraps `src/core/` foundation (`db.ts`, `valkey.ts`, `spire_ffi.ts`, `main.ts`). - Adds `auth_guards.ts` for payload capping, CSRF check, and rate limiting. - Adds `content_negotiation.ts` and `sse_adapter.ts` for Datastar transport helpers. - Adds `error_fragments.tsx` for standardized Datastar error morphs. - Introduces `scripts/lint_arch.ts` to block imperative DOM usage. - Updates `deno.json` with src workspace configs and lint commands. Co-authored-by: mrteye <1945243+mrteye@users.noreply.github.com>
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import type { Context, Next } from "jsr:@hono/hono@4";
|
|
|
|
export type ClientType = "datastar" | "cli" | "shell" | "browser";
|
|
|
|
export function determineClientType(c: Context): ClientType {
|
|
const accept = c.req.header("accept") || "";
|
|
const userAgent = c.req.header("user-agent") || "";
|
|
|
|
if (c.req.header("datastar-request") === "true") {
|
|
return "datastar";
|
|
}
|
|
|
|
if (
|
|
accept.includes("application/json") || userAgent.includes("curl") ||
|
|
userAgent.includes("AuthYesCLI")
|
|
) {
|
|
// If it's explicitly asking for JSON, or it's a CLI tool like curl/our custom CLI
|
|
if (accept.includes("application/json")) {
|
|
return "cli";
|
|
}
|
|
// curl without explicit accept json might just want text/plain shell strings
|
|
return "shell";
|
|
}
|
|
|
|
if (accept.includes("text/html")) {
|
|
return "browser";
|
|
}
|
|
|
|
// Default to cli/json if unknown
|
|
return "cli";
|
|
}
|
|
|
|
export function contentNegotiation() {
|
|
return async (c: Context, next: Next) => {
|
|
const clientType = determineClientType(c);
|
|
c.set("clientType", clientType);
|
|
await next();
|
|
};
|
|
}
|