@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,115 +0,0 @@
1
- import { toJsonSafeValue } from './json-utils'
2
-
3
- describe('toJsonSafeValue', () => {
4
- it('converts circular and unsupported values while preserving shared references', () => {
5
- const shared = { id: 1 }
6
- const value: Record<string, unknown> = {
7
- count: BigInt(42),
8
- callback: () => undefined,
9
- symbol: Symbol('test'),
10
- invalidNumber: Number.NaN,
11
- loneSurrogate: '\ud800',
12
- validSurrogatePair: '😀',
13
- first: shared,
14
- second: shared,
15
- }
16
- value.self = value
17
-
18
- expect(toJsonSafeValue(value)).toEqual({
19
- count: '42',
20
- callback: '[Function]',
21
- symbol: 'Symbol(test)',
22
- invalidNumber: null,
23
- loneSurrogate: '�',
24
- validSurrogatePair: '😀',
25
- first: { id: 1 },
26
- second: { id: 1 },
27
- self: '[Circular]',
28
- })
29
- })
30
-
31
- it('sanitizes toJSON results and falls back when toJSON throws', () => {
32
- const serializableToJSON = vi.fn(() => ({ count: BigInt(2) }))
33
- const serializable = Object.create({ toJSON: serializableToJSON })
34
- const throwingToJSON = vi.fn(() => {
35
- throw new Error('cannot serialize')
36
- })
37
- const selfReturningValue: { toJSON: () => unknown } = {
38
- toJSON: () => selfReturningValue,
39
- }
40
- const getTimeOverride = vi.fn(() => 0)
41
- const toISOStringOverride = vi.fn(() => BigInt(2))
42
- const date = new Date('2025-01-02T03:04:05.000Z')
43
- Object.defineProperties(date, {
44
- getTime: { value: getTimeOverride },
45
- toISOString: { value: toISOStringOverride },
46
- })
47
-
48
- expect(
49
- toJsonSafeValue({
50
- serializable,
51
- throwing: { value: 'kept', toJSON: throwingToJSON },
52
- selfReturningValue,
53
- date,
54
- })
55
- ).toEqual({
56
- serializable: { count: '2' },
57
- throwing: { value: 'kept', toJSON: '[Function]' },
58
- selfReturningValue: '[Circular]',
59
- date: '2025-01-02T03:04:05.000Z',
60
- })
61
- expect(serializableToJSON).toHaveBeenCalledTimes(1)
62
- expect(throwingToJSON).toHaveBeenCalledTimes(1)
63
- expect(getTimeOverride).not.toHaveBeenCalled()
64
- expect(toISOStringOverride).not.toHaveBeenCalled()
65
- })
66
-
67
- it('stops before traversing inherited enumerable properties', () => {
68
- const prototype = Object.fromEntries(Array.from({ length: 2_000 }, (_, index) => [`key-${index}`, index]))
69
- const target = Object.assign(Object.create(prototype), { own: 'kept' })
70
- const getOwnPropertyDescriptor = vi.fn((object: object, key: string | symbol) =>
71
- Object.getOwnPropertyDescriptor(object, key)
72
- )
73
- const value = new Proxy(target, { getOwnPropertyDescriptor })
74
-
75
- expect(toJsonSafeValue(value)).toEqual({ own: 'kept' })
76
- expect(getOwnPropertyDescriptor.mock.calls.length).toBeLessThan(10)
77
- })
78
-
79
- it('returns a fallback for values that throw during traversal', () => {
80
- const value = new Proxy(
81
- {},
82
- {
83
- ownKeys() {
84
- throw new Error('cannot inspect')
85
- },
86
- }
87
- )
88
-
89
- expect(toJsonSafeValue(value)).toBe('[Unserializable]')
90
- })
91
-
92
- it('bounds deeply nested, oversized, and node-heavy values', () => {
93
- const deepValue: Record<string, unknown> = {}
94
- let cursor = deepValue
95
- for (let depth = 0; depth < 100; depth++) {
96
- const child: Record<string, unknown> = {}
97
- cursor.child = child
98
- cursor = child
99
- }
100
- const oversizedArray = Array.from({ length: 2_000 }, (_, index) => index)
101
- const oversizedObject = Object.fromEntries(Array.from({ length: 2_000 }, (_, index) => [`key-${index}`, index]))
102
- const nodeHeavyValue = Array.from({ length: 1_000 }, () => Array.from({ length: 20 }, () => true))
103
-
104
- expect(JSON.stringify(toJsonSafeValue(deepValue))).toContain('[Truncated]')
105
- expect(toJsonSafeValue(oversizedArray)).toEqual([...oversizedArray.slice(0, 1_000), '[Truncated]'])
106
-
107
- const safeObject = toJsonSafeValue(oversizedObject) as Record<string, unknown>
108
- expect(Object.keys(safeObject)).toHaveLength(1_001)
109
- expect(safeObject['[Truncated]']).toBe('Additional properties omitted')
110
-
111
- const safeNodeHeavyValue = toJsonSafeValue(nodeHeavyValue) as unknown[]
112
- expect(safeNodeHeavyValue).toHaveLength(478)
113
- expect(safeNodeHeavyValue.at(-1)).toBe('[Truncated]')
114
- })
115
- })
@@ -1,36 +0,0 @@
1
- import { createNamedError } from './index'
2
-
3
- describe('createNamedError', () => {
4
- it('sets the name and message', () => {
5
- const error = createNamedError('AbortError', 'timed out')
6
- expect(error.name).toBe('AbortError')
7
- expect(error.message).toBe('timed out')
8
- })
9
-
10
- it('defines name with the same descriptor a plain assignment would produce', () => {
11
- const error = createNamedError('AbortError', 'timed out')
12
- expect(Object.getOwnPropertyDescriptor(error, 'name')).toEqual({
13
- value: 'AbortError',
14
- writable: true,
15
- enumerable: true,
16
- configurable: true,
17
- })
18
- })
19
-
20
- it('preserves the name when Error.prototype.name is non-writable', () => {
21
- const descriptor = Object.getOwnPropertyDescriptor(Error.prototype, 'name') ?? {
22
- value: 'Error',
23
- writable: true,
24
- enumerable: false,
25
- configurable: true,
26
- }
27
- Object.defineProperty(Error.prototype, 'name', { value: 'Error', writable: false, configurable: true })
28
- try {
29
- const error = createNamedError('AbortError', 'timed out')
30
- expect(error.name).toBe('AbortError')
31
- expect(error.message).toBe('timed out')
32
- } finally {
33
- Object.defineProperty(Error.prototype, 'name', descriptor)
34
- }
35
- })
36
- })
@@ -1,212 +0,0 @@
1
- import { createMockLogger } from '@/testing'
2
- import { clampToRange, getRemoteConfigBool, getRemoteConfigNumber, isValidSampleRate } from './number-utils'
3
-
4
- describe('number-utils', () => {
5
- const mockLogger = createMockLogger()
6
-
7
- describe('clampToRange', () => {
8
- it.each([
9
- [
10
- 'returns max when value is not a number',
11
- {
12
- value: null,
13
- min: 10,
14
- max: 100,
15
- expected: 100,
16
- fallback: undefined,
17
- },
18
- ],
19
- [
20
- 'returns max when value is not a number',
21
- {
22
- value: 'not-a-number',
23
- min: 10,
24
- max: 100,
25
- expected: 100,
26
- fallback: undefined,
27
- },
28
- ],
29
- [
30
- 'returns max when value is greater than max',
31
- {
32
- value: 150,
33
- min: 10,
34
- max: 100,
35
- expected: 100,
36
- fallback: undefined,
37
- },
38
- ],
39
- [
40
- 'returns min when value is less than min',
41
- {
42
- value: 5,
43
- min: 10,
44
- max: 100,
45
- expected: 10,
46
- fallback: undefined,
47
- },
48
- ],
49
- [
50
- 'returns the value when it is within the range',
51
- {
52
- value: 50,
53
- min: 10,
54
- max: 100,
55
- expected: 50,
56
- fallback: undefined,
57
- },
58
- ],
59
- [
60
- 'returns the fallback value when provided is not valid',
61
- {
62
- value: 'invalid',
63
- min: 10,
64
- max: 100,
65
- expected: 20,
66
- fallback: 20,
67
- },
68
- ],
69
- [
70
- 'honors a fallback of 0 instead of falling back to max',
71
- {
72
- value: 'invalid',
73
- min: 0,
74
- max: 100,
75
- expected: 0,
76
- fallback: 0,
77
- },
78
- ],
79
- [
80
- 'clamps a fallback of 0 up to min',
81
- {
82
- value: 'invalid',
83
- min: 10,
84
- max: 100,
85
- expected: 10,
86
- fallback: 0,
87
- },
88
- ],
89
- [
90
- 'returns the max value when fallback is not valid',
91
- {
92
- value: 'invalid',
93
- min: 10,
94
- max: 75,
95
- expected: 75,
96
- fallback: '20',
97
- },
98
- ],
99
- [
100
- 'returns the fallback when value is NaN (NaN is not a valid number)',
101
- {
102
- value: NaN,
103
- min: 10,
104
- max: 100,
105
- expected: 20,
106
- fallback: 20,
107
- },
108
- ],
109
- [
110
- 'returns max when value is NaN and no fallback is provided',
111
- {
112
- value: NaN,
113
- min: 10,
114
- max: 100,
115
- expected: 100,
116
- fallback: undefined,
117
- },
118
- ],
119
- [
120
- 'clamps positive Infinity to max',
121
- {
122
- value: Infinity,
123
- min: 10,
124
- max: 100,
125
- expected: 100,
126
- fallback: undefined,
127
- },
128
- ],
129
- [
130
- 'clamps negative Infinity to min',
131
- {
132
- value: -Infinity,
133
- min: 10,
134
- max: 100,
135
- expected: 10,
136
- fallback: undefined,
137
- },
138
- ],
139
- ])('%s', (_description, { value, min, max, expected, fallback }) => {
140
- const result = clampToRange(value, min, max, mockLogger, fallback as any)
141
- expect(result).toBe(expected)
142
- })
143
-
144
- it('logs a warning when min is greater than max', () => {
145
- expect(clampToRange(50, 100, 10, mockLogger)).toBe(10)
146
- expect(mockLogger.warn).toHaveBeenCalledWith('min cannot be greater than max.')
147
- })
148
- })
149
-
150
- describe('getRemoteConfigBool', () => {
151
- it('returns default value when field is undefined or null', () => {
152
- expect(getRemoteConfigBool(undefined, 'key')).toBe(true)
153
- expect(getRemoteConfigBool(undefined, 'key', false)).toBe(false)
154
- expect(getRemoteConfigBool(null as any, 'key')).toBe(true)
155
- })
156
-
157
- it('returns the boolean directly when field is boolean', () => {
158
- expect(getRemoteConfigBool(true, 'key')).toBe(true)
159
- expect(getRemoteConfigBool(false, 'key')).toBe(false)
160
- expect(getRemoteConfigBool(false, 'key', true)).toBe(false)
161
- })
162
-
163
- it('returns the key value when field is an object with a boolean key', () => {
164
- expect(getRemoteConfigBool({ autocaptureExceptions: true }, 'autocaptureExceptions')).toBe(true)
165
- expect(getRemoteConfigBool({ autocaptureExceptions: false }, 'autocaptureExceptions')).toBe(false)
166
- })
167
-
168
- it('returns default value when key is missing or non-boolean', () => {
169
- expect(getRemoteConfigBool({ otherKey: 'value' }, 'autocaptureExceptions')).toBe(true)
170
- expect(getRemoteConfigBool({ otherKey: 'value' }, 'autocaptureExceptions', false)).toBe(false)
171
- expect(getRemoteConfigBool({ autocaptureExceptions: 'yes' }, 'autocaptureExceptions')).toBe(true)
172
- })
173
-
174
- it('returns default true for empty object', () => {
175
- expect(getRemoteConfigBool({}, 'key')).toBe(true)
176
- })
177
- })
178
-
179
- describe('getRemoteConfigNumber', () => {
180
- it('returns undefined for missing/invalid fields', () => {
181
- expect(getRemoteConfigNumber(undefined, 'sampleRate')).toBeUndefined()
182
- expect(getRemoteConfigNumber(false, 'sampleRate')).toBeUndefined()
183
- expect(getRemoteConfigNumber({}, 'sampleRate')).toBeUndefined()
184
- expect(getRemoteConfigNumber({ sampleRate: 'abc' }, 'sampleRate')).toBeUndefined()
185
- expect(getRemoteConfigNumber({ sampleRate: '' }, 'sampleRate')).toBeUndefined()
186
- expect(getRemoteConfigNumber({ sampleRate: ' ' }, 'sampleRate')).toBeUndefined()
187
- })
188
-
189
- it('returns numeric value from number', () => {
190
- expect(getRemoteConfigNumber({ sampleRate: 0.5 }, 'sampleRate')).toBe(0.5)
191
- })
192
-
193
- it('returns numeric value from numeric string', () => {
194
- expect(getRemoteConfigNumber({ sampleRate: '0.5' }, 'sampleRate')).toBe(0.5)
195
- })
196
- })
197
-
198
- describe('isValidSampleRate', () => {
199
- it('returns true only for finite values in [0, 1]', () => {
200
- expect(isValidSampleRate(0)).toBe(true)
201
- expect(isValidSampleRate(0.5)).toBe(true)
202
- expect(isValidSampleRate(1)).toBe(true)
203
-
204
- expect(isValidSampleRate(-0.1)).toBe(false)
205
- expect(isValidSampleRate(1.1)).toBe(false)
206
- expect(isValidSampleRate(Number.POSITIVE_INFINITY)).toBe(false)
207
- expect(isValidSampleRate(Number.NaN)).toBe(false)
208
- expect(isValidSampleRate('0.5')).toBe(false)
209
- expect(isValidSampleRate(null)).toBe(false)
210
- })
211
- })
212
- })