UNPKG

@himorishige/noren-dict-reloader

Version:

Dynamic dictionary and policy hot-reloading for Noren using ETag-based updates

74 lines (73 loc) 2.35 kB
type SourceMeta = { etag?: string; lastModified?: string; text?: string; json?: unknown; }; export type ReloaderOptions<TCompiled> = { policyUrl: string; dictManifestUrl: string; headers?: Record<string, string>; intervalMs?: number; maxIntervalMs?: number; jitter?: number; onSwap?: (compiled: TCompiled, changed: string[]) => void; onError?: (err: unknown) => void; compile: (policy: unknown, dicts: unknown[], options?: CompileOptions) => TCompiled; /** * Optional custom loader. If provided, it overrides the default HTTP(S) loader. * This enables loading from file://, in-memory stores, or custom transports. */ load?: LoaderFn; /** * Request timeout in milliseconds for HTTP requests */ requestTimeoutMs?: number; /** * URL validation function to prevent SSRF attacks */ validateUrl?: (url: URL) => boolean; /** * Maximum number of concurrent dictionary downloads */ maxConcurrent?: number; /** * Additional options for Registry compilation */ compileOptions?: CompileOptions; }; export type CompileOptions = { environment?: 'production' | 'test' | 'development'; enableConfidenceScoring?: boolean; allowDenyConfig?: { customAllowlist?: Record<string, Set<string>>; customDenylist?: Record<string, Set<string>>; }; }; export type LoaderResult = { status: 200 | 304; meta: SourceMeta; }; export type LoaderFn = (url: string, prev: SourceMeta | undefined, headers: Record<string, string>, force: boolean) => Promise<LoaderResult>; export declare class PolicyDictReloader<TCompiled> { private opts; private timer; private running; private backoff; private policy; private manifest; private dicts; private compiled; private loader; constructor(opts: ReloaderOptions<TCompiled>); getCompiled(): NonNullable<TCompiled>; start(): Promise<void>; stop(): void; forceReload(): Promise<void>; private nextDelay; private schedule; private tick; private loadDictionariesWithConcurrency; } declare function conditionalGet(url: string, prev: SourceMeta | undefined, headers: Record<string, string>, force: boolean, timeoutMs?: number): Promise<LoaderResult>; export { conditionalGet as defaultLoader };