@posthog/core 1.69.9 → 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 (122) 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/featureFlagLocalEvaluation.d.ts +2 -0
  35. package/dist/featureFlagLocalEvaluation.d.ts.map +1 -1
  36. package/dist/featureFlagLocalEvaluation.js +38 -0
  37. package/dist/featureFlagLocalEvaluation.mjs +38 -0
  38. package/dist/featureFlagUtils.d.ts +7 -5
  39. package/dist/featureFlagUtils.d.ts.map +1 -1
  40. package/dist/featureFlagUtils.js +17 -11
  41. package/dist/featureFlagUtils.mjs +14 -11
  42. package/dist/index.d.ts +1 -1
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +27 -16
  45. package/dist/index.mjs +1 -1
  46. package/dist/posthog-core-stateless.d.ts +44 -7
  47. package/dist/posthog-core-stateless.d.ts.map +1 -1
  48. package/dist/posthog-core-stateless.js +103 -24
  49. package/dist/posthog-core-stateless.mjs +103 -24
  50. package/dist/posthog-core.d.ts +138 -53
  51. package/dist/posthog-core.d.ts.map +1 -1
  52. package/dist/posthog-core.js +341 -115
  53. package/dist/posthog-core.mjs +338 -115
  54. package/dist/types.d.ts +33 -1
  55. package/dist/types.d.ts.map +1 -1
  56. package/dist/utils/index.d.ts +1 -1
  57. package/dist/utils/index.d.ts.map +1 -1
  58. package/dist/utils/index.js +25 -12
  59. package/dist/utils/index.mjs +1 -1
  60. package/dist/utils/json-utils.d.ts +11 -1
  61. package/dist/utils/json-utils.d.ts.map +1 -1
  62. package/dist/utils/json-utils.js +50 -2
  63. package/dist/utils/json-utils.mjs +46 -1
  64. package/package.json +13 -3
  65. package/src/capture-v1/compression.ts +50 -0
  66. package/src/capture-v1/errors.ts +91 -0
  67. package/src/capture-v1/index.ts +34 -0
  68. package/src/capture-v1/sender.ts +779 -0
  69. package/src/capture-v1/serialize.ts +113 -0
  70. package/src/capture-v1/transform.ts +135 -0
  71. package/src/capture-v1/types.ts +59 -0
  72. package/src/capture-v1/uuid.ts +53 -0
  73. package/src/featureFlagLocalEvaluation.ts +51 -0
  74. package/src/featureFlagUtils.ts +22 -10
  75. package/src/index.ts +1 -0
  76. package/src/posthog-core-stateless.ts +190 -35
  77. package/src/posthog-core.ts +615 -178
  78. package/src/types.ts +31 -1
  79. package/src/utils/index.ts +1 -1
  80. package/src/utils/json-utils.ts +64 -1
  81. package/src/error-tracking/chunk-ids.spec.ts +0 -77
  82. package/src/error-tracking/coercers/error-event-coercer.spec.ts +0 -142
  83. package/src/error-tracking/coercers/promise-rejection-event.spec.ts +0 -77
  84. package/src/error-tracking/coercers/string-coercer.spec.ts +0 -26
  85. package/src/error-tracking/error-properties-builder.aggregate-errors.spec.ts +0 -412
  86. package/src/error-tracking/error-properties-builder.coerce.spec.ts +0 -337
  87. package/src/error-tracking/error-properties-builder.metadata.spec.ts +0 -318
  88. package/src/error-tracking/error-properties-builder.parse.spec.ts +0 -37
  89. package/src/error-tracking/exception-steps.spec.ts +0 -115
  90. package/src/error-tracking/parsers/base.spec.ts +0 -156
  91. package/src/error-tracking/parsers/index.spec.ts +0 -270
  92. package/src/error-tracking/parsers/node.spec.ts +0 -34
  93. package/src/error-tracking/release.spec.ts +0 -34
  94. package/src/logs/index.spec.ts +0 -2421
  95. package/src/logs/logs-utils.spec.ts +0 -266
  96. package/src/metrics/index.spec.ts +0 -1249
  97. package/src/surveys/activation.spec.ts +0 -61
  98. package/src/surveys/events.spec.ts +0 -183
  99. package/src/surveys/keys.spec.ts +0 -29
  100. package/src/surveys/property-matching.spec.ts +0 -539
  101. package/src/surveys/shuffling.spec.ts +0 -51
  102. package/src/surveys/translations.spec.ts +0 -274
  103. package/src/surveys/validation.spec.ts +0 -168
  104. package/src/traces/config.spec.ts +0 -271
  105. package/src/traces/ids.spec.ts +0 -115
  106. package/src/traces/index.spec.ts +0 -3287
  107. package/src/traces/live-spans.spec.ts +0 -69
  108. package/src/traces/otlp.spec.ts +0 -368
  109. package/src/traces/span.spec.ts +0 -1137
  110. package/src/traces/traceparent.spec.ts +0 -184
  111. package/src/utils/bot-detection.spec.ts +0 -107
  112. package/src/utils/bucketed-rate-limiter.spec.ts +0 -327
  113. package/src/utils/json-utils.spec.ts +0 -115
  114. package/src/utils/named-error.spec.ts +0 -36
  115. package/src/utils/number-utils.spec.ts +0 -212
  116. package/src/utils/otlp-any-value.spec.ts +0 -376
  117. package/src/utils/otlp-resource.spec.ts +0 -171
  118. package/src/utils/promise-queue.spec.ts +0 -55
  119. package/src/utils/string-utils.spec.ts +0 -158
  120. package/src/utils/type-utils.spec.ts +0 -38
  121. package/src/utils/webview-app-utils.spec.ts +0 -118
  122. package/src/vendor/uuidv7.spec.ts +0 -26
