@richardmcquiston01/error-log-adapter 0.7.0 → 0.9.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/dist/index.d.cts CHANGED
@@ -127,6 +127,15 @@ declare class WebhookErrorAdapter<TCode extends string = string> implements Erro
127
127
  /** @throws {RangeError} if `url` is not a valid `https://` URL (see `allowInsecure`) or a timeout, retry or signing option is invalid. */
128
128
  constructor(url: string, options?: WebhookErrorOptions);
129
129
  log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
130
+ /**
131
+ * Sends several entries in one request, as a JSON array of the same records
132
+ * `log` sends one at a time (each with an `id`). `BatchingErrorAdapter` uses
133
+ * this when it wraps a webhook, so the receiver must accept an array. The
134
+ * whole request succeeds or fails together.
135
+ */
136
+ logBatch(entries: ErrorLogEntry<TCode>[]): Promise<ErrorLogResult>;
137
+ /** Sends one prepared body, trying again as `retries` allows. */
138
+ private deliver;
130
139
  /** Milliseconds to wait before retry number `retryNumber` (1 for the first). */
131
140
  private retryWaitMs;
132
141
  private send;
@@ -1059,4 +1068,239 @@ declare class WebSocketErrorAdapter<TCode extends string = string> implements Er
1059
1068
  log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
1060
1069
  }
1061
1070
 
