posthog-node 5.47.8 → 5.47.9

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,4 +1,7 @@
1
1
  import { ErrorTracking as CoreErrorTracking } from '@posthog/core';
2
- export declare function addUncaughtExceptionListener(captureFn: (exception: Error, hint: CoreErrorTracking.EventHint) => void, onFatalFn: (exception: Error) => void): void;
3
- export declare function addUnhandledRejectionListener(captureFn: (exception: unknown, hint: CoreErrorTracking.EventHint) => void): void;
2
+ type UnhandledRejectionMode = 'throw' | 'strict' | 'warn' | 'warn-with-error-code' | 'none';
3
+ export declare function getUnhandledRejectionMode(execArgv?: string[], nodeOptions?: string | undefined): UnhandledRejectionMode;
4
+ export declare function addUncaughtExceptionListener(captureFn: (exception: Error, hint: CoreErrorTracking.EventHint) => void, onFatalFn: (exception: Error) => void, mode?: UnhandledRejectionMode): void;
5
+ export declare function addUnhandledRejectionListener(captureFn: (exception: unknown, hint: CoreErrorTracking.EventHint) => void, mode?: UnhandledRejectionMode): void;
6
+ export {};
4
7
  //# sourceMappingURL=autocapture.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"autocapture.d.ts","sourceRoot":"","sources":["../../../src/extensions/error-tracking/autocapture.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,IAAI,iBAAiB,EAAE,MAAM,eAAe,CAAA;AA6ClE,wBAAgB,4BAA4B,CAC1C,SAAS,EAAE,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,iBAAiB,CAAC,SAAS,KAAK,IAAI,EACxE,SAAS,EAAE,CAAC,SAAS,EAAE,KAAK,KAAK,IAAI,GACpC,IAAI,CAEN;AAED,wBAAgB,6BAA6B,CAC3C,SAAS,EAAE,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,iBAAiB,CAAC,SAAS,KAAK,IAAI,GACzE,IAAI,CASN"}
