11cb0ef41Sopenharmony_ci/* eslint-env browser */ 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci/** 41cb0ef41Sopenharmony_ci * This is the web browser implementation of `debug()`. 51cb0ef41Sopenharmony_ci */ 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciexports.formatArgs = formatArgs; 81cb0ef41Sopenharmony_ciexports.save = save; 91cb0ef41Sopenharmony_ciexports.load = load; 101cb0ef41Sopenharmony_ciexports.useColors = useColors; 111cb0ef41Sopenharmony_ciexports.storage = localstorage(); 121cb0ef41Sopenharmony_ciexports.destroy = (() => { 131cb0ef41Sopenharmony_ci let warned = false; 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci return () => { 161cb0ef41Sopenharmony_ci if (!warned) { 171cb0ef41Sopenharmony_ci warned = true; 181cb0ef41Sopenharmony_ci console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'); 191cb0ef41Sopenharmony_ci } 201cb0ef41Sopenharmony_ci }; 211cb0ef41Sopenharmony_ci})(); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci/** 241cb0ef41Sopenharmony_ci * Colors. 251cb0ef41Sopenharmony_ci */ 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciexports.colors = [ 281cb0ef41Sopenharmony_ci '#0000CC', 291cb0ef41Sopenharmony_ci '#0000FF', 301cb0ef41Sopenharmony_ci '#0033CC', 311cb0ef41Sopenharmony_ci '#0033FF', 321cb0ef41Sopenharmony_ci '#0066CC', 331cb0ef41Sopenharmony_ci '#0066FF', 341cb0ef41Sopenharmony_ci '#0099CC', 351cb0ef41Sopenharmony_ci '#0099FF', 361cb0ef41Sopenharmony_ci '#00CC00', 371cb0ef41Sopenharmony_ci '#00CC33', 381cb0ef41Sopenharmony_ci '#00CC66', 391cb0ef41Sopenharmony_ci '#00CC99', 401cb0ef41Sopenharmony_ci '#00CCCC', 411cb0ef41Sopenharmony_ci '#00CCFF', 421cb0ef41Sopenharmony_ci '#3300CC', 431cb0ef41Sopenharmony_ci '#3300FF', 441cb0ef41Sopenharmony_ci '#3333CC', 451cb0ef41Sopenharmony_ci '#3333FF', 461cb0ef41Sopenharmony_ci '#3366CC', 471cb0ef41Sopenharmony_ci '#3366FF', 481cb0ef41Sopenharmony_ci '#3399CC', 491cb0ef41Sopenharmony_ci '#3399FF', 501cb0ef41Sopenharmony_ci '#33CC00', 511cb0ef41Sopenharmony_ci '#33CC33', 521cb0ef41Sopenharmony_ci '#33CC66', 531cb0ef41Sopenharmony_ci '#33CC99', 541cb0ef41Sopenharmony_ci '#33CCCC', 551cb0ef41Sopenharmony_ci '#33CCFF', 561cb0ef41Sopenharmony_ci '#6600CC', 571cb0ef41Sopenharmony_ci '#6600FF', 581cb0ef41Sopenharmony_ci '#6633CC', 591cb0ef41Sopenharmony_ci '#6633FF', 601cb0ef41Sopenharmony_ci '#66CC00', 611cb0ef41Sopenharmony_ci '#66CC33', 621cb0ef41Sopenharmony_ci '#9900CC', 631cb0ef41Sopenharmony_ci '#9900FF', 641cb0ef41Sopenharmony_ci '#9933CC', 651cb0ef41Sopenharmony_ci '#9933FF', 661cb0ef41Sopenharmony_ci '#99CC00', 671cb0ef41Sopenharmony_ci '#99CC33', 681cb0ef41Sopenharmony_ci '#CC0000', 691cb0ef41Sopenharmony_ci '#CC0033', 701cb0ef41Sopenharmony_ci '#CC0066', 711cb0ef41Sopenharmony_ci '#CC0099', 721cb0ef41Sopenharmony_ci '#CC00CC', 731cb0ef41Sopenharmony_ci '#CC00FF', 741cb0ef41Sopenharmony_ci '#CC3300', 751cb0ef41Sopenharmony_ci '#CC3333', 761cb0ef41Sopenharmony_ci '#CC3366', 771cb0ef41Sopenharmony_ci '#CC3399', 781cb0ef41Sopenharmony_ci '#CC33CC', 791cb0ef41Sopenharmony_ci '#CC33FF', 801cb0ef41Sopenharmony_ci '#CC6600', 811cb0ef41Sopenharmony_ci '#CC6633', 821cb0ef41Sopenharmony_ci '#CC9900', 831cb0ef41Sopenharmony_ci '#CC9933', 841cb0ef41Sopenharmony_ci '#CCCC00', 851cb0ef41Sopenharmony_ci '#CCCC33', 861cb0ef41Sopenharmony_ci '#FF0000', 871cb0ef41Sopenharmony_ci '#FF0033', 881cb0ef41Sopenharmony_ci '#FF0066', 891cb0ef41Sopenharmony_ci '#FF0099', 901cb0ef41Sopenharmony_ci '#FF00CC', 911cb0ef41Sopenharmony_ci '#FF00FF', 921cb0ef41Sopenharmony_ci '#FF3300', 931cb0ef41Sopenharmony_ci '#FF3333', 941cb0ef41Sopenharmony_ci '#FF3366', 951cb0ef41Sopenharmony_ci '#FF3399', 961cb0ef41Sopenharmony_ci '#FF33CC', 971cb0ef41Sopenharmony_ci '#FF33FF', 981cb0ef41Sopenharmony_ci '#FF6600', 991cb0ef41Sopenharmony_ci '#FF6633', 1001cb0ef41Sopenharmony_ci '#FF9900', 1011cb0ef41Sopenharmony_ci '#FF9933', 1021cb0ef41Sopenharmony_ci '#FFCC00', 1031cb0ef41Sopenharmony_ci '#FFCC33' 1041cb0ef41Sopenharmony_ci]; 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci/** 1071cb0ef41Sopenharmony_ci * Currently only WebKit-based Web Inspectors, Firefox >= v31, 1081cb0ef41Sopenharmony_ci * and the Firebug extension (any Firefox version) are known 1091cb0ef41Sopenharmony_ci * to support "%c" CSS customizations. 1101cb0ef41Sopenharmony_ci * 1111cb0ef41Sopenharmony_ci * TODO: add a `localStorage` variable to explicitly enable/disable colors 1121cb0ef41Sopenharmony_ci */ 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci// eslint-disable-next-line complexity 1151cb0ef41Sopenharmony_cifunction useColors() { 1161cb0ef41Sopenharmony_ci // NB: In an Electron preload script, document will be defined but not fully 1171cb0ef41Sopenharmony_ci // initialized. Since we know we're in Chrome, we'll just detect this case 1181cb0ef41Sopenharmony_ci // explicitly 1191cb0ef41Sopenharmony_ci if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) { 1201cb0ef41Sopenharmony_ci return true; 1211cb0ef41Sopenharmony_ci } 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci // Internet Explorer and Edge do not support colors. 1241cb0ef41Sopenharmony_ci if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) { 1251cb0ef41Sopenharmony_ci return false; 1261cb0ef41Sopenharmony_ci } 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ci // Is webkit? http://stackoverflow.com/a/16459606/376773 1291cb0ef41Sopenharmony_ci // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632 1301cb0ef41Sopenharmony_ci return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) || 1311cb0ef41Sopenharmony_ci // Is firebug? http://stackoverflow.com/a/398120/376773 1321cb0ef41Sopenharmony_ci (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) || 1331cb0ef41Sopenharmony_ci // Is firefox >= v31? 1341cb0ef41Sopenharmony_ci // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages 1351cb0ef41Sopenharmony_ci (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) || 1361cb0ef41Sopenharmony_ci // Double check webkit in userAgent just in case we are in a worker 1371cb0ef41Sopenharmony_ci (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/)); 1381cb0ef41Sopenharmony_ci} 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci/** 1411cb0ef41Sopenharmony_ci * Colorize log arguments if enabled. 1421cb0ef41Sopenharmony_ci * 1431cb0ef41Sopenharmony_ci * @api public 1441cb0ef41Sopenharmony_ci */ 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_cifunction formatArgs(args) { 1471cb0ef41Sopenharmony_ci args[0] = (this.useColors ? '%c' : '') + 1481cb0ef41Sopenharmony_ci this.namespace + 1491cb0ef41Sopenharmony_ci (this.useColors ? ' %c' : ' ') + 1501cb0ef41Sopenharmony_ci args[0] + 1511cb0ef41Sopenharmony_ci (this.useColors ? '%c ' : ' ') + 1521cb0ef41Sopenharmony_ci '+' + module.exports.humanize(this.diff); 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci if (!this.useColors) { 1551cb0ef41Sopenharmony_ci return; 1561cb0ef41Sopenharmony_ci } 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci const c = 'color: ' + this.color; 1591cb0ef41Sopenharmony_ci args.splice(1, 0, c, 'color: inherit'); 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci // The final "%c" is somewhat tricky, because there could be other 1621cb0ef41Sopenharmony_ci // arguments passed either before or after the %c, so we need to 1631cb0ef41Sopenharmony_ci // figure out the correct index to insert the CSS into 1641cb0ef41Sopenharmony_ci let index = 0; 1651cb0ef41Sopenharmony_ci let lastC = 0; 1661cb0ef41Sopenharmony_ci args[0].replace(/%[a-zA-Z%]/g, match => { 1671cb0ef41Sopenharmony_ci if (match === '%%') { 1681cb0ef41Sopenharmony_ci return; 1691cb0ef41Sopenharmony_ci } 1701cb0ef41Sopenharmony_ci index++; 1711cb0ef41Sopenharmony_ci if (match === '%c') { 1721cb0ef41Sopenharmony_ci // We only are interested in the *last* %c 1731cb0ef41Sopenharmony_ci // (the user may have provided their own) 1741cb0ef41Sopenharmony_ci lastC = index; 1751cb0ef41Sopenharmony_ci } 1761cb0ef41Sopenharmony_ci }); 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci args.splice(lastC, 0, c); 1791cb0ef41Sopenharmony_ci} 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ci/** 1821cb0ef41Sopenharmony_ci * Invokes `console.debug()` when available. 1831cb0ef41Sopenharmony_ci * No-op when `console.debug` is not a "function". 1841cb0ef41Sopenharmony_ci * If `console.debug` is not available, falls back 1851cb0ef41Sopenharmony_ci * to `console.log`. 1861cb0ef41Sopenharmony_ci * 1871cb0ef41Sopenharmony_ci * @api public 1881cb0ef41Sopenharmony_ci */ 1891cb0ef41Sopenharmony_ciexports.log = console.debug || console.log || (() => {}); 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ci/** 1921cb0ef41Sopenharmony_ci * Save `namespaces`. 1931cb0ef41Sopenharmony_ci * 1941cb0ef41Sopenharmony_ci * @param {String} namespaces 1951cb0ef41Sopenharmony_ci * @api private 1961cb0ef41Sopenharmony_ci */ 1971cb0ef41Sopenharmony_cifunction save(namespaces) { 1981cb0ef41Sopenharmony_ci try { 1991cb0ef41Sopenharmony_ci if (namespaces) { 2001cb0ef41Sopenharmony_ci exports.storage.setItem('debug', namespaces); 2011cb0ef41Sopenharmony_ci } else { 2021cb0ef41Sopenharmony_ci exports.storage.removeItem('debug'); 2031cb0ef41Sopenharmony_ci } 2041cb0ef41Sopenharmony_ci } catch (error) { 2051cb0ef41Sopenharmony_ci // Swallow 2061cb0ef41Sopenharmony_ci // XXX (@Qix-) should we be logging these? 2071cb0ef41Sopenharmony_ci } 2081cb0ef41Sopenharmony_ci} 2091cb0ef41Sopenharmony_ci 2101cb0ef41Sopenharmony_ci/** 2111cb0ef41Sopenharmony_ci * Load `namespaces`. 2121cb0ef41Sopenharmony_ci * 2131cb0ef41Sopenharmony_ci * @return {String} returns the previously persisted debug modes 2141cb0ef41Sopenharmony_ci * @api private 2151cb0ef41Sopenharmony_ci */ 2161cb0ef41Sopenharmony_cifunction load() { 2171cb0ef41Sopenharmony_ci let r; 2181cb0ef41Sopenharmony_ci try { 2191cb0ef41Sopenharmony_ci r = exports.storage.getItem('debug'); 2201cb0ef41Sopenharmony_ci } catch (error) { 2211cb0ef41Sopenharmony_ci // Swallow 2221cb0ef41Sopenharmony_ci // XXX (@Qix-) should we be logging these? 2231cb0ef41Sopenharmony_ci } 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ci // If debug isn't set in LS, and we're in Electron, try to load $DEBUG 2261cb0ef41Sopenharmony_ci if (!r && typeof process !== 'undefined' && 'env' in process) { 2271cb0ef41Sopenharmony_ci r = process.env.DEBUG; 2281cb0ef41Sopenharmony_ci } 2291cb0ef41Sopenharmony_ci 2301cb0ef41Sopenharmony_ci return r; 2311cb0ef41Sopenharmony_ci} 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ci/** 2341cb0ef41Sopenharmony_ci * Localstorage attempts to return the localstorage. 2351cb0ef41Sopenharmony_ci * 2361cb0ef41Sopenharmony_ci * This is necessary because safari throws 2371cb0ef41Sopenharmony_ci * when a user disables cookies/localstorage 2381cb0ef41Sopenharmony_ci * and you attempt to access it. 2391cb0ef41Sopenharmony_ci * 2401cb0ef41Sopenharmony_ci * @return {LocalStorage} 2411cb0ef41Sopenharmony_ci * @api private 2421cb0ef41Sopenharmony_ci */ 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_cifunction localstorage() { 2451cb0ef41Sopenharmony_ci try { 2461cb0ef41Sopenharmony_ci // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context 2471cb0ef41Sopenharmony_ci // The Browser also has localStorage in the global context. 2481cb0ef41Sopenharmony_ci return localStorage; 2491cb0ef41Sopenharmony_ci } catch (error) { 2501cb0ef41Sopenharmony_ci // Swallow 2511cb0ef41Sopenharmony_ci // XXX (@Qix-) should we be logging these? 2521cb0ef41Sopenharmony_ci } 2531cb0ef41Sopenharmony_ci} 2541cb0ef41Sopenharmony_ci 2551cb0ef41Sopenharmony_cimodule.exports = require('./common')(exports); 2561cb0ef41Sopenharmony_ci 2571cb0ef41Sopenharmony_ciconst {formatters} = module.exports; 2581cb0ef41Sopenharmony_ci 2591cb0ef41Sopenharmony_ci/** 2601cb0ef41Sopenharmony_ci * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. 2611cb0ef41Sopenharmony_ci */ 2621cb0ef41Sopenharmony_ci 2631cb0ef41Sopenharmony_ciformatters.j = function (v) { 2641cb0ef41Sopenharmony_ci try { 2651cb0ef41Sopenharmony_ci return JSON.stringify(v); 2661cb0ef41Sopenharmony_ci } catch (error) { 2671cb0ef41Sopenharmony_ci return '[UnexpectedJSONParseError]: ' + error.message; 2681cb0ef41Sopenharmony_ci } 2691cb0ef41Sopenharmony_ci}; 270