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

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.
@@ -570,7 +570,7 @@ var GlobalStore$1 = /** @class */ (function () {
570
570
  var notifier = {
571
571
  name: 'honeybadger-js',
572
572
  url: 'https://github.com/honeybadger-io/honeybadger-js',
573
- version: '4.0.0-beta.2'
573
+ version: '4.0.0-beta.5'
574
574
  };
575
575
  // Split at commas and spaces
576
576
  var TAG_SEPARATOR = /,|\s+/;
@@ -682,7 +682,7 @@ var Client = /** @class */ (function () {
682
682
  stack: notice.stack
683
683
  }
684
684
  });
685
- var breadcrumbs = this.__getStoreOrDefaultObject().breadcrumbs;
685
+ var breadcrumbs = this.__getStoreContentsOrDefault().breadcrumbs;
686
686
  notice.__breadcrumbs = this.config.breadcrumbsEnabled ? breadcrumbs.slice() : [];
687
687
  // we need to have the source file data before the beforeNotifyHandlers,
688
688
  // in case they modify them
@@ -757,7 +757,7 @@ var Client = /** @class */ (function () {
757
757
  if (objectIsEmpty(notice)) {
758
758
  return null;
759
759
  }
760
- var context = this.__getStoreOrDefaultObject().context;
760
+ var context = this.__getStoreContentsOrDefault().context;
761
761
  var noticeTags = this.__constructTags(notice.tags);
762
762
  var contextTags = this.__constructTags(context["tags"]);
763
763
  var configTags = this.__constructTags(this.config.tags);
@@ -858,13 +858,13 @@ var Client = /** @class */ (function () {
858
858
  /**
859
859
  * For ALS, the store may be uninitialized (if .run()` has not been called).
860
860
  * This provides an easy way to read the existing store object or fall back to a default.
861
- * Returns *a copy* of the store.
861
+ * Returns *a copy* of the store contents.
862
862
  * @internal
863
863
  */
864
- Client.prototype.__getStoreOrDefaultObject = function () {
865
- var existingStore = this.__store.getStore();
866
- var store = existingStore || {};
867
- return __assign({ context: {}, breadcrumbs: [] }, store);
864
+ Client.prototype.__getStoreContentsOrDefault = function () {
865
+ var existingStoreContents = this.__store.getStore();
866
+ var storeContents = existingStoreContents || {};
867
+ return __assign({ context: {}, breadcrumbs: [] }, storeContents);
868
868
  };
869
869
  return Client;
870
870
  }());
@@ -1078,7 +1078,7 @@ function fullUrl(req) {
1078
1078
  });
1079
1079
  }
1080
1080
  function requestHandler(req, res, next) {
1081
- this.run(next, next);
1081
+ this.withRequest(req, next, next);
1082
1082
  }
1083
1083
  function errorHandler(err, req, _res, next) {
1084
1084
  this.notify(err, {
@@ -1091,9 +1091,12 @@ function errorHandler(err, req, _res, next) {
1091
1091
  REQUEST_METHOD: req.method
1092
1092
  }
1093
1093
  });
1094
- return next(err);
1094
+ if (next) {
1095
+ return next(err);
1096
+ }
1095
1097
  }
1096
1098
 
1099
+ var kHoneybadgerStore = Symbol.for("kHoneybadgerStore");
1097
1100
  var Honeybadger = /** @class */ (function (_super) {
1098
1101
  __extends(Honeybadger, _super);
1099
1102
  function Honeybadger(opts) {
@@ -1165,11 +1168,20 @@ var Honeybadger = /** @class */ (function (_super) {
1165
1168
  req.end();
1166
1169
  });
1167
1170
  };
1171
+ // This method is intended for web frameworks.
1172
+ // It allows us to track context for individual requests without leaking to other requests
1173
+ // by doing two things:
1174
+ // 1. Using AsyncLocalStorage so the context is tracked across async operations.
1175
+ // 2. Attaching the store contents to the request object,
1176
+ // so, even if the store is destroyed, we can still recover the context for a given request
1168
1177
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1169
- Honeybadger.prototype.run = function (handler, onError) {
1178
+ Honeybadger.prototype.withRequest = function (request, handler, onError) {
1170
1179
  var _this = this;
1171
- var storeObject = this.__getStoreOrDefaultObject();
1180
+ var storeObject = (request[kHoneybadgerStore] || this.__getStoreContentsOrDefault());
1172
1181
  this.__setStore(AsyncStore);
1182
+ if (!request[kHoneybadgerStore]) {
1183
+ request[kHoneybadgerStore] = storeObject;
1184
+ }
1173
1185
  if (onError) {
1174
1186
  // ALS is fine for context-tracking, but `domain` allows us to catch errors
1175
1187
  // thrown asynchronously (timers, event emitters)
@@ -1,6 +1,6 @@
1
1
  import { Config, Logger, BeforeNotifyHandler, AfterNotifyHandler, Notice, Noticeable, HoneybadgerStore, BreadcrumbRecord } from './types';
2
2
  export default class Client {
3
- private __pluginsExecuted;
3
+ protected __pluginsExecuted: boolean;
4
4
  protected __store: HoneybadgerStore<{
5
5
  context: Record<string, unknown>;
6
6
  breadcrumbs: BreadcrumbRecord[];
@@ -91,3 +91,7 @@ export interface CGIData {
91
91
  [x: string]: unknown;
92
92
  }
93
93
  export declare type HoneybadgerStore<T> = Pick<AsyncLocalStorage<T>, 'getStore' | 'run'>;
94
+ export declare type DefaultStoreContents = {
95
+ context: Record<string, unknown>;
96
+ breadcrumbs: BreadcrumbRecord[];
97
+ };
@@ -8,7 +8,7 @@ declare class Honeybadger extends Client {
8
8
  lambdaHandler: typeof lambdaHandler;
9
9
  constructor(opts?: Partial<Config>);
10
10
  factory(opts?: Partial<Config>): Honeybadger;
11
- run<R>(handler: (...args: never[]) => R, onError?: (...args: any[]) => any): R | void;
11
+ withRequest<R>(request: Record<symbol, unknown>, handler: (...args: never[]) => R, onError?: (...args: any[]) => any): R | void;
12
12
  }
13
13
  declare const _default: Honeybadger;
14
14
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@honeybadger-io/js",
3
- "version": "4.0.0-beta.2",
3
+ "version": "4.0.0-beta.5",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/honeybadger-io/honeybadger-js",
6
6
  "author": {
@@ -29,7 +29,7 @@
29
29
  "url": "https://github.com/honeybadger-io/honeybadger-js/issues"
30
30
  },
31
31
  "scripts": {
32
- "prepare": "npm run build",
32
+ "prepare": "npm run build && husky install",
33
33
  "test": "concurrently \"npm:test:browser\" \"npm:test:server\"",
34
34
  "test:browser": "jest --env=jsdom --testPathPattern=\"test/unit/.*(?<![\\./]server)\\.test\\.(js|ts)\"",
35
35
  "test:server": "jest --env=node --testPathPattern=\"test/unit/.*(?<![\\./]browser)\\.test\\.(js|ts)\"",
@@ -45,6 +45,8 @@
45
45
  "stacktrace-parser": "^0.1.10"
46
46
  },
47
47
  "devDependencies": {
48
+ "@commitlint/cli": "^16.2.3",
49
+ "@commitlint/config-conventional": "^16.2.1",
48
50
  "@rollup/plugin-node-resolve": "^13.0.6",
49
51
  "@rollup/plugin-replace": "^4.0.0",
50
52
  "@rollup/plugin-typescript": "^8.3.0",
@@ -61,15 +63,16 @@
61
63
  "eslint-plugin-jest": "^26.0.0",
62
64
  "eslint-plugin-promise": "^6.0.0",
63
65
  "express": "^4.17.1",
66
+ "husky": "^7.0.4",
64
67
  "jest": "^27.4.4",
65
68
  "nock": "^13.2.1",
66
69
  "rollup": "^2.61.1",
67
70
  "rollup-plugin-uglify": "^6.0.2",
68
- "shipjs": "0.24.3",
71
+ "shipjs": "0.24.4",
69
72
  "sinon": "^13.0.1",
70
73
  "supertest": "^6.1.6",
71
74
  "ts-jest": "^27.1.1",
72
- "tsd": "^0.19.0",
75
+ "tsd": "^0.20.0",
73
76
  "tslib": "^2.3.1",
74
77
  "typescript": "^4.5.3"
75
78
  },