1
+ {"version":3,"file":"autocapture.d.ts","sourceRoot":"","sources":["../../../src/extensions/error-tracking/autocapture.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,IAAI,iBAAiB,EAAE,MAAM,eAAe,CAAA;AAGlE,KAAK,sBAAsB,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,GAAG,sBAAsB,GAAG,MAAM,CAAA;AA+D3F,wBAAgB,yBAAyB,CACvC,QAAQ,GAAE,MAAM,EAAsB,EACtC,WAAW,GAAE,MAAM,GAAG,SAAgC,GACrD,sBAAsB,CAKxB;AA6CD,wBAAgB,4BAA4B,CAC1C,SAAS,EAAE,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,iBAAiB,CAAC,SAAS,KAAK,IAAI,EACxE,SAAS,EAAE,CAAC,SAAS,EAAE,KAAK,KAAK,IAAI,EACrC,IAAI,GAAE,sBAAyD,GAC9D,IAAI,CAYN;AAED,wBAAgB,6BAA6B,CAC3C,SAAS,EAAE,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,iBAAiB,CAAC,SAAS,KAAK,IAAI,EAC1E,IAAI,GAAE,sBAAyD,GAC9D,IAAI,CAiBN"}
@@ -25,20 +25,70 @@ var __webpack_exports__ = {};
25
25
  __webpack_require__.r(__webpack_exports__);
26
26
  __webpack_require__.d(__webpack_exports__, {
27
27
  addUncaughtExceptionListener: ()=>addUncaughtExceptionListener,
28
- addUnhandledRejectionListener: ()=>addUnhandledRejectionListener
28
+ addUnhandledRejectionListener: ()=>addUnhandledRejectionListener,
29
+ getUnhandledRejectionMode: ()=>getUnhandledRejectionMode
29
30
  });
31
+ const UNHANDLED_REJECTION_OPTION_NAMES = [
32
+ '--unhandled-rejections',
33
+ '--unhandled_rejections'
34
+ ];
35
+ const UNHANDLED_REJECTION_MODES = new Set([
36
+ 'throw',
37
+ 'strict',
38
+ 'warn',
39
+ 'warn-with-error-code',
40
+ 'none'
41
+ ]);
42
+ const STARTUP_EXEC_ARGV = [
43
+ ...globalThis.process?.execArgv ?? []
44
+ ];
45
+ const STARTUP_NODE_OPTIONS = globalThis.process?.env?.NODE_OPTIONS;
46
+ function splitNodeOptions(nodeOptions) {
47
+ const args = [];
48
+ let current = '';
49
+ let isInString = false;
50
+ for(let index = 0; index < nodeOptions.length; index++){
51
+ const character = nodeOptions[index];
52
+ if ('\\' === character && isInString && index + 1 < nodeOptions.length) current += nodeOptions[++index];
53
+ else if (' ' !== character || isInString) if ('"' === character) isInString = !isInString;
54
+ else current += character;
55
+ else if (current) {
56
+ args.push(current);
57
+ current = '';
58
+ }
59
+ }
60
+ if (current) args.push(current);
61
+ return args;
62
+ }
63
+ function findUnhandledRejectionMode(args) {
64
+ let mode;
65
+ for(let index = 0; index < args.length; index++){
66
+ const argument = args[index];
67
+ const optionName = UNHANDLED_REJECTION_OPTION_NAMES.find((name)=>argument === name || argument.startsWith(`${name}=`));
68
+ if (!optionName) continue;
69
+ const value = argument === optionName ? args[++index] : argument.slice(optionName.length + 1);
70
+ if (UNHANDLED_REJECTION_MODES.has(value)) mode = value;
71
+ }
72
+ return mode;
73
+ }
74
+ function getUnhandledRejectionMode(execArgv = STARTUP_EXEC_ARGV, nodeOptions = STARTUP_NODE_OPTIONS) {
75
+ return findUnhandledRejectionMode(execArgv) ?? findUnhandledRejectionMode(splitNodeOptions(nodeOptions ?? '')) ?? 'throw';
76
+ }
77
+ const STARTUP_UNHANDLED_REJECTION_MODE = getUnhandledRejectionMode();
78
+ function captureUncaughtException(captureFn, error, origin) {
79
+ captureFn(error, {
80
+ mechanism: {
81
+ type: 'unhandledRejection' === origin ? 'onunhandledrejection' : 'onuncaughtexception',
82
+ handled: false
83
+ }
84
+ });
85
+ }
30
86
  function makeUncaughtExceptionHandler(captureFn, onFatalFn) {
31
87
  let calledFatalError = false;
32
- return Object.assign((error)=>{
88
+ return Object.assign((error, origin)=>{
33
89
  const userProvidedListenersCount = global.process.listeners('uncaughtException').filter((listener)=>'domainUncaughtExceptionClear' !== listener.name && true !== listener._posthogErrorHandler).length;
34
- const processWouldExit = 0 === userProvidedListenersCount;
35
- captureFn(error, {
36
- mechanism: {
37
- type: 'onuncaughtexception',
38
- handled: false
39
- }
40
- });
41
- if (!calledFatalError && processWouldExit) {
90
+ captureUncaughtException(captureFn, error, origin);
91
+ if (!calledFatalError && 0 === userProvidedListenersCount) {
42
92
  calledFatalError = true;
43
93
  onFatalFn(error);
44
94
  }
@@ -46,22 +96,31 @@ function makeUncaughtExceptionHandler(captureFn, onFatalFn) {
46
96
  _posthogErrorHandler: true
47
97
  });
48
98
  }
49
- function addUncaughtExceptionListener(captureFn, onFatalFn) {
50
- globalThis.process?.on('uncaughtException', makeUncaughtExceptionHandler(captureFn, onFatalFn));
99
+ function addUncaughtExceptionListener(captureFn, onFatalFn, mode = STARTUP_UNHANDLED_REJECTION_MODE) {
100
+ const process = globalThis.process;
101
+ if (!process) return;
102
+ if ('strict' === mode) return void process.on('uncaughtExceptionMonitor', (error, origin)=>captureUncaughtException(captureFn, error, origin));
103
+ process.on('uncaughtException', makeUncaughtExceptionHandler(captureFn, onFatalFn));
51
104
  }
52
- function addUnhandledRejectionListener(captureFn) {
53
- globalThis.process?.on('unhandledRejection', (reason)=>captureFn(reason, {
105
+ function addUnhandledRejectionListener(captureFn, mode = STARTUP_UNHANDLED_REJECTION_MODE) {
106
+ const process = globalThis.process;
107
+ if (!process || 'throw' === mode || 'strict' === mode || 'warn-with-error-code' === mode) return;
108
+ process.on('unhandledRejection', (reason)=>{
109
+ captureFn(reason, {
54
110
  mechanism: {
55
111
  type: 'onunhandledrejection',
56
112
  handled: false
57
113
  }
58
- }));
114
+ });
115
+ });
59
116
  }
60
117
  exports.addUncaughtExceptionListener = __webpack_exports__.addUncaughtExceptionListener;
61
118
  exports.addUnhandledRejectionListener = __webpack_exports__.addUnhandledRejectionListener;
119
+ exports.getUnhandledRejectionMode = __webpack_exports__.getUnhandledRejectionMode;
62
120
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
63
121
  "addUncaughtExceptionListener",
64
- "addUnhandledRejectionListener"
122
+ "addUnhandledRejectionListener",
123
+ "getUnhandledRejectionMode"
65
124
  ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
66
125
  Object.defineProperty(exports, '__esModule', {
67
126
  value: true
@@ -1,15 +1,64 @@
1
+ const UNHANDLED_REJECTION_OPTION_NAMES = [
2
+ '--unhandled-rejections',
3
+ '--unhandled_rejections'
4
+ ];
5
+ const UNHANDLED_REJECTION_MODES = new Set([
6
+ 'throw',
7
+ 'strict',
8
+ 'warn',
9
+ 'warn-with-error-code',
10
+ 'none'
11
+ ]);
12
+ const STARTUP_EXEC_ARGV = [
13
+ ...globalThis.process?.execArgv ?? []
14
+ ];
15
+ const STARTUP_NODE_OPTIONS = globalThis.process?.env?.NODE_OPTIONS;
16
+ function splitNodeOptions(nodeOptions) {
17
+ const args = [];
18
+ let current = '';
19
+ let isInString = false;
20
+ for(let index = 0; index < nodeOptions.length; index++){
21
+ const character = nodeOptions[index];
22
+ if ('\\' === character && isInString && index + 1 < nodeOptions.length) current += nodeOptions[++index];
23
+ else if (' ' !== character || isInString) if ('"' === character) isInString = !isInString;
24
+ else current += character;
25
+ else if (current) {
26
+ args.push(current);
27
+ current = '';
28
+ }
29
+ }
30
+ if (current) args.push(current);
31
+ return args;
32
+ }
33
+ function findUnhandledRejectionMode(args) {
34
+ let mode;
35
+ for(let index = 0; index < args.length; index++){
36
+ const argument = args[index];
37
+ const optionName = UNHANDLED_REJECTION_OPTION_NAMES.find((name)=>argument === name || argument.startsWith(`${name}=`));
38
+ if (!optionName) continue;
39
+ const value = argument === optionName ? args[++index] : argument.slice(optionName.length + 1);
40
+ if (UNHANDLED_REJECTION_MODES.has(value)) mode = value;
41
+ }
42
+ return mode;
43
+ }
44
+ function getUnhandledRejectionMode(execArgv = STARTUP_EXEC_ARGV, nodeOptions = STARTUP_NODE_OPTIONS) {
45
+ return findUnhandledRejectionMode(execArgv) ?? findUnhandledRejectionMode(splitNodeOptions(nodeOptions ?? '')) ?? 'throw';
46
+ }
47
+ const STARTUP_UNHANDLED_REJECTION_MODE = getUnhandledRejectionMode();
48
+ function captureUncaughtException(captureFn, error, origin) {
49
+ captureFn(error, {
50
+ mechanism: {
51
+ type: 'unhandledRejection' === origin ? 'onunhandledrejection' : 'onuncaughtexception',
52
+ handled: false
53
+ }
54
+ });
55
+ }
1
56
  function makeUncaughtExceptionHandler(captureFn, onFatalFn) {
2
57
  let calledFatalError = false;
3
- return Object.assign((error)=>{
58
+ return Object.assign((error, origin)=>{
4
59
  const userProvidedListenersCount = global.process.listeners('uncaughtException').filter((listener)=>'domainUncaughtExceptionClear' !== listener.name && true !== listener._posthogErrorHandler).length;
5
- const processWouldExit = 0 === userProvidedListenersCount;
6
- captureFn(error, {
7
- mechanism: {
8
- type: 'onuncaughtexception',
9
- handled: false
10
- }
11
- });
12
- if (!calledFatalError && processWouldExit) {
60
+ captureUncaughtException(captureFn, error, origin);
61
+ if (!calledFatalError && 0 === userProvidedListenersCount) {
13
62
  calledFatalError = true;
14
63
  onFatalFn(error);
15
64
  }
@@ -17,15 +66,22 @@ function makeUncaughtExceptionHandler(captureFn, onFatalFn) {
17
66
  _posthogErrorHandler: true
18
67
  });
19
68
  }
20
- function addUncaughtExceptionListener(captureFn, onFatalFn) {
21
- globalThis.process?.on('uncaughtException', makeUncaughtExceptionHandler(captureFn, onFatalFn));
69
+ function addUncaughtExceptionListener(captureFn, onFatalFn, mode = STARTUP_UNHANDLED_REJECTION_MODE) {
70
+ const process = globalThis.process;
71
+ if (!process) return;
72
+ if ('strict' === mode) return void process.on('uncaughtExceptionMonitor', (error, origin)=>captureUncaughtException(captureFn, error, origin));
73
+ process.on('uncaughtException', makeUncaughtExceptionHandler(captureFn, onFatalFn));
22
74
  }
23
- function addUnhandledRejectionListener(captureFn) {
24
- globalThis.process?.on('unhandledRejection', (reason)=>captureFn(reason, {
75
+ function addUnhandledRejectionListener(captureFn, mode = STARTUP_UNHANDLED_REJECTION_MODE) {
76
+ const process = globalThis.process;
77
+ if (!process || 'throw' === mode || 'strict' === mode || 'warn-with-error-code' === mode) return;
78
+ process.on('unhandledRejection', (reason)=>{
79
+ captureFn(reason, {
25
80
  mechanism: {
26
81
  type: 'onunhandledrejection',
27
82
  handled: false
28
83
  }
29
- }));
84
+ });
85
+ });
30
86
  }
31
- export { addUncaughtExceptionListener, addUnhandledRejectionListener };
87
+ export { addUncaughtExceptionListener, addUnhandledRejectionListener, getUnhandledRejectionMode };
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const version = "5.47.8";
1
+ export declare const version = "5.47.9";
2
2
  //# sourceMappingURL=version.d.ts.map
package/dist/version.js CHANGED
@@ -26,7 +26,7 @@ __webpack_require__.r(__webpack_exports__);
26
26
  __webpack_require__.d(__webpack_exports__, {
27
27
  version: ()=>version
28
28
  });
29
- const version = '5.47.8';
29
+ const version = '5.47.9';
30
30
  exports.version = __webpack_exports__.version;
31
31
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
32
32
  "version"
package/dist/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- const version = '5.47.8';
1
+ const version = '5.47.9';
2
2
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-node",
3
- "version": "5.47.8",
3
+ "version": "5.47.9",
4
4
  "bugs": {
5
5
  "url": "https://github.com/PostHog/posthog-js/issues"
6
6
  },
@@ -4,7 +4,94 @@
4
4
 
5
5
  import { ErrorTracking as CoreErrorTracking } from '@posthog/core'
6
6
 
7
- type ErrorHandler = { _posthogErrorHandler: boolean } & ((error: Error) => void)
7
+ type ErrorHandler = { _posthogErrorHandler: boolean } & NodeJS.UncaughtExceptionListener
8
+ type UnhandledRejectionMode = 'throw' | 'strict' | 'warn' | 'warn-with-error-code' | 'none'
9
+
10
+ const UNHANDLED_REJECTION_OPTION_NAMES = ['--unhandled-rejections', '--unhandled_rejections']
11
+ const UNHANDLED_REJECTION_MODES = new Set<UnhandledRejectionMode>([
12
+ 'throw',
13
+ 'strict',
14
+ 'warn',
15
+ 'warn-with-error-code',
16
+ 'none',
17
+ ])
18
+ const STARTUP_EXEC_ARGV = [...(globalThis.process?.execArgv ?? [])]
19
+ const STARTUP_NODE_OPTIONS = globalThis.process?.env?.NODE_OPTIONS
20
+
21
+ function splitNodeOptions(nodeOptions: string): string[] {
22
+ const args: string[] = []
23
+ let current = ''
24
+ let isInString = false
25
+
26
+ for (let index = 0; index < nodeOptions.length; index++) {
27
+ const character = nodeOptions[index]
28
+
29
+ if (character === '\\' && isInString && index + 1 < nodeOptions.length) {
30
+ current += nodeOptions[++index]
31
+ } else if (character === ' ' && !isInString) {
32
+ if (current) {
33
+ args.push(current)
34
+ current = ''
35
+ }
36
+ } else if (character === '"') {
37
+ isInString = !isInString
38
+ } else {
39
+ current += character
40
+ }
41
+ }
42
+
43
+ if (current) {
44
+ args.push(current)
45
+ }
46
+
47
+ return args
48
+ }
49
+
50
+ function findUnhandledRejectionMode(args: string[]): UnhandledRejectionMode | undefined {
51
+ let mode: UnhandledRejectionMode | undefined
52
+
53
+ for (let index = 0; index < args.length; index++) {
54
+ const argument = args[index]
55
+ const optionName = UNHANDLED_REJECTION_OPTION_NAMES.find(
56
+ (name) => argument === name || argument.startsWith(`${name}=`)
57
+ )
58
+ if (!optionName) {
59
+ continue
60
+ }
61
+
62
+ const value = argument === optionName ? args[++index] : argument.slice(optionName.length + 1)
63
+ if (UNHANDLED_REJECTION_MODES.has(value as UnhandledRejectionMode)) {
64
+ mode = value as UnhandledRejectionMode
65
+ }
66
+ }
67
+
68
+ return mode
69
+ }
70
+
71
+ export function getUnhandledRejectionMode(
72
+ execArgv: string[] = STARTUP_EXEC_ARGV,
73
+ nodeOptions: string | undefined = STARTUP_NODE_OPTIONS
74
+ ): UnhandledRejectionMode {
75
+ // Command-line arguments take precedence over NODE_OPTIONS, and the last occurrence wins within each source.
76
+ return (
77
+ findUnhandledRejectionMode(execArgv) ?? findUnhandledRejectionMode(splitNodeOptions(nodeOptions ?? '')) ?? 'throw'
78
+ )
79
+ }
80
+
81
+ const STARTUP_UNHANDLED_REJECTION_MODE = getUnhandledRejectionMode()
82
+
83
+ function captureUncaughtException(
84
+ captureFn: (exception: Error, hint: CoreErrorTracking.EventHint) => void,
85
+ error: Error,
86
+ origin: NodeJS.UncaughtExceptionOrigin
87
+ ): void {
88
+ captureFn(error, {
89
+ mechanism: {
90
+ type: origin === 'unhandledRejection' ? 'onunhandledrejection' : 'onuncaughtexception',
91
+ handled: false,
92
+ },
93
+ })
94
+ }
8
95
 
9
96
  function makeUncaughtExceptionHandler(
10
97
  captureFn: (exception: Error, hint: CoreErrorTracking.EventHint) => void,
@@ -13,32 +100,19 @@ function makeUncaughtExceptionHandler(
13
100
  let calledFatalError: boolean = false
14
101
 
15
102
  return Object.assign(
16
- (error: Error): void => {
17
- // Attaching a listener to `uncaughtException` will prevent the node process from exiting. We generally do not
18
- // want to alter this behaviour so we check for other listeners that users may have attached themselves and adjust
19
- // exit behaviour of the SDK accordingly:
20
- // - If other listeners are attached, do not exit.
21
- // - If the only listener attached is ours, exit.
103
+ (error: Error, origin: NodeJS.UncaughtExceptionOrigin): void => {
104
+ // Like Sentry, we only consider listeners that are currently registered. We intentionally do not reconstruct
105
+ // participation by once listeners or listeners added or removed during dispatch.
22
106
  const userProvidedListenersCount = global.process.listeners('uncaughtException').filter((listener) => {
23
- // There are 2 listeners we ignore:
24
107
  return (
25
- // as soon as we're using domains this listener is attached by node itself
26
108
  listener.name !== 'domainUncaughtExceptionClear' &&
27
- // the handler we register in this integration
28
- (listener as ErrorHandler)._posthogErrorHandler !== true
109
+ (listener as Partial<ErrorHandler>)._posthogErrorHandler !== true
29
110
  )
30
111
  }).length
31
112
 
32
- const processWouldExit = userProvidedListenersCount === 0
33
-
34
- captureFn(error, {
35
- mechanism: {
36
- type: 'onuncaughtexception',
37
- handled: false,
38
- },
39
- })
113
+ captureUncaughtException(captureFn, error, origin)
40
114
 
41
- if (!calledFatalError && processWouldExit) {
115
+ if (!calledFatalError && userProvidedListenersCount === 0) {
42
116
  calledFatalError = true
43
117
  onFatalFn(error)
44
118
  }
@@ -49,16 +123,36 @@ function makeUncaughtExceptionHandler(
49
123
 
50
124
  export function addUncaughtExceptionListener(
51
125
  captureFn: (exception: Error, hint: CoreErrorTracking.EventHint) => void,
52
- onFatalFn: (exception: Error) => void
126
+ onFatalFn: (exception: Error) => void,
127
+ mode: UnhandledRejectionMode = STARTUP_UNHANDLED_REJECTION_MODE
53
128
  ): void {
54
- globalThis.process?.on('uncaughtException', makeUncaughtExceptionHandler(captureFn, onFatalFn))
129
+ const process = globalThis.process
130
+ if (!process) {
131
+ return
132
+ }
133
+
134
+ if (mode === 'strict') {
135
+ process.on('uncaughtExceptionMonitor', (error, origin) => captureUncaughtException(captureFn, error, origin))
136
+ return
137
+ }
138
+
139
+ process.on('uncaughtException', makeUncaughtExceptionHandler(captureFn, onFatalFn))
55
140
  }
56
141
 
57
142
  export function addUnhandledRejectionListener(
58
- captureFn: (exception: unknown, hint: CoreErrorTracking.EventHint) => void
143
+ captureFn: (exception: unknown, hint: CoreErrorTracking.EventHint) => void,
144
+ mode: UnhandledRejectionMode = STARTUP_UNHANDLED_REJECTION_MODE
59
145
  ): void {
60
- globalThis.process?.on('unhandledRejection', (reason: unknown) => {
61
- return captureFn(reason, {
146
+ const process = globalThis.process
147
+
148
+ // Installing an unhandledRejection listener changes Node's behavior in these modes. We intentionally skip capture
149
+ // rather than reproducing Node's fatal handling or warn-with-error-code warning and exit-code semantics in the SDK.
150
+ if (!process || mode === 'throw' || mode === 'strict' || mode === 'warn-with-error-code') {
151
+ return
152
+ }
153
+
154
+ process.on('unhandledRejection', (reason: unknown) => {
155
+ captureFn(reason, {
62
156
  mechanism: {
63
157
  type: 'onunhandledrejection',
64
158
  handled: false,
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '5.47.8'
1
+ export const version = '5.47.9'