@logtape/logtape 1.0.0-dev.240 → 1.0.0-dev.246

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/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/logtape",
3
- "version": "1.0.0-dev.240+9524a425",
3
+ "version": "1.0.0-dev.246+c7630de7",
4
4
  "license": "MIT",
5
5
  "exports": "./mod.ts",
6
6
  "imports": {
package/dist/sink.cjs CHANGED
@@ -112,73 +112,22 @@ function getStreamSink(stream, options = {}) {
112
112
  const formatter = options.formatter ?? require_formatter.defaultTextFormatter;
113
113
  const encoder = options.encoder ?? new TextEncoder();
114
114
  const writer = stream.getWriter();
115
- if (!options.nonBlocking) {
116
- let lastPromise = Promise.resolve();
117
- const sink = (record) => {
118
- const bytes = encoder.encode(formatter(record));
119
- lastPromise = lastPromise.then(() => writer.ready).then(() => writer.write(bytes));
120
- };
121
- sink[Symbol.asyncDispose] = async () => {
122
- await lastPromise;
123
- await writer.close();
124
- };
125
- return sink;
126
- }
127
- const nonBlockingConfig = options.nonBlocking === true ? {} : options.nonBlocking;
128
- const bufferSize = nonBlockingConfig.bufferSize ?? 100;
129
- const flushInterval = nonBlockingConfig.flushInterval ?? 100;
130
- const buffer = [];
131
- let flushTimer = null;
132
- let disposed = false;
133
- let activeFlush = null;
134
- const maxBufferSize = bufferSize * 2;
135
- async function flush() {
136
- if (buffer.length === 0) return;
137
- const records = buffer.splice(0);
138
- for (const record of records) try {
139
- const bytes = encoder.encode(formatter(record));
140
- await writer.ready;
141
- await writer.write(bytes);
142
- } catch {}
143
- }
144
- function scheduleFlush() {
145
- if (activeFlush) return;
146
- activeFlush = flush().finally(() => {
147
- activeFlush = null;
148
- });
149
- }
150
- function startFlushTimer() {
151
- if (flushTimer !== null || disposed) return;
152
- flushTimer = setInterval(() => {
153
- scheduleFlush();
154
- }, flushInterval);
155
- }
156
- const nonBlockingSink = (record) => {
157
- if (disposed) return;
158
- if (buffer.length >= maxBufferSize) buffer.shift();
159
- buffer.push(record);
160
- if (buffer.length >= bufferSize) scheduleFlush();
161
- else if (flushTimer === null) startFlushTimer();
115
+ let lastPromise = Promise.resolve();
116
+ const sink = (record) => {
117
+ const bytes = encoder.encode(formatter(record));
118
+ lastPromise = lastPromise.then(() => writer.ready).then(() => writer.write(bytes));
162
119
  };
163
- nonBlockingSink[Symbol.asyncDispose] = async () => {
164
- disposed = true;
165
- if (flushTimer !== null) {
166
- clearInterval(flushTimer);
167
- flushTimer = null;
168
- }
169
- await flush();
170
- try {
171
- await writer.close();
172
- } catch {}
120
+ sink[Symbol.asyncDispose] = async () => {
121
+ await lastPromise;
122
+ await writer.close();
173
123
  };
174
- return nonBlockingSink;
124
+ return sink;
175
125
  }
176
126
  /**
177
127
  * A console sink factory that returns a sink that logs to the console.
178
128
  *
179
129
  * @param options The options for the sink.
180
- * @returns A sink that logs to the console. If `nonBlocking` is enabled,
181
- * returns a sink that also implements {@link Disposable}.
130
+ * @returns A sink that logs to the console.
182
131
  */
183
132
  function getConsoleSink(options = {}) {
184
133
  const formatter = options.formatter ?? require_formatter.defaultConsoleFormatter;
@@ -192,7 +141,7 @@ function getConsoleSink(options = {}) {
192
141
  ...options.levelMap ?? {}
193
142
  };
194
143
  const console = options.console ?? globalThis.console;
195
- const baseSink = (record) => {
144
+ return (record) => {
196
145
  const args = formatter(record);
197
146
  const method = levelMap[record.level];
198
147
  if (method === void 0) throw new TypeError(`Invalid log level: ${record.level}.`);
@@ -201,52 +150,6 @@ function getConsoleSink(options = {}) {
201
150
  console[method](msg);
202
151
  } else console[method](...args);
203
152
  };
204
- if (!options.nonBlocking) return baseSink;
205
- const nonBlockingConfig = options.nonBlocking === true ? {} : options.nonBlocking;
206
- const bufferSize = nonBlockingConfig.bufferSize ?? 100;
207
- const flushInterval = nonBlockingConfig.flushInterval ?? 100;
208
- const buffer = [];
209
- let flushTimer = null;
210
- let disposed = false;
211
- let flushScheduled = false;
212
- const maxBufferSize = bufferSize * 2;
213
- function flush() {
214
- if (buffer.length === 0) return;
215
- const records = buffer.splice(0);
216
- for (const record of records) try {
217
- baseSink(record);
218
- } catch {}
219
- }
220
- function scheduleFlush() {
221
- if (flushScheduled) return;
222
- flushScheduled = true;
223
- setTimeout(() => {
224
- flushScheduled = false;
225
- flush();
226
- }, 0);
227
- }
228
- function startFlushTimer() {
229
- if (flushTimer !== null || disposed) return;
230
- flushTimer = setInterval(() => {
231
- flush();
232
- }, flushInterval);
233
- }
234
- const nonBlockingSink = (record) => {
235
- if (disposed) return;
236
- if (buffer.length >= maxBufferSize) buffer.shift();
237
- buffer.push(record);
238
- if (buffer.length >= bufferSize) scheduleFlush();
239
- else if (flushTimer === null) startFlushTimer();
240
- };
241
- nonBlockingSink[Symbol.dispose] = () => {
242
- disposed = true;
243
- if (flushTimer !== null) {
244
- clearInterval(flushTimer);
245
- flushTimer = null;
246
- }
247
- flush();
248
- };
249
- return nonBlockingSink;
250
153
  }
251
154
  /**
252
155
  * Converts an async sink into a regular sink with proper async handling.
package/dist/sink.d.cts CHANGED
@@ -92,40 +92,6 @@ interface StreamSinkOptions {
92
92
  encoder?: {
93
93
  encode(text: string): Uint8Array;
94
94
  };
95
- /**
96
- * Enable non-blocking mode with optional buffer configuration.
97
- * When enabled, log records are buffered and flushed in the background.
98
- *
99
- * @example Simple non-blocking mode
100
- * ```typescript
101
- * getStreamSink(stream, { nonBlocking: true });
102
- * ```
103
- *
104
- * @example Custom buffer configuration
105
- * ```typescript
106
- * getStreamSink(stream, {
107
- * nonBlocking: {
108
- * bufferSize: 1000,
109
- * flushInterval: 50
110
- * }
111
- * });
112
- * ```
113
- *
114
- * @default `false`
115
- * @since 1.0.0
116
- */
117
- nonBlocking?: boolean | {
118
- /**
119
- * Maximum number of records to buffer before flushing.
120
- * @default `100`
121
- */
122
- bufferSize?: number;
123
- /**
124
- * Interval in milliseconds between automatic flushes.
125
- * @default `100`
126
- */
127
- flushInterval?: number;
128
- };
129
95
  }
130
96
  /**
131
97
  * A factory that returns a sink that writes to a {@link WritableStream}.
@@ -182,49 +148,14 @@ interface ConsoleSinkOptions {
182
148
  * The console to log to. Defaults to {@link console}.
183
149
  */
184
150
  console?: Console;
185
- /**
186
- * Enable non-blocking mode with optional buffer configuration.
187
- * When enabled, log records are buffered and flushed in the background.
188
- *
189
- * @example Simple non-blocking mode
190
- * ```typescript
191
- * getConsoleSink({ nonBlocking: true });
192
- * ```
193
- *
194
- * @example Custom buffer configuration
195
- * ```typescript
196
- * getConsoleSink({
197
- * nonBlocking: {
198
- * bufferSize: 1000,
199
- * flushInterval: 50
200
- * }
201
- * });
202
- * ```
203
- *
204
- * @default `false`
205
- * @since 1.0.0
206
- */
207
- nonBlocking?: boolean | {
208
- /**
209
- * Maximum number of records to buffer before flushing.
210
- * @default `100`
211
- */
212
- bufferSize?: number;
213
- /**
214
- * Interval in milliseconds between automatic flushes.
215
- * @default `100`
216
- */
217
- flushInterval?: number;
218
- };
219
151
  }
220
152
  /**
221
153
  * A console sink factory that returns a sink that logs to the console.
222
154
  *
223
155
  * @param options The options for the sink.
224
- * @returns A sink that logs to the console. If `nonBlocking` is enabled,
225
- * returns a sink that also implements {@link Disposable}.
156
+ * @returns A sink that logs to the console.
226
157
  */
227
- declare function getConsoleSink(options?: ConsoleSinkOptions): Sink | (Sink & Disposable);
158
+ declare function getConsoleSink(options?: ConsoleSinkOptions): Sink;
228
159
  /**
229
160
  * Converts an async sink into a regular sink with proper async handling.
230
161
  * The returned sink chains async operations to ensure proper ordering and
@@ -1 +1 @@
1
- {"version":3,"file":"sink.d.cts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;AAWA;;;;AAAsD;AAgBtD;AAA0B,KA3Bd,IAAA,GA2Bc,CAAA,MAAA,EA3BE,SA2BF,EAAA,GAAA,IAAA;;;;AAAsC;AAWhE;AAmCA;;;;AAGG,KAjES,SAAA,GAiET,CAAA,MAAA,EAjE8B,SAiE9B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UAgL7B,CAAA,EAhL0C,IAgL1C;AAKlB;;;;AAsBoB,UAhMH,iBAAA,CAgMG;EAAQ;;;AAKT;AASnB;EAA8B,UAAA,CAAA,EAAA,MAAA;EAAA;;AAAwC;AA+CtE;;;EAAkD,aAAG,CAAA,EAAA,MAAA;;AAAsB;;;;;;;;;;;;;;;;;;iBA1N3D,UAAA,OACR,gBACG,oBACR,OAAO;;;;UAsEO,iBAAA;;;;cAIH;;;;;0BAKsB;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2BpB,aAAA,SACN,0BACC,oBACR,OAAO;KAkBL,aAAA;;;;UAKY,kBAAA;;;;;cAKH,mBAAmB;;;;;;;;;;;;;;;;aAiBpB,OAAO,UAAU;;;;YAKlB;;;;;;;;iBASI,cAAA,WAAwB,qBAA0B;;;;;;;;;;;;;;;;;;;;;iBA+ClD,aAAA,YAAyB,YAAY,OAAO"}
package/dist/sink.d.ts CHANGED
@@ -92,40 +92,6 @@ interface StreamSinkOptions {
92
92
  encoder?: {
93
93
  encode(text: string): Uint8Array;
94
94
  };
95
- /**
96
- * Enable non-blocking mode with optional buffer configuration.
97
- * When enabled, log records are buffered and flushed in the background.
98
- *
99
- * @example Simple non-blocking mode
100
- * ```typescript
101
- * getStreamSink(stream, { nonBlocking: true });
102
- * ```
103
- *
104
- * @example Custom buffer configuration
105
- * ```typescript
106
- * getStreamSink(stream, {
107
- * nonBlocking: {
108
- * bufferSize: 1000,
109
- * flushInterval: 50
110
- * }
111
- * });
112
- * ```
113
- *
114
- * @default `false`
115
- * @since 1.0.0
116
- */
117
- nonBlocking?: boolean | {
118
- /**
119
- * Maximum number of records to buffer before flushing.
120
- * @default `100`
121
- */
122
- bufferSize?: number;
123
- /**
124
- * Interval in milliseconds between automatic flushes.
125
- * @default `100`
126
- */
127
- flushInterval?: number;
128
- };
129
95
  }
130
96
  /**
131
97
  * A factory that returns a sink that writes to a {@link WritableStream}.
@@ -182,49 +148,14 @@ interface ConsoleSinkOptions {
182
148
  * The console to log to. Defaults to {@link console}.
183
149
  */
184
150
  console?: Console;
185
- /**
186
- * Enable non-blocking mode with optional buffer configuration.
187
- * When enabled, log records are buffered and flushed in the background.
188
- *
189
- * @example Simple non-blocking mode
190
- * ```typescript
191
- * getConsoleSink({ nonBlocking: true });
192
- * ```
193
- *
194
- * @example Custom buffer configuration
195
- * ```typescript
196
- * getConsoleSink({
197
- * nonBlocking: {
198
- * bufferSize: 1000,
199
- * flushInterval: 50
200
- * }
201
- * });
202
- * ```
203
- *
204
- * @default `false`
205
- * @since 1.0.0
206
- */
207
- nonBlocking?: boolean | {
208
- /**
209
- * Maximum number of records to buffer before flushing.
210
- * @default `100`
211
- */
212
- bufferSize?: number;
213
- /**
214
- * Interval in milliseconds between automatic flushes.
215
- * @default `100`
216
- */
217
- flushInterval?: number;
218
- };
219
151
  }