1062
- export { type CollectionLike, CompositeErrorAdapter, type CompositeErrorOptions, ConsoleErrorAdapter, type ConsoleErrorOptions, type EmitterLike, type ErrorLogAdapter, type ErrorLogEntry, type ErrorLogResult, EventEmitterErrorAdapter, type EventEmitterErrorOptions, type EventEmitterErrorRecord, type FileSystemLike, type FirebaseErrorOptions, type FirebaseErrorRecord, type FirestoreCollectionLike, FirestoreErrorAdapter, FlatFileErrorAdapter, type FlatFileErrorOptions, type IdbDatabaseLike, type IdbObjectStoreLike, type IdbRequestLike, type IdbTransactionLike, type IdbUpgradeDatabaseLike, IndexedDbErrorAdapter, type IndexedDbErrorOptions, type IndexedDbErrorRecord, JsonFileErrorAdapter, type JsonFileErrorOptions, type JsonFileErrorRecord, type LockManagerLike, MongoDbErrorAdapter, type MongoDbErrorOptions, MySqlErrorAdapter, PostgresErrorAdapter, type ReadWriteFileSystemLike, RealtimeDatabaseErrorAdapter, type RealtimeDatabaseRefLike, type RedisClientLike, RedisErrorAdapter, type RedisErrorOptions, type S3BatchOptions, type S3ClientLike, S3ErrorAdapter, type S3ErrorOptions, type S3PutObjectInput, type SerializedErrorLogEntry, type SqlClientLike, type SqlDialect, type SqlErrorOptions, type SqliteClientLike, SqliteErrorAdapter, type SqliteErrorOptions, type StorageLike, type SupabaseClientLike, SupabaseErrorAdapter, type SupabaseErrorLike, type SupabaseErrorOptions, type SupabaseErrorRow, type SupabaseInsertResult, type SupabaseSetupOptions, WebSocketErrorAdapter, type WebSocketErrorOptions, type WebSocketLike, WebStorageErrorAdapter, type WebStorageErrorOptions, type WebStorageErrorRecord, WebhookErrorAdapter, type WebhookErrorOptions, buildCreateErrorLogTableSql, buildErrorLogTableDdl, buildMySqlWidenColumnsSql, buildSupabaseSetupSql, createErrorLogStore, formatErrorLogLine, serializeCause, serializeEntry };
1071
+ /** One event as `PutLogEvents` takes it. */
1072
+ interface CloudWatchLogEvent {
1073
+ /** Milliseconds since the epoch. */
1074
+ timestamp: number;
1075
+ message: string;
1076
+ }
1077
+ /** The parameters this adapter passes to `putLogEvents`. */
1078
+ interface CloudWatchPutLogEventsInput {
1079
+ logGroupName: string;
1080
+ logStreamName: string;
1081
+ logEvents: CloudWatchLogEvent[];
1082
+ }
1083
+ /**
1084
+ * What an AWS SDK client needs to offer. The AWS SDK v3 aggregated client
1085
+ * (`new CloudWatchLogs({ ... })` from `@aws-sdk/client-cloudwatch-logs`) fits as
1086
+ * it is. `createLogGroup` and `createLogStream` are only used when
1087
+ * `createMissing` is on (the default); leave them out of a hand-written client
1088
+ * that cannot create them.
1089
+ */
1090
+ interface CloudWatchLogsClientLike {
1091
+ putLogEvents(input: CloudWatchPutLogEventsInput): Promise<unknown>;
1092
+ createLogGroup?(input: {
1093
+ logGroupName: string;
1094
+ }): Promise<unknown>;
1095
+ createLogStream?(input: {
1096
+ logGroupName: string;
1097
+ logStreamName: string;
1098
+ }): Promise<unknown>;
1099
+ }
1100
+ interface CloudWatchBatchOptions {
1101
+ /** Send once this many events are buffered. */
1102
+ batchSize: number;
1103
+ /**
1104
+ * Also send when the oldest buffered event has waited this long (ms). The
1105
+ * timer does not keep the process alive, so call `flush()` before exiting.
1106
+ */
1107
+ maxWaitMs?: number;
1108
+ }
1109
+ interface CloudWatchLogsErrorOptions {
1110
+ /**
1111
+ * Create the log stream, and the log group if that is missing too, when
1112
+ * `PutLogEvents` says they do not exist, then try again once. This needs the
1113
+ * `logs:CreateLogStream` (and `logs:CreateLogGroup`) permissions and only runs
1114
+ * after a "not found" answer, so a stream that exists costs nothing. A group
1115
+ * created this way never expires its logs; set a retention policy on it.
1116
+ * Defaults to `true`.
1117
+ */
1118
+ createMissing?: boolean;
1119
+ /**
1120
+ * Buffer events and send them together, up to the 10,000 events and 1 MiB
1121
+ * that one request allows. Off by default: one request per entry.
1122
+ */
1123
+ batch?: CloudWatchBatchOptions;
1124
+ /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */
1125
+ prefix?: string;
1126
+ /**
1127
+ * How long a request may take before `log` (or `flush`) gives up waiting, in
1128
+ * milliseconds. Defaults to 10000. Pass `Infinity` to wait without limit. The
1129
+ * request cannot be cancelled, so it may still finish afterwards; in batch
1130
+ * mode the events stay buffered and are sent again by the next flush.
1131
+ */
1132
+ timeoutMs?: number;
1133
+ }
1134
+ /**
1135
+ * Sends entries to one CloudWatch Logs stream through `PutLogEvents`, each as a
1136
+ * JSON event (the serialized entry plus its UUID `id`) stamped with the entry's
1137
+ * time, so Logs Insights can query its fields. AWS no longer asks for sequence
1138
+ * tokens, so none are tracked.
1139
+ *
1140
+ * By default each entry is its own request and `log` resolves once CloudWatch
1141
+ * accepts it. With `batch`, events are buffered and sent together; `log` then
1142
+ * resolves ok as soon as the event is buffered (it is not stored yet), except
1143
+ * for the call that fills the batch, which resolves with the send result. Call
1144
+ * `flush()` before shutting down.
1145
+ *
1146
+ * CloudWatch can answer ok and still reject events it considers too old (over
1147
+ * 14 days) or too new (over 2 hours ahead); that is reported as `ok: false`. An
1148
+ * event over 256 KiB is refused before sending, since CloudWatch would reject
1149
+ * the whole request.
1150
+ *
1151
+ * In AWS Lambda, ECS and similar runtimes, anything written to stdout already
1152
+ * reaches CloudWatch; this adapter is for apps running elsewhere.
1153
+ */
1154
+ declare class CloudWatchLogsErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
1155
+ private readonly client;
1156
+ private readonly logGroupName;
1157
+ private readonly logStreamName;
1158
+ private readonly createMissing;
1159
+ private readonly batch;
1160
+ private readonly logPrefix;
1161
+ private readonly timeoutMs;
1162
+ private buffer;
1163
+ private timer;
1164
+ private readonly flushQueue;
1165
+ /** @throws {RangeError} if a name is empty, the batch or timeout options are invalid, or `createMissing` is on but the client cannot create streams. */
1166
+ constructor(client: CloudWatchLogsClientLike, logGroupName: string, logStreamName: string, options?: CloudWatchLogsErrorOptions);
1167
+ log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
1168
+ /**
1169
+ * Sends any buffered events now. Resolves ok when nothing is buffered. Events
1170
+ * from a failed send stay buffered and are retried by the next flush.
1171
+ */
1172
+ flush(): Promise<ErrorLogResult>;
1173
+ private addToBatch;
1174
+ private scheduleFlush;
1175
+ private sendBuffered;
1176
+ /**
1177
+ * Sends one request, creating the stream first if CloudWatch says it is
1178
+ * missing. Resolves to a description of any events CloudWatch refused, or
1179
+ * undefined when it took them all.
1180
+ */
1181
+ private send;
1182
+ private putLogEvents;
1183
+ private createStream;
1184
+ private fail;
1185
+ }
1186
+
1187
+ /**
1188
+ * An adapter that can send several entries in one go, such as
1189
+ * `WebhookErrorAdapter` (one request carrying a JSON array).
1190
+ * `BatchingErrorAdapter` uses it when it is there. The whole batch succeeds or
1191
+ * fails together.
1192
+ */
1193
+ interface BatchCapableAdapter<TCode extends string = string> extends ErrorLogAdapter<TCode> {
1194
+ logBatch(entries: ErrorLogEntry<TCode>[]): Promise<ErrorLogResult>;
1195
+ }
1196
+ interface BatchingErrorOptions<TCode extends string = string> {
1197
+ /** Deliver once this many entries are waiting. Defaults to 20. */
1198
+ batchSize?: number;
1199
+ /**
1200
+ * Also deliver once the oldest waiting entry has waited this long (ms).
1201
+ * Defaults to 5000. Pass `Infinity` to deliver only when `batchSize` is
1202
+ * reached or `flush()` is called. The timer does not keep the process alive,
1203
+ * so call `flush()` before exiting.
1204
+ */
1205
+ maxWaitMs?: number;
1206
+ /**
1207
+ * How many times to try an entry again after a failed delivery before giving
1208
+ * up on it. Defaults to 3 (four attempts in all).
1209
+ */
1210
+ retries?: number;
1211
+ /** Wait before the first retry, in milliseconds; it doubles after that. Defaults to 500. */
1212
+ retryDelayMs?: number;
1213
+ /** Longest wait between attempts, in milliseconds. Defaults to 30000. */
1214
+ maxRetryDelayMs?: number;
1215
+ /**
1216
+ * Most entries kept while the sink is slow or down. Past this, the oldest
1217
+ * are dropped. Defaults to 1000. Must be at least `batchSize`.
1218
+ */
1219
+ maxBuffered?: number;
1220
+ /**
1221
+ * Use the wrapped adapter's `logBatch` when it has one. Defaults to `true`.
1222
+ * Pass `false` to deliver one entry at a time even then (a wrapped
1223
+ * `WebhookErrorAdapter` then sends one request per entry, not an array).
1224
+ */
1225
+ useBatchMethod?: boolean;
1226
+ /**
1227
+ * Called when entries are given up on, either because they failed every
1228
+ * attempt or because the buffer was full. Errors it throws are ignored.
1229
+ */
1230
+ onDrop?: (entries: ErrorLogEntry<TCode>[], reason: string) => void;
1231
+ /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */
1232
+ prefix?: string;
1233
+ }
1234
+ /**
1235
+ * Puts a queue in front of any other adapter. `log()` returns `ok: true` as
1236
+ * soon as the entry is queued, so a slow or failing sink never holds up the
1237
+ * application; entries are then delivered in order, in batches, and a failed
1238
+ * delivery is tried again with exponential backoff before the entry is given up
1239
+ * on (see `onDrop`).
1240
+ *
1241
+ * Delivery uses the wrapped adapter's `logBatch` when it has one (the webhook
1242
+ * sends one request for the whole batch); otherwise the entries go one at a
1243
+ * time through `log`, stopping at the first failure so a sink that is down is
1244
+ * not hammered. Because of that, `ok: true` from `log()` means "queued", not
1245
+ * "stored": call `flush()` before the process exits, and watch `onDrop` or the
1246
+ * console for entries that could not be delivered. The queue is in memory, so
1247
+ * entries still waiting when the process dies are lost; put a durable adapter
1248
+ * beside it in a `CompositeErrorAdapter` when that matters.
1249
+ *
1250
+ * ```ts
1251
+ * const errorLog = new BatchingErrorAdapter(
1252
+ * new WebhookErrorAdapter('https://example.com/errors'),
1253
+ * { batchSize: 50, maxWaitMs: 2000 }
1254
+ * );
1255
+ * process.on('beforeExit', () => void errorLog.flush());
1256
+ * ```
1257
+ *
1258
+ * Do not also turn on the wrapped adapter's own retries (for example the
1259
+ * webhook's `retries`): the two multiply.
1260
+ */
1261
+ declare class BatchingErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
1262
+ private readonly inner;
1263
+ private readonly batchSize;
1264
+ private readonly maxWaitMs;
1265
+ private readonly retries;
1266
+ private readonly retryDelayMs;
1267
+ private readonly maxRetryDelayMs;
1268
+ private readonly maxBuffered;
1269
+ private readonly useBatchMethod;
1270
+ private readonly onDrop;
1271
+ private readonly logPrefix;
1272
+ private queue;
1273
+ private waitTimer;
1274
+ private backoffTimer;
1275
+ private draining;
1276
+ private flushers;
1277
+ private droppedSinceFlush;
1278
+ private lastDropReason;
1279
+ /** @throws {RangeError} if an option is out of range. */
1280
+ constructor(inner: ErrorLogAdapter<TCode>, options?: BatchingErrorOptions<TCode>);
1281
+ /** Queues the entry. Resolves ok straight away; delivery happens in the background. */
1282
+ log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
1283
+ /**
1284
+ * Delivers everything queued, retrying as configured, and resolves once the
1285
+ * queue is empty. Resolves `ok: false` if any entries were given up on since
1286
+ * the last flush. With retries on and the sink down this can take a while
1287
+ * (the waits add up to well over a minute with the defaults).
1288
+ */
1289
+ flush(): Promise<ErrorLogResult>;
1290
+ private scheduleDrain;
1291
+ private startDraining;
1292
+ /** Delivers batches until the queue is empty, waiting out a backoff after each failure. */
1293
+ private drain;
1294
+ /**
1295
+ * Tries to deliver `batch` (the front of the queue). Delivered entries leave
1296
+ * the queue; an entry that failed has its failure count raised and stays.
1297
+ * Resolves to the failure message, or undefined when everything went through.
1298
+ */
1299
+ private deliver;
1300
+ /** The wrapped adapter should not throw, but a bug in one must not stop the queue. */
1301
+ private safely;
1302
+ private backoff;
1303
+ private drop;
1304
+ }
1305
+
1306
+ export { type BatchCapableAdapter, BatchingErrorAdapter, type BatchingErrorOptions, type CloudWatchBatchOptions, type CloudWatchLogEvent, type CloudWatchLogsClientLike, CloudWatchLogsErrorAdapter, type CloudWatchLogsErrorOptions, type CloudWatchPutLogEventsInput, type CollectionLike, CompositeErrorAdapter, type CompositeErrorOptions, ConsoleErrorAdapter, type ConsoleErrorOptions, type EmitterLike, type ErrorLogAdapter, type ErrorLogEntry, type ErrorLogResult, EventEmitterErrorAdapter, type EventEmitterErrorOptions, type EventEmitterErrorRecord, type FileSystemLike, type FirebaseErrorOptions, type FirebaseErrorRecord, type FirestoreCollectionLike, FirestoreErrorAdapter, FlatFileErrorAdapter, type FlatFileErrorOptions, type IdbDatabaseLike, type IdbObjectStoreLike, type IdbRequestLike, type IdbTransactionLike, type IdbUpgradeDatabaseLike, IndexedDbErrorAdapter, type IndexedDbErrorOptions, type IndexedDbErrorRecord, JsonFileErrorAdapter, type JsonFileErrorOptions, type JsonFileErrorRecord, type LockManagerLike, MongoDbErrorAdapter, type MongoDbErrorOptions, MySqlErrorAdapter, PostgresErrorAdapter, type ReadWriteFileSystemLike, RealtimeDatabaseErrorAdapter, type RealtimeDatabaseRefLike, type RedisClientLike, RedisErrorAdapter, type RedisErrorOptions, type S3BatchOptions, type S3ClientLike, S3ErrorAdapter, type S3ErrorOptions, type S3PutObjectInput, type SerializedErrorLogEntry, type SqlClientLike, type SqlDialect, type SqlErrorOptions, type SqliteClientLike, SqliteErrorAdapter, type SqliteErrorOptions, type StorageLike, type SupabaseClientLike, SupabaseErrorAdapter, type SupabaseErrorLike, type SupabaseErrorOptions, type SupabaseErrorRow, type SupabaseInsertResult, type SupabaseSetupOptions, WebSocketErrorAdapter, type WebSocketErrorOptions, type WebSocketLike, WebStorageErrorAdapter, type WebStorageErrorOptions, type WebStorageErrorRecord, WebhookErrorAdapter, type WebhookErrorOptions, buildCreateErrorLogTableSql, buildErrorLogTableDdl, buildMySqlWidenColumnsSql, buildSupabaseSetupSql, createErrorLogStore, formatErrorLogLine, serializeCause, serializeEntry };
package/dist/index.d.ts CHANGED
@@ -127,6 +127,15 @@ declare class WebhookErrorAdapter<TCode extends string = string> implements Erro
127
127
  /** @throws {RangeError} if `url` is not a valid `https://` URL (see `allowInsecure`) or a timeout, retry or signing option is invalid. */
