pino-testkit 10.4.5

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 (196) hide show
  1. package/.nojekyll +0 -0
  2. package/.prettierignore +1 -0
  3. package/CNAME +1 -0
  4. package/CONTRIBUTING.md +30 -0
  5. package/LICENSE +21 -0
  6. package/README.md +175 -0
  7. package/SECURITY.md +87 -0
  8. package/benchmarks/basic.bench.js +95 -0
  9. package/benchmarks/child-child.bench.js +52 -0
  10. package/benchmarks/child-creation.bench.js +73 -0
  11. package/benchmarks/child.bench.js +62 -0
  12. package/benchmarks/deep-object.bench.js +88 -0
  13. package/benchmarks/formatters.bench.js +50 -0
  14. package/benchmarks/internal/custom-levels.js +67 -0
  15. package/benchmarks/internal/just-pino-heavy.bench.js +76 -0
  16. package/benchmarks/internal/just-pino.bench.js +182 -0
  17. package/benchmarks/internal/parent-vs-child.bench.js +75 -0
  18. package/benchmarks/internal/redact.bench.js +86 -0
  19. package/benchmarks/long-string.bench.js +81 -0
  20. package/benchmarks/multi-arg.bench.js +193 -0
  21. package/benchmarks/multistream.js +98 -0
  22. package/benchmarks/object.bench.js +82 -0
  23. package/benchmarks/utils/generate-benchmark-doc.js +36 -0
  24. package/benchmarks/utils/runbench.js +138 -0
  25. package/benchmarks/utils/wrap-log-level.js +55 -0
  26. package/bin.js +6 -0
  27. package/browser.js +547 -0
  28. package/build/sync-version.js +25 -0
  29. package/docs/api.md +1593 -0
  30. package/docs/asynchronous.md +40 -0
  31. package/docs/benchmarks.md +55 -0
  32. package/docs/browser.md +278 -0
  33. package/docs/bundling.md +40 -0
  34. package/docs/child-loggers.md +95 -0
  35. package/docs/diagnostics.md +16 -0
  36. package/docs/ecosystem.md +86 -0
  37. package/docs/help.md +345 -0
  38. package/docs/lts.md +64 -0
  39. package/docs/pretty.md +35 -0
  40. package/docs/redaction.md +135 -0
  41. package/docs/transports.md +1376 -0
  42. package/docs/web.md +309 -0
  43. package/eslint.config.js +28 -0
  44. package/examples/basic.js +43 -0
  45. package/examples/transport.js +68 -0
  46. package/favicon.ico +0 -0
  47. package/file.js +12 -0
  48. package/inc-version.sh +42 -0
  49. package/index.html +55 -0
  50. package/lib/caller.js +30 -0
  51. package/lib/constants.js +28 -0
  52. package/lib/contract.js +8 -0
  53. package/lib/deprecations.js +8 -0
  54. package/lib/levels.js +241 -0
  55. package/lib/meta.js +3 -0
  56. package/lib/multistream.js +203 -0
  57. package/lib/proto.js +256 -0
  58. package/lib/redaction.js +114 -0
  59. package/lib/symbols.js +74 -0
  60. package/lib/time.js +39 -0
  61. package/lib/tools.js +427 -0
  62. package/lib/transport-stream.js +56 -0
  63. package/lib/transport.js +289 -0
  64. package/lib/worker.js +194 -0
  65. package/package.json +119 -0
  66. package/pino.d.ts +908 -0
  67. package/pino.js +240 -0
  68. package/test/basic.test.js +886 -0
  69. package/test/broken-pipe.test.js +59 -0
  70. package/test/browser-child.test.js +132 -0
  71. package/test/browser-disabled.test.js +87 -0
  72. package/test/browser-early-console-freeze.test.js +12 -0
  73. package/test/browser-is-level-enabled.test.js +101 -0
  74. package/test/browser-levels.test.js +241 -0
  75. package/test/browser-serializers.test.js +352 -0
  76. package/test/browser-timestamp.test.js +88 -0
  77. package/test/browser-transmit.test.js +417 -0
  78. package/test/browser.test.js +698 -0
  79. package/test/complex-objects.test.js +36 -0
  80. package/test/crlf.test.js +34 -0
  81. package/test/custom-levels.test.js +267 -0
  82. package/test/diagnostics.test.js +107 -0
  83. package/test/error-key.test.js +37 -0
  84. package/test/error.test.js +403 -0
  85. package/test/escaping.test.js +93 -0
  86. package/test/esm/esm.mjs +14 -0
  87. package/test/esm/index.test.js +21 -0
  88. package/test/esm/named-exports.mjs +29 -0
  89. package/test/exit.test.js +79 -0
  90. package/test/fixtures/broken-pipe/basic.js +9 -0
  91. package/test/fixtures/broken-pipe/destination.js +10 -0
  92. package/test/fixtures/broken-pipe/syncfalse.js +12 -0
  93. package/test/fixtures/console-transport.js +13 -0
  94. package/test/fixtures/crashing-transport.js +13 -0
  95. package/test/fixtures/default-exit.js +8 -0
  96. package/test/fixtures/destination-exit.js +8 -0
  97. package/test/fixtures/eval/index.js +13 -0
  98. package/test/fixtures/eval/node_modules/14-files.js +3 -0
  99. package/test/fixtures/eval/node_modules/2-files.js +3 -0
  100. package/test/fixtures/eval/node_modules/file1.js +5 -0
  101. package/test/fixtures/eval/node_modules/file10.js +5 -0
  102. package/test/fixtures/eval/node_modules/file11.js +5 -0
  103. package/test/fixtures/eval/node_modules/file12.js +5 -0
  104. package/test/fixtures/eval/node_modules/file13.js +5 -0
  105. package/test/fixtures/eval/node_modules/file14.js +11 -0
  106. package/test/fixtures/eval/node_modules/file2.js +5 -0
  107. package/test/fixtures/eval/node_modules/file3.js +5 -0
  108. package/test/fixtures/eval/node_modules/file4.js +5 -0
  109. package/test/fixtures/eval/node_modules/file5.js +5 -0
  110. package/test/fixtures/eval/node_modules/file6.js +5 -0
  111. package/test/fixtures/eval/node_modules/file7.js +5 -0
  112. package/test/fixtures/eval/node_modules/file8.js +5 -0
  113. package/test/fixtures/eval/node_modules/file9.js +5 -0
  114. package/test/fixtures/noop-transport.js +10 -0
  115. package/test/fixtures/pretty/null-prototype.js +8 -0
  116. package/test/fixtures/stdout-hack-protection.js +11 -0
  117. package/test/fixtures/syncfalse-child.js +6 -0
  118. package/test/fixtures/syncfalse-exit.js +9 -0
  119. package/test/fixtures/syncfalse-flush-exit.js +10 -0
  120. package/test/fixtures/syncfalse.js +6 -0
  121. package/test/fixtures/syntax-error-esm.mjs +2 -0
  122. package/test/fixtures/to-file-transport-with-transform.js +20 -0
  123. package/test/fixtures/to-file-transport.js +13 -0
  124. package/test/fixtures/to-file-transport.mjs +8 -0
  125. package/test/fixtures/transport/index.js +12 -0
  126. package/test/fixtures/transport/package.json +5 -0
  127. package/test/fixtures/transport-exit-immediately-with-async-dest.js +16 -0
  128. package/test/fixtures/transport-exit-immediately.js +11 -0
  129. package/test/fixtures/transport-exit-on-ready.js +12 -0
  130. package/test/fixtures/transport-invalid-node-options.js +26 -0
  131. package/test/fixtures/transport-main.js +9 -0
  132. package/test/fixtures/transport-many-lines.js +29 -0
  133. package/test/fixtures/transport-preload-main.mjs +13 -0
  134. package/test/fixtures/transport-preload.mjs +13 -0
  135. package/test/fixtures/transport-string-stdout.js +9 -0
  136. package/test/fixtures/transport-transform.js +21 -0
  137. package/test/fixtures/transport-uses-pino-config.js +33 -0
  138. package/test/fixtures/transport-with-on-exit.js +12 -0
  139. package/test/fixtures/transport-worker-data.js +19 -0
  140. package/test/fixtures/transport-worker-name.js +24 -0
  141. package/test/fixtures/transport-worker.js +15 -0
  142. package/test/fixtures/transport-wrong-export-type.js +3 -0
  143. package/test/fixtures/ts/to-file-transport-native.mts +15 -0
  144. package/test/fixtures/ts/to-file-transport-with-transform.ts +18 -0
  145. package/test/fixtures/ts/to-file-transport.ts +11 -0
  146. package/test/fixtures/ts/transpile.cjs +36 -0
  147. package/test/fixtures/ts/transport-exit-immediately-with-async-dest.ts +15 -0
  148. package/test/fixtures/ts/transport-exit-immediately.ts +10 -0
  149. package/test/fixtures/ts/transport-exit-on-ready.ts +11 -0
  150. package/test/fixtures/ts/transport-main.ts +8 -0
  151. package/test/fixtures/ts/transport-string-stdout.ts +8 -0
  152. package/test/fixtures/ts/transport-worker.ts +14 -0
  153. package/test/formatters.test.js +364 -0
  154. package/test/helper.d.ts +4 -0
  155. package/test/helper.js +155 -0
  156. package/test/hooks.test.js +114 -0
  157. package/test/http.test.js +214 -0
  158. package/test/internals/version.test.js +17 -0
  159. package/test/is-level-enabled.test.js +179 -0
  160. package/test/jest/basic.spec.js +10 -0
  161. package/test/levels.test.js +810 -0
  162. package/test/metadata.test.js +120 -0
  163. package/test/mixin-merge-strategy.test.js +57 -0
  164. package/test/mixin.test.js +241 -0
  165. package/test/multistream.test.js +729 -0
  166. package/test/redact.test.js +893 -0
  167. package/test/serializers.test.js +257 -0
  168. package/test/stdout-protection.test.js +41 -0
  169. package/test/syncfalse.test.js +186 -0
  170. package/test/timestamp-nano.test.js +37 -0
  171. package/test/timestamp.test.js +124 -0
  172. package/test/transport/big.test.js +42 -0
  173. package/test/transport/bundlers-support.test.js +99 -0
  174. package/test/transport/caller.test.js +24 -0
  175. package/test/transport/core.test.js +733 -0
  176. package/test/transport/core.transpiled.test.ts +114 -0
  177. package/test/transport/crash.test.js +36 -0
  178. package/test/transport/module-link.test.js +241 -0
  179. package/test/transport/native-type-stripping.test.mjs +151 -0
  180. package/test/transport/node-options.test.js +116 -0
  181. package/test/transport/pipeline.test.js +137 -0
  182. package/test/transport/preload.test.js +54 -0
  183. package/test/transport/repl.test.js +15 -0
  184. package/test/transport/sync-false.test.js +67 -0
  185. package/test/transport/sync-true.test.js +57 -0
  186. package/test/transport/targets.test.js +48 -0
  187. package/test/transport/uses-pino-config.test.js +166 -0
  188. package/test/transport-stream.test.js +40 -0
  189. package/test/types/pino-import.test-d.cts +30 -0
  190. package/test/types/pino-multistream.test-d.ts +28 -0
  191. package/test/types/pino-top-export.test-d.ts +34 -0
  192. package/test/types/pino-transport.test-d.ts +156 -0
  193. package/test/types/pino-type-only.test-d.ts +75 -0
  194. package/test/types/pino.test-d.ts +683 -0
  195. package/test/types/pino.ts +91 -0
  196. package/tsconfig.json +14 -0
