You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB

'use strict'
const os = require('os')
const writer = require('flush-write-stream')
const split = require('split2')
const pid = process.pid
const hostname = os.hostname()
const v = 1
const isWin = process.platform === 'win32'
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)
is(chunk.v, v)
}
function sleep (ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
module.exports = { getPathToNull, sink, check, once, sleep }