@posthog/core 1.70.0 → 1.71.0

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.
Files changed (117) hide show
  1. package/capture-v1/package.json +6 -0
  2. package/dist/capture-v1/compression.d.ts +18 -0
  3. package/dist/capture-v1/compression.d.ts.map +1 -0
  4. package/dist/capture-v1/compression.js +70 -0
  5. package/dist/capture-v1/compression.mjs +26 -0
  6. package/dist/capture-v1/errors.d.ts +48 -0
  7. package/dist/capture-v1/errors.d.ts.map +1 -0
  8. package/dist/capture-v1/errors.js +82 -0
  9. package/dist/capture-v1/errors.mjs +38 -0
  10. package/dist/capture-v1/index.d.ts +15 -0
  11. package/dist/capture-v1/index.d.ts.map +1 -0
  12. package/dist/capture-v1/index.js +88 -0
  13. package/dist/capture-v1/index.mjs +7 -0
  14. package/dist/capture-v1/sender.d.ts +150 -0
  15. package/dist/capture-v1/sender.d.ts.map +1 -0
  16. package/dist/capture-v1/sender.js +554 -0
  17. package/dist/capture-v1/sender.mjs +507 -0
  18. package/dist/capture-v1/serialize.d.ts +30 -0
  19. package/dist/capture-v1/serialize.d.ts.map +1 -0
  20. package/dist/capture-v1/serialize.js +106 -0
  21. package/dist/capture-v1/serialize.mjs +59 -0
  22. package/dist/capture-v1/transform.d.ts +19 -0
  23. package/dist/capture-v1/transform.d.ts.map +1 -0
  24. package/dist/capture-v1/transform.js +118 -0
  25. package/dist/capture-v1/transform.mjs +77 -0
  26. package/dist/capture-v1/types.d.ts +52 -0
  27. package/dist/capture-v1/types.d.ts.map +1 -0
  28. package/dist/capture-v1/types.js +44 -0
  29. package/dist/capture-v1/types.mjs +3 -0
  30. package/dist/capture-v1/uuid.d.ts +9 -0
  31. package/dist/capture-v1/uuid.d.ts.map +1 -0
  32. package/dist/capture-v1/uuid.js +75 -0
  33. package/dist/capture-v1/uuid.mjs +34 -0
  34. package/dist/featureFlagUtils.d.ts +7 -5
  35. package/dist/featureFlagUtils.d.ts.map +1 -1
  36. package/dist/featureFlagUtils.js +17 -11
  37. package/dist/featureFlagUtils.mjs +14 -11
  38. package/dist/index.d.ts +1 -1
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +27 -16
  41. package/dist/index.mjs +1 -1
  42. package/dist/posthog-core-stateless.d.ts +40 -3
  43. package/dist/posthog-core-stateless.d.ts.map +1 -1
  44. package/dist/posthog-core-stateless.js +87 -19
  45. package/dist/posthog-core-stateless.mjs +87 -19
  46. package/dist/posthog-core.d.ts +138 -53
  47. package/dist/posthog-core.d.ts.map +1 -1
  48. package/dist/posthog-core.js +341 -115
  49. package/dist/posthog-core.mjs +338 -115
  50. package/dist/types.d.ts +24 -1
  51. package/dist/types.d.ts.map +1 -1
  52. package/dist/utils/index.d.ts +1 -1
  53. package/dist/utils/index.d.ts.map +1 -1
  54. package/dist/utils/index.js +25 -12
  55. package/dist/utils/index.mjs +1 -1
  56. package/dist/utils/json-utils.d.ts +11 -1
  57. package/dist/utils/json-utils.d.ts.map +1 -1
  58. package/dist/utils/json-utils.js +50 -2
  59. package/dist/utils/json-utils.mjs +46 -1
  60. package/package.json +12 -2
  61. package/src/capture-v1/compression.ts +50 -0
  62. package/src/capture-v1/errors.ts +91 -0
  63. package/src/capture-v1/index.ts +34 -0
  64. package/src/capture-v1/sender.ts +779 -0
  65. package/src/capture-v1/serialize.ts +113 -0
  66. package/src/capture-v1/transform.ts +135 -0
  67. package/src/capture-v1/types.ts +59 -0
  68. package/src/capture-v1/uuid.ts +53 -0
  69. package/src/featureFlagUtils.ts +22 -10
  70. package/src/index.ts +1 -0
  71. package/src/posthog-core-stateless.ts +166 -30
  72. package/src/posthog-core.ts +615 -178
  73. package/src/types.ts +21 -1
  74. package/src/utils/index.ts +1 -1
  75. package/src/utils/json-utils.ts +64 -1
  76. package/src/error-tracking/chunk-ids.spec.ts +0 -77
  77. package/src/error-tracking/coercers/error-event-coercer.spec.ts +0 -142
  78. package/src/error-tracking/coercers/promise-rejection-event.spec.ts +0 -77
  79. package/src/error-tracking/coercers/string-coercer.spec.ts +0 -26
  80. package/src/error-tracking/error-properties-builder.aggregate-errors.spec.ts +0 -412
  81. package/src/error-tracking/error-properties-builder.coerce.spec.ts +0 -337
  82. package/src/error-tracking/error-properties-builder.metadata.spec.ts +0 -318
  83. package/src/error-tracking/error-properties-builder.parse.spec.ts +0 -37
  84. package/src/error-tracking/exception-steps.spec.ts +0 -115
  85. package/src/error-tracking/parsers/base.spec.ts +0 -156
  86. package/src/error-tracking/parsers/index.spec.ts +0 -270
  87. package/src/error-tracking/parsers/node.spec.ts +0 -34
  88. package/src/error-tracking/release.spec.ts +0 -34
  89. package/src/logs/index.spec.ts +0 -2421
  90. package/src/logs/logs-utils.spec.ts +0 -266
  91. package/src/metrics/index.spec.ts +0 -1249
  92. package/src/surveys/activation.spec.ts +0 -61
  93. package/src/surveys/events.spec.ts +0 -183
  94. package/src/surveys/keys.spec.ts +0 -29
  95. package/src/surveys/property-matching.spec.ts +0 -539
  96. package/src/surveys/shuffling.spec.ts +0 -51
  97. package/src/surveys/translations.spec.ts +0 -274
  98. package/src/surveys/validation.spec.ts +0 -168
  99. package/src/traces/config.spec.ts +0 -271
  100. package/src/traces/ids.spec.ts +0 -115
  101. package/src/traces/index.spec.ts +0 -3287
  102. package/src/traces/live-spans.spec.ts +0 -69
  103. package/src/traces/otlp.spec.ts +0 -368
  104. package/src/traces/span.spec.ts +0 -1137
  105. package/src/traces/traceparent.spec.ts +0 -184
  106. package/src/utils/bot-detection.spec.ts +0 -107
  107. package/src/utils/bucketed-rate-limiter.spec.ts +0 -327
  108. package/src/utils/json-utils.spec.ts +0 -115
  109. package/src/utils/named-error.spec.ts +0 -36
  110. package/src/utils/number-utils.spec.ts +0 -212
  111. package/src/utils/otlp-any-value.spec.ts +0 -376
  112. package/src/utils/otlp-resource.spec.ts +0 -171
  113. package/src/utils/promise-queue.spec.ts +0 -55
  114. package/src/utils/string-utils.spec.ts +0 -158
  115. package/src/utils/type-utils.spec.ts +0 -38
  116. package/src/utils/webview-app-utils.spec.ts +0 -118
  117. package/src/vendor/uuidv7.spec.ts +0 -26
