-
Notifications
You must be signed in to change notification settings - Fork 847
/
transport.js
95 lines (76 loc) 路 2.2 KB
/
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
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
'use strict'
const { createRequire } = require('module')
const caller = require('get-caller-file')
const { join, isAbsolute } = require('path')
const ThreadStream = require('thread-stream')
function setupOnExit (stream) {
/* istanbul ignore next */
if (global.WeakRef && global.WeakMap && global.FinalizationRegistry) {
// This is leak free, it does not leave event handlers
const onExit = require('on-exit-leak-free')
onExit.register(stream, autoEnd)
stream.on('close', function () {
onExit.unregister(stream)
})
} else {
const fn = autoEnd.bind(null, stream)
process.on('exit', fn)
stream.on('close', function () {
process.removeListener('exit', fn)
})
}
}
function buildStream (filename, workerData, workerOpts) {
const stream = new ThreadStream({
filename,
workerData,
workerOpts,
sync: true // TODO should this be configurable?
})
stream.on('ready', function () {
stream.unref()
if (workerOpts.autoEnd !== false) {
setupOnExit(stream)
}
})
return stream
}
function autoEnd (stream) {
stream.end()
}
function transport (fullOptions) {
const { targets, options = {}, worker = {} } = fullOptions
// This function call MUST stay in the top-level function of this module
const callerFile = caller()
const callerRequire = createRequire(callerFile)
let target = fullOptions.target
if (target && targets) {
throw new Error('Only one of target or targets can be specified')
}
if (targets) {
target = join(__dirname, 'worker.js')
options.targets = targets.map((dest) => {
return {
...dest,
target: fixTarget(dest.target)
}
})
}
return buildStream(fixTarget(target), options, worker)
function fixTarget (origin) {
if (isAbsolute(origin) || origin.indexOf('file://') === 0) {
return origin
}
switch (origin) {
case '#pino/pretty':
return join(__dirname, 'pretty-target.js')
case '#pino/file':
return join(__dirname, 'file-target.js')
/* istanbul ignore next */
default:
// TODO we cannot test this on Windows.. might not work.
return callerRequire.resolve(origin)
}
}
}
module.exports = transport