@honeybadger-io/js 4.0.0-beta.5 → 4.0.2

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.
@@ -1,15 +1,12 @@
1
1
  import Client from './types/core/client';
2
- import { Config } from './types/core/types';
3
- interface BrowserConfig extends Config {
4
- async: boolean;
5
- maxErrors: number;
6
- }
2
+ import { BrowserConfig } from './types/core/types';
7
3
  declare class Honeybadger extends Client {
8
4
  config: BrowserConfig;
9
5
  constructor(opts?: Partial<BrowserConfig>);
10
- configure(opts?: Partial<BrowserConfig>): Client;
6
+ configure(opts?: Partial<BrowserConfig>): this;
11
7
  resetMaxErrors(): number;
12
- factory(opts?: Partial<BrowserConfig>): Honeybadger;
8
+ factory(opts?: Partial<BrowserConfig>): this;
9
+ checkIn(_id: string): Promise<void>;
13
10
  }
14
11
  declare const _default: Honeybadger;
15
12
  export default _default;
@@ -44,7 +44,45 @@
44
44
  return t;
45
45
  };
46
46
  return __assign.apply(this, arguments);
47
- };
47
+ };
48
+
49
+ function __awaiter(thisArg, _arguments, P, generator) {
50
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
51
+ return new (P || (P = Promise))(function (resolve, reject) {
52
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
53
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
54
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
55
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
56
+ });
57
+ }
58
+
59
+ function __generator(thisArg, body) {
60
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
61
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
62
+ function verb(n) { return function (v) { return step([n, v]); }; }
63
+ function step(op) {
64
+ if (f) throw new TypeError("Generator is already executing.");
65
+ while (_) try {
66
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
67
+ if (y = 0, t) op = [op[0] & 2, t.value];
68
+ switch (op[0]) {
69
+ case 0: case 1: t = op; break;
70
+ case 4: _.label++; return { value: op[1], done: false };
71
+ case 5: _.label++; y = op[1]; op = [0]; continue;
72
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
73
+ default:
74
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
75
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
76
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
77
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
78
+ if (t[2]) _.ops.pop();
79
+ _.trys.pop(); continue;
80
+ }
81
+ op = body.call(thisArg, _);
82
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
83
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
84
+ }
85
+ }
48
86
 
49
87
  var UNKNOWN_FUNCTION = '<unknown>';
50
88
  /**
@@ -232,24 +270,31 @@
232
270
  return [];
233
271
  }
234
272
  }
235
- function getSourceForBacktrace(backtrace, getSourceFileHandler, cb) {
236
- if (!getSourceFileHandler || !backtrace || !backtrace.length) {
237
- cb([]);
238
- return;
239
- }
240
- var result = [];
241
- var getSourceFromFile = function (index) {
242
- if (index === void 0) { index = 0; }
243
- if (!backtrace.length) {
244
- return cb(result);
245
- }
246
- var trace = backtrace.splice(0)[index];
247
- getSourceFileHandler(trace.file, (function (fileContent) {
248
- result[index] = getSourceCodeSnippet(fileContent, trace.number);
249
- getSourceFromFile(index + 1);
250
- }));
251
- };
252
- getSourceFromFile();
273
+ function getSourceForBacktrace(backtrace, getSourceFileHandler) {
274
+ return __awaiter(this, void 0, void 0, function () {
275
+ var result, index, trace, fileContent;
276
+ return __generator(this, function (_a) {
277
+ switch (_a.label) {
278
+ case 0:
279
+ result = [];
280
+ if (!getSourceFileHandler || !backtrace || !backtrace.length) {
281
+ return [2 /*return*/, result];
282
+ }
283
+ index = 0;
284
+ _a.label = 1;
285
+ case 1:
286
+ if (!backtrace.length) return [3 /*break*/, 3];
287
+ trace = backtrace.splice(0)[index];
288
+ return [4 /*yield*/, getSourceFileHandler(trace.file)];
289
+ case 2:
290
+ fileContent = _a.sent();
291
+ result[index] = getSourceCodeSnippet(fileContent, trace.number);
292
+ index++;
293
+ return [3 /*break*/, 1];
294
+ case 3: return [2 /*return*/, result];
295
+ }
296
+ });
297
+ });
253
298
  }
