-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
106 lines (96 loc) · 3.71 KB
/
index.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
'use strict'
const pino = require('pino')
require('module').wrap = override
const debug = require('debug')
module.exports = pinoDebug
/* istanbul ignore next */
if (module.parent && module.parent.parent === null && module.parent.filename === null) {
// preloaded with -r flag
pinoDebug()
}
function pinoDebug (logger, opts) {
if (pinoDebug.called) throw Error('pino-debug can only be called once')
pinoDebug.called = true
opts = opts || {}
const auto = 'auto' in opts ? opts.auto : true
const map = opts.map || {}
const namespaces = getNamespaces()
debug.map = Object.keys(map).sort(byPrecision).reduce(function (m, k) {
if (auto) namespaces.push(k)
m.set(RegExp('^' + k.replace(/[\\^$+?.()|[\]{}]/g, '\\$&').replace(/\*/g, '.*?') + '$'), map[k])
return m
}, new Map())
debug.logger = logger || pino({ level: 'debug' })
if (opts.skip) {
opts.skip.map(function (ns) { return '-' + ns }).forEach(function (ns) { namespaces.push(ns) })
}
debug.enable(namespaces.join(','))
}
function getNamespaces () {
const namespaces = process.env.DEBUG
if (namespaces != null) {
return (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/)
}
return []
}
function byPrecision (a, b) {
const aix = a.indexOf('*')
const bix = b.indexOf('*')
if (aix === -1 && bix === -1) return 0
if (~aix && ~bix) {
if (aix > bix) return -1
if (aix < bix) return 1
return a.length < b.length ? 1 : (a.length === b.length ? 0 : -1)
}
return ~bix ? -1 : 1
}
function override (script) {
// Escape backslashes to prevent from interpreting backslashes on Windows platform
// during expression interpolation in ES6 template literal.
// Without this change, Windows path retrieved from require.resolve (eg.
// F:\Projekty\Learn\pino-debug\debug.js) will be interpreted during interpolation
// as F:ProjektyLearnpino-debugdebug.js and node.js will throw error
// Cannot find module 'F:ProjektyLearnpino-debugdebug.js'
const pathToPinoDebug = require.resolve('./debug').replace(/\\/g, '\\\\')
const head = `(function(exports, require, module, __filename, __dirname) {
require = (function (req) {
var pinoDebugOs = require('os')
var pinoDebugPath = require('path')
var Object = ({}).constructor
return Object.setPrototypeOf(function pinoDebugWrappedRequire(s) {
var dirname = __dirname.split(pinoDebugPath.sep)
var lastNodeModulesIndex = dirname.lastIndexOf('node_modules')
var isDebug = lastNodeModulesIndex >= 0 && dirname[lastNodeModulesIndex + 1] === 'debug'
var pathToPinoDebug = '${pathToPinoDebug}'
if (isDebug) {
var dbg = req(pathToPinoDebug)
var real = req(s)
if (s === './common') {
var wrapped = function pinoDebugWrapSetup(env) {
var orig = real(env)
Object.assign(dbg, orig)
Object.defineProperty(orig, 'save', {get: function () {
Object.defineProperty(orig, 'save', {value: dbg.save})
return orig.save
}, configurable: true})
return dbg
}
return wrapped
}
if (s === './debug') {
Object.assign(dbg, real)
Object.defineProperty(real, 'save', {get: function () {
Object.defineProperty(real, 'save', {value: dbg.save})
return real.save
}, configurable: true})
return dbg
}
}
return req(s)
}, req)
}(require))
return (function(){
`.trim().replace(/\n/g, ';').replace(/\s+/g, ' ').replace(/;+/g, ';')
const tail = '\n}).call(this);})'
return head + script + tail
}