220
152
  /**
221
153
  * A console sink factory that returns a sink that logs to the console.
222
154
  *
223
155
  * @param options The options for the sink.
224
- * @returns A sink that logs to the console. If `nonBlocking` is enabled,
225
- * returns a sink that also implements {@link Disposable}.
156
+ * @returns A sink that logs to the console.
226
157
  */
227
- declare function getConsoleSink(options?: ConsoleSinkOptions): Sink | (Sink & Disposable);
158
+ declare function getConsoleSink(options?: ConsoleSinkOptions): Sink;
228
159
  /**
229
160
  * Converts an async sink into a regular sink with proper async handling.
230
161
  * The returned sink chains async operations to ensure proper ordering and
@@ -1 +1 @@
1
- {"version":3,"file":"sink.d.ts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;AAWA;;;;AAAsD;AAgBtD;AAA0B,KA3Bd,IAAA,GA2Bc,CAAA,MAAA,EA3BE,SA2BF,EAAA,GAAA,IAAA;;;;AAAsC;AAWhE;AAmCA;;;;AAGG,KAjES,SAAA,GAiET,CAAA,MAAA,EAjE8B,SAiE9B,EAAA,GAjE4C,OAiE5C,CAAA,IAAA,CAAA;;AAAsB;AAsEzB;;;;AAS8C;AA+D9C;;;;;;AAGyB;AAkGpB,i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UAAA,CAAA,EAAA,MAAA;EAAA;;AAAwC;AA+CtE;;;EAAkD,aAAG,CAAA,EAAA,MAAA;;AAAsB;;;;;;;;;;;;;;;;;;iBA1N3D,UAAA,OACR,gBACG,oBACR,OAAO;;;;UAsEO,iBAAA;;;;cAIH;;;;;0BAKsB;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2BpB,aAAA,SACN,0BACC,oBACR,OAAO;KAkBL,aAAA;;;;UAKY,kBAAA;;;;;cAKH,mBAAmB;;;;;;;;;;;;;;;;aAiBpB,OAAO,UAAU;;;;YAKlB;;;;;;;;iBASI,cAAA,WAAwB,qBAA0B;;;;;;;;;;;;;;;;;;;;;iBA+ClD,aAAA,YAAyB,YAAY,OAAO"}
package/dist/sink.js CHANGED
@@ -112,73 +112,22 @@ function getStreamSink(stream, options = {}) {
112
112
  const formatter = options.formatter ?? defaultTextFormatter;
113
113
  const encoder = options.encoder ?? new TextEncoder();
114
114
  const writer = stream.getWriter();
115
- if (!options.nonBlocking) {
116
- let lastPromise = Promise.resolve();
117
- const sink = (record) => {
118
- const bytes = encoder.encode(formatter(record));
119
- lastPromise = lastPromise.then(() => writer.ready).then(() => writer.write(bytes));
120
- };
121
- sink[Symbol.asyncDispose] = async () => {
122
- await lastPromise;
123
- await writer.close();
124
- };
125
- return sink;
126
- }
127
- const nonBlockingConfig = options.nonBlocking === true ? {} : options.nonBlocking;
128
- const bufferSize = nonBlockingConfig.bufferSize ?? 100;
129
- const flushInterval = nonBlockingConfig.flushInterval ?? 100;
130
- const buffer = [];
131
- let flushTimer = null;
132
- let disposed = false;
133
- let activeFlush = null;
134
- const maxBufferSize = bufferSize * 2;
135
- async function flush() {
136
- if (buffer.length === 0) return;
137
- const records = buffer.splice(0);
138
- for (const record of records) try {
139
- const bytes = encoder.encode(formatter(record));
140
- await writer.ready;
141
- await writer.write(bytes);
142
- } catch {}
143
- }
144
- function scheduleFlush() {
145
- if (activeFlush) return;
146
- activeFlush = flush().finally(() => {
147
- activeFlush = null;
148
- });
149
- }
150
- function startFlushTimer() {
151
- if (flushTimer !== null || disposed) return;
152
- flushTimer = setInterval(() => {
153
- scheduleFlush();
154
- }, flushInterval);
155
- }
156
- const nonBlockingSink = (record) => {
157
- if (disposed) return;
158
- if (buffer.length >= maxBufferSize) buffer.shift();
159
- buffer.push(record);
160
- if (buffer.length >= bufferSize) scheduleFlush();
161
- else if (flushTimer === null) startFlushTimer();
115
+ let lastPromise = Promise.resolve();
116
+ const sink = (record) => {
117
+ const bytes = encoder.encode(formatter(record));
118
+ lastPromise = lastPromise.then(() => writer.ready).then(() => writer.write(bytes));
162
119
  };
163
- nonBlockingSink[Symbol.asyncDispose] = async () => {
164
- disposed = true;
165
- if (flushTimer !== null) {
166
- clearInterval(flushTimer);
167
- flushTimer = null;
168
- }
169
- await flush();
170
- try {
171
- await writer.close();
172
- } catch {}
120
+ sink[Symbol.asyncDispose] = async () => {
121
+ await lastPromise;
122
+ await writer.close();
173
123
  };
174
- return nonBlockingSink;
124
+ return sink;
175
125
  }
176
126
  /**
177
127
  * A console sink factory that returns a sink that logs to the console.
178
128
  *
179
129
  * @param options The options for the sink.
180
- * @returns A sink that logs to the console. If `nonBlocking` is enabled,
181
- * returns a sink that also implements {@link Disposable}.
130
+ * @returns A sink that logs to the console.
182
131
  */
