-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
68 lines (59 loc) · 1.53 KB
/
index.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict'
const split = require('split2')
const stream = require('readable-stream')
const { compile } = require('./lib/parser')
const tokens = require('./lib/tokens')
const pinoTransport = require('./lib/transport')
const pump = stream.pipeline
const eos = stream.finished
const Transform = stream.Transform
function parse () {
function parseRow (row) {
try {
return JSON.parse(row)
} catch (e) {
// Ignore errors
}
}
return split(parseRow, { autoDestroy: true })
}
module.exports = pinoTransport
module.exports.default = pinoTransport
module.exports.toke = toke
toke.compile = compile
function toke (format, destination, ancillary) {
const printer = parse()
let keep
if (typeof format === 'object') {
const opts = format
format = opts.format
keep = opts.keep
}
const line = typeof format === 'function' ? format : compile(format)
const transform = new Transform({
objectMode: true,
transform: function (o, _, cb) {
if (!(o.req && o.res && o.msg === 'request completed')) {
if (ancillary) ancillary.write(JSON.stringify(o) + '\n')
cb()
return
}
if (keep && ancillary) ancillary.write(JSON.stringify(o) + '\n')
const toWrite = line(tokens, o)
cb(null, toWrite)
}
})
const out = destination || process.stdout
pump(printer, transform, function (err) {
if (err) {
out.end(err.message + '\n')
return
}
out.end('\n')
})
eos(out, function () {
printer.destroy()
})
transform.pipe(out)
return printer
}