···33 LogLevelStreamEntry,
44 LogLevel,
55 StreamDestination,
66- FileDestination,
66+ FileDestination, JsonPrettyDestination,
77} from "./types.js";
88-import {destination} from "pino";
99-import {build} from "pino-pretty"
88+import {destination, DestinationStream} from "pino";
99+import {build, prettyFactory} from "pino-pretty"
1010import {PRETTY_OPTS_FILE, prettyOptsConsoleFactory, prettyOptsFileFactory} from "./pretty.js";
1111import {fileOrDirectoryIsWriteable} from "./util.js";
1212import path from "path";
1313+import {Transform, TransformCallback} from "node:stream";
1414+import pump from 'pump';
1515+import abstractTransport from 'pino-abstract-transport';
131614171518const pRoll = pinoRoll as unknown as typeof pinoRoll.default;
···110113 return {
111114 level: level,
112115 stream: build({...prettyOptsConsoleFactory(options)})
116116+ }
117117+}
118118+119119+/**
120120+ * Creates a `LogLevelStreamEntry` stream that writes the raw log data with prettified log line, as either a JSON string or object, to a `NodeJs.WriteableStream` or [Sonic Boom `DestinationStream`](https://github.com/pinojs/sonic-boom) at or above the minimum `level`
121121+ *
122122+ * Use this to get raw log data + formatted line rendered by pino-pretty. The prettified line is set to the `line` key in the object.
123123+ *
124124+ * WARNING: This is not a fast operation. The log data must be parsed to json before being prettified, which also parses data to json. If you only need raw log data you should pass a plain Passthrough or Transform stream as an additional destination and then JSON.parse() on 'data' event manually.
125125+ *
126126+ * If used with `object: true` then
127127+ *
128128+ * * the `destination` cannot be a file or SonicBoom object
129129+ * * the `destination` stream passed MUST be set to `objectMode: true`
130130+ *
131131+ * @see DestinationStream
132132+ * */
133133+export const buildDestinationJsonPrettyStream = (level: LogLevel, options: JsonPrettyDestination): LogLevelStreamEntry => {
134134+ const {object = false} = options;
135135+ const factoryOpts = prettyOptsConsoleFactory(options);
136136+ const prettyFunc = prettyFactory(factoryOpts);
137137+138138+ const stream = new Transform({
139139+ objectMode: object,
140140+ autoDestroy: true,
141141+ transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback) {
142142+ const data = JSON.parse(chunk.toString());
143143+ const line = prettyFunc(chunk);
144144+ data.line = line.substring(0, line.length - 1);
145145+ if(object) {
146146+ callback(null, data);
147147+ } else {
148148+ callback(null, `${JSON.stringify(data)}\n`);
149149+ }
150150+ }
151151+ });
152152+153153+ let destinationStream: DestinationStream | NodeJS.WritableStream;
154154+155155+ if(typeof options.destination === 'object' && typeof options.destination.write === 'function') {
156156+ destinationStream = options.destination;
157157+ } else {
158158+ destinationStream = destination({...factoryOpts, dest: stream});
159159+ }
160160+161161+ if(object) {
162162+ // @ts-ignore
163163+ pump(stream, destinationStream)
164164+165165+ return {
166166+ level: level,
167167+ stream: stream
168168+ }
169169+ }
170170+171171+ // @ts-ignore
172172+ const transport = abstractTransport(function (source) {
173173+ source.on('unknown', function (line) {
174174+ destinationStream.write(line + '\n')
175175+ })
176176+177177+ // @ts-ignore
178178+ pump(source, stream, destinationStream);
179179+ return stream;
180180+ }, { parse: 'lines' });
181181+182182+ return {
183183+ level: level,
184184+ stream: transport
113185 }
114186}
115187