debug.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _events = require("events");
  7. var util = _interopRequireWildcard(require("util"));
  8. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
  9. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  10. class Debug extends _events.EventEmitter {
  11. /*
  12. @options Which debug details should be sent.
  13. data - dump of packet data
  14. payload - details of decoded payload
  15. */
  16. constructor({
  17. data = false,
  18. payload = false,
  19. packet = false,
  20. token = false
  21. } = {}) {
  22. super();
  23. this.options = {
  24. data,
  25. payload,
  26. packet,
  27. token
  28. };
  29. this.indent = ' ';
  30. }
  31. packet(direction, packet) {
  32. if (this.haveListeners() && this.options.packet) {
  33. this.log('');
  34. this.log(direction);
  35. this.log(packet.headerToString(this.indent));
  36. }
  37. }
  38. data(packet) {
  39. if (this.haveListeners() && this.options.data) {
  40. this.log(packet.dataToString(this.indent));
  41. }
  42. }
  43. payload(generatePayloadText) {
  44. if (this.haveListeners() && this.options.payload) {
  45. this.log(generatePayloadText());
  46. }
  47. }
  48. token(token) {
  49. if (this.haveListeners() && this.options.token) {
  50. this.log(util.inspect(token, {
  51. showHidden: false,
  52. depth: 5,
  53. colors: true
  54. }));
  55. }
  56. }
  57. haveListeners() {
  58. return this.listeners('debug').length > 0;
  59. }
  60. log(text) {
  61. this.emit('debug', text);
  62. }
  63. }
  64. var _default = Debug;
  65. exports.default = _default;
  66. module.exports = Debug;