Skip to content

Commit 7115485

Browse files
authored
fix: use internal fork of unmaintained strong-log-transformer (lerna#4195)
1 parent 569b85d commit 7115485

File tree

8 files changed

+672
-37
lines changed

8 files changed

+672
-37
lines changed

libs/child-process/src/forked-strong-log-transformer.spec.ts

Lines changed: 423 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
/* eslint-disable */
2+
// @ts-nocheck
3+
4+
/**
5+
* Internal fork of the unmaintained strong-log-transformer package.
6+
* https://github.com/strongloop/strong-log-transformer/blob/3315d59bc4c912d025e15a6ca22a600a85406f14/lib/logger.js
7+
*
8+
* Notable changes:
9+
* - replaced the dependency on duplexer, which has been deprecated for a long time, with inline createDuplex()
10+
* - modernized core node.js imports and removed the usage of deprecated node:util._extend()
11+
*/
12+
13+
// Copyright IBM Corp. 2014,2018. All Rights Reserved.
14+
// Node module: strong-log-transformer
15+
// This file is licensed under the Apache License 2.0.
16+
// License text available at https://opensource.org/licenses/Apache-2.0
17+
18+
import stream from "node:stream";
19+
import { StringDecoder } from "node:string_decoder";
20+
import util from "node:util";
21+
import through from "through";
22+
23+
export default Logger;
24+
25+
Logger.DEFAULTS = {
26+
format: "text",
27+
tag: "",
28+
mergeMultiline: false,
29+
timeStamp: false,
30+
};
31+
32+
var formatters = {
33+
text: textFormatter,
34+
json: jsonFormatter,
35+
};
36+
37+
function Logger(options?) {
38+
var defaults = JSON.parse(JSON.stringify(Logger.DEFAULTS));
39+
options = Object.assign(defaults, options || {});
40+
var catcher = deLiner();
41+
var emitter = catcher;
42+
var transforms = [objectifier()];
43+
44+
if (options.tag) {
45+
transforms.push(staticTagger(options.tag));
46+
}
47+
48+
if (options.mergeMultiline) {
49+
transforms.push(lineMerger());
50+
}
51+
52+
// TODO
53+
// if (options.pidStamp) {
54+
// transforms.push(pidStamper(options.pid));
55+
// }
56+
57+
// TODO
58+
// if (options.workerStamp) {
59+
// transforms.push(workerStamper(options.worker));
60+
// }
61+
62+
transforms.push(formatters[options.format](options));
63+
64+
// restore line endings that were removed by line splitting
65+
transforms.push(reLiner());
66+
67+
for (var t in transforms) {
68+
emitter = emitter.pipe(transforms[t]);
69+
}
70+
71+
return createDuplex(catcher, emitter);
72+
}
73+
74+
function deLiner() {
75+
var decoder = new StringDecoder("utf8");
76+
var last = "";
77+
78+
return new stream.Transform({
79+
transform(chunk, _enc, callback) {
80+
last += decoder.write(chunk);
81+
var list = last.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g);
82+
last = list.pop();
83+
for (var i = 0; i < list.length; i++) {
84+
// swallow empty lines
85+
if (list[i]) {
86+
this.push(list[i]);
87+
}
88+
}
89+
callback();
90+
},
91+
flush(callback) {
92+
// incomplete UTF8 sequences become UTF8 replacement characters
93+
last += decoder.end();
94+
if (last) {
95+
this.push(last);
96+
}
97+
callback();
98+
},
99+
});
100+
}
101+
102+
function reLiner() {
103+
return through(appendNewline);
104+
105+
function appendNewline(line) {
106+
this.emit("data", line + "\n");
107+
}
108+
}
109+
110+
function objectifier() {
111+
return through(objectify, null, { autoDestroy: false });
112+
113+
function objectify(line) {
114+
this.emit("data", {
115+
msg: line,
116+
time: Date.now(),
117+
});
118+
}
119+
}
120+
121+
function staticTagger(tag) {
122+
return through(tagger);
123+
124+
function tagger(logEvent) {
125+
logEvent.tag = tag;
126+
this.emit("data", logEvent);
127+
}
128+
}
129+
130+
function textFormatter(options) {
131+
return through(textify);
132+
133+
function textify(logEvent) {
134+
var line = util.format("%s%s", textifyTags(logEvent.tag), logEvent.msg.toString());
135+
if (options.timeStamp) {
136+
line = util.format("%s %s", new Date(logEvent.time).toISOString(), line);
137+
}
138+
this.emit("data", line.replace(/\n/g, "\\n"));
139+
}
140+
141+
function textifyTags(tags) {
142+
var str = "";
143+
if (typeof tags === "string") {
144+
str = tags + " ";
145+
} else if (typeof tags === "object") {
146+
for (var t in tags) {
147+
str += t + ":" + tags[t] + " ";
148+
}
149+
}
150+
return str;
151+
}
152+
}
153+
154+
function jsonFormatter(options) {
155+
return through(jsonify);
156+
157+
function jsonify(logEvent) {
158+
if (options.timeStamp) {
159+
logEvent.time = new Date(logEvent.time).toISOString();
160+
} else {
161+
delete logEvent.time;
162+
}
163+
logEvent.msg = logEvent.msg.toString();
164+
this.emit("data", JSON.stringify(logEvent));
165+
}
166+
}
167+
168+
function lineMerger(host) {
169+
var previousLine = null;
170+
var flushTimer = null;
171+
var stream = through(lineMergerWrite, lineMergerEnd);
172+
var flush = _flush.bind(stream);
173+
174+
return stream;
175+
176+
function lineMergerWrite(line) {
177+
if (/^\s+/.test(line.msg)) {
178+
if (previousLine) {
179+
previousLine.msg += "\n" + line.msg;
180+
} else {
181+
previousLine = line;
182+
}
183+
} else {
184+
flush();
185+
previousLine = line;
186+
}
187+
// rolling timeout
188+
clearTimeout(flushTimer);
189+
flushTimer = setTimeout(flush.bind(this), 10);
190+
}
191+
192+
function _flush() {
193+
if (previousLine) {
194+
this.emit("data", previousLine);
195+
previousLine = null;
196+
}
197+
}
198+
199+
function lineMergerEnd() {
200+
flush.call(this);
201+
this.emit("end");
202+
}
203+
}
204+
205+
/**
206+
* Net new function in internal fork to replace duplexer
207+
*/
208+
209+
function createDuplex(input: stream.Readable, output: stream.Writable) {
210+
const duplex = new stream.Duplex({
211+
objectMode: false,
212+
allowHalfOpen: false,
213+
});
214+
// Forward writes to the input stream
215+
duplex._write = (chunk, encoding, cb) => {
216+
input.write(chunk, encoding, cb);
217+
};
218+
// Forward reads from the output stream
219+
duplex._read = () => {
220+
// This will be handled by the output stream's data events
221+
};
222+
// Handle end of writing
223+
duplex._final = (cb) => {
224+
input.end(cb);
225+
};
226+
// Pipe output stream data to our duplex stream
227+
output.on("data", (chunk) => {
228+
duplex.push(chunk);
229+
});
230+
output.on("end", () => {
231+
duplex.push(null); // Signal end of stream
232+
});
233+
// Forward errors
234+
input.on("error", (err) => {
235+
duplex.emit("error", err);
236+
});
237+
output.on("error", (err) => {
238+
duplex.emit("error", err);
239+
});
240+
return duplex;
241+
}

