appos 0.1.2-0 → 0.1.3-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/build/{lib/dist-cjs-i0PJ4BkY.mjs → exports/dist-cjs-B43A5HXv.mjs} +3 -3
  2. package/build/{lib/dist-cjs-Ckv1CA7H.mjs → exports/dist-cjs-BmbYbMhp.mjs} +2 -2
  3. package/build/{lib/dist-cjs-CZJ3_gz8.mjs → exports/dist-cjs-DHlTgXUO.mjs} +1 -0
  4. package/build/{lib/dist-cjs-BwD8H9no.mjs → exports/dist-cjs-Ddm6Stp9.mjs} +3 -3
  5. package/build/{lib/sso-oidc-DihM2auV.mjs → exports/sso-oidc-Qom0PEDW.mjs} +1 -1
  6. package/build/{lib/sts-qpAtvsDA.mjs → exports/sts-CLRaHSW_.mjs} +2 -2
  7. package/build/{lib/sts-DxVJ4opX.mjs → exports/sts-Dl7yZqX1.mjs} +1 -1
  8. package/build/{lib → exports}/test.mjs +4 -4
  9. package/build/{lib → exports}/ui/index.d.ts +281 -281
  10. package/build/lib/caller.js +30 -0
  11. package/build/lib/constants.js +28 -0
  12. package/build/lib/deprecations.js +8 -0
  13. package/build/lib/levels.js +241 -0
  14. package/build/lib/meta.js +3 -0
  15. package/build/lib/multistream.js +203 -0
  16. package/build/lib/proto.js +256 -0
  17. package/build/lib/redaction.js +114 -0
  18. package/build/lib/symbols.js +74 -0
  19. package/build/lib/time.js +39 -0
  20. package/build/lib/tools.js +423 -0
  21. package/build/lib/transport-stream.js +56 -0
  22. package/build/lib/transport.js +167 -0
  23. package/build/lib/worker.js +194 -0
  24. package/build/main.js +4 -4
  25. package/package.json +19 -18
  26. /package/build/{lib → exports}/adapter-factory-CLtf7vT5-CTKiMV2u.mjs +0 -0
  27. /package/build/{lib → exports}/bun-sqlite-dialect-DN7EKoIB-0pWIoG0L.mjs +0 -0
  28. /package/build/{lib → exports}/chunk-BYI67GdN.mjs +0 -0
  29. /package/build/{lib → exports}/chunk-CkKlU7xI.mjs +0 -0
  30. /package/build/{lib → exports}/client-BtuhFgvy.mjs +0 -0
  31. /package/build/{lib → exports}/client.d.ts +0 -0
  32. /package/build/{lib → exports}/client.js +0 -0
  33. /package/build/{lib → exports}/date.d.ts +0 -0
  34. /package/build/{lib → exports}/date.js +0 -0
  35. /package/build/{lib → exports}/dialect-BeMwdQoz-D41NdSow.mjs +0 -0
  36. /package/build/{lib → exports}/dist-cjs-B_wkbabY.mjs +0 -0
  37. /package/build/{lib → exports}/dist-cjs-Cc337rwW.mjs +0 -0
  38. /package/build/{lib → exports}/dist-cjs-CgzfN8xY.mjs +0 -0
  39. /package/build/{lib → exports}/dist-cjs-DBFcizQf.mjs +0 -0
  40. /package/build/{lib → exports}/dist-cjs-DC-UL-xx.mjs +0 -0
  41. /package/build/{lib → exports}/dist-cjs-DYAmJYzN.mjs +0 -0
  42. /package/build/{lib → exports}/dist-cjs-GFGBhaZg.mjs +0 -0
  43. /package/build/{lib → exports}/dist-cjs-ieCOxrOi.mjs +0 -0
  44. /package/build/{lib → exports}/dist-cjs-nbOGa7i_.mjs +0 -0
  45. /package/build/{lib → exports}/dist-cjs-pN6ekzMW.mjs +0 -0
  46. /package/build/{lib → exports}/dist-cjs-yYlQ1OiO.mjs +0 -0
  47. /package/build/{lib → exports}/esm-DPet4lFW.mjs +0 -0
  48. /package/build/{lib → exports}/event-streams-x9fP9BzF.mjs +0 -0
  49. /package/build/{lib → exports}/file-preview-dialog-Cugg8tEc.mjs +0 -0
  50. /package/build/{lib → exports}/kysely-adapter-BTWfu5rn.mjs +0 -0
  51. /package/build/{lib → exports}/magic-string.es-jZcifc5m.mjs +0 -0
  52. /package/build/{lib → exports}/memory-adapter-DiU519m9.mjs +0 -0
  53. /package/build/{lib → exports}/node-ponyfill-Dmm1SWTU.mjs +0 -0
  54. /package/build/{lib → exports}/node-sqlite-dialect-BqISUSC3-DYvTLBpY.mjs +0 -0
  55. /package/build/{lib → exports}/package-DfozRyK6.mjs +0 -0
  56. /package/build/{lib → exports}/react-dom-CPjM_WCt.mjs +0 -0
  57. /package/build/{lib → exports}/server.d.mts +0 -0
  58. /package/build/{lib → exports}/server.mjs +0 -0
  59. /package/build/{lib → exports}/test.d.mts +0 -0
  60. /package/build/{lib → exports}/ui/index.js +0 -0
  61. /package/build/{lib → exports}/utils.d.ts +0 -0
  62. /package/build/{lib → exports}/utils.js +0 -0
  63. /package/build/{lib → exports}/zod.d.ts +0 -0
  64. /package/build/{lib → exports}/zod.js +0 -0
