browser.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. "use strict";
  2. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. /* eslint-env browser */
  4. /**
  5. * This is the web browser implementation of `debug()`.
  6. */
  7. exports.log = log;
  8. exports.formatArgs = formatArgs;
  9. exports.save = save;
  10. exports.load = load;
  11. exports.useColors = useColors;
  12. exports.storage = localstorage();
  13. /**
  14. * Colors.
  15. */
  16. exports.colors = ['#0000CC', '#0000FF', '#0033CC', '#0033FF', '#0066CC', '#0066FF', '#0099CC', '#0099FF', '#00CC00', '#00CC33', '#00CC66', '#00CC99', '#00CCCC', '#00CCFF', '#3300CC', '#3300FF', '#3333CC', '#3333FF', '#3366CC', '#3366FF', '#3399CC', '#3399FF', '#33CC00', '#33CC33', '#33CC66', '#33CC99', '#33CCCC', '#33CCFF', '#6600CC', '#6600FF', '#6633CC', '#6633FF', '#66CC00', '#66CC33', '#9900CC', '#9900FF', '#9933CC', '#9933FF', '#99CC00', '#99CC33', '#CC0000', '#CC0033', '#CC0066', '#CC0099', '#CC00CC', '#CC00FF', '#CC3300', '#CC3333', '#CC3366', '#CC3399', '#CC33CC', '#CC33FF', '#CC6600', '#CC6633', '#CC9900', '#CC9933', '#CCCC00', '#CCCC33', '#FF0000', '#FF0033', '#FF0066', '#FF0099', '#FF00CC', '#FF00FF', '#FF3300', '#FF3333', '#FF3366', '#FF3399', '#FF33CC', '#FF33FF', '#FF6600', '#FF6633', '#FF9900', '#FF9933', '#FFCC00', '#FFCC33'];
  17. /**
  18. * Currently only WebKit-based Web Inspectors, Firefox >= v31,
  19. * and the Firebug extension (any Firefox version) are known
  20. * to support "%c" CSS customizations.
  21. *
  22. * TODO: add a `localStorage` variable to explicitly enable/disable colors
  23. */
  24. // eslint-disable-next-line complexity
  25. function useColors() {
  26. // NB: In an Electron preload script, document will be defined but not fully
  27. // initialized. Since we know we're in Chrome, we'll just detect this case
  28. // explicitly
  29. if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
  30. return true;
  31. } // Internet Explorer and Edge do not support colors.
  32. if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
  33. return false;
  34. } // Is webkit? http://stackoverflow.com/a/16459606/376773
  35. // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
  36. return typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance || // Is firebug? http://stackoverflow.com/a/398120/376773
  37. typeof window !== 'undefined' && window.console && (window.console.firebug || window.console.exception && window.console.table) || // Is firefox >= v31?
  38. // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
  39. typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31 || // Double check webkit in userAgent just in case we are in a worker
  40. typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/);
  41. }
  42. /**
  43. * Colorize log arguments if enabled.
  44. *
  45. * @api public
  46. */
  47. function formatArgs(args) {
  48. args[0] = (this.useColors ? '%c' : '') + this.namespace + (this.useColors ? ' %c' : ' ') + args[0] + (this.useColors ? '%c ' : ' ') + '+' + module.exports.humanize(this.diff);
  49. if (!this.useColors) {
  50. return;
  51. }
  52. var c = 'color: ' + this.color;
  53. args.splice(1, 0, c, 'color: inherit'); // The final "%c" is somewhat tricky, because there could be other
  54. // arguments passed either before or after the %c, so we need to
  55. // figure out the correct index to insert the CSS into
  56. var index = 0;
  57. var lastC = 0;
  58. args[0].replace(/%[a-zA-Z%]/g, function (match) {
  59. if (match === '%%') {
  60. return;
  61. }
  62. index++;
  63. if (match === '%c') {
  64. // We only are interested in the *last* %c
  65. // (the user may have provided their own)
  66. lastC = index;
  67. }
  68. });
  69. args.splice(lastC, 0, c);
  70. }
  71. /**
  72. * Invokes `console.log()` when available.
  73. * No-op when `console.log` is not a "function".
  74. *
  75. * @api public
  76. */
  77. function log() {
  78. var _console;
  79. // This hackery is required for IE8/9, where
  80. // the `console.log` function doesn't have 'apply'
  81. return (typeof console === "undefined" ? "undefined" : _typeof(console)) === 'object' && console.log && (_console = console).log.apply(_console, arguments);
  82. }
  83. /**
  84. * Save `namespaces`.
  85. *
  86. * @param {String} namespaces
  87. * @api private
  88. */
  89. function save(namespaces) {
  90. try {
  91. if (namespaces) {
  92. exports.storage.setItem('debug', namespaces);
  93. } else {
  94. exports.storage.removeItem('debug');
  95. }
  96. } catch (error) {// Swallow
  97. // XXX (@Qix-) should we be logging these?
  98. }
  99. }
  100. /**
  101. * Load `namespaces`.
  102. *
  103. * @return {String} returns the previously persisted debug modes
  104. * @api private
  105. */
  106. function load() {
  107. var r;
  108. try {
  109. r = exports.storage.getItem('debug');
  110. } catch (error) {} // Swallow
  111. // XXX (@Qix-) should we be logging these?
  112. // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
  113. if (!r && typeof process !== 'undefined' && 'env' in process) {
  114. r = process.env.DEBUG;
  115. }
  116. return r;
  117. }
  118. /**
  119. * Localstorage attempts to return the localstorage.
  120. *
  121. * This is necessary because safari throws
  122. * when a user disables cookies/localstorage
  123. * and you attempt to access it.
  124. *
  125. * @return {LocalStorage}
  126. * @api private
  127. */
  128. function localstorage() {
  129. try {
  130. // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
  131. // The Browser also has localStorage in the global context.
  132. return localStorage;
  133. } catch (error) {// Swallow
  134. // XXX (@Qix-) should we be logging these?
  135. }
  136. }
  137. module.exports = require('./common')(exports);
  138. var formatters = module.exports.formatters;
  139. /**
  140. * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
  141. */
  142. formatters.j = function (v) {
  143. try {
  144. return JSON.stringify(v);
  145. } catch (error) {
  146. return '[UnexpectedJSONParseError]: ' + error.message;
  147. }
  148. };