forked from pinojs/pino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
128 lines (115 loc) · 3.33 KB
/
helper.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
'use strict'
const crypto = require('crypto')
const os = require('node:os')
const writer = require('flush-write-stream')
const split = require('split2')
const { existsSync, readFileSync, statSync, unlinkSync } = require('node:fs')
const pid = process.pid
const hostname = os.hostname()
const t = require('tap')
const { join } = require('node:path')
const { tmpdir } = os
const isWin = process.platform === 'win32'
const isYarnPnp = process.versions.pnp !== undefined
function getPathToNull () {
return isWin ? '\\\\.\\NUL' : '/dev/null'
}
function once (emitter, name) {
return new Promise((resolve, reject) => {
if (name !== 'error') emitter.once('error', reject)
emitter.once(name, (...args) => {
emitter.removeListener('error', reject)
resolve(...args)
})
})
}
function sink (func) {
const result = split((data) => {
try {
return JSON.parse(data)
} catch (err) {
console.log(err)
console.log(data)
}
})
if (func) result.pipe(writer.obj(func))
return result
}
function check (is, chunk, level, msg) {
is(new Date(chunk.time) <= new Date(), true, 'time is greater than Date.now()')
delete chunk.time
is(chunk.pid, pid)
is(chunk.hostname, hostname)
is(chunk.level, level)
is(chunk.msg, msg)
}
function sleep (ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
function watchFileCreated (filename) {
return new Promise((resolve, reject) => {
const TIMEOUT = process.env.PINO_TEST_WAIT_WATCHFILE_TIMEOUT || 10000
const INTERVAL = 100
const threshold = TIMEOUT / INTERVAL
let counter = 0
const interval = setInterval(() => {
const exists = existsSync(filename)
// On some CI runs file is created but not filled
if (exists && statSync(filename).size !== 0) {
clearInterval(interval)
resolve()
} else if (counter <= threshold) {
counter++
} else {
clearInterval(interval)
reject(new Error(
`${filename} hasn't been created within ${TIMEOUT} ms. ` +
(exists ? 'File exist, but still empty.' : 'File not yet created.')
))
}
}, INTERVAL)
})
}
function watchForWrite (filename, testString) {
return new Promise((resolve, reject) => {
const TIMEOUT = process.env.PINO_TEST_WAIT_WRITE_TIMEOUT || 10000
const INTERVAL = 100
const threshold = TIMEOUT / INTERVAL
let counter = 0
const interval = setInterval(() => {
if (readFileSync(filename).includes(testString)) {
clearInterval(interval)
resolve()
} else if (counter <= threshold) {
counter++
} else {
clearInterval(interval)
reject(new Error(`'${testString}' hasn't been written to ${filename} within ${TIMEOUT} ms.`))
}
}, INTERVAL)
})
}
let files = []
function file () {
const hash = crypto.randomBytes(12).toString('hex')
const file = join(tmpdir(), `pino-${pid}-${hash}`)
files.push(file)
return file
}
process.on('beforeExit', () => {
if (files.length === 0) return
t.comment('unlink files')
for (const file of files) {
try {
t.comment(`unliking ${file}`)
unlinkSync(file)
} catch (e) {
console.log(e)
}
}
files = []
t.comment('unlink completed')
})
module.exports = { getPathToNull, sink, check, once, sleep, watchFileCreated, watchForWrite, isWin, isYarnPnp, file }