183
132
  function getConsoleSink(options = {}) {
184
133
  const formatter = options.formatter ?? defaultConsoleFormatter;
@@ -192,7 +141,7 @@ function getConsoleSink(options = {}) {
192
141
  ...options.levelMap ?? {}
193
142
  };
194
143
  const console = options.console ?? globalThis.console;
195
- const baseSink = (record) => {
144
+ return (record) => {
196
145
  const args = formatter(record);
197
146
  const method = levelMap[record.level];
198
147
  if (method === void 0) throw new TypeError(`Invalid log level: ${record.level}.`);
@@ -201,52 +150,6 @@ function getConsoleSink(options = {}) {
201
150
  console[method](msg);
202
151
  } else console[method](...args);
203
152
  };
204
- if (!options.nonBlocking) return baseSink;
205
- const nonBlockingConfig = options.nonBlocking === true ? {} : options.nonBlocking;
206
- const bufferSize = nonBlockingConfig.bufferSize ?? 100;
207
- const flushInterval = nonBlockingConfig.flushInterval ?? 100;
208
- const buffer = [];
209
- let flushTimer = null;
210
- let disposed = false;
211
- let flushScheduled = false;
212
- const maxBufferSize = bufferSize * 2;
213
- function flush() {
214
- if (buffer.length === 0) return;
215
- const records = buffer.splice(0);
216
- for (const record of records) try {
217
- baseSink(record);
218
- } catch {}
219
- }
220
- function scheduleFlush() {
221
- if (flushScheduled) return;
222
- flushScheduled = true;
223
- setTimeout(() => {
224
- flushScheduled = false;
225
- flush();
226
- }, 0);
227
- }
228
- function startFlushTimer() {
229
- if (flushTimer !== null || disposed) return;
230
- flushTimer = setInterval(() => {
231
- flush();
232
- }, flushInterval);
233
- }
234
- const nonBlockingSink = (record) => {
235
- if (disposed) return;
236
- if (buffer.length >= maxBufferSize) buffer.shift();
237
- buffer.push(record);
238
- if (buffer.length >= bufferSize) scheduleFlush();
239
- else if (flushTimer === null) startFlushTimer();
240
- };
241
- nonBlockingSink[Symbol.dispose] = () => {
242
- disposed = true;
243
- if (flushTimer !== null) {
244
- clearInterval(flushTimer);
245
- flushTimer = null;
246
- }
247
- flush();
248
- };
249
- return nonBlockingSink;
250
153
  }
251
154
  /**
252
155
  * Converts an async sink into a regular sink with proper async handling.
package/dist/sink.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"sink.js","names":["sink: Sink","filter: FilterLike","record: LogRecord","options: BufferSinkOptions","buffer: LogRecord[]","flushTimer: ReturnType<typeof setTimeout> | null","bufferedSink: Sink & AsyncDisposable","stream: WritableStream","options: StreamSinkOptions","sink: Sink & AsyncDisposable","flushTimer: ReturnType<typeof setInterval> | null","activeFlush: Promise<void> | null","nonBlockingSink: Sink & AsyncDisposable","options: ConsoleSinkOptions","levelMap: Record<LogLevel, ConsoleMethod>","nonBlockingSink: Sink & Disposable","asyncSink: AsyncSink"],"sources":["../sink.ts"],"sourcesContent":["import { type FilterLike, toFilter } from \"./filter.ts\";\nimport {\n type ConsoleFormatter,\n defaultConsoleFormatter,\n defaultTextFormatter,\n type TextFormatter,\n} from \"./formatter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport type { LogRecord } from \"./record.ts\";\n\n/**\n * A sink is a function that accepts a log record and prints it somewhere.\n * Thrown exceptions will be suppressed and then logged to the meta logger,\n * a {@link Logger} with the category `[\"logtape\", \"meta\"]`. (In that case,\n * the meta log record will not be passed to the sink to avoid infinite\n * recursion.)\n *\n * @param record The log record to sink.\n */\nexport type Sink = (record: LogRecord) => void;\n\n/**\n * An async sink is a function that accepts a log record and asynchronously\n * processes it. This type is used with {@link fromAsyncSink} to create\n * a regular sink that properly handles asynchronous operations.\n *\n * @param record The log record to process asynchronously.\n * @returns A promise that resolves when the record has been processed.\n * @since 1.0.0\n */\nexport type AsyncSink = (record: LogRecord) => Promise<void>;\n\n/**\n * Turns a sink into a filtered sink. The returned sink only logs records that\n * pass the filter.\n *\n * @example Filter a console sink to only log records with the info level\n * ```typescript\n * const sink = withFilter(getConsoleSink(), \"info\");\n * ```\n *\n * @param sink A sink to be filtered.\n * @param filter A filter to apply to the sink. It can be either a filter\n * function or a {@link LogLevel} string.\n * @returns A sink that only logs records that pass the filter.\n */\nexport function withFilter(sink: Sink, filter: FilterLike): Sink {\n const filterFunc = toFilter(filter);\n return (record: LogRecord) => {\n if (filterFunc(record)) sink(record);\n };\n}\n\n/**\n * Options for the {@link withBuffer} function.\n * @since 1.0.0\n */\nexport interface BufferSinkOptions {\n /**\n * The maximum number of log records to buffer before flushing to the\n * underlying sink.\n * @default `10`\n */\n bufferSize?: number;\n\n /**\n * The maximum time in milliseconds to wait before flushing buffered records\n * to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or\n * negative to disable time-based flushing.\n * @default `5000`\n */\n flushInterval?: number;\n}\n\n/**\n * Turns a sink into a buffered sink. The returned sink buffers log records\n * in memory and flushes them to the underlying sink when the buffer is full\n * or after a specified time interval.\n *\n * @example Buffer a console sink with custom options\n * ```typescript\n * const sink = withBuffer(getConsoleSink(), {\n * bufferSize: 5,\n * flushInterval: 1000\n * });\n * ```\n *\n * @param sink A sink to be buffered.\n * @param options Options for the buffered sink.\n * @returns A buffered sink that flushes records periodically.\n * @since 1.0.0\n */\nexport function withBuffer(\n sink: Sink,\n options: BufferSinkOptions = {},\n): Sink & AsyncDisposable {\n const bufferSize = options.bufferSize ?? 10;\n const flushInterval = options.flushInterval ?? 5000;\n\n const buffer: LogRecord[] = [];\n let flushTimer: ReturnType<typeof setTimeout> | null = null;\n let disposed = false;\n\n function flush(): void {\n if (buffer.length === 0) return;\n\n const records = buffer.splice(0);\n for (const record of records) {\n try {\n sink(record);\n } catch (error) {\n // Errors are handled by the sink infrastructure\n throw error;\n }\n }\n\n if (flushTimer !== null) {\n clearTimeout(flushTimer);\n flushTimer = null;\n }\n }\n\n function scheduleFlush(): void {\n if (flushInterval <= 0 || flushTimer !== null || disposed) return;\n\n flushTimer = setTimeout(() => {\n flushTimer = null;\n flush();\n }, flushInterval);\n }\n\n const bufferedSink: Sink & AsyncDisposable = (record: LogRecord) => {\n if (disposed) return;\n\n buffer.push(record);\n\n if (buffer.length >= bufferSize) {\n flush();\n } else {\n scheduleFlush();\n }\n };\n\n bufferedSink[Symbol.asyncDispose] = async () => {\n disposed = true;\n if (flushTimer !== null) {\n clearTimeout(flushTimer);\n flushTimer = null;\n }\n flush();\n\n // Dispose the underlying sink if it's disposable\n if (Symbol.asyncDispose in sink) {\n await (sink as AsyncDisposable)[Symbol.asyncDispose]();\n } else if (Symbol.dispose in sink) {\n (sink as Disposable)[Symbol.dispose]();\n }\n };\n\n return bufferedSink;\n}\n\n/**\n * Options for the {@link getStreamSink} function.\n */\nexport interface StreamSinkOptions {\n /**\n * The text formatter to use. Defaults to {@link defaultTextFormatter}.\n */\n formatter?: TextFormatter;\n\n /**\n * The text encoder to use. Defaults to an instance of {@link TextEncoder}.\n */\n encoder?: { encode(text: string): Uint8Array };\n\n /**\n * Enable non-blocking mode with optional buffer configuration.\n * When enabled, log records are buffered and flushed in the background.\n *\n * @example Simple non-blocking mode\n * ```typescript\n * getStreamSink(stream, { nonBlocking: true });\n * ```\n *\n * @example Custom buffer configuration\n * ```typescript\n * getStreamSink(stream, {\n * nonBlocking: {\n * bufferSize: 1000,\n * flushInterval: 50\n * }\n * });\n * ```\n *\n * @default `false`\n * @since 1.0.0\n */\n nonBlocking?: boolean | {\n /**\n * Maximum number of records to buffer before flushing.\n * @default `100`\n */\n bufferSize?: number;\n\n /**\n * Interval in milliseconds between automatic flushes.\n * @default `100`\n */\n flushInterval?: number;\n };\n}\n\n/**\n * A factory that returns a sink that writes to a {@link WritableStream}.\n *\n * Note that the `stream` is of Web Streams API, which is different from\n * Node.js streams. You can convert a Node.js stream to a Web Streams API\n * stream using [`stream.Writable.toWeb()`] method.\n *\n * [`stream.Writable.toWeb()`]: https://nodejs.org/api/stream.html#streamwritabletowebstreamwritable\n *\n * @example Sink to the standard error in Deno\n * ```typescript\n * const stderrSink = getStreamSink(Deno.stderr.writable);\n * ```\n *\n * @example Sink to the standard error in Node.js\n * ```typescript\n * import stream from \"node:stream\";\n * const stderrSink = getStreamSink(stream.Writable.toWeb(process.stderr));\n * ```\n *\n * @param stream The stream to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the stream.\n */\nexport function getStreamSink(\n stream: WritableStream,\n options: StreamSinkOptions = {},\n): Sink & AsyncDisposable {\n const formatter = options.formatter ?? defaultTextFormatter;\n const encoder = options.encoder ?? new TextEncoder();\n const writer = stream.getWriter();\n\n if (!options.nonBlocking) {\n let lastPromise = Promise.resolve();\n const sink: Sink & AsyncDisposable = (record: LogRecord) => {\n const bytes = encoder.encode(formatter(record));\n lastPromise = lastPromise\n .then(() => writer.ready)\n .then(() => writer.write(bytes));\n };\n sink[Symbol.asyncDispose] = async () => {\n await lastPromise;\n await writer.close();\n };\n return sink;\n }\n\n // Non-blocking mode implementation\n const nonBlockingConfig = options.nonBlocking === true\n ? {}\n : options.nonBlocking;\n const bufferSize = nonBlockingConfig.bufferSize ?? 100;\n const flushInterval = nonBlockingConfig.flushInterval ?? 100;\n\n const buffer: LogRecord[] = [];\n let flushTimer: ReturnType<typeof setInterval> | null = null;\n let disposed = false;\n let activeFlush: Promise<void> | null = null;\n const maxBufferSize = bufferSize * 2; // Overflow protection\n\n async function flush(): Promise<void> {\n if (buffer.length === 0) return;\n\n const records = buffer.splice(0);\n for (const record of records) {\n try {\n const bytes = encoder.encode(formatter(record));\n await writer.ready;\n await writer.write(bytes);\n } catch {\n // Silently ignore errors in non-blocking mode to avoid disrupting the application\n }\n }\n }\n\n function scheduleFlush(): void {\n if (activeFlush) return;\n\n activeFlush = flush().finally(() => {\n activeFlush = null;\n });\n }\n\n function startFlushTimer(): void {\n if (flushTimer !== null || disposed) return;\n\n flushTimer = setInterval(() => {\n scheduleFlush();\n }, flushInterval);\n }\n\n const nonBlockingSink: Sink & AsyncDisposable = (record: LogRecord) => {\n if (disposed) return;\n\n // Buffer overflow protection: drop oldest records if buffer is too large\n if (buffer.length >= maxBufferSize) {\n buffer.shift(); // Remove oldest record\n }\n\n buffer.push(record);\n\n if (buffer.length >= bufferSize) {\n scheduleFlush();\n } else if (flushTimer === null) {\n startFlushTimer();\n }\n };\n\n nonBlockingSink[Symbol.asyncDispose] = async () => {\n disposed = true;\n if (flushTimer !== null) {\n clearInterval(flushTimer);\n flushTimer = null;\n }\n await flush();\n try {\n await writer.close();\n } catch {\n // Writer might already be closed or errored\n }\n };\n\n return nonBlockingSink;\n}\n\ntype ConsoleMethod = \"debug\" | \"info\" | \"log\" | \"warn\" | \"error\";\n\n/**\n * Options for the {@link getConsoleSink} function.\n */\nexport interface ConsoleSinkOptions {\n /**\n * The console formatter or text formatter to use.\n * Defaults to {@link defaultConsoleFormatter}.\n */\n formatter?: ConsoleFormatter | TextFormatter;\n\n /**\n * The mapping from log levels to console methods. Defaults to:\n *\n * ```typescript\n * {\n * trace: \"trace\",\n * debug: \"debug\",\n * info: \"info\",\n * warning: \"warn\",\n * error: \"error\",\n * fatal: \"error\",\n * }\n * ```\n * @since 0.9.0\n */\n levelMap?: Record<LogLevel, ConsoleMethod>;\n\n /**\n * The console to log to. Defaults to {@link console}.\n */\n console?: Console;\n\n /**\n * Enable non-blocking mode with optional buffer configuration.\n * When enabled, log records are buffered and flushed in the background.\n *\n * @example Simple non-blocking mode\n * ```typescript\n * getConsoleSink({ nonBlocking: true });\n * ```\n *\n * @example Custom buffer configuration\n * ```typescript\n * getConsoleSink({\n * nonBlocking: {\n * bufferSize: 1000,\n * flushInterval: 50\n * }\n * });\n * ```\n *\n * @default `false`\n * @since 1.0.0\n */\n nonBlocking?: boolean | {\n /**\n * Maximum number of records to buffer before flushing.\n * @default `100`\n */\n bufferSize?: number;\n\n /**\n * Interval in milliseconds between automatic flushes.\n * @default `100`\n */\n flushInterval?: number;\n };\n}\n\n/**\n * A console sink factory that returns a sink that logs to the console.\n *\n * @param options The options for the sink.\n * @returns A sink that logs to the console. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link Disposable}.\n */\nexport function getConsoleSink(\n options: ConsoleSinkOptions = {},\n): Sink | (Sink & Disposable) {\n const formatter = options.formatter ?? defaultConsoleFormatter;\n const levelMap: Record<LogLevel, ConsoleMethod> = {\n trace: \"debug\",\n debug: \"debug\",\n info: \"info\",\n warning: \"warn\",\n error: \"error\",\n fatal: \"error\",\n ...(options.levelMap ?? {}),\n };\n const console = options.console ?? globalThis.console;\n\n const baseSink = (record: LogRecord) => {\n const args = formatter(record);\n const method = levelMap[record.level];\n if (method === undefined) {\n throw new TypeError(`Invalid log level: ${record.level}.`);\n }\n if (typeof args === \"string\") {\n const msg = args.replace(/\\r?\\n$/, \"\");\n console[method](msg);\n } else {\n console[method](...args);\n }\n };\n\n if (!options.nonBlocking) {\n return baseSink;\n }\n\n // Non-blocking mode implementation\n const nonBlockingConfig = options.nonBlocking === true\n ? {}\n : options.nonBlocking;\n const bufferSize = nonBlockingConfig.bufferSize ?? 100;\n const flushInterval = nonBlockingConfig.flushInterval ?? 100;\n\n const buffer: LogRecord[] = [];\n let flushTimer: ReturnType<typeof setInterval> | null = null;\n let disposed = false;\n let flushScheduled = false;\n const maxBufferSize = bufferSize * 2; // Overflow protection\n\n function flush(): void {\n if (buffer.length === 0) return;\n\n const records = buffer.splice(0);\n for (const record of records) {\n try {\n baseSink(record);\n } catch {\n // Silently ignore errors in non-blocking mode to avoid disrupting the application\n }\n }\n }\n\n function scheduleFlush(): void {\n if (flushScheduled) return;\n\n flushScheduled = true;\n setTimeout(() => {\n flushScheduled = false;\n flush();\n }, 0);\n }\n\n function startFlushTimer(): void {\n if (flushTimer !== null || disposed) return;\n\n flushTimer = setInterval(() => {\n flush();\n }, flushInterval);\n }\n\n const nonBlockingSink: Sink & Disposable = (record: LogRecord) => {\n if (disposed) return;\n\n // Buffer overflow protection: drop oldest records if buffer is too large\n if (buffer.length >= maxBufferSize) {\n buffer.shift(); // Remove oldest record\n }\n\n buffer.push(record);\n\n if (buffer.length >= bufferSize) {\n scheduleFlush();\n } else if (flushTimer === null) {\n startFlushTimer();\n }\n };\n\n nonBlockingSink[Symbol.dispose] = () => {\n disposed = true;\n if (flushTimer !== null) {\n clearInterval(flushTimer);\n flushTimer = null;\n }\n flush();\n };\n\n return nonBlockingSink;\n}\n\n/**\n * Converts an async sink into a regular sink with proper async handling.\n * The returned sink chains async operations to ensure proper ordering and\n * implements AsyncDisposable to wait for all pending operations on disposal.\n *\n * @example Create a sink that asynchronously posts to a webhook\n * ```typescript\n * const asyncSink: AsyncSink = async (record) => {\n * await fetch(\"https://example.com/logs\", {\n * method: \"POST\",\n * body: JSON.stringify(record),\n * });\n * };\n * const sink = fromAsyncSink(asyncSink);\n * ```\n *\n * @param asyncSink The async sink function to convert.\n * @returns A sink that properly handles async operations and disposal.\n * @since 1.0.0\n */\nexport function fromAsyncSink(asyncSink: AsyncSink): Sink & AsyncDisposable {\n let lastPromise = Promise.resolve();\n const sink: Sink & AsyncDisposable = (record: LogRecord) => {\n lastPromise = lastPromise\n .then(() => asyncSink(record))\n .catch(() => {\n // Errors are handled by the sink infrastructure\n });\n };\n sink[Symbol.asyncDispose] = async () => {\n await lastPromise;\n };\n return sink;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA8CA,SAAgB,WAAWA,MAAYC,QAA0B;CAC/D,MAAM,aAAa,SAAS,OAAO;AACnC,QAAO,CAACC,WAAsB;AAC5B,MAAI,WAAW,OAAO,CAAE,MAAK,OAAO;CACrC;AACF;;;;;;;;;;;;;;;;;;;AAyCD,SAAgB,WACdF,MACAG,UAA6B,CAAE,GACP;CACxB,MAAM,aAAa,QAAQ,cAAc;CACzC,MAAM,gBAAgB,QAAQ,iBAAiB;CAE/C,MAAMC,SAAsB,CAAE;CAC9B,IAAIC,aAAmD;CACvD,IAAI,WAAW;CAEf,SAAS,QAAc;AACrB,MAAI,OAAO,WAAW,EAAG;EAEzB,MAAM,UAAU,OAAO,OAAO,EAAE;AAChC,OAAK,MAAM,UAAU,QACnB,KAAI;AACF,QAAK,OAAO;EACb,SAAQ,OAAO;AAEd,SAAM;EACP;AAGH,MAAI,eAAe,MAAM;AACvB,gBAAa,WAAW;AACxB,gBAAa;EACd;CACF;CAED,SAAS,gBAAsB;AAC7B,MAAI,iBAAiB,KAAK,eAAe,QAAQ,SAAU;AAE3D,eAAa,WAAW,MAAM;AAC5B,gBAAa;AACb,UAAO;EACR,GAAE,cAAc;CAClB;CAED,MAAMC,eAAuC,CAACJ,WAAsB;AAClE,MAAI,SAAU;AAEd,SAAO,KAAK,OAAO;AAEnB,MAAI,OAAO,UAAU,WACnB,QAAO;MAEP,gBAAe;CAElB;AAED,cAAa,OAAO,gBAAgB,YAAY;AAC9C,aAAW;AACX,MAAI,eAAe,MAAM;AACvB,gBAAa,WAAW;AACxB,gBAAa;EACd;AACD,SAAO;AAGP,MAAI,OAAO,gBAAgB,KACzB,OAAM,AAAC,KAAyB,OAAO,eAAe;WAC7C,OAAO,WAAW,KAC3B,CAAC,KAAoB,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OAAO,WAAW,EAAG;EAEzB,MAAM,UAAU,OAAO,OAAO,EAAE;AAChC,OAAK,MAAM,UAAU,QACnB,KAAI;AACF,YAAS,OAAO;EACjB,QAAO,CAEP;CAEJ;CAED,SAAS,gBAAsB;AAC7B,MAAI,eAAgB;AAEpB,mBAAiB;AACjB,aAAW,MAAM;AACf,oBAAiB;AACjB,UAAO;EACR,GAAE,EAAE;CACN;CAED,SAAS,kBAAwB;AAC/B,MAAI,eAAe,QAAQ,SAAU;AAErC,eAAa,YAAY,MAAM;AAC7B,UAAO;EACR,GAAE,cAAc;CAClB;CAED,MAAMK,kBAAqC,CAACb,WAAsB;AAChE,MAAI,SAAU;AAGd,MAAI,OAAO,UAAU,cACnB,QAAO,OAAO;AAGhB,SAAO,KAAK,OAAO;AAEnB,MAAI,OAAO,UAAU,WACnB,gBAAe;WACN,eAAe,KACxB,kBAAiB;CAEpB;AAED,iBAAgB,OAAO,WAAW,MAAM;AACtC,aAAW;AACX,MAAI,eAAe,MAAM;AACvB,iBAAc,WAAW;AACzB,gBAAa;EACd;AACD,SAAO;CACR;AAED,QAAO;AACR;;;;;;;;;;;;;;;;;;;;;AAsBD,SAAgB,cAAcc,WAA8C;CAC1E,IAAI,cAAc,QAAQ,SAAS;CACnC,MAAMP,OAA+B,CAACP,WAAsB;AAC1D,gBAAc,YACX,KAAK,MAAM,UAAU,OAAO,CAAC,CAC7B,MAAM,MAAM,CAEZ,EAAC;CACL;AACD,MAAK,OAAO,gBAAgB,YAAY;AACtC,QAAM;CACP;AACD,QAAO;AACR"}
1
+ {"version":3,"file":"sink.js","names":["sink: Sink","filter: FilterLike","record: LogRecord","options: BufferSinkOptions","buffer: LogRecord[]","flushTimer: ReturnType<typeof setTimeout> | null","bufferedSink: Sink & AsyncDisposable","stream: WritableStream","options: StreamSinkOptions","sink: Sink & AsyncDisposable","options: ConsoleSinkOptions","levelMap: Record<LogLevel, ConsoleMethod>","asyncSink: AsyncSink"],"sources":["../sink.ts"],"sourcesContent":["import { type FilterLike, toFilter } from \"./filter.ts\";\nimport {\n type ConsoleFormatter,\n defaultConsoleFormatter,\n defaultTextFormatter,\n type TextFormatter,\n} from \"./formatter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport type { LogRecord } from \"./record.ts\";\n\n/**\n * A sink is a function that accepts a log record and prints it somewhere.\n * Thrown exceptions will be suppressed and then logged to the meta logger,\n * a {@link Logger} with the category `[\"logtape\", \"meta\"]`. (In that case,\n * the meta log record will not be passed to the sink to avoid infinite\n * recursion.)\n *\n * @param record The log record to sink.\n */\nexport type Sink = (record: LogRecord) => void;\n\n/**\n * An async sink is a function that accepts a log record and asynchronously\n * processes it. This type is used with {@link fromAsyncSink} to create\n * a regular sink that properly handles asynchronous operations.\n *\n * @param record The log record to process asynchronously.\n * @returns A promise that resolves when the record has been processed.\n * @since 1.0.0\n */\nexport type AsyncSink = (record: LogRecord) => Promise<void>;\n\n/**\n * Turns a sink into a filtered sink. The returned sink only logs records that\n * pass the filter.\n *\n * @example Filter a console sink to only log records with the info level\n * ```typescript\n * const sink = withFilter(getConsoleSink(), \"info\");\n * ```\n *\n * @param sink A sink to be filtered.\n * @param filter A filter to apply to the sink. It can be either a filter\n * function or a {@link LogLevel} string.\n * @returns A sink that only logs records that pass the filter.\n */\nexport function withFilter(sink: Sink, filter: FilterLike): Sink {\n const filterFunc = toFilter(filter);\n return (record: LogRecord) => {\n if (filterFunc(record)) sink(record);\n };\n}\n\n/**\n * Options for the {@link withBuffer} function.\n * @since 1.0.0\n */\nexport interface BufferSinkOptions {\n /**\n * The maximum number of log records to buffer before flushing to the\n * underlying sink.\n * @default `10`\n */\n bufferSize?: number;\n\n /**\n * The maximum time in milliseconds to wait before flushing buffered records\n * to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or\n * negative to disable time-based flushing.\n * @default `5000`\n */\n flushInterval?: number;\n}\n\n/**\n * Turns a sink into a buffered sink. The returned sink buffers log records\n * in memory and flushes them to the underlying sink when the buffer is full\n * or after a specified time interval.\n *\n * @example Buffer a console sink with custom options\n * ```typescript\n * const sink = withBuffer(getConsoleSink(), {\n * bufferSize: 5,\n * flushInterval: 1000\n * });\n * ```\n *\n * @param sink A sink to be buffered.\n * @param options Options for the buffered sink.\n * @returns A buffered sink that flushes records periodically.\n * @since 1.0.0\n */\nexport function withBuffer(\n sink: Sink,\n options: BufferSinkOptions = {},\n): Sink & AsyncDisposable {\n const bufferSize = options.bufferSize ?? 10;\n const flushInterval = options.flushInterval ?? 5000;\n\n const buffer: LogRecord[] = [];\n let flushTimer: ReturnType<typeof setTimeout> | null = null;\n let disposed = false;\n\n function flush(): void {\n if (buffer.length === 0) return;\n\n const records = buffer.splice(0);\n for (const record of records) {\n try {\n sink(record);\n } catch (error) {\n // Errors are handled by the sink infrastructure\n throw error;\n }\n }\n\n if (flushTimer !== null) {\n clearTimeout(flushTimer);\n flushTimer = null;\n }\n }\n\n function scheduleFlush(): void {\n if (flushInterval <= 0 || flushTimer !== null || disposed) return;\n\n flushTimer = setTimeout(() => {\n flushTimer = null;\n flush();\n }, flushInterval);\n }\n\n const bufferedSink: Sink & AsyncDisposable = (record: LogRecord) => {\n if (disposed) return;\n\n buffer.push(record);\n\n if (buffer.length >= bufferSize) {\n flush();\n } else {\n scheduleFlush();\n }\n };\n\n bufferedSink[Symbol.asyncDispose] = async () => {\n disposed = true;\n if (flushTimer !== null) {\n clearTimeout(flushTimer);\n flushTimer = null;\n }\n flush();\n\n // Dispose the underlying sink if it's disposable\n if (Symbol.asyncDispose in sink) {\n await (sink as AsyncDisposable)[Symbol.asyncDispose]();\n } else if (Symbol.dispose in sink) {\n (sink as Disposable)[Symbol.dispose]();\n }\n };\n\n return bufferedSink;\n}\n\n/**\n * Options for the {@link getStreamSink} function.\n */\nexport interface StreamSinkOptions {\n /**\n * The text formatter to use. Defaults to {@link defaultTextFormatter}.\n */\n formatter?: TextFormatter;\n\n /**\n * The text encoder to use. Defaults to an instance of {@link TextEncoder}.\n */\n encoder?: { encode(text: string): Uint8Array };\n}\n\n/**\n * A factory that returns a sink that writes to a {@link WritableStream}.\n *\n * Note that the `stream` is of Web Streams API, which is different from\n * Node.js streams. You can convert a Node.js stream to a Web Streams API\n * stream using [`stream.Writable.toWeb()`] method.\n *\n * [`stream.Writable.toWeb()`]: https://nodejs.org/api/stream.html#streamwritabletowebstreamwritable\n *\n * @example Sink to the standard error in Deno\n * ```typescript\n * const stderrSink = getStreamSink(Deno.stderr.writable);\n * ```\n *\n * @example Sink to the standard error in Node.js\n * ```typescript\n * import stream from \"node:stream\";\n * const stderrSink = getStreamSink(stream.Writable.toWeb(process.stderr));\n * ```\n *\n * @param stream The stream to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the stream.\n */\nexport function getStreamSink(\n stream: WritableStream,\n options: StreamSinkOptions = {},\n): Sink & AsyncDisposable {\n const formatter = options.formatter ?? defaultTextFormatter;\n const encoder = options.encoder ?? new TextEncoder();\n const writer = stream.getWriter();\n let lastPromise = Promise.resolve();\n const sink: Sink & AsyncDisposable = (record: LogRecord) => {\n const bytes = encoder.encode(formatter(record));\n lastPromise = lastPromise\n .then(() => writer.ready)\n .then(() => writer.write(bytes));\n };\n sink[Symbol.asyncDispose] = async () => {\n await lastPromise;\n await writer.close();\n };\n return sink;\n}\n\ntype ConsoleMethod = \"debug\" | \"info\" | \"log\" | \"warn\" | \"error\";\n\n/**\n * Options for the {@link getConsoleSink} function.\n */\nexport interface ConsoleSinkOptions {\n /**\n * The console formatter or text formatter to use.\n * Defaults to {@link defaultConsoleFormatter}.\n */\n formatter?: ConsoleFormatter | TextFormatter;\n\n /**\n * The mapping from log levels to console methods. Defaults to:\n *\n * ```typescript\n * {\n * trace: \"trace\",\n * debug: \"debug\",\n * info: \"info\",\n * warning: \"warn\",\n * error: \"error\",\n * fatal: \"error\",\n * }\n * ```\n * @since 0.9.0\n */\n levelMap?: Record<LogLevel, ConsoleMethod>;\n\n /**\n * The console to log to. Defaults to {@link console}.\n */\n console?: Console;\n}\n\n/**\n * A console sink factory that returns a sink that logs to the console.\n *\n * @param options The options for the sink.\n * @returns A sink that logs to the console.\n */\nexport function getConsoleSink(options: ConsoleSinkOptions = {}): Sink {\n const formatter = options.formatter ?? defaultConsoleFormatter;\n const levelMap: Record<LogLevel, ConsoleMethod> = {\n trace: \"debug\",\n debug: \"debug\",\n info: \"info\",\n warning: \"warn\",\n error: \"error\",\n fatal: \"error\",\n ...(options.levelMap ?? {}),\n };\n const console = options.console ?? globalThis.console;\n return (record: LogRecord) => {\n const args = formatter(record);\n const method = levelMap[record.level];\n if (method === undefined) {\n throw new TypeError(`Invalid log level: ${record.level}.`);\n }\n if (typeof args === \"string\") {\n const msg = args.replace(/\\r?\\n$/, \"\");\n console[method](msg);\n } else {\n console[method](...args);\n }\n };\n}\n\n/**\n * Converts an async sink into a regular sink with proper async handling.\n * The returned sink chains async operations to ensure proper ordering and\n * implements AsyncDisposable to wait for all pending operations on disposal.\n *\n * @example Create a sink that asynchronously posts to a webhook\n * ```typescript\n * const asyncSink: AsyncSink = async (record) => {\n * await fetch(\"https://example.com/logs\", {\n * method: \"POST\",\n * body: JSON.stringify(record),\n * });\n * };\n * const sink = fromAsyncSink(asyncSink);\n * ```\n *\n * @param asyncSink The async sink function to convert.\n * @returns A sink that properly handles async operations and disposal.\n * @since 1.0.0\n */\nexport function fromAsyncSink(asyncSink: AsyncSink): Sink & AsyncDisposable {\n let lastPromise = Promise.resolve();\n const sink: Sink & AsyncDisposable = (record: LogRecord) => {\n lastPromise = lastPromise\n .then(() => asyncSink(record))\n .catch(() => {\n // Errors are handled by the sink infrastructure\n });\n };\n sink[Symbol.asyncDispose] = async () => {\n await lastPromise;\n };\n return sink;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA8CA,SAAgB,WAAWA,MAAYC,QAA0B;CAC/D,MAAM,aAAa,SAAS,OAAO;AACnC,QAAO,CAACC,WAAsB;AAC5B,MAAI,WAAW,OAAO,CAAE,MAAK,OAAO;CACrC;AACF;;;;;;;;;;;;;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O,CAAC;AAC/C,gBAAc,YACX,KAAK,MAAM,OAAO,MAAM,CACxB,KAAK,MAAM,OAAO,MAAM,MAAM,CAAC;CACnC;AACD,MAAK,OAAO,gBAAgB,YAAY;AACtC,QAAM;AACN,QAAM,OAAO,OAAO;CACrB;AACD,QAAO;AACR;;;;;;;AA2CD,SAAgB,eAAeQ,UAA8B,CAAE,GAAQ;CACrE,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAMC,WAA4C;EAChD,OAAO;EACP,OAAO;EACP,MAAM;EACN,SAAS;EACT,OAAO;EACP,OAAO;EACP,GAAI,QAAQ,YAAY,CAAE;CAC3B;CACD,MAAM,UAAU,QAAQ,WAAW,WAAW;AAC9C,QAAO,CAACT,WAAsB;EAC5B,MAAM,OAAO,UAAU,OAAO;EAC9B,MAAM,SAAS,SAAS,OAAO;AAC/B,MAAI,kBACF,OAAM,IAAI,WAAW,qBAAqB,OAAO,MAAM;AAEzD,aAAW,SAAS,UAAU;GAC5B,MAAM,MAAM,KAAK,QAAQ,UAAU,GAAG;AACtC,WAAQ,QAAQ,IAAI;EACrB,MACC,SAAQ,QAAQ,GAAG,KAAK;CAE3B;AACF;;;;;;;;;;;;;;;;;;;;;AAsBD,SAAgB,cAAcU,WAA8C;CAC1E,IAAI,cAAc,QAAQ,SAAS;CACnC,MAAMH,OAA+B,CAACP,WAAsB;AAC1D,gBAAc,YACX,KAAK,MAAM,UAAU,OAAO,CAAC,CAC7B,MAAM,MAAM,CAEZ,EAAC;CACL;AACD,MAAK,OAAO,gBAAgB,YAAY;AACtC,QAAM;CACP;AACD,QAAO;AACR"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/logtape",
3
- "version": "1.0.0-dev.240+9524a425",
3
+ "version": "1.0.0-dev.246+c7630de7",
4
4
  "description": "Simple logging library with zero dependencies for Deno/Node.js/Bun/browsers",
5
5
  "keywords": [
6
6
  "logging",
@@ -31,21 +31,37 @@
31
31
  "types": "./dist/mod.d.ts",
32
32
  "exports": {
33
33
  ".": {
34
+ "types": {
35
+ "import": "./dist/mod.d.ts",
36
+ "require": "./dist/mod.d.cts"
37
+ },
34
38
  "import": "./dist/mod.js",
35
- "require": "./dist/mod.cjs",
36
- "types": "./dist/mod.d.ts"
39
+ "require": "./dist/mod.cjs"
37
40
  },
38
41
  "./package.json": "./package.json"
39
42
  },
40
43
  "imports": {
41
44
  "#util": {
42
- "node": "./dist/util.node.cjs",
43
- "bun": "./dist/util.node.js",
45
+ "types": {
46
+ "import": "./dist/util.d.ts",
47
+ "require": "./dist/util.d.cts"
48
+ },
49
+ "browser": {
50
+ "import": "./dist/util.js",
51
+ "require": "./dist/util.cjs"
52
+ },
53
+ "node": {
54
+ "import": "./dist/util.node.js",
55
+ "require": "./dist/util.node.cjs"
56
+ },
57
+ "bun": {
58
+ "import": "./dist/util.node.js",
59
+ "require": "./dist/util.node.cjs"
60
+ },
44
61
  "deno": "./dist/util.deno.js",
45
62
  "import": "./dist/util.js",
46
63
  "require": "./dist/util.cjs",
47
- "default": "./dist/util.js",
48
- "types": "./dist/util.d.ts"
64
+ "default": "./dist/util.js"
49
65
  }
50
66
  },
51
67
  "devDependencies": {