posthog-node 5.73.1 → 6.0.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/MIGRATION.md +556 -0
- package/README.md +3 -1
- package/dist/ai-capture/routing.d.ts +0 -3
- package/dist/ai-capture/routing.d.ts.map +1 -1
- package/dist/ai-capture/routing.js +2 -14
- package/dist/ai-capture/routing.mjs +1 -4
- package/dist/capture-lane/lane.d.ts +109 -0
- package/dist/capture-lane/lane.d.ts.map +1 -0
- package/dist/capture-lane/lane.js +308 -0
- package/dist/capture-lane/lane.mjs +270 -0
- package/dist/capture-lane/limits.d.ts +7 -0
- package/dist/capture-lane/limits.d.ts.map +1 -0
- package/dist/{capture-v1/config.js → capture-lane/limits.js} +13 -11
- package/dist/capture-lane/limits.mjs +4 -0
- package/dist/capture-lane/settings.d.ts +30 -0
- package/dist/capture-lane/settings.d.ts.map +1 -0
- package/dist/capture-lane/settings.js +100 -0
- package/dist/capture-lane/settings.mjs +47 -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 +23 -7
- package/dist/capture-v1/errors.d.ts.map +1 -1
- package/dist/capture-v1/errors.js +37 -8
- package/dist/capture-v1/errors.mjs +29 -6
- package/dist/capture-v1/sender.d.ts +63 -23
- package/dist/capture-v1/sender.d.ts.map +1 -1
- package/dist/capture-v1/sender.js +281 -74
- package/dist/capture-v1/sender.mjs +275 -77
- package/dist/capture-v1/serialize.d.ts +28 -0
- package/dist/capture-v1/serialize.d.ts.map +1 -0
- package/dist/capture-v1/serialize.js +82 -0
- package/dist/capture-v1/serialize.mjs +38 -0
- package/dist/capture-v1/transform.d.ts +10 -25
- package/dist/capture-v1/transform.d.ts.map +1 -1
- package/dist/capture-v1/transform.js +33 -64
- package/dist/capture-v1/transform.mjs +28 -53
- package/dist/capture-v1/types.d.ts +16 -14
- package/dist/capture-v1/types.d.ts.map +1 -1
- package/dist/capture-v1/types.js +27 -1
- package/dist/capture-v1/types.mjs +3 -1
- 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/client.d.ts +201 -94
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +485 -246
- package/dist/client.mjs +485 -246
- package/dist/compression.node.d.ts +13 -0
- package/dist/compression.node.d.ts.map +1 -0
- package/dist/{capture-v1/routing.js → compression.node.js} +23 -16
- package/dist/compression.node.mjs +18 -0
- package/dist/entrypoints/index.node.d.ts +2 -0
- package/dist/entrypoints/index.node.d.ts.map +1 -1
- package/dist/entrypoints/index.node.js +11 -4
- package/dist/entrypoints/index.node.mjs +4 -0
- package/dist/event-defaults.d.ts +46 -0
- package/dist/event-defaults.d.ts.map +1 -0
- package/dist/event-defaults.js +141 -0
- package/dist/event-defaults.mjs +79 -0
- package/dist/exports.d.ts +3 -0
- package/dist/exports.d.ts.map +1 -1
- package/dist/exports.js +22 -11
- package/dist/exports.mjs +1 -0
- package/dist/extensions/context/context.d.ts +0 -1
- package/dist/extensions/context/context.d.ts.map +1 -1
- package/dist/extensions/context/context.js +4 -3
- package/dist/extensions/context/context.mjs +4 -3
- package/dist/extensions/context/event-context.d.ts.map +1 -1
- package/dist/extensions/context/event-context.js +3 -0
- package/dist/extensions/context/event-context.mjs +3 -0
- package/dist/extensions/context/types.d.ts +9 -1
- package/dist/extensions/context/types.d.ts.map +1 -1
- package/dist/extensions/error-tracking/index.d.ts +1 -1
- package/dist/extensions/error-tracking/index.d.ts.map +1 -1
- package/dist/extensions/error-tracking/index.js +71 -7
- package/dist/extensions/error-tracking/index.mjs +75 -11
- package/dist/extensions/express.d.ts +8 -0
- package/dist/extensions/express.d.ts.map +1 -1
- package/dist/extensions/express.js +3 -3
- package/dist/extensions/express.mjs +4 -4
- package/dist/extensions/fetch-request.d.ts.map +1 -1
- package/dist/extensions/fetch-request.js +2 -2
- package/dist/extensions/fetch-request.mjs +3 -3
- package/dist/extensions/nestjs.d.ts.map +1 -1
- package/dist/extensions/nestjs.js +7 -4
- package/dist/extensions/nestjs.mjs +8 -5
- package/dist/extensions/tracing-headers.d.ts +7 -0
- package/dist/extensions/tracing-headers.d.ts.map +1 -1
- package/dist/extensions/tracing-headers.js +11 -1
- package/dist/extensions/tracing-headers.mjs +9 -2
- package/dist/feature-flag-evaluations.d.ts +5 -5
- package/dist/feature-flag-evaluations.d.ts.map +1 -1
- package/dist/feature-flag-evaluations.js +16 -9
- package/dist/feature-flag-evaluations.mjs +16 -9
- package/dist/flag-events.d.ts +1 -1
- package/dist/flag-events.d.ts.map +1 -1
- package/dist/flag-events.js +2 -2
- package/dist/flag-events.mjs +2 -2
- package/dist/flags-response.d.ts +2 -0
- package/dist/flags-response.d.ts.map +1 -1
- package/dist/flags-response.js +6 -0
- package/dist/flags-response.mjs +4 -1
- package/dist/types.d.ts +192 -27
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/dist/types.mjs +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +5 -4
- package/src/ai-capture/routing.ts +0 -6
- package/src/capture-lane/lane.ts +414 -0
- package/src/capture-lane/limits.ts +8 -0
- package/src/capture-lane/settings.ts +119 -0
- package/src/capture-v1/compression.ts +50 -0
- package/src/capture-v1/errors.ts +50 -11
- package/src/capture-v1/sender.ts +405 -111
- package/src/capture-v1/serialize.ts +77 -0
- package/src/capture-v1/transform.ts +59 -92
- package/src/capture-v1/types.ts +19 -15
- package/src/capture-v1/uuid.ts +53 -0
- package/src/client.ts +885 -446
- package/src/compression.node.ts +30 -0
- package/src/entrypoints/index.node.ts +6 -0
- package/src/event-defaults.ts +177 -0
- package/src/exports.ts +3 -0
- package/src/extensions/context/context.ts +4 -4
- package/src/extensions/context/event-context.ts +3 -1
- package/src/extensions/context/types.ts +10 -1
- package/src/extensions/error-tracking/index.ts +51 -7
- package/src/extensions/express.ts +12 -3
- package/src/extensions/fetch-request.ts +7 -3
- package/src/extensions/nestjs.ts +9 -5
- package/src/extensions/tracing-headers.ts +26 -7
- package/src/feature-flag-evaluations.ts +31 -23
- package/src/flag-events.ts +5 -2
- package/src/flags-response.ts +6 -1
- package/src/types.ts +212 -44
- package/src/version.ts +1 -1
- package/dist/ai-capture/batching.d.ts +0 -11
- package/dist/ai-capture/batching.d.ts.map +0 -1
- package/dist/ai-capture/batching.js +0 -77
- package/dist/ai-capture/batching.mjs +0 -36
- package/dist/capture-v1/config.d.ts +0 -11
- package/dist/capture-v1/config.d.ts.map +0 -1
- package/dist/capture-v1/config.mjs +0 -8
- package/dist/capture-v1/routing.d.ts +0 -21
- package/dist/capture-v1/routing.d.ts.map +0 -1
- package/dist/capture-v1/routing.mjs +0 -7
- package/src/ai-capture/batching.ts +0 -47
- package/src/capture-v1/config.ts +0 -18
- package/src/capture-v1/routing.ts +0 -28
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { type PostHogEventProperties, safeJsonStringify } from '@posthog/core'
|
|
2
|
+
|
|
3
|
+
import { buildV1Event } from './transform'
|
|
4
|
+
|
|
5
|
+
const encoder = new TextEncoder()
|
|
6
|
+
|
|
7
|
+
// `buildV1Event` puts `properties` last, so the event JSON is the rest of the event with this key and the properties appended.
|
|
8
|
+
const PROPERTIES_KEY = ',"properties":'
|
|
9
|
+
const PROPERTIES_KEY_BYTES = encoder.encode(PROPERTIES_KEY).length
|
|
10
|
+
|
|
11
|
+
/** One wire event, serialized once and reused verbatim in every request body that carries it. */
|
|
12
|
+
export interface SerializedV1Event {
|
|
13
|
+
uuid: string
|
|
14
|
+
json: string
|
|
15
|
+
/** UTF-8 size of `json`. */
|
|
16
|
+
bytes: number
|
|
17
|
+
/** UTF-8 size of the serialized `properties` object alone, which is what the AI endpoint limits. */
|
|
18
|
+
propertiesBytes: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/
|
|
22
|
+
// JSON.stringify writes a lone surrogate as a `\udXXX` escape (a valid pair stays raw), which capture rejects for the whole
|
|
23
|
+
// request. The lookbehind skips an escaped backslash followed by literal "udXXX" text.
|
|
24
|
+
const LONE_SURROGATE_ESCAPE = /(?<!\\)((?:\\\\)*)\\u[dD][89a-fA-F][0-9a-fA-F]{2}/g
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Why capture would reject this event, or undefined when it can be sent. A name must be a non-empty string, the
|
|
28
|
+
* distinct id a string, and the name and ids well-formed Unicode: a broken id is never repaired, because two broken ids
|
|
29
|
+
* could collapse into one.
|
|
30
|
+
*/
|
|
31
|
+
export function wireProblem(message: PostHogEventProperties): string | undefined {
|
|
32
|
+
const { event, distinct_id: distinctId, properties } = message
|
|
33
|
+
if (typeof event !== 'string' || event === '') {
|
|
34
|
+
return 'event name is missing, empty or not a string'
|
|
35
|
+
}
|
|
36
|
+
// Caller ids are already strings, so only a beforeSend hook can leave a non-string here.
|
|
37
|
+
if (typeof distinctId !== 'string') {
|
|
38
|
+
return 'distinct id is missing or not a string'
|
|
39
|
+
}
|
|
40
|
+
const props = (properties ?? {}) as Record<string, unknown>
|
|
41
|
+
const ids = [event, distinctId, props.$session_id, props.$window_id]
|
|
42
|
+
if (ids.some((value) => typeof value === 'string' && LONE_SURROGATE.test(value))) {
|
|
43
|
+
return 'event name or id is not well-formed Unicode'
|
|
44
|
+
}
|
|
45
|
+
return undefined
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function serializeV1Event(message: PostHogEventProperties): SerializedV1Event {
|
|
49
|
+
const { properties, ...rest } = buildV1Event(message)
|
|
50
|
+
const restJson = safeJsonStringify(rest)
|
|
51
|
+
const rawPropertiesJson = safeJsonStringify(properties)
|
|
52
|
+
// Property strings are repaired to U+FFFD rather than dropping the event.
|
|
53
|
+
const propertiesJson =
|
|
54
|
+
rawPropertiesJson.includes('\\ud') || rawPropertiesJson.includes('\\uD')
|
|
55
|
+
? rawPropertiesJson.replace(LONE_SURROGATE_ESCAPE, '$1\\ufffd')
|
|
56
|
+
: rawPropertiesJson
|
|
57
|
+
const propertiesBytes = encoder.encode(propertiesJson).length
|
|
58
|
+
return {
|
|
59
|
+
uuid: rest.uuid,
|
|
60
|
+
json: `${restJson.slice(0, -1)}${PROPERTIES_KEY}${propertiesJson}}`,
|
|
61
|
+
// The closing brace moves from the end of `restJson` to the end of the event, so it is counted once.
|
|
62
|
+
bytes: encoder.encode(restJson).length + PROPERTIES_KEY_BYTES + propertiesBytes,
|
|
63
|
+
propertiesBytes,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The Capture V1 batch envelope, built from already-serialized events. Unlike v0 there is no
|
|
69
|
+
* `api_key` (Bearer auth) and no `sent_at`. `historical_migration` is omitted when false.
|
|
70
|
+
*/
|
|
71
|
+
export function buildV1Body(
|
|
72
|
+
events: readonly { json: string }[],
|
|
73
|
+
{ createdAt, historicalMigration }: { createdAt: string; historicalMigration: boolean }
|
|
74
|
+
): string {
|
|
75
|
+
const migration = historicalMigration ? ',"historical_migration":true' : ''
|
|
76
|
+
return `{"created_at":${JSON.stringify(createdAt)}${migration},"batch":[${events.map((event) => event.json).join(',')}]}`
|
|
77
|
+
}
|
|
@@ -1,76 +1,56 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { EVENT_OPTION_LEGACY_PROPERTIES, type JsonType, type PostHogEventProperties } from '@posthog/core'
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { V1Event } from './types'
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* Coerce a value to a boolean the way the capture server does, or return
|
|
7
|
-
* `undefined` if it cannot be coerced (the option is then omitted so the server
|
|
8
|
-
* falls back to its default). Accepts native booleans, the strings
|
|
9
|
-
* `"true"`/`"1"`/`"false"`/`"0"` (trimmed, case-insensitive), and any number
|
|
10
|
-
* (nonzero -> true).
|
|
11
|
-
*/
|
|
12
|
-
export function coerceBool(value: JsonType): boolean | undefined {
|
|
13
|
-
if (typeof value === 'boolean') {
|
|
14
|
-
return value
|
|
15
|
-
}
|
|
16
|
-
if (typeof value === 'number') {
|
|
17
|
-
return value !== 0
|
|
18
|
-
}
|
|
19
|
-
if (typeof value === 'string') {
|
|
20
|
-
const normalized = value.trim().toLowerCase()
|
|
21
|
-
if (normalized === 'true' || normalized === '1') {
|
|
22
|
-
return true
|
|
23
|
-
}
|
|
24
|
-
if (normalized === 'false' || normalized === '0') {
|
|
25
|
-
return false
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return undefined
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Accept only a native string, else return `undefined` (the option is then
|
|
33
|
-
* omitted). The backend's `product_tour_id` is `Option<String>`, so numbers,
|
|
34
|
-
* booleans, objects, and arrays are dropped rather than coerced — matching
|
|
35
|
-
* posthog-go, posthog-rs, and posthog-python exactly.
|
|
36
|
-
*/
|
|
37
|
-
export function coerceString(value: JsonType): string | undefined {
|
|
38
|
-
return typeof value === 'string' ? value : undefined
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Legacy sentinel property -> typed `options` field, with its coercion. */
|
|
42
|
-
const OPTION_SENTINELS: ReadonlyArray<{
|
|
43
|
-
property: string
|
|
44
|
-
optionKey: keyof V1EventOptions
|
|
45
|
-
coerce: (value: JsonType) => boolean | string | undefined
|
|
46
|
-
}> = [
|
|
47
|
-
{ property: '$cookieless_mode', optionKey: 'cookieless_mode', coerce: coerceBool },
|
|
48
|
-
{ property: '$ignore_sent_at', optionKey: 'disable_skew_correction', coerce: coerceBool },
|
|
49
|
-
{ property: '$process_person_profile', optionKey: 'process_person_profile', coerce: coerceBool },
|
|
50
|
-
{ property: '$product_tour_id', optionKey: 'product_tour_id', coerce: coerceString },
|
|
51
|
-
]
|
|
52
|
-
|
|
53
|
-
/** Sentinel property -> top-level string field (string-only, non-string dropped). */
|
|
5
|
+
/** Property -> top-level string field; any value that is not a string is dropped. */
|
|
54
6
|
const TOPLEVEL_SENTINELS: ReadonlyArray<{ property: string; field: 'session_id' | 'window_id' }> = [
|
|
55
7
|
{ property: '$session_id', field: 'session_id' },
|
|
56
8
|
{ property: '$window_id', field: 'window_id' },
|
|
57
9
|
]
|
|
58
10
|
|
|
59
|
-
function isRecord(value:
|
|
11
|
+
function isRecord(value: unknown): value is PostHogEventProperties {
|
|
60
12
|
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
61
13
|
}
|
|
62
14
|
|
|
15
|
+
function jsonTypeName(value: JsonType): string {
|
|
16
|
+
return Array.isArray(value) ? 'array' : typeof value
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A copy of the caller's per-event options, or undefined when there are none. Any value that
|
|
21
|
+
* is not an object is reported and ignored, so the event is still sent.
|
|
22
|
+
*/
|
|
23
|
+
export function readEventOptions(value: unknown): Record<string, JsonType> | undefined {
|
|
24
|
+
if (value === undefined || value === null) {
|
|
25
|
+
return undefined
|
|
26
|
+
}
|
|
27
|
+
if (isRecord(value)) {
|
|
28
|
+
return { ...value }
|
|
29
|
+
}
|
|
30
|
+
// console.error, not the client logger: the logger only prints in debug mode, and the event loses its options.
|
|
31
|
+
console.error(
|
|
32
|
+
`[PostHog] options must be an object, got ${jsonTypeName(value as JsonType)}; sending the event without its options`
|
|
33
|
+
)
|
|
34
|
+
return undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
63
37
|
/**
|
|
64
38
|
* Normalize an event timestamp to an RFC 3339 string. The queued message
|
|
65
39
|
* carries either an ISO string (from `currentISOTime()`) or a `Date` (when the
|
|
66
40
|
* caller passed one); numbers are treated as epoch milliseconds. Invalid values
|
|
67
41
|
* fall back to "now" so a bad timestamp never rejects the whole batch.
|
|
68
42
|
*/
|
|
43
|
+
function replaceUnparseableTimestamp(): string {
|
|
44
|
+
// console.warn, not the client logger: the logger only prints in debug mode, and the event's time changes. Never logs the value.
|
|
45
|
+
console.warn('[PostHog] replacing an unparseable event timestamp with the current time')
|
|
46
|
+
return new Date().toISOString()
|
|
47
|
+
}
|
|
48
|
+
|
|
69
49
|
function toRfc3339(timestamp: unknown): string {
|
|
70
50
|
if (typeof timestamp === 'string') {
|
|
71
51
|
const asDate = new Date(timestamp)
|
|
72
52
|
if (Number.isNaN(asDate.getTime())) {
|
|
73
|
-
return
|
|
53
|
+
return replaceUnparseableTimestamp()
|
|
74
54
|
}
|
|
75
55
|
|
|
76
56
|
const normalized = asDate.toISOString()
|
|
@@ -80,7 +60,7 @@ function toRfc3339(timestamp: unknown): string {
|
|
|
80
60
|
: normalized
|
|
81
61
|
}
|
|
82
62
|
if (timestamp instanceof Date) {
|
|
83
|
-
return Number.isNaN(timestamp.getTime()) ?
|
|
63
|
+
return Number.isNaN(timestamp.getTime()) ? replaceUnparseableTimestamp() : timestamp.toISOString()
|
|
84
64
|
}
|
|
85
65
|
const asDate = timestamp == null ? new Date() : new Date(timestamp as string | number)
|
|
86
66
|
return Number.isNaN(asDate.getTime()) ? new Date().toISOString() : asDate.toISOString()
|
|
@@ -101,42 +81,44 @@ function relocateInto(properties: PostHogEventProperties, key: string, value: Js
|
|
|
101
81
|
* Transform one already-normalized queued message into a Capture V1 wire event.
|
|
102
82
|
*
|
|
103
83
|
* The input message is never mutated (events may be retried or handed to
|
|
104
|
-
* callbacks):
|
|
105
|
-
*
|
|
106
|
-
* the
|
|
107
|
-
*
|
|
108
|
-
* `$set`/`$set_once` are relocated into `properties`.
|
|
84
|
+
* callbacks): fresh `options` and `properties` maps are built. Option values are
|
|
85
|
+
* sent unchanged; capture reads them. Each legacy option property is removed from
|
|
86
|
+
* `properties` and fills its option when the caller left that option missing or
|
|
87
|
+
* `null`. `$session_id`/`$window_id` move to the top-level `session_id`/`window_id`
|
|
88
|
+
* fields; top-level `$set`/`$set_once` are relocated into `properties`.
|
|
109
89
|
*/
|
|
110
90
|
export function buildV1Event(message: PostHogEventProperties): V1Event {
|
|
111
91
|
const sourceProperties = isRecord(message.properties) ? message.properties : {}
|
|
112
92
|
const properties: PostHogEventProperties = { ...sourceProperties }
|
|
113
93
|
|
|
114
|
-
const options
|
|
115
|
-
for (const
|
|
116
|
-
if (property in properties) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
94
|
+
const options = readEventOptions(message.options) ?? {}
|
|
95
|
+
for (const [optionKey, property] of Object.entries(EVENT_OPTION_LEGACY_PROPERTIES)) {
|
|
96
|
+
if (!(property in properties)) {
|
|
97
|
+
continue
|
|
98
|
+
}
|
|
99
|
+
const legacyValue = properties[property]
|
|
100
|
+
delete properties[property]
|
|
101
|
+
const option = options[optionKey]
|
|
102
|
+
if ((option === undefined || option === null) && legacyValue !== undefined) {
|
|
103
|
+
options[optionKey] = legacyValue
|
|
123
104
|
}
|
|
124
105
|
}
|
|
125
106
|
|
|
126
107
|
const topLevel: { session_id?: string; window_id?: string } = {}
|
|
127
108
|
for (const { property, field } of TOPLEVEL_SENTINELS) {
|
|
128
|
-
if (property in properties) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
109
|
+
if (!(property in properties)) {
|
|
110
|
+
continue
|
|
111
|
+
}
|
|
112
|
+
const value = properties[property]
|
|
113
|
+
delete properties[property]
|
|
114
|
+
if (typeof value === 'string') {
|
|
115
|
+
topLevel[field] = value
|
|
116
|
+
} else if (value !== undefined && value !== null) {
|
|
117
|
+
// console.warn, not the client logger: the logger only prints in debug mode, and the event loses this id.
|
|
118
|
+
console.warn(`[PostHog] dropping ${property}: a ${jsonTypeName(value)} value is not a string`)
|
|
134
119
|
}
|
|
135
120
|
}
|
|
136
121
|
|
|
137
|
-
delete properties.$lib
|
|
138
|
-
delete properties.$lib_version
|
|
139
|
-
|
|
140
122
|
relocateInto(properties, '$set', message.$set)
|
|
141
123
|
relocateInto(properties, '$set_once', message.$set_once)
|
|
142
124
|
|
|
@@ -150,18 +132,3 @@ export function buildV1Event(message: PostHogEventProperties): V1Event {
|
|
|
150
132
|
properties,
|
|
151
133
|
}
|
|
152
134
|
}
|
|
153
|
-
|
|
154
|
-
/** Build a Capture V1 batch envelope from already-normalized queued messages. */
|
|
155
|
-
export function buildV1Batch(
|
|
156
|
-
messages: PostHogEventProperties[],
|
|
157
|
-
{ createdAt, historicalMigration }: { createdAt: string; historicalMigration?: boolean }
|
|
158
|
-
): V1Batch {
|
|
159
|
-
const batch: V1Batch = {
|
|
160
|
-
created_at: createdAt,
|
|
161
|
-
batch: messages.map(buildV1Event),
|
|
162
|
-
}
|
|
163
|
-
if (historicalMigration) {
|
|
164
|
-
batch.historical_migration = true
|
|
165
|
-
}
|
|
166
|
-
return batch
|
|
167
|
-
}
|
package/src/capture-v1/types.ts
CHANGED
|
@@ -1,19 +1,31 @@
|
|
|
1
|
-
import type { PostHogEventProperties } from '@posthog/core'
|
|
1
|
+
import type { JsonType, PostHogEventProperties } from '@posthog/core'
|
|
2
|
+
|
|
3
|
+
/** Capture lane for analytics events. */
|
|
4
|
+
export const ANALYTICS_CAPTURE_ENDPOINT = '/i/v1/analytics/events'
|
|
5
|
+
|
|
6
|
+
/** Capture lane for events sent through `captureAi`. */
|
|
7
|
+
export const AI_CAPTURE_ENDPOINT = '/i/v1/ai/events'
|
|
8
|
+
|
|
9
|
+
export type CaptureEndpoint = typeof ANALYTICS_CAPTURE_ENDPOINT | typeof AI_CAPTURE_ENDPOINT
|
|
2
10
|
|
|
3
11
|
/**
|
|
4
|
-
*
|
|
5
|
-
* properties
|
|
12
|
+
* The `options` object on a Capture V1 event: the caller's options plus any legacy
|
|
13
|
+
* `$`-prefixed option properties lifted by the transform. Values are sent unchanged.
|
|
6
14
|
* Always serialized as an object (`{}` when empty), never `null`.
|
|
7
15
|
*/
|
|
8
16
|
export interface V1EventOptions {
|
|
17
|
+
/** Whether to process this event in cookieless mode. */
|
|
9
18
|
cookieless_mode?: boolean
|
|
19
|
+
/** Whether to disable server-side timestamp skew correction for this event. */
|
|
10
20
|
disable_skew_correction?: boolean
|
|
21
|
+
/** Whether to create or update a person profile for this event. */
|
|
11
22
|
process_person_profile?: boolean
|
|
12
|
-
|
|
23
|
+
/** Additional capture options, sent to capture v1 unchanged. */
|
|
24
|
+
[key: string]: JsonType | undefined
|
|
13
25
|
}
|
|
14
26
|
|
|
15
27
|
/**
|
|
16
|
-
* A single event in a Capture V1 batch
|
|
28
|
+
* A single event in a Capture V1 batch.
|
|
17
29
|
* `session_id`/`window_id` are promoted from the `$session_id`/`$window_id`
|
|
18
30
|
* sentinels; there is no top-level `api_key`.
|
|
19
31
|
*/
|
|
@@ -28,16 +40,6 @@ export interface V1Event {
|
|
|
28
40
|
properties: PostHogEventProperties
|
|
29
41
|
}
|
|
30
42
|
|
|
31
|
-
/**
|
|
32
|
-
* The Capture V1 batch envelope. Unlike v0 there is no `api_key` (Bearer auth)
|
|
33
|
-
* and no `sent_at`. `historical_migration` is omitted when false.
|
|
34
|
-
*/
|
|
35
|
-
export interface V1Batch {
|
|
36
|
-
created_at: string
|
|
37
|
-
historical_migration?: boolean
|
|
38
|
-
batch: V1Event[]
|
|
39
|
-
}
|
|
40
|
-
|
|
41
43
|
/**
|
|
42
44
|
* Per-event result codes in a v1 2xx response body. Unknown strings are
|
|
43
45
|
* tolerated as terminal success for forward compatibility.
|
|
@@ -47,6 +49,8 @@ export type V1ResultCode = 'ok' | 'warning' | 'drop' | 'retry'
|
|
|
47
49
|
export interface V1EventResult {
|
|
48
50
|
result: V1ResultCode | string
|
|
49
51
|
details?: string | null
|
|
52
|
+
/** How many later copies of this uuid in the request the server dropped as duplicates. */
|
|
53
|
+
duplicates_dropped?: number
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
/** Shape of a v1 2xx response body: a per-uuid map of results. */
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { V7Generator } from '@posthog/core'
|
|
2
|
+
|
|
3
|
+
const HYPHENATED = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
4
|
+
const HEX_32 = /^[0-9a-f]{32}$/i
|
|
5
|
+
const URN_PREFIX = 'urn:uuid:'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The lowercase, hyphenated form of an RFC 4122 uuid string, or undefined for anything else.
|
|
9
|
+
* Accepts 32 hex digits, the hyphenated form, the hyphenated form in braces, and the
|
|
10
|
+
* `urn:uuid:` form, in any case.
|
|
11
|
+
*/
|
|
12
|
+
export function canonicalizeUuid(value: string): string | undefined {
|
|
13
|
+
let hyphenated = value
|
|
14
|
+
if (value.length === 38 && value.startsWith('{') && value.endsWith('}')) {
|
|
15
|
+
hyphenated = value.slice(1, -1)
|
|
16
|
+
} else if (value.length === 45 && value.slice(0, URN_PREFIX.length).toLowerCase() === URN_PREFIX) {
|
|
17
|
+
hyphenated = value.slice(URN_PREFIX.length)
|
|
18
|
+
} else if (HEX_32.test(value)) {
|
|
19
|
+
hyphenated = `${value.slice(0, 8)}-${value.slice(8, 12)}-${value.slice(12, 16)}-${value.slice(16, 20)}-${value.slice(20)}`
|
|
20
|
+
}
|
|
21
|
+
return HYPHENATED.test(hyphenated) ? hyphenated.toLowerCase() : undefined
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type CryptoLike = { getRandomValues(array: Uint32Array): Uint32Array }
|
|
25
|
+
|
|
26
|
+
/** 32-bit words from the platform's cryptographic source, filled eight at a time to keep the per-uuid cost low. */
|
|
27
|
+
class CryptoRandom {
|
|
28
|
+
private readonly words = new Uint32Array(8)
|
|
29
|
+
private cursor = this.words.length
|
|
30
|
+
|
|
31
|
+
constructor(private readonly crypto: CryptoLike) {}
|
|
32
|
+
|
|
33
|
+
nextUint32(): number {
|
|
34
|
+
if (this.cursor >= this.words.length) {
|
|
35
|
+
this.crypto.getRandomValues(this.words)
|
|
36
|
+
this.cursor = 0
|
|
37
|
+
}
|
|
38
|
+
return this.words[this.cursor++]
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function cryptoRandom(): CryptoRandom | undefined {
|
|
43
|
+
const crypto = (globalThis as { crypto?: Partial<CryptoLike> }).crypto
|
|
44
|
+
return typeof crypto?.getRandomValues === 'function' ? new CryptoRandom(crypto as CryptoLike) : undefined
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Without a crypto source (none in supported Node and edge runtimes), the core generator's Math.random source is used.
|
|
48
|
+
const generator = new V7Generator(cryptoRandom())
|
|
49
|
+
|
|
50
|
+
/** A v7 uuid for an event or a personless distinct id, with random bits from `crypto.getRandomValues`. */
|
|
51
|
+
export function eventUuidv7(): string {
|
|
52
|
+
return generator.generate().toString()
|
|
53
|
+
}
|