@iwoplaza/debug 4.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/common.js ADDED
@@ -0,0 +1,292 @@
1
+
2
+ /**
3
+ * This is the common logic for both the Node.js and web browser
4
+ * implementations of `debug()`.
5
+ */
6
+
7
+ function setup(env) {
8
+ createDebug.debug = createDebug;
9
+ createDebug.default = createDebug;
10
+ createDebug.coerce = coerce;
11
+ createDebug.disable = disable;
12
+ createDebug.enable = enable;
13
+ createDebug.enabled = enabled;
14
+ createDebug.humanize = require('ms');
15
+ createDebug.destroy = destroy;
16
+
17
+ Object.keys(env).forEach(key => {
18
+ createDebug[key] = env[key];
19
+ });
20
+
21
+ /**
22
+ * The currently active debug mode names, and names to skip.
23
+ */
24
+
25
+ createDebug.names = [];
26
+ createDebug.skips = [];
27
+
28
+ /**
29
+ * Map of special "%n" handling functions, for the debug "format" argument.
30
+ *
31
+ * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
32
+ */
33
+ createDebug.formatters = {};
34
+
35
+ /**
36
+ * Selects a color for a debug namespace
37
+ * @param {String} namespace The namespace string for the debug instance to be colored
38
+ * @return {Number|String} An ANSI color code for the given namespace
39
+ * @api private
40
+ */
41
+ function selectColor(namespace) {
42
+ let hash = 0;
43
+
44
+ for (let i = 0; i < namespace.length; i++) {
45
+ hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
46
+ hash |= 0; // Convert to 32bit integer
47
+ }
48
+
49
+ return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
50
+ }
51
+ createDebug.selectColor = selectColor;
52
+
53
+ /**
54
+ * Create a debugger with the given `namespace`.
55
+ *
56
+ * @param {String} namespace
57
+ * @return {Function}
58
+ * @api public
59
+ */
60
+ function createDebug(namespace) {
61
+ let prevTime;
62
+ let enableOverride = null;
63
+ let namespacesCache;
64
+ let enabledCache;
65
+
66
+ function debug(...args) {
67
+ // Disabled?
68
+ if (!debug.enabled) {
69
+ return;
70
+ }
71
+
72
+ const self = debug;
73
+
74
+ // Set `diff` timestamp
75
+ const curr = Number(new Date());
76
+ const ms = curr - (prevTime || curr);
77
+ self.diff = ms;
78
+ self.prev = prevTime;
79
+ self.curr = curr;
80
+ prevTime = curr;
81
+
82
+ args[0] = createDebug.coerce(args[0]);
83
+
84
+ if (typeof args[0] !== 'string') {
85
+ // Anything else let's inspect with %O
86
+ args.unshift('%O');
87
+ }
88
+
89
+ // Apply any `formatters` transformations
90
+ let index = 0;
91
+ args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
92
+ // If we encounter an escaped % then don't increase the array index
93
+ if (match === '%%') {
94
+ return '%';
95
+ }
96
+ index++;
97
+ const formatter = createDebug.formatters[format];
98
+ if (typeof formatter === 'function') {
99
+ const val = args[index];
100
+ match = formatter.call(self, val);
101
+
102
+ // Now we need to remove `args[index]` since it's inlined in the `format`
103
+ args.splice(index, 1);
104
+ index--;
105
+ }
106
+ return match;
107
+ });
108
+
109
+ // Apply env-specific formatting (colors, etc.)
110
+ createDebug.formatArgs.call(self, args);
111
+
112
+ const logFn = self.log || createDebug.log;
113
+ logFn.apply(self, args);
114
+ }
115
+
116
+ debug.namespace = namespace;
117
+ debug.useColors = createDebug.useColors();
118
+ debug.color = createDebug.selectColor(namespace);
119
+ debug.extend = extend;
120
+ debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release.
121
+
122
+ Object.defineProperty(debug, 'enabled', {
123
+ enumerable: true,
124
+ configurable: false,
125
+ get: () => {
126
+ if (enableOverride !== null) {
127
+ return enableOverride;
128
+ }
129
+ if (namespacesCache !== createDebug.namespaces) {
130
+ namespacesCache = createDebug.namespaces;
131
+ enabledCache = createDebug.enabled(namespace);
132
+ }
133
+
134
+ return enabledCache;
135
+ },
136
+ set: v => {
137
+ enableOverride = v;
138
+ }
139
+ });
140
+
141
+ // Env-specific initialization logic for debug instances
142
+ if (typeof createDebug.init === 'function') {
143
+ createDebug.init(debug);
144
+ }
145
+
146
+ return debug;
147
+ }
148
+
149
+ function extend(namespace, delimiter) {
150
+ const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
151
+ newDebug.log = this.log;
152
+ return newDebug;
153
+ }
154
+
155
+ /**
156
+ * Enables a debug mode by namespaces. This can include modes
157
+ * separated by a colon and wildcards.
158
+ *
159
+ * @param {String} namespaces
160
+ * @api public
161
+ */
162
+ function enable(namespaces) {
163
+ createDebug.save(namespaces);
164
+ createDebug.namespaces = namespaces;
165
+
166
+ createDebug.names = [];
167
+ createDebug.skips = [];
168
+
169
+ const split = (typeof namespaces === 'string' ? namespaces : '')
170
+ .trim()
171
+ .replace(/\s+/g, ',')
172
+ .split(',')
173
+ .filter(Boolean);
174
+
175
+ for (const ns of split) {
176
+ if (ns[0] === '-') {
177
+ createDebug.skips.push(ns.slice(1));
178
+ } else {
179
+ createDebug.names.push(ns);
180
+ }
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Checks if the given string matches a namespace template, honoring
186
+ * asterisks as wildcards.
187
+ *
188
+ * @param {String} search
189
+ * @param {String} template
190
+ * @return {Boolean}
191
+ */
192
+ function matchesTemplate(search, template) {
193
+ let searchIndex = 0;
194
+ let templateIndex = 0;
195
+ let starIndex = -1;
196
+ let matchIndex = 0;
197
+
198
+ while (searchIndex < search.length) {
199
+ if (templateIndex < template.length && (template[templateIndex] === search[searchIndex] || template[templateIndex] === '*')) {
200
+ // Match character or proceed with wildcard
201
+ if (template[templateIndex] === '*') {
202
+ starIndex = templateIndex;
203
+ matchIndex = searchIndex;
204
+ templateIndex++; // Skip the '*'
205
+ } else {
206
+ searchIndex++;
207
+ templateIndex++;
208
+ }
209
+ } else if (starIndex !== -1) { // eslint-disable-line no-negated-condition
210
+ // Backtrack to the last '*' and try to match more characters
211
+ templateIndex = starIndex + 1;
212
+ matchIndex++;
213
+ searchIndex = matchIndex;
214
+ } else {
215
+ return false; // No match
216
+ }
217
+ }
218
+
219
+ // Handle trailing '*' in template
220
+ while (templateIndex < template.length && template[templateIndex] === '*') {
221
+ templateIndex++;
222
+ }
223
+
224
+ return templateIndex === template.length;
225
+ }
226
+
227
+ /**
228
+ * Disable debug output.
229
+ *
230
+ * @return {String} namespaces
231
+ * @api public
232
+ */
233
+ function disable() {
234
+ const namespaces = [
235
+ ...createDebug.names,
236
+ ...createDebug.skips.map(namespace => '-' + namespace)
237
+ ].join(',');
238
+ createDebug.enable('');
239
+ return namespaces;
240
+ }
241
+
242
+ /**
243
+ * Returns true if the given mode name is enabled, false otherwise.
244
+ *
245
+ * @param {String} name
246
+ * @return {Boolean}
247
+ * @api public
248
+ */
249
+ function enabled(name) {
250
+ for (const skip of createDebug.skips) {
251
+ if (matchesTemplate(name, skip)) {
252
+ return false;
253
+ }
254
+ }
255
+
256
+ for (const ns of createDebug.names) {
257
+ if (matchesTemplate(name, ns)) {
258
+ return true;
259
+ }
260
+ }
261
+
262
+ return false;
263
+ }
264
+
265
+ /**
266
+ * Coerce `val`.
267
+ *
268
+ * @param {Mixed} val
269
+ * @return {Mixed}
270
+ * @api private
271
+ */
272
+ function coerce(val) {
273
+ if (val instanceof Error) {
274
+ return val.stack || val.message;
275
+ }
276
+ return val;
277
+ }
278
+
279
+ /**
280
+ * XXX DO NOT USE. This is a temporary stub function.
281
+ * XXX It WILL be removed in the next major release.
282
+ */
283
+ function destroy() {
284
+ console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
285
+ }
286
+
287
+ createDebug.enable(createDebug.load());
288
+
289
+ return createDebug;
290
+ }
291
+
292
+ module.exports = setup;
package/src/common.mjs ADDED
@@ -0,0 +1,293 @@
1
+
2
+ /**
3
+ * This is the common logic for both the Node.js and web browser
4
+ * implementations of `debug()`.
5
+ */
6
+ import humanize from 'ms';
7
+
8
+ function setup(env) {
9
+ createDebug.debug = createDebug;
10
+ createDebug.default = createDebug;
11
+ createDebug.coerce = coerce;
12
+ createDebug.disable = disable;
13
+ createDebug.enable = enable;
14
+ createDebug.enabled = enabled;
15
+ createDebug.humanize = humanize;
16
+ createDebug.destroy = destroy;
17
+
18
+ Object.keys(env).forEach(key => {
19
+ createDebug[key] = env[key];
20
+ });
21
+
22
+ /**
23
+ * The currently active debug mode names, and names to skip.
24
+ */
25
+
26
+ createDebug.names = [];
27
+ createDebug.skips = [];
28
+
29
+ /**
30
+ * Map of special "%n" handling functions, for the debug "format" argument.
31
+ *
32
+ * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
33
+ */
34
+ createDebug.formatters = {};
35
+
36
+ /**
37
+ * Selects a color for a debug namespace
38
+ * @param {String} namespace The namespace string for the debug instance to be colored
39
+ * @return {Number|String} An ANSI color code for the given namespace
40
+ * @api private
41
+ */
42
+ function selectColor(namespace) {
43
+ let hash = 0;
44
+
45
+ for (let i = 0; i < namespace.length; i++) {
46
+ hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
47
+ hash |= 0; // Convert to 32bit integer
48
+ }
49
+
50
+ return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
51
+ }
52
+ createDebug.selectColor = selectColor;
53
+
54
+ /**
55
+ * Create a debugger with the given `namespace`.
56
+ *
57
+ * @param {String} namespace
58
+ * @return {Function}
59
+ * @api public
60
+ */
61
+ function createDebug(namespace) {
62
+ let prevTime;
63
+ let enableOverride = null;
64
+ let namespacesCache;
65
+ let enabledCache;
66
+
67
+ function debug(...args) {
68
+ // Disabled?
69
+ if (!debug.enabled) {
70
+ return;
71
+ }
72
+
73
+ const self = debug;
74
+
75
+ // Set `diff` timestamp
76
+ const curr = Number(new Date());
77
+ const ms = curr - (prevTime || curr);
78
+ self.diff = ms;
79
+ self.prev = prevTime;
80
+ self.curr = curr;
81
+ prevTime = curr;
82
+
83
+ args[0] = createDebug.coerce(args[0]);
84
+
85
+ if (typeof args[0] !== 'string') {
86
+ // Anything else let's inspect with %O
87
+ args.unshift('%O');
88
+ }
89
+
90
+ // Apply any `formatters` transformations
91
+ let index = 0;
92
+ args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
93
+ // If we encounter an escaped % then don't increase the array index
94
+ if (match === '%%') {
95
+ return '%';
96
+ }
97
+ index++;
98
+ const formatter = createDebug.formatters[format];
99
+ if (typeof formatter === 'function') {
100
+ const val = args[index];
101
+ match = formatter.call(self, val);
102
+
103
+ // Now we need to remove `args[index]` since it's inlined in the `format`
104
+ args.splice(index, 1);
105
+ index--;
106
+ }
107
+ return match;
108
+ });
109
+
110
+ // Apply env-specific formatting (colors, etc.)
111
+ createDebug.formatArgs.call(self, args);
112
+
113
+ const logFn = self.log || createDebug.log;
114
+ logFn.apply(self, args);
115
+ }
116
+
117
+ debug.namespace = namespace;
118
+ debug.useColors = createDebug.useColors();
119
+ debug.color = createDebug.selectColor(namespace);
120
+ debug.extend = extend;
121
+ debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release.
122
+
123
+ Object.defineProperty(debug, 'enabled', {
124
+ enumerable: true,
125
+ configurable: false,
126
+ get: () => {
127
+ if (enableOverride !== null) {
128
+ return enableOverride;
129
+ }
130
+ if (namespacesCache !== createDebug.namespaces) {
131
+ namespacesCache = createDebug.namespaces;
132
+ enabledCache = createDebug.enabled(namespace);
133
+ }
134
+
135
+ return enabledCache;
136
+ },
137
+ set: v => {
138
+ enableOverride = v;
139
+ }
140
+ });
141
+
142
+ // Env-specific initialization logic for debug instances
143
+ if (typeof createDebug.init === 'function') {
144
+ createDebug.init(debug);
145
+ }
146
+
147
+ return debug;
148
+ }
149
+
150
+ function extend(namespace, delimiter) {
151
+ const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
152
+ newDebug.log = this.log;
153
+ return newDebug;
154
+ }
155
+
156
+ /**
157
+ * Enables a debug mode by namespaces. This can include modes
158
+ * separated by a colon and wildcards.
159
+ *
160
+ * @param {String} namespaces
161
+ * @api public
162
+ */
163
+ function enable(namespaces) {
164
+ createDebug.save(namespaces);
165
+ createDebug.namespaces = namespaces;
166
+
167
+ createDebug.names = [];
168
+ createDebug.skips = [];
169
+
170
+ const split = (typeof namespaces === 'string' ? namespaces : '')
171
+ .trim()
172
+ .replace(/\s+/g, ',')
173
+ .split(',')
174
+ .filter(Boolean);
175
+
176
+ for (const ns of split) {
177
+ if (ns[0] === '-') {
178
+ createDebug.skips.push(ns.slice(1));
179
+ } else {
180
+ createDebug.names.push(ns);
181
+ }
182
+ }
183
+ }
184
+
185
+ /**
186
+ * Checks if the given string matches a namespace template, honoring
187
+ * asterisks as wildcards.
188
+ *
189
+ * @param {String} search
190
+ * @param {String} template
191
+ * @return {Boolean}
192
+ */
193
+ function matchesTemplate(search, template) {
194
+ let searchIndex = 0;
195
+ let templateIndex = 0;
196
+ let starIndex = -1;
197
+ let matchIndex = 0;
198
+
199
+ while (searchIndex < search.length) {
200
+ if (templateIndex < template.length && (template[templateIndex] === search[searchIndex] || template[templateIndex] === '*')) {
201
+ // Match character or proceed with wildcard
202
+ if (template[templateIndex] === '*') {
203
+ starIndex = templateIndex;
204
+ matchIndex = searchIndex;
205
+ templateIndex++; // Skip the '*'
206
+ } else {
207
+ searchIndex++;
208
+ templateIndex++;
209
+ }
210
+ } else if (starIndex !== -1) { // eslint-disable-line no-negated-condition
211
+ // Backtrack to the last '*' and try to match more characters
212
+ templateIndex = starIndex + 1;
213
+ matchIndex++;
214
+ searchIndex = matchIndex;
215
+ } else {
216
+ return false; // No match
217
+ }
218
+ }
219
+
220
+ // Handle trailing '*' in template
221
+ while (templateIndex < template.length && template[templateIndex] === '*') {
222
+ templateIndex++;
223
+ }
224
+
225
+ return templateIndex === template.length;
226
+ }
227
+
228
+ /**
229
+ * Disable debug output.
230
+ *
231
+ * @return {String} namespaces
232
+ * @api public
233
+ */
234
+ function disable() {
235
+ const namespaces = [
236
+ ...createDebug.names,
237
+ ...createDebug.skips.map(namespace => '-' + namespace)
238
+ ].join(',');
239
+ createDebug.enable('');
240
+ return namespaces;
241
+ }
242
+
243
+ /**
244
+ * Returns true if the given mode name is enabled, false otherwise.
245
+ *
246
+ * @param {String} name
247
+ * @return {Boolean}
248
+ * @api public
249
+ */
250
+ function enabled(name) {
251
+ for (const skip of createDebug.skips) {
252
+ if (matchesTemplate(name, skip)) {
253
+ return false;
254
+ }
255
+ }
256
+
257
+ for (const ns of createDebug.names) {
258
+ if (matchesTemplate(name, ns)) {
259
+ return true;
260
+ }
261
+ }
262
+
263
+ return false;
264
+ }
265
+
266
+ /**
267
+ * Coerce `val`.
268
+ *
269
+ * @param {Mixed} val
270
+ * @return {Mixed}
271
+ * @api private
272
+ */
273
+ function coerce(val) {
274
+ if (val instanceof Error) {
275
+ return val.stack || val.message;
276
+ }
277
+ return val;
278
+ }
279
+
280
+ /**
281
+ * XXX DO NOT USE. This is a temporary stub function.
282
+ * XXX It WILL be removed in the next major release.
283
+ */
284
+ function destroy() {
285
+ console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
286
+ }
287
+
288
+ createDebug.enable(createDebug.load());
289
+
290
+ return createDebug;
291
+ }
292
+
293
+ export default setup;
package/src/index.js ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Detect Electron renderer / nwjs process, which is node, but we should
3
+ * treat as a browser.
4
+ */
5
+
6
+ if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
7
+ module.exports = require('./browser.js');
8
+ } else {
9
+ module.exports = require('./node.js');
10
+ }