-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespace.ts
More file actions
107 lines (99 loc) · 2.97 KB
/
namespace.ts
File metadata and controls
107 lines (99 loc) · 2.97 KB
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
/**
* @fileoverview Namespace-handling helpers — `extractOptions`
* normalises the polymorphic first argument that every `*Ns`
* function takes, `getDebugJsInstance` per-namespace caches the
* underlying `debug-js` instance with `customLog` patched in, and
* `isEnabled` / `isDebug` / `isDebugNs` are the gate predicates
* every output function consults before writing.
*/
import { getDebug } from '../env/debug'
import { getSocketDebug } from '../env/socket'
import debugJs from '../external/debug'
import { StringPrototypeStartsWith } from '../primordials/string'
import { customLog, debugByNamespace } from './_internal'
import type { DebugOptions, NamespacesOrOptions } from './types'
/**
* Extract options from namespaces parameter.
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
export function extractOptions(namespaces: NamespacesOrOptions): DebugOptions {
return namespaces !== null && typeof namespaces === 'object'
? ({ __proto__: null, ...namespaces } as DebugOptions)
: ({ __proto__: null, namespaces } as DebugOptions)
}
/**
* Get or create a debug instance for a namespace.
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
export function getDebugJsInstance(namespace: string) {
let inst = debugByNamespace.get(namespace)
// Per-namespace cache hit; first-call always misses. Same-namespace
// hit fires only when isEnabled() reuses the cached probe.
/* c8 ignore start */
if (inst) {
return inst
}
if (
!getDebug() &&
getSocketDebug() &&
(namespace === 'error' || namespace === 'notice')
) {
debugJs.enable(namespace)
}
/* c8 ignore stop */
/* c8 ignore next - External debug library call */
inst = debugJs(namespace)
inst.log = customLog
debugByNamespace.set(namespace, inst)
return inst
}
/**
* Check if debug mode is enabled.
*/
/*@__NO_SIDE_EFFECTS__*/
export function isDebug(): boolean {
return !!getSocketDebug()
}
/**
* Check if debug mode is enabled.
*/
/*@__NO_SIDE_EFFECTS__*/
export function isDebugNs(namespaces: string | undefined): boolean {
return !!getSocketDebug() && isEnabled(namespaces)
}
/**
* Check if debug is enabled for given namespaces.
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
export function isEnabled(namespaces: string | undefined) {
// Check if debugging is enabled at all
if (!getSocketDebug()) {
return false
}
if (typeof namespaces !== 'string' || !namespaces || namespaces === '*') {
return true
}
// Namespace splitting logic is based the 'debug' package implementation:
// https://github.com/debug-js/debug/blob/4.4.1/src/common.js#L169-L173.
const split = namespaces
.trim()
.replace(/\s+/g, ',')
.split(',')
.filter(Boolean)
const names = []
const skips = []
for (const ns of split) {
if (StringPrototypeStartsWith(ns, '-')) {
skips.push(ns.slice(1))
} else {
names.push(ns)
}
}
if (names.length && !names.some(ns => getDebugJsInstance(ns).enabled)) {
return false
}
return skips.every(ns => !getDebugJsInstance(ns).enabled)
}