@logtape/logtape 1.0.0-dev.262 → 1.0.0-dev.268

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.262+96c86667",
3
+ "version": "1.0.0-dev.268+484e180d",
4
4
  "license": "MIT",
5
5
  "exports": "./mod.ts",
6
6
  "imports": {
package/dist/mod.cjs CHANGED
@@ -31,6 +31,5 @@ exports.parseLogLevel = require_level.parseLogLevel;
31
31
  exports.reset = require_config.reset;
32
32
  exports.resetSync = require_config.resetSync;
33
33
  exports.toFilter = require_filter.toFilter;
34
- exports.withBuffer = require_sink.withBuffer;
35
34
  exports.withContext = require_context.withContext;
36
35
  exports.withFilter = require_sink.withFilter;
package/dist/mod.d.cts CHANGED
@@ -3,7 +3,7 @@ import { LogLevel, compareLogLevel, getLogLevels, isLogLevel, parseLogLevel } fr
3
3
  import { LogRecord } from "./record.cjs";
4
4
  import { Filter, FilterLike, getLevelFilter, toFilter } from "./filter.cjs";
5
5
  import { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, ConsoleFormatter, FormattedValues, JsonLinesFormatterOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, defaultConsoleFormatter, defaultTextFormatter, getAnsiColorFormatter, getJsonLinesFormatter, getTextFormatter, jsonLinesFormatter } from "./formatter.cjs";
6
- import { AsyncSink, BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter } from "./sink.cjs";
6
+ import { AsyncSink, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withFilter } from "./sink.cjs";
7
7
  import { Config, ConfigError, LoggerConfig, configure, configureSync, dispose, disposeSync, getConfig, reset, resetSync } from "./config.cjs";
8
8
  import { LogMethod, Logger, getLogger } from "./logger.cjs";
9
- export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, AsyncSink, BufferSinkOptions, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, fromAsyncSink, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withBuffer, withContext, withFilter };
9
+ export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, AsyncSink, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, fromAsyncSink, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withContext, withFilter };
package/dist/mod.d.ts CHANGED
@@ -3,7 +3,7 @@ import { LogLevel, compareLogLevel, getLogLevels, isLogLevel, parseLogLevel } fr
3
3
  import { LogRecord } from "./record.js";
4
4
  import { Filter, FilterLike, getLevelFilter, toFilter } from "./filter.js";
5
5
  import { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, ConsoleFormatter, FormattedValues, JsonLinesFormatterOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, defaultConsoleFormatter, defaultTextFormatter, getAnsiColorFormatter, getJsonLinesFormatter, getTextFormatter, jsonLinesFormatter } from "./formatter.js";
6
- import { AsyncSink, BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter } from "./sink.js";
6
+ import { AsyncSink, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withFilter } from "./sink.js";
7
7
  import { Config, ConfigError, LoggerConfig, configure, configureSync, dispose, disposeSync, getConfig, reset, resetSync } from "./config.js";
8
8
  import { LogMethod, Logger, getLogger } from "./logger.js";
9
- export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, AsyncSink, BufferSinkOptions, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, fromAsyncSink, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withBuffer, withContext, withFilter };
9
+ export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, AsyncSink, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, fromAsyncSink, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withContext, withFilter };
package/dist/mod.js CHANGED
@@ -2,8 +2,8 @@ import { getLevelFilter, toFilter } from "./filter.js";
2
2
  import { compareLogLevel, getLogLevels, isLogLevel, parseLogLevel } from "./level.js";
3
3
  import { getLogger } from "./logger.js";
4
4
  import { ansiColorFormatter, defaultConsoleFormatter, defaultTextFormatter, getAnsiColorFormatter, getJsonLinesFormatter, getTextFormatter, jsonLinesFormatter } from "./formatter.js";
5
- import { fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter } from "./sink.js";
5
+ import { fromAsyncSink, getConsoleSink, getStreamSink, withFilter } from "./sink.js";
6
6
  import { ConfigError, configure, configureSync, dispose, disposeSync, getConfig, reset, resetSync } from "./config.js";
7
7
  import { withContext } from "./context.js";
8
8
 
9
- export { ConfigError, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, fromAsyncSink, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withBuffer, withContext, withFilter };
9
+ export { ConfigError, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, fromAsyncSink, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withContext, withFilter };
package/dist/sink.cjs CHANGED
@@ -23,68 +23,6 @@ function withFilter(sink, filter) {
23
23
  };
24
24
  }