@@ -1,271 +0,0 @@
1
- import { createMockLogger } from '@/testing'
2
- import { resolveTracesConfig } from './config'
3
-
4
- describe('resolveTracesConfig', () => {
5
- it.each([
6
- ['zero', 0],
7
- ['negative', -1],
8
- ['not a number', NaN],
9
- // Floored, these read as 1, 2 and 3 — caps an order of magnitude below what
10
- // the caller wrote, applied silently.
11
- ['a fraction', 1.5],
12
- ['a large fraction', 200.5],
13
- ['infinity', Infinity],
14
- ])('falls back to the default per-span caps when given %s', (_label, value) => {
15
- const resolved = resolveTracesConfig({
16
- maxAttributesPerSpan: value,
17
- maxEventsPerSpan: value,
18
- maxAttributeValueLength: value,
19
- })
20
- expect(resolved.maxAttributesPerSpan).toBe(128)
21
- expect(resolved.maxEventsPerSpan).toBe(128)
22
- expect(resolved.maxAttributeValueLength).toBe(8192)
23
- })
24
-
25
- it('honours explicit per-span caps', () => {
26
- // Without this the resolver can ignore maxEventsPerSpan entirely and every
27
- // other test still passes, because they all assert the default.
28
- expect(resolveTracesConfig({ maxAttributesPerSpan: 5, maxEventsPerSpan: 7 })).toMatchObject({
29
- maxAttributesPerSpan: 5,
30
- maxEventsPerSpan: 7,
31
- })
32
- })
33
-
34
- it('keeps the per-event attribute cap fixed', () => {
35
- // The cap is internal, so an untyped caller naming it gets the default.
36
- expect(resolveTracesConfig({ maxAttributesPerEvent: 9 } as any).maxAttributesPerEvent).toBe(128)
37
- })
38
-
39
- it('applies the documented defaults', () => {
40
- expect(resolveTracesConfig(undefined)).toMatchObject({
41
- flushIntervalMs: 5000,
42
- maxExportBatchSize: 512,
43
- maxQueueSize: 2048,
44
- maxAttributesPerSpan: 128,
45
- maxEventsPerSpan: 128,
46
- maxAttributesPerEvent: 128,
47
- maxAttributeValueLength: 8192,
48
- maxLiveSpans: 10_000,
49
- maxSpanAgeMs: 3_600_000,
50
- })
51
- })
52
-
53
- it('honours an explicit attribute value bound', () => {
54
- expect(resolveTracesConfig({ maxAttributeValueLength: 256 }).maxAttributeValueLength).toBe(256)
55
- })
56
-
57
- it('honours explicit live-span bounds', () => {
58
- expect(resolveTracesConfig({ maxLiveSpans: 50, maxSpanAgeMs: 30_000 })).toMatchObject({
59
- maxLiveSpans: 50,
60
- maxSpanAgeMs: 30_000,
61
- })
62
- })
63
-
64
- it.each([0, -1, Number.NaN])('falls back to the defaults for unusable live-span bounds (%p)', (value) => {
65
- expect(resolveTracesConfig({ maxLiveSpans: value, maxSpanAgeMs: value })).toMatchObject({
66
- maxLiveSpans: 10_000,
67
- maxSpanAgeMs: 3_600_000,
68
- })
69
- })
70
-
71
- it('leaves serviceName unset so core supplies unknown_service', () => {
72
- expect(resolveTracesConfig({}).serviceName).toBeUndefined()
73
- })
74
-
75
- it('honours explicit values', () => {
76
- expect(
77
- resolveTracesConfig({ serviceName: 'checkout', flushIntervalMs: 1000, maxExportBatchSize: 50 })
78
- ).toMatchObject({
79
- serviceName: 'checkout',
80
- flushIntervalMs: 1000,
81
- maxExportBatchSize: 50,
82
- })
83
- })
84
-
85
- it('lets OTLP resource attributes override the named fields', () => {
86
- const resolved = resolveTracesConfig({
87
- serviceName: 'named',
88
- serviceVersion: '1.0.0',
89
- environment: 'staging',
90
- resourceAttributes: {
91
- 'service.name': 'from-attributes',
92
- 'service.version': '2.0.0',
93
- 'deployment.environment': 'production',
94
- },
95
- })
96
-
97
- expect(resolved.serviceName).toBe('from-attributes')
98
- expect(resolved.serviceVersion).toBe('2.0.0')
99
- expect(resolved.environment).toBe('production')
100
- })
101
-
102
- it.each([0, -1, 0.5, Number.NaN, Number.POSITIVE_INFINITY])(
103
- 'falls back to the default for an unusable maxExportBatchSize (%p)',
104
- (value) => {
105
- // A non-positive batch size reaches an export loop that cannot make
106
- // progress with it, so it spins forever posting empty batches.
107
- expect(resolveTracesConfig({ maxExportBatchSize: value }).maxExportBatchSize).toBe(512)
108
- }
109
- )
110
-
111
- it('takes the default for a fractional batch size rather than flooring it', () => {
112
- // Every numeric knob resolves the same way, so a fraction is a value the
113
- // caller did not mean rather than one to round down behind their back.
114
- expect(resolveTracesConfig({ maxExportBatchSize: 10.9 }).maxExportBatchSize).toBe(512)
115
- })
116
-
117
- it.each([0, -1, 1.5, Number.NaN])('falls back to the default for an unusable flushIntervalMs (%p)', (value) => {
118
- expect(resolveTracesConfig({ flushIntervalMs: value }).flushIntervalMs).toBe(5000)
119
- })
120
-
121
- it.each([0.5, 10_000.5])('falls back to the defaults for fractional live-span bounds (%p)', (value) => {
122
- expect(resolveTracesConfig({ maxLiveSpans: value, maxSpanAgeMs: value })).toMatchObject({
123
- maxLiveSpans: 10_000,
124
- maxSpanAgeMs: 3_600_000,
125
- })
126
- })
127
-
128
- it('keeps the queue at least as large as the export batch', () => {
129
- // A queue smaller than the flush trigger would stop the depth-based flush
130
- // from ever firing.
131
- expect(resolveTracesConfig({ maxExportBatchSize: 5000 }).maxQueueSize).toBe(5000)
132
- expect(resolveTracesConfig({ maxExportBatchSize: 10 }).maxQueueSize).toBe(2048)
133
- })
134
-
135
- it('honours an explicit maxQueueSize', () => {
136
- expect(resolveTracesConfig({ maxQueueSize: 100_000 }).maxQueueSize).toBe(100_000)
137
- })
138
-
139
- it('floors an explicit maxQueueSize at the export batch size', () => {
140
- expect(resolveTracesConfig({ maxExportBatchSize: 512, maxQueueSize: 10 }).maxQueueSize).toBe(512)
141
- })
142
-
143
- it('attaches the host resource attributes the entrypoint supplies', () => {
144
- expect(
145
- resolveTracesConfig(undefined, { 'os.name': 'linux', 'os.version': '6.1.0-27-amd64' }).resourceAttributes
146
- ).toEqual({ 'os.name': 'linux', 'os.version': '6.1.0-27-amd64' })
147
- })
148
-
149
- it('lets user resource attributes override the host ones', () => {
150
- expect(
151
- resolveTracesConfig(
152
- { resourceAttributes: { 'os.name': 'my-os', 'os.version': '1.2.3' } },
153
- { 'os.name': 'linux', 'os.version': '6.1.0-27-amd64' }
154
- ).resourceAttributes
155
- ).toEqual({ 'os.name': 'my-os', 'os.version': '1.2.3' })
156
- })
157
-
158
- it('resolves when the entrypoint supplies no host attributes', () => {
159
- expect(resolveTracesConfig({ resourceAttributes: { 'host.name': 'worker-01' } }).resourceAttributes).toEqual({
160
- 'host.name': 'worker-01',
161
- })
162
- })
163
- })
164
-
165
- describe('resourceAttributes guarding', () => {
166
- it('ignores a non-object value', () => {
167
- const { resourceAttributes } = resolveTracesConfig({ resourceAttributes: 'oops' as never })
168
-
169
- expect(Object.keys(resourceAttributes ?? {})).toEqual([])
170
- })
171
-
172
- it('ignores an array, which would otherwise spread as numeric keys', () => {
173
- const { resourceAttributes } = resolveTracesConfig({
174
- resourceAttributes: [{ key: 'service.name' }] as never,
175
- })
176
-
177
- expect(Object.keys(resourceAttributes ?? {})).not.toContain('0')
178
- })
179
-
180
- it('drops an identity key that is not a string', () => {
181
- const resolved = resolveTracesConfig({
182
- serviceName: 'checkout-api',
183
- resourceAttributes: { 'service.name': 12345 as never, region: 'us' },
184
- })
185
-
186
- expect(resolved.serviceName).toBe('checkout-api')
187
- expect(resolved.resourceAttributes).toEqual({ region: 'us' })
188
- })
189
-
190
- it('ignores a beforeSpanSend entry that is not a function', () => {
191
- // A plain-JS caller passing the wrong shape would otherwise have every span
192
- // dropped by a hook that throws on call, with tracing silently off.
193
- const scrub = (span: any): any => span
194
- const resolved = resolveTracesConfig({ beforeSpanSend: ['not a function', scrub] as never })
195
-
196
- expect(resolved.beforeSpanSend).toEqual([scrub])
197
- })
198
-
199
- it('warns about a dropped hook, since the redaction it was configured for is gone', () => {
200
- const logger = createMockLogger()
201
- const scrub = (span: any): any => span
202
-
203
- resolveTracesConfig({ beforeSpanSend: ['not a function', scrub] as never }, undefined, logger)
204
-
205
- // `critical`, not `warn`: every other level is gated behind `debug: true`.
206
- expect(logger.critical).toHaveBeenCalledWith(expect.stringContaining('ignoring 1 of 2 entries'))
207
- })
208
-
209
- it('stays quiet for a conditionally disabled hook', () => {
210
- // `[featureEnabled && scrub]` is ordinary JS; nothing was configured to
211
- // redact, so claiming redaction is broken would be false alarm.
212
- const logger = createMockLogger()
213
-
214
- const resolved = resolveTracesConfig({ beforeSpanSend: [false, null, undefined] as never }, undefined, logger)
215
-
216
- expect(resolved.beforeSpanSend).toEqual([])
217
- expect(logger.critical).not.toHaveBeenCalled()
218
- })
219
-
220
- it('stays quiet when every hook is callable', () => {
221
- const logger = createMockLogger()
222
-
223
- resolveTracesConfig({ beforeSpanSend: [(span: any): any => span] }, undefined, logger)
224
-
225
- expect(logger.critical).not.toHaveBeenCalled()
226
- })
227
-
228
- it('resolves to no hooks when beforeSpanSend is the wrong type entirely', () => {
229
- const resolved = resolveTracesConfig({ beforeSpanSend: { scrub: true } as never })
230
-
231
- expect(resolved.beforeSpanSend).toEqual([])
232
- })
233
-
234
- it('does not throw when an identity accessor throws', () => {
235
- const hostile = {}
236
- Object.defineProperty(hostile, 'service.name', {
237
- enumerable: true,
238
- get() {
239
- throw new Error('config getter exploded')
240
- },
241
- })
242
-
243
- expect(() => resolveTracesConfig({ resourceAttributes: hostile as never })).not.toThrow()
244
- })
245
-
246
- it('does not throw when a non-identity accessor throws', () => {
247
- const hostile = { 'service.name': 'checkout-api' }
248
- Object.defineProperty(hostile, 'region', {
249
- enumerable: true,
250
- get() {
251
- throw new Error('config getter exploded')
252
- },
253
- })
254
-
255
- expect(() => resolveTracesConfig({ resourceAttributes: hostile as never }, { 'os.name': 'Linux' })).not.toThrow()
256
- })
257
-
258
- it('keeps the readable attributes when one accessor throws', () => {
259
- const hostile = { region: 'us' }
260
- Object.defineProperty(hostile, 'tenant', {
261
- enumerable: true,
262
- get() {
263
- throw new Error('config getter exploded')
264
- },
265
- })
266
-
267
- const resolved = resolveTracesConfig({ resourceAttributes: hostile as never }, { 'os.name': 'Linux' })
268
-
269
- expect(resolved.resourceAttributes).toMatchObject({ 'os.name': 'Linux', region: 'us' })
270
- })
271
- })
@@ -1,115 +0,0 @@
1
- import { getRandomBytes, isValidSpanId, isValidTraceId, newSpanId, newTraceId } from './ids'
2
-
3
- describe('trace and span ids', () => {
4
- describe('newTraceId', () => {
5
- it('is 32 lowercase hex characters', () => {
6
- for (let i = 0; i < 50; i++) {
7
- expect(newTraceId()).toMatch(/^[0-9a-f]{32}$/)
8
- }
9
- })
10
-
11
- it('is never all zeros', () => {
12
- for (let i = 0; i < 50; i++) {
13
- expect(newTraceId()).not.toBe('0'.repeat(32))
14
- }
15
- })
16
-
17
- it('does not repeat', () => {
18
- const ids = new Set(Array.from({ length: 200 }, newTraceId))
19
- expect(ids.size).toBe(200)
20
- })
21
- })
22
-
23
- describe('newSpanId', () => {
24
- it('is 16 lowercase hex characters', () => {
25
- for (let i = 0; i < 50; i++) {
26
- expect(newSpanId()).toMatch(/^[0-9a-f]{16}$/)
27
- }
28
- })
29
-
30
- it('does not repeat', () => {
31
- const ids = new Set(Array.from({ length: 200 }, newSpanId))
32
- expect(ids.size).toBe(200)
33
- })
34
- })
35
-
36
- describe('getRandomBytes', () => {
37
- it('returns the requested length', () => {
38
- expect(getRandomBytes(8)).toHaveLength(8)
39
- expect(getRandomBytes(16)).toHaveLength(16)
40
- })
41
-
42
- it('falls back to Math.random when crypto is unavailable', () => {
43
- const original = Object.getOwnPropertyDescriptor(globalThis, 'crypto')
44
- // React Native has no global crypto without a polyfill — the fallback path
45
- // is what keeps span ids working there.
46
- Object.defineProperty(globalThis, 'crypto', { value: undefined, configurable: true })
47
- try {
48
- expect(newTraceId()).toMatch(/^[0-9a-f]{32}$/)
49
- expect(newSpanId()).toMatch(/^[0-9a-f]{16}$/)
50
- } finally {
51
- if (original) {
52
- Object.defineProperty(globalThis, 'crypto', original)
53
- }
54
- }
55
- })
56
-
57
- it('falls back when getRandomValues throws', () => {
58
- const original = Object.getOwnPropertyDescriptor(globalThis, 'crypto')
59
- Object.defineProperty(globalThis, 'crypto', {
60
- value: {
61
- getRandomValues: () => {
62
- throw new Error('not allowed')
63
- },
64
- },
65
- configurable: true,
66
- })
67
- try {
68
- expect(newTraceId()).toMatch(/^[0-9a-f]{32}$/)
69
- } finally {
70
- if (original) {
71
- Object.defineProperty(globalThis, 'crypto', original)
72
- }
73
- }
74
- })
75
-
76
- it('never emits an all-zero id even when the random source is broken', () => {
77
- const original = Object.getOwnPropertyDescriptor(globalThis, 'crypto')
78
- Object.defineProperty(globalThis, 'crypto', {
79
- value: { getRandomValues: (array: Uint8Array) => array.fill(0) },
80
- configurable: true,
81
- })
82
- try {
83
- // The server zeroes ids it can't use, so an all-zero id would be stored
84
- // and silently orphaned rather than rejected.
85
- expect(newTraceId()).not.toBe('0'.repeat(32))
86
- expect(newSpanId()).not.toBe('0'.repeat(16))
87
- } finally {
88
- if (original) {
89
- Object.defineProperty(globalThis, 'crypto', original)
90
- }
91
- }
92
- })
93
- })
94
-
95
- describe('validation', () => {
96
- it.each([
97
- ['a valid trace id', '4bf92f3577b34da6a3ce929d0e0e4736', true],
98
- ['an all-zero trace id', '0'.repeat(32), false],
99
- ['a short trace id', 'abc', false],
100
- ['uppercase hex', '4BF92F3577B34DA6A3CE929D0E0E4736', false],
101
- ['a non-hex string', 'zzf92f3577b34da6a3ce929d0e0e4736', false],
102
- ['a non-string', 12345, false],
103
- ])('isValidTraceId rejects/accepts %s', (_name, value, expected) => {
104
- expect(isValidTraceId(value)).toBe(expected)
105
- })
106
-
107
- it.each([
108
- ['a valid span id', '00f067aa0ba902b7', true],
109
- ['an all-zero span id', '0'.repeat(16), false],
110
- ['a trace-length id', '4bf92f3577b34da6a3ce929d0e0e4736', false],
111
- ])('isValidSpanId rejects/accepts %s', (_name, value, expected) => {
112
- expect(isValidSpanId(value)).toBe(expected)
113
- })
114
- })
115
- })