@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,266 +0,0 @@
1
- import type { CaptureLogOptions, LogSeverityLevel } from '@posthog/types'
2
- import type { LogSdkContext } from './types'
3
- import { buildOtlpLogRecord, buildOtlpLogsPayload, getOtlpSeverityNumber, getOtlpSeverityText } from './logs-utils'
4
-
5
- const browserSdkContext: LogSdkContext = {
6
- distinctId: 'user-123',
7
- sessionId: 'session-456',
8
- windowId: 'window-789',
9
- sessionStartTimestamp: 1672567200000,
10
- lastActivityTimestamp: 1672569000000,
11
- currentUrl: 'https://example.com/page',
12
- activeFeatureFlags: ['flag-a', 'flag-b'],
13
- }
14
-
15
- const mobileSdkContext: LogSdkContext = {
16
- distinctId: 'user-123',
17
- sessionId: 'session-456',
18
- screenName: 'Home',
19
- appState: 'foreground',
20
- activeFeatureFlags: ['flag-a', 'flag-b'],
21
- }
22
-
23
- const minimalSdkContext: LogSdkContext = {}
24
-
25
- describe('logs-utils', () => {
26
- describe('getOtlpSeverityText', () => {
27
- it.each([
28
- ['trace', 'TRACE'],
29
- ['debug', 'DEBUG'],
30
- ['info', 'INFO'],
31
- ['warn', 'WARN'],
32
- ['error', 'ERROR'],
33
- ['fatal', 'FATAL'],
34
- ] as [LogSeverityLevel, string][])('maps %s to %s', (level, expected) => {
35
- expect(getOtlpSeverityText(level)).toBe(expected)
36
- })
37
-
38
- it('falls back to INFO for unknown levels', () => {
39
- expect(getOtlpSeverityText('bogus' as LogSeverityLevel)).toBe('INFO')
40
- })
41
- })
42
-
43
- describe('getOtlpSeverityNumber', () => {
44
- it.each([
45
- ['trace', 1],
46
- ['debug', 5],
47
- ['info', 9],
48
- ['warn', 13],
49
- ['error', 17],
50
- ['fatal', 21],
51
- ] as [LogSeverityLevel, number][])('maps %s to %d', (level, expected) => {
52
- expect(getOtlpSeverityNumber(level)).toBe(expected)
53
- })
54
-
55
- it('falls back to 9 (INFO) for unknown levels', () => {
56
- expect(getOtlpSeverityNumber('bogus' as LogSeverityLevel)).toBe(9)
57
- })
58
- })
59
-
60
- describe('buildOtlpLogRecord attribute reads', () => {
61
- // Reading `options.attributes` happens before the encoder's per-key guard.
62
- it('marks an attribute whose getter throws without dropping the record', () => {
63
- const attributes = {
64
- ok: 1,
65
- get bad(): number {
66
- throw new Error('disposed store')
67
- },
68
- }
69
- const record = buildOtlpLogRecord({ body: 'x', attributes }, {})
70
- expect(record.attributes).toContainEqual({ key: 'ok', value: { intValue: '1' } })
71
- expect(record.attributes).toContainEqual({ key: 'bad', value: { stringValue: '[Unserializable]' } })
72
- })
73
-
74
- // Plain assignment hits the prototype setter and the attribute vanishes.
75
- it('keeps an attribute literally named __proto__', () => {
76
- const attributes = JSON.parse('{"__proto__": {"a": 1}, "normal": "n"}')
77
- const record = buildOtlpLogRecord({ body: 'x', attributes }, {})
78
- expect(record.attributes.map((a) => a.key)).toContain('__proto__')
79
- })
80
-
81
- it('coerces a non-string body', () => {
82
- // A non-string stringValue is refused for the whole request.
83
- const record = buildOtlpLogRecord({ body: 12345 as unknown as string }, {})
84
- expect(record.body).toEqual({ stringValue: '12345' })
85
- })
86
-
87
- // `String()` throws on these, and buildOtlpLogRecord is called straight from
88
- // captureLog — beforeSend can also hand back any body.
89
- it('marks a body that cannot be coerced to a string', () => {
90
- const throwing = {
91
- toString() {
92
- throw new Error('boom')
93
- },
94
- } as unknown as string
95
- expect(() => buildOtlpLogRecord({ body: throwing }, {})).not.toThrow()
96
- expect(buildOtlpLogRecord({ body: throwing }, {}).body).toEqual({ stringValue: '[Unserializable]' })
97
- expect(buildOtlpLogRecord({ body: Object.create(null) }, {}).body).toEqual({
98
- stringValue: '[Unserializable]',
99
- })
100
- })
101
-
102
- it('keeps the record when the attributes object itself cannot be read', () => {
103
- const revocable = Proxy.revocable({ a: 1 }, {})
104
- revocable.revoke()
105
-
106
- expect(buildOtlpLogRecord({ body: 'x', attributes: revocable.proxy }, {}).body).toEqual({ stringValue: 'x' })
107
- })
108
- })
109
-
110
- describe('buildOtlpLogRecord', () => {
111
- it('builds a minimal log record', () => {
112
- const record = buildOtlpLogRecord({ body: 'hello world' }, minimalSdkContext)
113
-
114
- expect(record.body).toEqual({ stringValue: 'hello world' })
115
- expect(record.severityText).toBe('INFO')
116
- expect(record.severityNumber).toBe(9)
117
- expect(record.timeUnixNano).toBeDefined()
118
- expect(record.observedTimeUnixNano).toBeDefined()
119
- expect(record.observedTimeUnixNano).toBe(record.timeUnixNano)
120
- })
121
-
122
- it('stamps both timestamps from the event time when the record is built late', () => {
123
- const occurredAtMs = Date.now() - 5000
124
- const record = buildOtlpLogRecord({ body: 'buffered' }, minimalSdkContext, undefined, occurredAtMs)
125
-
126
- expect(record.timeUnixNano).toBe(String(occurredAtMs) + '000000')
127
- // The logs spec requires the client to keep the two equal.
128
- expect(record.observedTimeUnixNano).toBe(record.timeUnixNano)
129
- })
130
-
131
- it.each([undefined, NaN, 'nope' as unknown as number])(
132
- 'falls back to now when the event time is %p',
133
- (occurredAtMs) => {
134
- const before = Date.now()
135
- const record = buildOtlpLogRecord({ body: 'x' }, minimalSdkContext, undefined, occurredAtMs)
136
- // Divide and the 19-digit nanosecond value loses its last digits to float.
137
- const stampedMs = Number(record.timeUnixNano.slice(0, -6))
138
-
139
- expect(record.timeUnixNano).toMatch(/^\d+000000$/)
140
- expect(stampedMs).toBeGreaterThanOrEqual(before)
141
- expect(stampedMs).toBeLessThanOrEqual(Date.now())
142
- expect(record.observedTimeUnixNano).toBe(record.timeUnixNano)
143
- }
144
- )
145
-
146
- it('maps severity levels correctly', () => {
147
- const record = buildOtlpLogRecord({ body: 'test', level: 'error' }, minimalSdkContext)
148
- expect(record.severityText).toBe('ERROR')
149
- expect(record.severityNumber).toBe(17)
150
- })
151
-
152
- it('falls back to INFO for unknown severity', () => {
153
- const record = buildOtlpLogRecord({ body: 'test', level: 'bogus' as LogSeverityLevel }, minimalSdkContext)
154
- expect(record.severityText).toBe('INFO')
155
- expect(record.severityNumber).toBe(9)
156
- })
157
-
158
- it('auto-populates browser SDK context (currentUrl → url.full)', () => {
159
- const record = buildOtlpLogRecord({ body: 'test' }, browserSdkContext)
160
- const attrs = Object.fromEntries(record.attributes.map((a) => [a.key, a.value]))
161
- expect(attrs['posthogDistinctId']).toEqual({ stringValue: 'user-123' })
162
- expect(attrs['sessionId']).toEqual({ stringValue: 'session-456' })
163
- expect(attrs['window.id']).toEqual({ stringValue: 'window-789' })
164
- expect(attrs['sessionStartTimestamp']).toEqual({ stringValue: '1672567200000' })
165
- expect(attrs['lastActivityTimestamp']).toEqual({ stringValue: '1672569000000' })
166
- expect(attrs['url.full']).toEqual({ stringValue: 'https://example.com/page' })
167
- expect(attrs['feature_flags']).toEqual({
168
- arrayValue: { values: [{ stringValue: 'flag-a' }, { stringValue: 'flag-b' }] },
169
- })
170
- // browser context shouldn't leak mobile-only attrs
171
- expect(attrs['screen.name']).toBeUndefined()
172
- expect(attrs['app.state']).toBeUndefined()
173
- })
174
-
175
- it.each(['window.id', 'sessionStartTimestamp', 'lastActivityTimestamp'])(
176
- 'omits %s when absent from the SDK context',
177
- (attribute) => {
178
- const record = buildOtlpLogRecord({ body: 'test' }, minimalSdkContext)
179
- expect(record.attributes.map((a) => a.key)).not.toContain(attribute)
180
- }
181
- )
182
-
183
- it('preserves a sessionStartTimestamp of 0 (epoch)', () => {
184
- const record = buildOtlpLogRecord({ body: 'test' }, { ...minimalSdkContext, sessionStartTimestamp: 0 })
185
- const attrs = Object.fromEntries(record.attributes.map((a) => [a.key, a.value]))
186
- expect(attrs['sessionStartTimestamp']).toEqual({ stringValue: '0' })
187
- })
188
-
189
- it('auto-populates mobile SDK context (screenName + appState)', () => {
190
- const record = buildOtlpLogRecord({ body: 'test' }, mobileSdkContext)
191
- const attrs = Object.fromEntries(record.attributes.map((a) => [a.key, a.value]))
192
- expect(attrs['screen.name']).toEqual({ stringValue: 'Home' })
193
- expect(attrs['app.state']).toEqual({ stringValue: 'foreground' })
194
- // mobile context shouldn't leak browser-only attrs
195
- expect(attrs['url.full']).toBeUndefined()
196
- })
197
-
198
- it('user attributes override auto-populated ones', () => {
199
- const record = buildOtlpLogRecord(
200
- { body: 'test', attributes: { posthogDistinctId: 'custom-id' } },
201
- browserSdkContext
202
- )
203
- const attrs = Object.fromEntries(record.attributes.map((a) => [a.key, a.value]))
204
- expect(attrs['posthogDistinctId']).toEqual({ stringValue: 'custom-id' })
205
- })
206
-
207
- it('includes trace context when provided', () => {
208
- const options: CaptureLogOptions = {
209
- body: 'test',
210
- trace_id: '4bf92f3577b34da6a3ce929d0e0e4736',
211
- span_id: '00f067aa0ba902b7',
212
- trace_flags: 1,
213
- }
214
- const record = buildOtlpLogRecord(options, minimalSdkContext)
215
- expect(record.traceId).toBe('4bf92f3577b34da6a3ce929d0e0e4736')
216
- expect(record.spanId).toBe('00f067aa0ba902b7')
217
- expect(record.flags).toBe(1)
218
- })
219
-
220
- it('omits trace context when not provided', () => {
221
- const record = buildOtlpLogRecord({ body: 'test' }, minimalSdkContext)
222
- expect(record.traceId).toBeUndefined()
223
- expect(record.spanId).toBeUndefined()
224
- expect(record.flags).toBeUndefined()
225
- })
226
-
227
- it('preserves trace_flags === 0', () => {
228
- // Easy to lose with a falsy check; trace_flags is a numeric bitfield
229
- // where 0 is a valid value (means "not sampled").
230
- const record = buildOtlpLogRecord({ body: 'test', trace_flags: 0 }, minimalSdkContext)
231
- expect(record.flags).toBe(0)
232
- })
233
-
234
- it('does not include feature_flags when the array is empty', () => {
235
- const record = buildOtlpLogRecord({ body: 'test' }, { ...minimalSdkContext, activeFeatureFlags: [] })
236
- expect(record.attributes.map((a) => a.key)).not.toContain('feature_flags')
237
- })
238
- })
239
-
240
- describe('buildOtlpLogsPayload', () => {
241
- it('wraps log records in the OTLP envelope with scope name and version', () => {
242
- const record = buildOtlpLogRecord({ body: 'test' }, minimalSdkContext)
243
- const payload = buildOtlpLogsPayload([record], { 'service.name': 'my-app' }, 'posthog-js', '1.371.0')
244
-
245
- expect(payload.resourceLogs).toHaveLength(1)
246
- expect(payload.resourceLogs[0].resource.attributes).toEqual([
247
- { key: 'service.name', value: { stringValue: 'my-app' } },
248
- ])
249
- expect(payload.resourceLogs[0].scopeLogs).toHaveLength(1)
250
- expect(payload.resourceLogs[0].scopeLogs[0].scope).toEqual({
251
- name: 'posthog-js',
252
- version: '1.371.0',
253
- })
254
- expect(payload.resourceLogs[0].scopeLogs[0].logRecords).toHaveLength(1)
255
- })
256
-
257
- it('includes multiple log records', () => {
258
- const records = [
259
- buildOtlpLogRecord({ body: 'log 1' }, minimalSdkContext),
260
- buildOtlpLogRecord({ body: 'log 2', level: 'error' }, minimalSdkContext),
261
- ]
262
- const payload = buildOtlpLogsPayload(records, { 'service.name': 'x' }, 'lib', '1.0.0')
263
- expect(payload.resourceLogs[0].scopeLogs[0].logRecords).toHaveLength(2)
264
- })
265
- })
266
- })