UNPKG

5.21 kBJavaScriptView Raw
1var __defProp = Object.defineProperty;
2var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
3var __export = (target, all) => {
4 __markAsModule(target);
5 for (var name in all)
6 __defProp(target, name, { get: all[name], enumerable: true });
7};
8__export(exports, {
9 setup: () => setup
10});
11function setup(env) {
12 const createDebug = (namespace, logger) => {
13 let prevTime;
14 let enableOverride = null;
15 let namespacesCache;
16 let enabledCache;
17 const debug = (...args) => {
18 const self = debug;
19 const curr = Number(new Date());
20 const ms = curr - (prevTime || curr);
21 self.diff = ms;
22 self.prev = prevTime;
23 self.curr = curr;
24 prevTime = curr;
25 args[0] = createDebug.coerce(args[0]);
26 if (typeof args[0] !== "string") {
27 args.unshift("%O");
28 }
29 let index = 0;
30 args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
31 if (match === "%%") {
32 return "%";
33 }
34 index++;
35 const formatter = createDebug.formatters[format];
36 if (typeof formatter === "function") {
37 const val = args[index];
38 match = formatter.call(self, val);
39 args.splice(index, 1);
40 index--;
41 }
42 return match;
43 });
44 createDebug.formatArgs.call(self, args);
45 if (logger && typeof logger === "function") {
46 logger.apply(self, args);
47 }
48 if (debug.enabled) {
49 const logFn = self.log || createDebug.log;
50 logFn.apply(self, args);
51 }
52 };
53 debug.namespace = namespace;
54 debug.useColors = createDebug.useColors();
55 debug.color = createDebug.selectColor(namespace);
56 debug.extend = extend;
57 debug.destroy = createDebug.destroy;
58 Object.defineProperty(debug, "enabled", {
59 enumerable: true,
60 configurable: false,
61 get: () => {
62 if (enableOverride !== null) {
63 return enableOverride;
64 }
65 if (namespacesCache !== createDebug.namespaces) {
66 namespacesCache = createDebug.namespaces;
67 enabledCache = createDebug.enabled(namespace);
68 }
69 return enabledCache;
70 },
71 set: (v) => {
72 enableOverride = v;
73 }
74 });
75 if (typeof createDebug.init === "function") {
76 createDebug.init(debug);
77 }
78 return debug;
79 };
80 createDebug.debug = createDebug;
81 createDebug.default = createDebug;
82 createDebug.coerce = coerce;
83 createDebug.disable = disable;
84 createDebug.enable = enable;
85 createDebug.enabled = enabled;
86 createDebug.humanize = require("ms");
87 createDebug.destroy = destroy;
88 Object.keys(env).forEach((key) => {
89 createDebug[key] = env[key];
90 });
91 createDebug.names = [];
92 createDebug.skips = [];
93 createDebug.formatters = {};
94 function selectColor(namespace) {
95 let hash = 0;
96 for (let i = 0; i < namespace.length; i++) {
97 hash = (hash << 5) - hash + namespace.charCodeAt(i);
98 hash |= 0;
99 }
100 return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
101 }
102 createDebug.selectColor = selectColor;
103 function extend(namespace, delimiter) {
104 const newDebug = createDebug(this.namespace + (typeof delimiter === "undefined" ? ":" : delimiter) + namespace);
105 newDebug.log = this.log;
106 return newDebug;
107 }
108 function enable(namespaces) {
109 createDebug.save(namespaces);
110 createDebug.namespaces = namespaces;
111 createDebug.names = [];
112 createDebug.skips = [];
113 let i;
114 const split = (typeof namespaces === "string" ? namespaces : "").split(/[\s,]+/);
115 const len = split.length;
116 for (i = 0; i < len; i++) {
117 if (!split[i]) {
118 continue;
119 }
120 namespaces = split[i].replace(/\*/g, ".*?");
121 if (namespaces[0] === "-") {
122 createDebug.skips.push(new RegExp("^" + namespaces.substr(1) + "$"));
123 } else {
124 createDebug.names.push(new RegExp("^" + namespaces + "$"));
125 }
126 }
127 }
128 function disable() {
129 const namespaces = [
130 ...createDebug.names.map(toNamespace),
131 ...createDebug.skips.map(toNamespace).map((namespace) => "-" + namespace)
132 ].join(",");
133 createDebug.enable("");
134 return namespaces;
135 }
136 function enabled(name) {
137 if (name[name.length - 1] === "*") {
138 return true;
139 }
140 let i;
141 let len;
142 for (i = 0, len = createDebug.skips.length; i < len; i++) {
143 if (createDebug.skips[i].test(name)) {
144 return false;
145 }
146 }
147 for (i = 0, len = createDebug.names.length; i < len; i++) {
148 if (createDebug.names[i].test(name)) {
149 return true;
150 }
151 }
152 return false;
153 }
154 function toNamespace(regexp) {
155 return regexp.toString().substring(2, regexp.toString().length - 2).replace(/\.\*\?$/, "*");
156 }
157 function coerce(val) {
158 if (val instanceof Error) {
159 return val.stack || val.message;
160 }
161 return val;
162 }
163 function destroy() {
164 console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.");
165 }
166 createDebug.enable(createDebug.load());
167 return createDebug;
168}
169// Annotate the CommonJS export names for ESM import in node:
1700 && (module.exports = {
171 setup
172});