@@ -0,0 +1,733 @@
1
+ 'use strict'
2
+
3
+ const test = require('node:test')
4
+ const assert = require('node:assert')
5
+ const os = require('node:os')
6
+ const { join } = require('node:path')
7
+ const { once } = require('node:events')
8
+ const { setImmediate: immediate } = require('node:timers/promises')
9
+ const { readFile, writeFile } = require('node:fs').promises
10
+ const url = require('url')
11
+ const strip = require('strip-ansi')
12
+ const execa = require('execa')
13
+ const writer = require('flush-write-stream')
14
+ const rimraf = require('rimraf')
15
+ const tspl = require('@matteo.collina/tspl')
16
+
17
+ const { match, watchFileCreated, watchForWrite, file } = require('../helper')
18
+ const pino = require('../../')
19
+
20
+ const { tmpdir } = os
21
+ const pid = process.pid
22
+ const hostname = os.hostname()
23
+
24
+ test('pino.transport with file', async (t) => {
25
+ const destination = file()
26
+ const transport = pino.transport({
27
+ target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
28
+ options: { destination }
29
+ })
30
+ t.after(transport.end.bind(transport))
31
+ const instance = pino(transport)
32
+ instance.info('hello')
33
+ await watchFileCreated(destination)
34
+ const result = JSON.parse(await readFile(destination))
35
+ delete result.time
36
+ assert.deepEqual(result, {
37
+ pid,
38
+ hostname,
39
+ level: 30,
40
+ msg: 'hello'
41
+ })
42
+ })
43
+
44
+ test('pino.transport with file (no options + error handling)', async () => {
45
+ const transport = pino.transport({
46
+ target: join(__dirname, '..', 'fixtures', 'to-file-transport.js')
47
+ })
48
+ const [err] = await once(transport, 'error')
49
+ assert.equal(err.message, 'kaboom')
50
+ })
51
+
52
+ test('pino.transport with file URL', async (t) => {
53
+ const destination = file()
54
+ const transport = pino.transport({
55
+ target: url.pathToFileURL(join(__dirname, '..', 'fixtures', 'to-file-transport.js')).href,
56
+ options: { destination }
57
+ })
58
+ t.after(transport.end.bind(transport))
59
+ const instance = pino(transport)
60
+ instance.info('hello')
61
+ await watchFileCreated(destination)
62
+ const result = JSON.parse(await readFile(destination))
63
+ delete result.time
64
+ assert.deepEqual(result, {
65
+ pid,
66
+ hostname,
67
+ level: 30,
68
+ msg: 'hello'
69
+ })
70
+ })
71
+
72
+ test('pino.transport errors if file does not exists', (t, end) => {
73
+ const instance = pino.transport({
74
+ target: join(__dirname, '..', 'fixtures', 'non-existent-file'),
75
+ worker: {
76
+ stdin: true,
77
+ stdout: true,
78
+ stderr: true
79
+ }
80
+ })
81
+ instance.on('error', function () {
82
+ assert.ok('error received')
83
+ end()
84
+ })
85
+ })
86
+
87
+ test('pino.transport errors if transport worker module does not export a function', async (t) => {
88
+ // TODO: add case for non-pipelined single target (needs changes in thread-stream)
89
+ const plan = tspl(t, { plan: 2 })
90
+ const manyTargetsInstance = pino.transport({
91
+ targets: [{
92
+ level: 'info',
93
+ target: join(__dirname, '..', 'fixtures', 'transport-wrong-export-type.js')
94
+ }, {
95
+ level: 'info',
96
+ target: join(__dirname, '..', 'fixtures', 'transport-wrong-export-type.js')
97
+ }]
98
+ })
99
+ manyTargetsInstance.on('error', function (e) {
100
+ plan.equal(e.message, 'exported worker is not a function')
101
+ })
102
+
103
+ const pipelinedInstance = pino.transport({
104
+ pipeline: [{
105
+ target: join(__dirname, '..', 'fixtures', 'transport-wrong-export-type.js')
106
+ }]
107
+ })
108
+ pipelinedInstance.on('error', function (e) {
109
+ plan.equal(e.message, 'exported worker is not a function')
110
+ })
111
+
112
+ await plan
113
+ })
114
+
115
+ test('pino.transport with esm', async (t) => {
116
+ const destination = file()
117
+ const transport = pino.transport({
118
+ target: join(__dirname, '..', 'fixtures', 'to-file-transport.mjs'),
119
+ options: { destination }
120
+ })
121
+ const instance = pino(transport)
122
+ t.after(transport.end.bind(transport))
123
+ instance.info('hello')
124
+ await watchFileCreated(destination)
125
+ const result = JSON.parse(await readFile(destination))
126
+ delete result.time
127
+ assert.deepEqual(result, {
128
+ pid,
129
+ hostname,
130
+ level: 30,
131
+ msg: 'hello'
132
+ })
133
+ })
134
+
135
+ test('pino.transport with two files', async (t) => {
136
+ const dest1 = file()
137
+ const dest2 = file()
138
+ const transport = pino.transport({
139
+ targets: [{
140
+ level: 'info',
141
+ target: 'file://' + join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
142
+ options: { destination: dest1 }
143
+ }, {
144
+ level: 'info',
145
+ target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
146
+ options: { destination: dest2 }
147
+ }]
148
+ })
149
+ t.after(transport.end.bind(transport))
150
+ const instance = pino(transport)
151
+ instance.info('hello')
152
+ await Promise.all([watchFileCreated(dest1), watchFileCreated(dest2)])
153
+ const result1 = JSON.parse(await readFile(dest1))
154
+ delete result1.time
155
+ assert.deepEqual(result1, {
156
+ pid,
157
+ hostname,
158
+ level: 30,
159
+ msg: 'hello'
160
+ })
161
+ const result2 = JSON.parse(await readFile(dest2))
162
+ delete result2.time
163
+ assert.deepEqual(result2, {
164
+ pid,
165
+ hostname,
166
+ level: 30,
167
+ msg: 'hello'
168
+ })
169
+ })
170
+
171
+ test('pino.transport with two files and custom levels', async (t) => {
172
+ const dest1 = file()
173
+ const dest2 = file()
174
+ const transport = pino.transport({
175
+ targets: [{
176
+ level: 'info',
177
+ target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
178
+ options: { destination: dest1 }
179
+ }, {
180
+ level: 'foo',
181
+ target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
182
+ options: { destination: dest2 }
183
+ }],
184
+ levels: { trace: 10, debug: 20, info: 30, warn: 40, error: 50, fatal: 60, foo: 25 }
185
+ })
186
+ t.after(transport.end.bind(transport))
187
+ const instance = pino(transport)
188
+ instance.info('hello')
189
+ await Promise.all([watchFileCreated(dest1), watchFileCreated(dest2)])
190
+ const result1 = JSON.parse(await readFile(dest1))
191
+ delete result1.time
192
+ assert.deepEqual(result1, {
193
+ pid,
194
+ hostname,
195
+ level: 30,
196
+ msg: 'hello'
197
+ })
198
+ const result2 = JSON.parse(await readFile(dest2))
199
+ delete result2.time
200
+ assert.deepEqual(result2, {
201
+ pid,
202
+ hostname,
203
+ level: 30,
204
+ msg: 'hello'
205
+ })
206
+ })
207
+
208
+ test('pino.transport without specifying default levels', async (t) => {
209
+ const dest = file()
210
+ const transport = pino.transport({
211
+ targets: [{
212
+ level: 'foo',
213
+ target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
214
+ options: { destination: dest }
215
+ }],
216
+ levels: { foo: 25 }
217
+ })
218
+ t.after(transport.end.bind(transport))
219
+ const instance = pino(transport)
220
+ instance.info('hello')
221
+ await Promise.all([watchFileCreated(dest)])
222
+ const result1 = JSON.parse(await readFile(dest))
223
+ delete result1.time
224
+ assert.deepEqual(result1, {
225
+ pid,
226
+ hostname,
227
+ level: 30,
228
+ msg: 'hello'
229
+ })
230
+ })
231
+
232
+ test('pino.transport with two files and dedupe', async (t) => {
233
+ const dest1 = file()
234
+ const dest2 = file()
235
+ const transport = pino.transport({
236
+ dedupe: true,
237
+ targets: [{
238
+ level: 'info',
239
+ target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
240
+ options: { destination: dest1 }
241
+ }, {
242
+ level: 'error',
243
+ target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
244
+ options: { destination: dest2 }
245
+ }]
246
+ })
247
+ t.after(transport.end.bind(transport))
248
+ const instance = pino(transport)
249
+ instance.info('hello')
250
+ instance.error('world')
251
+ await Promise.all([watchFileCreated(dest1), watchFileCreated(dest2)])
252
+ const result1 = JSON.parse(await readFile(dest1))
253
+ delete result1.time
254
+ assert.deepEqual(result1, {
255
+ pid,
256
+ hostname,
257
+ level: 30,
258
+ msg: 'hello'
259
+ })
260
+ const result2 = JSON.parse(await readFile(dest2))
261
+ delete result2.time
262
+ assert.deepEqual(result2, {
263
+ pid,
264
+ hostname,
265
+ level: 50,
266
+ msg: 'world'
267
+ })
268
+ })
269
+
270
+ test('pino.transport with an array including a pino-pretty destination', async (t) => {
271
+ const dest1 = file()
272
+ const dest2 = file()
273
+ const transport = pino.transport({
274
+ targets: [{
275
+ level: 'info',
276
+ target: 'pino/file',
277
+ options: {
278
+ destination: dest1
279
+ }
280
+ }, {
281
+ level: 'info',
282
+ target: 'pino-pretty',
283
+ options: {
284
+ destination: dest2
285
+ }
286
+ }]
287
+ })
288
+ t.after(transport.end.bind(transport))
289
+ const instance = pino(transport)
290
+ instance.info('hello')
291
+ await Promise.all([watchFileCreated(dest1), watchFileCreated(dest2)])
292
+ const result1 = JSON.parse(await readFile(dest1))
293
+ delete result1.time
294
+ assert.deepEqual(result1, {
295
+ pid,
296
+ hostname,
297
+ level: 30,
298
+ msg: 'hello'
299
+ })
300
+ const actual = (await readFile(dest2)).toString()
301
+ assert.match(strip(actual), /\[.*\] INFO.*hello/)
302
+ })
303
+
304
+ test('no transport.end()', async (t) => {
305
+ const destination = file()
306
+ const transport = pino.transport({
307
+ target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
308
+ options: { destination }
309
+ })
310
+ const instance = pino(transport)
311
+ instance.info('hello')
312
+ await watchFileCreated(destination)
313
+ const result = JSON.parse(await readFile(destination))
314
+ delete result.time
315
+ assert.deepEqual(result, {
316
+ pid,
317
+ hostname,
318
+ level: 30,
319
+ msg: 'hello'
320
+ })
321
+ })
322
+
323
+ test('autoEnd = false', async (t) => {
324
+ const destination = file()
325
+ const count = process.listenerCount('exit')
326
+ const transport = pino.transport({
327
+ target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
328
+ options: { destination },
329
+ worker: { autoEnd: false }
330
+ })
331
+ t.after(transport.end.bind(transport))
332
+ await once(transport, 'ready')
333
+
334
+ const instance = pino(transport)
335
+ instance.info('hello')
336
+
337
+ await watchFileCreated(destination)
338
+
339
+ assert.equal(count, process.listenerCount('exit'))
340
+
341
+ const result = JSON.parse(await readFile(destination))
342
+ delete result.time
343
+ assert.deepEqual(result, {
344
+ pid,
345
+ hostname,
346
+ level: 30,
347
+ msg: 'hello'
348
+ })
349
+ })
350
+
351
+ test('pino.transport with target and targets', async () => {
352
+ assert.throws(
353
+ () => {
354
+ pino.transport({
355
+ target: '/a/file',
356
+ targets: [{
357
+ target: '/a/file'
358
+ }]
359
+ })
360
+ },
361
+ /only one of target or targets can be specified/
362
+ )
363
+ })
364
+
365
+ test('pino.transport with target pino/file', async (t) => {
366
+ const destination = file()
367
+ const transport = pino.transport({
368
+ target: 'pino/file',
369
+ options: { destination }
370
+ })
371
+ t.after(transport.end.bind(transport))
372
+ const instance = pino(transport)
373
+ instance.info('hello')
374
+ await watchFileCreated(destination)
375
+ const result = JSON.parse(await readFile(destination))
376
+ delete result.time
377
+ assert.deepEqual(result, {
378
+ pid,
379
+ hostname,
380
+ level: 30,
381
+ msg: 'hello'
382
+ })
383
+ })
384
+
385
+ test('pino.transport with target pino/file and mkdir option', async (t) => {
386
+ const folder = join(tmpdir(), `pino-${process.pid}-mkdir-transport-file`)
387
+ const destination = join(folder, 'log.txt')
388
+ t.after(() => {
389
+ try {
390
+ rimraf.sync(folder)
391
+ } catch (err) {
392
+ // ignore
393
+ }
394
+ })
395
+ const transport = pino.transport({
396
+ target: 'pino/file',
397
+ options: { destination, mkdir: true }
398
+ })
399
+ t.after(transport.end.bind(transport))
400
+ const instance = pino(transport)
401
+ instance.info('hello')
402
+ await watchFileCreated(destination)
403
+ const result = JSON.parse(await readFile(destination))
404
+ delete result.time
405
+ assert.deepEqual(result, {
406
+ pid,
407
+ hostname,
408
+ level: 30,
409
+ msg: 'hello'
410
+ })
411
+ })
412
+
413
+ test('pino.transport with target pino/file and append option', async (t) => {
414
+ const destination = file()
415
+ await writeFile(destination, JSON.stringify({ pid, hostname, time: Date.now(), level: 30, msg: 'hello' }))
416
+ const transport = pino.transport({
417
+ target: 'pino/file',
418
+ options: { destination, append: false }
419
+ })
420
+ t.after(transport.end.bind(transport))
421
+ const instance = pino(transport)
422
+ instance.info('goodbye')
423
+ await watchForWrite(destination, '"goodbye"')
424
+ const result = JSON.parse(await readFile(destination))
425
+ delete result.time
426
+ assert.deepEqual(result, {
427
+ pid,
428
+ hostname,
429
+ level: 30,
430
+ msg: 'goodbye'
431
+ })
432
+ })
433
+
434
+ test('pino.transport should error with unknown target', async () => {
435
+ assert.throws(
436
+ () => {
437
+ pino.transport({
438
+ target: 'origin',
439
+ caller: 'unknown-file.js'
440
+ })
441
+ },
442
+ /unable to determine transport target for "origin"/
443
+ )
444
+ })
445
+
446
+ test('pino.transport with target pino-pretty', async (t) => {
447
+ const destination = file()
448
+ const transport = pino.transport({
449
+ target: 'pino-pretty',
450
+ options: { destination }
451
+ })
452
+ t.after(transport.end.bind(transport))
453
+ const instance = pino(transport)
454
+ instance.info('hello')
455
+ await watchFileCreated(destination)
456
+ const actual = await readFile(destination, 'utf8')
457
+ assert.match(strip(actual), /\[.*\] INFO.*hello/)
458
+ })
459
+
460
+ test('sets worker data informing the transport that pino will send its config', async (t) => {
461
+ const plan = tspl(t, { plan: 1 })
462
+ const transport = pino.transport({
463
+ target: join(__dirname, '..', 'fixtures', 'transport-worker-data.js')
464
+ })
465
+ t.after(transport.end.bind(transport))
466
+ const instance = pino(transport)
467
+ transport.once('workerData', (workerData) => {
468
+ match(workerData.workerData, { pinoWillSendConfig: true })
469
+ plan.ok('passed')
470
+ })
471
+ instance.info('hello')
472
+
473
+ await plan
474
+ })
475
+
476
+ test('sets worker data informing the transport that pino will send its config (frozen file)', async (t) => {
477
+ const plan = tspl(t, { plan: 1 })
478
+ const config = {
479
+ transport: {
480
+ target: join(__dirname, '..', 'fixtures', 'transport-worker-data.js'),
481
+ options: {}
482
+ }
483
+ }
484
+ Object.freeze(config)
485
+ Object.freeze(config.transport)
486
+ Object.freeze(config.transport.options)
487
+ const instance = pino(config)
488
+ const transport = instance[pino.symbols.streamSym]
489
+ t.after(transport.end.bind(transport))
490
+ transport.once('workerData', (workerData) => {
491
+ match(workerData.workerData, { pinoWillSendConfig: true })
492
+ plan.ok('passed')
493
+ })
494
+ instance.info('hello')
495
+
496
+ await plan
497
+ })
498
+
499
+ test('stdout in worker', async () => {
500
+ let actual = ''
501
+ const child = execa(process.argv[0], [join(__dirname, '..', 'fixtures', 'transport-main.js')])
502
+
503
+ for await (const chunk of child.stdout) {
504
+ actual += chunk
505
+ }
506
+ assert.equal(strip(actual).match(/Hello/) != null, true)
507
+ })
508
+
509
+ test('log and exit on ready', async () => {
510
+ let actual = ''
511
+ const child = execa(process.argv[0], [join(__dirname, '..', 'fixtures', 'transport-exit-on-ready.js')])
512
+
513
+ child.stdout.pipe(writer((s, enc, cb) => {
514
+ actual += s
515
+ cb()
516
+ }))
517
+ await once(child, 'close')
518
+ await immediate()
519
+ assert.equal(strip(actual).match(/Hello/) != null, true)
520
+ })
521
+
522
+ test('log and exit before ready', async () => {
523
+ let actual = ''
524
+ const child = execa(process.argv[0], [join(__dirname, '..', 'fixtures', 'transport-exit-immediately.js')])
525
+
526
+ child.stdout.pipe(writer((s, enc, cb) => {
527
+ actual += s
528
+ cb()
529
+ }))
530
+ await once(child, 'close')
531
+ await immediate()
532
+ assert.equal(strip(actual).match(/Hello/) != null, true)
533
+ })
534
+
535
+ test('log and exit before ready with async dest', async () => {
536
+ const destination = file()
537
+ const child = execa(process.argv[0], [join(__dirname, '..', 'fixtures', 'transport-exit-immediately-with-async-dest.js'), destination])
538
+
539
+ await once(child, 'exit')
540
+
541
+ const actual = await readFile(destination, 'utf8')
542
+ assert.equal(strip(actual).match(/HELLO/) != null, true)
543
+ assert.equal(strip(actual).match(/WORLD/) != null, true)
544
+ })
545
+
546
+ test('string integer destination', async () => {
547
+ let actual = ''
548
+ const child = execa(process.argv[0], [join(__dirname, '..', 'fixtures', 'transport-string-stdout.js')])
549
+
550
+ child.stdout.pipe(writer((s, enc, cb) => {
551
+ actual += s
552
+ cb()
553
+ }))
554
+ await once(child, 'close')
555
+ await immediate()
556
+ assert.equal(strip(actual).match(/Hello/) != null, true)
557
+ })
558
+
559
+ test('pino transport options with target', async (t) => {
560
+ const destination = file()
561
+ const instance = pino({
562
+ transport: {
563
+ target: 'pino/file',
564
+ options: { destination }
565
+ }
566
+ })
567
+ const transportStream = instance[pino.symbols.streamSym]
568
+ t.after(transportStream.end.bind(transportStream))
569
+ instance.info('transport option test')
570
+ await watchFileCreated(destination)
571
+ const result = JSON.parse(await readFile(destination))
572
+ delete result.time
573
+ assert.deepEqual(result, {
574
+ pid,
575
+ hostname,
576
+ level: 30,
577
+ msg: 'transport option test'
578
+ })
579
+ })
580
+
581
+ test('pino transport options with targets', async (t) => {
582
+ const dest1 = file()
583
+ const dest2 = file()
584
+ const instance = pino({
585
+ transport: {
586
+ targets: [
587
+ { target: 'pino/file', options: { destination: dest1 } },
588
+ { target: 'pino/file', options: { destination: dest2 } }
589
+ ]
590
+ }
591
+ })
592
+ const transportStream = instance[pino.symbols.streamSym]
593
+ t.after(transportStream.end.bind(transportStream))
594
+ instance.info('transport option test')
595
+
596
+ await Promise.all([watchFileCreated(dest1), watchFileCreated(dest2)])
597
+ const result1 = JSON.parse(await readFile(dest1))
598
+ delete result1.time
599
+ assert.deepEqual(result1, {
600
+ pid,
601
+ hostname,
602
+ level: 30,
603
+ msg: 'transport option test'
604
+ })
605
+ const result2 = JSON.parse(await readFile(dest2))
606
+ delete result2.time
607
+ assert.deepEqual(result2, {
608
+ pid,
609
+ hostname,
610
+ level: 30,
611
+ msg: 'transport option test'
612
+ })
613
+ })
614
+
615
+ test('transport options with target and targets', async () => {
616
+ assert.throws(
617
+ () => {
618
+ pino({
619
+ transport: {
620
+ target: {},
621
+ targets: {}
622
+ }
623
+ })
624
+ },
625
+ /only one of target or targets can be specified/
626
+ )
627
+ })
628
+
629
+ test('transport options with target and stream', async () => {
630
+ assert.throws(
631
+ () => {
632
+ pino({
633
+ transport: {
634
+ target: {}
635
+ }
636
+ }, '/log/null')
637
+ },
638
+ /only one of option.transport or stream can be specified/
639
+ )
640
+ })
641
+
642
+ test('transport options with stream', async (t) => {
643
+ const dest1 = file()
644
+ const transportStream = pino.transport({ target: 'pino/file', options: { destination: dest1 } })
645
+ t.after(transportStream.end.bind(transportStream))
646
+ assert.throws(
647
+ () => {
648
+ pino({
649
+ transport: transportStream
650
+ })
651
+ },
652
+ Error('option.transport do not allow stream, please pass to option directly. e.g. pino(transport)')
653
+ )
654
+ })
655
+
656
+ test('pino.transport handles prototype pollution of __bundlerPathsOverrides', async (t) => {
657
+ // eslint-disable-next-line no-extend-native
658
+ Object.prototype.__bundlerPathsOverrides = { 'pino/file': '/malicious/path' }
659
+ t.after(() => {
660
+ delete Object.prototype.__bundlerPathsOverrides
661
+ })
662
+
663
+ const destination = file()
664
+ const transport = pino.transport({
665
+ target: 'pino/file',
666
+ options: { destination }
667
+ })
668
+ t.after(transport.end.bind(transport))
669
+
670
+ const instance = pino(transport)
671
+ instance.info('hello')
672
+ await watchFileCreated(destination)
673
+ const result = JSON.parse(await readFile(destination))
674
+ delete result.time
675
+ assert.deepEqual(result, {
676
+ pid,
677
+ hostname,
678
+ level: 30,
679
+ msg: 'hello'
680
+ })
681
+ })
682
+
683
+ const hasThreadName = 'threadName' in require('worker_threads')
684
+
685
+ test('pino.transport with single target sets worker thread name to target', { skip: !hasThreadName }, async (t) => {
686
+ const plan = tspl(t, { plan: 1 })
687
+ const transport = pino.transport({
688
+ target: join(__dirname, '..', 'fixtures', 'transport-worker-name.js')
689
+ })
690
+ t.after(transport.end.bind(transport))
691
+ const instance = pino(transport)
692
+ transport.once('workerThreadName', (name) => {
693
+ plan.equal(name, join(__dirname, '..', 'fixtures', 'transport-worker-name.js'))
694
+ })
695
+ instance.info('hello')
696
+
697
+ await plan
698
+ })
699
+
700
+ test('pino.transport with targets sets worker thread name to pino.transport', { skip: !hasThreadName }, async (t) => {
701
+ const plan = tspl(t, { plan: 1 })
702
+ const transport = pino.transport({
703
+ targets: [{
704
+ level: 'info',
705
+ target: join(__dirname, '..', 'fixtures', 'transport-worker-name.js')
706
+ }]
707
+ })
708
+ t.after(transport.end.bind(transport))
709
+ const instance = pino(transport)
710
+ transport.once('workerThreadName', (name) => {
711
+ plan.equal(name, 'pino.transport')
712
+ })
713
+ instance.info('hello')
714
+
715
+ await plan
716
+ })
717
+
718
+ test('pino.transport with pipeline sets worker thread name to pino.transport', { skip: !hasThreadName }, async (t) => {
719
+ const plan = tspl(t, { plan: 1 })
720
+ const transport = pino.transport({
721
+ pipeline: [{
722
+ target: join(__dirname, '..', 'fixtures', 'transport-worker-name.js')
723
+ }]
724
+ })
725
+ t.after(transport.end.bind(transport))
726
+ const instance = pino(transport)
727
+ transport.once('workerThreadName', (name) => {
728
+ plan.equal(name, 'pino.transport')
729
+ })
730
+ instance.info('hello')
731
+
732
+ await plan
733
+ })