25
25
  /**
26
- * Turns a sink into a buffered sink. The returned sink buffers log records
27
- * in memory and flushes them to the underlying sink when the buffer is full
28
- * or after a specified time interval.
29
- *
30
- * @example Buffer a console sink with custom options
31
- * ```typescript
32
- * const sink = withBuffer(getConsoleSink(), {
33
- * bufferSize: 5,
34
- * flushInterval: 1000
35
- * });
36
- * ```
37
- *
38
- * @param sink A sink to be buffered.
39
- * @param options Options for the buffered sink.
40
- * @returns A buffered sink that flushes records periodically.
41
- * @since 1.0.0
42
- */
43
- function withBuffer(sink, options = {}) {
44
- const bufferSize = options.bufferSize ?? 10;
45
- const flushInterval = options.flushInterval ?? 5e3;
46
- const buffer = [];
47
- let flushTimer = null;
48
- let disposed = false;
49
- function flush() {
50
- if (buffer.length === 0) return;
51
- const records = buffer.splice(0);
52
- for (const record of records) try {
53
- sink(record);
54
- } catch (error) {
55
- throw error;
56
- }
57
- if (flushTimer !== null) {
58
- clearTimeout(flushTimer);
59
- flushTimer = null;
60
- }
61
- }
62
- function scheduleFlush() {
63
- if (flushInterval <= 0 || flushTimer !== null || disposed) return;
64
- flushTimer = setTimeout(() => {
65
- flushTimer = null;
66
- flush();
67
- }, flushInterval);
68
- }
69
- const bufferedSink = (record) => {
70
- if (disposed) return;
71
- buffer.push(record);
72
- if (buffer.length >= bufferSize) flush();
73
- else scheduleFlush();
74
- };
75
- bufferedSink[Symbol.asyncDispose] = async () => {
76
- disposed = true;
77
- if (flushTimer !== null) {
78
- clearTimeout(flushTimer);
79
- flushTimer = null;
80
- }
81
- flush();
82
- if (Symbol.asyncDispose in sink) await sink[Symbol.asyncDispose]();
83
- else if (Symbol.dispose in sink) sink[Symbol.dispose]();
84
- };
85
- return bufferedSink;
86
- }
87
- /**
88
26
  * A factory that returns a sink that writes to a {@link WritableStream}.
89
27
  *
90
28
  * Note that the `stream` is of Web Streams API, which is different from
@@ -283,5 +221,4 @@ function fromAsyncSink(asyncSink) {
283
221
  exports.fromAsyncSink = fromAsyncSink;
284
222
  exports.getConsoleSink = getConsoleSink;
285
223
  exports.getStreamSink = getStreamSink;
286
- exports.withBuffer = withBuffer;
287
224
  exports.withFilter = withFilter;
package/dist/sink.d.cts CHANGED
@@ -40,44 +40,6 @@ type AsyncSink = (record: LogRecord) => Promise<void>;
40
40
  * @returns A sink that only logs records that pass the filter.
41
41
  */
42
42
  declare function withFilter(sink: Sink, filter: FilterLike): Sink;
43
- /**
44
- * Options for the {@link withBuffer} function.
45
- * @since 1.0.0
46
- */
47
- interface BufferSinkOptions {
48
- /**
49
- * The maximum number of log records to buffer before flushing to the
50
- * underlying sink.
51
- * @default `10`
52
- */
53
- bufferSize?: number;
54
- /**
55
- * The maximum time in milliseconds to wait before flushing buffered records
56
- * to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
57
- * negative to disable time-based flushing.
58
- * @default `5000`
59
- */
60
- flushInterval?: number;
61
- }
62
- /**
63
- * Turns a sink into a buffered sink. The returned sink buffers log records
64
- * in memory and flushes them to the underlying sink when the buffer is full
65
- * or after a specified time interval.
66
- *
67
- * @example Buffer a console sink with custom options
68
- * ```typescript
69
- * const sink = withBuffer(getConsoleSink(), {
70
- * bufferSize: 5,
71
- * flushInterval: 1000
72
- * });
73
- * ```
74
- *
75
- * @param sink A sink to be buffered.
76
- * @param options Options for the buffered sink.
77
- * @returns A buffered sink that flushes records periodically.
78
- * @since 1.0.0
79
- */
80
- declare function withBuffer(sink: Sink, options?: BufferSinkOptions): Sink & AsyncDisposable;
81
43
  /**
82
44
  * Options for the {@link getStreamSink} function.
83
45
  */
