@richardmcquiston01/error-log-adapter 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/README.md +156 -2
- package/dist/index.cjs +687 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +358 -0
- package/dist/index.d.ts +358 -0
- package/dist/index.mjs +647 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +58 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
/** A single error to be routed to a sink. */
|
|
2
|
+
interface ErrorLogEntry<TCode extends string = string> {
|
|
3
|
+
/** Machine-readable error code, e.g. `ADAPTER_ERROR`. */
|
|
4
|
+
code: TCode;
|
|
5
|
+
/** Human-readable description of what went wrong. */
|
|
6
|
+
message: string;
|
|
7
|
+
/** The underlying error or value that caused this entry. */
|
|
8
|
+
cause?: unknown;
|
|
9
|
+
/** Name of the application or module that raised the error. */
|
|
10
|
+
source?: string;
|
|
11
|
+
/** ISO 8601 timestamp. Adapters fill this in when it is omitted. */
|
|
12
|
+
timestamp?: string;
|
|
13
|
+
}
|
|
14
|
+
/** Outcome of a `log` call. Callers should check `ok`. */
|
|
15
|
+
interface ErrorLogResult {
|
|
16
|
+
ok: boolean;
|
|
17
|
+
/** Description of the failure when `ok` is false. */
|
|
18
|
+
error?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A sink for runtime errors. Implementations must never throw or reject:
|
|
22
|
+
* any failure is reported through the returned `ErrorLogResult`.
|
|
23
|
+
*/
|
|
24
|
+
interface ErrorLogAdapter<TCode extends string = string> {
|
|
25
|
+
log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A JSON-safe copy of an `ErrorLogEntry`. */
|
|
29
|
+
interface SerializedErrorLogEntry {
|
|
30
|
+
code: string;
|
|
31
|
+
message: string;
|
|
32
|
+
cause?: unknown;
|
|
33
|
+
source?: string;
|
|
34
|
+
timestamp: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Converts `Error` instances into plain objects, since `JSON.stringify`
|
|
38
|
+
* renders them as `{}`. Other values are returned unchanged.
|
|
39
|
+
*/
|
|
40
|
+
declare function serializeCause(cause: unknown): unknown;
|
|
41
|
+
/** Returns a JSON-safe copy of the entry, adding a timestamp if missing. */
|
|
42
|
+
declare function serializeEntry(entry: ErrorLogEntry): SerializedErrorLogEntry;
|
|
43
|
+
|
|
44
|
+
interface ConsoleErrorOptions {
|
|
45
|
+
/** Text printed before each entry. Defaults to `[error-log-adapter]`. */
|
|
46
|
+
prefix?: string;
|
|
47
|
+
}
|
|
48
|
+
declare class ConsoleErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
|
|
49
|
+
private readonly prefix;
|
|
50
|
+
constructor(options?: ConsoleErrorOptions);
|
|
51
|
+
log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface WebhookErrorOptions {
|
|
55
|
+
/** Extra headers merged into the request. */
|
|
56
|
+
headers?: Record<string, string>;
|
|
57
|
+
/** Text printed before failure messages. Defaults to `[error-log-adapter]`. */
|
|
58
|
+
prefix?: string;
|
|
59
|
+
}
|
|
60
|
+
declare class WebhookErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
|
|
61
|
+
private readonly url;
|
|
62
|
+
private readonly options;
|
|
63
|
+
private readonly prefix;
|
|
64
|
+
constructor(url: string, options?: WebhookErrorOptions);
|
|
65
|
+
log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
|
|
66
|
+
private fail;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The subset of a MongoDB `Collection` this adapter uses. */
|
|
70
|
+
interface CollectionLike {
|
|
71
|
+
insertOne(doc: object): Promise<unknown>;
|
|
72
|
+
}
|
|
73
|
+
interface MongoDbErrorOptions {
|
|
74
|
+
/** Text printed before failure messages. Defaults to `[error-log-adapter]`. */
|
|
75
|
+
prefix?: string;
|
|
76
|
+
}
|
|
77
|
+
declare class MongoDbErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
|
|
78
|
+
private readonly collection;
|
|
79
|
+
private readonly prefix;
|
|
80
|
+
constructor(collection: CollectionLike, options?: MongoDbErrorOptions);
|
|
81
|
+
log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Sends every entry to several adapters. Succeeds only if all of them do. */
|
|
85
|
+
declare class CompositeErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
|
|
86
|
+
private readonly adapters;
|
|
87
|
+
constructor(adapters: ReadonlyArray<ErrorLogAdapter<TCode>>);
|
|
88
|
+
log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Event handler slot. Typed loosely so real DOM `IDBTransaction` objects fit. */
|
|
92
|
+
type IdbHandler = ((event: never) => unknown) | null;
|
|
93
|
+
/** The subset of an `IDBObjectStore` this adapter uses. */
|
|
94
|
+
interface IdbObjectStoreLike {
|
|
95
|
+
add(value: unknown): unknown;
|
|
96
|
+
}
|
|
97
|
+
/** The subset of an `IDBTransaction` this adapter uses. */
|
|
98
|
+
interface IdbTransactionLike {
|
|
99
|
+
objectStore(name: string): IdbObjectStoreLike;
|
|
100
|
+
oncomplete: IdbHandler;
|
|
101
|
+
onerror: IdbHandler;
|
|
102
|
+
onabort: IdbHandler;
|
|
103
|
+
readonly error?: {
|
|
104
|
+
message?: string;
|
|
105
|
+
} | null;
|
|
106
|
+
}
|
|
107
|
+
/** The subset of an `IDBDatabase` this adapter uses. */
|
|
108
|
+
interface IdbDatabaseLike {
|
|
109
|
+
transaction(storeNames: string, mode: 'readwrite'): IdbTransactionLike;
|
|
110
|
+
}
|
|
111
|
+
/** The subset of an `IDBDatabase` available inside `onupgradeneeded`. */
|
|
112
|
+
interface IdbUpgradeDatabaseLike {
|
|
113
|
+
readonly objectStoreNames: {
|
|
114
|
+
contains(name: string): boolean;
|
|
115
|
+
};
|
|
116
|
+
createObjectStore(name: string, options: {
|
|
117
|
+
keyPath: string;
|
|
118
|
+
}): {
|
|
119
|
+
createIndex(name: string, keyPath: string): unknown;
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** A stored record: the serialized entry plus its generated UUID key. */
|
|
123
|
+
interface IndexedDbErrorRecord extends SerializedErrorLogEntry {
|
|
124
|
+
id: string;
|
|
125
|
+
}
|
|
126
|
+
interface IndexedDbErrorOptions {
|
|
127
|
+
/** Object store to write to. Defaults to `error_log`. */
|
|
128
|
+
storeName?: string;
|
|
129
|
+
/** Text printed before failure messages. Defaults to `[error-log-adapter]`. */
|
|
130
|
+
prefix?: string;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Creates the error log object store (UUID `id` key, `timestamp` index).
|
|
134
|
+
* Call it from your `onupgradeneeded` handler. Does nothing if it already exists.
|
|
135
|
+
*/
|
|
136
|
+
declare function createErrorLogStore(database: IdbUpgradeDatabaseLike, storeName?: string): void;
|
|
137
|
+
declare class IndexedDbErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
|
|
138
|
+
private readonly database;
|
|
139
|
+
private readonly storeName;
|
|
140
|
+
private readonly prefix;
|
|
141
|
+
constructor(database: IdbDatabaseLike, options?: IndexedDbErrorOptions);
|
|
142
|
+
log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
|
|
143
|
+
/** Resolves once the transaction commits; rejects if it errors or aborts. */
|
|
144
|
+
private write;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The only thing this adapter needs from a SQLite driver: run one statement
|
|
149
|
+
* with positional (`?`) parameters. `bun:sqlite` already matches this shape.
|
|
150
|
+
* Other drivers need a one-line wrapper, for example:
|
|
151
|
+
*
|
|
152
|
+
* ```ts
|
|
153
|
+
* // better-sqlite3 or node:sqlite
|
|
154
|
+
* const client: SqliteClientLike = {
|
|
155
|
+
* run: (sql, params) => db.prepare(sql).run(...params),
|
|
156
|
+
* };
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
interface SqliteClientLike {
|
|
160
|
+
run(sql: string, params: unknown[]): unknown;
|
|
161
|
+
}
|
|
162
|
+
interface SqliteErrorOptions {
|
|
163
|
+
/** Table to write to. Defaults to `error_log`. Letters, digits and `_` only. */
|
|
164
|
+
tableName?: string;
|
|
165
|
+
/** Create the table (and index) on first use. Defaults to `true`. */
|
|
166
|
+
createTable?: boolean;
|
|
167
|
+
/** Text printed before failure messages. Defaults to `[error-log-adapter]`. */
|
|
168
|
+
prefix?: string;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Returns the statements that create the error log table and its index.
|
|
172
|
+
* Use this to run your own migration when `createTable` is disabled.
|
|
173
|
+
*/
|
|
174
|
+
declare function buildCreateErrorLogTableSql(tableName?: string): string[];
|
|
175
|
+
declare class SqliteErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
|
|
176
|
+
private readonly client;
|
|
177
|
+
private readonly tableName;
|
|
178
|
+
private readonly createTable;
|
|
179
|
+
private readonly prefix;
|
|
180
|
+
private tableReady;
|
|
181
|
+
/** @throws {RangeError} if `options.tableName` is not a plain identifier. */
|
|
182
|
+
constructor(client: SqliteClientLike, options?: SqliteErrorOptions);
|
|
183
|
+
log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
|
|
184
|
+
/** Creates the table once. A failed attempt is retried on the next log call. */
|
|
185
|
+
private ensureTable;
|
|
186
|
+
private runCreateStatements;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The subset of the Web Storage API this adapter uses. Both `sessionStorage`
|
|
191
|
+
* and `localStorage` fit.
|
|
192
|
+
*/
|
|
193
|
+
interface StorageLike {
|
|
194
|
+
getItem(key: string): string | null;
|
|
195
|
+
setItem(key: string, value: string): void;
|
|
196
|
+
}
|
|
197
|
+
/** A stored record: the serialized entry plus its generated UUID. */
|
|
198
|
+
interface WebStorageErrorRecord extends SerializedErrorLogEntry {
|
|
199
|
+
id: string;
|
|
200
|
+
}
|
|
201
|
+
interface WebStorageErrorOptions {
|
|
202
|
+
/** Key the entries are stored under, as one JSON array. Defaults to `error_log`. */
|
|
203
|
+
key?: string;
|
|
204
|
+
/** Most entries to keep. The oldest are dropped first. Defaults to `100`. */
|
|
205
|
+
maxEntries?: number;
|
|
206
|
+
/** Text printed before failure messages. Defaults to `[error-log-adapter]`. */
|
|
207
|
+
prefix?: string;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Keeps the newest entries in a Web Storage object (`sessionStorage` or
|
|
211
|
+
* `localStorage`) as a JSON array under one key, capped at `maxEntries`.
|
|
212
|
+
* When the store is full, the oldest entries are dropped until the new one fits.
|
|
213
|
+
*/
|
|
214
|
+
declare class WebStorageErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
|
|
215
|
+
private readonly storage;
|
|
216
|
+
private readonly key;
|
|
217
|
+
private readonly maxEntries;
|
|
218
|
+
private readonly prefix;
|
|
219
|
+
/** @throws {RangeError} if `options.maxEntries` is not a positive integer. */
|
|
220
|
+
constructor(storage: StorageLike, options?: WebStorageErrorOptions);
|
|
221
|
+
log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
|
|
222
|
+
/** Reads, appends, trims, and writes back. Runs without awaiting, so calls never interleave. */
|
|
223
|
+
private append;
|
|
224
|
+
/** Returns the stored records, or none if the value is missing or unreadable. */
|
|
225
|
+
private readRecords;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* The subset of `node:fs/promises` the file adapters use. Pass `fs.promises`
|
|
230
|
+
* (or `node:fs/promises`) yourself, or leave `fs` unset in Node and Bun.
|
|
231
|
+
*/
|
|
232
|
+
interface FileSystemLike {
|
|
233
|
+
appendFile(path: string, data: string): Promise<unknown>;
|
|
234
|
+
mkdir(path: string, options: {
|
|
235
|
+
recursive: true;
|
|
236
|
+
}): Promise<unknown>;
|
|
237
|
+
stat(path: string): Promise<{
|
|
238
|
+
size: number;
|
|
239
|
+
}>;
|
|
240
|
+
rename(from: string, to: string): Promise<unknown>;
|
|
241
|
+
rm(path: string, options: {
|
|
242
|
+
force: true;
|
|
243
|
+
}): Promise<unknown>;
|
|
244
|
+
}
|
|
245
|
+
/** `FileSystemLike` plus the reading and replacing that whole-file writes need. */
|
|
246
|
+
interface ReadWriteFileSystemLike extends FileSystemLike {
|
|
247
|
+
readFile(path: string, encoding: 'utf8'): Promise<string>;
|
|
248
|
+
writeFile(path: string, data: string): Promise<unknown>;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
interface FlatFileErrorOptions {
|
|
252
|
+
/** File system to write to. Defaults to `node:fs/promises`, loaded on first use. */
|
|
253
|
+
fs?: FileSystemLike;
|
|
254
|
+
/** Turns an entry into one line of text. Defaults to `formatErrorLogLine`. */
|
|
255
|
+
formatLine?: (entry: SerializedErrorLogEntry) => string;
|
|
256
|
+
/** Rotate the file before it would grow past this many bytes. Off by default. */
|
|
257
|
+
maxBytes?: number;
|
|
258
|
+
/** Rotated files to keep (`app.log.1` is newest). Defaults to `3`. Needs `maxBytes`. */
|
|
259
|
+
maxFiles?: number;
|
|
260
|
+
/** Create missing parent directories on first use. Defaults to `true`. */
|
|
261
|
+
createDirectory?: boolean;
|
|
262
|
+
/** Text printed before failure messages. Defaults to `[error-log-adapter]`. */
|
|
263
|
+
prefix?: string;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* The default line format:
|
|
267
|
+
* `2026-01-02T03:04:05.000Z [CODE] source: message (cause: Error: details)`.
|
|
268
|
+
* `source` and `cause` are left out when the entry has none.
|
|
269
|
+
*/
|
|
270
|
+
declare function formatErrorLogLine(entry: SerializedErrorLogEntry): string;
|
|
271
|
+
/**
|
|
272
|
+
* Appends one text line per entry to a file, optionally rotating it by size.
|
|
273
|
+
* Writes are queued, so concurrent `log` calls never interleave or race a rotation.
|
|
274
|
+
*/
|
|
275
|
+
declare class FlatFileErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
|
|
276
|
+
private readonly filePath;
|
|
277
|
+
private readonly formatLine;
|
|
278
|
+
private readonly maxBytes;
|
|
279
|
+
private readonly maxFiles;
|
|
280
|
+
private readonly createDirectory;
|
|
281
|
+
private readonly prefix;
|
|
282
|
+
private fs;
|
|
283
|
+
private directoryReady;
|
|
284
|
+
private queue;
|
|
285
|
+
/** @throws {RangeError} if `maxBytes` or `maxFiles` is not a positive integer. */
|
|
286
|
+
constructor(filePath: string, options?: FlatFileErrorOptions);
|
|
287
|
+
log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
|
|
288
|
+
private write;
|
|
289
|
+
/** Uses the injected file system, or loads `node:fs/promises` on first use. */
|
|
290
|
+
private getFileSystem;
|
|
291
|
+
private prepareDirectory;
|
|
292
|
+
/**
|
|
293
|
+
* Moves `app.log` to `app.log.1` (shifting older files up, dropping the
|
|
294
|
+
* oldest) when the next line would push it past `maxBytes`. A failed rotation
|
|
295
|
+
* is reported, but the entry is still written so it is not lost.
|
|
296
|
+
*/
|
|
297
|
+
private rotateIfNeeded;
|
|
298
|
+
private renameIfPresent;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** A stored record: the serialized entry plus its generated UUID. */
|
|
302
|
+
interface JsonFileErrorRecord extends SerializedErrorLogEntry {
|
|
303
|
+
id: string;
|
|
304
|
+
}
|
|
305
|
+
interface JsonFileErrorOptions {
|
|
306
|
+
/**
|
|
307
|
+
* `ndjson` (default) appends one compact JSON object per line, which is cheap
|
|
308
|
+
* and safe to tail. `array` keeps the whole file as one JSON array that is
|
|
309
|
+
* rewritten on every entry.
|
|
310
|
+
*/
|
|
311
|
+
mode?: 'ndjson' | 'array';
|
|
312
|
+
/** Array mode only: most entries to keep. The oldest are dropped first. */
|
|
313
|
+
maxEntries?: number;
|
|
314
|
+
/** Array mode only: spaces of indentation. Defaults to `2`; `0` is compact. */
|
|
315
|
+
indent?: number;
|
|
316
|
+
/** File system to write to. Defaults to `node:fs/promises`, loaded on first use. */
|
|
317
|
+
fs?: ReadWriteFileSystemLike;
|
|
318
|
+
/** Create missing parent directories on first use. Defaults to `true`. */
|
|
319
|
+
createDirectory?: boolean;
|
|
320
|
+
/** Text printed before failure messages. Defaults to `[error-log-adapter]`. */
|
|
321
|
+
prefix?: string;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Stores entries as JSON in a file (Node and Bun), either as newline-delimited
|
|
325
|
+
* JSON or as one array. Writes are queued, so concurrent `log` calls stay in
|
|
326
|
+
* order. Only one process should write to a given file.
|
|
327
|
+
*/
|
|
328
|
+
declare class JsonFileErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
|
|
329
|
+
private readonly filePath;
|
|
330
|
+
private readonly mode;
|
|
331
|
+
private readonly maxEntries;
|
|
332
|
+
private readonly indent;
|
|
333
|
+
private readonly createDirectory;
|
|
334
|
+
private readonly prefix;
|
|
335
|
+
private fs;
|
|
336
|
+
private directoryReady;
|
|
337
|
+
private queue;
|
|
338
|
+
/** @throws {RangeError} if `maxEntries` or `indent` is invalid, or `maxEntries` is used outside array mode. */
|
|
339
|
+
constructor(filePath: string, options?: JsonFileErrorOptions);
|
|
340
|
+
log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
|
|
341
|
+
private write;
|
|
342
|
+
private getFileSystem;
|
|
343
|
+
private prepareDirectory;
|
|
344
|
+
/**
|
|
345
|
+
* Adds the record to the array and replaces the file. The new content is
|
|
346
|
+
* written to a temporary file first and renamed into place, so a crash
|
|
347
|
+
* mid-write cannot leave a half-written file.
|
|
348
|
+
*/
|
|
349
|
+
private rewriteArray;
|
|
350
|
+
/**
|
|
351
|
+
* Returns the records already in the file. A missing or empty file has none.
|
|
352
|
+
* A file that is not a JSON array is moved to `<path>.corrupt` (so nothing is
|
|
353
|
+
* lost) and logging continues with a fresh file.
|
|
354
|
+
*/
|
|
355
|
+
private readRecords;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export { type CollectionLike, CompositeErrorAdapter, ConsoleErrorAdapter, type ConsoleErrorOptions, type ErrorLogAdapter, type ErrorLogEntry, type ErrorLogResult, type FileSystemLike, FlatFileErrorAdapter, type FlatFileErrorOptions, type IdbDatabaseLike, type IdbObjectStoreLike, type IdbTransactionLike, type IdbUpgradeDatabaseLike, IndexedDbErrorAdapter, type IndexedDbErrorOptions, type IndexedDbErrorRecord, JsonFileErrorAdapter, type JsonFileErrorOptions, type JsonFileErrorRecord, MongoDbErrorAdapter, type MongoDbErrorOptions, type ReadWriteFileSystemLike, type SerializedErrorLogEntry, type SqliteClientLike, SqliteErrorAdapter, type SqliteErrorOptions, type StorageLike, WebStorageErrorAdapter, type WebStorageErrorOptions, type WebStorageErrorRecord, WebhookErrorAdapter, type WebhookErrorOptions, buildCreateErrorLogTableSql, createErrorLogStore, formatErrorLogLine, serializeCause, serializeEntry };
|