Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/realtime/src/guards.ts
T

99 lines
3.2 KiB
TypeScript

import { type NotificationStreamItem, type NotificationChannel, type RealtimeEvent } from "./events.js";
export type NotificationStreamScope = {
tenantId: string;
userId: string;
afterCursor?: string;
};
export type NotificationStreamScopeInput = {
tenantId?: unknown;
userId?: unknown;
afterCursor?: unknown;
};
const MAX_SCOPE_VALUE_LENGTH = 500;
const SECRET_VALUE_PATTERN = /bearer\s+[a-z0-9._~+\-/]+=*|api[-_]?key\s*[:=]\s*\S+|token\s*[:=]\s*\S+|password\s*[:=]\s*\S+|secret\s*[:=]\s*\S+|authorization\s*[:=]\s*\S+|provider|metadata|payload|exception|stack/gi;
export function normalizeNotificationStreamScope(input: NotificationStreamScopeInput): NotificationStreamScope {
const tenantId = normalizeRequiredScopeString(input.tenantId, "tenantId");
const userId = normalizeRequiredScopeString(input.userId, "userId");
const afterCursor = normalizeOptionalScopeString(input.afterCursor, "afterCursor");
return afterCursor === undefined ? { tenantId, userId } : { tenantId, userId, afterCursor };
}
export function assertNotificationItemInAppScope(item: NotificationStreamItem, scope: NotificationStreamScope): void {
assertInAppChannel(item.channel);
assertScopeValueMatches(item.tenantId, scope.tenantId, "item.tenantId");
assertScopeValueMatches(item.userId, scope.userId, "item.userId");
}
export function assertNotificationEventInAppScope(event: RealtimeEvent, scope: NotificationStreamScope): void {
assertScopeValueMatches(event.tenantId, scope.tenantId, "event.tenantId");
assertScopeValueMatches(event.userId, scope.userId, "event.userId");
switch (event.event) {
case "notification.snapshot":
for (const item of event.items) {
assertNotificationItemInAppScope(item, scope);
}
return;
case "notification.update":
assertNotificationItemInAppScope(event.item, scope);
return;
case "keepalive":
case "error":
return;
}
}
export function redactNotificationGuardDiagnostic(value: unknown): string {
const message = value instanceof Error ? value.message : String(value);
return message.replace(SECRET_VALUE_PATTERN, "[redacted]").replace(/: .+$/u, ": [redacted]").slice(0, 160);
}
function normalizeRequiredScopeString(value: unknown, label: string): string {
const normalized = normalizeOptionalScopeString(value, label);
if (normalized === undefined) {
throw new Error(`${label} is required`);
}
return normalized;
}
function normalizeOptionalScopeString(value: unknown, label: string): string | undefined {
if (value === undefined || value === null) {
return undefined;
}
if (typeof value !== "string") {
throw new Error(`${label} must be a string`);
}
const normalized = value.trim();
if (!normalized) {
throw new Error(`${label} must not be empty`);
}
if (normalized.length > MAX_SCOPE_VALUE_LENGTH) {
throw new Error(`${label} is too long`);
}
return normalized;
}
function assertInAppChannel(channel: NotificationChannel): void {
if (channel !== "IN_APP") {
throw new Error("item.channel must be IN_APP");
}
}
function assertScopeValueMatches(actual: string | null | undefined, expected: string, label: string): void {
if (actual !== expected) {
throw new Error(`${label} does not match notification stream scope`);
}
}