@@ -1,337 +0,0 @@
1
- import { runInNewContext } from 'node:vm'
2
-
3
- import {
4
- DOMExceptionCoercer,
5
- ErrorEventCoercer,
6
- ErrorCoercer,
7
- ObjectCoercer,
8
- StringCoercer,
9
- EventCoercer,
10
- } from './coercers'
11
- import { PrimitiveCoercer } from './coercers/primitive-coercer'
12
- import { PromiseRejectionEventCoercer } from './coercers/promise-rejection-event'
13
- import { ErrorPropertiesBuilder } from './error-properties-builder'
14
- import { createStackParser } from './parsers'
15
- import { ExceptionLike } from './types'
16
-
17
- describe('ErrorPropertiesBuilder', () => {
18
- describe('coerceUnknown', () => {
19
- class CustomTestError extends Error {
20
- constructor(message: string, cause?: unknown) {
21
- super(message)
22
- this.name = 'CustomTestError'
23
- this.cause = cause
24
- }
25
- }
26
-
27
- const errorPropertiesBuilder = new ErrorPropertiesBuilder(
28
- [
29
- new DOMExceptionCoercer(),
30
- new ErrorEventCoercer(),
31
- new ErrorCoercer(),
32
- new PromiseRejectionEventCoercer(),
33
- new EventCoercer(),
34
- new ObjectCoercer(),
35
- new StringCoercer(),
36
- new PrimitiveCoercer(),
37
- ],
38
- createStackParser('web:javascript'),
39
- []
40
- )
41
-
42
- function coerceInput(input: unknown, error: Error = new Error()): ExceptionLike | undefined {
43
- const coercingContext = errorPropertiesBuilder.buildCoercingContext(
44
- { handled: false },
45
- {
46
- syntheticException: error,
47
- }
48
- )
49
- return coercingContext.apply(input)
50
- }
51
-
52
- it('should handle null values', async () => {
53
- const syntheticError = new Error()
54
- const exception = coerceInput(null, syntheticError)
55
- expect(exception).toMatchObject({
56
- type: 'Error',
57
- value: 'Primitive value captured as exception: null',
58
- stack: syntheticError.stack,
59
- })
60
- })
61
-
62
- it('should handle string', () => {
63
- const syntheticError = new Error()
64
- const exception = coerceInput('test', syntheticError)
65
- expect(exception).toMatchObject({
66
- type: 'Error',
67
- value: 'test',
68
- stack: syntheticError.stack,
69
- })
70
- })
71
-
72
- it('should handle exception string', () => {
73
- const syntheticError = new Error()
74
- const exception = coerceInput('Uncaught exception: InternalError: but somehow still a string', syntheticError)
75
- expect(exception).toMatchObject({
76
- type: 'InternalError',
77
- value: 'but somehow still a string',
78
- stack: syntheticError.stack,
79
- })
80
- })
81
-
82
- it.each([
83
- ['all keys when they fit', { foo: 'Foo value', bar: 'Bar value' }, 'bar, foo'],
84
- [
85
- 'the longest key prefix that fits',
86
- Object.fromEntries(['golf', 'alpha', 'echo', 'bravo', 'foxtrot', 'delta', 'charlie'].map((k) => [k, 1])),
87
- 'alpha, bravo, charlie, delta, echo',
88
- ],
89
- ['nothing when the first key is too long', { ['x'.repeat(41)]: 1 }, ''],
90
- ])('should use %s in objects', (_case, errorObject, keys) => {
91
- const syntheticError = new Error()
92
- const exception = coerceInput(errorObject, syntheticError)
93
- expect(exception).toMatchObject({
94
- type: 'Error',
95
- value: `Object captured as exception with keys: ${keys}`,
96
- stack: syntheticError.stack,
97
- })
98
- })
99
-
100
- it('should summarize the keys of a very wide object quickly', () => {
101
- const errorObject = Object.fromEntries(Array.from({ length: 30_000 }, (_, i) => [`k${i}`, i]))
102
- const start = process.hrtime.bigint()
103
- const exception = coerceInput(errorObject, new Error())
104
- expect(Number(process.hrtime.bigint() - start) / 1e6).toBeLessThan(1000)
105
- expect(exception.value).toBe('Object captured as exception with keys: k0, k1, k10, k100, k1000, k10000, k10001')
106
- })
107
-
108
- it('should handle object with an error property', () => {
109
- const nestedError = new CustomTestError('My special error')
110
- const errorObject = { error: nestedError }
111
- const syntheticError = new Error()
112
- const exception = coerceInput(errorObject, syntheticError)
113
- expect(exception).toMatchObject({
114
- type: 'CustomTestError',
115
- value: 'My special error',
116
- stack: nestedError.stack,
117
- })
118
- })
119
-
120
- it('should handle error', () => {
121
- const errorObject = new CustomTestError('My special error')
122
- const exception = coerceInput(errorObject)
123
- expect(exception).toMatchObject({
124
- type: 'CustomTestError',
125
- value: 'My special error',
126
- stack: errorObject.stack,
127
- })
128
- })
129
-
130
- it('should fall back to the synthetic stack when the error has no stack', () => {
131
- // Firefox rejects a network `fetch` with a `TypeError` that carries no
132
- // stack, so the frames must come from the synthetic exception instead.
133
- const errorObject = new CustomTestError('My special error')
134
- errorObject.stack = ''
135
- const syntheticError = new Error()
136
- const exception = coerceInput(errorObject, syntheticError)
137
- expect(exception).toMatchObject({
138
- type: 'CustomTestError',
139
- value: 'My special error',
140
- stack: syntheticError.stack,
141
- synthetic: true,
142
- })
143
- })
144
-
145
- it('should preserve a cross-realm error', () => {
146
- const crossRealmError = runInNewContext(
147
- `new TypeError('cross-realm error', {
148
- cause: new Error('cross-realm cause')
149
- })`
150
- )
151
- expect(crossRealmError).not.toBeInstanceOf(Error)
152
-
153
- expect(coerceInput(crossRealmError)).toMatchObject({
154
- type: 'TypeError',
155
- value: 'cross-realm error',
156
- stack: crossRealmError.stack,
157
- synthetic: false,
158
- cause: {
159
- type: 'Error',
160
- value: 'cross-realm cause',
161
- stack: crossRealmError.cause.stack,
162
- synthetic: false,
163
- },
164
- })
165
- })
166
-
167
- it('should handle error with error cause', () => {
168
- const secondError = new CustomTestError('My original error')
169
- const firstError = new CustomTestError('My wrapped error', secondError)
170
- const exception = coerceInput(firstError)
171
- expect(exception).toMatchObject({
172
- type: 'CustomTestError',
173
- value: 'My wrapped error',
174
- stack: firstError.stack,
175
- cause: {
176
- type: 'CustomTestError',
177
- value: 'My original error',
178
- stack: secondError.stack,
179
- },
180
- })
181
- })
182
-
183
- it('should handle error with object cause', () => {
184
- const originalCause = { foo: 'bar', test: 'test' }
185
- const kaboomError = new CustomTestError('Front error', originalCause)
186
- const syntheticError = new Error()
187
- const exception = coerceInput(kaboomError, syntheticError)
188
- expect(exception).toMatchObject({
189
- type: 'CustomTestError',
190
- value: 'Front error',
191
- stack: kaboomError.stack,
192
- cause: {
193
- type: 'Error',
194
- value: 'Object captured as exception with keys: foo, test',
195
- // Do we want to use the stack from the synthetic error?
196
- stack: undefined,
197
- },
198
- })
199
- })
200
-
201
- it('should handle error with string cause', () => {
202
- const originalCause = 'My original error'
203
- const kaboomError = new CustomTestError('Front error', originalCause)
204
- const syntheticError = new Error()
205
- const exception = coerceInput(kaboomError, syntheticError)
206
- expect(exception).toMatchObject({
207
- type: 'CustomTestError',
208
- value: 'Front error',
209
- stack: kaboomError.stack,
210
- cause: {
211
- type: 'Error',
212
- value: 'My original error',
213
- // Do we want to use the stack from the synthetic error?
214
- stack: undefined,
215
- },
216
- })
217
- })
218
-
219
- it('should convert a plain Event to an error', () => {
220
- class MouseEvent extends Event {
221
- constructor(type: string, eventInitDict?: EventInit) {
222
- super(type, eventInitDict)
223
- }
224
- }
225
- const event = new MouseEvent('click', { bubbles: true, cancelable: true, composed: true })
226
- const syntheticError = new Error()
227
- const exception = coerceInput(event, syntheticError)
228
- expect(exception).toMatchObject({
229
- type: 'MouseEvent',
230
- value: 'MouseEvent captured as exception with keys: [object has no keys]',
231
- stack: syntheticError.stack,
232
- synthetic: true,
233
- })
234
- })
235
-
236
- it('should convert a DOM Error to an error', () => {
237
- class FakeDomError {
238
- constructor(
239
- public name: string,
240
- public message: string
241
- ) {}
242
- [Symbol.toStringTag] = 'DOMError'
243
- }
244
- const event = new FakeDomError('click', 'foo')
245
- const exception = coerceInput(event)
246
- expect(exception).toMatchObject({
247
- type: 'DOMError',
248
- value: 'click: foo',
249
- stack: undefined,
250
- synthetic: false,
251
- })
252
- })
253
-
254
- it('should convert a DOM Exception to an error', () => {
255
- const event = new DOMException('oh no disaster', 'dom-exception')
256
- const exception = coerceInput(event)
257
- expect(exception).toBeDefined()
258
- expect(exception).toMatchObject({
259
- type: 'DOMException',
260
- value: 'dom-exception: oh no disaster',
261
- synthetic: false,
262
- })
263
- })
264
-
265
- it('should extract the buried Error from a CustomEvent wrapping a PromiseRejectionEvent', () => {
266
- const buriedError = new Error('Extension context invalidated.')
267
- const customEvent = new CustomEvent('unhandledrejection', {
268
- detail: {
269
- reason: buriedError,
270
- promise: Promise.resolve(),
271
- },
272
- })
273
-
274
- const exception = coerceInput(customEvent)
275
-
276
- expect(exception).toMatchObject({
277
- type: 'Error',
278
- value: 'Extension context invalidated.',
279
- stack: buriedError.stack,
280
- })
281
- })
282
-
283
- it('should preserve an error-like stack from a CustomEvent wrapping a PromiseRejectionEvent', () => {
284
- const stack = 'TypeError: Extension context invalidated.\n at extension.js:1:2'
285
- const customEvent = new CustomEvent('unhandledrejection', {
286
- detail: {
287
- reason: {
288
- name: 'TypeError',
289
- message: 'Extension context invalidated.',
290
- stack,
291
- },
292
- },
293
- })
294
-
295
- expect(coerceInput(customEvent)).toMatchObject({
296
- type: 'TypeError',
297
- value: "'TypeError' captured as exception with message: 'Extension context invalidated.'",
298
- stack,
299
- })
300
- })
301
-
302
- it('should fall back to Error when the object has no name property', () => {
303
- const exception = coerceInput({ message: 'oh no disaster' })
304
-
305
- expect(exception).toMatchObject({
306
- type: 'Error',
307
- value: 'oh no disaster',
308
- })
309
- })
310
-
311
- it('should fall back to Error when the name property is an empty string', () => {
312
- const exception = coerceInput({ name: '', message: 'oh no disaster' })
313
-
314
- expect(exception).toMatchObject({
315
- type: 'Error',
316
- })
317
- })
318
-
319
- it('should preserve a string type when name is a stateful accessor', () => {
320
- let reads = 0
321
- const candidate = { message: 'boom' }
322
-
323
- Object.defineProperty(candidate, 'name', {
324
- // Keep this non-enumerable so the object property scan does not read it first.
325
- get() {
326
- reads += 1
327
- return reads === 1 ? 'TypeError' : 42
328
- },
329
- })
330
-
331
- expect(coerceInput(candidate)).toMatchObject({
332
- type: 'TypeError',
333
- value: 'boom',
334
- })
335
- })
336
- })
337
- })
@@ -1,318 +0,0 @@
1
- import { ErrorCoercer, ErrorEventCoercer, ObjectCoercer, PrimitiveCoercer, StringCoercer } from './coercers'
2
- import { ErrorPropertiesBuilder } from './error-properties-builder'
3
- import { chromeStackLineParser, createStackParser } from './parsers'
4
-
5
- // Canonical nested_cause and aggregate examples from the exception-event-metadata specification.
6
- describe('ErrorPropertiesBuilder relationship metadata', () => {
7
- const builder = new ErrorPropertiesBuilder(
8
- [new ErrorEventCoercer(), new ErrorCoercer(), new ObjectCoercer(), new StringCoercer(), new PrimitiveCoercer()],
9
- createStackParser('web:javascript', chromeStackLineParser)
10
- )
11
- function error(type: string, value: string, cause?: unknown): Error {
12
- const result = new Error(value, { cause })
13
- result.name = type
14
- result.stack = undefined
15
- return result
16
- }
17
-
18
- it('matches the canonical cause example without inventing nested handled state', () => {
19
- expect(
20
- builder.buildFromUnknown(error('CheckoutError', 'Checkout failed', error('DatabaseError', 'Connection failed')))
21
- ).toEqual({
22
- $exception_level: 'error',
23
- $exception_list: [
24
- {
25
- type: 'CheckoutError',
26
- value: 'Checkout failed',
27
- mechanism: { type: 'generic', handled: true, synthetic: false, exception_id: 0 },
28
- },
29
- {
30
- type: 'DatabaseError',
31
- value: 'Connection failed',
32
- mechanism: { type: 'chained', source: 'cause', synthetic: false, exception_id: 1, parent_id: 0 },
33
- },
34
- ],
35
- })
36
- })
37
-
38
- it('matches the canonical aggregate example including the second member cause', () => {
39
- const root = new AggregateError(
40
- [
41
- error('NetworkError', 'Network failed'),
42
- error('ValidationError', 'Validation failed', error('ParseError', 'Invalid value')),
43
- ],
44
- 'Two operations failed'
45
- )
46
- root.stack = undefined
47
- expect(builder.buildFromUnknown(root, { mechanism: { type: 'task', handled: false } }).$exception_list).toEqual([
48
- {
49
- type: 'AggregateError',
50
- value: 'Two operations failed',
51
- mechanism: { type: 'task', handled: false, synthetic: false, exception_id: 0 },
52
- },
53
- {
54
- type: 'NetworkError',
55
- value: 'Network failed',
56
- mechanism: { type: 'chained', source: 'member', synthetic: false, exception_id: 1, parent_id: 0 },
57
- },
58
- {
59
- type: 'ValidationError',
60
- value: 'Validation failed',
61
- mechanism: { type: 'chained', source: 'member', synthetic: false, exception_id: 2, parent_id: 0 },
62
- },
63
- {
64
- type: 'ParseError',
65
- value: 'Invalid value',
66
- mechanism: { type: 'chained', source: 'cause', synthetic: false, exception_id: 3, parent_id: 2 },
67
- },
68
- ])
69
- })
70
-
71
- it('retains the first 50 entries of a deep cause chain with deterministic valid IDs', () => {
72
- let root = error('Error', '60')
73
- for (let index = 59; index >= 0; index--) root = error('Error', String(index), root)
74
- const first = builder.buildFromUnknown(root).$exception_list
75
- expect(first).toEqual(builder.buildFromUnknown(root).$exception_list)
76
- expect(first.map((entry) => entry.value)).toEqual(Array.from({ length: 50 }, (_, index) => String(index)))
77
- first.forEach((entry, index) => {
78
- expect(entry.mechanism?.exception_id).toBe(index)
79
- if (index) expect(entry.mechanism?.parent_id).toBe(index - 1)
80
- })
81
- })
82
-
83
- it('skips cycles and shared objects globally without spending the emission budget', () => {
84
- const shared = error('Error', 'shared')
85
- const root = new AggregateError([], 'root', { cause: shared })
86
- shared.cause = root
87
- root.errors = [
88
- ...Array(150).fill(root),
89
- ...Array(150).fill(shared),
90
- ...Array.from({ length: 60 }, (_, index) => error('Error', String(index))),
91
- ]
92
- expect(builder.buildFromUnknown(root).$exception_list.map((entry) => entry.value)).toEqual([
93
- 'root',
94
- 'shared',
95
- ...Array.from({ length: 48 }, (_, index) => String(index)),
96
- ])
97
- })
98
-
99
- it('does not read handled properties from user errors or inherit root provenance', () => {
100
- const child = error('Error', 'native')
101
- Object.defineProperty(child, 'handled', {
102
- get() {
103
- throw new Error('untrusted')
104
- },
105
- })
106
- const root = new AggregateError([child, 'converted'], 'root')
107
- root.stack = undefined
108
- const entries = builder.buildFromUnknown(root, {
109
- syntheticException: new Error('capture'),
110
- mechanism: { handled: false, type: 'middleware' },
111
- }).$exception_list
112
- expect(entries.map((entry) => entry.mechanism)).toEqual([
113
- { type: 'middleware', handled: false, synthetic: true, exception_id: 0 },
114
- { type: 'chained', source: 'member', synthetic: false, exception_id: 1, parent_id: 0 },
115
- { type: 'chained', source: 'member', synthetic: true, exception_id: 2, parent_id: 0 },
116
- ])
117
- })
118
-
119
- it('preserves typed capture metadata but assigns root linkage at final assembly', () => {
120
- const root = error('Error', 'root', error('Error', 'child'))
121
- const entries = builder.buildFromUnknown(root, {
122
- mechanism: {
123
- type: 'custom.integration',
124
- handled: false,
125
- synthetic: true,
126
- exception_id: 81,
127
- parent_id: 80,
128
- source: 'cause',
129
- },
130
- }).$exception_list
131
- expect(entries[0].mechanism).toEqual({
132
- type: 'custom.integration',
133
- handled: false,
134
- synthetic: true,
135
- exception_id: 0,
136
- })
137
- expect(entries[1].mechanism).toEqual({
138
- type: 'chained',
139
- source: 'cause',
140
- synthetic: false,
141
- exception_id: 1,
142
- parent_id: 0,
143
- })
144
- })
145
- it.each([undefined, '', 123])('keeps a native child without a usable stack non-synthetic: %s', (stack) => {
146
- const child = error('Error', 'child')
147
- child.stack = stack as string | undefined
148
- const entries = builder.buildFromUnknown(new Error('root', { cause: child })).$exception_list
149
- expect(entries[1].mechanism?.synthetic).toBe(false)
150
- expect(entries[1].stacktrace).toBeUndefined()
151
- })
152
-
153
- it('keeps a native child with a throwing stack accessor non-synthetic', () => {
154
- const child = error('Error', 'child')
155
- Object.defineProperty(child, 'stack', {
156
- get() {
157
- throw new Error('unreadable')
158
- },
159
- })
160
- const entries = builder.buildFromUnknown(new Error('root', { cause: child })).$exception_list
161
- expect(entries[1]).toEqual({
162
- type: 'Error',
163
- value: 'child',
164
- mechanism: { type: 'chained', source: 'cause', synthetic: false, exception_id: 1, parent_id: 0 },
165
- })
166
- })
167
-
168
- it.each(['yes', null, 0, {}, []])('omits invalid handled metadata: %j', (handled) => {
169
- const entries = builder.buildFromUnknown(error('Error', 'root'), {
170
- mechanism: { type: '', handled, synthetic: true } as any,
171
- }).$exception_list
172
- expect(entries[0].mechanism).toEqual({ type: 'generic', synthetic: true, exception_id: 0 })
173
- expect(entries[0].mechanism).not.toHaveProperty('handled')
174
- })
175
-
176
- it.each([undefined, true, false])(
177
- 'preserves the manual capture default and boolean handled metadata: %s',
178
- (handled) => {
179
- const entries = builder.buildFromUnknown(error('Error', 'root'), { mechanism: { handled } }).$exception_list
180
- expect(entries[0].mechanism.handled).toBe(handled === undefined ? true : handled)
181
- }
182
- )
183
-
184
- it('preserves all supplied mechanism fields in the low-level coercion context', () => {
185
- const mechanism = {
186
- type: 'custom',
187
- handled: false,
188
- synthetic: true,
189
- source: 'inner',
190
- exception_id: 7,
191
- parent_id: 6,
192
- }
193
- const context = builder.buildCoercingContext(mechanism, { mechanism })
194
- expect(context.mechanism).toEqual(mechanism)
195
- context.apply(error('Error', 'root'))
196
- expect(context.mechanism).toEqual(mechanism)
197
- })
198
-
199
- it('deduplicates shared exceptions reached through distinct wrappers', () => {
200
- const shared = error('Error', 'shared')
201
- const root = new AggregateError([shared, { error: shared }, { error: shared }, error('Error', 'last')], 'root')
202
- const entries = builder.buildFromUnknown(root).$exception_list
203
- expect(entries.map((entry) => entry.value)).toEqual(['root', 'shared', 'last'])
204
- expect(entries.map((entry) => entry.mechanism?.exception_id)).toEqual([0, 1, 2])
205
- })
206
-
207
- it('resets the wrapper limit on real cause edges', () => {
208
- let root = error('Error', '8')
209
- for (let index = 7; index >= 0; index--) root = error('Error', String(index), { error: root })
210
- expect(builder.buildFromUnknown(root).$exception_list.map((entry) => entry.value)).toEqual(
211
- Array.from({ length: 9 }, (_, index) => String(index))
212
- )
213
- })
214
-
215
- it('bounds consecutive forwarding wrappers without discarding the next member', () => {
216
- let wrapper: unknown = error('Error', 'unreachable')
217
- for (let index = 0; index < 10; index++) wrapper = { [Symbol.toStringTag]: 'ErrorEvent', error: wrapper }
218
- const entries = builder.buildFromUnknown(
219
- new AggregateError([wrapper, error('Error', 'last')], 'root')
220
- ).$exception_list
221
- expect(entries.map((entry) => entry.value)).toEqual(['root', 'Unknown error', 'last'])
222
- expect(entries.map((entry) => entry.mechanism?.exception_id)).toEqual([0, 1, 2])
223
- })
224
-
225
- it.each([NaN, Infinity, -1, 1.5, 0x100000000, '2'])('ignores invalid proxy collection lengths: %s', (length) => {
226
- const root = new AggregateError([], 'root')
227
- const readMember = vi.fn()
228
- root.errors = new Proxy([], { get: (_target, key) => (key === 'length' ? length : readMember(key)) })
229
- expect(builder.buildFromUnknown(root).$exception_list.map((entry) => entry.value)).toEqual(['root'])
230
- expect(readMember).not.toHaveBeenCalled()
231
- })
232
-
233
- it('reads array length once and ignores later growth', () => {
234
- const members = [error('Error', 'first')]
235
- const getLength = vi.fn(() => members.length)
236
- const root = new AggregateError([], 'root')
237
- root.errors = new Proxy(members, {
238
- get: (target, key) => {
239
- if (key === 'length') return getLength()
240
- if (key === '0') members.push(error('Error', 'not in snapshot'))
241
- return Reflect.get(target, key)
242
- },
243
- })
244
- expect(builder.buildFromUnknown(root).$exception_list.map((entry) => entry.value)).toEqual(['root', 'first'])
245
- expect(getLength).toHaveBeenCalledTimes(1)
246
- })
247
-
248
- it('preserves root and cause when reading the collection length throws', () => {
249
- const root = new AggregateError([], 'root', { cause: error('Error', 'cause') })
250
- root.errors = new Proxy([], {
251
- get() {
252
- throw new Error('unreadable length')
253
- },
254
- })
255
- expect(builder.buildFromUnknown(root).$exception_list.map((entry) => entry.value)).toEqual(['root', 'cause'])
256
- })
257
- it('caps huge proxy member collections before reading beyond 1000 attempts and resets between captures', () => {
258
- const root = new AggregateError([], 'root')
259
- const readMember = vi.fn((index: number) => {
260
- // The old implementation also terminates safely: after 1000 duplicate reads,
261
- // fallback entries fill its emission budget rather than scanning the huge length.
262
- if (index >= 1000) throw new Error('past the inspection budget')
263
- return root
264
- })
265
- root.errors = new Proxy([], {
266
- get: (_target, key) => (key === 'length' ? 0xffffffff : readMember(Number(key))),
267
- })
268
- for (let capture = 0; capture < 2; capture++) {
269
- readMember.mockClear()
270
- expect(builder.buildFromUnknown(root).$exception_list.map((entry) => entry.value)).toEqual(['root'])
271
- expect(readMember).toHaveBeenCalledTimes(1000)
272
- expect(readMember).toHaveBeenLastCalledWith(999)
273
- }
274
- expect(
275
- builder
276
- .buildFromUnknown(new AggregateError([error('Error', 'fresh child')], 'fresh root'))
277
- .$exception_list.map((entry) => entry.value)
278
- ).toEqual(['fresh root', 'fresh child'])
279
- })
280
-
281
- it('shares attempted member inspections across groups, including throwing getters, without limiting cause edges', () => {
282
- const root = new AggregateError([], 'root')
283
- const first = new AggregateError(Array(600).fill(root), 'first group')
284
- Object.defineProperty(first.errors, '0', {
285
- get() {
286
- throw new Error('unreadable member')
287
- },
288
- })
289
- let cause = error('Error', 'cause 8')
290
- for (let index = 7; index >= 0; index--) cause = error('Error', `cause ${index}`, cause)
291
- const second = new AggregateError([...Array(397).fill(root), cause, error('Error', 'past budget')], 'second group')
292
- const readLastRootMember = vi.fn(() => error('Error', 'unread root member'))
293
- root.errors = [first, second]
294
- Object.defineProperty(root.errors, '2', { get: readLastRootMember })
295
- const entries = builder.buildFromUnknown(root).$exception_list
296
- // 1 first-group access + 600 members + 1 second-group access + 398 members.
297
- expect(entries.map((entry) => entry.value)).toEqual([
298
- 'root',
299
- 'first group',
300
- 'Unknown error',
301
- 'second group',
302
- ...Array.from({ length: 9 }, (_, index) => `cause ${index}`),
303
- ])
304
- expect(readLastRootMember).not.toHaveBeenCalled()
305
- entries.forEach((entry, index) => {
306
- expect(entry.mechanism?.exception_id).toBe(index)
307
- if (index > 0) {
308
- expect(entry.mechanism).toEqual({
309
- type: 'chained',
310
- source: index > 4 ? 'cause' : 'member',
311
- synthetic: index === 2,
312
- exception_id: index,
313
- parent_id: index === 1 || index === 3 ? 0 : index - 1,
314
- })
315
- }
316
- })
317
- })
318
- })
@@ -1,37 +0,0 @@
1
- import { ErrorPropertiesBuilder } from './error-properties-builder'
2
- import { chromeStackLineParser, createStackParser } from './parsers'
3
- import { StackFrame } from './types'
4
-
5
- describe('ErrorPropertiesBuilder', () => {
6
- describe('coerceUnknown', () => {
7
- const errorPropertiesBuilder = new ErrorPropertiesBuilder(
8
- [],
9
- createStackParser('web:javascript', chromeStackLineParser),
10
- []
11
- )
12
-
13
- function parseStack(error: Error): StackFrame[] | undefined {
14
- const ctx = { skipFirstLines: 1 }
15
- //@ts-expect-error: testing private method
16
- const exception = errorPropertiesBuilder.parseStacktrace(
17
- {
18
- type: 'Error',
19
- value: 'Whatever',
20
- stack: error.stack,
21
- synthetic: false,
22
- },
23
- ctx
24
- )
25
- return exception.stack
26
- }
27
-
28
- it('should parse stacktraces', () => {
29
- const syntheticError = new Error()
30
- const frames = parseStack(syntheticError)
31
- expect(frames).toBeDefined()
32
- // The test runner calls itself once per nested describe block, and the parser keeps one copy
33
- // of a repeated frame cycle, so those repeats count as a single frame here.
34
- expect(frames).toHaveLength(13)
35
- })
36
- })
37
- })