libs/child-process/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import os from "os";
21
import chalk from "chalk";
32
import execa from "execa";
4-
import strongLogTransformer from "strong-log-transformer";
3+
import os from "node:os";
4+
import strongLogTransformer from "./forked-strong-log-transformer";
55
import { setExitCode } from "./set-exit-code";
66

77
type withPkg<T> = T & { pkg?: unknown };

package-lock.json

Lines changed: 3 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
"@types/semver": "^7.5.8",
7171
"@types/signal-exit": "^3.0.4",
7272
"@types/ssri": "^7.1.5",
73-
"@types/strong-log-transformer": "^1.0.2",
7473
"@types/tar": "^6.1.13",
7574
"@types/temp-dir": "^1.0.0",
7675
"@types/tmp": "^0.2.6",

packages/legacy-package-management/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@
8282
"slash": "3.0.0",
8383
"ssri": "^10.0.6",
8484
"string-width": "^4.2.3",
85-
"strong-log-transformer": "2.1.0",
8685
"tar": "6.2.1",
8786
"temp-dir": "1.0.0",
8887
"tempy": "1.0.0",
88+
"through": "2.3.8",
8989
"tinyglobby": "0.2.12",
9090
"upath": "2.0.1",
9191
"uuid": "^10.0.0",

packages/legacy-structure/commands/create/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@
8787
"slash": "^3.0.0",
8888
"ssri": "^10.0.6",
8989
"string-width": "^4.2.3",
90-
"strong-log-transformer": "2.1.0",
9190
"tar": "6.2.1",
9291
"temp-dir": "1.0.0",
92+
"through": "2.3.8",
9393
"tinyglobby": "0.2.12",
9494
"upath": "2.0.1",
9595
"uuid": "^10.0.0",

packages/lerna/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@
103103
"slash": "3.0.0",
104104
"ssri": "^10.0.6",
105105
"string-width": "^4.2.3",
106-
"strong-log-transformer": "2.1.0",
107106
"tar": "6.2.1",
108107
"temp-dir": "1.0.0",
108+
"through": "2.3.8",
109109
"tinyglobby": "0.2.12",
110110
"typescript": ">=3 < 6",
111111
"upath": "2.0.1",

0 commit comments

Comments
 (0)