Files
bknd/app/src/core/utils/runtime.ts
dswbx 7e5c28d621 enhance Guard and permission handling with new test cases
- Updated the `Guard` class to improve context validation and permission checks, ensuring clearer error messages for unmet conditions.
- Refactored the `Policy` and `RolePermission` classes to support default effects and better handle conditions and filters.
- Enhanced tests in `authorize.spec.ts` and `permissions.spec.ts` to cover new permission scenarios, including guest and member role behaviors.
- Added new tests for context validation in permission middleware, ensuring robust error handling for invalid contexts.
- Improved utility functions for better integration with the updated permission structure.
2025-10-13 21:03:49 +02:00

80 lines
1.7 KiB
TypeScript

import { getRuntimeKey as honoGetRuntimeKey } from "hono/adapter";
/**
* Adds additional checks for nextjs
*/
export function getRuntimeKey(): string {
const global = globalThis as any;
// Detect Next.js server-side runtime
if (global?.process?.env?.NEXT_RUNTIME === "nodejs") {
return "nextjs";
}
// Detect Next.js edge runtime
if (global?.process?.env?.NEXT_RUNTIME === "edge") {
return "nextjs-edge";
}
// Detect Next.js client-side runtime
// @ts-ignore
if (typeof window !== "undefined" && window.__NEXT_DATA__) {
return "nextjs-client";
}
return honoGetRuntimeKey();
}
const features = {
// supports the redirect of not full qualified addresses
// not supported in nextjs
redirects_non_fq: true,
};
export function runtimeSupports(feature: keyof typeof features) {
const runtime = getRuntimeKey();
if (runtime.startsWith("nextjs")) {
features.redirects_non_fq = false;
}
return features[feature];
}
export function isNode() {
try {
return global?.process?.release?.name === "node";
} catch (e) {
return false;
}
}
export function isBun() {
try {
return typeof Bun !== "undefined";
} catch (e) {
return false;
}
}
export function invariant(condition: boolean | any, message: string) {
if (!condition) {
throw new Error(message);
}
}
export function threw(fn: () => any, instance?: new (...args: any[]) => Error) {
try {
fn();
return false;
} catch (e) {
if (instance) {
if (e instanceof instance) {
return true;
}
// if instance given but not what expected, throw
throw e;
}
return true;
}
}