@elgato/utils 0.7.0 → 0.7.1

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.
@@ -11,9 +11,17 @@ export declare class FileTarget implements LogTarget {
11
11
  */
12
12
  constructor(options: FileTargetOptions);
13
13
  /**
14
- * @inheritdoc
14
+ * Writes the log entry to the log file. A log entry that cannot be written, for example because the disk is
15
+ * full or the file is read-only, is dropped rather than thrown to the caller; the number of dropped entries
16
+ * is written to the log file once writing succeeds again.
17
+ * @param entry The log entry to write.
15
18
  */
16
19
  write(entry: LogEntry): void;
20
+ /**
21
+ * Gets the log entry that reports the number of log entries that were dropped.
22
+ * @returns The log entry.
23
+ */
24
+ private getDroppedEntry;
17
25
  /**
18
26
  * Gets the file path to an indexed log file.
19
27
  * @param index Optional index of the log file to be included as part of the file name.
@@ -31,6 +39,12 @@ export declare class FileTarget implements LogTarget {
31
39
  * for a new log file.
32
40
  */
33
41
  private reIndex;
42
+ /**
43
+ * Appends the data to the log file.
44
+ * @param data Data to append.
45
+ * @returns `true` when the data was written; otherwise `false`.
46
+ */
47
+ private tryAppend;
34
48
  }
35
49
  /**
36
50
  * Options that determine the behavior of a {@link FileTarget}.
@@ -16,6 +16,10 @@ export class FileTarget {
16
16
  * Current size of the logs that have been written to the {@link FileTarget.#filePath}.
17
17
  */
18
18
  #size = 0;
19
+ /**
20
+ * Number of log entries that could not be written, and have not yet been reported in the log file.
21
+ */
22
+ #dropped = 0;
19
23
  /**
20
24
  * Initializes a new instance of the {@link FileTarget} class.
21
25
  * @param options Options that defines how logs should be written to the local file system.
@@ -26,23 +30,41 @@ export class FileTarget {
26
30
  this.reIndex();
27
31
  }
28
32
  /**
29
- * @inheritdoc
33
+ * Writes the log entry to the log file. A log entry that cannot be written, for example because the disk is
34
+ * full or the file is read-only, is dropped rather than thrown to the caller; the number of dropped entries
35
+ * is written to the log file once writing succeeds again.
36
+ * @param entry The log entry to write.
30
37
  */
31
38
  write(entry) {
32
- const fd = fs.openSync(this.#filePath, "a");
33
- try {
34
- const msg = this.#options.format(entry);
35
- fs.writeSync(fd, msg + "\n");
36
- this.#size += msg.length;
39
+ let msg = this.#options.format(entry);
40
+ if (this.#dropped > 0) {
41
+ msg = `${this.#options.format(this.getDroppedEntry())}\n${msg}`;
37
42
  }
38
- finally {
39
- fs.closeSync(fd);
43
+ if (!this.tryAppend(msg + "\n")) {
44
+ this.#dropped++;
45
+ return;
40
46
  }
47
+ this.#dropped = 0;
48
+ this.#size += msg.length;
41
49
  if (this.#size >= this.#options.maxSize) {
42
50
  this.reIndex();
43
51
  this.#size = 0;
44
52
  }
45
53
  }
54
+ /**
55
+ * Gets the log entry that reports the number of log entries that were dropped.
56
+ * @returns The log entry.
57
+ */
58
+ getDroppedEntry() {
59
+ const count = this.#dropped;
60
+ return {
61
+ data: [
62
+ `${count} log ${count === 1 ? "entry was" : "entries were"} dropped because the log file could not be written.`,
63
+ ],
64
+ level: "warn",
65
+ scope: "",
66
+ };
67
+ }
46
68
  /**
47
69
  * Gets the file path to an indexed log file.
48
70
  * @param index Optional index of the log file to be included as part of the file name.
@@ -104,5 +126,31 @@ export class FileTarget {
104
126
  }
105
127
  }
106
128
  }
129
+ /**
130
+ * Appends the data to the log file.
131
+ * @param data Data to append.
132
+ * @returns `true` when the data was written; otherwise `false`.
133
+ */
134
+ tryAppend(data) {
135
+ let fd;
136
+ try {
137
+ fd = fs.openSync(this.#filePath, "a");
138
+ fs.writeSync(fd, data);
139
+ return true;
140
+ }
141
+ catch {
142
+ return false;
143
+ }
144
+ finally {
145
+ if (fd !== undefined) {
146
+ try {
147
+ fs.closeSync(fd);
148
+ }
149
+ catch {
150
+ // The data has been written, and the file descriptor cannot be used again.
151
+ }
152
+ }
153
+ }
154
+ }
107
155
  }
108
156
  //# sourceMappingURL=file-target.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elgato/utils",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Utilities used throughout the Elgato ecosystem.",
5
5
  "keywords": [
6
6
  "elgato"
@@ -45,7 +45,7 @@
45
45
  }
46
46
  },
47
47
  "dependencies": {
48
- "zod": "^3.25.24"
48
+ "zod": "^4.6.7"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@elgato/eslint-config": "^0.3.2",