128
128
  constructor(url: string, options?: WebhookErrorOptions);
129
129
  log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
130
+ /**
131
+ * Sends several entries in one request, as a JSON array of the same records
132
+ * `log` sends one at a time (each with an `id`). `BatchingErrorAdapter` uses
133
+ * this when it wraps a webhook, so the receiver must accept an array. The
134
+ * whole request succeeds or fails together.
135
+ */
136
+ logBatch(entries: ErrorLogEntry<TCode>[]): Promise<ErrorLogResult>;
137
+ /** Sends one prepared body, trying again as `retries` allows. */
138
+ private deliver;
130
139
  /** Milliseconds to wait before retry number `retryNumber` (1 for the first). */
131
140
  private retryWaitMs;
132
141
  private send;
@@ -1059,4 +1068,239 @@ declare class WebSocketErrorAdapter<TCode extends string = string> implements Er
1059
1068
  log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
1060
1069
  }
1061
1070
 
1062
- export { type CollectionLike, CompositeErrorAdapter, type CompositeErrorOptions, ConsoleErrorAdapter, type ConsoleErrorOptions, type EmitterLike, type ErrorLogAdapter, type ErrorLogEntry, type ErrorLogResult, EventEmitterErrorAdapter, type EventEmitterErrorOptions, type EventEmitterErrorRecord, type FileSystemLike, type FirebaseErrorOptions, type FirebaseErrorRecord, type FirestoreCollectionLike, FirestoreErrorAdapter, FlatFileErrorAdapter, type FlatFileErrorOptions, type IdbDatabaseLike, type IdbObjectStoreLike, type IdbRequestLike, type IdbTransactionLike, type IdbUpgradeDatabaseLike, IndexedDbErrorAdapter, type IndexedDbErrorOptions, type IndexedDbErrorRecord, JsonFileErrorAdapter, type JsonFileErrorOptions, type JsonFileErrorRecord, type LockManagerLike, MongoDbErrorAdapter, type MongoDbErrorOptions, MySqlErrorAdapter, PostgresErrorAdapter, type ReadWriteFileSystemLike, RealtimeDatabaseErrorAdapter, type RealtimeDatabaseRefLike, type RedisClientLike, RedisErrorAdapter, type RedisErrorOptions, type S3BatchOptions, type S3ClientLike, S3ErrorAdapter, type S3ErrorOptions, type S3PutObjectInput, type SerializedErrorLogEntry, type SqlClientLike, type SqlDialect, type SqlErrorOptions, type SqliteClientLike, SqliteErrorAdapter, type SqliteErrorOptions, type StorageLike, type SupabaseClientLike, SupabaseErrorAdapter, type SupabaseErrorLike, type SupabaseErrorOptions, type SupabaseErrorRow, type SupabaseInsertResult, type SupabaseSetupOptions, WebSocketErrorAdapter, type WebSocketErrorOptions, type WebSocketLike, WebStorageErrorAdapter, type WebStorageErrorOptions, type WebStorageErrorRecord, WebhookErrorAdapter, type WebhookErrorOptions, buildCreateErrorLogTableSql, buildErrorLogTableDdl, buildMySqlWidenColumnsSql, buildSupabaseSetupSql, createErrorLogStore, formatErrorLogLine, serializeCause, serializeEntry };
1071
+ /** One event as `PutLogEvents` takes it. */
1072
+ interface CloudWatchLogEvent {
1073
+ /** Milliseconds since the epoch. */
1074
+ timestamp: number;
1075
+ message: string;
1076
+ }
1077
+ /** The parameters this adapter passes to `putLogEvents`. */
1078
+ interface CloudWatchPutLogEventsInput {
1079
+ logGroupName: string;
1080
+ logStreamName: string;
1081
+ logEvents: CloudWatchLogEvent[];
1082
+ }
1083
+ /**
1084
+ * What an AWS SDK client needs to offer. The AWS SDK v3 aggregated client
1085
+ * (`new CloudWatchLogs({ ... })` from `@aws-sdk/client-cloudwatch-logs`) fits as
1086
+ * it is. `createLogGroup` and `createLogStream` are only used when
1087
+ * `createMissing` is on (the default); leave them out of a hand-written client
1088
+ * that cannot create them.
1089
+ */
1090
+ interface CloudWatchLogsClientLike {
1091
+ putLogEvents(input: CloudWatchPutLogEventsInput): Promise<unknown>;
1092
+ createLogGroup?(input: {
1093
+ logGroupName: string;
1094
+ }): Promise<unknown>;
1095
+ createLogStream?(input: {
1096
+ logGroupName: string;
1097
+ logStreamName: string;
1098
+ }): Promise<unknown>;
1099
+ }
1100
+ interface CloudWatchBatchOptions {
1101
+ /** Send once this many events are buffered. */
1102
+ batchSize: number;
1103
+ /**
1104
+ * Also send when the oldest buffered event has waited this long (ms). The
1105
+ * timer does not keep the process alive, so call `flush()` before exiting.
1106
+ */
1107
+ maxWaitMs?: number;
1108
+ }
1109
+ interface CloudWatchLogsErrorOptions {
1110
+ /**
1111
+ * Create the log stream, and the log group if that is missing too, when
1112
+ * `PutLogEvents` says they do not exist, then try again once. This needs the
1113
+ * `logs:CreateLogStream` (and `logs:CreateLogGroup`) permissions and only runs
1114
+ * after a "not found" answer, so a stream that exists costs nothing. A group
1115
+ * created this way never expires its logs; set a retention policy on it.
1116
+ * Defaults to `true`.
1117
+ */
1118
+ createMissing?: boolean;
1119
+ /**
1120
+ * Buffer events and send them together, up to the 10,000 events and 1 MiB
1121
+ * that one request allows. Off by default: one request per entry.
1122
+ */
1123
+ batch?: CloudWatchBatchOptions;
1124
+ /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */
1125
+ prefix?: string;
1126
+ /**
1127
+ * How long a request may take before `log` (or `flush`) gives up waiting, in
1128
+ * milliseconds. Defaults to 10000. Pass `Infinity` to wait without limit. The
1129
+ * request cannot be cancelled, so it may still finish afterwards; in batch
1130
+ * mode the events stay buffered and are sent again by the next flush.
1131
+ */
1132
+ timeoutMs?: number;
1133
+ }
1134
+ /**
1135
+ * Sends entries to one CloudWatch Logs stream through `PutLogEvents`, each as a
1136
+ * JSON event (the serialized entry plus its UUID `id`) stamped with the entry's
1137
+ * time, so Logs Insights can query its fields. AWS no longer asks for sequence
1138
+ * tokens, so none are tracked.
1139
+ *
1140
+ * By default each entry is its own request and `log` resolves once CloudWatch
1141
+ * accepts it. With `batch`, events are buffered and sent together; `log` then
1142
+ * resolves ok as soon as the event is buffered (it is not stored yet), except
1143
+ * for the call that fills the batch, which resolves with the send result. Call
1144
+ * `flush()` before shutting down.
1145
+ *
1146
+ * CloudWatch can answer ok and still reject events it considers too old (over
1147
+ * 14 days) or too new (over 2 hours ahead); that is reported as `ok: false`. An
1148
+ * event over 256 KiB is refused before sending, since CloudWatch would reject
1149
+ * the whole request.
1150
+ *
1151
+ * In AWS Lambda, ECS and similar runtimes, anything written to stdout already
1152
+ * reaches CloudWatch; this adapter is for apps running elsewhere.
1153
+ */
1154
+ declare class CloudWatchLogsErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
1155
+ private readonly client;
1156
+ private readonly logGroupName;
1157
+ private readonly logStreamName;
1158
+ private readonly createMissing;
1159
+ private readonly batch;
1160
+ private readonly logPrefix;
1161
+ private readonly timeoutMs;
1162
+ private buffer;
1163
+ private timer;
1164
+ private readonly flushQueue;
1165
+ /** @throws {RangeError} if a name is empty, the batch or timeout options are invalid, or `createMissing` is on but the client cannot create streams. */
1166
+ constructor(client: CloudWatchLogsClientLike, logGroupName: string, logStreamName: string, options?: CloudWatchLogsErrorOptions);
1167
+ log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
1168
+ /**
1169
+ * Sends any buffered events now. Resolves ok when nothing is buffered. Events
1170
+ * from a failed send stay buffered and are retried by the next flush.
1171
+ */
1172
+ flush(): Promise<ErrorLogResult>;
1173
+ private addToBatch;
1174
+ private scheduleFlush;
1175
+ private sendBuffered;
1176
+ /**
1177
+ * Sends one request, creating the stream first if CloudWatch says it is
1178
+ * missing. Resolves to a description of any events CloudWatch refused, or
1179
+ * undefined when it took them all.
1180
+ */
1181
+ private send;
1182
+ private putLogEvents;
1183
+ private createStream;
1184
+ private fail;
1185
+ }
1186
+
1187
+ /**
1188
+ * An adapter that can send several entries in one go, such as
1189
+ * `WebhookErrorAdapter` (one request carrying a JSON array).
1190
+ * `BatchingErrorAdapter` uses it when it is there. The whole batch succeeds or
1191
+ * fails together.
1192
+ */
1193
+ interface BatchCapableAdapter<TCode extends string = string> extends ErrorLogAdapter<TCode> {
1194
+ logBatch(entries: ErrorLogEntry<TCode>[]): Promise<ErrorLogResult>;
1195
+ }
1196
+ interface BatchingErrorOptions<TCode extends string = string> {
1197
+ /** Deliver once this many entries are waiting. Defaults to 20. */
1198
+ batchSize?: number;
1199
+ /**
1200
+ * Also deliver once the oldest waiting entry has waited this long (ms).
1201
+ * Defaults to 5000. Pass `Infinity` to deliver only when `batchSize` is
1202
+ * reached or `flush()` is called. The timer does not keep the process alive,
1203
+ * so call `flush()` before exiting.
1204
+ */
1205
+ maxWaitMs?: number;
1206
+ /**
1207
+ * How many times to try an entry again after a failed delivery before giving
1208
+ * up on it. Defaults to 3 (four attempts in all).
1209
+ */
1210
+ retries?: number;
1211
+ /** Wait before the first retry, in milliseconds; it doubles after that. Defaults to 500. */
1212
+ retryDelayMs?: number;
1213
+ /** Longest wait between attempts, in milliseconds. Defaults to 30000. */
1214
+ maxRetryDelayMs?: number;
1215
+ /**
1216
+ * Most entries kept while the sink is slow or down. Past this, the oldest
1217
+ * are dropped. Defaults to 1000. Must be at least `batchSize`.
1218
+ */
1219
+ maxBuffered?: number;
1220
+ /**
1221
+ * Use the wrapped adapter's `logBatch` when it has one. Defaults to `true`.
1222
+ * Pass `false` to deliver one entry at a time even then (a wrapped
1223
+ * `WebhookErrorAdapter` then sends one request per entry, not an array).
1224
+ */
1225
+ useBatchMethod?: boolean;
1226
+ /**
1227
+ * Called when entries are given up on, either because they failed every
1228
+ * attempt or because the buffer was full. Errors it throws are ignored.
1229
+ */
1230
+ onDrop?: (entries: ErrorLogEntry<TCode>[], reason: string) => void;
1231
+ /** Text printed before failure messages. Defaults to `[error-log-adapter]`. */
1232
+ prefix?: string;
1233
+ }
1234
+ /**
1235
+ * Puts a queue in front of any other adapter. `log()` returns `ok: true` as
1236
+ * soon as the entry is queued, so a slow or failing sink never holds up the
1237
+ * application; entries are then delivered in order, in batches, and a failed
1238
+ * delivery is tried again with exponential backoff before the entry is given up
1239
+ * on (see `onDrop`).
1240
+ *
1241
+ * Delivery uses the wrapped adapter's `logBatch` when it has one (the webhook
1242
+ * sends one request for the whole batch); otherwise the entries go one at a
1243
+ * time through `log`, stopping at the first failure so a sink that is down is
1244
+ * not hammered. Because of that, `ok: true` from `log()` means "queued", not
1245
+ * "stored": call `flush()` before the process exits, and watch `onDrop` or the
1246
+ * console for entries that could not be delivered. The queue is in memory, so
1247
+ * entries still waiting when the process dies are lost; put a durable adapter
1248
+ * beside it in a `CompositeErrorAdapter` when that matters.
1249
+ *
1250
+ * ```ts
1251
+ * const errorLog = new BatchingErrorAdapter(
1252
+ * new WebhookErrorAdapter('https://example.com/errors'),
1253
+ * { batchSize: 50, maxWaitMs: 2000 }
1254
+ * );
1255
+ * process.on('beforeExit', () => void errorLog.flush());
1256
+ * ```
1257
+ *
1258
+ * Do not also turn on the wrapped adapter's own retries (for example the
1259
+ * webhook's `retries`): the two multiply.
1260
+ */
1261
+ declare class BatchingErrorAdapter<TCode extends string = string> implements ErrorLogAdapter<TCode> {
1262
+ private readonly inner;
1263
+ private readonly batchSize;
1264
+ private readonly maxWaitMs;
1265
+ private readonly retries;
1266
+ private readonly retryDelayMs;
1267
+ private readonly maxRetryDelayMs;
1268
+ private readonly maxBuffered;
1269
+ private readonly useBatchMethod;
1270
+ private readonly onDrop;
1271
+ private readonly logPrefix;
1272
+ private queue;
1273
+ private waitTimer;
1274
+ private backoffTimer;
1275
+ private draining;
1276
+ private flushers;
1277
+ private droppedSinceFlush;
1278
+ private lastDropReason;
1279
+ /** @throws {RangeError} if an option is out of range. */
1280
+ constructor(inner: ErrorLogAdapter<TCode>, options?: BatchingErrorOptions<TCode>);
1281
+ /** Queues the entry. Resolves ok straight away; delivery happens in the background. */
1282
+ log(entry: ErrorLogEntry<TCode>): Promise<ErrorLogResult>;
1283
+ /**
1284
+ * Delivers everything queued, retrying as configured, and resolves once the
1285
+ * queue is empty. Resolves `ok: false` if any entries were given up on since
1286
+ * the last flush. With retries on and the sink down this can take a while
1287
+ * (the waits add up to well over a minute with the defaults).
1288
+ */
1289
+ flush(): Promise<ErrorLogResult>;
1290
+ private scheduleDrain;
1291
+ private startDraining;
1292
+ /** Delivers batches until the queue is empty, waiting out a backoff after each failure. */
1293
+ private drain;
1294
+ /**
1295
+ * Tries to deliver `batch` (the front of the queue). Delivered entries leave
1296
+ * the queue; an entry that failed has its failure count raised and stays.
1297
+ * Resolves to the failure message, or undefined when everything went through.
1298
+ */
1299
+ private deliver;
1300
+ /** The wrapped adapter should not throw, but a bug in one must not stop the queue. */
1301
+ private safely;
1302
+ private backoff;
1303
+ private drop;
1304
+ }
1305
+
1306
+ export { type BatchCapableAdapter, BatchingErrorAdapter, type BatchingErrorOptions, type CloudWatchBatchOptions, type CloudWatchLogEvent, type CloudWatchLogsClientLike, CloudWatchLogsErrorAdapter, type CloudWatchLogsErrorOptions, type CloudWatchPutLogEventsInput, type CollectionLike, CompositeErrorAdapter, type CompositeErrorOptions, ConsoleErrorAdapter, type ConsoleErrorOptions, type EmitterLike, type ErrorLogAdapter, type ErrorLogEntry, type ErrorLogResult, EventEmitterErrorAdapter, type EventEmitterErrorOptions, type EventEmitterErrorRecord, type FileSystemLike, type FirebaseErrorOptions, type FirebaseErrorRecord, type FirestoreCollectionLike, FirestoreErrorAdapter, FlatFileErrorAdapter, type FlatFileErrorOptions, type IdbDatabaseLike, type IdbObjectStoreLike, type IdbRequestLike, type IdbTransactionLike, type IdbUpgradeDatabaseLike, IndexedDbErrorAdapter, type IndexedDbErrorOptions, type IndexedDbErrorRecord, JsonFileErrorAdapter, type JsonFileErrorOptions, type JsonFileErrorRecord, type LockManagerLike, MongoDbErrorAdapter, type MongoDbErrorOptions, MySqlErrorAdapter, PostgresErrorAdapter, type ReadWriteFileSystemLike, RealtimeDatabaseErrorAdapter, type RealtimeDatabaseRefLike, type RedisClientLike, RedisErrorAdapter, type RedisErrorOptions, type S3BatchOptions, type S3ClientLike, S3ErrorAdapter, type S3ErrorOptions, type S3PutObjectInput, type SerializedErrorLogEntry, type SqlClientLike, type SqlDialect, type SqlErrorOptions, type SqliteClientLike, SqliteErrorAdapter, type SqliteErrorOptions, type StorageLike, type SupabaseClientLike, SupabaseErrorAdapter, type SupabaseErrorLike, type SupabaseErrorOptions, type SupabaseErrorRow, type SupabaseInsertResult, type SupabaseSetupOptions, WebSocketErrorAdapter, type WebSocketErrorOptions, type WebSocketLike, WebStorageErrorAdapter, type WebStorageErrorOptions, type WebStorageErrorRecord, WebhookErrorAdapter, type WebhookErrorOptions, buildCreateErrorLogTableSql, buildErrorLogTableDdl, buildMySqlWidenColumnsSql, buildSupabaseSetupSql, createErrorLogStore, formatErrorLogLine, serializeCause, serializeEntry };