@c9up/spectrum 0.1.11 → 0.1.12
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.
|
@@ -18,6 +18,14 @@ export declare class FileChannel implements LogChannel {
|
|
|
18
18
|
name: string;
|
|
19
19
|
constructor(config: FileChannelConfig);
|
|
20
20
|
write(entry: LogEntry): void;
|
|
21
|
+
/**
|
|
22
|
+
* Stop writing, for good.
|
|
23
|
+
*
|
|
24
|
+
* Anything buffered by a rotation goes out first — those lines were accepted
|
|
25
|
+
* and dropping them at close loses log the caller believed was written — and
|
|
26
|
+
* the flag is set before the flush so nothing the flush triggers can reopen
|
|
27
|
+
* the file.
|
|
28
|
+
*/
|
|
21
29
|
close(): void;
|
|
22
30
|
}
|
|
23
31
|
//# sourceMappingURL=FileChannel.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FileChannel.d.ts","sourceRoot":"","sources":["../../src/channels/FileChannel.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAExD,MAAM,WAAW,iBAAiB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,WAAY,YAAW,UAAU;;IAC7C,IAAI,SAAU;
|
|
1
|
+
{"version":3,"file":"FileChannel.d.ts","sourceRoot":"","sources":["../../src/channels/FileChannel.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAExD,MAAM,WAAW,iBAAiB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,WAAY,YAAW,UAAU;;IAC7C,IAAI,SAAU;gBAkBF,MAAM,EAAE,iBAAiB;IAMrC,KAAK,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI;IA2I5B;;;;;;;OAOG;IACH,KAAK,IAAI,IAAI;CAoCb"}
|
|
@@ -19,6 +19,14 @@ export class FileChannel {
|
|
|
19
19
|
#currentSize = 0;
|
|
20
20
|
#dirReady = false;
|
|
21
21
|
#rotating = false;
|
|
22
|
+
/**
|
|
23
|
+
* Terminal once `close()` has run.
|
|
24
|
+
*
|
|
25
|
+
* Nulling the stream was not enough: `write()` reopens a null stream, and so
|
|
26
|
+
* does a rotation, so a line written after close silently reopened the file
|
|
27
|
+
* and appended to it. A closed channel stays closed.
|
|
28
|
+
*/
|
|
29
|
+
#closed = false;
|
|
22
30
|
#pending = [];
|
|
23
31
|
constructor(config) {
|
|
24
32
|
this.#filePath = config.path;
|
|
@@ -26,6 +34,8 @@ export class FileChannel {
|
|
|
26
34
|
this.#maxFiles = config.maxFiles ?? 5;
|
|
27
35
|
}
|
|
28
36
|
write(entry) {
|
|
37
|
+
if (this.#closed)
|
|
38
|
+
return;
|
|
29
39
|
const line = `${JSON.stringify(entry)}\n`;
|
|
30
40
|
const bytes = Buffer.byteLength(line, "utf8");
|
|
31
41
|
if (this.#rotating) {
|
|
@@ -63,6 +73,9 @@ export class FileChannel {
|
|
|
63
73
|
}
|
|
64
74
|
}
|
|
65
75
|
#openStream() {
|
|
76
|
+
// A rotation in flight when `close()` lands must not bring the file back.
|
|
77
|
+
if (this.#closed)
|
|
78
|
+
return;
|
|
66
79
|
// mkdirSync + statSync only on first open (boot) — never on the hot path.
|
|
67
80
|
// The fast write path goes straight through this.#stream.write() (non-blocking).
|
|
68
81
|
if (!this.#dirReady) {
|
|
@@ -82,7 +95,7 @@ export class FileChannel {
|
|
|
82
95
|
});
|
|
83
96
|
}
|
|
84
97
|
#scheduleRotation() {
|
|
85
|
-
if (this.#rotating)
|
|
98
|
+
if (this.#rotating || this.#closed)
|
|
86
99
|
return;
|
|
87
100
|
this.#rotating = true;
|
|
88
101
|
queueMicrotask(() => {
|
|
@@ -151,10 +164,49 @@ export class FileChannel {
|
|
|
151
164
|
stream.end(() => resolve());
|
|
152
165
|
});
|
|
153
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Stop writing, for good.
|
|
169
|
+
*
|
|
170
|
+
* Anything buffered by a rotation goes out first — those lines were accepted
|
|
171
|
+
* and dropping them at close loses log the caller believed was written — and
|
|
172
|
+
* the flag is set before the flush so nothing the flush triggers can reopen
|
|
173
|
+
* the file.
|
|
174
|
+
*/
|
|
154
175
|
close() {
|
|
176
|
+
if (this.#closed)
|
|
177
|
+
return;
|
|
178
|
+
const pending = this.#pending.splice(0);
|
|
179
|
+
this.#closed = true;
|
|
155
180
|
if (this.#stream) {
|
|
181
|
+
for (const line of pending)
|
|
182
|
+
this.#stream.write(line);
|
|
156
183
|
this.#stream.end();
|
|
157
184
|
this.#stream = null;
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
// No stream, because a rotation is in flight: it nulls the stream before
|
|
188
|
+
// awaiting the old one's end, and it will not reopen now that #closed is
|
|
189
|
+
// set. Writing the buffer to the stream that no longer exists is how
|
|
190
|
+
// those lines used to disappear — accepted by write(), then dropped.
|
|
191
|
+
//
|
|
192
|
+
// Appended directly instead, and synchronously, because close() is: the
|
|
193
|
+
// path is the current log either way, whether the rotation has renamed
|
|
194
|
+
// it yet or not.
|
|
195
|
+
this.#appendDirectly(pending);
|
|
196
|
+
}
|
|
197
|
+
/** Last-resort write for lines with no stream left to carry them. */
|
|
198
|
+
#appendDirectly(lines) {
|
|
199
|
+
if (lines.length === 0)
|
|
200
|
+
return;
|
|
201
|
+
try {
|
|
202
|
+
if (!this.#dirReady) {
|
|
203
|
+
fs.mkdirSync(path.dirname(this.#filePath), { recursive: true });
|
|
204
|
+
this.#dirReady = true;
|
|
205
|
+
}
|
|
206
|
+
fs.appendFileSync(this.#filePath, lines.join(""));
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
process.stderr.write(`[Spectrum] FileChannel: ${lines.length} buffered line(s) lost closing '${this.#filePath}'\n`);
|
|
158
210
|
}
|
|
159
211
|
}
|
|
160
212
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FileChannel.js","sourceRoot":"","sources":["../../src/channels/FileChannel.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,GAAG,MAAM,kBAAkB,CAAC;AACxC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AASlC,MAAM,OAAO,WAAW;IACvB,IAAI,GAAG,MAAM,CAAC;IACd,SAAS,CAAS;IAClB,QAAQ,CAAS;IACjB,SAAS,CAAS;IAClB,OAAO,GAA0B,IAAI,CAAC;IACtC,YAAY,GAAG,CAAC,CAAC;IACjB,SAAS,GAAG,KAAK,CAAC;IAClB,SAAS,GAAG,KAAK,CAAC;IAClB,QAAQ,GAAa,EAAE,CAAC;IAExB,YAAY,MAAyB;QACpC,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC;QAC7B,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,YAAY,IAAI,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;QACxD,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,QAAQ,IAAI,CAAC,CAAC;IACvC,CAAC;IAED,KAAK,CAAC,KAAe;QACpB,MAAM,IAAI,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC;QAC1C,MAAM,KAAK,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAE9C,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACzB,OAAO;QACR,CAAC;QAED,IAAI,C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CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAClB,KAAK,MAAM,IAAI,IAAI,OAAO;gBAAE,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACrD,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC;YACnB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;YACpB,OAAO;QACR,CAAC;QACD,yEAAyE;QACzE,yEAAyE;QACzE,qEAAqE;QACrE,qEAAqE;QACrE,EAAE;QACF,wEAAwE;QACxE,uEAAuE;QACvE,iBAAiB;QACjB,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;IAC/B,CAAC;IAED,qEAAqE;IACrE,eAAe,CAAC,KAAe;QAC9B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO;QAC/B,IAAI,CAAC;YACJ,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;gBACrB,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;gBAChE,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;YACvB,CAAC;YACD,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;QACnD,CAAC;QAAC,MAAM,CAAC;YACR,OAAO,CAAC,MAAM,CAAC,KAAK,CACnB,2BAA2B,KAAK,CAAC,MAAM,mCAAmC,IAAI,CAAC,SAAS,KAAK,CAC7F,CAAC;QACH,CAAC;IACF,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -28,6 +28,14 @@ export class FileChannel implements LogChannel {
|
|
|
28
28
|
#currentSize = 0;
|
|
29
29
|
#dirReady = false;
|
|
30
30
|
#rotating = false;
|
|
31
|
+
/**
|
|
32
|
+
* Terminal once `close()` has run.
|
|
33
|
+
*
|
|
34
|
+
* Nulling the stream was not enough: `write()` reopens a null stream, and so
|
|
35
|
+
* does a rotation, so a line written after close silently reopened the file
|
|
36
|
+
* and appended to it. A closed channel stays closed.
|
|
37
|
+
*/
|
|
38
|
+
#closed = false;
|
|
31
39
|
#pending: string[] = [];
|
|
32
40
|
|
|
33
41
|
constructor(config: FileChannelConfig) {
|
|
@@ -37,6 +45,7 @@ export class FileChannel implements LogChannel {
|
|
|
37
45
|
}
|
|
38
46
|
|
|
39
47
|
write(entry: LogEntry): void {
|
|
48
|
+
if (this.#closed) return;
|
|
40
49
|
const line = `${JSON.stringify(entry)}\n`;
|
|
41
50
|
const bytes = Buffer.byteLength(line, "utf8");
|
|
42
51
|
|
|
@@ -80,6 +89,8 @@ export class FileChannel implements LogChannel {
|
|
|
80
89
|
}
|
|
81
90
|
|
|
82
91
|
#openStream(): void {
|
|
92
|
+
// A rotation in flight when `close()` lands must not bring the file back.
|
|
93
|
+
if (this.#closed) return;
|
|
83
94
|
// mkdirSync + statSync only on first open (boot) — never on the hot path.
|
|
84
95
|
// The fast write path goes straight through this.#stream.write() (non-blocking).
|
|
85
96
|
if (!this.#dirReady) {
|
|
@@ -101,7 +112,7 @@ export class FileChannel implements LogChannel {
|
|
|
101
112
|
}
|
|
102
113
|
|
|
103
114
|
#scheduleRotation(): void {
|
|
104
|
-
if (this.#rotating) return;
|
|
115
|
+
if (this.#rotating || this.#closed) return;
|
|
105
116
|
this.#rotating = true;
|
|
106
117
|
queueMicrotask(() => {
|
|
107
118
|
this.#rotate().finally(() => {
|
|
@@ -172,10 +183,48 @@ export class FileChannel implements LogChannel {
|
|
|
172
183
|
});
|
|
173
184
|
}
|
|
174
185
|
|
|
186
|
+
/**
|
|
187
|
+
* Stop writing, for good.
|
|
188
|
+
*
|
|
189
|
+
* Anything buffered by a rotation goes out first — those lines were accepted
|
|
190
|
+
* and dropping them at close loses log the caller believed was written — and
|
|
191
|
+
* the flag is set before the flush so nothing the flush triggers can reopen
|
|
192
|
+
* the file.
|
|
193
|
+
*/
|
|
175
194
|
close(): void {
|
|
195
|
+
if (this.#closed) return;
|
|
196
|
+
const pending = this.#pending.splice(0);
|
|
197
|
+
this.#closed = true;
|
|
176
198
|
if (this.#stream) {
|
|
199
|
+
for (const line of pending) this.#stream.write(line);
|
|
177
200
|
this.#stream.end();
|
|
178
201
|
this.#stream = null;
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
// No stream, because a rotation is in flight: it nulls the stream before
|
|
205
|
+
// awaiting the old one's end, and it will not reopen now that #closed is
|
|
206
|
+
// set. Writing the buffer to the stream that no longer exists is how
|
|
207
|
+
// those lines used to disappear — accepted by write(), then dropped.
|
|
208
|
+
//
|
|
209
|
+
// Appended directly instead, and synchronously, because close() is: the
|
|
210
|
+
// path is the current log either way, whether the rotation has renamed
|
|
211
|
+
// it yet or not.
|
|
212
|
+
this.#appendDirectly(pending);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Last-resort write for lines with no stream left to carry them. */
|
|
216
|
+
#appendDirectly(lines: string[]): void {
|
|
217
|
+
if (lines.length === 0) return;
|
|
218
|
+
try {
|
|
219
|
+
if (!this.#dirReady) {
|
|
220
|
+
fs.mkdirSync(path.dirname(this.#filePath), { recursive: true });
|
|
221
|
+
this.#dirReady = true;
|
|
222
|
+
}
|
|
223
|
+
fs.appendFileSync(this.#filePath, lines.join(""));
|
|
224
|
+
} catch {
|
|
225
|
+
process.stderr.write(
|
|
226
|
+
`[Spectrum] FileChannel: ${lines.length} buffered line(s) lost closing '${this.#filePath}'\n`,
|
|
227
|
+
);
|
|
179
228
|
}
|
|
180
229
|
}
|
|
181
230
|
}
|