11cb0ef41Sopenharmony_ci/** 21cb0ef41Sopenharmony_ci * Module dependencies. 31cb0ef41Sopenharmony_ci */ 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst tty = require('tty'); 61cb0ef41Sopenharmony_ciconst util = require('util'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci/** 91cb0ef41Sopenharmony_ci * This is the Node.js implementation of `debug()`. 101cb0ef41Sopenharmony_ci */ 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciexports.init = init; 131cb0ef41Sopenharmony_ciexports.log = log; 141cb0ef41Sopenharmony_ciexports.formatArgs = formatArgs; 151cb0ef41Sopenharmony_ciexports.save = save; 161cb0ef41Sopenharmony_ciexports.load = load; 171cb0ef41Sopenharmony_ciexports.useColors = useColors; 181cb0ef41Sopenharmony_ciexports.destroy = util.deprecate( 191cb0ef41Sopenharmony_ci () => {}, 201cb0ef41Sopenharmony_ci 'Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.' 211cb0ef41Sopenharmony_ci); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci/** 241cb0ef41Sopenharmony_ci * Colors. 251cb0ef41Sopenharmony_ci */ 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciexports.colors = [6, 2, 3, 4, 5, 1]; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_citry { 301cb0ef41Sopenharmony_ci // Optional dependency (as in, doesn't need to be installed, NOT like optionalDependencies in package.json) 311cb0ef41Sopenharmony_ci // eslint-disable-next-line import/no-extraneous-dependencies 321cb0ef41Sopenharmony_ci const supportsColor = require('supports-color'); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) { 351cb0ef41Sopenharmony_ci exports.colors = [ 361cb0ef41Sopenharmony_ci 20, 371cb0ef41Sopenharmony_ci 21, 381cb0ef41Sopenharmony_ci 26, 391cb0ef41Sopenharmony_ci 27, 401cb0ef41Sopenharmony_ci 32, 411cb0ef41Sopenharmony_ci 33, 421cb0ef41Sopenharmony_ci 38, 431cb0ef41Sopenharmony_ci 39, 441cb0ef41Sopenharmony_ci 40, 451cb0ef41Sopenharmony_ci 41, 461cb0ef41Sopenharmony_ci 42, 471cb0ef41Sopenharmony_ci 43, 481cb0ef41Sopenharmony_ci 44, 491cb0ef41Sopenharmony_ci 45, 501cb0ef41Sopenharmony_ci 56, 511cb0ef41Sopenharmony_ci 57, 521cb0ef41Sopenharmony_ci 62, 531cb0ef41Sopenharmony_ci 63, 541cb0ef41Sopenharmony_ci 68, 551cb0ef41Sopenharmony_ci 69, 561cb0ef41Sopenharmony_ci 74, 571cb0ef41Sopenharmony_ci 75, 581cb0ef41Sopenharmony_ci 76, 591cb0ef41Sopenharmony_ci 77, 601cb0ef41Sopenharmony_ci 78, 611cb0ef41Sopenharmony_ci 79, 621cb0ef41Sopenharmony_ci 80, 631cb0ef41Sopenharmony_ci 81, 641cb0ef41Sopenharmony_ci 92, 651cb0ef41Sopenharmony_ci 93, 661cb0ef41Sopenharmony_ci 98, 671cb0ef41Sopenharmony_ci 99, 681cb0ef41Sopenharmony_ci 112, 691cb0ef41Sopenharmony_ci 113, 701cb0ef41Sopenharmony_ci 128, 711cb0ef41Sopenharmony_ci 129, 721cb0ef41Sopenharmony_ci 134, 731cb0ef41Sopenharmony_ci 135, 741cb0ef41Sopenharmony_ci 148, 751cb0ef41Sopenharmony_ci 149, 761cb0ef41Sopenharmony_ci 160, 771cb0ef41Sopenharmony_ci 161, 781cb0ef41Sopenharmony_ci 162, 791cb0ef41Sopenharmony_ci 163, 801cb0ef41Sopenharmony_ci 164, 811cb0ef41Sopenharmony_ci 165, 821cb0ef41Sopenharmony_ci 166, 831cb0ef41Sopenharmony_ci 167, 841cb0ef41Sopenharmony_ci 168, 851cb0ef41Sopenharmony_ci 169, 861cb0ef41Sopenharmony_ci 170, 871cb0ef41Sopenharmony_ci 171, 881cb0ef41Sopenharmony_ci 172, 891cb0ef41Sopenharmony_ci 173, 901cb0ef41Sopenharmony_ci 178, 911cb0ef41Sopenharmony_ci 179, 921cb0ef41Sopenharmony_ci 184, 931cb0ef41Sopenharmony_ci 185, 941cb0ef41Sopenharmony_ci 196, 951cb0ef41Sopenharmony_ci 197, 961cb0ef41Sopenharmony_ci 198, 971cb0ef41Sopenharmony_ci 199, 981cb0ef41Sopenharmony_ci 200, 991cb0ef41Sopenharmony_ci 201, 1001cb0ef41Sopenharmony_ci 202, 1011cb0ef41Sopenharmony_ci 203, 1021cb0ef41Sopenharmony_ci 204, 1031cb0ef41Sopenharmony_ci 205, 1041cb0ef41Sopenharmony_ci 206, 1051cb0ef41Sopenharmony_ci 207, 1061cb0ef41Sopenharmony_ci 208, 1071cb0ef41Sopenharmony_ci 209, 1081cb0ef41Sopenharmony_ci 214, 1091cb0ef41Sopenharmony_ci 215, 1101cb0ef41Sopenharmony_ci 220, 1111cb0ef41Sopenharmony_ci 221 1121cb0ef41Sopenharmony_ci ]; 1131cb0ef41Sopenharmony_ci } 1141cb0ef41Sopenharmony_ci} catch (error) { 1151cb0ef41Sopenharmony_ci // Swallow - we only care if `supports-color` is available; it doesn't have to be. 1161cb0ef41Sopenharmony_ci} 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci/** 1191cb0ef41Sopenharmony_ci * Build up the default `inspectOpts` object from the environment variables. 1201cb0ef41Sopenharmony_ci * 1211cb0ef41Sopenharmony_ci * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js 1221cb0ef41Sopenharmony_ci */ 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ciexports.inspectOpts = Object.keys(process.env).filter(key => { 1251cb0ef41Sopenharmony_ci return /^debug_/i.test(key); 1261cb0ef41Sopenharmony_ci}).reduce((obj, key) => { 1271cb0ef41Sopenharmony_ci // Camel-case 1281cb0ef41Sopenharmony_ci const prop = key 1291cb0ef41Sopenharmony_ci .substring(6) 1301cb0ef41Sopenharmony_ci .toLowerCase() 1311cb0ef41Sopenharmony_ci .replace(/_([a-z])/g, (_, k) => { 1321cb0ef41Sopenharmony_ci return k.toUpperCase(); 1331cb0ef41Sopenharmony_ci }); 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci // Coerce string value into JS value 1361cb0ef41Sopenharmony_ci let val = process.env[key]; 1371cb0ef41Sopenharmony_ci if (/^(yes|on|true|enabled)$/i.test(val)) { 1381cb0ef41Sopenharmony_ci val = true; 1391cb0ef41Sopenharmony_ci } else if (/^(no|off|false|disabled)$/i.test(val)) { 1401cb0ef41Sopenharmony_ci val = false; 1411cb0ef41Sopenharmony_ci } else if (val === 'null') { 1421cb0ef41Sopenharmony_ci val = null; 1431cb0ef41Sopenharmony_ci } else { 1441cb0ef41Sopenharmony_ci val = Number(val); 1451cb0ef41Sopenharmony_ci } 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci obj[prop] = val; 1481cb0ef41Sopenharmony_ci return obj; 1491cb0ef41Sopenharmony_ci}, {}); 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci/** 1521cb0ef41Sopenharmony_ci * Is stdout a TTY? Colored output is enabled when `true`. 1531cb0ef41Sopenharmony_ci */ 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_cifunction useColors() { 1561cb0ef41Sopenharmony_ci return 'colors' in exports.inspectOpts ? 1571cb0ef41Sopenharmony_ci Boolean(exports.inspectOpts.colors) : 1581cb0ef41Sopenharmony_ci tty.isatty(process.stderr.fd); 1591cb0ef41Sopenharmony_ci} 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci/** 1621cb0ef41Sopenharmony_ci * Adds ANSI color escape codes if enabled. 1631cb0ef41Sopenharmony_ci * 1641cb0ef41Sopenharmony_ci * @api public 1651cb0ef41Sopenharmony_ci */ 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_cifunction formatArgs(args) { 1681cb0ef41Sopenharmony_ci const {namespace: name, useColors} = this; 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci if (useColors) { 1711cb0ef41Sopenharmony_ci const c = this.color; 1721cb0ef41Sopenharmony_ci const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c); 1731cb0ef41Sopenharmony_ci const prefix = ` ${colorCode};1m${name} \u001B[0m`; 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ci args[0] = prefix + args[0].split('\n').join('\n' + prefix); 1761cb0ef41Sopenharmony_ci args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m'); 1771cb0ef41Sopenharmony_ci } else { 1781cb0ef41Sopenharmony_ci args[0] = getDate() + name + ' ' + args[0]; 1791cb0ef41Sopenharmony_ci } 1801cb0ef41Sopenharmony_ci} 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_cifunction getDate() { 1831cb0ef41Sopenharmony_ci if (exports.inspectOpts.hideDate) { 1841cb0ef41Sopenharmony_ci return ''; 1851cb0ef41Sopenharmony_ci } 1861cb0ef41Sopenharmony_ci return new Date().toISOString() + ' '; 1871cb0ef41Sopenharmony_ci} 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci/** 1901cb0ef41Sopenharmony_ci * Invokes `util.format()` with the specified arguments and writes to stderr. 1911cb0ef41Sopenharmony_ci */ 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_cifunction log(...args) { 1941cb0ef41Sopenharmony_ci return process.stderr.write(util.format(...args) + '\n'); 1951cb0ef41Sopenharmony_ci} 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci/** 1981cb0ef41Sopenharmony_ci * Save `namespaces`. 1991cb0ef41Sopenharmony_ci * 2001cb0ef41Sopenharmony_ci * @param {String} namespaces 2011cb0ef41Sopenharmony_ci * @api private 2021cb0ef41Sopenharmony_ci */ 2031cb0ef41Sopenharmony_cifunction save(namespaces) { 2041cb0ef41Sopenharmony_ci if (namespaces) { 2051cb0ef41Sopenharmony_ci process.env.DEBUG = namespaces; 2061cb0ef41Sopenharmony_ci } else { 2071cb0ef41Sopenharmony_ci // If you set a process.env field to null or undefined, it gets cast to the 2081cb0ef41Sopenharmony_ci // string 'null' or 'undefined'. Just delete instead. 2091cb0ef41Sopenharmony_ci delete process.env.DEBUG; 2101cb0ef41Sopenharmony_ci } 2111cb0ef41Sopenharmony_ci} 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ci/** 2141cb0ef41Sopenharmony_ci * Load `namespaces`. 2151cb0ef41Sopenharmony_ci * 2161cb0ef41Sopenharmony_ci * @return {String} returns the previously persisted debug modes 2171cb0ef41Sopenharmony_ci * @api private 2181cb0ef41Sopenharmony_ci */ 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_cifunction load() { 2211cb0ef41Sopenharmony_ci return process.env.DEBUG; 2221cb0ef41Sopenharmony_ci} 2231cb0ef41Sopenharmony_ci 2241cb0ef41Sopenharmony_ci/** 2251cb0ef41Sopenharmony_ci * Init logic for `debug` instances. 2261cb0ef41Sopenharmony_ci * 2271cb0ef41Sopenharmony_ci * Create a new `inspectOpts` object in case `useColors` is set 2281cb0ef41Sopenharmony_ci * differently for a particular `debug` instance. 2291cb0ef41Sopenharmony_ci */ 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_cifunction init(debug) { 2321cb0ef41Sopenharmony_ci debug.inspectOpts = {}; 2331cb0ef41Sopenharmony_ci 2341cb0ef41Sopenharmony_ci const keys = Object.keys(exports.inspectOpts); 2351cb0ef41Sopenharmony_ci for (let i = 0; i < keys.length; i++) { 2361cb0ef41Sopenharmony_ci debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]]; 2371cb0ef41Sopenharmony_ci } 2381cb0ef41Sopenharmony_ci} 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_cimodule.exports = require('./common')(exports); 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ciconst {formatters} = module.exports; 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ci/** 2451cb0ef41Sopenharmony_ci * Map %o to `util.inspect()`, all on a single line. 2461cb0ef41Sopenharmony_ci */ 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ciformatters.o = function (v) { 2491cb0ef41Sopenharmony_ci this.inspectOpts.colors = this.useColors; 2501cb0ef41Sopenharmony_ci return util.inspect(v, this.inspectOpts) 2511cb0ef41Sopenharmony_ci .split('\n') 2521cb0ef41Sopenharmony_ci .map(str => str.trim()) 2531cb0ef41Sopenharmony_ci .join(' '); 2541cb0ef41Sopenharmony_ci}; 2551cb0ef41Sopenharmony_ci 2561cb0ef41Sopenharmony_ci/** 2571cb0ef41Sopenharmony_ci * Map %O to `util.inspect()`, allowing multiple lines if needed. 2581cb0ef41Sopenharmony_ci */ 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ciformatters.O = function (v) { 2611cb0ef41Sopenharmony_ci this.inspectOpts.colors = this.useColors; 2621cb0ef41Sopenharmony_ci return util.inspect(v, this.inspectOpts); 2631cb0ef41Sopenharmony_ci}; 264