@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.
- package/capture-v1/package.json +6 -0
- package/dist/capture-v1/compression.d.ts +18 -0
- package/dist/capture-v1/compression.d.ts.map +1 -0
- package/dist/capture-v1/compression.js +70 -0
- package/dist/capture-v1/compression.mjs +26 -0
- package/dist/capture-v1/errors.d.ts +48 -0
- package/dist/capture-v1/errors.d.ts.map +1 -0
- package/dist/capture-v1/errors.js +82 -0
- package/dist/capture-v1/errors.mjs +38 -0
- package/dist/capture-v1/index.d.ts +15 -0
- package/dist/capture-v1/index.d.ts.map +1 -0
- package/dist/capture-v1/index.js +88 -0
- package/dist/capture-v1/index.mjs +7 -0
- package/dist/capture-v1/sender.d.ts +150 -0
- package/dist/capture-v1/sender.d.ts.map +1 -0
- package/dist/capture-v1/sender.js +554 -0
- package/dist/capture-v1/sender.mjs +507 -0
- package/dist/capture-v1/serialize.d.ts +30 -0
- package/dist/capture-v1/serialize.d.ts.map +1 -0
- package/dist/capture-v1/serialize.js +106 -0
- package/dist/capture-v1/serialize.mjs +59 -0
- package/dist/capture-v1/transform.d.ts +19 -0
- package/dist/capture-v1/transform.d.ts.map +1 -0
- package/dist/capture-v1/transform.js +118 -0
- package/dist/capture-v1/transform.mjs +77 -0
- package/dist/capture-v1/types.d.ts +52 -0
- package/dist/capture-v1/types.d.ts.map +1 -0
- package/dist/capture-v1/types.js +44 -0
- package/dist/capture-v1/types.mjs +3 -0
- package/dist/capture-v1/uuid.d.ts +9 -0
- package/dist/capture-v1/uuid.d.ts.map +1 -0
- package/dist/capture-v1/uuid.js +75 -0
- package/dist/capture-v1/uuid.mjs +34 -0
- package/dist/featureFlagUtils.d.ts +7 -5
- package/dist/featureFlagUtils.d.ts.map +1 -1
- package/dist/featureFlagUtils.js +17 -11
- package/dist/featureFlagUtils.mjs +14 -11
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +27 -16
- package/dist/index.mjs +1 -1
- package/dist/posthog-core-stateless.d.ts +40 -3
- package/dist/posthog-core-stateless.d.ts.map +1 -1
- package/dist/posthog-core-stateless.js +87 -19
- package/dist/posthog-core-stateless.mjs +87 -19
- package/dist/posthog-core.d.ts +138 -53
- package/dist/posthog-core.d.ts.map +1 -1
- package/dist/posthog-core.js +341 -115
- package/dist/posthog-core.mjs +338 -115
- package/dist/types.d.ts +24 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +25 -12
- package/dist/utils/index.mjs +1 -1
- package/dist/utils/json-utils.d.ts +11 -1
- package/dist/utils/json-utils.d.ts.map +1 -1
- package/dist/utils/json-utils.js +50 -2
- package/dist/utils/json-utils.mjs +46 -1
- package/package.json +12 -2
- package/src/capture-v1/compression.ts +50 -0
- package/src/capture-v1/errors.ts +91 -0
- package/src/capture-v1/index.ts +34 -0
- package/src/capture-v1/sender.ts +779 -0
- package/src/capture-v1/serialize.ts +113 -0
- package/src/capture-v1/transform.ts +135 -0
- package/src/capture-v1/types.ts +59 -0
- package/src/capture-v1/uuid.ts +53 -0
- package/src/featureFlagUtils.ts +22 -10
- package/src/index.ts +1 -0
- package/src/posthog-core-stateless.ts +166 -30
- package/src/posthog-core.ts +615 -178
- package/src/types.ts +21 -1
- package/src/utils/index.ts +1 -1
- package/src/utils/json-utils.ts +64 -1
- package/src/error-tracking/chunk-ids.spec.ts +0 -77
- package/src/error-tracking/coercers/error-event-coercer.spec.ts +0 -142
- package/src/error-tracking/coercers/promise-rejection-event.spec.ts +0 -77
- package/src/error-tracking/coercers/string-coercer.spec.ts +0 -26
- package/src/error-tracking/error-properties-builder.aggregate-errors.spec.ts +0 -412
- package/src/error-tracking/error-properties-builder.coerce.spec.ts +0 -337
- package/src/error-tracking/error-properties-builder.metadata.spec.ts +0 -318
- package/src/error-tracking/error-properties-builder.parse.spec.ts +0 -37
- package/src/error-tracking/exception-steps.spec.ts +0 -115
- package/src/error-tracking/parsers/base.spec.ts +0 -156
- package/src/error-tracking/parsers/index.spec.ts +0 -270
- package/src/error-tracking/parsers/node.spec.ts +0 -34
- package/src/error-tracking/release.spec.ts +0 -34
- package/src/logs/index.spec.ts +0 -2421
- package/src/logs/logs-utils.spec.ts +0 -266
- package/src/metrics/index.spec.ts +0 -1249
- package/src/surveys/activation.spec.ts +0 -61
- package/src/surveys/events.spec.ts +0 -183
- package/src/surveys/keys.spec.ts +0 -29
- package/src/surveys/property-matching.spec.ts +0 -539
- package/src/surveys/shuffling.spec.ts +0 -51
- package/src/surveys/translations.spec.ts +0 -274
- package/src/surveys/validation.spec.ts +0 -168
- package/src/traces/config.spec.ts +0 -271
- package/src/traces/ids.spec.ts +0 -115
- package/src/traces/index.spec.ts +0 -3287
- package/src/traces/live-spans.spec.ts +0 -69
- package/src/traces/otlp.spec.ts +0 -368
- package/src/traces/span.spec.ts +0 -1137
- package/src/traces/traceparent.spec.ts +0 -184
- package/src/utils/bot-detection.spec.ts +0 -107
- package/src/utils/bucketed-rate-limiter.spec.ts +0 -327
- package/src/utils/json-utils.spec.ts +0 -115
- package/src/utils/named-error.spec.ts +0 -36
- package/src/utils/number-utils.spec.ts +0 -212
- package/src/utils/otlp-any-value.spec.ts +0 -376
- package/src/utils/otlp-resource.spec.ts +0 -171
- package/src/utils/promise-queue.spec.ts +0 -55
- package/src/utils/string-utils.spec.ts +0 -158
- package/src/utils/type-utils.spec.ts +0 -38
- package/src/utils/webview-app-utils.spec.ts +0 -118
- package/src/vendor/uuidv7.spec.ts +0 -26
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
EXCEPTION_STEP_INTERNAL_FIELDS,
|
|
3
|
-
ExceptionStep,
|
|
4
|
-
ExceptionStepsBuffer,
|
|
5
|
-
getUtf8ByteLength,
|
|
6
|
-
resolveExceptionStepsConfig,
|
|
7
|
-
stripReservedExceptionStepFields,
|
|
8
|
-
} from './exception-steps'
|
|
9
|
-
|
|
10
|
-
describe('exception steps', () => {
|
|
11
|
-
describe('resolveExceptionStepsConfig', () => {
|
|
12
|
-
it('uses defaults when no config is passed', () => {
|
|
13
|
-
expect(resolveExceptionStepsConfig()).toEqual({
|
|
14
|
-
enabled: true,
|
|
15
|
-
max_bytes: 32768,
|
|
16
|
-
})
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
it('falls back to defaults for invalid values', () => {
|
|
20
|
-
expect(resolveExceptionStepsConfig({ max_bytes: Number.NaN })).toEqual({
|
|
21
|
-
enabled: true,
|
|
22
|
-
max_bytes: 32768,
|
|
23
|
-
})
|
|
24
|
-
})
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
describe('stripReservedExceptionStepFields', () => {
|
|
28
|
-
it('strips reserved fields and keeps custom properties', () => {
|
|
29
|
-
const result = stripReservedExceptionStepFields({
|
|
30
|
-
[EXCEPTION_STEP_INTERNAL_FIELDS.MESSAGE]: 'should-strip',
|
|
31
|
-
[EXCEPTION_STEP_INTERNAL_FIELDS.TIMESTAMP]: 'should-strip',
|
|
32
|
-
custom: true,
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
expect(result).toEqual({
|
|
36
|
-
sanitizedProperties: { custom: true },
|
|
37
|
-
droppedKeys: [EXCEPTION_STEP_INTERNAL_FIELDS.MESSAGE, EXCEPTION_STEP_INTERNAL_FIELDS.TIMESTAMP],
|
|
38
|
-
})
|
|
39
|
-
})
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
describe('ExceptionStepsBuffer', () => {
|
|
43
|
-
const makeStep = (message: string): ExceptionStep => ({
|
|
44
|
-
[EXCEPTION_STEP_INTERNAL_FIELDS.MESSAGE]: message,
|
|
45
|
-
[EXCEPTION_STEP_INTERNAL_FIELDS.TIMESTAMP]: '2026-01-01T00:00:00.000Z',
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
const bytesOf = (step: ExceptionStep) => getUtf8ByteLength(JSON.stringify(step))
|
|
49
|
-
|
|
50
|
-
it.each([
|
|
51
|
-
{
|
|
52
|
-
desc: 'evicts oldest steps when max bytes are exceeded',
|
|
53
|
-
config: { max_bytes: bytesOf(makeStep('two')) + bytesOf(makeStep('three')) },
|
|
54
|
-
steps: [makeStep('one'), makeStep('two'), makeStep('three')],
|
|
55
|
-
expected: ['two', 'three'],
|
|
56
|
-
},
|
|
57
|
-
{
|
|
58
|
-
desc: 'keeps the most recent step when budget fits only one',
|
|
59
|
-
config: { max_bytes: bytesOf(makeStep('one')) },
|
|
60
|
-
steps: [makeStep('one'), makeStep('two')],
|
|
61
|
-
expected: ['two'],
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
desc: 'skips malformed steps that cannot be normalized',
|
|
65
|
-
config: { max_bytes: 10000 },
|
|
66
|
-
steps: [{ $message: '', $timestamp: '2026-01-01T00:00:00.000Z' } as ExceptionStep, makeStep('valid')],
|
|
67
|
-
expected: ['valid'],
|
|
68
|
-
},
|
|
69
|
-
{
|
|
70
|
-
desc: 'drops a step that exceeds the entire budget on its own',
|
|
71
|
-
config: { max_bytes: 10 },
|
|
72
|
-
steps: [makeStep('this message is way too long for the tiny budget')],
|
|
73
|
-
expected: [],
|
|
74
|
-
},
|
|
75
|
-
])('$desc', ({ config, steps, expected }) => {
|
|
76
|
-
const buffer = new ExceptionStepsBuffer(config)
|
|
77
|
-
for (const step of steps) {
|
|
78
|
-
buffer.add(step)
|
|
79
|
-
}
|
|
80
|
-
expect(buffer.getAttachable().map((s) => s.$message)).toEqual(expected)
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
it('clears captured steps while retaining newer steps and their byte accounting', () => {
|
|
84
|
-
const budget = bytesOf(makeStep('two')) + bytesOf(makeStep('three'))
|
|
85
|
-
const buffer = new ExceptionStepsBuffer({ max_bytes: budget })
|
|
86
|
-
buffer.add(makeStep('one'))
|
|
87
|
-
const captured = buffer.getAttachable()[0]
|
|
88
|
-
buffer.add(makeStep('two'))
|
|
89
|
-
buffer.clearThrough(captured)
|
|
90
|
-
expect(buffer.getAttachable().map((step) => step.$message)).toEqual(['two'])
|
|
91
|
-
buffer.add(makeStep('three'))
|
|
92
|
-
expect(buffer.getAttachable().map((step) => step.$message)).toEqual(['two', 'three'])
|
|
93
|
-
buffer.clearThrough(captured)
|
|
94
|
-
expect(buffer.size()).toBe(2)
|
|
95
|
-
buffer.clearThrough(buffer.getAttachable()[1])
|
|
96
|
-
expect(buffer.size()).toBe(0)
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
it('leaves newer steps untouched when a captured step has already been evicted', () => {
|
|
100
|
-
const buffer = new ExceptionStepsBuffer({ max_bytes: bytesOf(makeStep('one')) })
|
|
101
|
-
buffer.add(makeStep('one'))
|
|
102
|
-
const captured = buffer.getAttachable()[0]
|
|
103
|
-
buffer.add(makeStep('two'))
|
|
104
|
-
buffer.clearThrough(captured)
|
|
105
|
-
expect(buffer.getAttachable().map((step) => step.$message)).toEqual(['two'])
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
it('clears all steps', () => {
|
|
109
|
-
const buffer = new ExceptionStepsBuffer({ max_bytes: 10000 })
|
|
110
|
-
buffer.add(makeStep('one'))
|
|
111
|
-
buffer.clear()
|
|
112
|
-
expect(buffer.size()).toBe(0)
|
|
113
|
-
})
|
|
114
|
-
})
|
|
115
|
-
})
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import { createFrame } from './base'
|
|
2
|
-
import { createDefaultStackParser } from './index'
|
|
3
|
-
|
|
4
|
-
describe.each(['web:javascript', 'hermes'] as const)('createFrame (%s)', (platform) => {
|
|
5
|
-
it('marks ordinary browser frames as in_app', () => {
|
|
6
|
-
const frame = createFrame(platform, 'https://example.com/app.js', 'doThing', 1, 2)
|
|
7
|
-
expect(frame.in_app).toBe(true)
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
it.each([
|
|
11
|
-
['Safari masking an extension content script', 'webkit-masked-url://hidden/'],
|
|
12
|
-
['an Android in-app browser bridge', 'iabjs://navigation_performance_logger_android'],
|
|
13
|
-
['a Chromium extension content script', 'chrome-extension://abcdef/content.js'],
|
|
14
|
-
['a Firefox extension content script', 'moz-extension://abcdef/content.js'],
|
|
15
|
-
['a Safari extension, which the parser prefixes with a bare scheme', 'safari-extension:abcdef/content.js'],
|
|
16
|
-
['a Safari web extension', 'safari-web-extension:abcdef/content.js'],
|
|
17
|
-
['an extension with a mixed-case scheme', 'CHROME-Extension://abcdef/content.js'],
|
|
18
|
-
['an unknown container scheme', 'other-bridge://injected.js'],
|
|
19
|
-
['an inline data URL', 'data:text/javascript,void 0'],
|
|
20
|
-
])('does not mark a frame the container injected as in_app: %s', (_name, filename) => {
|
|
21
|
-
const frame = createFrame(platform, filename, 'doThing', 1, 2)
|
|
22
|
-
expect(frame.in_app).toBe(false)
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it.each([
|
|
26
|
-
['a script served over http', 'http://localhost:8000/app.js'],
|
|
27
|
-
['a Cordova or Electron app served from disk', 'file:///android_asset/www/app.js'],
|
|
28
|
-
['a worker the page created from a blob', 'blob:https://example.com/6b0b0e6a'],
|
|
29
|
-
['a Capacitor webview', 'capacitor://localhost/app.js'],
|
|
30
|
-
['an Ionic webview', 'ionic://localhost/app.js'],
|
|
31
|
-
['a script with a mixed-case scheme', 'HTTPS://example.com/app.js'],
|
|
32
|
-
['a webpack source frame', 'webpack:///./src/App.js'],
|
|
33
|
-
['an Angular JIT component', 'ng:///CheckoutComponent.js'],
|
|
34
|
-
['a custom app scheme, as an Electron or React Native shell serves', 'app://checkout.js'],
|
|
35
|
-
['a bundler-rewritten dev frame', 'webpack-internal:///./src/App.js'],
|
|
36
|
-
['a React Native release bundle, which has no scheme', 'index.android.bundle'],
|
|
37
|
-
['an absolute path with no scheme', '/data/user/0/com.example/files/index.bundle'],
|
|
38
|
-
['a Windows path with backslashes', 'C:\\app\\index.bundle'],
|
|
39
|
-
['a Windows path with forward slashes', 'C:/app/index.bundle'],
|
|
40
|
-
])("still marks the app's own code as in_app: %s", (_name, filename) => {
|
|
41
|
-
const frame = createFrame(platform, filename, 'doThing', 1, 2)
|
|
42
|
-
expect(frame.in_app).toBe(true)
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
it('keeps the masked frame rather than discarding it', () => {
|
|
46
|
-
const frame = createFrame(platform, 'webkit-masked-url://hidden/', 'someFn', 3, 4)
|
|
47
|
-
expect(frame).toMatchObject({
|
|
48
|
-
filename: 'webkit-masked-url://hidden/',
|
|
49
|
-
function: 'someFn',
|
|
50
|
-
lineno: 3,
|
|
51
|
-
colno: 4,
|
|
52
|
-
})
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
it('does not mark Chromium <anonymous> frames as in_app', () => {
|
|
56
|
-
// Chromium reports scripts that have no URL (extension-injected code, devtools, string eval)
|
|
57
|
-
// as `<anonymous>`. They can never be symbolicated and are not attributable to the page.
|
|
58
|
-
const frame = createFrame(platform, '<anonymous>', '?', 1, 394)
|
|
59
|
-
expect(frame.in_app).toBe(false)
|
|
60
|
-
expect(frame).toMatchObject({ filename: '<anonymous>', function: '?', lineno: 1, colno: 394 })
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
it('does not mark frames without a filename as in_app', () => {
|
|
64
|
-
// Regex capture groups that do not participate arrive here as undefined despite the type
|
|
65
|
-
const frame = createFrame(platform, undefined as unknown as string, 'doThing')
|
|
66
|
-
expect(frame.in_app).toBe(false)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
it('does not mark frames with an empty filename as in_app', () => {
|
|
70
|
-
const frame = createFrame(platform, '', 'sendDataToNative', 1, 10198)
|
|
71
|
-
expect(frame.in_app).toBe(false)
|
|
72
|
-
expect(frame).toMatchObject({ function: 'sendDataToNative', lineno: 1, colno: 10198 })
|
|
73
|
-
})
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
describe('createDefaultStackParser in_app classification', () => {
|
|
77
|
-
const parse = createDefaultStackParser()
|
|
78
|
-
|
|
79
|
-
it('demotes a bare <anonymous> frame from injected code', () => {
|
|
80
|
-
const frames = parse("SyntaxError: Failed to execute 'appendChild' on 'Node': boom\n at <anonymous>:1:394")
|
|
81
|
-
expect(frames).toEqual([
|
|
82
|
-
{ platform: 'web:javascript', filename: '<anonymous>', function: '?', in_app: false, lineno: 1, colno: 394 },
|
|
83
|
-
])
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
it('demotes the native bridge frames an Android in-app browser injects', () => {
|
|
87
|
-
// The bridge script has no URL, so every frame arrives as `at <fn> (:1:<col>)`.
|
|
88
|
-
const frames = parse(
|
|
89
|
-
'Error: Error invoking postMessage: Java object is gone\n' +
|
|
90
|
-
' at sendDataToNative (:1:10198)\n' +
|
|
91
|
-
' at sendJsBlockingTimeMessage (:1:14668)'
|
|
92
|
-
)
|
|
93
|
-
expect(frames.map((f) => [f.function, f.in_app])).toEqual([
|
|
94
|
-
['sendJsBlockingTimeMessage', false],
|
|
95
|
-
['sendDataToNative', false],
|
|
96
|
-
])
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
it('demotes the bridge frames an Android in-app browser serves from its own scheme', () => {
|
|
100
|
-
const frames = parse(
|
|
101
|
-
'Error: Error invoking postMessage: Java object is gone\n' +
|
|
102
|
-
' at sendDataToNative (iabjs://navigation_performance_logger_android:1:10198)\n' +
|
|
103
|
-
' at sendJsBlockingTimeMessage (iabjs://navigation_performance_logger_android:1:14668)'
|
|
104
|
-
)
|
|
105
|
-
expect(frames.map((f) => [f.function, f.in_app])).toEqual([
|
|
106
|
-
['sendJsBlockingTimeMessage', false],
|
|
107
|
-
['sendDataToNative', false],
|
|
108
|
-
])
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
it.each([
|
|
112
|
-
' at CheckoutComponent_Template (ng:///CheckoutComponent.js:10:5)',
|
|
113
|
-
'CheckoutComponent_Template@ng:///CheckoutComponent.js:10:5',
|
|
114
|
-
])('keeps Angular JIT application frames as in_app: %s', (stackLine) => {
|
|
115
|
-
const frames = parse(`Error: template failed\n${stackLine}`)
|
|
116
|
-
expect(frames).toEqual([
|
|
117
|
-
expect.objectContaining({
|
|
118
|
-
filename: 'ng:///CheckoutComponent.js',
|
|
119
|
-
function: 'CheckoutComponent_Template',
|
|
120
|
-
lineno: 10,
|
|
121
|
-
colno: 5,
|
|
122
|
-
in_app: true,
|
|
123
|
-
}),
|
|
124
|
-
])
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
it.each([
|
|
128
|
-
'webkit-masked-url://hidden/',
|
|
129
|
-
'<anonymous>',
|
|
130
|
-
'iabjs://navigation_performance_logger_android',
|
|
131
|
-
'chrome-extension://abcdef/content.js',
|
|
132
|
-
'moz-extension://abcdef/content.js',
|
|
133
|
-
])('retains injected frames without demoting the calling app frame: %s', (filename) => {
|
|
134
|
-
const frames = parse(
|
|
135
|
-
`Error: boom\n at injected (${filename}:1:2)\n at checkout (https://example.com/app.js:10:5)`
|
|
136
|
-
)
|
|
137
|
-
expect(frames.map((frame) => [frame.filename, frame.in_app])).toEqual([
|
|
138
|
-
['https://example.com/app.js', true],
|
|
139
|
-
[filename, false],
|
|
140
|
-
])
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
it("keeps the page's own eval'd code as in_app", () => {
|
|
144
|
-
// V8 attributes eval'd code to the script that called eval; the chrome parser already
|
|
145
|
-
// rewrites these frames to that URL, so they must not be caught by the <anonymous> rule.
|
|
146
|
-
const frames = parse(
|
|
147
|
-
'Error: boom\n' +
|
|
148
|
-
' at eval (eval at <anonymous> (https://example.com/app.js:10:5), <anonymous>:1:394)\n' +
|
|
149
|
-
' at https://example.com/app.js:10:5'
|
|
150
|
-
)
|
|
151
|
-
expect(frames.map((f) => [f.filename, f.in_app])).toEqual([
|
|
152
|
-
['https://example.com/app.js', true],
|
|
153
|
-
['https://example.com/app.js', true],
|
|
154
|
-
])
|
|
155
|
-
})
|
|
156
|
-
})
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
import { createDefaultStackParser } from './index'
|
|
2
|
-
|
|
3
|
-
describe('createDefaultStackParser repeated cycle collapsing', () => {
|
|
4
|
-
const parse = createDefaultStackParser()
|
|
5
|
-
|
|
6
|
-
const OVERFLOW = 'RangeError: Maximum call stack size exceeded'
|
|
7
|
-
|
|
8
|
-
const frame = (func: string, line: number, column: number = 10): string =>
|
|
9
|
-
` at ${func} (https://posthog.com/app.js:${line}:${column})`
|
|
10
|
-
|
|
11
|
-
function recursion(cycle: string[], repeats: number): string[] {
|
|
12
|
-
const frames: string[] = []
|
|
13
|
-
|
|
14
|
-
for (let i = 0; i < repeats; i++) {
|
|
15
|
-
cycle.forEach((func, index) => frames.push(frame(func, index + 1)))
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return frames
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// The runtime lists the innermost frame first and cuts the stack wherever the depth ran out, so
|
|
22
|
-
// `dropInnermost` is how many frames of the cycle that cut took off the innermost end.
|
|
23
|
-
function overflowStack(cycle: string[], repeats: number, dropInnermost: number = 0): string {
|
|
24
|
-
return [
|
|
25
|
-
OVERFLOW,
|
|
26
|
-
...recursion(cycle, repeats).slice(dropInnermost),
|
|
27
|
-
frame('handleClick', 90),
|
|
28
|
-
frame('main', 91),
|
|
29
|
-
].join('\n')
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function names(stack: string): (string | undefined)[] {
|
|
33
|
-
return parse(stack).map((f) => f.function)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
it('keeps one copy of a recursion cycle', () => {
|
|
37
|
-
expect(names(overflowStack(['a', 'b'], 30))).toEqual(['main', 'handleClick', 'b', 'a'])
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('keeps one copy when a single function calls itself', () => {
|
|
41
|
-
expect(names(overflowStack(['a'], 60))).toEqual(['main', 'handleClick', 'a'])
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
it('keeps the outer frames a recursion used to push past the frame limit', () => {
|
|
45
|
-
// Without collapsing, the frame limit is spent on the cycle and the frames that name the
|
|
46
|
-
// caller never reach the event.
|
|
47
|
-
const frames = names(overflowStack(['a', 'b'], 400))
|
|
48
|
-
expect(frames).toContain('main')
|
|
49
|
-
expect(frames).toContain('handleClick')
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
it('gives the same frames wherever the runtime cut the recursion', () => {
|
|
53
|
-
// The overflow can happen in either function of the pair, which used to change the frames and
|
|
54
|
-
// so open a new issue for every throw.
|
|
55
|
-
const pair = names(overflowStack(['a', 'b'], 30))
|
|
56
|
-
expect(names(overflowStack(['a', 'b'], 30, 1))).toEqual(pair)
|
|
57
|
-
|
|
58
|
-
const triple = names(overflowStack(['a', 'b', 'c'], 30))
|
|
59
|
-
expect(names(overflowStack(['a', 'b', 'c'], 30, 1))).toEqual(triple)
|
|
60
|
-
expect(names(overflowStack(['a', 'b', 'c'], 30, 2))).toEqual(triple)
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
it('gives the same frames when the runtime cut everything outside the recursion', () => {
|
|
64
|
-
// The runtime keeps the innermost frames, so a deep recursion can push every other frame out of
|
|
65
|
-
// the stack. Nothing then says which function of the cycle ran out of stack first.
|
|
66
|
-
const recursionOnly = (dropInnermost: number): (string | undefined)[] =>
|
|
67
|
-
names([OVERFLOW, ...recursion(['a', 'b'], 40).slice(dropInnermost)].join('\n'))
|
|
68
|
-
|
|
69
|
-
expect(recursionOnly(1)).toEqual(recursionOnly(0))
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('gives the same frames when the stack holds a second repeated section', () => {
|
|
73
|
-
// Stacks repeat outside the recursion too, such as a test runner that calls itself once per
|
|
74
|
-
// nested block. The frames of that section have to keep the recursion's own partial copy.
|
|
75
|
-
const withOuterRepeat = (dropInnermost: number): (string | undefined)[] =>
|
|
76
|
-
names(
|
|
77
|
-
[
|
|
78
|
-
OVERFLOW,
|
|
79
|
-
...recursion(['a', 'b'], 30).slice(dropInnermost),
|
|
80
|
-
frame('handleClick', 90),
|
|
81
|
-
frame('walk', 91),
|
|
82
|
-
frame('walk', 91),
|
|
83
|
-
frame('main', 92),
|
|
84
|
-
].join('\n')
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
expect(withOuterRepeat(0)).toEqual(['main', 'walk', 'handleClick', 'b', 'a'])
|
|
88
|
-
expect(withOuterRepeat(1)).toEqual(withOuterRepeat(0))
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
it('gives the same frames when a cycle that repeats a call site is all that is left', () => {
|
|
92
|
-
// One copy of a cycle can hold the same call site twice, and the innermost frame carries a
|
|
93
|
-
// column of its own, so a single frame does not say which rotation of the copy this throw is.
|
|
94
|
-
const cycle = [frame('a', 1, 10), frame('b', 2), frame('a', 1, 20), frame('c', 4)]
|
|
95
|
-
const lines: string[] = []
|
|
96
|
-
|
|
97
|
-
for (let i = 0; i < 30; i++) {
|
|
98
|
-
lines.push(...cycle)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const fromCut = (dropInnermost: number): (string | undefined)[] =>
|
|
102
|
-
names([OVERFLOW, ...lines.slice(dropInnermost)].join('\n'))
|
|
103
|
-
|
|
104
|
-
expect(fromCut(0)).toEqual(['c', 'a', 'b', 'a'])
|
|
105
|
-
expect(fromCut(1)).toEqual(fromCut(0))
|
|
106
|
-
expect(fromCut(2)).toEqual(fromCut(0))
|
|
107
|
-
expect(fromCut(3)).toEqual(fromCut(0))
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
it('reads the outer frames a leftover copy would have pushed past the frame limit', () => {
|
|
111
|
-
// The leftover copy is dropped once the whole stack is read, so it must not hold a slot of the
|
|
112
|
-
// frame limit while the parser is still reading the frames outside the recursion.
|
|
113
|
-
const callers: string[] = []
|
|
114
|
-
|
|
115
|
-
for (let i = 0; i < 48; i++) {
|
|
116
|
-
callers.push(frame(`c${i}`, i + 10))
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const withCallers = (dropInnermost: number): (string | undefined)[] =>
|
|
120
|
-
names([OVERFLOW, ...recursion(['a', 'b'], 30).slice(dropInnermost), ...callers].join('\n'))
|
|
121
|
-
|
|
122
|
-
expect(withCallers(0)).toHaveLength(50)
|
|
123
|
-
expect(withCallers(1)).toEqual(withCallers(0))
|
|
124
|
-
|
|
125
|
-
// A distinct throw location is not a partial copy and must use a slot of the frame budget.
|
|
126
|
-
const withThrowColumn = [OVERFLOW, frame('a', 1, 4), ...recursion(['a', 'b'], 30).slice(1), ...callers].join('\n')
|
|
127
|
-
const frames = parse(withThrowColumn)
|
|
128
|
-
expect(frames).toHaveLength(50)
|
|
129
|
-
expect(frames.map((f) => f.function)).toEqual([...withCallers(0).slice(1), 'a'])
|
|
130
|
-
expect(frames[frames.length - 1]?.colno).toBe(4)
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
it('gives the same frames when the recursion path calls one function twice in a row', () => {
|
|
134
|
-
// A minified bundle holds one line number per function, so the two calls to `b` are told apart
|
|
135
|
-
// by their column alone. The kept copy holds both of them, and every cut agrees on it.
|
|
136
|
-
const cycle = [frame('a', 1), frame('b', 2, 10), frame('b', 2, 20)]
|
|
137
|
-
const lines: string[] = []
|
|
138
|
-
|
|
139
|
-
for (let i = 0; i < 30; i++) {
|
|
140
|
-
lines.push(...cycle)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const fromCut = (dropInnermost: number): (string | undefined)[] =>
|
|
144
|
-
names([OVERFLOW, ...lines.slice(dropInnermost), frame('handleClick', 90), frame('main', 91)].join('\n'))
|
|
145
|
-
|
|
146
|
-
expect(fromCut(0)).toEqual(['main', 'handleClick', 'b', 'b', 'a'])
|
|
147
|
-
expect(fromCut(1)).toEqual(fromCut(0))
|
|
148
|
-
expect(fromCut(2)).toEqual(fromCut(0))
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
it('keeps minified frames that share a name and a line but not a column', () => {
|
|
152
|
-
// A minified bundle puts every function on one line and hands out the same short names, so the
|
|
153
|
-
// column is all that tells two of them apart. Nothing repeats here, so no frame may be dropped.
|
|
154
|
-
const stack = [
|
|
155
|
-
'Error: boom',
|
|
156
|
-
frame('i', 1, 310),
|
|
157
|
-
frame('t', 1, 4821),
|
|
158
|
-
frame('t', 1, 9137),
|
|
159
|
-
frame('r', 1, 2210),
|
|
160
|
-
frame('onClick', 1, 640),
|
|
161
|
-
].join('\n')
|
|
162
|
-
|
|
163
|
-
expect(parse(stack).map((f) => `${f.function}:${f.colno}`)).toEqual([
|
|
164
|
-
'onClick:640',
|
|
165
|
-
'r:2210',
|
|
166
|
-
't:9137',
|
|
167
|
-
't:4821',
|
|
168
|
-
'i:310',
|
|
169
|
-
])
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
it.each(['Error: boom', OVERFLOW])('preserves the unnamed throw location for %s', (header) => {
|
|
173
|
-
// Even a confirmed recursion cannot establish that a different innermost column is redundant.
|
|
174
|
-
// Keep that location for both ordinary errors and overflow stacks.
|
|
175
|
-
const anonymous = (column: number): string => ` at https://posthog.com/app.js:1:${column}`
|
|
176
|
-
|
|
177
|
-
const withThrowColumn = (column: number): string[] => {
|
|
178
|
-
const lines = [anonymous(column)]
|
|
179
|
-
|
|
180
|
-
for (let i = 1; i < 40; i++) {
|
|
181
|
-
lines.push(anonymous(10))
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return parse([header, ...lines, frame('handleClick', 90), frame('main', 91)].join('\n')).map(
|
|
185
|
-
(f) => `${f.function}:${f.colno}`
|
|
186
|
-
)
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
expect(withThrowColumn(4)).toEqual(['main:10', 'handleClick:10', '?:10', '?:4'])
|
|
190
|
-
expect(withThrowColumn(5)).toEqual(['main:10', 'handleClick:10', '?:10', '?:5'])
|
|
191
|
-
expect(withThrowColumn(10)).toEqual(['main:10', 'handleClick:10', '?:10'])
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
it('preserves the column of the call that ran out of stack', () => {
|
|
195
|
-
// Do not guess whether a different column is a redundant overflow location or a distinct throw.
|
|
196
|
-
const stack = [OVERFLOW, frame('a', 1, 4), ...recursion(['a', 'b'], 3).slice(1), frame('handleClick', 90)].join(
|
|
197
|
-
'\n'
|
|
198
|
-
)
|
|
199
|
-
|
|
200
|
-
expect(parse(stack).map((f) => `${f.function}:${f.colno}`)).toEqual(['handleClick:10', 'b:10', 'a:10', 'a:4'])
|
|
201
|
-
})
|
|
202
|
-
|
|
203
|
-
it('keeps frames that repeat without a complete second cycle', () => {
|
|
204
|
-
const stack = ['Error: boom', frame('a', 1), frame('b', 2), frame('a', 1), frame('main', 91)].join('\n')
|
|
205
|
-
expect(names(stack)).toEqual(['main', 'a', 'b', 'a'])
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
it('invalidates a cycle record subsumed by a later collapse', () => {
|
|
209
|
-
const stack = ['Error: boom', frame('a', 1), frame('b', 2), frame('a', 1), frame('a', 1), frame('b', 2)].join('\n')
|
|
210
|
-
|
|
211
|
-
expect(names(stack)).toEqual(['b', 'a'])
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
it('keeps disjoint cycle records when a later collapse subsumes another record', () => {
|
|
215
|
-
const stack = [
|
|
216
|
-
'Error: boom',
|
|
217
|
-
frame('inner', 90),
|
|
218
|
-
frame('inner', 90),
|
|
219
|
-
frame('a', 1),
|
|
220
|
-
frame('b', 2),
|
|
221
|
-
frame('a', 1),
|
|
222
|
-
frame('a', 1),
|
|
223
|
-
frame('b', 2),
|
|
224
|
-
frame('outer', 91),
|
|
225
|
-
frame('outer', 91),
|
|
226
|
-
].join('\n')
|
|
227
|
-
|
|
228
|
-
expect(names(stack)).toEqual(['outer', 'b', 'a', 'inner'])
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
it.each([4, 5])('preserves the throw column %i of an ordinary recursive error', (column) => {
|
|
232
|
-
const stack = [
|
|
233
|
-
'Error: boom',
|
|
234
|
-
frame('recurse', 1, column),
|
|
235
|
-
frame('recurse', 1, 10),
|
|
236
|
-
frame('recurse', 1, 10),
|
|
237
|
-
frame('main', 91),
|
|
238
|
-
].join('\n')
|
|
239
|
-
|
|
240
|
-
expect(parse(stack).map((f) => `${f.function}:${f.colno}`)).toEqual(['main:10', 'recurse:10', `recurse:${column}`])
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
it('keeps distinct innermost minified call sites for an ordinary error', () => {
|
|
244
|
-
const stack = ['Error: boom', frame('t', 1, 100), frame('t', 1, 200), frame('main', 91)].join('\n')
|
|
245
|
-
|
|
246
|
-
expect(parse(stack).map((f) => `${f.function}:${f.colno}`)).toEqual(['main:10', 't:200', 't:100'])
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
it('canonicalizes recursion-only cycles whose rotations differ only by columns', () => {
|
|
250
|
-
const cycle = [frame('a', 1, 10), frame('b', 1, 20), frame('a', 1, 30), frame('b', 1, 40)]
|
|
251
|
-
const lines: string[] = []
|
|
252
|
-
|
|
253
|
-
for (let i = 0; i < 30; i++) {
|
|
254
|
-
lines.push(...cycle)
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
const fromCut = (dropInnermost: number): string[] =>
|
|
258
|
-
parse([OVERFLOW, ...lines.slice(dropInnermost)].join('\n')).map((f) => `${f.function}:${f.colno}`)
|
|
259
|
-
|
|
260
|
-
expect(fromCut(0)).toEqual(['b:40', 'a:30', 'b:20', 'a:10'])
|
|
261
|
-
expect(fromCut(1)).toEqual(fromCut(0))
|
|
262
|
-
expect(fromCut(2)).toEqual(fromCut(0))
|
|
263
|
-
expect(fromCut(3)).toEqual(fromCut(0))
|
|
264
|
-
})
|
|
265
|
-
|
|
266
|
-
it('keeps calls to one function from different call sites', () => {
|
|
267
|
-
const stack = ['Error: boom', frame('a', 1), frame('a', 2), frame('a', 3), frame('main', 91)].join('\n')
|
|
268
|
-
expect(names(stack)).toEqual(['main', 'a', 'a', 'a'])
|
|
269
|
-
})
|
|
270
|
-
})
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { nodeStackLineParser } from './node'
|
|
2
|
-
|
|
3
|
-
describe('nodeStackLineParser', () => {
|
|
4
|
-
it.each([
|
|
5
|
-
['at async Promise.all (index 3)', 'Promise.all'],
|
|
6
|
-
['at async Promise.all (index 472)', 'Promise.all'],
|
|
7
|
-
['at async Promise.any (index 11)', 'Promise.any'],
|
|
8
|
-
])('canonicalizes the V8 Promise index in %s', (line, functionName) => {
|
|
9
|
-
expect(nodeStackLineParser(line, 'node:javascript')).toEqual({
|
|
10
|
-
filename: 'node:internal/promise',
|
|
11
|
-
module: undefined,
|
|
12
|
-
function: functionName,
|
|
13
|
-
lineno: undefined,
|
|
14
|
-
colno: undefined,
|
|
15
|
-
in_app: false,
|
|
16
|
-
platform: 'node:javascript',
|
|
17
|
-
})
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
it.each([
|
|
21
|
-
['at async (/app/server/api/handler.ts:91:27)', 'async'],
|
|
22
|
-
['at async (work) (/app/server/api/handler.ts:91:27)', '(work)'],
|
|
23
|
-
])('parses the function name and filename in %s', (line, functionName) => {
|
|
24
|
-
expect(nodeStackLineParser(line, 'node:javascript')).toEqual({
|
|
25
|
-
filename: '/app/server/api/handler.ts',
|
|
26
|
-
module: undefined,
|
|
27
|
-
function: functionName,
|
|
28
|
-
lineno: 91,
|
|
29
|
-
colno: 27,
|
|
30
|
-
in_app: true,
|
|
31
|
-
platform: 'node:javascript',
|
|
32
|
-
})
|
|
33
|
-
})
|
|
34
|
-
})
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { getInjectedReleaseId } from './release'
|
|
2
|
-
|
|
3
|
-
describe('getInjectedReleaseId', () => {
|
|
4
|
-
const originalReleaseId = process.env.POSTHOG_RELEASE_ID
|
|
5
|
-
|
|
6
|
-
afterEach(() => {
|
|
7
|
-
if (originalReleaseId === undefined) {
|
|
8
|
-
delete process.env.POSTHOG_RELEASE_ID
|
|
9
|
-
} else {
|
|
10
|
-
process.env.POSTHOG_RELEASE_ID = originalReleaseId
|
|
11
|
-
}
|
|
12
|
-
delete (globalThis as any)._posthogReleaseId
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
it('reads and trims POSTHOG_RELEASE_ID', () => {
|
|
16
|
-
process.env.POSTHOG_RELEASE_ID = ' release-from-environment '
|
|
17
|
-
|
|
18
|
-
expect(getInjectedReleaseId()).toBe('release-from-environment')
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
it('prefers POSTHOG_RELEASE_ID over the injected global', () => {
|
|
22
|
-
process.env.POSTHOG_RELEASE_ID = 'release-from-environment'
|
|
23
|
-
;(globalThis as any)._posthogReleaseId = 'release-from-injection'
|
|
24
|
-
|
|
25
|
-
expect(getInjectedReleaseId()).toBe('release-from-environment')
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('falls back to the injected global when the environment value is empty', () => {
|
|
29
|
-
process.env.POSTHOG_RELEASE_ID = ' '
|
|
30
|
-
;(globalThis as any)._posthogReleaseId = 'release-from-injection'
|
|
31
|
-
|
|
32
|
-
expect(getInjectedReleaseId()).toBe('release-from-injection')
|
|
33
|
-
})
|
|
34
|
-
})
|