@logtape/file 1.1.1 → 1.1.2

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/file",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "license": "MIT",
5
5
  "exports": "./src/mod.ts",
6
6
  "imports": {
@@ -0,0 +1,74 @@
1
+ import { StreamSinkOptions } from "@logtape/logtape";
2
+
3
+ //#region dist/filesink.base.d.ts
4
+
5
+ //#region src/filesink.base.d.ts
6
+ /**
7
+ * Options for the {@link getBaseFileSink} function.
8
+ */
9
+ type FileSinkOptions = StreamSinkOptions & {
10
+ /**
11
+ * If `true`, the file is not opened until the first write. Defaults to `false`.
12
+ */
13
+ lazy?: boolean;
14
+ /**
15
+ * The size of the buffer to use when writing to the file. If not specified,
16
+ * a default buffer size will be used. If it is less or equal to 0,
17
+ * the file will be written directly without buffering.
18
+ * @default 8192
19
+ * @since 0.12.0
20
+ */
21
+ bufferSize?: number;
22
+ /**
23
+ * The maximum time interval in milliseconds between flushes. If this time
24
+ * passes since the last flush, the buffer will be flushed regardless of size.
25
+ * This helps prevent log loss during unexpected process termination.
26
+ * @default 5000
27
+ * @since 0.12.0
28
+ */
29
+ flushInterval?: number;
30
+ /**
31
+ * Enable non-blocking mode with background flushing.
32
+ * When enabled, flush operations are performed asynchronously to prevent
33
+ * blocking the main thread during file I/O operations.
34
+ *
35
+ * @default `false`
36
+ * @since 1.0.0
37
+ */
38
+ nonBlocking?: boolean;
39
+ };
40
+ /**
41
+ * A platform-specific file sink driver.
42
+ * @template TFile The type of the file descriptor.
43
+ */
44
+
45
+ /**
46
+ * Get a platform-independent file sink.
47
+ *
48
+ * @template TFile The type of the file descriptor.
49
+ * @param path A path to the file to write to.
50
+ * @param options The options for the sink and the file driver.
51
+ * @returns A sink that writes to the file. The sink is also a disposable
52
+ * object that closes the file when disposed. If `nonBlocking` is enabled,
53
+ * returns a sink that also implements {@link AsyncDisposable}.
54
+ */
55
+
56
+ /**
57
+ * Options for the {@link getBaseRotatingFileSink} function.
58
+ */
59
+ interface RotatingFileSinkOptions extends Omit<FileSinkOptions, "lazy"> {
60
+ /**
61
+ * The maximum bytes of the file before it is rotated. 1 MiB by default.
62
+ */
63
+ maxSize?: number;
64
+ /**
65
+ * The maximum number of files to keep. 5 by default.
66
+ */
67
+ maxFiles?: number;
68
+ }
69
+ /**
70
+ * A platform-specific rotating file sink driver.
71
+ */
72
+ //#endregion
73
+ export { FileSinkOptions, RotatingFileSinkOptions };
74
+ //# sourceMappingURL=filesink.base.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filesink.base.d.cts","names":["Sink","StreamSinkOptions","FileSinkOptions","FileSinkDriver","TFile","Uint8Array","AsyncFileSinkDriver","Promise","RotatingFileSinkOptions","Omit","RotatingFileSinkDriver","AsyncRotatingFileSinkDriver"],"sources":["../filesink.base.d.ts"],"sourcesContent":["import { Sink, StreamSinkOptions } from \"@logtape/logtape\";\n\n//#region src/filesink.base.d.ts\n\n/**\n * Options for the {@link getBaseFileSink} function.\n */\ntype FileSinkOptions = StreamSinkOptions & {\n /**\n * If `true`, the file is not opened until the first write. Defaults to `false`.\n */\n lazy?: boolean;\n /**\n * The size of the buffer to use when writing to the file. If not specified,\n * a default buffer size will be used. If it is less or equal to 0,\n * the file will be written directly without buffering.\n * @default 8192\n * @since 0.12.0\n */\n bufferSize?: number;\n /**\n * The maximum time interval in milliseconds between flushes. If this time\n * passes since the last flush, the buffer will be flushed regardless of size.\n * This helps prevent log loss during unexpected process termination.\n * @default 5000\n * @since 0.12.0\n */\n flushInterval?: number;\n /**\n * Enable non-blocking mode with background flushing.\n * When enabled, flush operations are performed asynchronously to prevent\n * blocking the main thread during file I/O operations.\n *\n * @default `false`\n * @since 1.0.0\n */\n nonBlocking?: boolean;\n};\n/**\n * A platform-specific file sink driver.\n * @template TFile The type of the file descriptor.\n */\ninterface FileSinkDriver<TFile> {\n /**\n * Open a file for appending and return a file descriptor.\n * @param path A path to the file to open.\n */\n openSync(path: string): TFile;\n /**\n * Write a chunk of data to the file.\n * @param fd The file descriptor.\n * @param chunk The data to write.\n */\n writeSync(fd: TFile, chunk: Uint8Array): void;\n /**\n * Write multiple chunks of data to the file in a single operation.\n * This is optional - if not implemented, falls back to multiple writeSync calls.\n * @param fd The file descriptor.\n * @param chunks Array of data chunks to write.\n */\n writeManySync?(fd: TFile, chunks: Uint8Array[]): void;\n /**\n * Flush the file to ensure that all data is written to the disk.\n * @param fd The file descriptor.\n */\n flushSync(fd: TFile): void;\n /**\n * Close the file.\n * @param fd The file descriptor.\n */\n closeSync(fd: TFile): void;\n}\n/**\n * A platform-specific async file sink driver.\n * @template TFile The type of the file descriptor.\n * @since 1.0.0\n */\ninterface AsyncFileSinkDriver<TFile> extends FileSinkDriver<TFile> {\n /**\n * Asynchronously write multiple chunks of data to the file in a single operation.\n * This is optional - if not implemented, falls back to multiple writeSync calls.\n * @param fd The file descriptor.\n * @param chunks Array of data chunks to write.\n */\n writeMany?(fd: TFile, chunks: Uint8Array[]): Promise<void>;\n /**\n * Asynchronously flush the file to ensure that all data is written to the disk.\n * @param fd The file descriptor.\n */\n flush(fd: TFile): Promise<void>;\n /**\n * Asynchronously close the file.\n * @param fd The file descriptor.\n */\n close(fd: TFile): Promise<void>;\n}\n/**\n * Get a platform-independent file sink.\n *\n * @template TFile The type of the file descriptor.\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\n\n/**\n * Options for the {@link getBaseRotatingFileSink} function.\n */\ninterface RotatingFileSinkOptions extends Omit<FileSinkOptions, \"lazy\"> {\n /**\n * The maximum bytes of the file before it is rotated. 1 MiB by default.\n */\n maxSize?: number;\n /**\n * The maximum number of files to keep. 5 by default.\n */\n maxFiles?: number;\n}\n/**\n * A platform-specific rotating file sink driver.\n */\ninterface RotatingFileSinkDriver<TFile> extends FileSinkDriver<TFile> {\n /**\n * Get the size of the file.\n * @param path A path to the file.\n * @returns The `size` of the file in bytes, in an object.\n */\n statSync(path: string): {\n size: number;\n };\n /**\n * Rename a file.\n * @param oldPath A path to the file to rename.\n * @param newPath A path to be renamed to.\n */\n renameSync(oldPath: string, newPath: string): void;\n}\n/**\n * A platform-specific async rotating file sink driver.\n * @since 1.0.0\n */\ninterface AsyncRotatingFileSinkDriver<TFile> extends AsyncFileSinkDriver<TFile> {\n /**\n * Get the size of the file.\n * @param path A path to the file.\n * @returns The `size` of the file in bytes, in an object.\n */\n statSync(path: string): {\n size: number;\n };\n /**\n * Rename a file.\n * @param oldPath A path to the file to rename.\n * @param newPath A path to be renamed to.\n */\n renameSync(oldPath: string, newPath: string): void;\n}\n/**\n * Get a platform-independent rotating file sink.\n *\n * This sink writes log records to a file, and rotates the file when it reaches\n * the `maxSize`. The rotated files are named with the original file name\n * followed by a dot and a number, starting from 1. The number is incremented\n * for each rotation, and the maximum number of files to keep is `maxFiles`.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\n//#endregion\nexport { AsyncRotatingFileSinkDriver, FileSinkDriver, FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions };\n//# sourceMappingURL=filesink.base.d.ts.map"],"mappings":";;;;;AA6E2D;;;KAtEtDE,eAAAA,GAAkBD,iBAuGmBQ,GAAAA;EAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAApCD,uBAAAA,SAAgCC,KAAKP"}
@@ -0,0 +1,45 @@
1
+ import { FileSinkOptions, RotatingFileSinkOptions } from "./filesink.base.cjs";
2
+ import { Sink } from "@logtape/logtape";
3
+
4
+ //#region dist/filesink.node.d.ts
5
+
6
+ /**
7
+ * Get a file sink.
8
+ *
9
+ * Note that this function is unavailable in the browser.
10
+ *
11
+ * @param path A path to the file to write to.
12
+ * @param options The options for the sink.
13
+ * @returns A sink that writes to the file. The sink is also a disposable
14
+ * object that closes the file when disposed. If `nonBlocking` is enabled,
15
+ * returns a sink that also implements {@link AsyncDisposable}.
16
+ */
17
+ declare function getFileSink(path: string, options?: FileSinkOptions): Sink & Disposable;
18
+ declare function getFileSink(path: string, options: FileSinkOptions & {
19
+ nonBlocking: true;
20
+ }): Sink & AsyncDisposable;
21
+ /**
22
+ * Get a rotating file sink.
23
+ *
24
+ * This sink writes log records to a file, and rotates the file when it reaches
25
+ * the `maxSize`. The rotated files are named with the original file name
26
+ * followed by a dot and a number, starting from 1. The number is incremented
27
+ * for each rotation, and the maximum number of files to keep is `maxFiles`.
28
+ *
29
+ * Note that this function is unavailable in the browser.
30
+ *
31
+ * @param path A path to the file to write to.
32
+ * @param options The options for the sink and the file driver.
33
+ * @returns A sink that writes to the file. The sink is also a disposable
34
+ * object that closes the file when disposed. If `nonBlocking` is enabled,
35
+ * returns a sink that also implements {@link AsyncDisposable}.
36
+ */
37
+ declare function getRotatingFileSink(path: string, options?: RotatingFileSinkOptions): Sink & Disposable;
38
+ declare function getRotatingFileSink(path: string, options: RotatingFileSinkOptions & {
39
+ nonBlocking: true;
40
+ }): Sink & AsyncDisposable;
41
+ //# sourceMappingURL=filesink.node.d.ts.map
42
+ //#endregion
43
+ //#endregion
44
+ export { getFileSink, getRotatingFileSink };
45
+ //# sourceMappingURL=filesink.node.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filesink.node.d.cts","names":["AsyncRotatingFileSinkDriver","FileSinkOptions","RotatingFileSinkDriver","RotatingFileSinkOptions","Sink","nodeDriver","nodeAsyncDriver","getFileSink","Disposable","AsyncDisposable","getRotatingFileSink"],"sources":["../filesink.node.d.ts"],"sourcesContent":["import { AsyncRotatingFileSinkDriver, FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions } from \"./filesink.base.js\";\nimport { Sink } from \"@logtape/logtape\";\n\n//#region src/filesink.node.d.ts\n\n/**\n * A Node.js-specific file sink driver.\n */\ndeclare const nodeDriver: RotatingFileSinkDriver<number | void>;\n/**\n * A Node.js-specific async file sink driver.\n * @since 1.0.0\n */\ndeclare const nodeAsyncDriver: AsyncRotatingFileSinkDriver<number | void>;\n/**\n * Get a file sink.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\ndeclare function getFileSink(path: string, options?: FileSinkOptions): Sink & Disposable;\ndeclare function getFileSink(path: string, options: FileSinkOptions & {\n nonBlocking: true;\n}): Sink & AsyncDisposable;\n/**\n * Get a rotating file sink.\n *\n * This sink writes log records to a file, and rotates the file when it reaches\n * the `maxSize`. The rotated files are named with the original file name\n * followed by a dot and a number, starting from 1. The number is incremented\n * for each rotation, and the maximum number of files to keep is `maxFiles`.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\ndeclare function getRotatingFileSink(path: string, options?: RotatingFileSinkOptions): Sink & Disposable;\ndeclare function getRotatingFileSink(path: string, options: RotatingFileSinkOptions & {\n nonBlocking: true;\n}): Sink & AsyncDisposable;\n//# sourceMappingURL=filesink.node.d.ts.map\n//#endregion\nexport { getFileSink, getRotatingFileSink, nodeAsyncDriver, nodeDriver };\n//# sourceMappingURL=filesink.node.d.ts.map"],"mappings":";;;;;;AA4B0B;AAAA;;;;;AAiB8E;AAAA;;;iBApBvFO,WAAAA,CAuBbH,IAAAA,EAAAA,MAAAA,EAAAA,OAAAA,CAAAA,EAvBiDH,eAuBjDG,CAAAA,EAvBmEA,IAuBnEA,GAvB0EI,UAuB1EJ;iBAtBaG,WAAAA,CAsBNE,IAAAA,EAAAA,MAAAA,EAAAA,OAAAA,EAtByCR,eAsBzCQ,GAAAA;EAAe,WAAA,EAAA,IAAA;IApBtBL,OAAOK;;;;;;;;;;;;;;;;;iBAiBMC,mBAAAA,yBAA4CP,0BAA0BC,OAAOI;iBAC7EE,mBAAAA,wBAA2CP;;IAExDC,OAAOK"}
package/dist/mod.d.cts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { FileSinkDriver, FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions } from "./filesink.base.cjs";
2
2
  import { StreamFileSinkOptions, getStreamFileSink } from "./streamfilesink.cjs";
3
- import { getFileSink, getRotatingFileSink } from "#filesink";
3
+ import { getFileSink, getRotatingFileSink } from "./dist/filesink.node.cjs";
4
4
  export { FileSinkDriver, FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions, StreamFileSinkOptions, getFileSink, getRotatingFileSink, getStreamFileSink };
@@ -53,7 +53,8 @@ const node_stream = require_rolldown_runtime.__toESM(require("node:stream"));
53
53
  * if it doesn't exist, or appended to if it does exist.
54
54
  * @param options Configuration options for the stream-based sink.
55
55
  * @returns A sink that writes formatted log records to the specified file.
56
- * The returned sink implements `Disposable` for proper resource cleanup.
56
+ * The returned sink implements `AsyncDisposable` for proper resource cleanup
57
+ * that waits for all data to be flushed to disk.
57
58
  *
58
59
  * @since 1.0.0
59
60
  */
@@ -84,7 +84,8 @@ interface StreamFileSinkOptions {
84
84
  * if it doesn't exist, or appended to if it does exist.
85
85
  * @param options Configuration options for the stream-based sink.
86
86
  * @returns A sink that writes formatted log records to the specified file.
87
- * The returned sink implements `Disposable` for proper resource cleanup.
87
+ * The returned sink implements `AsyncDisposable` for proper resource cleanup
88
+ * that waits for all data to be flushed to disk.
88
89
  *
89
90
  * @since 1.0.0
90
91
  */
@@ -1 +1 @@
1
- {"version":3,"file":"streamfilesink.d.cts","names":[],"sources":["../src/streamfilesink.ts"],"sourcesContent":[],"mappings":";;;;;;AAkBA;AA+EA;;;;;AAGyB;UAlFR,qBAAA;;;;;;;;;;;;;;;;;;;;;;uBAuBM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAwDP,iBAAA,yBAEL,wBACR,OAAO"}
1
+ {"version":3,"file":"streamfilesink.d.cts","names":[],"sources":["../src/streamfilesink.ts"],"sourcesContent":[],"mappings":";;;;;;AAkBA;AAgFA;;;;;AAGyB;UAnFR,qBAAA;;;;;;;;;;;;;;;;;;;;;;uBAuBM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyDP,iBAAA,yBAEL,wBACR,OAAO"}
@@ -84,7 +84,8 @@ interface StreamFileSinkOptions {
84
84
  * if it doesn't exist, or appended to if it does exist.
85
85
  * @param options Configuration options for the stream-based sink.
86
86
  * @returns A sink that writes formatted log records to the specified file.
87
- * The returned sink implements `Disposable` for proper resource cleanup.
87
+ * The returned sink implements `AsyncDisposable` for proper resource cleanup
88
+ * that waits for all data to be flushed to disk.
88
89
  *
89
90
  * @since 1.0.0
90
91
  */
@@ -1 +1 @@
1
- {"version":3,"file":"streamfilesink.d.ts","names":[],"sources":["../src/streamfilesink.ts"],"sourcesContent":[],"mappings":";;;;;;AAkBA;AA+EA;;;;;AAGyB;UAlFR,qBAAA;;;;;;;;;;;;;;;;;;;;;;uBAuBM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAwDP,iBAAA,yBAEL,wBACR,OAAO"}
1
+ {"version":3,"file":"streamfilesink.d.ts","names":[],"sources":["../src/streamfilesink.ts"],"sourcesContent":[],"mappings":";;;;;;AAkBA;AAgFA;;;;;AAGyB;UAnFR,qBAAA;;;;;;;;;;;;;;;;;;;;;;uBAuBM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyDP,iBAAA,yBAEL,wBACR,OAAO"}
@@ -52,7 +52,8 @@ import { PassThrough } from "node:stream";
52
52
  * if it doesn't exist, or appended to if it does exist.
53
53
  * @param options Configuration options for the stream-based sink.
54
54
  * @returns A sink that writes formatted log records to the specified file.
55
- * The returned sink implements `Disposable` for proper resource cleanup.
55
+ * The returned sink implements `AsyncDisposable` for proper resource cleanup
56
+ * that waits for all data to be flushed to disk.
56
57
  *
57
58
  * @since 1.0.0
58
59
  */
@@ -1 +1 @@
1
- {"version":3,"file":"streamfilesink.js","names":["path: string","options: StreamFileSinkOptions","sink: Sink & AsyncDisposable","record: LogRecord"],"sources":["../src/streamfilesink.ts"],"sourcesContent":["import {\n defaultTextFormatter,\n type LogRecord,\n type Sink,\n type TextFormatter,\n} from \"@logtape/logtape\";\nimport { createWriteStream } from \"node:fs\";\nimport { PassThrough } from \"node:stream\";\n\n/**\n * Options for the {@link getStreamFileSink} function.\n *\n * This interface configures the high-performance stream-based file sink that\n * uses Node.js PassThrough streams for optimal I/O performance with automatic\n * backpressure management.\n *\n * @since 1.0.0\n */\nexport interface StreamFileSinkOptions {\n /**\n * High water mark for the PassThrough stream buffer in bytes.\n *\n * This controls the internal buffer size of the PassThrough stream.\n * Higher values can improve performance for high-volume logging but use\n * more memory. Lower values reduce memory usage but may impact performance.\n *\n * @default 16384\n * @since 1.0.0\n */\n readonly highWaterMark?: number;\n\n /**\n * A custom formatter for log records.\n *\n * If not specified, the default text formatter will be used, which formats\n * records in the standard LogTape format with timestamp, level, category,\n * and message.\n *\n * @default defaultTextFormatter\n * @since 1.0.0\n */\n readonly formatter?: TextFormatter;\n}\n\n/**\n * Create a high-performance stream-based file sink that writes log records to a file.\n *\n * This sink uses Node.js PassThrough streams piped to WriteStreams for optimal\n * I/O performance. It leverages the Node.js stream infrastructure to provide\n * automatic backpressure management, efficient buffering, and asynchronous writes\n * without blocking the main thread.\n *\n * ## Performance Characteristics\n *\n * - **High Performance**: Optimized for high-volume logging scenarios\n * - **Non-blocking**: Uses asynchronous I/O that doesn't block the main thread\n * - **Memory Efficient**: Automatic backpressure prevents memory buildup\n * - **Stream-based**: Leverages Node.js native stream optimizations\n *\n * ## When to Use\n *\n * Use this sink when you need:\n * - High-performance file logging for production applications\n * - Non-blocking I/O behavior for real-time applications\n * - Automatic backpressure handling for high-volume scenarios\n * - Simple file output without complex buffering configuration\n *\n * For more control over buffering behavior, consider using {@link getFileSink}\n * instead, which provides options for buffer size, flush intervals, and\n * non-blocking modes.\n *\n * ## Example\n *\n * ```typescript\n * import { configure } from \"@logtape/logtape\";\n * import { getStreamFileSink } from \"@logtape/file\";\n *\n * await configure({\n * sinks: {\n * file: getStreamFileSink(\"app.log\", {\n * highWaterMark: 32768 // 32KB buffer for high-volume logging\n * })\n * },\n * loggers: [\n * { category: [\"myapp\"], sinks: [\"file\"] }\n * ]\n * });\n * ```\n *\n * @param path The path to the file to write logs to. The file will be created\n * if it doesn't exist, or appended to if it does exist.\n * @param options Configuration options for the stream-based sink.\n * @returns A sink that writes formatted log records to the specified file.\n * The returned sink implements `Disposable` for proper resource cleanup.\n *\n * @since 1.0.0\n */\nexport function getStreamFileSink(\n path: string,\n options: StreamFileSinkOptions = {},\n): Sink & AsyncDisposable {\n const highWaterMark = options.highWaterMark ?? 16384;\n const formatter = options.formatter ?? defaultTextFormatter;\n\n // Create PassThrough stream for optimal performance\n const passThrough = new PassThrough({\n highWaterMark,\n objectMode: false,\n });\n\n // Create WriteStream immediately (not lazy)\n const writeStream = createWriteStream(path, { flags: \"a\" });\n\n // Pipe PassThrough to WriteStream for automatic backpressure handling\n passThrough.pipe(writeStream);\n\n let disposed = false;\n\n // Stream-based sink function for high performance\n const sink: Sink & AsyncDisposable = (record: LogRecord) => {\n if (disposed) return;\n\n // Direct write to PassThrough stream\n passThrough.write(formatter(record));\n };\n\n // Asynchronous disposal with sequential stream closure\n sink[Symbol.asyncDispose] = async () => {\n if (disposed) return;\n disposed = true;\n\n // Wait for PassThrough to drain if needed\n if (passThrough.writableNeedDrain) {\n await new Promise<void>((resolve) => {\n passThrough.once(\"drain\", resolve);\n });\n }\n\n // End the PassThrough stream first and wait for it to finish\n await new Promise<void>((resolve) => {\n passThrough.once(\"finish\", resolve);\n passThrough.end();\n });\n\n // Wait for WriteStream to finish and close (piped streams auto-close)\n await new Promise<void>((resolve) => {\n if (writeStream.closed || writeStream.destroyed) {\n resolve();\n return;\n }\n writeStream.once(\"close\", resolve);\n });\n };\n\n return sink;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiGA,SAAgB,kBACdA,MACAC,UAAiC,CAAE,GACX;CACxB,MAAM,gBAAgB,QAAQ,iBAAiB;CAC/C,MAAM,YAAY,QAAQ,aAAa;CAGvC,MAAM,cAAc,IAAI,YAAY;EAClC;EACA,YAAY;CACb;CAGD,MAAM,cAAc,kBAAkB,MAAM,EAAE,OAAO,IAAK,EAAC;AAG3D,aAAY,KAAK,YAAY;CAE7B,IAAI,WAAW;CAGf,MAAMC,OAA+B,CAACC,WAAsB;AAC1D,MAAI,SAAU;AAGd,cAAY,MAAM,UAAU,OAAO,CAAC;CACrC;AAGD,MAAK,OAAO,gBAAgB,YAAY;AACtC,MAAI,SAAU;AACd,aAAW;AAGX,MAAI,YAAY,kBACd,OAAM,IAAI,QAAc,CAAC,YAAY;AACnC,eAAY,KAAK,SAAS,QAAQ;EACnC;AAIH,QAAM,IAAI,QAAc,CAAC,YAAY;AACnC,eAAY,KAAK,UAAU,QAAQ;AACnC,eAAY,KAAK;EAClB;AAGD,QAAM,IAAI,QAAc,CAAC,YAAY;AACnC,OAAI,YAAY,UAAU,YAAY,WAAW;AAC/C,aAAS;AACT;GACD;AACD,eAAY,KAAK,SAAS,QAAQ;EACnC;CACF;AAED,QAAO;AACR"}
1
+ {"version":3,"file":"streamfilesink.js","names":["path: string","options: StreamFileSinkOptions","sink: Sink & AsyncDisposable","record: LogRecord"],"sources":["../src/streamfilesink.ts"],"sourcesContent":["import {\n defaultTextFormatter,\n type LogRecord,\n type Sink,\n type TextFormatter,\n} from \"@logtape/logtape\";\nimport { createWriteStream } from \"node:fs\";\nimport { PassThrough } from \"node:stream\";\n\n/**\n * Options for the {@link getStreamFileSink} function.\n *\n * This interface configures the high-performance stream-based file sink that\n * uses Node.js PassThrough streams for optimal I/O performance with automatic\n * backpressure management.\n *\n * @since 1.0.0\n */\nexport interface StreamFileSinkOptions {\n /**\n * High water mark for the PassThrough stream buffer in bytes.\n *\n * This controls the internal buffer size of the PassThrough stream.\n * Higher values can improve performance for high-volume logging but use\n * more memory. Lower values reduce memory usage but may impact performance.\n *\n * @default 16384\n * @since 1.0.0\n */\n readonly highWaterMark?: number;\n\n /**\n * A custom formatter for log records.\n *\n * If not specified, the default text formatter will be used, which formats\n * records in the standard LogTape format with timestamp, level, category,\n * and message.\n *\n * @default defaultTextFormatter\n * @since 1.0.0\n */\n readonly formatter?: TextFormatter;\n}\n\n/**\n * Create a high-performance stream-based file sink that writes log records to a file.\n *\n * This sink uses Node.js PassThrough streams piped to WriteStreams for optimal\n * I/O performance. It leverages the Node.js stream infrastructure to provide\n * automatic backpressure management, efficient buffering, and asynchronous writes\n * without blocking the main thread.\n *\n * ## Performance Characteristics\n *\n * - **High Performance**: Optimized for high-volume logging scenarios\n * - **Non-blocking**: Uses asynchronous I/O that doesn't block the main thread\n * - **Memory Efficient**: Automatic backpressure prevents memory buildup\n * - **Stream-based**: Leverages Node.js native stream optimizations\n *\n * ## When to Use\n *\n * Use this sink when you need:\n * - High-performance file logging for production applications\n * - Non-blocking I/O behavior for real-time applications\n * - Automatic backpressure handling for high-volume scenarios\n * - Simple file output without complex buffering configuration\n *\n * For more control over buffering behavior, consider using {@link getFileSink}\n * instead, which provides options for buffer size, flush intervals, and\n * non-blocking modes.\n *\n * ## Example\n *\n * ```typescript\n * import { configure } from \"@logtape/logtape\";\n * import { getStreamFileSink } from \"@logtape/file\";\n *\n * await configure({\n * sinks: {\n * file: getStreamFileSink(\"app.log\", {\n * highWaterMark: 32768 // 32KB buffer for high-volume logging\n * })\n * },\n * loggers: [\n * { category: [\"myapp\"], sinks: [\"file\"] }\n * ]\n * });\n * ```\n *\n * @param path The path to the file to write logs to. The file will be created\n * if it doesn't exist, or appended to if it does exist.\n * @param options Configuration options for the stream-based sink.\n * @returns A sink that writes formatted log records to the specified file.\n * The returned sink implements `AsyncDisposable` for proper resource cleanup\n * that waits for all data to be flushed to disk.\n *\n * @since 1.0.0\n */\nexport function getStreamFileSink(\n path: string,\n options: StreamFileSinkOptions = {},\n): Sink & AsyncDisposable {\n const highWaterMark = options.highWaterMark ?? 16384;\n const formatter = options.formatter ?? defaultTextFormatter;\n\n // Create PassThrough stream for optimal performance\n const passThrough = new PassThrough({\n highWaterMark,\n objectMode: false,\n });\n\n // Create WriteStream immediately (not lazy)\n const writeStream = createWriteStream(path, { flags: \"a\" });\n\n // Pipe PassThrough to WriteStream for automatic backpressure handling\n passThrough.pipe(writeStream);\n\n let disposed = false;\n\n // Stream-based sink function for high performance\n const sink: Sink & AsyncDisposable = (record: LogRecord) => {\n if (disposed) return;\n\n // Direct write to PassThrough stream\n passThrough.write(formatter(record));\n };\n\n // Asynchronous disposal with sequential stream closure\n sink[Symbol.asyncDispose] = async () => {\n if (disposed) return;\n disposed = true;\n\n // Wait for PassThrough to drain if needed\n if (passThrough.writableNeedDrain) {\n await new Promise<void>((resolve) => {\n passThrough.once(\"drain\", resolve);\n });\n }\n\n // End the PassThrough stream first and wait for it to finish\n await new Promise<void>((resolve) => {\n passThrough.once(\"finish\", resolve);\n passThrough.end();\n });\n\n // Wait for WriteStream to finish and close (piped streams auto-close)\n await new Promise<void>((resolve) => {\n if (writeStream.closed || writeStream.destroyed) {\n resolve();\n return;\n }\n writeStream.once(\"close\", resolve);\n });\n };\n\n return sink;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkGA,SAAgB,kBACdA,MACAC,UAAiC,CAAE,GACX;CACxB,MAAM,gBAAgB,QAAQ,iBAAiB;CAC/C,MAAM,YAAY,QAAQ,aAAa;CAGvC,MAAM,cAAc,IAAI,YAAY;EAClC;EACA,YAAY;CACb;CAGD,MAAM,cAAc,kBAAkB,MAAM,EAAE,OAAO,IAAK,EAAC;AAG3D,aAAY,KAAK,YAAY;CAE7B,IAAI,WAAW;CAGf,MAAMC,OAA+B,CAACC,WAAsB;AAC1D,MAAI,SAAU;AAGd,cAAY,MAAM,UAAU,OAAO,CAAC;CACrC;AAGD,MAAK,OAAO,gBAAgB,YAAY;AACtC,MAAI,SAAU;AACd,aAAW;AAGX,MAAI,YAAY,kBACd,OAAM,IAAI,QAAc,CAAC,YAAY;AACnC,eAAY,KAAK,SAAS,QAAQ;EACnC;AAIH,QAAM,IAAI,QAAc,CAAC,YAAY;AACnC,eAAY,KAAK,UAAU,QAAQ;AACnC,eAAY,KAAK;EAClB;AAGD,QAAM,IAAI,QAAc,CAAC,YAAY;AACnC,OAAI,YAAY,UAAU,YAAY,WAAW;AAC/C,aAAS;AACT;GACD;AACD,eAAY,KAAK,SAAS,QAAQ;EACnC;CACF;AAED,QAAO;AACR"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/file",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "File sink and rotating file sink for LogTape",
5
5
  "keywords": [
6
6
  "logging",
@@ -57,7 +57,7 @@
57
57
  },
58
58
  "sideEffects": false,
59
59
  "peerDependencies": {
60
- "@logtape/logtape": "^1.1.1"
60
+ "@logtape/logtape": "^1.1.2"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@alinea/suite": "^0.6.3",
@@ -138,8 +138,6 @@ test("getStreamFileSink() disposal stops writing", async () => {
138
138
  sink(info);
139
139
  sink(warning);
140
140
 
141
- await delay(50);
142
-
143
141
  const content = fs.readFileSync(path, { encoding: "utf-8" });
144
142
  const lines = content.split("\n").filter((line) => line.length > 0);
145
143
  assertEquals(lines.length, 1); // Only debug record
@@ -91,7 +91,8 @@ export interface StreamFileSinkOptions {
91
91
  * if it doesn't exist, or appended to if it does exist.
92
92
  * @param options Configuration options for the stream-based sink.
93
93
  * @returns A sink that writes formatted log records to the specified file.
94
- * The returned sink implements `Disposable` for proper resource cleanup.
94
+ * The returned sink implements `AsyncDisposable` for proper resource cleanup
95
+ * that waits for all data to be flushed to disk.
95
96
  *
96
97
  * @since 1.0.0
97
98
  */