@@ -0,0 +1,423 @@
1
+ 'use strict'
2
+
3
+ /* eslint no-prototype-builtins: 0 */
4
+
5
+ const diagChan = require('node:diagnostics_channel')
6
+ const format = require('quick-format-unescaped')
7
+ const { mapHttpRequest, mapHttpResponse } = require('pino-std-serializers')
8
+ const SonicBoom = require('sonic-boom')
9
+ const onExit = require('on-exit-leak-free')
10
+ const {
11
+ lsCacheSym,
12
+ chindingsSym,
13
+ writeSym,
14
+ serializersSym,
15
+ formatOptsSym,
16
+ endSym,
17
+ stringifiersSym,
18
+ stringifySym,
19
+ stringifySafeSym,
20
+ wildcardFirstSym,
21
+ nestedKeySym,
22
+ formattersSym,
23
+ messageKeySym,
24
+ errorKeySym,
25
+ nestedKeyStrSym,
26
+ msgPrefixSym
27
+ } = require('./symbols')
28
+ const { isMainThread } = require('worker_threads')
29
+ const transport = require('./transport')
30
+
31
+ const asJsonChan = diagChan.tracingChannel('pino_asJson')
32
+
33
+ function noop () {
34
+ }
35
+
36
+ function genLog (level, hook) {
37
+ if (!hook) return LOG
38
+
39
+ return function hookWrappedLog (...args) {
40
+ hook.call(this, args, LOG, level)
41
+ }
42
+
43
+ function LOG (o, ...n) {
44
+ if (typeof o === 'object') {
45
+ let msg = o
46
+ if (o !== null) {
47
+ if (o.method && o.headers && o.socket) {
48
+ o = mapHttpRequest(o)
49
+ } else if (typeof o.setHeader === 'function') {
50
+ o = mapHttpResponse(o)
51
+ }
52
+ }
53
+ let formatParams
54
+ if (msg === null && n.length === 0) {
55
+ formatParams = [null]
56
+ } else {
57
+ msg = n.shift()
58
+ formatParams = n
59
+ }
60
+ // We do not use a coercive check for `msg` as it is
61
+ // measurably slower than the explicit checks.
62
+ if (typeof this[msgPrefixSym] === 'string' && msg !== undefined && msg !== null) {
63
+ msg = this[msgPrefixSym] + msg
64
+ }
65
+ this[writeSym](o, format(msg, formatParams, this[formatOptsSym]), level)
66
+ } else {
67
+ let msg = o === undefined ? n.shift() : o
68
+
69
+ // We do not use a coercive check for `msg` as it is
70
+ // measurably slower than the explicit checks.
71
+ if (typeof this[msgPrefixSym] === 'string' && msg !== undefined && msg !== null) {
72
+ msg = this[msgPrefixSym] + msg
73
+ }
74
+ this[writeSym](null, format(msg, n, this[formatOptsSym]), level)
75
+ }
76
+ }
77
+ }
78
+
79
+ // magically escape strings for json
80
+ // relying on their charCodeAt
81
+ // everything below 32 needs JSON.stringify()
82
+ // 34 and 92 happens all the time, so we
83
+ // have a fast case for them
84
+ function asString (str) {
85
+ let result = ''
86
+ let last = 0
87
+ let found = false
88
+ let point = 255
89
+ const l = str.length
90
+ if (l > 100) {
91
+ return JSON.stringify(str)
92
+ }
93
+ for (var i = 0; i < l && point >= 32; i++) {
94
+ point = str.charCodeAt(i)
95
+ if (point === 34 || point === 92) {
96
+ result += str.slice(last, i) + '\\'
97
+ last = i
98
+ found = true
99
+ }
100
+ }
101
+ if (!found) {
102
+ result = str
103
+ } else {
104
+ result += str.slice(last)
105
+ }
106
+ return point < 32 ? JSON.stringify(str) : '"' + result + '"'
107
+ }
108
+
109
+ /**
110
+ * `asJson` wraps `_asJson` in order to facilitate generating diagnostics.
111
+ *
112
+ * @param {object} obj The merging object passed to the log method.
113
+ * @param {string} msg The log message passed to the log method.
114
+ * @param {number} num The log level number.
115
+ * @param {number} time The log time in milliseconds.
116
+ *
117
+ * @returns {string}
118
+ */
119
+ function asJson (obj, msg, num, time) {
120
+ if (asJsonChan.hasSubscribers === false) {
121
+ return _asJson.call(this, obj, msg, num, time)
122
+ }
123
+
124
+ const store = { instance: this, arguments }
125
+ return asJsonChan.traceSync(_asJson, store, this, obj, msg, num, time)
126
+ }
127
+
128
+ /**
129
+ * `_asJson` parses all collected data and generates the finalized newline
130
+ * delimited JSON string.
131
+ *
132
+ * @param {object} obj The merging object passed to the log method.
133
+ * @param {string} msg The log message passed to the log method.
134
+ * @param {number} num The log level number.
135
+ * @param {number} time The log time in milliseconds.
136
+ *
137
+ * @returns {string} The finalized log string terminated with a newline.
138
+ * @private
139
+ */
140
+ function _asJson (obj, msg, num, time) {
141
+ const stringify = this[stringifySym]
142
+ const stringifySafe = this[stringifySafeSym]
143
+ const stringifiers = this[stringifiersSym]
144
+ const end = this[endSym]
145
+ const chindings = this[chindingsSym]
146
+ const serializers = this[serializersSym]
147
+ const formatters = this[formattersSym]
148
+ const messageKey = this[messageKeySym]
149
+ const errorKey = this[errorKeySym]
150
+ let data = this[lsCacheSym][num] + time
151
+
152
+ // we need the child bindings added to the output first so instance logged
153
+ // objects can take precedence when JSON.parse-ing the resulting log line
154
+ data = data + chindings
155
+
156
+ let value
157
+ if (formatters.log) {
158
+ obj = formatters.log(obj)
159
+ }
160
+ const wildcardStringifier = stringifiers[wildcardFirstSym]
161
+ let propStr = ''
162
+ for (const key in obj) {
163
+ value = obj[key]
164
+ if (Object.prototype.hasOwnProperty.call(obj, key) && value !== undefined) {
165
+ if (serializers[key]) {
166
+ value = serializers[key](value)
167
+ } else if (key === errorKey && serializers.err) {
168
+ value = serializers.err(value)
169
+ }
170
+
171
+ const stringifier = stringifiers[key] || wildcardStringifier
172
+
173
+ switch (typeof value) {
174
+ case 'undefined':
175
+ case 'function':
176
+ continue
177
+ case 'number':
178
+ /* eslint no-fallthrough: "off" */
179
+ if (Number.isFinite(value) === false) {
180
+ value = null
181
+ }
182
+ // this case explicitly falls through to the next one
183
+ case 'boolean':
184
+ if (stringifier) value = stringifier(value)
185
+ break
186
+ case 'string':
187
+ value = (stringifier || asString)(value)
188
+ break
189
+ default:
190
+ value = (stringifier || stringify)(value, stringifySafe)
191
+ }
192
+ if (value === undefined) continue
193
+ const strKey = asString(key)
194
+ propStr += ',' + strKey + ':' + value
195
+ }
196
+ }
197
+
198
+ let msgStr = ''
199
+ if (msg !== undefined) {
200
+ value = serializers[messageKey] ? serializers[messageKey](msg) : msg
201
+ const stringifier = stringifiers[messageKey] || wildcardStringifier
202
+
203
+ switch (typeof value) {
204
+ case 'function':
205
+ break
206
+ case 'number':
207
+ if (Number.isFinite(value) === false) {
208
+ value = null
209
+ }
210
+ // this case explicitly falls through to the next one
211
+ case 'boolean':
212
+ if (stringifier) value = stringifier(value)
213
+ msgStr = ',"' + messageKey + '":' + value
214
+ break
215
+ case 'string':
216
+ value = (stringifier || asString)(value)
217
+ msgStr = ',"' + messageKey + '":' + value
218
+ break
219
+ default:
220
+ value = (stringifier || stringify)(value, stringifySafe)
221
+ msgStr = ',"' + messageKey + '":' + value
222
+ }
223
+ }
224
+
225
+ if (this[nestedKeySym] && propStr) {
226
+ // place all the obj properties under the specified key
227
+ // the nested key is already formatted from the constructor
228
+ return data + this[nestedKeyStrSym] + propStr.slice(1) + '}' + msgStr + end
229
+ } else {
230
+ return data + propStr + msgStr + end
231
+ }
232
+ }
233
+
234
+ function asChindings (instance, bindings) {
235
+ let value
236
+ let data = instance[chindingsSym]
237
+ const stringify = instance[stringifySym]
238
+ const stringifySafe = instance[stringifySafeSym]
239
+ const stringifiers = instance[stringifiersSym]
240
+ const wildcardStringifier = stringifiers[wildcardFirstSym]
241
+ const serializers = instance[serializersSym]
242
+ const formatter = instance[formattersSym].bindings
243
+ bindings = formatter(bindings)
244
+
245
+ for (const key in bindings) {
246
+ value = bindings[key]
247
+ const valid = (key.length < 5 || (key !== 'level' &&
248
+ key !== 'serializers' &&
249
+ key !== 'formatters' &&
250
+ key !== 'customLevels')) &&
251
+ bindings.hasOwnProperty(key) &&
252
+ value !== undefined
253
+ if (valid === true) {
254
+ value = serializers[key] ? serializers[key](value) : value
255
+ value = (stringifiers[key] || wildcardStringifier || stringify)(value, stringifySafe)
256
+ if (value === undefined) continue
257
+ data += ',"' + key + '":' + value
258
+ }
259
+ }
260
+ return data
261
+ }
262
+
263
+ function hasBeenTampered (stream) {
264
+ return stream.write !== stream.constructor.prototype.write
265
+ }
266
+
267
+ function buildSafeSonicBoom (opts) {
268
+ const stream = new SonicBoom(opts)
269
+ stream.on('error', filterBrokenPipe)
270
+ // If we are sync: false, we must flush on exit
271
+ if (!opts.sync && isMainThread) {
272
+ onExit.register(stream, autoEnd)
273
+
274
+ stream.on('close', function () {
275
+ onExit.unregister(stream)
276
+ })
277
+ }
278
+ return stream
279
+
280
+ function filterBrokenPipe (err) {
281
+ // Impossible to replicate across all operating systems
282
+ /* istanbul ignore next */
283
+ if (err.code === 'EPIPE') {
284
+ // If we get EPIPE, we should stop logging here
285
+ // however we have no control to the consumer of
286
+ // SonicBoom, so we just overwrite the write method
287
+ stream.write = noop
288
+ stream.end = noop
289
+ stream.flushSync = noop
290
+ stream.destroy = noop
291
+ return
292
+ }
293
+ stream.removeListener('error', filterBrokenPipe)
294
+ stream.emit('error', err)
295
+ }
296
+ }
297
+
298
+ function autoEnd (stream, eventName) {
299
+ // This check is needed only on some platforms
300
+ /* istanbul ignore next */
301
+ if (stream.destroyed) {
302
+ return
303
+ }
304
+
305
+ if (eventName === 'beforeExit') {
306
+ // We still have an event loop, let's use it
307
+ stream.flush()
308
+ stream.on('drain', function () {
309
+ stream.end()
310
+ })
311
+ } else {
312
+ // For some reason istanbul is not detecting this, but it's there
313
+ /* istanbul ignore next */
314
+ // We do not have an event loop, so flush synchronously
315
+ stream.flushSync()
316
+ }
317
+ }
318
+
319
+ function createArgsNormalizer (defaultOptions) {
320
+ return function normalizeArgs (instance, caller, opts = {}, stream) {
321
+ // support stream as a string
322
+ if (typeof opts === 'string') {
323
+ stream = buildSafeSonicBoom({ dest: opts })
324
+ opts = {}
325
+ } else if (typeof stream === 'string') {
326
+ if (opts && opts.transport) {
327
+ throw Error('only one of option.transport or stream can be specified')
328
+ }
329
+ stream = buildSafeSonicBoom({ dest: stream })
330
+ } else if (opts instanceof SonicBoom || opts.writable || opts._writableState) {
331
+ stream = opts
332
+ opts = {}
333
+ } else if (opts.transport) {
334
+ if (opts.transport instanceof SonicBoom || opts.transport.writable || opts.transport._writableState) {
335
+ throw Error('option.transport do not allow stream, please pass to option directly. e.g. pino(transport)')
336
+ }
337
+ if (opts.transport.targets && opts.transport.targets.length && opts.formatters && typeof opts.formatters.level === 'function') {
338
+ throw Error('option.transport.targets do not allow custom level formatters')
339
+ }
340
+
341
+ let customLevels
342
+ if (opts.customLevels) {
343
+ customLevels = opts.useOnlyCustomLevels ? opts.customLevels : Object.assign({}, opts.levels, opts.customLevels)
344
+ }
345
+ stream = transport({ caller, ...opts.transport, levels: customLevels })
346
+ }
347
+ opts = Object.assign({}, defaultOptions, opts)
348
+ opts.serializers = Object.assign({}, defaultOptions.serializers, opts.serializers)
349
+ opts.formatters = Object.assign({}, defaultOptions.formatters, opts.formatters)
350
+
351
+ if (opts.prettyPrint) {
352
+ throw new Error('prettyPrint option is no longer supported, see the pino-pretty package (https://github.com/pinojs/pino-pretty)')
353
+ }
354
+
355
+ const { enabled, onChild } = opts
356
+ if (enabled === false) opts.level = 'silent'
357
+ if (!onChild) opts.onChild = noop
358
+ if (!stream) {
359
+ if (!hasBeenTampered(process.stdout)) {
360
+ // If process.stdout.fd is undefined, it means that we are running
361
+ // in a worker thread. Let's assume we are logging to file descriptor 1.
362
+ stream = buildSafeSonicBoom({ fd: process.stdout.fd || 1 })
363
+ } else {
364
+ stream = process.stdout
365
+ }
366
+ }
367
+ return { opts, stream }
368
+ }
369
+ }
370
+
371
+ function stringify (obj, stringifySafeFn) {
372
+ try {
373
+ return JSON.stringify(obj)
374
+ } catch (_) {
375
+ try {
376
+ const stringify = stringifySafeFn || this[stringifySafeSym]
377
+ return stringify(obj)
378
+ } catch (_) {
379
+ return '"[unable to serialize, circular reference is too complex to analyze]"'
380
+ }
381
+ }
382
+ }
383
+
384
+ function buildFormatters (level, bindings, log) {
385
+ return {
386
+ level,
387
+ bindings,
388
+ log
389
+ }
390
+ }
391
+
392
+ /**
393
+ * Convert a string integer file descriptor to a proper native integer
394
+ * file descriptor.
395
+ *
396
+ * @param {string} destination The file descriptor string to attempt to convert.
397
+ *
398
+ * @returns {Number}
399
+ */
400
+ function normalizeDestFileDescriptor (destination) {
401
+ const fd = Number(destination)
402
+ if (typeof destination === 'string' && Number.isFinite(fd)) {
403
+ return fd
404
+ }
405
+ // destination could be undefined if we are in a worker
406
+ if (destination === undefined) {
407
+ // This is stdout in UNIX systems
408
+ return 1
409
+ }
410
+ return destination
411
+ }
412
+
413
+ module.exports = {
414
+ noop,
415
+ buildSafeSonicBoom,
416
+ asChindings,
417
+ asJson,
418
+ genLog,
419
+ createArgsNormalizer,
420
+ stringify,
421
+ buildFormatters,
422
+ normalizeDestFileDescriptor
423
+ }
@@ -0,0 +1,56 @@
1
+ 'use strict'
2
+
3
+ const { realImport, realRequire } = require('real-require')
4
+
5
+ module.exports = loadTransportStreamBuilder
6
+
7
+ /**
8
+ * Loads & returns a function to build transport streams
9
+ * @param {string} target
10
+ * @returns {Promise<function(object): Promise<import('node:stream').Writable>>}
11
+ * @throws {Error} In case the target module does not export a function
12
+ */
13
+ async function loadTransportStreamBuilder (target) {
14
+ let fn
15
+ try {
16
+ const toLoad = target.startsWith('file://') ? target : 'file://' + target
17
+
18
+ if (toLoad.endsWith('.ts') || toLoad.endsWith('.cts')) {
19
+ // TODO: add support for the TSM modules loader ( https://github.com/lukeed/tsm ).
20
+ if (process[Symbol.for('ts-node.register.instance')]) {
21
+ realRequire('ts-node/register')
22
+ } else if (process.env && process.env.TS_NODE_DEV) {
23
+ realRequire('ts-node-dev')
24
+ }
25
+ // TODO: Support ES imports once tsc, tap & ts-node provide better compatibility guarantees.
26
+ fn = realRequire(decodeURIComponent(target))
27
+ } else {
28
+ fn = (await realImport(toLoad))
29
+ }
30
+ } catch (error) {
31
+ // See this PR for details: https://github.com/pinojs/thread-stream/pull/34
32
+ if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND')) {
33
+ fn = realRequire(target)
34
+ } else if (error.code === undefined || error.code === 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING') {
35
+ // When bundled with pkg, an undefined error is thrown when called with realImport
36
+ // When bundled with pkg and using node v20, an ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING error is thrown when called with realImport
37
+ // More info at: https://github.com/pinojs/thread-stream/issues/143
38
+ try {
39
+ fn = realRequire(decodeURIComponent(target))
40
+ } catch {
41
+ throw error
42
+ }
43
+ } else {
44
+ throw error
45
+ }
46
+ }
47
+
48
+ // Depending on how the default export is performed, and on how the code is
49
+ // transpiled, we may find cases of two nested "default" objects.
50
+ // See https://github.com/pinojs/pino/issues/1243#issuecomment-982774762
51
+ if (typeof fn === 'object') fn = fn.default
52
+ if (typeof fn === 'object') fn = fn.default
53
+ if (typeof fn !== 'function') throw Error('exported worker is not a function')
54
+
55
+ return fn
56
+ }
@@ -0,0 +1,167 @@
1
+ 'use strict'
2
+
3
+ const { createRequire } = require('module')
4
+ const getCallers = require('./caller')
5
+ const { join, isAbsolute, sep } = require('node:path')
6
+ const sleep = require('atomic-sleep')
7
+ const onExit = require('on-exit-leak-free')
8
+ const ThreadStream = require('thread-stream')
9
+
10
+ function setupOnExit (stream) {
11
+ // This is leak free, it does not leave event handlers
12
+ onExit.register(stream, autoEnd)
13
+ onExit.registerBeforeExit(stream, flush)
14
+
15
+ stream.on('close', function () {
16
+ onExit.unregister(stream)
17
+ })
18
+ }
19
+
20
+ function buildStream (filename, workerData, workerOpts, sync) {
21
+ const stream = new ThreadStream({
22
+ filename,
23
+ workerData,
24
+ workerOpts,
25
+ sync
26
+ })
27
+
28
+ stream.on('ready', onReady)
29
+ stream.on('close', function () {
30
+ process.removeListener('exit', onExit)
31
+ })
32
+
33
+ process.on('exit', onExit)
34
+
35
+ function onReady () {
36
+ process.removeListener('exit', onExit)
37
+ stream.unref()
38
+
39
+ if (workerOpts.autoEnd !== false) {
40
+ setupOnExit(stream)
41
+ }
42
+ }
43
+
44
+ function onExit () {
45
+ /* istanbul ignore next */
46
+ if (stream.closed) {
47
+ return
48
+ }
49
+ stream.flushSync()
50
+ // Apparently there is a very sporadic race condition
51
+ // that in certain OS would prevent the messages to be flushed
52
+ // because the thread might not have been created still.
53
+ // Unfortunately we need to sleep(100) in this case.
54
+ sleep(100)
55
+ stream.end()
56
+ }
57
+
58
+ return stream
59
+ }
60
+
61
+ function autoEnd (stream) {
62
+ stream.ref()
63
+ stream.flushSync()
64
+ stream.end()
65
+ stream.once('close', function () {
66
+ stream.unref()
67
+ })
68
+ }
69
+
70
+ function flush (stream) {
71
+ stream.flushSync()
72
+ }
73
+
74
+ function transport (fullOptions) {
75
+ const { pipeline, targets, levels, dedupe, worker = {}, caller = getCallers(), sync = false } = fullOptions
76
+
77
+ const options = {
78
+ ...fullOptions.options
79
+ }
80
+
81
+ // Backwards compatibility
82
+ const callers = typeof caller === 'string' ? [caller] : caller
83
+
84
+ // This will be eventually modified by bundlers
85
+ const bundlerOverrides = '__bundlerPathsOverrides' in globalThis ? globalThis.__bundlerPathsOverrides : {}
86
+
87
+ let target = fullOptions.target
88
+
89
+ if (target && targets) {
90
+ throw new Error('only one of target or targets can be specified')
91
+ }
92
+
93
+ if (targets) {
94
+ target = bundlerOverrides['pino-worker'] || join(__dirname, 'worker.js')
95
+ options.targets = targets.filter(dest => dest.target).map((dest) => {
96
+ return {
97
+ ...dest,
98
+ target: fixTarget(dest.target)
99
+ }
100
+ })
101
+ options.pipelines = targets.filter(dest => dest.pipeline).map((dest) => {
102
+ return dest.pipeline.map((t) => {
103
+ return {
104
+ ...t,
105
+ level: dest.level, // duplicate the pipeline `level` property defined in the upper level
106
+ target: fixTarget(t.target)
107
+ }
108
+ })
109
+ })
110
+ } else if (pipeline) {
111
+ target = bundlerOverrides['pino-worker'] || join(__dirname, 'worker.js')
112
+ options.pipelines = [pipeline.map((dest) => {
113
+ return {
114
+ ...dest,
115
+ target: fixTarget(dest.target)
116
+ }
117
+ })]
118
+ }
119
+
120
+ if (levels) {
121
+ options.levels = levels
122
+ }
123
+
124
+ if (dedupe) {
125
+ options.dedupe = dedupe
126
+ }
127
+
128
+ options.pinoWillSendConfig = true
129
+
130
+ return buildStream(fixTarget(target), options, worker, sync)
131
+
132
+ function fixTarget (origin) {
133
+ origin = bundlerOverrides[origin] || origin
134
+
135
+ if (isAbsolute(origin) || origin.indexOf('file://') === 0) {
136
+ return origin
137
+ }
138
+
139
+ if (origin === 'pino/file') {
140
+ return join(__dirname, '..', 'file.js')
141
+ }
142
+
143
+ let fixTarget
144
+
145
+ for (const filePath of callers) {
146
+ try {
147
+ const context = filePath === 'node:repl'
148
+ ? process.cwd() + sep
149
+ : filePath
150
+
151
+ fixTarget = createRequire(context).resolve(origin)
152
+ break
153
+ } catch (err) {
154
+ // Silent catch
155
+ continue
156
+ }
157
+ }
158
+
159
+ if (!fixTarget) {
160
+ throw new Error(`unable to determine transport target for "${origin}"`)
161
+ }
162
+
163
+ return fixTarget
164
+ }
165
+ }
166
+
167
+ module.exports = transport