254
299
  function runBeforeNotifyHandlers(notice, handlers) {
255
300
  var result = true;
@@ -434,8 +479,8 @@
434
479
  // User-Agent: Mozilla/5.0 (Linux; Android 10; SAMSUNG SM-G960F) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/12.1 Chrome/79.0.3945.136 Mobile Safari/537.36
435
480
  }
436
481
  }
437
- function endpoint(config, path) {
438
- var endpoint = config.endpoint.trim().replace(/\/$/, '');
482
+ function endpoint(base, path) {
483
+ var endpoint = base.trim().replace(/\/$/, '');
439
484
  path = path.trim().replace(/(^\/|\/$)/g, '');
440
485
  return "".concat(endpoint, "/").concat(path);
441
486
  }
@@ -567,6 +612,9 @@
567
612
  }
568
613
  return result;
569
614
  }
615
+ function isBrowserConfig(config) {
616
+ return config.async !== undefined;
617
+ }
570
618
 
571
619
  var GlobalStore = /** @class */ (function () {
572
620
  function GlobalStore(store) {
@@ -589,14 +637,14 @@
589
637
  var notifier = {
590
638
  name: 'honeybadger-js',
591
639
  url: 'https://github.com/honeybadger-io/honeybadger-js',
592
- version: '4.0.0-beta.5'
640
+ version: '4.0.2'
593
641
  };
594
642
  // Split at commas and spaces
595
643
  var TAG_SEPARATOR = /,|\s+/;
596
644
  // Checks for non-blank characters
597
645
  var NOT_BLANK = /\S/;
598
646
  var Client = /** @class */ (function () {
599
- function Client(opts) {
647
+ function Client(opts, transport) {
600
648
  if (opts === void 0) { opts = {}; }
601
649
  this.__pluginsExecuted = false;
602
650
  this.__store = null;
@@ -606,11 +654,9 @@
606
654
  // First, we go with the global (shared) store.
607
655
  // Webserver middleware can then switch to the AsyncStore for async context tracking.
608
656
  this.__store = new GlobalStore({ context: {}, breadcrumbs: [] });
657
+ this.__transport = transport;
609
658
  this.logger = logger(this);
610
659
  }
611
- Client.prototype.factory = function (_opts) {
612
- throw (new Error('Must implement __factory in subclass'));
613
- };
614
660
  Client.prototype.getVersion = function () {
615
661
  return notifier.version;
616
662
  };
@@ -684,6 +730,9 @@
684
730
  this.logger.warn('could not send error report: no API key has been configured', notice);
685
731
  preConditionError = new Error('missing API key');
686
732
  }
733
+ // we need to have the source file data before the beforeNotifyHandlers,
734
+ // in case they modify them
735
+ var sourceCodeData = notice && notice.backtrace ? notice.backtrace.map(function (trace) { return shallowClone(trace); }) : null;
687
736
  var beforeNotifyResult = runBeforeNotifyHandlers(notice, this.__beforeNotifyHandlers);
688
737
  if (!preConditionError && !beforeNotifyResult) {
689
738
  this.logger.debug('skipping error report: beforeNotify handlers returned false', notice);
@@ -703,14 +752,41 @@
703
752
  });
704
753
  var breadcrumbs = this.__getStoreContentsOrDefault().breadcrumbs;
705
754
  notice.__breadcrumbs = this.config.breadcrumbsEnabled ? breadcrumbs.slice() : [];
706
- // we need to have the source file data before the beforeNotifyHandlers,
707
- // in case they modify them
708
- var sourceCodeData = notice && notice.backtrace ? notice.backtrace.map(function (trace) { return shallowClone(trace); }) : null;
709
- getSourceForBacktrace(sourceCodeData, this.__getSourceFileHandler, function (sourcePerTrace) {
755
+ getSourceForBacktrace(sourceCodeData, this.__getSourceFileHandler)
756
+ .then(function (sourcePerTrace) {
710
757
  sourcePerTrace.forEach(function (source, index) {
711
758
  notice.backtrace[index].source = source;
712
759
  });
713
- _this.__send(notice);
760
+ var payload = _this.__buildPayload(notice);
761
+ _this.__transport
762
+ .send({
763
+ headers: {
764
+ 'X-API-Key': _this.config.apiKey,
765
+ 'Content-Type': 'application/json',
766
+ 'Accept': 'text/json, application/json'
767
+ },
768
+ method: 'POST',
769
+ endpoint: endpoint(_this.config.endpoint, '/v1/notices/js'),
770
+ maxObjectDepth: _this.config.maxObjectDepth,
771
+ logger: _this.logger,
772
+ async: isBrowserConfig(_this.config) ? _this.config.async : undefined,
773
+ }, payload)
774
+ .then(function (res) {
775
+ if (res.statusCode !== 201) {
776
+ runAfterNotifyHandlers(notice, _this.__afterNotifyHandlers, new Error("Bad HTTP response: ".concat(res.statusCode)));
777
+ _this.logger.warn("Error report failed: unknown response from server. code=".concat(res.statusCode));
778
+ return;
779
+ }
780
+ var uuid = JSON.parse(res.body).id;
781
+ runAfterNotifyHandlers(merge(notice, {
782
+ id: uuid
783
+ }), _this.__afterNotifyHandlers);
784
+ _this.logger.info("Error report sent \u26A1 https://app.honeybadger.io/notice/".concat(uuid));
785
+ })
786
+ .catch(function (err) {
787
+ _this.logger.error('Error report failed: an unknown error occurred.', "message=".concat(err.message));
788
+ runAfterNotifyHandlers(notice, _this.__afterNotifyHandlers, err);
789
+ });
714
790
  });
715
791
  return true;
716
792
  };
@@ -778,7 +854,7 @@
778
854
  }
779
855
  var context = this.__getStoreContentsOrDefault().context;
780
856
  var noticeTags = this.__constructTags(notice.tags);
781
- var contextTags = this.__constructTags(context["tags"]);
857
+ var contextTags = this.__constructTags(context['tags']);
782
858
  var configTags = this.__constructTags(this.config.tags);
783
859
  // Turning into a Set will remove duplicates
784
860
  var tags = noticeTags.concat(contextTags).concat(configTags);
@@ -830,9 +906,6 @@
830
906
  }
831
907
  return (this.config.environment && this.config.developmentEnvironments.includes(this.config.environment));
832
908
  };
833
- Client.prototype.__send = function (_notice) {
834
- throw (new Error('Must implement send in subclass'));
835
- };
836
909
  Client.prototype.__buildPayload = function (notice) {
837
910
  var headers = filter(notice.headers, this.config.filters) || {};
838
911
  var cgiData = filter(__assign(__assign({}, notice.cgiData), formatCGIData(headers, 'HTTP_')), this.config.filters);
@@ -1494,11 +1567,37 @@
1494
1567
  };
1495
1568
  }
1496
1569
 
1570
+ var BrowserTransport = /** @class */ (function () {
1571
+ function BrowserTransport() {
1572
+ }
1573
+ BrowserTransport.prototype.send = function (options, payload) {
1574
+ return new Promise(function (resolve, reject) {
1575
+ try {
1576
+ var x_1 = new XMLHttpRequest();
1577
+ x_1.open(options.method, options.endpoint, options.async);
1578
+ if (Object.keys(options.headers || []).length) {
1579
+ for (var i in options.headers) {
1580
+ if (typeof options.headers[i] !== 'undefined') {
1581
+ x_1.setRequestHeader(i, String(options.headers[i]));
1582
+ }
1583
+ }
1584
+ }
1585
+ x_1.send(payload ? JSON.stringify(sanitize(payload, options.maxObjectDepth)) : undefined);
1586
+ x_1.onload = function () { return resolve({ statusCode: x_1.status, body: x_1.response }); };
1587
+ }
1588
+ catch (err) {
1589
+ reject(err);
1590
+ }
1591
+ });
1592
+ };
1593
+ return BrowserTransport;
1594
+ }());
1595
+
1497
1596
  var Honeybadger = /** @class */ (function (_super) {
1498
1597
  __extends(Honeybadger, _super);
1499
1598
  function Honeybadger(opts) {
1500
1599
  if (opts === void 0) { opts = {}; }
1501
- var _this = _super.call(this, __assign({ async: true, maxErrors: null, projectRoot: window.location.protocol + '//' + window.location.host }, opts)) || this;
1600
+ var _this = _super.call(this, __assign({ async: true, maxErrors: null, projectRoot: window.location.protocol + '//' + window.location.host }, opts), new BrowserTransport()) || this;
1502
1601
  /** @internal */
1503
1602
  _this.__errorsSent = 0;
1504
1603
  /** @internal */
@@ -1513,6 +1612,7 @@
1513
1612
  if (!notice.url) {
1514
1613
  notice.url = document.URL;
1515
1614
  }
1615
+ _this.__incrementErrorsCount();
1516
1616
  return true;
1517
1617
  }
1518
1618
  ];
@@ -1526,8 +1626,12 @@
1526
1626
  return (this.__errorsSent = 0);
1527
1627
  };
1528
1628
  Honeybadger.prototype.factory = function (opts) {
1629
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1529
1630
  return new Honeybadger(opts);
1530
1631
  };
1632
+ Honeybadger.prototype.checkIn = function (_id) {
1633
+ throw new Error('Honeybadger.checkIn() is not supported on the browser');
1634
+ };
1531
1635
  /** @internal */
1532
1636
  Honeybadger.prototype.__buildPayload = function (notice) {
1533
1637
  var cgiData = {
@@ -1553,36 +1657,6 @@
1553
1657
  payload.request.cgi_data = merge(cgiData, payload.request.cgi_data);
1554
1658
  return payload;
1555
1659
  };
1556
- /** @internal */
1557
- Honeybadger.prototype.__send = function (notice) {
1558
- var _this = this;
1559
- this.__incrementErrorsCount();
1560
- var payload = this.__buildPayload(notice);
1561
- try {
1562
- var x_1 = new XMLHttpRequest();
1563
- x_1.open('POST', endpoint(this.config, '/v1/notices/js'), this.config.async);
1564
- x_1.setRequestHeader('X-API-Key', this.config.apiKey);
1565
- x_1.setRequestHeader('Content-Type', 'application/json');
1566
- x_1.setRequestHeader('Accept', 'text/json, application/json');
1567
- x_1.send(JSON.stringify(sanitize(payload, this.config.maxObjectDepth)));
1568
- x_1.onload = function () {
1569
- if (x_1.status !== 201) {
1570
- runAfterNotifyHandlers(notice, _this.__afterNotifyHandlers, new Error("Bad HTTP response: ".concat(x_1.status)));
1571
- _this.logger.debug("Unable to send error report: ".concat(x_1.status, ": ").concat(x_1.statusText), x_1, notice);
1572
- return;
1573
- }
1574
- var uuid = JSON.parse(x_1.response).id;
1575
- runAfterNotifyHandlers(merge(notice, {
1576
- id: uuid
1577
- }), _this.__afterNotifyHandlers);
1578
- _this.logger.debug("Error report sent \u26A1 https://app.honeybadger.io/notice/".concat(uuid));
1579
- };
1580
- }
1581
- catch (err) {
1582
- runAfterNotifyHandlers(notice, this.__afterNotifyHandlers, err);
1583
- this.logger.error('Unable to send error report: error while initializing request', err, notice);
1584
- }
1585
- };
1586
1660
  /**
1587
1661
  * wrap always returns the same function so that callbacks can be removed via
1588
1662
  * removeEventListener.