-
Notifications
You must be signed in to change notification settings - Fork 17
/
transport.js
31 lines (25 loc) 路 901 Bytes
/
transport.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
const split = require('split2')
const Parse = require('fast-json-parse')
const { getDestinationStream, getLevelNumber } = require('./util')
const teeTransport = options => {
const filters = Object
.entries(options.filters)
.map(([level, dest]) => [level, getDestinationStream(dest)])
return line => {
const res = new Parse(line)
if (!res.value) {
throw new Error('Failed to parse line: ', line)
}
filters
.filter(([level]) => res.value.level >= getLevelNumber(level))
.forEach(([, destination]) => destination.write(line + '\n'))
}
}
module.exports = (options) => {
const splitter = split(teeTransport(options))
// https://github.com/pinojs/thread-stream/issues/36#issuecomment-1008939471
// This fixes the stream hanging and timing out at 10s
splitter.end = splitter.destroy
return splitter
}
module.exports.teeTransport = teeTransport