@logtape/file 1.3.0 → 1.3.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.
@@ -1,122 +0,0 @@
1
- import type { Sink } from "@logtape/logtape";
2
- import {
3
- type AsyncRotatingFileSinkDriver,
4
- type FileSinkOptions,
5
- getBaseFileSink,
6
- getBaseRotatingFileSink,
7
- type RotatingFileSinkDriver,
8
- type RotatingFileSinkOptions,
9
- } from "./filesink.base.ts";
10
-
11
- /**
12
- * A Deno-specific file sink driver.
13
- */
14
- export const denoDriver: RotatingFileSinkDriver<Deno.FsFile> = {
15
- openSync(path: string) {
16
- return Deno.openSync(path, { create: true, append: true });
17
- },
18
- writeSync(fd, chunk) {
19
- fd.writeSync(chunk);
20
- },
21
- writeManySync(fd: Deno.FsFile, chunks: Uint8Array[]): void {
22
- // Deno doesn't have writev, but we can optimize by writing all chunks
23
- // then doing a single sync operation
24
- for (const chunk of chunks) {
25
- fd.writeSync(chunk);
26
- }
27
- },
28
- flushSync(fd) {
29
- fd.syncSync();
30
- },
31
- closeSync(fd) {
32
- fd.close();
33
- },
34
- statSync: globalThis?.Deno.statSync,
35
- renameSync: globalThis?.Deno.renameSync,
36
- };
37
-
38
- /**
39
- * A Deno-specific async file sink driver.
40
- * @since 1.0.0
41
- */
42
- export const denoAsyncDriver: AsyncRotatingFileSinkDriver<Deno.FsFile> = {
43
- ...denoDriver,
44
- async writeMany(fd: Deno.FsFile, chunks: Uint8Array[]): Promise<void> {
45
- // Deno doesn't have async writev, but we can write all chunks
46
- // then do a single async sync
47
- for (const chunk of chunks) {
48
- await fd.write(chunk);
49
- }
50
- },
51
- async flush(fd) {
52
- await fd.sync();
53
- },
54
- close(fd) {
55
- return Promise.resolve(fd.close());
56
- },
57
- };
58
-
59
- /**
60
- * Get a file sink.
61
- *
62
- * Note that this function is unavailable in the browser.
63
- *
64
- * @param path A path to the file to write to.
65
- * @param options The options for the sink.
66
- * @returns A sink that writes to the file. The sink is also a disposable
67
- * object that closes the file when disposed. If `nonBlocking` is enabled,
68
- * returns a sink that also implements {@link AsyncDisposable}.
69
- */
70
- export function getFileSink(
71
- path: string,
72
- options?: FileSinkOptions,
73
- ): Sink & Disposable;
74
- export function getFileSink(
75
- path: string,
76
- options: FileSinkOptions & { nonBlocking: true },
77
- ): Sink & AsyncDisposable;
78
- export function getFileSink(
79
- path: string,
80
- options: FileSinkOptions = {},
81
- ): Sink & (Disposable | AsyncDisposable) {
82
- if (options.nonBlocking) {
83
- return getBaseFileSink(path, { ...options, ...denoAsyncDriver });
84
- }
85
- return getBaseFileSink(path, { ...options, ...denoDriver });
86
- }
87
-
88
- /**
89
- * Get a rotating file sink.
90
- *
91
- * This sink writes log records to a file, and rotates the file when it reaches
92
- * the `maxSize`. The rotated files are named with the original file name
93
- * followed by a dot and a number, starting from 1. The number is incremented
94
- * for each rotation, and the maximum number of files to keep is `maxFiles`.
95
- *
96
- * Note that this function is unavailable in the browser.
97
- *
98
- * @param path A path to the file to write to.
99
- * @param options The options for the sink and the file driver.
100
- * @returns A sink that writes to the file. The sink is also a disposable
101
- * object that closes the file when disposed. If `nonBlocking` is enabled,
102
- * returns a sink that also implements {@link AsyncDisposable}.
103
- */
104
- export function getRotatingFileSink(
105
- path: string,
106
- options?: RotatingFileSinkOptions,
107
- ): Sink & Disposable;
108
- export function getRotatingFileSink(
109
- path: string,
110
- options: RotatingFileSinkOptions & { nonBlocking: true },
111
- ): Sink & AsyncDisposable;
112
- export function getRotatingFileSink(
113
- path: string,
114
- options: RotatingFileSinkOptions = {},
115
- ): Sink & (Disposable | AsyncDisposable) {
116
- if (options.nonBlocking) {
117
- return getBaseRotatingFileSink(path, { ...options, ...denoAsyncDriver });
118
- }
119
- return getBaseRotatingFileSink(path, { ...options, ...denoDriver });
120
- }
121
-
122
- // cSpell: ignore filesink
@@ -1,77 +0,0 @@
1
- import type { Sink } from "@logtape/logtape";
2
- import type {
3
- FileSinkOptions,
4
- RotatingFileSinkOptions,
5
- } from "./filesink.base.ts";
6
-
7
- const filesink: Omit<
8
- typeof import("./filesink.deno.ts"),
9
- "denoDriver" | "denoAsyncDriver"
10
- > =
11
- // dnt-shim-ignore
12
- await ("Deno" in globalThis
13
- ? import("./filesink.deno.ts")
14
- : import("./filesink.node.ts"));
15
-
16
- /**
17
- * Get a file sink.
18
- *
19
- * Note that this function is unavailable in the browser.
20
- *
21
- * @param path A path to the file to write to.
22
- * @param options The options for the sink.
23
- * @returns A sink that writes to the file. The sink is also a disposable
24
- * object that closes the file when disposed. If `nonBlocking` is enabled,
25
- * returns a sink that also implements {@link AsyncDisposable}.
26
- */
27
- export function getFileSink(
28
- path: string,
29
- options?: FileSinkOptions,
30
- ): Sink & Disposable;
31
- export function getFileSink(
32
- path: string,
33
- options: FileSinkOptions & { nonBlocking: true },
34
- ): Sink & AsyncDisposable;
35
- export function getFileSink(
36
- path: string,
37
- options: FileSinkOptions = {},
38
- ): Sink & (Disposable | AsyncDisposable) {
39
- return filesink.getFileSink(path, options) as
40
- & Sink
41
- & (Disposable | AsyncDisposable);
42
- }
43
-
44
- /**
45
- * Get a rotating file sink.
46
- *
47
- * This sink writes log records to a file, and rotates the file when it reaches
48
- * the `maxSize`. The rotated files are named with the original file name
49
- * followed by a dot and a number, starting from 1. The number is incremented
50
- * for each rotation, and the maximum number of files to keep is `maxFiles`.
51
- *
52
- * Note that this function is unavailable in the browser.
53
- *
54
- * @param path A path to the file to write to.
55
- * @param options The options for the sink and the file driver.
56
- * @returns A sink that writes to the file. The sink is also a disposable
57
- * object that closes the file when disposed. If `nonBlocking` is enabled,
58
- * returns a sink that also implements {@link AsyncDisposable}.
59
- */
60
- export function getRotatingFileSink(
61
- path: string,
62
- options?: RotatingFileSinkOptions,
63
- ): Sink & Disposable;
64
- export function getRotatingFileSink(
65
- path: string,
66
- options: RotatingFileSinkOptions & { nonBlocking: true },
67
- ): Sink & AsyncDisposable;
68
- export function getRotatingFileSink(
69
- path: string,
70
- options: RotatingFileSinkOptions = {},
71
- ): Sink & (Disposable | AsyncDisposable) {
72
- return filesink.getRotatingFileSink(path, options) as
73
- & Sink
74
- & (Disposable | AsyncDisposable);
75
- }
76
-
77
- // cSpell: ignore filesink
@@ -1,118 +0,0 @@
1
- import type { Sink } from "@logtape/logtape";
2
- import fs from "node:fs";
3
- import { promisify } from "node:util";
4
- import {
5
- type AsyncRotatingFileSinkDriver,
6
- type FileSinkOptions,
7
- getBaseFileSink,
8
- getBaseRotatingFileSink,
9
- type RotatingFileSinkDriver,
10
- type RotatingFileSinkOptions,
11
- } from "./filesink.base.ts";
12
-
13
- /**
14
- * A Node.js-specific file sink driver.
15
- */
16
- export const nodeDriver: RotatingFileSinkDriver<number | void> = {
17
- openSync(path: string) {
18
- return fs.openSync(path, "a");
19
- },
20
- writeSync: fs.writeSync,
21
- writeManySync(fd: number, chunks: Uint8Array[]): void {
22
- if (chunks.length === 0) return;
23
- if (chunks.length === 1) {
24
- fs.writeSync(fd, chunks[0]);
25
- return;
26
- }
27
- // Use writev for multiple chunks
28
- fs.writevSync(fd, chunks);
29
- },
30
- flushSync: fs.fsyncSync,
31
- closeSync: fs.closeSync,
32
- statSync: fs.statSync,
33
- renameSync: fs.renameSync,
34
- };
35
-
36
- /**
37
- * A Node.js-specific async file sink driver.
38
- * @since 1.0.0
39
- */
40
- export const nodeAsyncDriver: AsyncRotatingFileSinkDriver<number | void> = {
41
- ...nodeDriver,
42
- async writeMany(fd: number, chunks: Uint8Array[]): Promise<void> {
43
- if (chunks.length === 0) return;
44
- if (chunks.length === 1) {
45
- await promisify(fs.write)(fd, chunks[0]);
46
- return;
47
- }
48
- // Use async writev for multiple chunks
49
- await promisify(fs.writev)(fd, chunks);
50
- },
51
- flush: promisify(fs.fsync),
52
- close: promisify(fs.close),
53
- };
54
-
55
- /**
56
- * Get a file sink.
57
- *
58
- * Note that this function is unavailable in the browser.
59
- *
60
- * @param path A path to the file to write to.
61
- * @param options The options for the sink.
62
- * @returns A sink that writes to the file. The sink is also a disposable
63
- * object that closes the file when disposed. If `nonBlocking` is enabled,
64
- * returns a sink that also implements {@link AsyncDisposable}.
65
- */
66
- export function getFileSink(
67
- path: string,
68
- options?: FileSinkOptions,
69
- ): Sink & Disposable;
70
- export function getFileSink(
71
- path: string,
72
- options: FileSinkOptions & { nonBlocking: true },
73
- ): Sink & AsyncDisposable;
74
- export function getFileSink(
75
- path: string,
76
- options: FileSinkOptions = {},
77
- ): Sink & (Disposable | AsyncDisposable) {
78
- if (options.nonBlocking) {
79
- return getBaseFileSink(path, { ...options, ...nodeAsyncDriver });
80
- }
81
- return getBaseFileSink(path, { ...options, ...nodeDriver });
82
- }
83
-
84
- /**
85
- * Get a rotating file sink.
86
- *
87
- * This sink writes log records to a file, and rotates the file when it reaches
88
- * the `maxSize`. The rotated files are named with the original file name
89
- * followed by a dot and a number, starting from 1. The number is incremented
90
- * for each rotation, and the maximum number of files to keep is `maxFiles`.
91
- *
92
- * Note that this function is unavailable in the browser.
93
- *
94
- * @param path A path to the file to write to.
95
- * @param options The options for the sink and the file driver.
96
- * @returns A sink that writes to the file. The sink is also a disposable
97
- * object that closes the file when disposed. If `nonBlocking` is enabled,
98
- * returns a sink that also implements {@link AsyncDisposable}.
99
- */
100
- export function getRotatingFileSink(
101
- path: string,
102
- options?: RotatingFileSinkOptions,
103
- ): Sink & Disposable;
104
- export function getRotatingFileSink(
105
- path: string,
106
- options: RotatingFileSinkOptions & { nonBlocking: true },
107
- ): Sink & AsyncDisposable;
108
- export function getRotatingFileSink(
109
- path: string,
110
- options: RotatingFileSinkOptions = {},
111
- ): Sink & (Disposable | AsyncDisposable) {
112
- if (options.nonBlocking) {
113
- return getBaseRotatingFileSink(path, { ...options, ...nodeAsyncDriver });
114
- }
115
- return getBaseRotatingFileSink(path, { ...options, ...nodeDriver });
116
- }
117
-
118
- // cSpell: ignore filesink