-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
55 lines (47 loc) · 1.62 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {Transform} from 'node:stream';
import pump from 'pump';
import abstractTransport, {type OnUnknown} from 'pino-abstract-transport';
import type {SonicBoom, SonicBoomOpts} from 'sonic-boom';
import prettify from './lib/prettify.js';
import buildSafeSonicBoom from './lib/build-safe-sonic-boom.js';
import type {PrettifyOptions} from './lib/utils/types.js';
function build(
options: PrettifyOptions &
SonicBoomOpts & {destination?: Transform & OnUnknown},
): Transform & OnUnknown {
const pretty = prettify(options);
function transform(source: Transform & OnUnknown): void {
const stream = new Transform({
objectMode: true,
autoDestroy: true,
transform(chunk: string, enc: string, cb) {
const line = pretty(chunk);
cb(null, line);
},
});
let destination: Transform | (SonicBoom & OnUnknown);
if (
(typeof options?.destination === 'object' &&
typeof options?.destination.write === 'function') ||
options.destination instanceof Transform
) {
destination = options.destination;
} else {
destination = buildSafeSonicBoom({
dest: options.destination ?? 1,
append: options.append,
mkdir: options.mkdir,
sync: options.sync,
});
}
source.on('unknown', function (line) {
destination.write(line + '\n');
});
pump(source, stream, destination as Transform);
}
return abstractTransport(transform, {parse: 'lines'});
}
export {build};
export default build;
export {default as prettify} from './lib/prettify.js';
export type {PrettifyOptions as Configuration} from './lib/utils/types.js';