-
Notifications
You must be signed in to change notification settings - Fork 898
/
Copy pathworker.js
194 lines (177 loc) Β· 8.75 KB
/
worker.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'use strict'
const EE = require('node:events')
const { pipeline, PassThrough } = require('node:stream')
const pino = require('../pino.js')
const build = require('pino-abstract-transport')
const loadTransportStreamBuilder = require('./transport-stream')
// This file is not checked by the code coverage tool,
// as it is not reliable.
/* istanbul ignore file */
/*
* > Multiple targets & pipelines
*
*
* βββββββββββββββββββββββββββββββββββββββββββββββββββ βββββββ
* β β β p β
* β β β i β
* β target β β n β
* β β βββββββββββββββββββββββββββββββββΌβββββ€ o β
* β targets β target β β . β
* β βββββββββββββΊ β βββββββββββββββββββββββββββββββββΌβββββ€ m β source
* β β target β β u β β
* β β βββββββββββββββββββββββββββββββββΌβββββ€ l β βwrite
* β β β β t β βΌ
* β β pipeline βββββββββββββββββ β β i β ββββββββββ
* β β βββββββββββΊ β PassThrough βββββΌβββββ€ s ββββββββ€ β
* β β βββββββββββββββββ β β t β writeβ Thread β
* β β β β r ββββββββ€ Stream β
* β β pipeline βββββββββββββββββ β β e β β β
* β β βββββββββββΊ β PassThrough βββββΌβββββ€ a β ββββββββββ
* β βββββββββββββββββ β β m β
* β β β β
* βββββββββββββββββββββββββββββββββββββββββββββββββββ βββββββ
*
*
*
* > One single pipeline or target
*
*
* source
* β
* ββββββββββββββββββββββββββββββββββββββββββββββββββ βwrite
* β β βΌ
* β β ββββββββββ
* β targets β target β β β
* β βββββββββββββΊ β βββββββββββββββββββββββββββββββ€ β β
* β β β β β
* β ββββββββ€ β
* β β β β
* β β β β
* β OR β β β
* β β β β
* β β β β
* β ββββββββββββββββ β β β
* β targets β pipeline β β β β Thread β
* β βββββββββββββΊ β βββββββββββββΊβ PassThrough βββ€ β Stream β
* β β β β β β β
* β ββββββββββββββββ β β β
* β β β β
* β OR β writeβ β
* β ββββββββ€ β
* β β β β
* β ββββββββββββββββ β β β
* β pipeline β β β β β
* β βββββββββββββββΊβ PassThrough ββββββββββββββββββ€ β β
* β β β β β β
* β ββββββββββββββββ β ββββββββββ
* β β
* β β
* ββββββββββββββββββββββββββββββββββββββββββββββββββ
*/
module.exports = async function ({ targets, pipelines, levels, dedupe }) {
const targetStreams = []
// Process targets
if (targets && targets.length) {
targets = await Promise.all(targets.map(async (t) => {
const fn = await loadTransportStreamBuilder(t.target)
const stream = await fn(t.options)
return {
level: t.level,
stream
}
}))
targetStreams.push(...targets)
}
// Process pipelines
if (pipelines && pipelines.length) {
pipelines = await Promise.all(
pipelines.map(async (p) => {
let level
const pipeDests = await Promise.all(
p.map(async (t) => {
// level assigned to pipeline is duplicated over all its targets, just store it
level = t.level
const fn = await loadTransportStreamBuilder(t.target)
const stream = await fn(t.options)
return stream
}
))
return {
level,
stream: createPipeline(pipeDests)
}
})
)
targetStreams.push(...pipelines)
}
// Skip building the multistream step if either one single pipeline or target is defined and
// return directly the stream instance back to TreadStream.
// This is equivalent to define either:
//
// pino.transport({ target: ... })
//
// OR
//
// pino.transport({ pipeline: ... })
if (targetStreams.length === 1) {
return targetStreams[0].stream
} else {
return build(process, {
parse: 'lines',
metadata: true,
close (err, cb) {
let expected = 0
for (const transport of targetStreams) {
expected++
transport.stream.on('close', closeCb)
transport.stream.end()
}
function closeCb () {
if (--expected === 0) {
cb(err)
}
}
}
})
}
// TODO: Why split2 was not used for pipelines?
function process (stream) {
const multi = pino.multistream(targetStreams, { levels, dedupe })
// TODO manage backpressure
stream.on('data', function (chunk) {
const { lastTime, lastMsg, lastObj, lastLevel } = this
multi.lastLevel = lastLevel
multi.lastTime = lastTime
multi.lastMsg = lastMsg
multi.lastObj = lastObj
// TODO handle backpressure
multi.write(chunk + '\n')
})
}
/**
* Creates a pipeline using the provided streams and return an instance of `PassThrough` stream
* as a source for the pipeline.
*
* @param {(TransformStream|WritableStream)[]} streams An array of streams.
* All intermediate streams in the array *MUST* be `Transform` streams and only the last one `Writable`.
* @returns A `PassThrough` stream instance representing the source stream of the pipeline
*/
function createPipeline (streams) {
const ee = new EE()
const stream = new PassThrough({
autoDestroy: true,
destroy (_, cb) {
ee.on('error', cb)
ee.on('closed', cb)
}
})
pipeline(stream, ...streams, function (err) {
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
ee.emit('error', err)
return
}
ee.emit('closed')
})
return stream
}
}