@richardmcquiston01/error-log-adapter 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -13
- package/dist/index.cjs +771 -156
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +383 -13
- package/dist/index.d.ts +383 -13
- package/dist/index.mjs +766 -156
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -1
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/serialize.ts","../src/adapters/console-error-adapter.ts","../src/adapters/webhook-error-adapter.ts","../src/adapters/mongodb-error-adapter.ts","../src/adapters/composite-error-adapter.ts","../src/adapters/indexeddb-error-adapter.ts","../src/adapters/sqlite-error-adapter.ts","../src/adapters/web-storage-error-adapter.ts","../src/file-system.ts","../src/adapters/flat-file-error-adapter.ts","../src/adapters/json-file-error-adapter.ts","../src/adapters/sql-error-adapter.ts","../src/adapters/redis-error-adapter.ts","../src/adapters/s3-error-adapter.ts"],"sourcesContent":["import type { ErrorLogEntry } from './types.js';\n\n/** A JSON-safe copy of an `ErrorLogEntry`. */\nexport interface SerializedErrorLogEntry {\n code: string;\n message: string;\n cause?: unknown;\n source?: string;\n timestamp: string;\n}\n\n/**\n * Converts `Error` instances into plain objects, since `JSON.stringify`\n * renders them as `{}`. Other values are returned unchanged.\n */\nexport function serializeCause(cause: unknown): unknown {\n if (cause instanceof Error) {\n return { name: cause.name, message: cause.message, stack: cause.stack };\n }\n return cause;\n}\n\n/** Returns a JSON-safe copy of the entry, adding a timestamp if missing. */\nexport function serializeEntry(entry: ErrorLogEntry): SerializedErrorLogEntry {\n const serialized: SerializedErrorLogEntry = {\n code: entry.code,\n message: entry.message,\n timestamp: entry.timestamp ?? new Date().toISOString(),\n };\n if (entry.cause !== undefined) {\n serialized.cause = serializeCause(entry.cause);\n }\n if (entry.source !== undefined) {\n serialized.source = entry.source;\n }\n return serialized;\n}\n\n/** Turns an unknown thrown value into a descriptive message. */\nexport function describeError(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n","import type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\nexport interface ConsoleErrorOptions {\n /** Text printed before each entry. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\nexport class ConsoleErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly prefix: string;\n\n constructor(options: ConsoleErrorOptions = {}) {\n this.prefix = options.prefix ?? '[error-log-adapter]';\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n console.error(this.prefix, entry);\n return { ok: true };\n } catch (error: unknown) {\n return {\n ok: false,\n error: `ConsoleErrorAdapter failed: ${String(error)}`,\n };\n }\n }\n}\n","import { describeError, serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\nexport interface WebhookErrorOptions {\n /** Extra headers merged into the request. */\n headers?: Record<string, string>;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\nexport class WebhookErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly prefix: string;\n\n constructor(\n private readonly url: string,\n private readonly options: WebhookErrorOptions = {}\n ) {\n this.prefix = options.prefix ?? '[error-log-adapter]';\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const response: Response = await fetch(this.url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n ...this.options.headers,\n },\n body: JSON.stringify(serializeEntry(entry)),\n });\n if (!response.ok) {\n return this.fail(\n `webhook ${this.url} responded with HTTP ${response.status}`\n );\n }\n return { ok: true };\n } catch (error: unknown) {\n console.error(`${this.prefix} WebhookErrorAdapter:`, error);\n return this.fail(\n `could not reach webhook ${this.url}: ${describeError(error)}`\n );\n }\n }\n\n private fail(reason: string): ErrorLogResult {\n return { ok: false, error: `WebhookErrorAdapter failed: ${reason}` };\n }\n}\n","import { describeError, serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** The subset of a MongoDB `Collection` this adapter uses. */\nexport interface CollectionLike {\n insertOne(doc: object): Promise<unknown>;\n}\n\nexport interface MongoDbErrorOptions {\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\nexport class MongoDbErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly prefix: string;\n\n constructor(\n private readonly collection: CollectionLike,\n options: MongoDbErrorOptions = {}\n ) {\n this.prefix = options.prefix ?? '[error-log-adapter]';\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n await this.collection.insertOne(serializeEntry(entry));\n return { ok: true };\n } catch (error: unknown) {\n console.error(`${this.prefix} MongoDbErrorAdapter:`, error);\n return {\n ok: false,\n error: `MongoDbErrorAdapter failed to insert entry: ${describeError(error)}`,\n };\n }\n }\n}\n","import type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Sends every entry to several adapters. Succeeds only if all of them do. */\nexport class CompositeErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n constructor(\n private readonly adapters: ReadonlyArray<ErrorLogAdapter<TCode>>\n ) {}\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n const results: ErrorLogResult[] = await Promise.all(\n this.adapters.map(\n async (adapter: ErrorLogAdapter<TCode>): Promise<ErrorLogResult> => {\n try {\n return await adapter.log(entry);\n } catch (error: unknown) {\n // Adapters should not throw, but a faulty custom one must not break the others.\n return {\n ok: false,\n error: `adapter threw instead of returning a result: ${String(error)}`,\n };\n }\n }\n )\n );\n const failures: string[] = results.flatMap(\n (result: ErrorLogResult): string[] =>\n result.ok ? [] : [result.error ?? 'unknown failure']\n );\n if (failures.length === 0) {\n return { ok: true };\n }\n return {\n ok: false,\n error: `${failures.length} of ${results.length} adapters failed: ${failures.join('; ')}`,\n };\n }\n}\n","import { describeError, serializeEntry } from '../serialize.js';\nimport type { SerializedErrorLogEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default object store name. */\nexport const DEFAULT_STORE_NAME = 'error_log';\n\n/** Event handler slot. Typed loosely so real DOM `IDBTransaction` objects fit. */\ntype IdbHandler = ((event: never) => unknown) | null;\n\n/** The subset of an `IDBObjectStore` this adapter uses. */\nexport interface IdbObjectStoreLike {\n add(value: unknown): unknown;\n}\n\n/** The subset of an `IDBTransaction` this adapter uses. */\nexport interface IdbTransactionLike {\n objectStore(name: string): IdbObjectStoreLike;\n oncomplete: IdbHandler;\n onerror: IdbHandler;\n onabort: IdbHandler;\n readonly error?: { message?: string } | null;\n}\n\n/** The subset of an `IDBDatabase` this adapter uses. */\nexport interface IdbDatabaseLike {\n transaction(storeNames: string, mode: 'readwrite'): IdbTransactionLike;\n}\n\n/** The subset of an `IDBDatabase` available inside `onupgradeneeded`. */\nexport interface IdbUpgradeDatabaseLike {\n readonly objectStoreNames: { contains(name: string): boolean };\n createObjectStore(\n name: string,\n options: { keyPath: string }\n ): { createIndex(name: string, keyPath: string): unknown };\n}\n\n/** A stored record: the serialized entry plus its generated UUID key. */\nexport interface IndexedDbErrorRecord extends SerializedErrorLogEntry {\n id: string;\n}\n\nexport interface IndexedDbErrorOptions {\n /** Object store to write to. Defaults to `error_log`. */\n storeName?: string;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\n/**\n * Creates the error log object store (UUID `id` key, `timestamp` index).\n * Call it from your `onupgradeneeded` handler. Does nothing if it already exists.\n */\nexport function createErrorLogStore(\n database: IdbUpgradeDatabaseLike,\n storeName: string = DEFAULT_STORE_NAME\n): void {\n if (database.objectStoreNames.contains(storeName)) {\n return;\n }\n database\n .createObjectStore(storeName, { keyPath: 'id' })\n .createIndex('timestamp', 'timestamp');\n}\n\nexport class IndexedDbErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly storeName: string;\n private readonly prefix: string;\n\n constructor(\n private readonly database: IdbDatabaseLike,\n options: IndexedDbErrorOptions = {}\n ) {\n this.storeName = options.storeName ?? DEFAULT_STORE_NAME;\n this.prefix = options.prefix ?? '[error-log-adapter]';\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record: IndexedDbErrorRecord = {\n id: crypto.randomUUID(),\n ...serializeEntry(entry),\n };\n await this.write(record);\n return { ok: true };\n } catch (error: unknown) {\n console.error(`${this.prefix} IndexedDbErrorAdapter:`, error);\n return {\n ok: false,\n error: `IndexedDbErrorAdapter failed to write to object store \"${this.storeName}\": ${describeError(error)}`,\n };\n }\n }\n\n /** Resolves once the transaction commits; rejects if it errors or aborts. */\n private write(record: IndexedDbErrorRecord): Promise<void> {\n return new Promise<void>(\n (resolve: () => void, reject: (reason: unknown) => void): void => {\n const transaction: IdbTransactionLike = this.database.transaction(\n this.storeName,\n 'readwrite'\n );\n const fail = (action: string) => (): void =>\n reject(\n new Error(\n `transaction ${action}: ${transaction.error?.message ?? 'no details provided'}`\n )\n );\n transaction.oncomplete = (): void => resolve();\n transaction.onerror = fail('failed');\n transaction.onabort = fail('aborted');\n transaction.objectStore(this.storeName).add(record);\n }\n );\n }\n}\n","import { describeError, serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default table name. */\nexport const DEFAULT_TABLE_NAME = 'error_log';\n\nconst IDENTIFIER_PATTERN: RegExp = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\n/**\n * The only thing this adapter needs from a SQLite driver: run one statement\n * with positional (`?`) parameters. `bun:sqlite` already matches this shape.\n * Other drivers need a one-line wrapper, for example:\n *\n * ```ts\n * // better-sqlite3 or node:sqlite\n * const client: SqliteClientLike = {\n * run: (sql, params) => db.prepare(sql).run(...params),\n * };\n * ```\n */\nexport interface SqliteClientLike {\n run(sql: string, params: unknown[]): unknown;\n}\n\nexport interface SqliteErrorOptions {\n /** Table to write to. Defaults to `error_log`. Letters, digits and `_` only. */\n tableName?: string;\n /** Create the table (and index) on first use. Defaults to `true`. */\n createTable?: boolean;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\n/** Throws a descriptive error unless `tableName` is a plain SQL identifier. */\nfunction assertValidTableName(tableName: string): void {\n if (!IDENTIFIER_PATTERN.test(tableName)) {\n throw new RangeError(\n `SqliteErrorAdapter: invalid table name \"${tableName}\". Use only letters, digits and underscores, and do not start with a digit.`\n );\n }\n}\n\n/**\n * Returns the statements that create the error log table and its index.\n * Use this to run your own migration when `createTable` is disabled.\n */\nexport function buildCreateErrorLogTableSql(\n tableName: string = DEFAULT_TABLE_NAME\n): string[] {\n assertValidTableName(tableName);\n return [\n `CREATE TABLE IF NOT EXISTS ${tableName} (` +\n 'id TEXT PRIMARY KEY NOT NULL, ' +\n 'code TEXT NOT NULL, ' +\n 'message TEXT NOT NULL, ' +\n 'cause TEXT, ' +\n 'source TEXT, ' +\n 'occurred_at TEXT NOT NULL, ' +\n \"created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))\" +\n ')',\n `CREATE INDEX IF NOT EXISTS ${tableName}_occurred_at_idx ON ${tableName} (occurred_at)`,\n ];\n}\n\nexport class SqliteErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly tableName: string;\n private readonly createTable: boolean;\n private readonly prefix: string;\n private tableReady: Promise<void> | null = null;\n\n /** @throws {RangeError} if `options.tableName` is not a plain identifier. */\n constructor(\n private readonly client: SqliteClientLike,\n options: SqliteErrorOptions = {}\n ) {\n this.tableName = options.tableName ?? DEFAULT_TABLE_NAME;\n assertValidTableName(this.tableName);\n this.createTable = options.createTable ?? true;\n this.prefix = options.prefix ?? '[error-log-adapter]';\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n await this.ensureTable();\n const serialized = serializeEntry(entry);\n await this.client.run(\n `INSERT INTO ${this.tableName} (id, code, message, cause, source, occurred_at) VALUES (?, ?, ?, ?, ?, ?)`,\n [\n crypto.randomUUID(),\n serialized.code,\n serialized.message,\n serialized.cause === undefined\n ? null\n : JSON.stringify(serialized.cause),\n serialized.source ?? null,\n serialized.timestamp,\n ]\n );\n return { ok: true };\n } catch (error: unknown) {\n console.error(`${this.prefix} SqliteErrorAdapter:`, error);\n return {\n ok: false,\n error: `SqliteErrorAdapter failed to write to table \"${this.tableName}\": ${describeError(error)}`,\n };\n }\n }\n\n /** Creates the table once. A failed attempt is retried on the next log call. */\n private ensureTable(): Promise<void> {\n if (!this.createTable) {\n return Promise.resolve();\n }\n if (this.tableReady === null) {\n this.tableReady = this.runCreateStatements().catch(\n (error: unknown): never => {\n this.tableReady = null;\n throw error;\n }\n );\n }\n return this.tableReady;\n }\n\n private async runCreateStatements(): Promise<void> {\n for (const sql of buildCreateErrorLogTableSql(this.tableName)) {\n await this.client.run(sql, []);\n }\n }\n}\n","import { describeError, serializeEntry } from '../serialize.js';\nimport type { SerializedErrorLogEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default storage key. */\nexport const DEFAULT_STORAGE_KEY = 'error_log';\n\n/** Default number of entries kept before the oldest are dropped. */\nexport const DEFAULT_MAX_ENTRIES = 100;\n\n/**\n * The subset of the Web Storage API this adapter uses. Both `sessionStorage`\n * and `localStorage` fit.\n */\nexport interface StorageLike {\n getItem(key: string): string | null;\n setItem(key: string, value: string): void;\n}\n\n/** A stored record: the serialized entry plus its generated UUID. */\nexport interface WebStorageErrorRecord extends SerializedErrorLogEntry {\n id: string;\n}\n\nexport interface WebStorageErrorOptions {\n /** Key the entries are stored under, as one JSON array. Defaults to `error_log`. */\n key?: string;\n /** Most entries to keep. The oldest are dropped first. Defaults to `100`. */\n maxEntries?: number;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\n/** Browsers report a full store with different names and codes. */\nfunction isQuotaError(error: unknown): boolean {\n if (typeof error !== 'object' || error === null) {\n return false;\n }\n const { name, code } = error as { name?: unknown; code?: unknown };\n return (\n (typeof name === 'string' && name.toLowerCase().includes('quota')) ||\n code === 22 ||\n code === 1014\n );\n}\n\n/**\n * Keeps the newest entries in a Web Storage object (`sessionStorage` or\n * `localStorage`) as a JSON array under one key, capped at `maxEntries`.\n * When the store is full, the oldest entries are dropped until the new one fits.\n */\nexport class WebStorageErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly key: string;\n private readonly maxEntries: number;\n private readonly prefix: string;\n\n /** @throws {RangeError} if `options.maxEntries` is not a positive integer. */\n constructor(\n private readonly storage: StorageLike,\n options: WebStorageErrorOptions = {}\n ) {\n this.key = options.key ?? DEFAULT_STORAGE_KEY;\n this.maxEntries = options.maxEntries ?? DEFAULT_MAX_ENTRIES;\n this.prefix = options.prefix ?? '[error-log-adapter]';\n if (!Number.isInteger(this.maxEntries) || this.maxEntries < 1) {\n throw new RangeError(\n `WebStorageErrorAdapter: maxEntries must be a positive integer, received ${String(options.maxEntries)}.`\n );\n }\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record: WebStorageErrorRecord = {\n id: crypto.randomUUID(),\n ...serializeEntry(entry),\n };\n this.append(record);\n return { ok: true };\n } catch (error: unknown) {\n console.error(`${this.prefix} WebStorageErrorAdapter:`, error);\n return {\n ok: false,\n error: `WebStorageErrorAdapter failed to write to storage key \"${this.key}\": ${describeError(error)}`,\n };\n }\n }\n\n /** Reads, appends, trims, and writes back. Runs without awaiting, so calls never interleave. */\n private append(record: WebStorageErrorRecord): void {\n const records: WebStorageErrorRecord[] = [\n ...this.readRecords(),\n record,\n ].slice(-this.maxEntries);\n for (;;) {\n try {\n this.storage.setItem(this.key, JSON.stringify(records));\n return;\n } catch (error: unknown) {\n if (!isQuotaError(error) || records.length <= 1) {\n throw error;\n }\n records.shift();\n }\n }\n }\n\n /** Returns the stored records, or none if the value is missing or unreadable. */\n private readRecords(): WebStorageErrorRecord[] {\n const stored: string | null = this.storage.getItem(this.key);\n if (stored === null) {\n return [];\n }\n try {\n const parsed: unknown = JSON.parse(stored);\n if (Array.isArray(parsed)) {\n return parsed as WebStorageErrorRecord[];\n }\n console.error(\n `${this.prefix} WebStorageErrorAdapter: storage key \"${this.key}\" did not hold an array, so it was replaced.`\n );\n } catch (error: unknown) {\n console.error(\n `${this.prefix} WebStorageErrorAdapter: storage key \"${this.key}\" held invalid JSON (${describeError(error)}), so it was replaced.`\n );\n }\n return [];\n }\n}\n","/**\n * The subset of `node:fs/promises` the file adapters use. Pass `fs.promises`\n * (or `node:fs/promises`) yourself, or leave `fs` unset in Node and Bun.\n */\nexport interface FileSystemLike {\n appendFile(path: string, data: string): Promise<unknown>;\n mkdir(path: string, options: { recursive: true }): Promise<unknown>;\n stat(path: string): Promise<{ size: number }>;\n rename(from: string, to: string): Promise<unknown>;\n rm(path: string, options: { force: true }): Promise<unknown>;\n}\n\n/** `FileSystemLike` plus the reading and replacing that whole-file writes need. */\nexport interface ReadWriteFileSystemLike extends FileSystemLike {\n readFile(path: string, encoding: 'utf8'): Promise<string>;\n writeFile(path: string, data: string): Promise<unknown>;\n}\n\n/** Loads `node:fs/promises`. Only call this from code that runs in Node or Bun. */\nexport async function loadNodeFileSystem(): Promise<ReadWriteFileSystemLike> {\n // A variable specifier keeps browser bundlers from resolving it.\n const moduleName: string = 'node:fs/promises';\n return (await import(moduleName)) as ReadWriteFileSystemLike;\n}\n\n/** Returns the folder part of a path, or `null` if the path has none. */\nexport function parentDirectory(filePath: string): string | null {\n const lastSeparator: number = Math.max(\n filePath.lastIndexOf('/'),\n filePath.lastIndexOf('\\\\')\n );\n if (lastSeparator < 0) {\n return null;\n }\n return lastSeparator === 0 ? '/' : filePath.slice(0, lastSeparator);\n}\n\n/** True for the error Node throws when a file or folder does not exist. */\nexport function isMissingFileError(error: unknown): boolean {\n return (\n typeof error === 'object' &&\n error !== null &&\n (error as { code?: unknown }).code === 'ENOENT'\n );\n}\n","import {\n isMissingFileError,\n loadNodeFileSystem,\n parentDirectory,\n} from '../file-system.js';\nimport type { FileSystemLike } from '../file-system.js';\nimport { describeError, serializeEntry } from '../serialize.js';\nimport type { SerializedErrorLogEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default number of rotated files kept when `maxBytes` is set. */\nexport const DEFAULT_MAX_FILES = 3;\n\nexport interface FlatFileErrorOptions {\n /** File system to write to. Defaults to `node:fs/promises`, loaded on first use. */\n fs?: FileSystemLike;\n /** Turns an entry into one line of text. Defaults to `formatErrorLogLine`. */\n formatLine?: (entry: SerializedErrorLogEntry) => string;\n /** Rotate the file before it would grow past this many bytes. Off by default. */\n maxBytes?: number;\n /** Rotated files to keep (`app.log.1` is newest). Defaults to `3`. Needs `maxBytes`. */\n maxFiles?: number;\n /** Create missing parent directories on first use. Defaults to `true`. */\n createDirectory?: boolean;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\n/** Keeps one entry on one line by escaping line breaks. */\nfunction escapeLineBreaks(text: string): string {\n return text.replace(/\\r?\\n/g, '\\\\n');\n}\n\nfunction describeCause(cause: unknown): string {\n if (typeof cause === 'string') {\n return cause;\n }\n if (typeof cause === 'object' && cause !== null) {\n const { name, message } = cause as { name?: unknown; message?: unknown };\n if (typeof name === 'string' && typeof message === 'string') {\n return `${name}: ${message}`;\n }\n }\n try {\n return JSON.stringify(cause) ?? String(cause);\n } catch {\n return String(cause);\n }\n}\n\n/**\n * The default line format:\n * `2026-01-02T03:04:05.000Z [CODE] source: message (cause: Error: details)`.\n * `source` and `cause` are left out when the entry has none.\n */\nexport function formatErrorLogLine(entry: SerializedErrorLogEntry): string {\n const source: string = entry.source === undefined ? '' : `${entry.source}: `;\n const cause: string =\n entry.cause === undefined ? '' : ` (cause: ${describeCause(entry.cause)})`;\n return escapeLineBreaks(\n `${entry.timestamp} [${entry.code}] ${source}${entry.message}${cause}`\n );\n}\n\nfunction assertPositiveInteger(value: number, name: string): void {\n if (!Number.isInteger(value) || value < 1) {\n throw new RangeError(\n `FlatFileErrorAdapter: ${name} must be a positive integer, received ${String(value)}.`\n );\n }\n}\n\n/**\n * Appends one text line per entry to a file, optionally rotating it by size.\n * Writes are queued, so concurrent `log` calls never interleave or race a rotation.\n */\nexport class FlatFileErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly formatLine: (entry: SerializedErrorLogEntry) => string;\n private readonly maxBytes: number | undefined;\n private readonly maxFiles: number;\n private readonly createDirectory: boolean;\n private readonly prefix: string;\n private fs: FileSystemLike | undefined;\n private directoryReady: boolean = false;\n private queue: Promise<unknown> = Promise.resolve();\n\n /** @throws {RangeError} if `maxBytes` or `maxFiles` is not a positive integer. */\n constructor(\n private readonly filePath: string,\n options: FlatFileErrorOptions = {}\n ) {\n this.fs = options.fs;\n this.formatLine = options.formatLine ?? formatErrorLogLine;\n this.maxBytes = options.maxBytes;\n this.maxFiles = options.maxFiles ?? DEFAULT_MAX_FILES;\n this.createDirectory = options.createDirectory ?? true;\n this.prefix = options.prefix ?? '[error-log-adapter]';\n if (this.maxBytes !== undefined) {\n assertPositiveInteger(this.maxBytes, 'maxBytes');\n }\n assertPositiveInteger(this.maxFiles, 'maxFiles');\n }\n\n log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n const result: Promise<ErrorLogResult> = this.queue.then(() =>\n this.write(entry)\n );\n // write() never rejects, but keep the queue alive no matter what.\n this.queue = result.catch(() => undefined);\n return result;\n }\n\n private async write(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const fs: FileSystemLike = await this.getFileSystem();\n const formatted: string = this.formatLine(serializeEntry(entry));\n const line: string = formatted.endsWith('\\n')\n ? formatted\n : `${formatted}\\n`;\n await this.prepareDirectory(fs);\n await this.rotateIfNeeded(fs, new TextEncoder().encode(line).length);\n await fs.appendFile(this.filePath, line);\n return { ok: true };\n } catch (error: unknown) {\n console.error(`${this.prefix} FlatFileErrorAdapter:`, error);\n return {\n ok: false,\n error: `FlatFileErrorAdapter failed to append to \"${this.filePath}\": ${describeError(error)}`,\n };\n }\n }\n\n /** Uses the injected file system, or loads `node:fs/promises` on first use. */\n private async getFileSystem(): Promise<FileSystemLike> {\n if (this.fs === undefined) {\n this.fs = await loadNodeFileSystem();\n }\n return this.fs;\n }\n\n private async prepareDirectory(fs: FileSystemLike): Promise<void> {\n if (!this.createDirectory || this.directoryReady) {\n return;\n }\n const directory: string | null = parentDirectory(this.filePath);\n if (directory !== null) {\n await fs.mkdir(directory, { recursive: true });\n }\n this.directoryReady = true;\n }\n\n /**\n * Moves `app.log` to `app.log.1` (shifting older files up, dropping the\n * oldest) when the next line would push it past `maxBytes`. A failed rotation\n * is reported, but the entry is still written so it is not lost.\n */\n private async rotateIfNeeded(\n fs: FileSystemLike,\n lineBytes: number\n ): Promise<void> {\n if (this.maxBytes === undefined) {\n return;\n }\n try {\n let currentSize: number;\n try {\n currentSize = (await fs.stat(this.filePath)).size;\n } catch (error: unknown) {\n if (isMissingFileError(error)) {\n return;\n }\n throw error;\n }\n if (currentSize === 0 || currentSize + lineBytes <= this.maxBytes) {\n return;\n }\n await fs.rm(`${this.filePath}.${this.maxFiles}`, { force: true });\n for (let index = this.maxFiles - 1; index >= 1; index -= 1) {\n await this.renameIfPresent(\n fs,\n `${this.filePath}.${index}`,\n `${this.filePath}.${index + 1}`\n );\n }\n await fs.rename(this.filePath, `${this.filePath}.1`);\n } catch (error: unknown) {\n console.error(\n `${this.prefix} FlatFileErrorAdapter: could not rotate \"${this.filePath}\", so it will keep growing: ${describeError(error)}`\n );\n }\n }\n\n private async renameIfPresent(\n fs: FileSystemLike,\n from: string,\n to: string\n ): Promise<void> {\n try {\n await fs.rename(from, to);\n } catch (error: unknown) {\n if (!isMissingFileError(error)) {\n throw error;\n }\n }\n }\n}\n","import {\n isMissingFileError,\n loadNodeFileSystem,\n parentDirectory,\n} from '../file-system.js';\nimport type { ReadWriteFileSystemLike } from '../file-system.js';\nimport { describeError, serializeEntry } from '../serialize.js';\nimport type { SerializedErrorLogEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default number of spaces used to indent array mode output. */\nexport const DEFAULT_INDENT = 2;\n\n/** A stored record: the serialized entry plus its generated UUID. */\nexport interface JsonFileErrorRecord extends SerializedErrorLogEntry {\n id: string;\n}\n\nexport interface JsonFileErrorOptions {\n /**\n * `ndjson` (default) appends one compact JSON object per line, which is cheap\n * and safe to tail. `array` keeps the whole file as one JSON array that is\n * rewritten on every entry.\n */\n mode?: 'ndjson' | 'array';\n /** Array mode only: most entries to keep. The oldest are dropped first. */\n maxEntries?: number;\n /** Array mode only: spaces of indentation. Defaults to `2`; `0` is compact. */\n indent?: number;\n /** File system to write to. Defaults to `node:fs/promises`, loaded on first use. */\n fs?: ReadWriteFileSystemLike;\n /** Create missing parent directories on first use. Defaults to `true`. */\n createDirectory?: boolean;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\n/**\n * Stores entries as JSON in a file (Node and Bun), either as newline-delimited\n * JSON or as one array. Writes are queued, so concurrent `log` calls stay in\n * order. Only one process should write to a given file.\n */\nexport class JsonFileErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly mode: 'ndjson' | 'array';\n private readonly maxEntries: number | undefined;\n private readonly indent: number;\n private readonly createDirectory: boolean;\n private readonly prefix: string;\n private fs: ReadWriteFileSystemLike | undefined;\n private directoryReady: boolean = false;\n private queue: Promise<unknown> = Promise.resolve();\n\n /** @throws {RangeError} if `maxEntries` or `indent` is invalid, or `maxEntries` is used outside array mode. */\n constructor(\n private readonly filePath: string,\n options: JsonFileErrorOptions = {}\n ) {\n this.mode = options.mode ?? 'ndjson';\n this.maxEntries = options.maxEntries;\n this.indent = options.indent ?? DEFAULT_INDENT;\n this.fs = options.fs;\n this.createDirectory = options.createDirectory ?? true;\n this.prefix = options.prefix ?? '[error-log-adapter]';\n if (this.maxEntries !== undefined) {\n if (this.mode !== 'array') {\n throw new RangeError(\n `JsonFileErrorAdapter: maxEntries only applies to mode \"array\", but mode is \"${this.mode}\". Use mode \"array\" or remove maxEntries.`\n );\n }\n if (!Number.isInteger(this.maxEntries) || this.maxEntries < 1) {\n throw new RangeError(\n `JsonFileErrorAdapter: maxEntries must be a positive integer, received ${String(this.maxEntries)}.`\n );\n }\n }\n if (!Number.isInteger(this.indent) || this.indent < 0) {\n throw new RangeError(\n `JsonFileErrorAdapter: indent must be a non-negative integer, received ${String(this.indent)}.`\n );\n }\n }\n\n log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n const result: Promise<ErrorLogResult> = this.queue.then(() =>\n this.write(entry)\n );\n // write() never rejects, but keep the queue alive no matter what.\n this.queue = result.catch(() => undefined);\n return result;\n }\n\n private async write(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record: JsonFileErrorRecord = {\n id: crypto.randomUUID(),\n ...serializeEntry(entry),\n };\n const fs: ReadWriteFileSystemLike = await this.getFileSystem();\n await this.prepareDirectory(fs);\n if (this.mode === 'ndjson') {\n await fs.appendFile(this.filePath, `${JSON.stringify(record)}\\n`);\n } else {\n await this.rewriteArray(fs, record);\n }\n return { ok: true };\n } catch (error: unknown) {\n console.error(`${this.prefix} JsonFileErrorAdapter:`, error);\n return {\n ok: false,\n error: `JsonFileErrorAdapter failed to write \"${this.filePath}\" in ${this.mode} mode: ${describeError(error)}`,\n };\n }\n }\n\n private async getFileSystem(): Promise<ReadWriteFileSystemLike> {\n if (this.fs === undefined) {\n this.fs = await loadNodeFileSystem();\n }\n return this.fs;\n }\n\n private async prepareDirectory(fs: ReadWriteFileSystemLike): Promise<void> {\n if (!this.createDirectory || this.directoryReady) {\n return;\n }\n const directory: string | null = parentDirectory(this.filePath);\n if (directory !== null) {\n await fs.mkdir(directory, { recursive: true });\n }\n this.directoryReady = true;\n }\n\n /**\n * Adds the record to the array and replaces the file. The new content is\n * written to a temporary file first and renamed into place, so a crash\n * mid-write cannot leave a half-written file.\n */\n private async rewriteArray(\n fs: ReadWriteFileSystemLike,\n record: JsonFileErrorRecord\n ): Promise<void> {\n const records: JsonFileErrorRecord[] = [\n ...(await this.readRecords(fs)),\n record,\n ];\n const kept: JsonFileErrorRecord[] =\n this.maxEntries === undefined ? records : records.slice(-this.maxEntries);\n const temporaryPath: string = `${this.filePath}.tmp`;\n await fs.writeFile(\n temporaryPath,\n `${JSON.stringify(kept, null, this.indent)}\\n`\n );\n await fs.rename(temporaryPath, this.filePath);\n }\n\n /**\n * Returns the records already in the file. A missing or empty file has none.\n * A file that is not a JSON array is moved to `<path>.corrupt` (so nothing is\n * lost) and logging continues with a fresh file.\n */\n private async readRecords(\n fs: ReadWriteFileSystemLike\n ): Promise<JsonFileErrorRecord[]> {\n let content: string;\n try {\n content = await fs.readFile(this.filePath, 'utf8');\n } catch (error: unknown) {\n if (isMissingFileError(error)) {\n return [];\n }\n throw error;\n }\n if (content.trim() === '') {\n return [];\n }\n let problem: string;\n try {\n const parsed: unknown = JSON.parse(content);\n if (Array.isArray(parsed)) {\n return parsed as JsonFileErrorRecord[];\n }\n problem = 'did not hold a JSON array';\n } catch (error: unknown) {\n problem = `held invalid JSON (${describeError(error)})`;\n }\n const corruptPath: string = `${this.filePath}.corrupt`;\n await fs.rename(this.filePath, corruptPath);\n console.error(\n `${this.prefix} JsonFileErrorAdapter: \"${this.filePath}\" ${problem}, so it was moved to \"${corruptPath}\" and a new file was started.`\n );\n return [];\n }\n}\n","import { describeError, serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default table name. */\nexport const DEFAULT_TABLE_NAME = 'error_log';\n\n/**\n * Longest allowed table name. The index name adds `_occurred_at_idx` (16\n * characters) and Postgres limits identifiers to 63.\n */\nexport const MAX_TABLE_NAME_LENGTH = 47;\n\nconst IDENTIFIER_PATTERN: RegExp = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\nexport type SqlDialect = 'postgres' | 'mysql';\n\n/**\n * The only thing these adapters need from a database driver: run one\n * parameterized statement. `pg` (`Pool` / `Client`) and `mysql2/promise`\n * (`Pool` / `Connection`) both fit as they are.\n */\nexport interface SqlClientLike {\n query(sql: string, params: unknown[]): Promise<unknown>;\n}\n\nexport interface SqlErrorOptions {\n /** Table to write to. Defaults to `error_log`. Letters, digits and `_` only, up to 47 characters. */\n tableName?: string;\n /** Create the table (and index) on first use. Defaults to `true`. */\n createTable?: boolean;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\n/** Throws a descriptive error unless `tableName` is a short, plain SQL identifier. */\nfunction assertValidTableName(tableName: string, adapterName: string): void {\n if (!IDENTIFIER_PATTERN.test(tableName)) {\n throw new RangeError(\n `${adapterName}: invalid table name \"${tableName}\". Use only letters, digits and underscores, and do not start with a digit.`\n );\n }\n if (tableName.length > MAX_TABLE_NAME_LENGTH) {\n throw new RangeError(\n `${adapterName}: table name \"${tableName}\" is ${tableName.length} characters long, but the limit is ${MAX_TABLE_NAME_LENGTH}.`\n );\n }\n}\n\nfunction quoteIdentifier(dialect: SqlDialect, identifier: string): string {\n return dialect === 'postgres' ? `\"${identifier}\"` : `\\`${identifier}\\``;\n}\n\n/**\n * Returns the statements that create the error log table and its index for a\n * dialect. Use this to run your own migration when `createTable` is disabled.\n *\n * Postgres gets a native `UUID` key, `JSONB` for the cause, and `TIMESTAMPTZ`\n * columns. MySQL gets a `CHAR(36)` UUID key (it has no UUID type), a `JSON`\n * cause, and `DATETIME(3)` columns holding UTC times.\n */\nexport function buildErrorLogTableDdl(\n dialect: SqlDialect,\n tableName: string = DEFAULT_TABLE_NAME\n): string[] {\n assertValidTableName(tableName, 'buildErrorLogTableDdl');\n const table: string = quoteIdentifier(dialect, tableName);\n const index: string = quoteIdentifier(\n dialect,\n `${tableName}_occurred_at_idx`\n );\n if (dialect === 'postgres') {\n return [\n `CREATE TABLE IF NOT EXISTS ${table} (` +\n 'id UUID PRIMARY KEY, ' +\n 'code TEXT NOT NULL, ' +\n 'message TEXT NOT NULL, ' +\n 'cause JSONB, ' +\n 'source TEXT, ' +\n 'occurred_at TIMESTAMPTZ NOT NULL, ' +\n 'created_at TIMESTAMPTZ NOT NULL DEFAULT now()' +\n ')',\n `CREATE INDEX IF NOT EXISTS ${index} ON ${table} (occurred_at)`,\n ];\n }\n // MySQL has no CREATE INDEX IF NOT EXISTS, so the index is part of the table.\n return [\n `CREATE TABLE IF NOT EXISTS ${table} (` +\n 'id CHAR(36) NOT NULL PRIMARY KEY, ' +\n 'code VARCHAR(255) NOT NULL, ' +\n 'message TEXT NOT NULL, ' +\n 'cause JSON NULL, ' +\n 'source VARCHAR(255) NULL, ' +\n 'occurred_at DATETIME(3) NOT NULL, ' +\n 'created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), ' +\n `INDEX ${index} (occurred_at)` +\n ')',\n ];\n}\n\n/** `2026-01-02T03:04:05.678Z` becomes `2026-01-02 03:04:05.678`, which MySQL accepts. */\nfunction toMysqlDateTime(date: Date): string {\n return date.toISOString().replace('T', ' ').replace('Z', '');\n}\n\n/** Shared by the Postgres and MySQL adapters: only the SQL text and time format differ. */\nabstract class SqlErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly tableName: string;\n private readonly createTable: boolean;\n private readonly prefix: string;\n private tableReady: Promise<void> | null = null;\n\n protected constructor(\n private readonly client: SqlClientLike,\n private readonly dialect: SqlDialect,\n private readonly adapterName: string,\n options: SqlErrorOptions\n ) {\n this.tableName = options.tableName ?? DEFAULT_TABLE_NAME;\n assertValidTableName(this.tableName, adapterName);\n this.createTable = options.createTable ?? true;\n this.prefix = options.prefix ?? '[error-log-adapter]';\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const serialized = serializeEntry(entry);\n const occurredAt: Date = new Date(serialized.timestamp);\n if (Number.isNaN(occurredAt.getTime())) {\n throw new RangeError(\n `timestamp \"${serialized.timestamp}\" is not a valid ISO 8601 date`\n );\n }\n const params: unknown[] = [\n crypto.randomUUID(),\n serialized.code,\n serialized.message,\n serialized.cause === undefined\n ? null\n : JSON.stringify(serialized.cause),\n serialized.source ?? null,\n this.dialect === 'postgres'\n ? occurredAt.toISOString()\n : toMysqlDateTime(occurredAt),\n ];\n await this.ensureTable();\n await this.client.query(this.insertSql(), params);\n return { ok: true };\n } catch (error: unknown) {\n console.error(`${this.prefix} ${this.adapterName}:`, error);\n return {\n ok: false,\n error: `${this.adapterName} failed to write to table \"${this.tableName}\": ${describeError(error)}`,\n };\n }\n }\n\n private insertSql(): string {\n const placeholders: string =\n this.dialect === 'postgres'\n ? '$1, $2, $3, $4, $5, $6'\n : '?, ?, ?, ?, ?, ?';\n return `INSERT INTO ${quoteIdentifier(this.dialect, this.tableName)} (id, code, message, cause, source, occurred_at) VALUES (${placeholders})`;\n }\n\n /** Creates the table once. A failed attempt is retried on the next log call. */\n private ensureTable(): Promise<void> {\n if (!this.createTable) {\n return Promise.resolve();\n }\n if (this.tableReady === null) {\n this.tableReady = this.runCreateStatements().catch(\n (error: unknown): never => {\n this.tableReady = null;\n throw error;\n }\n );\n }\n return this.tableReady;\n }\n\n private async runCreateStatements(): Promise<void> {\n for (const sql of buildErrorLogTableDdl(this.dialect, this.tableName)) {\n await this.client.query(sql, []);\n }\n }\n}\n\n/**\n * Writes entries to Postgres. Pass a `pg` `Pool` or `Client`.\n *\n * ```ts\n * const adapter = new PostgresErrorAdapter(new Pool({ connectionString }));\n * ```\n */\nexport class PostgresErrorAdapter<\n TCode extends string = string,\n> extends SqlErrorAdapter<TCode> {\n /** @throws {RangeError} if `options.tableName` is not a plain identifier of 47 characters or fewer. */\n constructor(client: SqlClientLike, options: SqlErrorOptions = {}) {\n super(client, 'postgres', 'PostgresErrorAdapter', options);\n }\n}\n\n/**\n * Writes entries to MySQL or MariaDB. Pass a `mysql2/promise` `Pool` or `Connection`.\n *\n * ```ts\n * const adapter = new MySqlErrorAdapter(mysql.createPool(connectionUri));\n * ```\n */\nexport class MySqlErrorAdapter<\n TCode extends string = string,\n> extends SqlErrorAdapter<TCode> {\n /** @throws {RangeError} if `options.tableName` is not a plain identifier of 47 characters or fewer. */\n constructor(client: SqlClientLike, options: SqlErrorOptions = {}) {\n super(client, 'mysql', 'MySqlErrorAdapter', options);\n }\n}\n","import { describeError, serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default Redis key. */\nexport const DEFAULT_REDIS_KEY = 'error_log';\n\n/**\n * The only thing this adapter needs from a Redis client: send one raw command\n * as an array of strings. node-redis (`client.sendCommand`) already matches.\n * ioredis needs a one-line wrapper:\n *\n * ```ts\n * const client: RedisClientLike = {\n * sendCommand: (args) => redis.call(args[0] as string, ...args.slice(1)),\n * };\n * ```\n */\nexport interface RedisClientLike {\n sendCommand(args: string[]): Promise<unknown>;\n}\n\nexport interface RedisErrorOptions {\n /**\n * `list` (default) pushes each entry onto a Redis list as one JSON string.\n * `stream` appends each entry to a Redis stream (`XADD`) as separate fields.\n */\n mode?: 'list' | 'stream';\n /** Key to write to. Defaults to `error_log`. */\n key?: string;\n /**\n * Most entries to keep. The oldest are dropped first. Unlimited by default.\n * Lists are trimmed with `LTRIM` after the push; streams use an exact `MAXLEN`.\n */\n maxEntries?: number;\n /**\n * List mode only: which end to push onto. `right` (default, `RPUSH`) keeps\n * the newest entry last; `left` (`LPUSH`) keeps the newest entry first.\n */\n side?: 'left' | 'right';\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\n/**\n * Writes entries to Redis, either as a capped list of JSON strings or as a\n * stream. Each record holds a UUID `id` plus the serialized entry. In stream\n * mode `cause` and `source` fields are left out when the entry has none.\n */\nexport class RedisErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly mode: 'list' | 'stream';\n private readonly key: string;\n private readonly maxEntries: number | undefined;\n private readonly side: 'left' | 'right';\n private readonly prefix: string;\n\n /** @throws {RangeError} if `key` is empty, `maxEntries` is not a positive integer, or `mode` / `side` is unknown. */\n constructor(\n private readonly client: RedisClientLike,\n options: RedisErrorOptions = {}\n ) {\n this.mode = options.mode ?? 'list';\n this.key = options.key ?? DEFAULT_REDIS_KEY;\n this.maxEntries = options.maxEntries;\n this.side = options.side ?? 'right';\n this.prefix = options.prefix ?? '[error-log-adapter]';\n if (this.mode !== 'list' && this.mode !== 'stream') {\n throw new RangeError(\n `RedisErrorAdapter: mode must be \"list\" or \"stream\", received \"${String(options.mode)}\".`\n );\n }\n if (this.side !== 'left' && this.side !== 'right') {\n throw new RangeError(\n `RedisErrorAdapter: side must be \"left\" or \"right\", received \"${String(options.side)}\".`\n );\n }\n if (this.key === '') {\n throw new RangeError('RedisErrorAdapter: key must not be empty.');\n }\n if (\n this.maxEntries !== undefined &&\n (!Number.isInteger(this.maxEntries) || this.maxEntries < 1)\n ) {\n throw new RangeError(\n `RedisErrorAdapter: maxEntries must be a positive integer, received ${String(this.maxEntries)}.`\n );\n }\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record = { id: crypto.randomUUID(), ...serializeEntry(entry) };\n if (this.mode === 'stream') {\n await this.client.sendCommand(this.buildStreamCommand(record));\n } else {\n await this.client.sendCommand([\n this.side === 'left' ? 'LPUSH' : 'RPUSH',\n this.key,\n JSON.stringify(record),\n ]);\n await this.trimList();\n }\n return { ok: true };\n } catch (error: unknown) {\n console.error(`${this.prefix} RedisErrorAdapter:`, error);\n return {\n ok: false,\n error: `RedisErrorAdapter failed to write to ${this.mode} \"${this.key}\": ${describeError(error)}`,\n };\n }\n }\n\n private buildStreamCommand(record: {\n id: string;\n code: string;\n message: string;\n cause?: unknown;\n source?: string;\n timestamp: string;\n }): string[] {\n const command: string[] = ['XADD', this.key];\n if (this.maxEntries !== undefined) {\n command.push('MAXLEN', '=', String(this.maxEntries));\n }\n command.push(\n '*',\n 'id',\n record.id,\n 'code',\n record.code,\n 'message',\n record.message,\n 'timestamp',\n record.timestamp\n );\n if (record.cause !== undefined) {\n command.push('cause', JSON.stringify(record.cause));\n }\n if (record.source !== undefined) {\n command.push('source', record.source);\n }\n return command;\n }\n\n /**\n * Drops the oldest list entries beyond `maxEntries`. The entry is already\n * stored by now, so a trim failure is reported but does not fail the call;\n * the next successful write trims the list again.\n */\n private async trimList(): Promise<void> {\n if (this.maxEntries === undefined) {\n return;\n }\n const keep: number = this.maxEntries;\n const [start, stop]: [string, string] =\n this.side === 'right' ? [String(-keep), '-1'] : ['0', String(keep - 1)];\n try {\n await this.client.sendCommand(['LTRIM', this.key, start, stop]);\n } catch (error: unknown) {\n console.error(\n `${this.prefix} RedisErrorAdapter: stored the entry but could not trim list \"${this.key}\" to ${keep} entries: ${describeError(error)}`\n );\n }\n }\n}\n","import { describeError, serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default key prefix for stored objects. */\nexport const DEFAULT_S3_PREFIX = 'error_log/';\n\n/** Buffered entries kept after failed uploads: this many times `batchSize`, oldest dropped first. */\nexport const BUFFER_LIMIT_MULTIPLIER = 10;\n\n/** The parameters this adapter passes to `putObject`. */\nexport interface S3PutObjectInput {\n Bucket: string;\n Key: string;\n Body: string;\n ContentType: string;\n}\n\n/**\n * The only thing this adapter needs from an S3 client: `putObject`. The AWS\n * SDK v3 aggregated client (`new S3({ ... })` from `@aws-sdk/client-s3`) fits as\n * it is, and so does anything else that follows the S3 API, including\n * DigitalOcean Spaces, Cloudflare R2 and MinIO when you point the client at\n * their endpoint.\n */\nexport interface S3ClientLike {\n putObject(input: S3PutObjectInput): Promise<unknown>;\n}\n\nexport interface S3BatchOptions {\n /** Upload once this many entries are buffered. */\n batchSize: number;\n /**\n * Also upload when the oldest buffered entry has waited this long (ms). The\n * timer does not keep the process alive, so call `flush()` before exiting.\n */\n maxWaitMs?: number;\n}\n\nexport interface S3ErrorOptions {\n /**\n * Key prefix, for example `logs/app/`. A trailing slash is added if missing.\n * Defaults to `error_log/`. Use `''` for no prefix.\n */\n keyPrefix?: string;\n /**\n * Content type of the uploaded objects. Defaults to `application/json`, or\n * `application/x-ndjson` when batching.\n */\n contentType?: string;\n /**\n * Buffer entries and upload them together as one newline-delimited JSON\n * object. Off by default: one object per entry.\n */\n batch?: S3BatchOptions;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\ninterface S3Record {\n id: string;\n code: string;\n message: string;\n cause?: unknown;\n source?: string;\n timestamp: string;\n}\n\n/** Turns `2026-01-02T03:04:05.678Z` into `2026/01/02/20260102T030405678Z`. */\nfunction datePath(date: Date): string {\n const compact: string = date.toISOString().replace(/[-:.]/g, '');\n return `${compact.slice(0, 4)}/${compact.slice(4, 6)}/${compact.slice(6, 8)}/${compact}`;\n}\n\nfunction normalizePrefix(prefix: string): string {\n const trimmed: string = prefix.replace(/^\\/+/, '');\n return trimmed === '' || trimmed.endsWith('/') ? trimmed : `${trimmed}/`;\n}\n\n/**\n * Stores entries as objects in an S3-compatible bucket, keyed by date and UUID:\n * `error_log/2026/01/02/20260102T030405678Z-<uuid>.json`.\n *\n * By default each entry becomes its own JSON object and `log` resolves once the\n * upload succeeds. With `batch`, entries are buffered and uploaded together as\n * one `.ndjson` object; `log` then resolves ok as soon as the entry is\n * buffered (it is not stored yet), except for the call that fills the batch,\n * which resolves with the upload result. Call `flush()` before shutting down.\n */\nexport class S3ErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly keyPrefix: string;\n private readonly contentType: string;\n private readonly batch: S3BatchOptions | undefined;\n private readonly logPrefix: string;\n private buffer: S3Record[] = [];\n private timer: ReturnType<typeof setTimeout> | null = null;\n private flushQueue: Promise<unknown> = Promise.resolve();\n\n /** @throws {RangeError} if `bucket` is empty or the batch options are invalid. */\n constructor(\n private readonly client: S3ClientLike,\n private readonly bucket: string,\n options: S3ErrorOptions = {}\n ) {\n this.keyPrefix = normalizePrefix(options.keyPrefix ?? DEFAULT_S3_PREFIX);\n this.batch = options.batch;\n this.contentType =\n options.contentType ??\n (this.batch === undefined ? 'application/json' : 'application/x-ndjson');\n this.logPrefix = options.prefix ?? '[error-log-adapter]';\n if (bucket === '') {\n throw new RangeError('S3ErrorAdapter: bucket must not be empty.');\n }\n if (this.batch !== undefined) {\n if (!Number.isInteger(this.batch.batchSize) || this.batch.batchSize < 1) {\n throw new RangeError(\n `S3ErrorAdapter: batch.batchSize must be a positive integer, received ${String(this.batch.batchSize)}.`\n );\n }\n if (\n this.batch.maxWaitMs !== undefined &&\n (!Number.isFinite(this.batch.maxWaitMs) || this.batch.maxWaitMs <= 0)\n ) {\n throw new RangeError(\n `S3ErrorAdapter: batch.maxWaitMs must be a positive number, received ${String(this.batch.maxWaitMs)}.`\n );\n }\n }\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record: S3Record = {\n id: crypto.randomUUID(),\n ...serializeEntry(entry),\n };\n const occurredAt: Date = new Date(record.timestamp);\n if (Number.isNaN(occurredAt.getTime())) {\n throw new RangeError(\n `timestamp \"${record.timestamp}\" is not a valid ISO 8601 date`\n );\n }\n if (this.batch === undefined) {\n const key: string = `${this.keyPrefix}${datePath(occurredAt)}-${record.id}.json`;\n await this.upload(key, `${JSON.stringify(record)}\\n`);\n return { ok: true };\n }\n return await this.addToBatch(record, this.batch);\n } catch (error: unknown) {\n return this.fail(error);\n }\n }\n\n /**\n * Uploads any buffered entries now. Resolves ok when nothing is buffered.\n * Entries from a failed upload stay buffered and are retried by the next flush.\n */\n flush(): Promise<ErrorLogResult> {\n const result: Promise<ErrorLogResult> = this.flushQueue.then(() =>\n this.uploadBuffered()\n );\n // uploadBuffered() never rejects, but keep the queue alive no matter what.\n this.flushQueue = result.catch(() => undefined);\n return result;\n }\n\n private async addToBatch(\n record: S3Record,\n batch: S3BatchOptions\n ): Promise<ErrorLogResult> {\n this.buffer.push(record);\n const limit: number = batch.batchSize * BUFFER_LIMIT_MULTIPLIER;\n if (this.buffer.length > limit) {\n const dropped: number = this.buffer.length - limit;\n this.buffer = this.buffer.slice(dropped);\n console.error(\n `${this.logPrefix} S3ErrorAdapter: dropped the ${dropped} oldest buffered entries because uploads to bucket \"${this.bucket}\" keep failing and the buffer is full (${limit} entries).`\n );\n }\n if (this.buffer.length >= batch.batchSize) {\n return this.flush();\n }\n this.scheduleFlush(batch);\n return { ok: true };\n }\n\n private scheduleFlush(batch: S3BatchOptions): void {\n if (batch.maxWaitMs === undefined || this.timer !== null) {\n return;\n }\n const timer = setTimeout((): void => {\n this.timer = null;\n void this.flush();\n }, batch.maxWaitMs);\n (timer as { unref?: () => void }).unref?.();\n this.timer = timer;\n }\n\n private async uploadBuffered(): Promise<ErrorLogResult> {\n if (this.timer !== null) {\n clearTimeout(this.timer);\n this.timer = null;\n }\n const pending: S3Record[] = this.buffer.slice();\n if (pending.length === 0) {\n return { ok: true };\n }\n try {\n const key: string = `${this.keyPrefix}${datePath(new Date())}-${crypto.randomUUID()}.ndjson`;\n const body: string = pending\n .map((record: S3Record): string => JSON.stringify(record))\n .join('\\n');\n await this.upload(key, `${body}\\n`);\n const uploaded: Set<S3Record> = new Set(pending);\n this.buffer = this.buffer.filter(\n (record: S3Record): boolean => !uploaded.has(record)\n );\n return { ok: true };\n } catch (error: unknown) {\n if (this.batch !== undefined) {\n this.scheduleFlush(this.batch);\n }\n return this.fail(error, pending.length);\n }\n }\n\n private upload(key: string, body: string): Promise<unknown> {\n return this.client.putObject({\n Bucket: this.bucket,\n Key: key,\n Body: body,\n ContentType: this.contentType,\n });\n }\n\n private fail(error: unknown, bufferedCount?: number): ErrorLogResult {\n console.error(`${this.logPrefix} S3ErrorAdapter:`, error);\n const retry: string =\n bufferedCount === undefined\n ? ''\n : ` (${bufferedCount} buffered ${bufferedCount === 1 ? 'entry was' : 'entries were'} kept for the next flush)`;\n return {\n ok: false,\n error: `S3ErrorAdapter failed to upload to bucket \"${this.bucket}\": ${describeError(error)}${retry}`,\n };\n }\n}\n"],"mappings":";AAeO,SAAS,eAAe,OAAyB;AACtD,MAAI,iBAAiB,OAAO;AAC1B,WAAO,EAAE,MAAM,MAAM,MAAM,SAAS,MAAM,SAAS,OAAO,MAAM,MAAM;AAAA,EACxE;AACA,SAAO;AACT;AAGO,SAAS,eAAe,OAA+C;AAC5E,QAAM,aAAsC;AAAA,IAC1C,MAAM,MAAM;AAAA,IACZ,SAAS,MAAM;AAAA,IACf,WAAW,MAAM,cAAa,oBAAI,KAAK,GAAE,YAAY;AAAA,EACvD;AACA,MAAI,MAAM,UAAU,QAAW;AAC7B,eAAW,QAAQ,eAAe,MAAM,KAAK;AAAA,EAC/C;AACA,MAAI,MAAM,WAAW,QAAW;AAC9B,eAAW,SAAS,MAAM;AAAA,EAC5B;AACA,SAAO;AACT;AAGO,SAAS,cAAc,OAAwB;AACpD,SAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAC9D;;;AC9BO,IAAM,sBAAN,MAE6B;AAAA,EAGlC,YAAY,UAA+B,CAAC,GAAG;AAC7C,SAAK,SAAS,QAAQ,UAAU;AAAA,EAClC;AAAA,EAEA,MAAM,IAAI,OAAsD;AAC9D,QAAI;AACF,cAAQ,MAAM,KAAK,QAAQ,KAAK;AAChC,aAAO,EAAE,IAAI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TableName"]}
|
|
1
|
+
{"version":3,"sources":["../src/serialize.ts","../src/fail.ts","../src/adapters/console-error-adapter.ts","../src/uuid.ts","../src/entry-id.ts","../src/timeout.ts","../src/adapters/webhook-error-adapter.ts","../src/adapters/mongodb-error-adapter.ts","../src/adapters/composite-error-adapter.ts","../src/adapters/indexeddb-error-adapter.ts","../src/sql-ident.ts","../src/adapters/sqlite-error-adapter.ts","../src/adapters/web-storage-error-adapter.ts","../src/queue.ts","../src/file-system.ts","../src/adapters/flat-file-error-adapter.ts","../src/adapters/json-file-error-adapter.ts","../src/adapters/sql-error-adapter.ts","../src/adapters/redis-error-adapter.ts","../src/adapters/s3-error-adapter.ts","../src/adapters/firebase-error-adapter.ts","../src/adapters/supabase-error-adapter.ts"],"sourcesContent":["import type { ErrorLogEntry } from './types.js';\n\n/** A JSON-safe copy of an `ErrorLogEntry`. */\nexport interface SerializedErrorLogEntry {\n code: string;\n message: string;\n cause?: unknown;\n source?: string;\n timestamp: string;\n}\n\n/** How many levels of nested causes and objects are kept before the rest is replaced by a marker. */\nexport const MAX_CAUSE_DEPTH = 6;\n\nfunction isErrorLike(value: object): value is {\n name: string;\n message: string;\n stack?: unknown;\n cause?: unknown;\n errors?: unknown;\n} {\n if (value instanceof Error) {\n return true;\n }\n // An error from another realm (vm, iframe) fails `instanceof Error`.\n const candidate = value as { name?: unknown; message?: unknown };\n return (\n typeof candidate.name === 'string' &&\n typeof candidate.message === 'string' &&\n typeof (value as { stack?: unknown }).stack === 'string'\n );\n}\n\nfunction readProperty(source: object, key: string): unknown {\n try {\n return (source as Record<string, unknown>)[key];\n } catch (error: unknown) {\n return `[Unreadable: ${error instanceof Error ? error.message : String(error)}]`;\n }\n}\n\nfunction toJsonSafe(\n value: unknown,\n depth: number,\n ancestors: WeakSet<object>\n): unknown {\n switch (typeof value) {\n case 'string':\n case 'boolean':\n case 'undefined':\n return value;\n case 'number':\n return Number.isFinite(value) ? value : String(value);\n case 'bigint':\n return `${value}n`;\n case 'symbol':\n return value.toString();\n case 'function':\n return `[Function ${value.name || 'anonymous'}]`;\n default:\n break;\n }\n if (value === null) {\n return null;\n }\n const objectValue: object = value as object;\n if (ancestors.has(objectValue)) {\n return '[Circular]';\n }\n if (depth >= MAX_CAUSE_DEPTH) {\n return '[Truncated]';\n }\n ancestors.add(objectValue);\n try {\n if (isErrorLike(objectValue)) {\n const result: Record<string, unknown> = {\n name: objectValue.name,\n message: objectValue.message,\n stack: objectValue.stack,\n };\n const nested: unknown = readProperty(objectValue, 'cause');\n if (nested !== undefined) {\n result['cause'] = toJsonSafe(nested, depth + 1, ancestors);\n }\n const errors: unknown = readProperty(objectValue, 'errors');\n if (Array.isArray(errors)) {\n result['errors'] = errors.map((item: unknown): unknown =>\n toJsonSafe(item, depth + 1, ancestors)\n );\n }\n return result;\n }\n if (Array.isArray(value)) {\n return value.map((item: unknown): unknown =>\n toJsonSafe(item, depth + 1, ancestors)\n );\n }\n const toJson: unknown = readProperty(objectValue, 'toJSON');\n if (typeof toJson === 'function') {\n try {\n return toJsonSafe(\n (toJson as () => unknown).call(objectValue),\n depth + 1,\n ancestors\n );\n } catch (error: unknown) {\n return `[toJSON failed: ${error instanceof Error ? error.message : String(error)}]`;\n }\n }\n const result: Record<string, unknown> = {};\n for (const key of Object.keys(objectValue)) {\n result[key] = toJsonSafe(\n readProperty(objectValue, key),\n depth + 1,\n ancestors\n );\n }\n return result;\n } finally {\n ancestors.delete(objectValue);\n }\n}\n\n/**\n * Returns a copy of `cause` that `JSON.stringify` can always handle, so a\n * strange cause can never stop an entry from being stored.\n *\n * - `Error` objects (including ones from another realm) become\n * `{ name, message, stack }`, plus `cause` and, for an `AggregateError`,\n * `errors`, which are converted the same way.\n * - Circular references become `\"[Circular]\"`, and anything nested deeper than\n * `MAX_CAUSE_DEPTH` levels becomes `\"[Truncated]\"`.\n * - `BigInt`, `Symbol` and function values, and non-finite numbers, become strings.\n * - A property or `toJSON` that throws is replaced by a short description.\n */\nexport function serializeCause(cause: unknown): unknown {\n return toJsonSafe(cause, 0, new WeakSet<object>());\n}\n\n/** Returns a JSON-safe copy of the entry, adding a timestamp if missing. */\nexport function serializeEntry(entry: ErrorLogEntry): SerializedErrorLogEntry {\n const serialized: SerializedErrorLogEntry = {\n code: entry.code,\n message: entry.message,\n timestamp: entry.timestamp ?? new Date().toISOString(),\n };\n if (entry.cause !== undefined) {\n serialized.cause = serializeCause(entry.cause);\n }\n if (entry.source !== undefined) {\n serialized.source = entry.source;\n }\n return serialized;\n}\n\n/** Turns an unknown thrown value into a descriptive message. */\nexport function describeError(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n","import { describeError } from './serialize.js';\nimport type { ErrorLogResult } from './types.js';\n\n/** Text printed before failure messages unless an adapter's `prefix` option says otherwise. */\nexport const DEFAULT_PREFIX = '[error-log-adapter]';\n\nexport interface FailureReport {\n /** Class name of the adapter, such as `PostgresErrorAdapter`. */\n adapterName: string;\n /** The adapter's `prefix` option. */\n prefix: string;\n /** What was being done, completing \"<adapter> failed ...\", such as `to write to table \"error_log\"`. */\n action: string;\n /** What went wrong. It is printed with `console.error`, and its message is returned. */\n error: unknown;\n /** Replaces the message taken from `error`, for sinks that describe failures in their own way. */\n reason?: string;\n}\n\n/**\n * Reports a failed `log` call the same way for every adapter: the problem goes\n * to `console.error` under the adapter's prefix, and the caller gets\n * `{ ok: false, error: \"<adapter> failed <action>: <reason>\" }`.\n */\nexport function reportFailure(report: FailureReport): ErrorLogResult {\n console.error(`${report.prefix} ${report.adapterName}:`, report.error);\n return {\n ok: false,\n error: `${report.adapterName} failed ${report.action}: ${report.reason ?? describeError(report.error)}`,\n };\n}\n","import { DEFAULT_PREFIX } from '../fail.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\nexport interface ConsoleErrorOptions {\n /** Text printed before each entry. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\nexport class ConsoleErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly prefix: string;\n\n constructor(options: ConsoleErrorOptions = {}) {\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n console.error(this.prefix, entry);\n return { ok: true };\n } catch (error: unknown) {\n return {\n ok: false,\n error: `ConsoleErrorAdapter failed: ${String(error)}`,\n };\n }\n }\n}\n","/** The parts of the Web Crypto API used here. */\ninterface CryptoLike {\n randomUUID?: () => string;\n getRandomValues?: (array: Uint8Array) => Uint8Array;\n}\n\n/**\n * Returns a random UUID (version 4).\n *\n * `crypto.randomUUID` only exists in secure contexts (`https://` and\n * `localhost`), so on a plain `http://` page it is undefined and calling it\n * would throw. `crypto.getRandomValues` is available everywhere, so it is used\n * as the fallback, with `Math.random` as the last resort for runtimes without\n * any `crypto`.\n */\nexport function generateId(): string {\n const cryptoApi: CryptoLike | undefined = (\n globalThis as { crypto?: CryptoLike }\n ).crypto;\n if (cryptoApi !== undefined && typeof cryptoApi.randomUUID === 'function') {\n return cryptoApi.randomUUID();\n }\n const bytes: Uint8Array = new Uint8Array(16);\n if (\n cryptoApi !== undefined &&\n typeof cryptoApi.getRandomValues === 'function'\n ) {\n cryptoApi.getRandomValues(bytes);\n } else {\n for (let index = 0; index < bytes.length; index += 1) {\n bytes[index] = Math.floor(Math.random() * 256);\n }\n }\n bytes[6] = (bytes[6] & 0x0f) | 0x40;\n bytes[8] = (bytes[8] & 0x3f) | 0x80;\n const hex: string = Array.from(bytes, (byte: number): string =>\n byte.toString(16).padStart(2, '0')\n ).join('');\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;\n}\n","import type { ErrorLogEntry } from './types.js';\nimport { generateId } from './uuid.js';\n\nconst UUID_PATTERN: RegExp =\n /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;\n\n/** True when the caller gave the entry its own `id`. */\nexport function hasCallerId(entry: ErrorLogEntry): boolean {\n return entry.id !== undefined;\n}\n\n/**\n * Returns the id to store an entry under: the caller's `id` (lower-cased, so the\n * same UUID always maps to the same record), or a new random UUID.\n *\n * @throws {RangeError} if the caller's `id` is not a UUID. Adapters call this\n * inside `log`'s try block, so the problem is reported as `ok: false`.\n */\nexport function resolveEntryId(entry: ErrorLogEntry): string {\n if (entry.id === undefined) {\n return generateId();\n }\n if (typeof entry.id !== 'string' || !UUID_PATTERN.test(entry.id)) {\n throw new RangeError(\n `entry.id must be a UUID such as one from crypto.randomUUID(), received ${JSON.stringify(entry.id)}`\n );\n }\n return entry.id.toLowerCase();\n}\n","/** Default time to wait for a sink to answer, in milliseconds. */\nexport const DEFAULT_TIMEOUT_MS = 10_000;\n\n/** Thrown (as a rejection) by `withTimeout` when the deadline passes first. */\nexport class SinkTimeoutError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'SinkTimeoutError';\n }\n}\n\n/**\n * Validates a `timeoutMs` option and applies the default.\n *\n * @throws {RangeError} unless the value is a positive number or `Infinity`.\n */\nexport function resolveTimeoutMs(\n value: number | undefined,\n adapterName: string\n): number {\n const timeoutMs: number = value ?? DEFAULT_TIMEOUT_MS;\n if (Number.isNaN(timeoutMs) || timeoutMs <= 0) {\n throw new RangeError(\n `${adapterName}: timeoutMs must be a positive number or Infinity, received ${String(value)}.`\n );\n }\n return timeoutMs;\n}\n\n/** What a caller should know when a sink did not answer in time. */\nexport function timeoutMessage(\n sink: string,\n timeoutMs: number,\n action: string = 'respond'\n): string {\n return `${sink} did not ${action} within ${timeoutMs} ms, so the entry may or may not have been stored (the operation can still finish later, so retrying could store it twice)`;\n}\n\n/**\n * Runs `start()` and waits for its result, but rejects with `message` if it takes\n * longer than `timeoutMs`. Drivers cannot generally cancel a call, so the call may\n * still complete after the deadline; `log()` just stops waiting for it. A `start`\n * that throws synchronously rejects too. `Infinity` waits without limit.\n */\nexport function withTimeout<T>(\n start: () => PromiseLike<T> | T,\n timeoutMs: number,\n message: string\n): Promise<T> {\n if (timeoutMs === Infinity) {\n return new Promise<T>((resolve, reject): void => {\n try {\n resolve(start() as T | PromiseLike<T>);\n } catch (error: unknown) {\n reject(error);\n }\n });\n }\n return new Promise<T>(\n (resolve: (value: T) => void, reject: (reason: unknown) => void): void => {\n const timer = setTimeout((): void => {\n reject(new SinkTimeoutError(message));\n }, timeoutMs);\n const settle =\n <A extends unknown[]>(\n done: (...args: A) => void\n ): ((...args: A) => void) =>\n (...args: A): void => {\n clearTimeout(timer);\n done(...args);\n };\n try {\n Promise.resolve(start()).then(settle(resolve), settle(reject));\n } catch (error: unknown) {\n settle(reject)(error);\n }\n }\n );\n}\n","import { DEFAULT_PREFIX } from '../fail.js';\nimport { hasCallerId, resolveEntryId } from '../entry-id.js';\nimport { describeError, serializeEntry } from '../serialize.js';\nimport { resolveTimeoutMs, timeoutMessage } from '../timeout.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\nexport interface WebhookErrorOptions {\n /** Extra headers merged into the request. */\n headers?: Record<string, string>;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n /**\n * Allow a plain `http://` URL. Entries contain error messages and stack\n * traces, so they are only sent over `https://` unless this is `true`.\n * `http://localhost`, `127.0.0.1` and `[::1]` are always allowed.\n * Defaults to `false`.\n */\n allowInsecure?: boolean;\n /**\n * How long to wait for the webhook to answer, in milliseconds. Defaults to\n * 10000. Pass `Infinity` to wait without limit. When it passes, the request is\n * cancelled, so the connection is closed rather than left open; the server may\n * still have received the entry.\n */\n timeoutMs?: number;\n}\n\nconst LOOPBACK_HOSTS: ReadonlySet<string> = new Set([\n 'localhost',\n '127.0.0.1',\n '[::1]',\n]);\n\n/** Parses the URL, and rejects one that is invalid or would send entries unencrypted. */\nfunction parseWebhookUrl(url: string, allowInsecure: boolean): URL {\n let parsed: URL;\n try {\n parsed = new URL(url);\n } catch {\n throw new RangeError(\n 'WebhookErrorAdapter: the webhook URL is not a valid absolute URL (for example https://example.com/errors).'\n );\n }\n if (parsed.protocol === 'https:') {\n return parsed;\n }\n if (\n parsed.protocol === 'http:' &&\n (allowInsecure || LOOPBACK_HOSTS.has(parsed.hostname))\n ) {\n return parsed;\n }\n throw new RangeError(\n `WebhookErrorAdapter: the webhook URL must use https:// because entries contain error messages and stack traces (received ${parsed.protocol}//${parsed.host}). Pass allowInsecure: true to send over http.`\n );\n}\n\nexport class WebhookErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly prefix: string;\n private readonly timeoutMs: number;\n /** Scheme and host only. Webhook URLs often carry a secret in the path or query. */\n private readonly target: string;\n\n /** @throws {RangeError} if `url` is not a valid `https://` URL (see `allowInsecure`) or `timeoutMs` is invalid. */\n constructor(\n private readonly url: string,\n private readonly options: WebhookErrorOptions = {}\n ) {\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.timeoutMs = resolveTimeoutMs(options.timeoutMs, 'WebhookErrorAdapter');\n this.target = parseWebhookUrl(url, options.allowInsecure ?? false).origin;\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n const controller: AbortController = new AbortController();\n let timedOut = false;\n const timer: ReturnType<typeof setTimeout> | undefined =\n this.timeoutMs === Infinity\n ? undefined\n : setTimeout((): void => {\n timedOut = true;\n controller.abort();\n }, this.timeoutMs);\n try {\n const response: Response = await fetch(this.url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n ...this.options.headers,\n },\n // A caller-supplied id goes along so the receiver can drop a repeat.\n body: JSON.stringify(\n hasCallerId(entry)\n ? { id: resolveEntryId(entry), ...serializeEntry(entry) }\n : serializeEntry(entry)\n ),\n // A redirect would turn the POST into a GET without a body (302, 303)\n // or re-send the entry and headers to another host (307, 308).\n redirect: 'manual',\n signal: controller.signal,\n });\n if (response.type === 'opaqueredirect') {\n return this.fail(this.redirectMessage('a redirect'));\n }\n if (!response.ok) {\n if (response.status >= 300 && response.status < 400) {\n return this.fail(this.redirectMessage(`HTTP ${response.status}`));\n }\n return this.fail(\n `webhook ${this.target} responded with HTTP ${response.status}`\n );\n }\n return { ok: true };\n } catch (error: unknown) {\n console.error(`${this.prefix} WebhookErrorAdapter:`, error);\n if (timedOut) {\n return this.fail(\n timeoutMessage(`webhook ${this.target}`, this.timeoutMs)\n );\n }\n return this.fail(\n `could not reach webhook ${this.target}: ${this.redact(describeError(error))}`\n );\n } finally {\n clearTimeout(timer);\n }\n }\n\n private redirectMessage(what: string): string {\n return `webhook ${this.target} responded with ${what}, which is not followed because the entry would not arrive intact; use the final URL instead`;\n }\n\n /** Replaces the full URL in a message with its origin, in case the runtime put it there. */\n private redact(message: string): string {\n return message.split(this.url).join(this.target);\n }\n\n private fail(reason: string): ErrorLogResult {\n return { ok: false, error: `WebhookErrorAdapter failed: ${reason}` };\n }\n}\n","import { DEFAULT_PREFIX, reportFailure } from '../fail.js';\nimport { hasCallerId, resolveEntryId } from '../entry-id.js';\nimport { resolveTimeoutMs, timeoutMessage, withTimeout } from '../timeout.js';\nimport { serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** The subset of a MongoDB `Collection` this adapter uses. */\nexport interface CollectionLike {\n insertOne(doc: object): Promise<unknown>;\n}\n\nexport interface MongoDbErrorOptions {\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n /**\n * How long `log` waits for the insert before giving up, in milliseconds. Defaults to\n * 10000. Pass `Infinity` to wait without limit. The call cannot be cancelled,\n * so it may still finish after `log` has returned a failure.\n */\n timeoutMs?: number;\n}\n\n/** MongoDB reports a repeated `_id` as error code 11000. */\nfunction isDuplicateKeyError(error: unknown): boolean {\n return (\n typeof error === 'object' &&\n error !== null &&\n (error as { code?: unknown }).code === 11000\n );\n}\n\nexport class MongoDbErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly prefix: string;\n private readonly timeoutMs: number;\n\n /** @throws {RangeError} if `options.timeoutMs` is not a positive number or Infinity. */\n constructor(\n private readonly collection: CollectionLike,\n options: MongoDbErrorOptions = {}\n ) {\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.timeoutMs = resolveTimeoutMs(options.timeoutMs, 'MongoDbErrorAdapter');\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const document: object = hasCallerId(entry)\n ? { _id: resolveEntryId(entry), ...serializeEntry(entry) }\n : serializeEntry(entry);\n await withTimeout(\n (): Promise<unknown> => this.collection.insertOne(document),\n this.timeoutMs,\n timeoutMessage('MongoDB', this.timeoutMs)\n );\n return { ok: true };\n } catch (error: unknown) {\n if (hasCallerId(entry) && isDuplicateKeyError(error)) {\n // The same id was stored before: this call is a retry that already succeeded.\n return { ok: true };\n }\n return reportFailure({\n adapterName: 'MongoDbErrorAdapter',\n prefix: this.prefix,\n action: 'to insert entry',\n error,\n });\n }\n }\n}\n","import {\n DEFAULT_TIMEOUT_MS,\n SinkTimeoutError,\n resolveTimeoutMs,\n withTimeout,\n} from '../timeout.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\nexport interface CompositeErrorOptions {\n /**\n * How long to wait for each adapter before reporting it as failed, in\n * milliseconds. Defaults to 30000, longer than the 10000 default of the built-in\n * adapters so their own, more specific timeout messages come first. This only\n * matters for a custom adapter that can hang: one stuck adapter then cannot\n * hold back the result of the others. Pass `Infinity` to wait without limit.\n */\n timeoutMs?: number;\n}\n\n/** Default wait for each adapter of a composite. */\nexport const DEFAULT_COMPOSITE_TIMEOUT_MS: number = DEFAULT_TIMEOUT_MS * 3;\n\n/** Sends every entry to several adapters. Succeeds only if all of them do. */\nexport class CompositeErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly timeoutMs: number;\n\n /** @throws {RangeError} if `options.timeoutMs` is not a positive number or Infinity. */\n constructor(\n private readonly adapters: ReadonlyArray<ErrorLogAdapter<TCode>>,\n options: CompositeErrorOptions = {}\n ) {\n this.timeoutMs = resolveTimeoutMs(\n options.timeoutMs ?? DEFAULT_COMPOSITE_TIMEOUT_MS,\n 'CompositeErrorAdapter'\n );\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n const results: ErrorLogResult[] = await Promise.all(\n this.adapters.map(\n async (\n adapter: ErrorLogAdapter<TCode>,\n index: number\n ): Promise<ErrorLogResult> => {\n try {\n return await withTimeout(\n (): Promise<ErrorLogResult> => adapter.log(entry),\n this.timeoutMs,\n `adapter ${index} (${adapter.constructor.name}) did not return within ${this.timeoutMs} ms, so the entry may or may not have been stored there`\n );\n } catch (error: unknown) {\n if (error instanceof SinkTimeoutError) {\n return { ok: false, error: error.message };\n }\n // Adapters should not throw, but a faulty custom one must not break the others.\n return {\n ok: false,\n error: `adapter threw instead of returning a result: ${String(error)}`,\n };\n }\n }\n )\n );\n const failures: string[] = results.flatMap(\n (result: ErrorLogResult): string[] =>\n result.ok ? [] : [result.error ?? 'unknown failure']\n );\n if (failures.length === 0) {\n return { ok: true };\n }\n return {\n ok: false,\n error: `${failures.length} of ${results.length} adapters failed: ${failures.join('; ')}`,\n };\n }\n}\n","import { DEFAULT_PREFIX, reportFailure } from '../fail.js';\nimport { hasCallerId, resolveEntryId } from '../entry-id.js';\nimport { resolveTimeoutMs, timeoutMessage, withTimeout } from '../timeout.js';\nimport { serializeEntry } from '../serialize.js';\nimport type { SerializedErrorLogEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default object store name. */\nexport const DEFAULT_STORE_NAME = 'error_log';\n\n/** Event handler slot. Typed loosely so real DOM `IDBTransaction` objects fit. */\ntype IdbHandler = ((event: never) => unknown) | null;\n\n/** The parts of a DOM event this adapter calls. */\ninterface IdbEventLike {\n preventDefault?(): void;\n stopPropagation?(): void;\n}\n\n/** The error object of a failed request or transaction (a `DOMException`). */\ninterface IdbErrorLike {\n name?: string;\n message?: string;\n}\n\n/** The subset of an `IDBRequest` this adapter uses. */\nexport interface IdbRequestLike {\n onerror: IdbHandler;\n readonly error?: IdbErrorLike | null;\n}\n\n/** The subset of an `IDBObjectStore` this adapter uses. */\nexport interface IdbObjectStoreLike {\n /** Returns an `IDBRequest`. Fakes that return nothing also work. */\n add(value: unknown): unknown;\n /**\n * Replaces the record with the same key. Used for entries that carry their own\n * `id`, so logging one again does not fail on the existing key. A real\n * `IDBObjectStore` has it; a fake without it falls back to `add`.\n */\n put?(value: unknown): unknown;\n}\n\n/** The subset of an `IDBTransaction` this adapter uses. */\nexport interface IdbTransactionLike {\n objectStore(name: string): IdbObjectStoreLike;\n oncomplete: IdbHandler;\n onerror: IdbHandler;\n onabort: IdbHandler;\n readonly error?: IdbErrorLike | null;\n}\n\n/** The subset of an `IDBDatabase` this adapter uses. */\nexport interface IdbDatabaseLike {\n transaction(storeNames: string, mode: 'readwrite'): IdbTransactionLike;\n}\n\n/** The subset of an `IDBDatabase` available inside `onupgradeneeded`. */\nexport interface IdbUpgradeDatabaseLike {\n readonly objectStoreNames: { contains(name: string): boolean };\n createObjectStore(\n name: string,\n options: { keyPath: string }\n ): { createIndex(name: string, keyPath: string): unknown };\n}\n\n/** A stored record: the serialized entry plus its generated UUID key. */\nexport interface IndexedDbErrorRecord extends SerializedErrorLogEntry {\n id: string;\n}\n\nexport interface IndexedDbErrorOptions {\n /** Object store to write to. Defaults to `error_log`. */\n storeName?: string;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n /**\n * How long `log` waits for the transaction to finish, in milliseconds. Defaults\n * to 10000. Pass `Infinity` to wait without limit. A transaction can stay\n * pending when a version change in another tab blocks it, or when the browser\n * has evicted the origin's storage. The write may still finish afterwards.\n */\n timeoutMs?: number;\n}\n\n/**\n * Creates the error log object store (UUID `id` key, `timestamp` index).\n * Call it from your `onupgradeneeded` handler. Does nothing if it already exists.\n */\nexport function createErrorLogStore(\n database: IdbUpgradeDatabaseLike,\n storeName: string = DEFAULT_STORE_NAME\n): void {\n if (database.objectStoreNames.contains(storeName)) {\n return;\n }\n database\n .createObjectStore(storeName, { keyPath: 'id' })\n .createIndex('timestamp', 'timestamp');\n}\n\nexport class IndexedDbErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly storeName: string;\n private readonly prefix: string;\n private readonly timeoutMs: number;\n\n /** @throws {RangeError} if `options.timeoutMs` is not a positive number or Infinity. */\n constructor(\n private readonly database: IdbDatabaseLike,\n options: IndexedDbErrorOptions = {}\n ) {\n this.storeName = options.storeName ?? DEFAULT_STORE_NAME;\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.timeoutMs = resolveTimeoutMs(\n options.timeoutMs,\n 'IndexedDbErrorAdapter'\n );\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record: IndexedDbErrorRecord = {\n id: resolveEntryId(entry),\n ...serializeEntry(entry),\n };\n await this.write(record, hasCallerId(entry));\n return { ok: true };\n } catch (error: unknown) {\n return reportFailure({\n adapterName: 'IndexedDbErrorAdapter',\n prefix: this.prefix,\n action: `to write to object store \"${this.storeName}\"`,\n error,\n });\n }\n }\n\n /**\n * Resolves once the transaction commits; rejects if the request fails, the\n * transaction aborts, or it does not finish within `timeoutMs`.\n *\n * Error events are handled here (`preventDefault` and `stopPropagation`), so a\n * failed log write does not reach the `onerror` handler an app may have set on\n * its own database object.\n */\n private write(\n record: IndexedDbErrorRecord,\n replaceExisting: boolean\n ): Promise<void> {\n return withTimeout(\n (): Promise<void> =>\n new Promise<void>(\n (resolve: () => void, reject: (reason: unknown) => void): void => {\n const transaction: IdbTransactionLike = this.database.transaction(\n this.storeName,\n 'readwrite'\n );\n const fail =\n (what: string, source: { error?: IdbErrorLike | null }) =>\n (event?: IdbEventLike): void => {\n event?.preventDefault?.();\n event?.stopPropagation?.();\n reject(new Error(`${what}: ${describeDomError(source.error)}`));\n };\n transaction.oncomplete = (): void => resolve();\n transaction.onerror = fail('transaction failed', transaction);\n transaction.onabort = fail('transaction aborted', transaction);\n const store: IdbObjectStoreLike = transaction.objectStore(\n this.storeName\n );\n const request: unknown =\n replaceExisting && typeof store.put === 'function'\n ? store.put(record)\n : store.add(record);\n if (typeof request === 'object' && request !== null) {\n // The request error holds the details (for example a\n // ConstraintError); the transaction's own error is null for it.\n (request as IdbRequestLike).onerror = fail(\n 'request failed',\n request as IdbRequestLike\n );\n }\n }\n ),\n this.timeoutMs,\n timeoutMessage('IndexedDB', this.timeoutMs, 'finish the transaction')\n );\n }\n}\n\n/** `ConstraintError: Unable to add key to index...`, or a note that nothing was given. */\nfunction describeDomError(error: IdbErrorLike | null | undefined): string {\n if (error === null || error === undefined) {\n return 'no details provided';\n }\n const text: string = [error.name, error.message]\n .filter((part): part is string => typeof part === 'string' && part !== '')\n .join(': ');\n return text === '' ? 'no details provided' : text;\n}\n","/** Default table name for the SQL, SQLite and Supabase adapters. */\nexport const DEFAULT_TABLE_NAME = 'error_log';\n\nconst IDENTIFIER_PATTERN: RegExp = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\n/**\n * Throws a descriptive `RangeError` unless `value` is a plain SQL identifier:\n * letters, digits and underscores, not starting with a digit. Quoting is then\n * never needed, and nothing in the name can alter a statement.\n *\n * @param label What the value is, for the message, such as `table name`.\n * @param owner Who is asking, such as `PostgresErrorAdapter`.\n * @param maxLength Longest allowed length, when the caller needs a limit.\n */\nexport function assertIdentifier(\n value: string,\n label: string,\n owner: string,\n maxLength?: number\n): void {\n if (!IDENTIFIER_PATTERN.test(value)) {\n throw new RangeError(\n `${owner}: invalid ${label} \"${value}\". Use only letters, digits and underscores, and do not start with a digit.`\n );\n }\n if (maxLength !== undefined && value.length > maxLength) {\n throw new RangeError(\n `${owner}: ${label} \"${value}\" is ${value.length} characters long, but the limit is ${maxLength}.`\n );\n }\n}\n","import { DEFAULT_PREFIX, reportFailure } from '../fail.js';\nimport { DEFAULT_TABLE_NAME, assertIdentifier } from '../sql-ident.js';\nimport { resolveEntryId } from '../entry-id.js';\nimport { resolveTimeoutMs, timeoutMessage, withTimeout } from '../timeout.js';\nimport { serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/**\n * The only thing this adapter needs from a SQLite driver: run one statement\n * with positional (`?`) parameters. `bun:sqlite` already matches this shape.\n * Other drivers need a one-line wrapper, for example:\n *\n * ```ts\n * // better-sqlite3 or node:sqlite\n * const client: SqliteClientLike = {\n * run: (sql, params) => db.prepare(sql).run(...params),\n * };\n * ```\n */\nexport interface SqliteClientLike {\n run(sql: string, params: unknown[]): unknown;\n}\n\nexport interface SqliteErrorOptions {\n /** Table to write to. Defaults to `error_log`. Letters, digits and `_` only. */\n tableName?: string;\n /** Create the table (and index) on first use. Defaults to `true`. */\n createTable?: boolean;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n /**\n * How long `log` waits for the driver before giving up, in milliseconds. Defaults\n * to 10000. Pass `Infinity` to wait without limit. The call cannot be\n * cancelled, so it may still finish after `log` has returned a failure.\n */\n timeoutMs?: number;\n}\n\n/** Throws a descriptive error unless `tableName` is a plain SQL identifier. */\nfunction assertValidTableName(tableName: string): void {\n assertIdentifier(tableName, 'table name', 'SqliteErrorAdapter');\n}\n\n/**\n * Returns the statements that create the error log table and its index.\n * Use this to run your own migration when `createTable` is disabled.\n */\nexport function buildCreateErrorLogTableSql(\n tableName: string = DEFAULT_TABLE_NAME\n): string[] {\n assertValidTableName(tableName);\n return [\n `CREATE TABLE IF NOT EXISTS ${tableName} (` +\n 'id TEXT PRIMARY KEY NOT NULL, ' +\n 'code TEXT NOT NULL, ' +\n 'message TEXT NOT NULL, ' +\n 'cause TEXT, ' +\n 'source TEXT, ' +\n 'occurred_at TEXT NOT NULL, ' +\n \"created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))\" +\n ')',\n `CREATE INDEX IF NOT EXISTS ${tableName}_occurred_at_idx ON ${tableName} (occurred_at)`,\n ];\n}\n\nexport class SqliteErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly tableName: string;\n private readonly createTable: boolean;\n private readonly prefix: string;\n private readonly timeoutMs: number;\n private tableReady: Promise<void> | null = null;\n\n /** @throws {RangeError} if `options.tableName` is not a plain identifier or `options.timeoutMs` is invalid. */\n constructor(\n private readonly client: SqliteClientLike,\n options: SqliteErrorOptions = {}\n ) {\n this.tableName = options.tableName ?? DEFAULT_TABLE_NAME;\n assertValidTableName(this.tableName);\n this.createTable = options.createTable ?? true;\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.timeoutMs = resolveTimeoutMs(options.timeoutMs, 'SqliteErrorAdapter');\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n await this.ensureTable();\n const serialized = serializeEntry(entry);\n await this.run(\n `INSERT INTO ${this.tableName} (id, code, message, cause, source, occurred_at) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT (id) DO NOTHING`,\n [\n resolveEntryId(entry),\n serialized.code,\n serialized.message,\n serialized.cause === undefined\n ? null\n : JSON.stringify(serialized.cause),\n serialized.source ?? null,\n serialized.timestamp,\n ]\n );\n return { ok: true };\n } catch (error: unknown) {\n return reportFailure({\n adapterName: 'SqliteErrorAdapter',\n prefix: this.prefix,\n action: `to write to table \"${this.tableName}\"`,\n error,\n });\n }\n }\n\n /** Creates the table once. A failed attempt is retried on the next log call. */\n private ensureTable(): Promise<void> {\n if (!this.createTable) {\n return Promise.resolve();\n }\n if (this.tableReady === null) {\n this.tableReady = this.runCreateStatements().catch(\n (error: unknown): never => {\n this.tableReady = null;\n throw error;\n }\n );\n }\n return this.tableReady;\n }\n\n private async runCreateStatements(): Promise<void> {\n for (const sql of buildCreateErrorLogTableSql(this.tableName)) {\n await this.run(sql, []);\n }\n }\n\n /** Runs one statement, giving up waiting after `timeoutMs`. */\n private run(sql: string, params: unknown[]): Promise<unknown> {\n return withTimeout(\n (): unknown => this.client.run(sql, params),\n this.timeoutMs,\n timeoutMessage('SQLite', this.timeoutMs)\n );\n }\n}\n","import { DEFAULT_PREFIX, reportFailure } from '../fail.js';\nimport { resolveEntryId } from '../entry-id.js';\nimport { describeError, serializeEntry } from '../serialize.js';\nimport type { SerializedErrorLogEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default storage key. */\nexport const DEFAULT_STORAGE_KEY = 'error_log';\n\n/** Default number of entries kept before the oldest are dropped. */\nexport const DEFAULT_MAX_ENTRIES = 100;\n\n/**\n * The subset of the Web Storage API this adapter uses. Both `sessionStorage`\n * and `localStorage` fit.\n */\nexport interface StorageLike {\n getItem(key: string): string | null;\n setItem(key: string, value: string): void;\n}\n\n/**\n * The part of `navigator.locks` this adapter uses. `localStorage` is shared by\n * every tab of an origin and has no atomic update, so a lock keeps two tabs from\n * overwriting each other's entry.\n */\nexport interface LockManagerLike {\n request<T>(name: string, callback: () => T | Promise<T>): Promise<T>;\n}\n\n/** A stored record: the serialized entry plus its generated UUID. */\nexport interface WebStorageErrorRecord extends SerializedErrorLogEntry {\n id: string;\n}\n\nexport interface WebStorageErrorOptions {\n /** Key the entries are stored under, as one JSON array. Defaults to `error_log`. */\n key?: string;\n /** Most entries to keep. The oldest are dropped first. Defaults to `100`. */\n maxEntries?: number;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n /**\n * Lock manager that makes tabs sharing a `localStorage` take turns. Defaults to\n * `navigator.locks` when the browser has it. This reduces lost entries but does\n * not remove them: a tab in another process can still read a stale copy\n * (measured in Chromium). Pass `null` to turn locking off.\n */\n locks?: LockManagerLike | null;\n}\n\n/** Browsers report a full store with different names and codes. */\nfunction isQuotaError(error: unknown): boolean {\n if (typeof error !== 'object' || error === null) {\n return false;\n }\n const { name, code } = error as { name?: unknown; code?: unknown };\n return (\n (typeof name === 'string' && name.toLowerCase().includes('quota')) ||\n code === 22 ||\n code === 1014\n );\n}\n\n/**\n * Keeps the newest entries in a Web Storage object (`sessionStorage` or\n * `localStorage`) as a JSON array under one key, capped at `maxEntries`.\n * When the store is full, the oldest entries are dropped until the new one fits.\n *\n * `sessionStorage` belongs to one tab, so it is safe. `localStorage` is shared by\n * every tab of the origin and has no atomic update: two tabs logging at the same\n * moment can overwrite each other's entry while both get `ok: true`. In Chromium,\n * tabs in separate processes (the usual case) even read a stale copy, so a lock\n * reduces the loss without removing it. Where tabs may log at the same time and\n * every entry matters, use `IndexedDbErrorAdapter`, whose writes are transactional.\n */\nexport class WebStorageErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly key: string;\n private readonly maxEntries: number;\n private readonly prefix: string;\n private readonly locks: LockManagerLike | null;\n\n /** @throws {RangeError} if `options.maxEntries` is not a positive integer. */\n constructor(\n private readonly storage: StorageLike,\n options: WebStorageErrorOptions = {}\n ) {\n this.key = options.key ?? DEFAULT_STORAGE_KEY;\n this.maxEntries = options.maxEntries ?? DEFAULT_MAX_ENTRIES;\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.locks =\n options.locks === undefined ? defaultLockManager() : options.locks;\n if (!Number.isInteger(this.maxEntries) || this.maxEntries < 1) {\n throw new RangeError(\n `WebStorageErrorAdapter: maxEntries must be a positive integer, received ${String(options.maxEntries)}.`\n );\n }\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record: WebStorageErrorRecord = {\n id: resolveEntryId(entry),\n ...serializeEntry(entry),\n };\n await this.appendLocked(record);\n return { ok: true };\n } catch (error: unknown) {\n return reportFailure({\n adapterName: 'WebStorageErrorAdapter',\n prefix: this.prefix,\n action: `to write to storage key \"${this.key}\"`,\n error,\n });\n }\n }\n\n /**\n * Appends under a cross-tab lock when one is available. If the lock itself\n * cannot be taken (for example a sandboxed frame), the entry is still written\n * without it rather than lost.\n */\n private async appendLocked(record: WebStorageErrorRecord): Promise<void> {\n if (this.locks === null) {\n this.append(record);\n return;\n }\n let appended = false;\n try {\n await this.locks.request(`error-log-adapter:${this.key}`, (): void => {\n appended = true;\n this.append(record);\n });\n } catch (error: unknown) {\n if (appended) {\n throw error;\n }\n this.append(record);\n }\n }\n\n /** Reads, appends, trims, and writes back. Runs without awaiting, so calls in one tab never interleave. */\n private append(record: WebStorageErrorRecord): void {\n // An entry logged again with the same id replaces its earlier record.\n const records: WebStorageErrorRecord[] = [\n ...this.readRecords().filter(\n (existing: WebStorageErrorRecord): boolean => existing?.id !== record.id\n ),\n record,\n ].slice(-this.maxEntries);\n for (;;) {\n try {\n this.storage.setItem(this.key, JSON.stringify(records));\n return;\n } catch (error: unknown) {\n if (!isQuotaError(error) || records.length <= 1) {\n throw error;\n }\n records.shift();\n }\n }\n }\n\n /** Returns the stored records, or none if the value is missing or unreadable. */\n private readRecords(): WebStorageErrorRecord[] {\n const stored: string | null = this.storage.getItem(this.key);\n if (stored === null) {\n return [];\n }\n try {\n const parsed: unknown = JSON.parse(stored);\n if (Array.isArray(parsed)) {\n return parsed as WebStorageErrorRecord[];\n }\n this.setAside(stored, 'did not hold an array');\n } catch (error: unknown) {\n this.setAside(stored, `held invalid JSON (${describeError(error)})`);\n }\n return [];\n }\n\n /**\n * Copies an unusable value to `<key>.corrupt` before it is replaced, so data\n * the host app stored under the same key is not silently destroyed. Only the\n * first one is kept; a later unusable value would not overwrite it.\n */\n private setAside(stored: string, reason: string): void {\n const backupKey = `${this.key}${CORRUPT_KEY_SUFFIX}`;\n let savedTo = 'but it could not be copied aside';\n try {\n if (this.storage.getItem(backupKey) === null) {\n this.storage.setItem(backupKey, stored);\n savedTo = `and a copy was kept under \"${backupKey}\"`;\n } else {\n savedTo = `and \"${backupKey}\" already holds an earlier copy`;\n }\n } catch {\n // The store may be full; the original value is replaced regardless.\n }\n console.error(\n `${this.prefix} WebStorageErrorAdapter: storage key \"${this.key}\" ${reason}, so it was replaced, ${savedTo}.`\n );\n }\n}\n\n/** Suffix of the key an unusable value is copied to before being replaced. */\nexport const CORRUPT_KEY_SUFFIX = '.corrupt';\n\nfunction defaultLockManager(): LockManagerLike | null {\n const locks: unknown = (globalThis as { navigator?: { locks?: unknown } })\n .navigator?.locks;\n return typeof (locks as LockManagerLike | undefined)?.request === 'function'\n ? (locks as LockManagerLike)\n : null;\n}\n","/**\n * Runs tasks one after another, in the order they were queued. A task that\n * rejects does not stop the ones queued behind it.\n */\nexport class SerialQueue {\n private tail: Promise<unknown> = Promise.resolve();\n\n /** Starts `task` once every earlier task has finished, and resolves with its result. */\n run<T>(task: () => Promise<T>): Promise<T> {\n const result: Promise<T> = this.tail.then(task);\n this.tail = result.catch((): undefined => undefined);\n return result;\n }\n}\n","/**\n * The subset of `node:fs/promises` the file adapters use. Pass `fs.promises`\n * (or `node:fs/promises`) yourself, or leave `fs` unset in Node and Bun.\n */\nexport interface FileSystemLike {\n appendFile(path: string, data: string): Promise<unknown>;\n mkdir(path: string, options: { recursive: true }): Promise<unknown>;\n stat(path: string): Promise<{ size: number }>;\n rename(from: string, to: string): Promise<unknown>;\n rm(path: string, options: { force: true }): Promise<unknown>;\n}\n\n/** `FileSystemLike` plus the reading and replacing that whole-file writes need. */\nexport interface ReadWriteFileSystemLike extends FileSystemLike {\n readFile(path: string, encoding: 'utf8'): Promise<string>;\n writeFile(path: string, data: string): Promise<unknown>;\n}\n\n/** Loads `node:fs/promises`. Only call this from code that runs in Node or Bun. */\nexport async function loadNodeFileSystem(): Promise<ReadWriteFileSystemLike> {\n // A variable specifier keeps browser bundlers from resolving it.\n const moduleName: string = 'node:fs/promises';\n return (await import(moduleName)) as ReadWriteFileSystemLike;\n}\n\n/** A filesystem root: `/`, `\\\\`, `C:\\\\` or `C:/`, which always exists and cannot be created. */\nconst ROOT_PATTERN: RegExp = /^(?:[A-Za-z]:)?[\\\\/]+$/;\n\n/**\n * Returns the folder to create for a file path, or `null` when there is nothing\n * to create: the path has no folder part (`app.log`), or its folder is a\n * filesystem root that always exists (`/app.log`, `C:\\\\app.log`, `\\\\app.log`).\n * Calling `mkdir` on a Windows drive root fails with `EPERM`.\n */\nexport function parentDirectory(filePath: string): string | null {\n const lastSeparator: number = Math.max(\n filePath.lastIndexOf('/'),\n filePath.lastIndexOf('\\\\')\n );\n if (lastSeparator < 0) {\n return null;\n }\n const directory: string = filePath.slice(0, lastSeparator + 1);\n if (ROOT_PATTERN.test(directory)) {\n return null;\n }\n return filePath.slice(0, lastSeparator);\n}\n\n/** True for the error Node throws when a file or folder does not exist. */\nexport function isMissingFileError(error: unknown): boolean {\n return (\n typeof error === 'object' &&\n error !== null &&\n (error as { code?: unknown }).code === 'ENOENT'\n );\n}\n","import { SerialQueue } from '../queue.js';\nimport { DEFAULT_PREFIX, reportFailure } from '../fail.js';\nimport {\n isMissingFileError,\n loadNodeFileSystem,\n parentDirectory,\n} from '../file-system.js';\nimport type { FileSystemLike } from '../file-system.js';\nimport { describeError, serializeEntry } from '../serialize.js';\nimport type { SerializedErrorLogEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default number of rotated files kept when `maxBytes` is set. */\nexport const DEFAULT_MAX_FILES = 3;\n\nexport interface FlatFileErrorOptions {\n /** File system to write to. Defaults to `node:fs/promises`, loaded on first use. */\n fs?: FileSystemLike;\n /** Turns an entry into one line of text. Defaults to `formatErrorLogLine`. */\n formatLine?: (entry: SerializedErrorLogEntry) => string;\n /** Rotate the file before it would grow past this many bytes. Off by default. */\n maxBytes?: number;\n /** Rotated files to keep (`app.log.1` is newest). Defaults to `3`. Needs `maxBytes`. */\n maxFiles?: number;\n /** Create missing parent directories on first use. Defaults to `true`. */\n createDirectory?: boolean;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\n/** Keeps one entry on one line by escaping line breaks. */\nfunction escapeLineBreaks(text: string): string {\n return text.replace(/\\r?\\n/g, '\\\\n');\n}\n\nfunction describeCause(cause: unknown): string {\n if (typeof cause === 'string') {\n return cause;\n }\n if (typeof cause === 'object' && cause !== null) {\n const { name, message } = cause as { name?: unknown; message?: unknown };\n if (typeof name === 'string' && typeof message === 'string') {\n return `${name}: ${message}`;\n }\n }\n try {\n return JSON.stringify(cause) ?? String(cause);\n } catch {\n return String(cause);\n }\n}\n\n/**\n * The default line format:\n * `2026-01-02T03:04:05.000Z [CODE] source: message (cause: Error: details)`.\n * `source` and `cause` are left out when the entry has none.\n */\nexport function formatErrorLogLine(entry: SerializedErrorLogEntry): string {\n const source: string = entry.source === undefined ? '' : `${entry.source}: `;\n const cause: string =\n entry.cause === undefined ? '' : ` (cause: ${describeCause(entry.cause)})`;\n return escapeLineBreaks(\n `${entry.timestamp} [${entry.code}] ${source}${entry.message}${cause}`\n );\n}\n\nfunction assertPositiveInteger(value: number, name: string): void {\n if (!Number.isInteger(value) || value < 1) {\n throw new RangeError(\n `FlatFileErrorAdapter: ${name} must be a positive integer, received ${String(value)}.`\n );\n }\n}\n\n/**\n * Appends one text line per entry to a file, optionally rotating it by size.\n * Writes are queued, so concurrent `log` calls never interleave or race a rotation.\n */\nexport class FlatFileErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly formatLine: (entry: SerializedErrorLogEntry) => string;\n private readonly maxBytes: number | undefined;\n private readonly maxFiles: number;\n private readonly createDirectory: boolean;\n private readonly prefix: string;\n private fs: FileSystemLike | undefined;\n private directoryReady: boolean = false;\n private readonly queue: SerialQueue = new SerialQueue();\n\n /** @throws {RangeError} if `maxBytes` or `maxFiles` is not a positive integer. */\n constructor(\n private readonly filePath: string,\n options: FlatFileErrorOptions = {}\n ) {\n this.fs = options.fs;\n this.formatLine = options.formatLine ?? formatErrorLogLine;\n this.maxBytes = options.maxBytes;\n this.maxFiles = options.maxFiles ?? DEFAULT_MAX_FILES;\n this.createDirectory = options.createDirectory ?? true;\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n if (this.maxBytes !== undefined) {\n assertPositiveInteger(this.maxBytes, 'maxBytes');\n }\n assertPositiveInteger(this.maxFiles, 'maxFiles');\n }\n\n log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n return this.queue.run((): Promise<ErrorLogResult> => this.write(entry));\n }\n\n private async write(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const fs: FileSystemLike = await this.getFileSystem();\n const formatted: string = this.formatLine(serializeEntry(entry));\n const line: string = formatted.endsWith('\\n')\n ? formatted\n : `${formatted}\\n`;\n await this.prepareDirectory(fs);\n await this.rotateIfNeeded(fs, new TextEncoder().encode(line).length);\n await fs.appendFile(this.filePath, line);\n return { ok: true };\n } catch (error: unknown) {\n return reportFailure({\n adapterName: 'FlatFileErrorAdapter',\n prefix: this.prefix,\n action: `to append to \"${this.filePath}\"`,\n error,\n });\n }\n }\n\n /** Uses the injected file system, or loads `node:fs/promises` on first use. */\n private async getFileSystem(): Promise<FileSystemLike> {\n if (this.fs === undefined) {\n this.fs = await loadNodeFileSystem();\n }\n return this.fs;\n }\n\n private async prepareDirectory(fs: FileSystemLike): Promise<void> {\n if (!this.createDirectory || this.directoryReady) {\n return;\n }\n const directory: string | null = parentDirectory(this.filePath);\n if (directory !== null) {\n await fs.mkdir(directory, { recursive: true });\n }\n this.directoryReady = true;\n }\n\n /**\n * Moves `app.log` to `app.log.1` (shifting older files up, dropping the\n * oldest) when the next line would push it past `maxBytes`. A failed rotation\n * is reported, but the entry is still written so it is not lost.\n */\n private async rotateIfNeeded(\n fs: FileSystemLike,\n lineBytes: number\n ): Promise<void> {\n if (this.maxBytes === undefined) {\n return;\n }\n try {\n let currentSize: number;\n try {\n currentSize = (await fs.stat(this.filePath)).size;\n } catch (error: unknown) {\n if (isMissingFileError(error)) {\n return;\n }\n throw error;\n }\n if (currentSize === 0 || currentSize + lineBytes <= this.maxBytes) {\n return;\n }\n await fs.rm(`${this.filePath}.${this.maxFiles}`, { force: true });\n for (let index = this.maxFiles - 1; index >= 1; index -= 1) {\n await this.renameIfPresent(\n fs,\n `${this.filePath}.${index}`,\n `${this.filePath}.${index + 1}`\n );\n }\n await fs.rename(this.filePath, `${this.filePath}.1`);\n } catch (error: unknown) {\n console.error(\n `${this.prefix} FlatFileErrorAdapter: could not rotate \"${this.filePath}\", so it will keep growing: ${describeError(error)}`\n );\n }\n }\n\n private async renameIfPresent(\n fs: FileSystemLike,\n from: string,\n to: string\n ): Promise<void> {\n try {\n await fs.rename(from, to);\n } catch (error: unknown) {\n if (!isMissingFileError(error)) {\n throw error;\n }\n }\n }\n}\n","import { SerialQueue } from '../queue.js';\nimport { DEFAULT_PREFIX, reportFailure } from '../fail.js';\nimport { resolveEntryId } from '../entry-id.js';\nimport {\n isMissingFileError,\n loadNodeFileSystem,\n parentDirectory,\n} from '../file-system.js';\nimport type { ReadWriteFileSystemLike } from '../file-system.js';\nimport { describeError, serializeEntry } from '../serialize.js';\nimport type { SerializedErrorLogEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default number of spaces used to indent array mode output. */\nexport const DEFAULT_INDENT = 2;\n\n/** A stored record: the serialized entry plus its generated UUID. */\nexport interface JsonFileErrorRecord extends SerializedErrorLogEntry {\n id: string;\n}\n\nexport interface JsonFileErrorOptions {\n /**\n * `ndjson` (default) appends one compact JSON object per line, which is cheap\n * and safe to tail. `array` keeps the whole file as one JSON array that is\n * rewritten on every entry.\n */\n mode?: 'ndjson' | 'array';\n /** Array mode only: most entries to keep. The oldest are dropped first. */\n maxEntries?: number;\n /** Array mode only: spaces of indentation. Defaults to `2`; `0` is compact. */\n indent?: number;\n /** File system to write to. Defaults to `node:fs/promises`, loaded on first use. */\n fs?: ReadWriteFileSystemLike;\n /** Create missing parent directories on first use. Defaults to `true`. */\n createDirectory?: boolean;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\n/**\n * Stores entries as JSON in a file (Node and Bun), either as newline-delimited\n * JSON or as one array. Writes are queued, so concurrent `log` calls stay in\n * order. Only one process should write to a given file.\n */\nexport class JsonFileErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly mode: 'ndjson' | 'array';\n private readonly maxEntries: number | undefined;\n private readonly indent: number;\n private readonly createDirectory: boolean;\n private readonly prefix: string;\n private fs: ReadWriteFileSystemLike | undefined;\n private directoryReady: boolean = false;\n private readonly queue: SerialQueue = new SerialQueue();\n\n /** @throws {RangeError} if `maxEntries` or `indent` is invalid, or `maxEntries` is used outside array mode. */\n constructor(\n private readonly filePath: string,\n options: JsonFileErrorOptions = {}\n ) {\n this.mode = options.mode ?? 'ndjson';\n this.maxEntries = options.maxEntries;\n this.indent = options.indent ?? DEFAULT_INDENT;\n this.fs = options.fs;\n this.createDirectory = options.createDirectory ?? true;\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n if (this.maxEntries !== undefined) {\n if (this.mode !== 'array') {\n throw new RangeError(\n `JsonFileErrorAdapter: maxEntries only applies to mode \"array\", but mode is \"${this.mode}\". Use mode \"array\" or remove maxEntries.`\n );\n }\n if (!Number.isInteger(this.maxEntries) || this.maxEntries < 1) {\n throw new RangeError(\n `JsonFileErrorAdapter: maxEntries must be a positive integer, received ${String(this.maxEntries)}.`\n );\n }\n }\n if (!Number.isInteger(this.indent) || this.indent < 0) {\n throw new RangeError(\n `JsonFileErrorAdapter: indent must be a non-negative integer, received ${String(this.indent)}.`\n );\n }\n }\n\n log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n return this.queue.run((): Promise<ErrorLogResult> => this.write(entry));\n }\n\n private async write(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record: JsonFileErrorRecord = {\n id: resolveEntryId(entry),\n ...serializeEntry(entry),\n };\n const fs: ReadWriteFileSystemLike = await this.getFileSystem();\n await this.prepareDirectory(fs);\n if (this.mode === 'ndjson') {\n await fs.appendFile(this.filePath, `${JSON.stringify(record)}\\n`);\n } else {\n await this.rewriteArray(fs, record);\n }\n return { ok: true };\n } catch (error: unknown) {\n return reportFailure({\n adapterName: 'JsonFileErrorAdapter',\n prefix: this.prefix,\n action: `to write \"${this.filePath}\" in ${this.mode} mode`,\n error,\n });\n }\n }\n\n private async getFileSystem(): Promise<ReadWriteFileSystemLike> {\n if (this.fs === undefined) {\n this.fs = await loadNodeFileSystem();\n }\n return this.fs;\n }\n\n private async prepareDirectory(fs: ReadWriteFileSystemLike): Promise<void> {\n if (!this.createDirectory || this.directoryReady) {\n return;\n }\n const directory: string | null = parentDirectory(this.filePath);\n if (directory !== null) {\n await fs.mkdir(directory, { recursive: true });\n }\n this.directoryReady = true;\n }\n\n /**\n * Adds the record to the array and replaces the file. The new content is\n * written to a temporary file first and renamed into place, so a crash\n * mid-write cannot leave a half-written file.\n */\n private async rewriteArray(\n fs: ReadWriteFileSystemLike,\n record: JsonFileErrorRecord\n ): Promise<void> {\n // An entry logged again with the same id replaces its earlier record.\n const records: JsonFileErrorRecord[] = [\n ...(await this.readRecords(fs)).filter(\n (existing: JsonFileErrorRecord): boolean => existing?.id !== record.id\n ),\n record,\n ];\n const kept: JsonFileErrorRecord[] =\n this.maxEntries === undefined ? records : records.slice(-this.maxEntries);\n const temporaryPath: string = `${this.filePath}.tmp`;\n await fs.writeFile(\n temporaryPath,\n `${JSON.stringify(kept, null, this.indent)}\\n`\n );\n await fs.rename(temporaryPath, this.filePath);\n }\n\n /**\n * Returns the records already in the file. A missing or empty file has none.\n * A file that is not a JSON array is moved to `<path>.corrupt` (so nothing is\n * lost) and logging continues with a fresh file.\n */\n private async readRecords(\n fs: ReadWriteFileSystemLike\n ): Promise<JsonFileErrorRecord[]> {\n let content: string;\n try {\n content = await fs.readFile(this.filePath, 'utf8');\n } catch (error: unknown) {\n if (isMissingFileError(error)) {\n return [];\n }\n throw error;\n }\n if (content.trim() === '') {\n return [];\n }\n let problem: string;\n try {\n const parsed: unknown = JSON.parse(content);\n if (Array.isArray(parsed)) {\n return parsed as JsonFileErrorRecord[];\n }\n problem = 'did not hold a JSON array';\n } catch (error: unknown) {\n problem = `held invalid JSON (${describeError(error)})`;\n }\n const corruptPath: string = `${this.filePath}.corrupt`;\n await fs.rename(this.filePath, corruptPath);\n console.error(\n `${this.prefix} JsonFileErrorAdapter: \"${this.filePath}\" ${problem}, so it was moved to \"${corruptPath}\" and a new file was started.`\n );\n return [];\n }\n}\n","import { DEFAULT_PREFIX, reportFailure } from '../fail.js';\nimport { DEFAULT_TABLE_NAME, assertIdentifier } from '../sql-ident.js';\nimport { resolveEntryId } from '../entry-id.js';\nimport { resolveTimeoutMs, timeoutMessage, withTimeout } from '../timeout.js';\nimport { serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/**\n * Longest allowed table name. The index name adds `_occurred_at_idx` (16\n * characters) and Postgres limits identifiers to 63.\n */\nexport const MAX_TABLE_NAME_LENGTH = 47;\n\nexport type SqlDialect = 'postgres' | 'mysql';\n\n/**\n * The only thing these adapters need from a database driver: run one\n * parameterized statement. `pg` (`Pool` / `Client`) and `mysql2/promise`\n * (`Pool` / `Connection`) both fit as they are.\n */\nexport interface SqlClientLike {\n query(sql: string, params: unknown[]): Promise<unknown>;\n}\n\nexport interface SqlErrorOptions {\n /** Table to write to. Defaults to `error_log`. Letters, digits and `_` only, up to 47 characters. */\n tableName?: string;\n /** Create the table (and index) on first use. Defaults to `true`. */\n createTable?: boolean;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n /**\n * How long `log` waits for each database statement before giving up, in milliseconds. Defaults\n * to 10000. Pass `Infinity` to wait without limit. The call cannot be\n * cancelled, so it may still finish after `log` has returned a failure.\n */\n timeoutMs?: number;\n}\n\n/** Throws a descriptive error unless `tableName` is a short, plain SQL identifier. */\nfunction assertValidTableName(tableName: string, adapterName: string): void {\n assertIdentifier(tableName, 'table name', adapterName, MAX_TABLE_NAME_LENGTH);\n}\n\nfunction quoteIdentifier(dialect: SqlDialect, identifier: string): string {\n return dialect === 'postgres' ? `\"${identifier}\"` : `\\`${identifier}\\``;\n}\n\n/**\n * Returns the statements that create the error log table and its index for a\n * dialect. Use this to run your own migration when `createTable` is disabled.\n *\n * Postgres gets a native `UUID` key, `JSONB` for the cause, and `TIMESTAMPTZ`\n * columns. MySQL gets a `CHAR(36)` UUID key (it has no UUID type), a `JSON`\n * cause, and `DATETIME(3)` columns holding UTC times.\n */\nexport function buildErrorLogTableDdl(\n dialect: SqlDialect,\n tableName: string = DEFAULT_TABLE_NAME\n): string[] {\n assertValidTableName(tableName, 'buildErrorLogTableDdl');\n const table: string = quoteIdentifier(dialect, tableName);\n const index: string = quoteIdentifier(\n dialect,\n `${tableName}_occurred_at_idx`\n );\n if (dialect === 'postgres') {\n return [\n `CREATE TABLE IF NOT EXISTS ${table} (` +\n 'id UUID PRIMARY KEY, ' +\n 'code TEXT NOT NULL, ' +\n 'message TEXT NOT NULL, ' +\n 'cause JSONB, ' +\n 'source TEXT, ' +\n 'occurred_at TIMESTAMPTZ NOT NULL, ' +\n 'created_at TIMESTAMPTZ NOT NULL DEFAULT now()' +\n ')',\n `CREATE INDEX IF NOT EXISTS ${index} ON ${table} (occurred_at)`,\n ];\n }\n // MySQL has no CREATE INDEX IF NOT EXISTS, so the index is part of the table.\n return [\n `CREATE TABLE IF NOT EXISTS ${table} (` +\n 'id CHAR(36) NOT NULL PRIMARY KEY, ' +\n 'code TEXT NOT NULL, ' +\n 'message TEXT NOT NULL, ' +\n 'cause JSON NULL, ' +\n 'source TEXT NULL, ' +\n 'occurred_at DATETIME(3) NOT NULL, ' +\n 'created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), ' +\n `INDEX ${index} (occurred_at)` +\n ')',\n ];\n}\n\n/**\n * Returns the statement that widens the `code` and `source` columns of a MySQL\n * table created by `buildErrorLogTableDdl` in 0.4.0 or earlier, where they were\n * `VARCHAR(255)` and a longer value made the insert fail. Run it once.\n */\nexport function buildMySqlWidenColumnsSql(\n tableName: string = DEFAULT_TABLE_NAME\n): string[] {\n assertValidTableName(tableName, 'buildMySqlWidenColumnsSql');\n return [\n `ALTER TABLE ${quoteIdentifier('mysql', tableName)} MODIFY code TEXT NOT NULL, MODIFY source TEXT NULL`,\n ];\n}\n\n/** `2026-01-02T03:04:05.678Z` becomes `2026-01-02 03:04:05.678`, which MySQL accepts. */\nfunction toMysqlDateTime(date: Date): string {\n return date.toISOString().replace('T', ' ').replace('Z', '');\n}\n\n/** Shared by the Postgres and MySQL adapters: only the SQL text and time format differ. */\nabstract class SqlErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly tableName: string;\n private readonly createTable: boolean;\n private readonly prefix: string;\n private readonly timeoutMs: number;\n private tableReady: Promise<void> | null = null;\n\n protected constructor(\n private readonly client: SqlClientLike,\n private readonly dialect: SqlDialect,\n private readonly adapterName: string,\n options: SqlErrorOptions\n ) {\n this.tableName = options.tableName ?? DEFAULT_TABLE_NAME;\n assertValidTableName(this.tableName, adapterName);\n this.createTable = options.createTable ?? true;\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.timeoutMs = resolveTimeoutMs(options.timeoutMs, adapterName);\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const serialized = serializeEntry(entry);\n const occurredAt: Date = new Date(serialized.timestamp);\n if (Number.isNaN(occurredAt.getTime())) {\n throw new RangeError(\n `timestamp \"${serialized.timestamp}\" is not a valid ISO 8601 date`\n );\n }\n const params: unknown[] = [\n resolveEntryId(entry),\n serialized.code,\n serialized.message,\n serialized.cause === undefined\n ? null\n : JSON.stringify(serialized.cause),\n serialized.source ?? null,\n this.dialect === 'postgres'\n ? occurredAt.toISOString()\n : toMysqlDateTime(occurredAt),\n ];\n await this.ensureTable();\n await this.query(this.insertSql(), params);\n return { ok: true };\n } catch (error: unknown) {\n return reportFailure({\n adapterName: this.adapterName,\n prefix: this.prefix,\n action: `to write to table \"${this.tableName}\"`,\n error,\n });\n }\n }\n\n private insertSql(): string {\n const placeholders: string =\n this.dialect === 'postgres'\n ? '$1, $2, $3, $4, $5, $6'\n : '?, ?, ?, ?, ?, ?';\n // A repeated entry id is a retry: keep the stored row instead of failing on the key.\n const onRepeat: string =\n this.dialect === 'postgres'\n ? 'ON CONFLICT (id) DO NOTHING'\n : 'ON DUPLICATE KEY UPDATE id = id';\n return `INSERT INTO ${quoteIdentifier(this.dialect, this.tableName)} (id, code, message, cause, source, occurred_at) VALUES (${placeholders}) ${onRepeat}`;\n }\n\n /** Creates the table once. A failed attempt is retried on the next log call. */\n private ensureTable(): Promise<void> {\n if (!this.createTable) {\n return Promise.resolve();\n }\n if (this.tableReady === null) {\n this.tableReady = this.runCreateStatements().catch(\n (error: unknown): never => {\n this.tableReady = null;\n throw error;\n }\n );\n }\n return this.tableReady;\n }\n\n private async runCreateStatements(): Promise<void> {\n for (const sql of buildErrorLogTableDdl(this.dialect, this.tableName)) {\n await this.query(sql, []);\n }\n }\n\n /** Runs one statement, giving up waiting after `timeoutMs`. */\n private query(sql: string, params: unknown[]): Promise<unknown> {\n return withTimeout(\n (): Promise<unknown> => this.client.query(sql, params),\n this.timeoutMs,\n timeoutMessage(\n this.dialect === 'postgres' ? 'Postgres' : 'MySQL',\n this.timeoutMs\n )\n );\n }\n}\n\n/**\n * Writes entries to Postgres. Pass a `pg` `Pool` or `Client`.\n *\n * ```ts\n * const adapter = new PostgresErrorAdapter(new Pool({ connectionString }));\n * ```\n */\nexport class PostgresErrorAdapter<\n TCode extends string = string,\n> extends SqlErrorAdapter<TCode> {\n /** @throws {RangeError} if `options.tableName` is not a plain identifier of 47 characters or fewer. */\n constructor(client: SqlClientLike, options: SqlErrorOptions = {}) {\n super(client, 'postgres', 'PostgresErrorAdapter', options);\n }\n}\n\n/**\n * Writes entries to MySQL or MariaDB. Pass a `mysql2/promise` `Pool` or `Connection`.\n *\n * ```ts\n * const adapter = new MySqlErrorAdapter(mysql.createPool(connectionUri));\n * ```\n */\nexport class MySqlErrorAdapter<\n TCode extends string = string,\n> extends SqlErrorAdapter<TCode> {\n /** @throws {RangeError} if `options.tableName` is not a plain identifier of 47 characters or fewer. */\n constructor(client: SqlClientLike, options: SqlErrorOptions = {}) {\n super(client, 'mysql', 'MySqlErrorAdapter', options);\n }\n}\n","import { DEFAULT_PREFIX, reportFailure } from '../fail.js';\nimport { resolveEntryId } from '../entry-id.js';\nimport { resolveTimeoutMs, timeoutMessage, withTimeout } from '../timeout.js';\nimport { describeError, serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default Redis key. */\nexport const DEFAULT_REDIS_KEY = 'error_log';\n\n/**\n * The only thing this adapter needs from a Redis client: send one raw command\n * as an array of strings. node-redis (`client.sendCommand`) already matches.\n * ioredis needs a one-line wrapper:\n *\n * ```ts\n * const client: RedisClientLike = {\n * sendCommand: (args) => redis.call(args[0] as string, ...args.slice(1)),\n * };\n * ```\n */\nexport interface RedisClientLike {\n sendCommand(args: string[]): Promise<unknown>;\n}\n\nexport interface RedisErrorOptions {\n /**\n * `list` (default) pushes each entry onto a Redis list as one JSON string.\n * `stream` appends each entry to a Redis stream (`XADD`) as separate fields.\n */\n mode?: 'list' | 'stream';\n /** Key to write to. Defaults to `error_log`. */\n key?: string;\n /**\n * Most entries to keep. The oldest are dropped first. Unlimited by default.\n * Lists are trimmed with `LTRIM` after the push; streams use an exact `MAXLEN`.\n */\n maxEntries?: number;\n /**\n * List mode only: which end to push onto. `right` (default, `RPUSH`) keeps\n * the newest entry last; `left` (`LPUSH`) keeps the newest entry first.\n */\n side?: 'left' | 'right';\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n /**\n * How long `log` waits for each Redis command before giving up, in milliseconds. Defaults\n * to 10000. Pass `Infinity` to wait without limit. The call cannot be\n * cancelled, so it may still finish after `log` has returned a failure.\n */\n timeoutMs?: number;\n}\n\n/**\n * Writes entries to Redis, either as a capped list of JSON strings or as a\n * stream. Each record holds a UUID `id` plus the serialized entry. In stream\n * mode `cause` and `source` fields are left out when the entry has none.\n */\nexport class RedisErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly mode: 'list' | 'stream';\n private readonly key: string;\n private readonly maxEntries: number | undefined;\n private readonly side: 'left' | 'right';\n private readonly prefix: string;\n private readonly timeoutMs: number;\n\n /** @throws {RangeError} if `key` is empty, `maxEntries` is not a positive integer, or `mode` / `side` is unknown. */\n constructor(\n private readonly client: RedisClientLike,\n options: RedisErrorOptions = {}\n ) {\n this.mode = options.mode ?? 'list';\n this.key = options.key ?? DEFAULT_REDIS_KEY;\n this.maxEntries = options.maxEntries;\n this.side = options.side ?? 'right';\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.timeoutMs = resolveTimeoutMs(options.timeoutMs, 'RedisErrorAdapter');\n if (this.mode !== 'list' && this.mode !== 'stream') {\n throw new RangeError(\n `RedisErrorAdapter: mode must be \"list\" or \"stream\", received \"${String(options.mode)}\".`\n );\n }\n if (this.side !== 'left' && this.side !== 'right') {\n throw new RangeError(\n `RedisErrorAdapter: side must be \"left\" or \"right\", received \"${String(options.side)}\".`\n );\n }\n if (this.key === '') {\n throw new RangeError('RedisErrorAdapter: key must not be empty.');\n }\n if (\n this.maxEntries !== undefined &&\n (!Number.isInteger(this.maxEntries) || this.maxEntries < 1)\n ) {\n throw new RangeError(\n `RedisErrorAdapter: maxEntries must be a positive integer, received ${String(this.maxEntries)}.`\n );\n }\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record = { id: resolveEntryId(entry), ...serializeEntry(entry) };\n if (this.mode === 'stream') {\n await this.send(this.buildStreamCommand(record));\n } else {\n await this.send([\n this.side === 'left' ? 'LPUSH' : 'RPUSH',\n this.key,\n JSON.stringify(record),\n ]);\n await this.trimList();\n }\n return { ok: true };\n } catch (error: unknown) {\n return reportFailure({\n adapterName: 'RedisErrorAdapter',\n prefix: this.prefix,\n action: `to write to ${this.mode} \"${this.key}\"`,\n error,\n });\n }\n }\n\n private buildStreamCommand(record: {\n id: string;\n code: string;\n message: string;\n cause?: unknown;\n source?: string;\n timestamp: string;\n }): string[] {\n const command: string[] = ['XADD', this.key];\n if (this.maxEntries !== undefined) {\n command.push('MAXLEN', '=', String(this.maxEntries));\n }\n command.push(\n '*',\n 'id',\n record.id,\n 'code',\n record.code,\n 'message',\n record.message,\n 'timestamp',\n record.timestamp\n );\n if (record.cause !== undefined) {\n command.push('cause', JSON.stringify(record.cause));\n }\n if (record.source !== undefined) {\n command.push('source', record.source);\n }\n return command;\n }\n\n /**\n * Drops the oldest list entries beyond `maxEntries`. The entry is already\n * stored by now, so a trim failure is reported but does not fail the call;\n * the next successful write trims the list again.\n */\n /** Sends one command, giving up waiting after `timeoutMs`. */\n private send(args: string[]): Promise<unknown> {\n return withTimeout(\n (): Promise<unknown> => this.client.sendCommand(args),\n this.timeoutMs,\n timeoutMessage('Redis', this.timeoutMs)\n );\n }\n\n private async trimList(): Promise<void> {\n if (this.maxEntries === undefined) {\n return;\n }\n const keep: number = this.maxEntries;\n const [start, stop]: [string, string] =\n this.side === 'right' ? [String(-keep), '-1'] : ['0', String(keep - 1)];\n try {\n await this.send(['LTRIM', this.key, start, stop]);\n } catch (error: unknown) {\n console.error(\n `${this.prefix} RedisErrorAdapter: stored the entry but could not trim list \"${this.key}\" to ${keep} entries: ${describeError(error)}`\n );\n }\n }\n}\n","import { DEFAULT_PREFIX, reportFailure } from '../fail.js';\nimport { SerialQueue } from '../queue.js';\nimport { resolveEntryId } from '../entry-id.js';\nimport { generateId } from '../uuid.js';\nimport { resolveTimeoutMs, timeoutMessage, withTimeout } from '../timeout.js';\nimport { describeError, serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** Default key prefix for stored objects. */\nexport const DEFAULT_S3_PREFIX = 'error_log/';\n\n/** Buffered entries kept after failed uploads: this many times `batchSize`, oldest dropped first. */\nexport const BUFFER_LIMIT_MULTIPLIER = 10;\n\n/** The parameters this adapter passes to `putObject`. */\nexport interface S3PutObjectInput {\n Bucket: string;\n Key: string;\n Body: string;\n ContentType: string;\n}\n\n/**\n * The only thing this adapter needs from an S3 client: `putObject`. The AWS\n * SDK v3 aggregated client (`new S3({ ... })` from `@aws-sdk/client-s3`) fits as\n * it is, and so does anything else that follows the S3 API, including\n * DigitalOcean Spaces, Cloudflare R2 and MinIO when you point the client at\n * their endpoint.\n *\n * Some S3-compatible providers reject the checksum headers that recent AWS SDK\n * v3 versions add by default. If uploads fail with a checksum or header error,\n * create the client with `requestChecksumCalculation: 'WHEN_REQUIRED'` and\n * `responseChecksumValidation: 'WHEN_REQUIRED'`.\n */\nexport interface S3ClientLike {\n putObject(input: S3PutObjectInput): Promise<unknown>;\n}\n\nexport interface S3BatchOptions {\n /** Upload once this many entries are buffered. */\n batchSize: number;\n /**\n * Also upload when the oldest buffered entry has waited this long (ms). The\n * timer does not keep the process alive, so call `flush()` before exiting.\n */\n maxWaitMs?: number;\n}\n\nexport interface S3ErrorOptions {\n /**\n * Key prefix, for example `logs/app/`. A trailing slash is added if missing.\n * Defaults to `error_log/`. Use `''` for no prefix.\n */\n keyPrefix?: string;\n /**\n * Content type of the uploaded objects. Defaults to `application/json`, or\n * `application/x-ndjson` when batching.\n */\n contentType?: string;\n /**\n * Buffer entries and upload them together as one newline-delimited JSON\n * object. Off by default: one object per entry.\n */\n batch?: S3BatchOptions;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n /**\n * How long an upload may take before `log` (or `flush`) gives up waiting, in\n * milliseconds. Defaults to 10000. Pass `Infinity` to wait without limit. The\n * upload cannot be cancelled, so it may still finish afterwards; in batch mode\n * the entries stay buffered and are uploaded again by the next flush.\n */\n timeoutMs?: number;\n}\n\ninterface S3Record {\n id: string;\n code: string;\n message: string;\n cause?: unknown;\n source?: string;\n timestamp: string;\n}\n\n/** Turns `2026-01-02T03:04:05.678Z` into `2026/01/02/20260102T030405678Z`. */\nfunction datePath(date: Date): string {\n const compact: string = date.toISOString().replace(/[-:.]/g, '');\n return `${compact.slice(0, 4)}/${compact.slice(4, 6)}/${compact.slice(6, 8)}/${compact}`;\n}\n\nfunction normalizePrefix(prefix: string): string {\n const trimmed: string = prefix.replace(/^\\/+/, '');\n return trimmed === '' || trimmed.endsWith('/') ? trimmed : `${trimmed}/`;\n}\n\n/**\n * Stores entries as objects in an S3-compatible bucket, keyed by date and UUID:\n * `error_log/2026/01/02/20260102T030405678Z-<uuid>.json`.\n *\n * By default each entry becomes its own JSON object and `log` resolves once the\n * upload succeeds. With `batch`, entries are buffered and uploaded together as\n * one `.ndjson` object; `log` then resolves ok as soon as the entry is\n * buffered (it is not stored yet), except for the call that fills the batch,\n * which resolves with the upload result. Call `flush()` before shutting down.\n */\nexport class S3ErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly keyPrefix: string;\n private readonly contentType: string;\n private readonly batch: S3BatchOptions | undefined;\n private readonly logPrefix: string;\n private readonly timeoutMs: number;\n private buffer: S3Record[] = [];\n private timer: ReturnType<typeof setTimeout> | null = null;\n private readonly flushQueue: SerialQueue = new SerialQueue();\n\n /** @throws {RangeError} if `bucket` is empty or the batch options are invalid. */\n constructor(\n private readonly client: S3ClientLike,\n private readonly bucket: string,\n options: S3ErrorOptions = {}\n ) {\n this.keyPrefix = normalizePrefix(options.keyPrefix ?? DEFAULT_S3_PREFIX);\n this.batch = options.batch;\n this.contentType =\n options.contentType ??\n (this.batch === undefined ? 'application/json' : 'application/x-ndjson');\n this.logPrefix = options.prefix ?? DEFAULT_PREFIX;\n this.timeoutMs = resolveTimeoutMs(options.timeoutMs, 'S3ErrorAdapter');\n if (bucket === '') {\n throw new RangeError('S3ErrorAdapter: bucket must not be empty.');\n }\n if (this.batch !== undefined) {\n if (!Number.isInteger(this.batch.batchSize) || this.batch.batchSize < 1) {\n throw new RangeError(\n `S3ErrorAdapter: batch.batchSize must be a positive integer, received ${String(this.batch.batchSize)}.`\n );\n }\n if (\n this.batch.maxWaitMs !== undefined &&\n (!Number.isFinite(this.batch.maxWaitMs) || this.batch.maxWaitMs <= 0)\n ) {\n throw new RangeError(\n `S3ErrorAdapter: batch.maxWaitMs must be a positive number, received ${String(this.batch.maxWaitMs)}.`\n );\n }\n }\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record: S3Record = {\n id: resolveEntryId(entry),\n ...serializeEntry(entry),\n };\n const occurredAt: Date = new Date(record.timestamp);\n if (Number.isNaN(occurredAt.getTime())) {\n throw new RangeError(\n `timestamp \"${record.timestamp}\" is not a valid ISO 8601 date`\n );\n }\n if (this.batch === undefined) {\n const key: string = `${this.keyPrefix}${datePath(occurredAt)}-${record.id}.json`;\n await this.upload(key, `${JSON.stringify(record)}\\n`);\n return { ok: true };\n }\n return await this.addToBatch(record, this.batch);\n } catch (error: unknown) {\n return this.fail(error);\n }\n }\n\n /**\n * Uploads any buffered entries now. Resolves ok when nothing is buffered.\n * Entries from a failed upload stay buffered and are retried by the next flush.\n */\n flush(): Promise<ErrorLogResult> {\n return this.flushQueue.run((): Promise<ErrorLogResult> =>\n this.uploadBuffered()\n );\n }\n\n private async addToBatch(\n record: S3Record,\n batch: S3BatchOptions\n ): Promise<ErrorLogResult> {\n this.buffer.push(record);\n const limit: number = batch.batchSize * BUFFER_LIMIT_MULTIPLIER;\n if (this.buffer.length > limit) {\n const dropped: number = this.buffer.length - limit;\n this.buffer = this.buffer.slice(dropped);\n console.error(\n `${this.logPrefix} S3ErrorAdapter: dropped the ${dropped} oldest buffered entries because uploads to bucket \"${this.bucket}\" keep failing and the buffer is full (${limit} entries).`\n );\n }\n if (this.buffer.length >= batch.batchSize) {\n return this.flush();\n }\n this.scheduleFlush(batch);\n return { ok: true };\n }\n\n private scheduleFlush(batch: S3BatchOptions): void {\n if (batch.maxWaitMs === undefined || this.timer !== null) {\n return;\n }\n const timer = setTimeout((): void => {\n this.timer = null;\n void this.flush();\n }, batch.maxWaitMs);\n (timer as { unref?: () => void }).unref?.();\n this.timer = timer;\n }\n\n private async uploadBuffered(): Promise<ErrorLogResult> {\n if (this.timer !== null) {\n clearTimeout(this.timer);\n this.timer = null;\n }\n const pending: S3Record[] = this.buffer.slice();\n if (pending.length === 0) {\n return { ok: true };\n }\n try {\n const key: string = `${this.keyPrefix}${datePath(new Date())}-${generateId()}.ndjson`;\n const body: string = pending\n .map((record: S3Record): string => JSON.stringify(record))\n .join('\\n');\n await this.upload(key, `${body}\\n`);\n const uploaded: Set<S3Record> = new Set(pending);\n this.buffer = this.buffer.filter(\n (record: S3Record): boolean => !uploaded.has(record)\n );\n return { ok: true };\n } catch (error: unknown) {\n if (this.batch !== undefined) {\n this.scheduleFlush(this.batch);\n }\n return this.fail(error, pending.length);\n }\n }\n\n private upload(key: string, body: string): Promise<unknown> {\n return withTimeout(\n (): Promise<unknown> =>\n this.client.putObject({\n Bucket: this.bucket,\n Key: key,\n Body: body,\n ContentType: this.contentType,\n }),\n this.timeoutMs,\n timeoutMessage('S3', this.timeoutMs)\n );\n }\n\n private fail(error: unknown, bufferedCount?: number): ErrorLogResult {\n const retry: string =\n bufferedCount === undefined\n ? ''\n : ` (${bufferedCount} buffered ${bufferedCount === 1 ? 'entry was' : 'entries were'} kept for the next flush)`;\n return reportFailure({\n adapterName: 'S3ErrorAdapter',\n prefix: this.logPrefix,\n action: `to upload to bucket \"${this.bucket}\"`,\n error,\n reason: `${describeError(error)}${retry}`,\n });\n }\n}\n","import { DEFAULT_PREFIX, reportFailure } from '../fail.js';\nimport { hasCallerId, resolveEntryId } from '../entry-id.js';\nimport {\n DEFAULT_TIMEOUT_MS,\n resolveTimeoutMs,\n withTimeout,\n} from '../timeout.js';\nimport { serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\n\n/** The fields stored for each entry, in Firestore and in the Realtime Database. */\nexport interface FirebaseErrorRecord {\n /** UUID. In Firestore it is also the document id. */\n id: string;\n code: string;\n message: string;\n /** ISO 8601 timestamp of the entry. */\n timestamp: string;\n source?: string;\n /**\n * The entry's cause as a JSON string. Plain text avoids Firestore's limits on\n * custom objects and nested arrays, and the Realtime Database's limits on key\n * characters, so an odd cause can never make the write fail.\n */\n cause?: string;\n}\n\n/**\n * The only thing the Firestore adapter needs from a collection: `doc(id).set(data)`.\n * A `firebase-admin` collection (`getFirestore().collection('error_log')`) fits\n * as it is. With the web SDK, wrap it:\n *\n * ```ts\n * const collectionLike: FirestoreCollectionLike = {\n * doc: (id) => ({ set: (data) => setDoc(doc(db, 'error_log', id), data) }),\n * };\n * ```\n */\nexport interface FirestoreCollectionLike {\n doc(documentId: string): { set(data: FirebaseErrorRecord): Promise<unknown> };\n}\n\n/**\n * The only thing the Realtime Database adapter needs from a reference:\n * `push(value)`, which resolves once the write is saved. A `firebase-admin`\n * reference (`getDatabase().ref('error_log')`) fits as it is. With the web SDK, wrap it:\n *\n * ```ts\n * const refLike: RealtimeDatabaseRefLike = {\n * push: (value) => push(ref(db, 'error_log'), value),\n * child: (key) => ({ set: (value) => set(ref(db, `error_log/${key}`), value) }),\n * };\n * ```\n *\n * `child(key).set(value)` is only needed for entries that carry their own `id`\n * (see `ErrorLogEntry.id`): they are written under that key, so logging the same\n * entry again overwrites the record instead of adding a second one.\n */\nexport interface RealtimeDatabaseRefLike {\n push(value: FirebaseErrorRecord): PromiseLike<unknown>;\n child?(key: string): {\n set(value: FirebaseErrorRecord): PromiseLike<unknown>;\n };\n}\n\n/** Default time to wait for a write to be saved, in milliseconds. */\nexport const DEFAULT_WRITE_TIMEOUT_MS: number = DEFAULT_TIMEOUT_MS;\n\nexport interface FirebaseErrorOptions {\n /**\n * How long `log` waits for the write to be saved before giving up, in\n * milliseconds. Defaults to 10000. Pass `Infinity` to wait without limit.\n *\n * The Firebase SDKs keep writes queued while the connection is down, and\n * their promises stay pending until it returns, so without a limit `log`\n * could hang for as long as an outage lasts. A write that timed out is still\n * queued by the SDK and may be saved later. Retry a timeout only with the same\n * `ErrorLogEntry.id`, so the retry overwrites the first write instead of\n * adding a second one.\n */\n timeoutMs?: number;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n}\n\n/** Waits for the write, but rejects with a descriptive error if it takes longer than `timeoutMs`. */\nfunction waitForWrite(\n start: () => PromiseLike<unknown>,\n timeoutMs: number\n): Promise<unknown> {\n return withTimeout(\n start,\n timeoutMs,\n `the write was not saved within ${timeoutMs} ms (the connection may be down; the SDK keeps the write queued and may still save it later, so logging the entry again could store it twice unless it carries an id)`\n );\n}\n\nfunction buildRecord<TCode extends string>(\n entry: ErrorLogEntry<TCode>\n): FirebaseErrorRecord {\n const serialized = serializeEntry(entry);\n const record: FirebaseErrorRecord = {\n id: resolveEntryId(entry),\n code: serialized.code,\n message: serialized.message,\n timestamp: serialized.timestamp,\n };\n if (serialized.source !== undefined) {\n record.source = serialized.source;\n }\n if (serialized.cause !== undefined) {\n record.cause = JSON.stringify(serialized.cause);\n }\n return record;\n}\n\n/**\n * Writes each entry as a document in a Firestore collection. The document id is\n * the entry's UUID, so the SDK finishing one queued write twice cannot create two\n * documents. Calling `log` again without an `id` on the entry is a new write\n * with a new UUID: if a call reported a timeout, the first write may still be\n * saved later, and logging the same entry again can store it twice. Give the\n * entry an `id` and the retry overwrites the same document.\n */\nexport class FirestoreErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly prefix: string;\n private readonly timeoutMs: number;\n\n /** @throws {RangeError} if `options.timeoutMs` is not a positive number or `Infinity`. */\n constructor(\n private readonly collection: FirestoreCollectionLike,\n options: FirebaseErrorOptions = {}\n ) {\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.timeoutMs = resolveTimeoutMs(\n options.timeoutMs,\n 'FirestoreErrorAdapter'\n );\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record: FirebaseErrorRecord = buildRecord(entry);\n await waitForWrite(\n () => this.collection.doc(record.id).set(record),\n this.timeoutMs\n );\n return { ok: true };\n } catch (error: unknown) {\n return reportFailure({\n adapterName: 'FirestoreErrorAdapter',\n prefix: this.prefix,\n action: `to write the document`,\n error,\n });\n }\n }\n}\n\n/**\n * Writes each entry under a Realtime Database reference with `push`, which\n * generates a chronologically ordered key.\n */\nexport class RealtimeDatabaseErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly prefix: string;\n private readonly timeoutMs: number;\n\n /** @throws {RangeError} if `options.timeoutMs` is not a positive number or `Infinity`. */\n constructor(\n private readonly reference: RealtimeDatabaseRefLike,\n options: FirebaseErrorOptions = {}\n ) {\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.timeoutMs = resolveTimeoutMs(\n options.timeoutMs,\n 'RealtimeDatabaseErrorAdapter'\n );\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const record: FirebaseErrorRecord = buildRecord(entry);\n let write: PromiseLike<unknown>;\n if (hasCallerId(entry)) {\n if (this.reference.child === undefined) {\n throw new TypeError(\n 'the entry has an id, which needs a reference with child(key).set(value) so a repeat overwrites the first write; add child to the reference or log without an id'\n );\n }\n write = this.reference.child(record.id).set(record);\n } else {\n write = this.reference.push(record);\n }\n await waitForWrite(() => write, this.timeoutMs);\n return { ok: true };\n } catch (error: unknown) {\n return reportFailure({\n adapterName: 'RealtimeDatabaseErrorAdapter',\n prefix: this.prefix,\n action: `to write the entry`,\n error,\n });\n }\n }\n}\n","import { DEFAULT_PREFIX, reportFailure } from '../fail.js';\nimport { DEFAULT_TABLE_NAME, assertIdentifier } from '../sql-ident.js';\nimport { hasCallerId, resolveEntryId } from '../entry-id.js';\nimport { resolveTimeoutMs, timeoutMessage, withTimeout } from '../timeout.js';\nimport { describeError, serializeEntry } from '../serialize.js';\nimport type {\n ErrorLogAdapter,\n ErrorLogEntry,\n ErrorLogResult,\n} from '../types.js';\nimport { buildErrorLogTableDdl } from './sql-error-adapter.js';\n\n/** The row inserted for each entry. The columns match the Postgres adapter's table. */\nexport interface SupabaseErrorRow {\n /** UUID primary key. */\n id: string;\n code: string;\n message: string;\n /** The cause as JSON, or `null` when the entry has none. */\n cause: unknown;\n source: string | null;\n /** ISO 8601 timestamp of the entry. */\n occurred_at: string;\n}\n\n/** The error object `supabase-js` returns (a `PostgrestError`) instead of throwing. */\nexport interface SupabaseErrorLike {\n message: string;\n code?: string;\n details?: string | null;\n hint?: string | null;\n}\n\n/** What `insert` resolves to. The status fields are used when the error has no message. */\nexport interface SupabaseInsertResult {\n error: SupabaseErrorLike | null;\n status?: number;\n statusText?: string;\n}\n\n/**\n * The only thing the Supabase adapter needs from a client: `from(table).insert(row)`.\n * A `supabase-js` client (`createClient(url, key)`) fits as it is.\n */\nexport interface SupabaseClientLike {\n from(table: string): {\n insert(row: SupabaseErrorRow): PromiseLike<SupabaseInsertResult>;\n };\n}\n\nexport interface SupabaseErrorOptions {\n /** Table to write to. Defaults to `error_log`. Letters, digits and `_` only. */\n tableName?: string;\n /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */\n prefix?: string;\n /**\n * How long `log` waits for Supabase to answer before giving up, in milliseconds. Defaults\n * to 10000. Pass `Infinity` to wait without limit. The call cannot be\n * cancelled, so it may still finish after `log` has returned a failure.\n */\n timeoutMs?: number;\n}\n\n/** Longest `code` or `source` an insert policy accepts, in characters. */\nexport const POLICY_MAX_CODE_LENGTH = 255;\n/** Longest `message` an insert policy accepts, in characters. */\nexport const POLICY_MAX_MESSAGE_LENGTH = 4096;\n/** Largest `cause` an insert policy accepts, in bytes of stored JSON. */\nexport const POLICY_MAX_CAUSE_BYTES = 8192;\n\nexport interface SupabaseSetupOptions {\n /**\n * Postgres roles allowed to insert through the API, for example\n * `['authenticated']`. Leave empty when only a server using the service role\n * key logs: that role bypasses row level security, so it needs no policy, and\n * its key must never be shipped to a browser.\n *\n * The generated policy only checks the size of each field. It does not tie a\n * row to a user, so any holder of an allowed role's key can still add entries\n * of their own making: forged entries can hide real incidents, and a flood of\n * them uses up your database quota. Prefer `authenticated` over `anon`.\n */\n insertRoles?: string[];\n /**\n * Required to allow `anon` (or `public`) to insert. The `anon` key is part of\n * your frontend, so this lets anyone who loads the page write to the log.\n * Defaults to `false`.\n */\n allowPublicInsert?: boolean;\n}\n\n/** The `WITH CHECK` condition of the insert policy: every field must be a reasonable size. */\nconst INSERT_POLICY_CHECK: string =\n `char_length(code) <= ${POLICY_MAX_CODE_LENGTH} ` +\n `AND char_length(message) <= ${POLICY_MAX_MESSAGE_LENGTH} ` +\n `AND (source IS NULL OR char_length(source) <= ${POLICY_MAX_CODE_LENGTH}) ` +\n `AND (cause IS NULL OR pg_column_size(cause) <= ${POLICY_MAX_CAUSE_BYTES})`;\n\n/**\n * Returns the SQL to run once in the Supabase SQL editor: the error log table,\n * with row level security turned on (as Supabase recommends for every table in\n * an exposed schema) and, optionally, an insert-only policy for the given roles.\n * The policy limits the size of each field. Nobody but the service role can read\n * the log through the API.\n *\n * @throws {RangeError} if the table name or a role is not a plain identifier, or\n * `anon` / `public` is listed without `allowPublicInsert: true`.\n */\nexport function buildSupabaseSetupSql(\n tableName: string = DEFAULT_TABLE_NAME,\n options: SupabaseSetupOptions = {}\n): string[] {\n const insertRoles: string[] = options.insertRoles ?? [];\n for (const role of insertRoles) {\n assertIdentifier(role, 'role name', 'buildSupabaseSetupSql');\n if (\n (role.toLowerCase() === 'anon' || role.toLowerCase() === 'public') &&\n options.allowPublicInsert !== true\n ) {\n throw new RangeError(\n `buildSupabaseSetupSql: role \"${role}\" would let anyone who loads your page write to the log, because the anon key is public. Use \"authenticated\" instead, or pass allowPublicInsert: true to accept the risk of forged entries and quota use.`\n );\n }\n }\n const statements: string[] = [\n ...buildErrorLogTableDdl('postgres', tableName),\n `ALTER TABLE \"${tableName}\" ENABLE ROW LEVEL SECURITY`,\n ];\n if (insertRoles.length > 0) {\n const policy: string = `${tableName}_insert_only`;\n statements.push(\n `DROP POLICY IF EXISTS \"${policy}\" ON \"${tableName}\"`,\n `CREATE POLICY \"${policy}\" ON \"${tableName}\" FOR INSERT TO ${insertRoles.map((role: string): string => `\"${role}\"`).join(', ')} WITH CHECK (${INSERT_POLICY_CHECK})`\n );\n }\n return statements;\n}\n\n/**\n * Joins the parts of a Supabase error that are present into one message.\n * `supabase-js` can return an error with no message at all (an empty-bodied\n * 404 for a table the API cannot see, for example), so the HTTP status stands\n * in for it. Only the first line of `details` is kept, since network failures\n * put a full stack trace there.\n */\nfunction describeSupabaseError(result: SupabaseInsertResult): string {\n const error: SupabaseErrorLike = result.error ?? { message: '' };\n const parts: string[] = [];\n if (error.message) {\n parts.push(error.message);\n } else if (result.status) {\n parts.push(\n `HTTP ${result.status}${result.statusText ? ` ${result.statusText}` : ''} with no error message (a 404 usually means the table does not exist or is not exposed by the API)`\n );\n } else {\n parts.push('unknown error with no message');\n }\n if (error.code) {\n parts.push(`code ${error.code}`);\n }\n const detailsLine: string = (error.details ?? '').split('\\n')[0].trim();\n if (detailsLine && detailsLine !== error.message) {\n parts.push(`details: ${detailsLine}`);\n }\n if (error.hint) {\n parts.push(`hint: ${error.hint}`);\n }\n return parts.join('; ');\n}\n\n/**\n * Writes entries to a Supabase table through `supabase-js`. Create the table\n * first with `buildSupabaseSetupSql` (or `buildErrorLogTableDdl('postgres')`).\n *\n * ```ts\n * const adapter = new SupabaseErrorAdapter(createClient(url, serviceRoleKey));\n * ```\n *\n * `supabase-js` reports failures in a returned `error` field rather than\n * throwing; the adapter turns both that and a thrown error into `{ ok: false }`.\n */\nexport class SupabaseErrorAdapter<\n TCode extends string = string,\n> implements ErrorLogAdapter<TCode> {\n private readonly tableName: string;\n private readonly prefix: string;\n private readonly timeoutMs: number;\n\n /** @throws {RangeError} if `options.tableName` is not a plain identifier or `options.timeoutMs` is invalid. */\n constructor(\n private readonly client: SupabaseClientLike,\n options: SupabaseErrorOptions = {}\n ) {\n this.tableName = options.tableName ?? DEFAULT_TABLE_NAME;\n assertIdentifier(this.tableName, 'table name', 'SupabaseErrorAdapter');\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.timeoutMs = resolveTimeoutMs(\n options.timeoutMs,\n 'SupabaseErrorAdapter'\n );\n }\n\n async log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult> {\n try {\n const serialized = serializeEntry(entry);\n const row: SupabaseErrorRow = {\n id: resolveEntryId(entry),\n code: serialized.code,\n message: serialized.message,\n cause: serialized.cause === undefined ? null : serialized.cause,\n source: serialized.source ?? null,\n occurred_at: serialized.timestamp,\n };\n const result: SupabaseInsertResult = await withTimeout(\n (): PromiseLike<SupabaseInsertResult> =>\n this.client.from(this.tableName).insert(row),\n this.timeoutMs,\n timeoutMessage('Supabase', this.timeoutMs)\n );\n if (result.error) {\n if (hasCallerId(entry) && result.error.code === '23505') {\n // Unique violation on the id: this entry was stored by an earlier call.\n return { ok: true };\n }\n return this.fail(result, describeSupabaseError(result));\n }\n return { ok: true };\n } catch (error: unknown) {\n return this.fail(error, describeError(error));\n }\n }\n\n private fail(logged: unknown, reason: string): ErrorLogResult {\n return reportFailure({\n adapterName: 'SupabaseErrorAdapter',\n prefix: this.prefix,\n action: `to insert into table \"${this.tableName}\"`,\n error: logged,\n reason,\n });\n }\n}\n"],"mappings":";AAYO,IAAM,kBAAkB;AAE/B,SAAS,YAAY,OAMnB;AACA,MAAI,iBAAiB,OAAO;AAC1B,WAAO;AAAA,EACT;AAEA,QAAM,YAAY;AAClB,SACE,OAAO,UAAU,SAAS,YAC1B,OAAO,UAAU,YAAY,YAC7B,OAAQ,MA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,OAAsD;AAC9D,QAAI;AACF,YAAM,aAAa,eAAe,KAAK;AACvC,YAAM,MAAwB;AAAA,QAC5B,IAAI,eAAe,KAAK;AAAA,QACxB,MAAM,WAAW;AAAA,QACjB,SAAS,WAAW;AAAA,QACpB,OAAO,WAAW,UAAU,SAAY,OAAO,WAAW;AAAA,QAC1D,QAAQ,WAAW,UAAU;AAAA,QAC7B,aAAa,WAAW;AAAA,MAC1B;AACA,YAAM,SAA+B,MAAM;AAAA,QACzC,MACE,KAAK,OAAO,KAAK,KAAK,SAAS,EAAE,OAAO,GAAG;AAAA,QAC7C,KAAK;AAAA,QACL,eAAe,YAAY,KAAK,SAAS;AAAA,MAC3C;AACA,UAAI,OAAO,OAAO;AAChB,YAAI,YAAY,KAAK,KAAK,OAAO,MAAM,SAAS,SAAS;AAEvD,iBAAO,EAAE,IAAI,KAAK;AAAA,QACpB;AACA,eAAO,KAAK,KAAK,QAAQ,sBAAsB,MAAM,CAAC;AAAA,MACxD;AACA,aAAO,EAAE,IAAI,KAAK;AAAA,IACpB,SAAS,OAAgB;AACvB,aAAO,KAAK,KAAK,OAAO,cAAc,KAAK,CAAC;AAAA,IAC9C;AAAA,EACF;AAAA,EAEQ,KAAK,QAAiB,QAAgC;AAC5D,WAAO,cAAc;AAAA,MACnB,aAAa;AAAA,MACb,QAAQ,KAAK;AAAA,MACb,QAAQ,yBAAyB,KAAK,SAAS;AAAA,MAC/C,OAAO;AAAA,MACP;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":["result","assertValidTableName"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@richardmcquiston01/error-log-adapter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Dependency-free TypeScript error logging with pluggable adapters for console, webhook, MongoDB, Postgres, MySQL, SQLite, Redis, S3, IndexedDB, Web Storage, and file sinks.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Richard McQuiston <richard.mcquiston01@gmail.com>",
|
|
@@ -50,11 +50,13 @@
|
|
|
50
50
|
"build": "tsup",
|
|
51
51
|
"prepublishOnly": "bun run build",
|
|
52
52
|
"test": "vitest run",
|
|
53
|
+
"test:live": "vitest run s3-error-adapter.live",
|
|
53
54
|
"typecheck": "tsc --noEmit",
|
|
54
55
|
"lint": "prettier --check .",
|
|
55
56
|
"format": "prettier --write ."
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
59
|
+
"@aws-sdk/client-s3": "^3.1152.0",
|
|
58
60
|
"@types/node": "^20.0.0",
|
|
59
61
|
"prettier": "^3.3.0",
|
|
60
62
|
"tsup": "^8.5.1",
|