@torrent-tv/proxy 2.70.0 → 2.71.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.
package/utils/logger.js CHANGED
@@ -38,6 +38,40 @@ let fileStream = null;
38
38
  /** @type {string} */
39
39
  let filePath = "";
40
40
  let writtenBytes = 0;
41
+ /**
42
+ * Where lines go when this module is running in a worker thread.
43
+ *
44
+ * A worker thread is a separate instance of the runtime: it loads its own copy
45
+ * of every module, so `fileStream` above is a DIFFERENT variable there, and
46
+ * `logToFile` is only ever called on the main thread. The result was silent:
47
+ * measured 2026-09-02 over a whole log file of 49 938 lines, every line the
48
+ * torrent thread wrote through this module — the piece reader's, including the
49
+ * comparison of the two claim strategies that had been awaited for weeks, and
50
+ * the torrent pool's — was absent, while the same lines were visible in the
51
+ * container's output, which is destroyed by every release.
52
+ *
53
+ * Two threads cannot both write the file: they would race on the rotation and
54
+ * could interleave mid-line. So there is one writer, and a worker sends its
55
+ * lines to it. Set by the worker at startup; unset on the main thread, where
56
+ * the file is written directly.
57
+ *
58
+ * @type {((level: string, message: string) => void) | null}
59
+ */
60
+ let forward = null;
61
+
62
+ /**
63
+ * Send this thread's log lines to the thread that owns the file.
64
+ *
65
+ * Called by a worker at startup. Until it is, a worker's lines reach the
66
+ * console and nothing else — which is what they did for as long as this module
67
+ * has existed.
68
+ *
69
+ * @param {((level: string, message: string) => void) | null} sink
70
+ * @returns {void}
71
+ */
72
+ export function forwardLogsTo(sink) {
73
+ forward = typeof sink === "function" ? sink : null;
74
+ }
41
75
 
42
76
  /**
43
77
  * Return the current time as a compact ISO-8601 (UTC) string, e.g.
@@ -119,24 +153,32 @@ function toFile(line) {
119
153
  * @type {ProxyLogger}
120
154
  */
121
155
  export const logger = {
122
- info: (message) => {
123
- const line = `${PREFIX} [${ts()}] ${message}`;
124
- console.log(chalk.cyan(line));
125
- toFile(line);
126
- },
127
- success: (message) => {
128
- const line = `${PREFIX} [${ts()}] ${message}`;
129
- console.log(chalk.green(line));
130
- toFile(line);
131
- },
132
- warn: (message) => {
133
- const line = `${PREFIX} [${ts()}] ${message}`;
134
- console.warn(chalk.yellow(line));
135
- toFile(line);
136
- },
137
- error: (message) => {
138
- const line = `${PREFIX} [${ts()}] ${message}`;
139
- console.error(chalk.red(line));
140
- toFile(line);
141
- }
156
+ info: (message) => write("info", message, chalk.cyan, console.log),
157
+ success: (message) => write("success", message, chalk.green, console.log),
158
+ warn: (message) => write("warn", message, chalk.yellow, console.warn),
159
+ error: (message) => write("error", message, chalk.red, console.error)
142
160
  };
161
+
162
+ /**
163
+ * One path for every level, so a line cannot reach the console and miss the
164
+ * file depending on which method was called or which thread called it.
165
+ *
166
+ * @param {string} level
167
+ * @param {string} message
168
+ * @param {(text: string) => string} colour
169
+ * @param {(text: string) => void} toConsole
170
+ * @returns {void}
171
+ */
172
+ function write(level, message, colour, toConsole) {
173
+ toConsole(colour(`${PREFIX} [${ts()}] ${message}`));
174
+ if (forward) {
175
+ try {
176
+ forward(level, message);
177
+ } catch {
178
+ // silent-ok: a thread whose channel has closed is shutting down, and a
179
+ // failed log line must not be what ends it.
180
+ }
181
+ return;
182
+ }
183
+ toFile(`${PREFIX} [${ts()}] ${message}`);
184
+ }