@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/browser.js ADDED
@@ -0,0 +1,272 @@
1
+ /* eslint-env browser */
2
+
3
+ /**
4
+ * This is the web browser implementation of `debug()`.
5
+ */
6
+
7
+ exports.formatArgs = formatArgs;
8
+ exports.save = save;
9
+ exports.load = load;
10
+ exports.useColors = useColors;
11
+ exports.storage = localstorage();
12
+ exports.destroy = (() => {
13
+ let warned = false;
14
+
15
+ return () => {
16
+ if (!warned) {
17
+ warned = true;
18
+ console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
19
+ }
20
+ };
21
+ })();
22
+
23
+ /**
24
+ * Colors.
25
+ */
26
+
27
+ exports.colors = [
28
+ '#0000CC',
29
+ '#0000FF',
30
+ '#0033CC',
31
+ '#0033FF',
32
+ '#0066CC',
33
+ '#0066FF',
34
+ '#0099CC',
35
+ '#0099FF',
36
+ '#00CC00',
37
+ '#00CC33',
38
+ '#00CC66',
39
+ '#00CC99',
40
+ '#00CCCC',
41
+ '#00CCFF',
42
+ '#3300CC',
43
+ '#3300FF',
44
+ '#3333CC',
45
+ '#3333FF',
46
+ '#3366CC',
47
+ '#3366FF',
48
+ '#3399CC',
49
+ '#3399FF',
50
+ '#33CC00',
51
+ '#33CC33',
52
+ '#33CC66',
53
+ '#33CC99',
54
+ '#33CCCC',
55
+ '#33CCFF',
56
+ '#6600CC',
57
+ '#6600FF',
58
+ '#6633CC',
59
+ '#6633FF',
60
+ '#66CC00',
61
+ '#66CC33',
62
+ '#9900CC',
63
+ '#9900FF',
64
+ '#9933CC',
65
+ '#9933FF',
66
+ '#99CC00',
67
+ '#99CC33',
68
+ '#CC0000',
69
+ '#CC0033',
70
+ '#CC0066',
71
+ '#CC0099',
72
+ '#CC00CC',
73
+ '#CC00FF',
74
+ '#CC3300',
75
+ '#CC3333',
76
+ '#CC3366',
77
+ '#CC3399',
78
+ '#CC33CC',
79
+ '#CC33FF',
80
+ '#CC6600',
81
+ '#CC6633',
82
+ '#CC9900',
83
+ '#CC9933',
84
+ '#CCCC00',
85
+ '#CCCC33',
86
+ '#FF0000',
87
+ '#FF0033',
88
+ '#FF0066',
89
+ '#FF0099',
90
+ '#FF00CC',
91
+ '#FF00FF',
92
+ '#FF3300',
93
+ '#FF3333',
94
+ '#FF3366',
95
+ '#FF3399',
96
+ '#FF33CC',
97
+ '#FF33FF',
98
+ '#FF6600',
99
+ '#FF6633',
100
+ '#FF9900',
101
+ '#FF9933',
102
+ '#FFCC00',
103
+ '#FFCC33'
104
+ ];
105
+
106
+ /**
107
+ * Currently only WebKit-based Web Inspectors, Firefox >= v31,
108
+ * and the Firebug extension (any Firefox version) are known
109
+ * to support "%c" CSS customizations.
110
+ *
111
+ * TODO: add a `localStorage` variable to explicitly enable/disable colors
112
+ */
113
+
114
+ // eslint-disable-next-line complexity
115
+ function useColors() {
116
+ // NB: In an Electron preload script, document will be defined but not fully
117
+ // initialized. Since we know we're in Chrome, we'll just detect this case
118
+ // explicitly
119
+ if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
120
+ return true;
121
+ }
122
+
123
+ // Internet Explorer and Edge do not support colors.
124
+ if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
125
+ return false;
126
+ }
127
+
128
+ let m;
129
+
130
+ // Is webkit? http://stackoverflow.com/a/16459606/376773
131
+ // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
132
+ // eslint-disable-next-line no-return-assign
133
+ return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
134
+ // Is firebug? http://stackoverflow.com/a/398120/376773
135
+ (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
136
+ // Is firefox >= v31?
137
+ // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
138
+ (typeof navigator !== 'undefined' && navigator.userAgent && (m = navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)) && parseInt(m[1], 10) >= 31) ||
139
+ // Double check webkit in userAgent just in case we are in a worker
140
+ (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
141
+ }
142
+
143
+ /**
144
+ * Colorize log arguments if enabled.
145
+ *
146
+ * @api public
147
+ */
148
+
149
+ function formatArgs(args) {
150
+ args[0] = (this.useColors ? '%c' : '') +
151
+ this.namespace +
152
+ (this.useColors ? ' %c' : ' ') +
153
+ args[0] +
154
+ (this.useColors ? '%c ' : ' ') +
155
+ '+' + module.exports.humanize(this.diff);
156
+
157
+ if (!this.useColors) {
158
+ return;
159
+ }
160
+
161
+ const c = 'color: ' + this.color;
162
+ args.splice(1, 0, c, 'color: inherit');
163
+
164
+ // The final "%c" is somewhat tricky, because there could be other
165
+ // arguments passed either before or after the %c, so we need to
166
+ // figure out the correct index to insert the CSS into
167
+ let index = 0;
168
+ let lastC = 0;
169
+ args[0].replace(/%[a-zA-Z%]/g, match => {
170
+ if (match === '%%') {
171
+ return;
172
+ }
173
+ index++;
174
+ if (match === '%c') {
175
+ // We only are interested in the *last* %c
176
+ // (the user may have provided their own)
177
+ lastC = index;
178
+ }
179
+ });
180
+
181
+ args.splice(lastC, 0, c);
182
+ }
183
+
184
+ /**
185
+ * Invokes `console.debug()` when available.
186
+ * No-op when `console.debug` is not a "function".
187
+ * If `console.debug` is not available, falls back
188
+ * to `console.log`.
189
+ *
190
+ * @api public
191
+ */
192
+ exports.log = console.debug || console.log || (() => {});
193
+
194
+ /**
195
+ * Save `namespaces`.
196
+ *
197
+ * @param {String} namespaces
198
+ * @api private
199
+ */
200
+ function save(namespaces) {
201
+ try {
202
+ if (namespaces) {
203
+ exports.storage.setItem('debug', namespaces);
204
+ } else {
205
+ exports.storage.removeItem('debug');
206
+ }
207
+ } catch (error) {
208
+ // Swallow
209
+ // XXX (@Qix-) should we be logging these?
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Load `namespaces`.
215
+ *
216
+ * @return {String} returns the previously persisted debug modes
217
+ * @api private
218
+ */
219
+ function load() {
220
+ let r;
221
+ try {
222
+ r = exports.storage.getItem('debug') || exports.storage.getItem('DEBUG') ;
223
+ } catch (error) {
224
+ // Swallow
225
+ // XXX (@Qix-) should we be logging these?
226
+ }
227
+
228
+ // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
229
+ if (!r && typeof process !== 'undefined' && 'env' in process) {
230
+ r = process.env.DEBUG;
231
+ }
232
+
233
+ return r;
234
+ }
235
+
236
+ /**
237
+ * Localstorage attempts to return the localstorage.
238
+ *
239
+ * This is necessary because safari throws
240
+ * when a user disables cookies/localstorage
241
+ * and you attempt to access it.
242
+ *
243
+ * @return {LocalStorage}
244
+ * @api private
245
+ */
246
+
247
+ function localstorage() {
248
+ try {
249
+ // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
250
+ // The Browser also has localStorage in the global context.
251
+ return localStorage;
252
+ } catch (error) {
253
+ // Swallow
254
+ // XXX (@Qix-) should we be logging these?
255
+ }
256
+ }
257
+
258
+ module.exports = require('./common')(exports);
259
+
260
+ const {formatters} = module.exports;
261
+
262
+ /**
263
+ * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
264
+ */
265
+
266
+ formatters.j = function (v) {
267
+ try {
268
+ return JSON.stringify(v);
269
+ } catch (error) {
270
+ return '[UnexpectedJSONParseError]: ' + error.message;
271
+ }
272
+ };
@@ -0,0 +1,284 @@
1
+ /* eslint-env browser */
2
+
3
+ /**
4
+ * This is the web browser implementation of `debug()`.
5
+ */
6
+
7
+ import humanize from 'ms';
8
+
9
+ const storage = localstorage();
10
+ const destroy = (() => {
11
+ let warned = false;
12
+
13
+ return () => {
14
+ if (!warned) {
15
+ warned = true;
16
+ console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
17
+ }
18
+ };
19
+ })();
20
+
21
+
22
+ /**
23
+ * Colors.
24
+ */
25
+
26
+ export const colors = [
27
+ '#0000CC',
28
+ '#0000FF',
29
+ '#0033CC',
30
+ '#0033FF',
31
+ '#0066CC',
32
+ '#0066FF',
33
+ '#0099CC',
34
+ '#0099FF',
35
+ '#00CC00',
36
+ '#00CC33',
37
+ '#00CC66',
38
+ '#00CC99',
39
+ '#00CCCC',
40
+ '#00CCFF',
41
+ '#3300CC',
42
+ '#3300FF',
43
+ '#3333CC',
44
+ '#3333FF',
45
+ '#3366CC',
46
+ '#3366FF',
47
+ '#3399CC',
48
+ '#3399FF',
49
+ '#33CC00',
50
+ '#33CC33',
51
+ '#33CC66',
52
+ '#33CC99',
53
+ '#33CCCC',
54
+ '#33CCFF',
55
+ '#6600CC',
56
+ '#6600FF',
57
+ '#6633CC',
58
+ '#6633FF',
59
+ '#66CC00',
60
+ '#66CC33',
61
+ '#9900CC',
62
+ '#9900FF',
63
+ '#9933CC',
64
+ '#9933FF',
65
+ '#99CC00',
66
+ '#99CC33',
67
+ '#CC0000',
68
+ '#CC0033',
69
+ '#CC0066',
70
+ '#CC0099',
71
+ '#CC00CC',
72
+ '#CC00FF',
73
+ '#CC3300',
74
+ '#CC3333',
75
+ '#CC3366',
76
+ '#CC3399',
77
+ '#CC33CC',
78
+ '#CC33FF',
79
+ '#CC6600',
80
+ '#CC6633',
81
+ '#CC9900',
82
+ '#CC9933',
83
+ '#CCCC00',
84
+ '#CCCC33',
85
+ '#FF0000',
86
+ '#FF0033',
87
+ '#FF0066',
88
+ '#FF0099',
89
+ '#FF00CC',
90
+ '#FF00FF',
91
+ '#FF3300',
92
+ '#FF3333',
93
+ '#FF3366',
94
+ '#FF3399',
95
+ '#FF33CC',
96
+ '#FF33FF',
97
+ '#FF6600',
98
+ '#FF6633',
99
+ '#FF9900',
100
+ '#FF9933',
101
+ '#FFCC00',
102
+ '#FFCC33'
103
+ ];
104
+
105
+ /**
106
+ * Currently only WebKit-based Web Inspectors, Firefox >= v31,
107
+ * and the Firebug extension (any Firefox version) are known
108
+ * to support "%c" CSS customizations.
109
+ *
110
+ * TODO: add a `localStorage` variable to explicitly enable/disable colors
111
+ */
112
+
113
+ // eslint-disable-next-line complexity
114
+ export function useColors() {
115
+ // NB: In an Electron preload script, document will be defined but not fully
116
+ // initialized. Since we know we're in Chrome, we'll just detect this case
117
+ // explicitly
118
+ if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
119
+ return true;
120
+ }
121
+
122
+ // Internet Explorer and Edge do not support colors.
123
+ if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
124
+ return false;
125
+ }
126
+
127
+ let m;
128
+
129
+ // Is webkit? http://stackoverflow.com/a/16459606/376773
130
+ // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
131
+ // eslint-disable-next-line no-return-assign
132
+ return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
133
+ // Is firebug? http://stackoverflow.com/a/398120/376773
134
+ (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
135
+ // Is firefox >= v31?
136
+ // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
137
+ (typeof navigator !== 'undefined' && navigator.userAgent && (m = navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)) && parseInt(m[1], 10) >= 31) ||
138
+ // Double check webkit in userAgent just in case we are in a worker
139
+ (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
140
+ }
141
+
142
+ /**
143
+ * Colorize log arguments if enabled.
144
+ *
145
+ * @api public
146
+ */
147
+
148
+ export function formatArgs(args) {
149
+ args[0] = (this.useColors ? '%c' : '') +
150
+ this.namespace +
151
+ (this.useColors ? ' %c' : ' ') +
152
+ args[0] +
153
+ (this.useColors ? '%c ' : ' ') +
154
+ '+' + humanize(this.diff);
155
+
156
+ if (!this.useColors) {
157
+ return;
158
+ }
159
+
160
+ const c = 'color: ' + this.color;
161
+ args.splice(1, 0, c, 'color: inherit');
162
+
163
+ // The final "%c" is somewhat tricky, because there could be other
164
+ // arguments passed either before or after the %c, so we need to
165
+ // figure out the correct index to insert the CSS into
166
+ let index = 0;
167
+ let lastC = 0;
168
+ args[0].replace(/%[a-zA-Z%]/g, match => {
169
+ if (match === '%%') {
170
+ return;
171
+ }
172
+ index++;
173
+ if (match === '%c') {
174
+ // We only are interested in the *last* %c
175
+ // (the user may have provided their own)
176
+ lastC = index;
177
+ }
178
+ });
179
+
180
+ args.splice(lastC, 0, c);
181
+ }
182
+
183
+ /**
184
+ * Invokes `console.debug()` when available.
185
+ * No-op when `console.debug` is not a "function".
186
+ * If `console.debug` is not available, falls back
187
+ * to `console.log`.
188
+ *
189
+ * @api public
190
+ */
191
+ const log = console.debug || console.log || (() => { });
192
+
193
+ /**
194
+ * Save `namespaces`.
195
+ *
196
+ * @param {String} namespaces
197
+ * @api private
198
+ */
199
+ export function save(namespaces) {
200
+ try {
201
+ if (namespaces) {
202
+ storage.setItem('debug', namespaces);
203
+ } else {
204
+ storage.removeItem('debug');
205
+ }
206
+ } catch (error) {
207
+ // Swallow
208
+ // XXX (@Qix-) should we be logging these?
209
+ }
210
+ }
211
+
212
+ /**
213
+ * Load `namespaces`.
214
+ *
215
+ * @return {String} returns the previously persisted debug modes
216
+ * @api private
217
+ */
218
+ export function load() {
219
+ let r;
220
+ try {
221
+ r = storage.getItem('debug') || storage.getItem('DEBUG');
222
+ } catch (error) {
223
+ // Swallow
224
+ // XXX (@Qix-) should we be logging these?
225
+ }
226
+
227
+ // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
228
+ if (!r && typeof process !== 'undefined' && 'env' in process) {
229
+ r = process.env.DEBUG;
230
+ }
231
+
232
+ return r;
233
+ }
234
+
235
+ /**
236
+ * Localstorage attempts to return the localstorage.
237
+ *
238
+ * This is necessary because safari throws
239
+ * when a user disables cookies/localstorage
240
+ * and you attempt to access it.
241
+ *
242
+ * @return {LocalStorage}
243
+ * @api private
244
+ */
245
+
246
+ function localstorage() {
247
+ try {
248
+ // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
249
+ // The Browser also has localStorage in the global context.
250
+ return localStorage;
251
+ } catch (error) {
252
+ // Swallow
253
+ // XXX (@Qix-) should we be logging these?
254
+ }
255
+ }
256
+
257
+ const env = {
258
+ formatArgs,
259
+ save,
260
+ load,
261
+ useColors,
262
+ storage,
263
+ destroy,
264
+ colors,
265
+ log,
266
+ };
267
+
268
+ import setup from './common.mjs';
269
+ const DEFAULT = setup(env);
270
+
271
+ /**
272
+ * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
273
+ */
274
+
275
+ DEFAULT.formatters.j = function (v) {
276
+ try {
277
+ return JSON.stringify(v);
278
+ } catch (error) {
279
+ return '[UnexpectedJSONParseError]: ' + error.message;
280
+ }
281
+ };
282
+
283
+ export default DEFAULT;
284
+