@@ -247,5 +209,5 @@ declare function getConsoleSink(options?: ConsoleSinkOptions): Sink | (Sink & Di
247
209
  */
248
210
  declare function fromAsyncSink(asyncSink: AsyncSink): Sink & AsyncDisposable;
249
211
  //#endregion
250
- export { AsyncSink, BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter };
212
+ export { AsyncSink, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withFilter };
251
213
  //# sourceMappingURL=sink.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sink.d.cts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;AAWA;;;;AAAsD;AAgBtD;AAA0B,KA3Bd,IAAA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B,CAAA,IAAA,CAAA;;;;;;AAGJ;AAgGxB;AAOD;;;;;;;AA2BY,iBAvNI,UAAA,CAuNJ,IAAA,EAvNqB,IAuNrB,EAAA,MAAA,EAvNmC,UAuNnC,CAAA,EAvNgD,IAuNhD;AAAO;AA8CnB;;AACW,UA5PM,iBAAA,CA4PN;EAAuB;;;EACN,SAAA,CAAA,EAzPd,aAyPc;EA4HZ;;;EAAkC,OAAG,CAAA,EAAA;IAAO,MAAA,CAAA,IAAA,EAAA,MAAA,CAAA,EAhXxB,UAgXwB;EAAe,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAjT3D,aAAA,SACN,0BACC,oBACR,OAAO;KAkGL,aAAA;;;;UAKY,kBAAA;;;;;cAKH,mBAAmB;;;;;;;;;;;;;;;;aAiBpB,OAAO,UAAU;;;;YAKlB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA8CI,cAAA,WACL,qBACR,QAAQ,OAAO;;;;;;;;;;;;;;;;;;;;;iBA4HF,aAAA,YAAyB,YAAY,OAAO"}
package/dist/sink.d.ts CHANGED
@@ -40,44 +40,6 @@ type AsyncSink = (record: LogRecord) => Promise<void>;
40
40
  * @returns A sink that only logs records that pass the filter.
41
41
  */
42
42
  declare function withFilter(sink: Sink, filter: FilterLike): Sink;
43
- /**
44
- * Options for the {@link withBuffer} function.
45
- * @since 1.0.0
46
- */
47
- interface BufferSinkOptions {
48
- /**
49
- * The maximum number of log records to buffer before flushing to the
50
- * underlying sink.
51
- * @default `10`
52
- */
53
- bufferSize?: number;
54
- /**
55
- * The maximum time in milliseconds to wait before flushing buffered records
56
- * to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
57
- * negative to disable time-based flushing.
58
- * @default `5000`
59
- */
60
- flushInterval?: number;
61
- }
62
- /**
63
- * Turns a sink into a buffered sink. The returned sink buffers log records
64
- * in memory and flushes them to the underlying sink when the buffer is full
65
- * or after a specified time interval.
66
- *
67
- * @example Buffer a console sink with custom options
68
- * ```typescript
69
- * const sink = withBuffer(getConsoleSink(), {
70
- * bufferSize: 5,
71
- * flushInterval: 1000
72
- * });
73
- * ```
74
- *
75
- * @param sink A sink to be buffered.
76
- * @param options Options for the buffered sink.
77
- * @returns A buffered sink that flushes records periodically.
78
- * @since 1.0.0
79
- */
80
- declare function withBuffer(sink: Sink, options?: BufferSinkOptions): Sink & AsyncDisposable;
81
43
  /**
82
44
  * Options for the {@link getStreamSink} function.
83
45
  */
@@ -247,5 +209,5 @@ declare function getConsoleSink(options?: ConsoleSinkOptions): Sink | (Sink & Di
247
209
  */
248
210
  declare function fromAsyncSink(asyncSink: AsyncSink): Sink & AsyncDisposable;
249
211
  //#endregion
250
- export { AsyncSink, BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter };
212
+ export { AsyncSink, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withFilter };
251
213
  //# sourceMappingURL=sink.d.ts.map
@@ -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,iBApSW,UAAA,CAoSE,IAAA,EApSe,IAoSf,EAAA,MAAA,EApS6B,UAoS7B,CAAA,EApS0C,IAoS1C;AAKlB;;;;AAsBoB,UApTH,iBAAA,CAoTG;EAAQ;;;AAKT;AA8CnB;EAA8B,UAA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;;;;;;;;;;;;;;;;;;;iBAjT3D,aAAA,SACN,0BACC,oBACR,OAAO;KAkGL,aAAA;;;;UAKY,kBAAA;;;;;cAKH,mBAAmB;;;;;;;;;;;;;;;;aAiBpB,OAAO,UAAU;;;;YAKlB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA8CI,cAAA,WACL,qBACR,QAAQ,OAAO;;;;;;;;;;;;;;;;;;;;;iBA4HF,aAAA,YAAyB,YAAY,OAAO"}
package/dist/sink.js CHANGED
@@ -23,68 +23,6 @@ function withFilter(sink, filter) {
23
23
  };
24
24
  }
25
25
  /**
26
- * Turns a sink into a buffered sink. The returned sink buffers log records
27
- * in memory and flushes them to the underlying sink when the buffer is full
28
- * or after a specified time interval.
29
- *
30
- * @example Buffer a console sink with custom options
31
- * ```typescript
32
- * const sink = withBuffer(getConsoleSink(), {
33
- * bufferSize: 5,
34
- * flushInterval: 1000
35
- * });
36
- * ```
37
- *
38
- * @param sink A sink to be buffered.
39
- * @param options Options for the buffered sink.
40
- * @returns A buffered sink that flushes records periodically.
41
- * @since 1.0.0
42
- */
43
- function withBuffer(sink, options = {}) {
44
- const bufferSize = options.bufferSize ?? 10;
45
- const flushInterval = options.flushInterval ?? 5e3;
46
- const buffer = [];
47
- let flushTimer = null;
48
- let disposed = false;
49
- function flush() {
50
- if (buffer.length === 0) return;
51
- const records = buffer.splice(0);
52
- for (const record of records) try {
53
- sink(record);
54
- } catch (error) {
55
- throw error;
56
- }
57
- if (flushTimer !== null) {
58
- clearTimeout(flushTimer);
59
- flushTimer = null;
60
- }
61
- }
62
- function scheduleFlush() {
63
- if (flushInterval <= 0 || flushTimer !== null || disposed) return;
64
- flushTimer = setTimeout(() => {
65
- flushTimer = null;
66
- flush();
67
- }, flushInterval);
68
- }
69
- const bufferedSink = (record) => {
70
- if (disposed) return;
71
- buffer.push(record);
72
- if (buffer.length >= bufferSize) flush();
73
- else scheduleFlush();
74
- };
75
- bufferedSink[Symbol.asyncDispose] = async () => {
76
- disposed = true;
77
- if (flushTimer !== null) {
78
- clearTimeout(flushTimer);
79
- flushTimer = null;
80
- }
81
- flush();
82
- if (Symbol.asyncDispose in sink) await sink[Symbol.asyncDispose]();
83
- else if (Symbol.dispose in sink) sink[Symbol.dispose]();
84
- };
85
- return bufferedSink;
86
- }
87
- /**
88
26
  * A factory that returns a sink that writes to a {@link WritableStream}.
89
27
  *
90
28
  * Note that the `stream` is of Web Streams API, which is different from
@@ -280,5 +218,5 @@ function fromAsyncSink(asyncSink) {
280
218
  }
281
219
 
282
220
  //#endregion
283
- export { fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter };
221
+ export { fromAsyncSink, getConsoleSink, getStreamSink, withFilter };
284
222
  //# sourceMappingURL=sink.js.map
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;;;;;;;;;;;;;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a,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","stream: WritableStream","options: StreamSinkOptions","sink: Sink & AsyncDisposable","buffer: LogRecord[]","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 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;;;;;;;;;;;;;;;;;;;;;;;;;AA6ED,SAAgB,cACdC,QACAC,UAA6B,CAAE,GACP;CACxB,MAAM,YAAY,QAAQ,aAAa;CAC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AAS,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,CAACV,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,cAAcW,WAA8C;CAC1E,IAAI,cAAc,QAAQ,SAAS;CACnC,MAAMR,OAA+B,CAACH,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/mod.ts CHANGED
@@ -45,14 +45,12 @@ export { getLogger, type Logger, type LogMethod } from "./logger.ts";
45
45
  export type { LogRecord } from "./record.ts";
46
46
  export {
47
47
  type AsyncSink,
48
- type BufferSinkOptions,
49
48
  type ConsoleSinkOptions,
50
49
  fromAsyncSink,
51
50
  getConsoleSink,
52
51
  getStreamSink,
53
52
  type Sink,
54
53
  type StreamSinkOptions,
55
- withBuffer,
56
54
  withFilter,
57
55
  } from "./sink.ts";
58
56
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/logtape",
3
- "version": "1.0.0-dev.262+96c86667",
3
+ "version": "1.0.0-dev.268+484e180d",
4
4
  "description": "Simple logging library with zero dependencies for Deno/Node.js/Bun/browsers",
5
5
  "keywords": [
6
6
  "logging",
package/sink.test.ts CHANGED
@@ -15,7 +15,6 @@ import {
15
15
  getConsoleSink,
16
16
  getStreamSink,
17
17
  type Sink,
18
- withBuffer,
19
18
  withFilter,
20
19
  } from "./sink.ts";
21
20
 
@@ -676,387 +675,6 @@ test("getConsoleSink() with nonBlocking - high volume non-blocking behavior", as
676
675
  (sink as Sink & Disposable)[Symbol.dispose]();
677
676
  });
678
677
 
679
- test("withBuffer() - buffer size limit", async () => {
680
- const buffer: LogRecord[] = [];
681
- const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
682
-
683
- // Add records one by one
684
- sink(trace);
685
- assertEquals(buffer.length, 0); // Not flushed yet
686
-
687
- sink(debug);
688
- assertEquals(buffer.length, 0); // Not flushed yet
689
-
690
- sink(info);
691
- assertEquals(buffer.length, 3); // Flushed when buffer is full
692
- assertEquals(buffer, [trace, debug, info]);
693
-
694
- // Add more records
695
- sink(warning);
696
- assertEquals(buffer.length, 3); // Previous records remain
697
-
698
- sink(error);
699
- assertEquals(buffer.length, 3); // Still not flushed
700
-
701
- sink(fatal);
702
- assertEquals(buffer.length, 6); // Flushed again
703
- assertEquals(buffer, [trace, debug, info, warning, error, fatal]);
704
-
705
- await sink[Symbol.asyncDispose]();
706
- });
707
-
708
- test("withBuffer() - flush interval", async () => {
709
- const buffer: LogRecord[] = [];
710
- const sink = withBuffer(buffer.push.bind(buffer), {
711
- bufferSize: 10,
712
- flushInterval: 100,
713
- });
714
-
715
- sink(trace);
716
- assertEquals(buffer.length, 0); // Not flushed immediately
717
-
718
- // Wait for flush interval
719
- await delay(150);
720
- assertEquals(buffer.length, 1); // Flushed after interval
721
- assertEquals(buffer, [trace]);
722
-
723
- await sink[Symbol.asyncDispose]();
724
- });
725
-
726
- test("withBuffer() - dispose flushes remaining records", async () => {
727
- const buffer: LogRecord[] = [];
728
- const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
729
-
730
- sink(trace);
731
- sink(debug);
732
- assertEquals(buffer.length, 0); // Not flushed yet
733
-
734
- await sink[Symbol.asyncDispose]();
735
- assertEquals(buffer.length, 2); // Flushed on dispose
736
- assertEquals(buffer, [trace, debug]);
737
- });
738
-
739
- test("withBuffer() - disabled flush interval", async () => {
740
- const buffer: LogRecord[] = [];
741
- const sink = withBuffer(buffer.push.bind(buffer), {
742
- bufferSize: 10,
743
- flushInterval: 0,
744
- });
745
-
746
- sink(trace);
747
- assertEquals(buffer.length, 0); // Not flushed immediately
748
-
749
- // Wait longer than normal flush interval
750
- await delay(200);
751
- assertEquals(buffer.length, 0); // Still not flushed due to disabled interval
752
-
753
- await sink[Symbol.asyncDispose]();
754
- assertEquals(buffer.length, 1); // Flushed only on dispose
755
- assertEquals(buffer, [trace]);
756
- });
757
-
758
- test("withBuffer() - default options", async () => {
759
- const buffer: LogRecord[] = [];
760
- const sink = withBuffer(buffer.push.bind(buffer));
761
-
762
- // Add 9 records (less than default buffer size of 10)
763
- for (let i = 0; i < 9; i++) {
764
- sink(trace);
765
- }
766
- assertEquals(buffer.length, 0); // Not flushed yet
767
-
768
- // Add 10th record to trigger flush
769
- sink(trace);
770
- assertEquals(buffer.length, 10); // Flushed when buffer is full
771
-
772
- await sink[Symbol.asyncDispose]();
773
- });
774
-
775
- test("withBuffer() - no operation after dispose", async () => {
776
- const buffer: LogRecord[] = [];
777
- const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
778
-
779
- await sink[Symbol.asyncDispose]();
780
-
781
- // Try to add records after dispose
782
- sink(trace);
783
- sink(debug);
784
- assertEquals(buffer.length, 0); // No records added after dispose
785
- });
786
-
787
- test("withBuffer() - disposes underlying AsyncDisposable sink", async () => {
788
- const buffer: LogRecord[] = [];
789
- let disposed = false;
790
-
791
- const disposableSink: Sink & AsyncDisposable = (record: LogRecord) => {
792
- buffer.push(record);
793
- };
794
- disposableSink[Symbol.asyncDispose] = () => {
795
- disposed = true;
796
- return Promise.resolve();
797
- };
798
-
799
- const bufferedSink = withBuffer(disposableSink);
800
-
801
- await bufferedSink[Symbol.asyncDispose]();
802
-
803
- assert(disposed); // Underlying sink should be disposed
804
- });
805
-
806
- test("withBuffer() - disposes underlying Disposable sink", async () => {
807
- const buffer: LogRecord[] = [];
808
- let disposed = false;
809
-
810
- const disposableSink: Sink & Disposable = (record: LogRecord) => {
811
- buffer.push(record);
812
- };
813
- disposableSink[Symbol.dispose] = () => {
814
- disposed = true;
815
- };
816
-
817
- const bufferedSink = withBuffer(disposableSink);
818
-
819
- await bufferedSink[Symbol.asyncDispose]();
820
-
821
- assert(disposed); // Underlying sink should be disposed
822
- });
823
-
824
- test("withBuffer() - handles non-disposable sink gracefully", async () => {
825
- const buffer: LogRecord[] = [];
826
- const regularSink: Sink = (record: LogRecord) => {
827
- buffer.push(record);
828
- };
829
-
830
- const bufferedSink = withBuffer(regularSink);
831
-
832
- // Should not throw when disposing non-disposable sink
833
- await bufferedSink[Symbol.asyncDispose]();
834
-
835
- // This test passes if no error is thrown
836
- assert(true);
837
- });
838
-
839
- test("withBuffer() - edge case: bufferSize 1", async () => {
840
- const buffer: LogRecord[] = [];
841
- const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 1 });
842
-
843
- // Every record should flush immediately
844
- sink(trace);
845
- assertEquals(buffer.length, 1);
846
- assertEquals(buffer, [trace]);
847
-
848
- sink(debug);
849
- assertEquals(buffer.length, 2);
850
- assertEquals(buffer, [trace, debug]);
851
-
852
- await sink[Symbol.asyncDispose]();
853
- });
854
-
855
- test("withBuffer() - edge case: bufferSize 0 or negative", async () => {
856
- const buffer: LogRecord[] = [];
857
- const sink1 = withBuffer(buffer.push.bind(buffer), { bufferSize: 0 });
858
- const sink2 = withBuffer(buffer.push.bind(buffer), { bufferSize: -5 });
859
-
860
- // Should still work, but behavior may vary
861
- sink1(trace);
862
- sink2(debug);
863
-
864
- await sink1[Symbol.asyncDispose]();
865
- await sink2[Symbol.asyncDispose]();
866
-
867
- assertEquals(buffer.length, 2);
868
- assertEquals(buffer, [trace, debug]);
869
- });
870
-
871
- test("withBuffer() - edge case: very large bufferSize", async () => {
872
- const buffer: LogRecord[] = [];
873
- const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10000 });
874
-
875
- // Add many records without hitting buffer limit
876
- for (let i = 0; i < 100; i++) {
877
- sink(trace);
878
- }
879
- assertEquals(buffer.length, 0); // Not flushed yet
880
-
881
- await sink[Symbol.asyncDispose]();
882
- assertEquals(buffer.length, 100); // All flushed on dispose
883
- });
884
-
885
- test("withBuffer() - edge case: rapid successive calls", async () => {
886
- const buffer: LogRecord[] = [];
887
- const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
888
-
889
- // Add records in rapid succession
890
- sink(trace);
891
- sink(debug);
892
- sink(info); // This should trigger flush
893
- sink(warning);
894
- sink(error);
895
- sink(fatal); // This should trigger another flush
896
-
897
- assertEquals(buffer.length, 6);
898
- assertEquals(buffer, [trace, debug, info, warning, error, fatal]);
899
-
900
- await sink[Symbol.asyncDispose]();
901
- });
902
-
903
- test("withBuffer() - edge case: multiple timer flushes", async () => {
904
- const buffer: LogRecord[] = [];
905
- const sink = withBuffer(buffer.push.bind(buffer), {
906
- bufferSize: 10,
907
- flushInterval: 50,
908
- });
909
-
910
- sink(trace);
911
- await delay(60);
912
- assertEquals(buffer.length, 1); // First flush
913
-
914
- sink(debug);
915
- await delay(60);
916
- assertEquals(buffer.length, 2); // Second flush
917
-
918
- sink(info);
919
- await delay(60);
920
- assertEquals(buffer.length, 3); // Third flush
921
-
922
- await sink[Symbol.asyncDispose]();
923
- });
924
-
925
- test("withBuffer() - edge case: timer and buffer size interaction", async () => {
926
- const buffer: LogRecord[] = [];
927
- const sink = withBuffer(buffer.push.bind(buffer), {
928
- bufferSize: 3,
929
- flushInterval: 100,
930
- });
931
-
932
- // Add 2 records (less than bufferSize)
933
- sink(trace);
934
- sink(debug);
935
- assertEquals(buffer.length, 0);
936
-
937
- // Wait for timer flush
938
- await delay(120);
939
- assertEquals(buffer.length, 2); // Timer flush
940
-
941
- // Add 3 more records to trigger buffer flush
942
- sink(info);
943
- sink(warning);
944
- sink(error); // Should trigger immediate flush
945
- assertEquals(buffer.length, 5);
946
-
947
- await sink[Symbol.asyncDispose]();
948
- });
949
-
950
- test("withBuffer() - edge case: dispose called multiple times", async () => {
951
- const buffer: LogRecord[] = [];
952
- const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
953
-
954
- sink(trace);
955
- sink(debug);
956
-
957
- // First dispose
958
- await sink[Symbol.asyncDispose]();
959
- assertEquals(buffer.length, 2);
960
-
961
- // Second dispose - should not cause errors or duplicate records
962
- await sink[Symbol.asyncDispose]();
963
- assertEquals(buffer.length, 2); // Should remain the same
964
-
965
- // Third dispose
966
- await sink[Symbol.asyncDispose]();
967
- assertEquals(buffer.length, 2); // Should remain the same
968
- });
969
-
970
- test("withBuffer() - edge case: underlying sink throws error", async () => {
971
- let errorCount = 0;
972
- const errorSink: Sink = () => {
973
- errorCount++;
974
- throw new Error("Sink error");
975
- };
976
-
977
- const bufferedSink = withBuffer(errorSink, { bufferSize: 2 });
978
-
979
- // First record goes to buffer
980
- bufferedSink(trace);
981
-
982
- // Second record should trigger flush and throw error
983
- try {
984
- bufferedSink(debug);
985
- // Should not reach here
986
- assertEquals(true, false, "Expected error to be thrown");
987
- } catch (error) {
988
- assertInstanceOf(error, Error);
989
- assertEquals(error.message, "Sink error");
990
- assertEquals(errorCount, 1); // Only first record processed before error
991
- }
992
-
993
- await bufferedSink[Symbol.asyncDispose]();
994
- });
995
-
996
- test("withBuffer() - edge case: underlying AsyncDisposable throws error", async () => {
997
- const buffer: LogRecord[] = [];
998
- let disposed = false;
999
-
1000
- const errorDisposableSink: Sink & AsyncDisposable = (record: LogRecord) => {
1001
- buffer.push(record);
1002
- };
1003
- errorDisposableSink[Symbol.asyncDispose] = () => {
1004
- disposed = true;
1005
- throw new Error("Dispose error");
1006
- };
1007
-
1008
- const bufferedSink = withBuffer(errorDisposableSink);
1009
-
1010
- bufferedSink(trace);
1011
-
1012
- try {
1013
- await bufferedSink[Symbol.asyncDispose]();
1014
- // Should not reach here
1015
- assertEquals(true, false, "Expected dispose error to be thrown");
1016
- } catch (error) {
1017
- assertInstanceOf(error, Error);
1018
- assertEquals(error.message, "Dispose error");
1019
- assert(disposed); // Should still be disposed
1020
- assertEquals(buffer.length, 1); // Buffer should have been flushed before dispose error
1021
- }
1022
- });
1023
-
1024
- test("withBuffer() - edge case: negative flushInterval", async () => {
1025
- const buffer: LogRecord[] = [];
1026
- const sink = withBuffer(buffer.push.bind(buffer), {
1027
- bufferSize: 10,
1028
- flushInterval: -1000,
1029
- });
1030
-
1031
- sink(trace);
1032
- assertEquals(buffer.length, 0);
1033
-
1034
- // Wait longer than a normal flush interval
1035
- await delay(200);
1036
- assertEquals(buffer.length, 0); // Should not flush due to negative interval
1037
-
1038
- await sink[Symbol.asyncDispose]();
1039
- assertEquals(buffer.length, 1); // Should only flush on dispose
1040
- });
1041
-
1042
- test("withBuffer() - edge case: concurrent dispose and log calls", async () => {
1043
- const buffer: LogRecord[] = [];
1044
- const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
1045
-
1046
- sink(trace);
1047
-
1048
- // Start dispose and immediately try to log more
1049
- const disposePromise = sink[Symbol.asyncDispose]();
1050
- sink(debug); // This should be ignored since dispose is in progress
1051
- sink(info); // This should be ignored since dispose is in progress
1052
-
1053
- await disposePromise;
1054
-
1055
- // Only the first record should be in buffer
1056
- assertEquals(buffer.length, 1);
1057
- assertEquals(buffer, [trace]);
1058
- });
1059
-
1060
678
  test("fromAsyncSink() - basic functionality", async () => {
1061
679
  const buffer: LogRecord[] = [];
1062
680
  const asyncSink: AsyncSink = async (record) => {
package/sink.ts CHANGED
@@ -51,115 +51,6 @@ export function withFilter(sink: Sink, filter: FilterLike): Sink {
51
51
  };
52
52
  }
53
53
 
54
- /**
55
- * Options for the {@link withBuffer} function.
56
- * @since 1.0.0
57
- */
58
- export interface BufferSinkOptions {
59
- /**
60
- * The maximum number of log records to buffer before flushing to the
61
- * underlying sink.
62
- * @default `10`
63
- */
64
- bufferSize?: number;
65
-
66
- /**
67
- * The maximum time in milliseconds to wait before flushing buffered records
68
- * to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
69
- * negative to disable time-based flushing.
70
- * @default `5000`
71
- */
72
- flushInterval?: number;
73
- }
74
-
75
- /**
76
- * Turns a sink into a buffered sink. The returned sink buffers log records
77
- * in memory and flushes them to the underlying sink when the buffer is full
78
- * or after a specified time interval.
79
- *
80
- * @example Buffer a console sink with custom options
81
- * ```typescript
82
- * const sink = withBuffer(getConsoleSink(), {
83
- * bufferSize: 5,
84
- * flushInterval: 1000
85
- * });
86
- * ```
87
- *
88
- * @param sink A sink to be buffered.
89
- * @param options Options for the buffered sink.
90
- * @returns A buffered sink that flushes records periodically.
91
- * @since 1.0.0
92
- */
93
- export function withBuffer(
94
- sink: Sink,
95
- options: BufferSinkOptions = {},
96
- ): Sink & AsyncDisposable {
97
- const bufferSize = options.bufferSize ?? 10;
98
- const flushInterval = options.flushInterval ?? 5000;
99
-
100
- const buffer: LogRecord[] = [];
101
- let flushTimer: ReturnType<typeof setTimeout> | null = null;
102
- let disposed = false;
103
-
104
- function flush(): void {
105
- if (buffer.length === 0) return;
106
-
107
- const records = buffer.splice(0);
108
- for (const record of records) {
109
- try {
110
- sink(record);
111
- } catch (error) {
112
- // Errors are handled by the sink infrastructure
113
- throw error;
114
- }
115
- }
116
-
117
- if (flushTimer !== null) {
118
- clearTimeout(flushTimer);
119
- flushTimer = null;
120
- }
121
- }
122
-
123
- function scheduleFlush(): void {
124
- if (flushInterval <= 0 || flushTimer !== null || disposed) return;
125
-
126
- flushTimer = setTimeout(() => {
127
- flushTimer = null;
128
- flush();
129
- }, flushInterval);
130
- }
131
-
132
- const bufferedSink: Sink & AsyncDisposable = (record: LogRecord) => {
133
- if (disposed) return;
134
-
135
- buffer.push(record);
136
-
137
- if (buffer.length >= bufferSize) {
138
- flush();
139
- } else {
140
- scheduleFlush();
141
- }
142
- };
143
-
144
- bufferedSink[Symbol.asyncDispose] = async () => {
145
- disposed = true;
146
- if (flushTimer !== null) {
147
- clearTimeout(flushTimer);
148
- flushTimer = null;
149
- }
150
- flush();
151
-
152
- // Dispose the underlying sink if it's disposable
153
- if (Symbol.asyncDispose in sink) {
154
- await (sink as AsyncDisposable)[Symbol.asyncDispose]();
155
- } else if (Symbol.dispose in sink) {
156
- (sink as Disposable)[Symbol.dispose]();
157
- }
158
- };
159
-
160
- return bufferedSink;
161
- }
162
-
163
54
  /**
164
55
  * Options for the {@link getStreamSink} function.
165
56
  */