posthog-node 5.67.2 → 5.68.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/dist/extensions/context/lifetime.d.ts +8 -0
- package/dist/extensions/context/lifetime.d.ts.map +1 -1
- package/dist/extensions/context/lifetime.js +12 -1
- package/dist/extensions/context/lifetime.mjs +10 -2
- package/dist/extensions/nestjs.d.ts +8 -0
- package/dist/extensions/nestjs.d.ts.map +1 -1
- package/dist/extensions/nestjs.js +37 -13
- package/dist/extensions/nestjs.mjs +37 -13
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +3 -3
- package/src/extensions/context/lifetime.ts +25 -1
- package/src/extensions/nestjs.ts +56 -11
- package/src/version.ts +1 -1
|
@@ -5,4 +5,12 @@ export interface ContextLifetime {
|
|
|
5
5
|
}
|
|
6
6
|
export declare function inheritContextLifetime(parent: ContextData | undefined, child: ContextData, signal?: AbortSignal): ContextData;
|
|
7
7
|
export declare function isContextActive(context: ContextData): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Returns a signal that aborts when the given context ends, or `undefined` if nothing can end it.
|
|
10
|
+
*
|
|
11
|
+
* A context ends when the AbortSignal given to its `withContext` call aborts. A nested context
|
|
12
|
+
* also ends when any context it is nested in ends, so it can depend on several signals. This
|
|
13
|
+
* combines them into one signal, so a fresh context can take it and end at the same time.
|
|
14
|
+
*/
|
|
15
|
+
export declare function getContextLifetimeSignal(context: ContextData): AbortSignal | undefined;
|
|
8
16
|
//# sourceMappingURL=lifetime.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lifetime.d.ts","sourceRoot":"","sources":["../../../src/extensions/context/lifetime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE1C,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,WAAW,CAAA;IACnB,MAAM,CAAC,EAAE,eAAe,CAAA;CACzB;
|
|
1
|
+
{"version":3,"file":"lifetime.d.ts","sourceRoot":"","sources":["../../../src/extensions/context/lifetime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE1C,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,WAAW,CAAA;IACnB,MAAM,CAAC,EAAE,eAAe,CAAA;CACzB;AAQD,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,WAAW,GAAG,SAAS,EAC/B,KAAK,EAAE,WAAW,EAClB,MAAM,CAAC,EAAE,WAAW,GACnB,WAAW,CAKb;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAO7D;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,GAAG,SAAS,CAWtF"}
|
|
@@ -27,7 +27,9 @@ var __webpack_require__ = {};
|
|
|
27
27
|
})();
|
|
28
28
|
var __webpack_exports__ = {};
|
|
29
29
|
__webpack_require__.r(__webpack_exports__);
|
|
30
|
-
const
|
|
30
|
+
const REGISTRY_KEY = Symbol.for('posthog-node.context-lifetimes');
|
|
31
|
+
const registryHolder = globalThis;
|
|
32
|
+
const lifetimes = registryHolder[REGISTRY_KEY] ??= new WeakMap();
|
|
31
33
|
function inheritContextLifetime(parent, child, signal) {
|
|
32
34
|
const lifetime = parent && lifetimes.get(parent);
|
|
33
35
|
if (signal) lifetimes.set(child, {
|
|
@@ -45,13 +47,22 @@ function isContextActive(context) {
|
|
|
45
47
|
}
|
|
46
48
|
return true;
|
|
47
49
|
}
|
|
50
|
+
function getContextLifetimeSignal(context) {
|
|
51
|
+
const signals = [];
|
|
52
|
+
for(let lifetime = lifetimes.get(context); lifetime; lifetime = lifetime.parent)signals.push(lifetime.signal);
|
|
53
|
+
if (0 === signals.length) return;
|
|
54
|
+
return 1 === signals.length ? signals[0] : AbortSignal.any(signals);
|
|
55
|
+
}
|
|
48
56
|
__webpack_require__.d(__webpack_exports__, {
|
|
57
|
+
getContextLifetimeSignal: ()=>getContextLifetimeSignal,
|
|
49
58
|
inheritContextLifetime: ()=>inheritContextLifetime,
|
|
50
59
|
isContextActive: ()=>isContextActive
|
|
51
60
|
});
|
|
61
|
+
exports.getContextLifetimeSignal = __webpack_exports__.getContextLifetimeSignal;
|
|
52
62
|
exports.inheritContextLifetime = __webpack_exports__.inheritContextLifetime;
|
|
53
63
|
exports.isContextActive = __webpack_exports__.isContextActive;
|
|
54
64
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
65
|
+
"getContextLifetimeSignal",
|
|
55
66
|
"inheritContextLifetime",
|
|
56
67
|
"isContextActive"
|
|
57
68
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
const
|
|
1
|
+
const REGISTRY_KEY = Symbol.for('posthog-node.context-lifetimes');
|
|
2
|
+
const registryHolder = globalThis;
|
|
3
|
+
const lifetimes = registryHolder[REGISTRY_KEY] ??= new WeakMap();
|
|
2
4
|
function inheritContextLifetime(parent, child, signal) {
|
|
3
5
|
const lifetime = parent && lifetimes.get(parent);
|
|
4
6
|
if (signal) lifetimes.set(child, {
|
|
@@ -16,4 +18,10 @@ function isContextActive(context) {
|
|
|
16
18
|
}
|
|
17
19
|
return true;
|
|
18
20
|
}
|
|
19
|
-
|
|
21
|
+
function getContextLifetimeSignal(context) {
|
|
22
|
+
const signals = [];
|
|
23
|
+
for(let lifetime = lifetimes.get(context); lifetime; lifetime = lifetime.parent)signals.push(lifetime.signal);
|
|
24
|
+
if (0 === signals.length) return;
|
|
25
|
+
return 1 === signals.length ? signals[0] : AbortSignal.any(signals);
|
|
26
|
+
}
|
|
27
|
+
export { getContextLifetimeSignal, inheritContextLifetime, isContextActive };
|
|
@@ -20,12 +20,20 @@ export interface ExceptionCaptureOptions {
|
|
|
20
20
|
export interface PostHogInterceptorOptions {
|
|
21
21
|
/** Enable exception capture. Pass `true` for defaults or an object to configure. @default false */
|
|
22
22
|
captureExceptions?: boolean | ExceptionCaptureOptions;
|
|
23
|
+
/**
|
|
24
|
+
* Returns the distinct ID for a request, such as the signed-in user's ID from your auth layer.
|
|
25
|
+
* It takes precedence over the `X-POSTHOG-DISTINCT-ID` tracing header and over a distinct ID
|
|
26
|
+
* already set in the PostHog context. Return `undefined` to fall back to those.
|
|
27
|
+
*/
|
|
28
|
+
getDistinctId?: (request: any) => string | undefined;
|
|
23
29
|
}
|
|
24
30
|
export declare class PostHogInterceptor implements NestInterceptor {
|
|
25
31
|
private posthog;
|
|
26
32
|
private captureExceptions;
|
|
27
33
|
private minStatusToCapture;
|
|
34
|
+
private getDistinctId?;
|
|
28
35
|
constructor(posthog: PostHogBackendClient, options?: PostHogInterceptorOptions);
|
|
36
|
+
private resolveDistinctId;
|
|
29
37
|
intercept(context: ExecutionContext, next: CallHandler): Observable<any>;
|
|
30
38
|
}
|
|
31
39
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nestjs.d.ts","sourceRoot":"","sources":["../../src/extensions/nestjs.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAc,MAAM,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"nestjs.d.ts","sourceRoot":"","sources":["../../src/extensions/nestjs.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAc,MAAM,MAAM,CAAA;AAa7C,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAA;AAGhD,UAAU,iBAAiB;IACzB,UAAU,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,CAAA;IACxB,WAAW,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,CAAA;CAC1B;AAED,UAAU,gBAAgB;IACxB,YAAY,IAAI,iBAAiB,CAAA;CAClC;AAED,UAAU,WAAW,CAAC,CAAC,GAAG,GAAG;IAC3B,MAAM,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;CACxB;AAED,UAAU,eAAe,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG;IACxC,SAAS,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAA;CAC1E;AAED,MAAM,WAAW,uBAAuB;IACtC,+GAA+G;IAC/G,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B;AAED,MAAM,WAAW,yBAAyB;IACxC,mGAAmG;IACnG,iBAAiB,CAAC,EAAE,OAAO,GAAG,uBAAuB,CAAA;IACrD;;;;OAIG;IACH,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,MAAM,GAAG,SAAS,CAAA;CACrD;AAwBD,qBAAa,kBAAmB,YAAW,eAAe;IACxD,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,iBAAiB,CAAS;IAClC,OAAO,CAAC,kBAAkB,CAAQ;IAClC,OAAO,CAAC,aAAa,CAAC,CAAsC;IAE5D,YAAY,OAAO,EAAE,oBAAoB,EAAE,OAAO,CAAC,EAAE,yBAAyB,EAM7E;IAED,OAAO,CAAC,iBAAiB;IAczB,SAAS,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC,GAAG,CAAC,CA+EvE;CACF"}
|
|
@@ -46,6 +46,7 @@ var index_js_default = /*#__PURE__*/ __webpack_require__.n(index_js_namespaceObj
|
|
|
46
46
|
const external_tracing_headers_js_namespaceObject = require("./tracing-headers.js");
|
|
47
47
|
const external_url_utils_js_namespaceObject = require("./url-utils.js");
|
|
48
48
|
const listeners_js_namespaceObject = require("./context/listeners.js");
|
|
49
|
+
const lifetime_js_namespaceObject = require("./context/lifetime.js");
|
|
49
50
|
function getClientIp(headers, request) {
|
|
50
51
|
const forwarded = (0, external_tracing_headers_js_namespaceObject.getFirstHeaderValue)(headers['x-forwarded-for']);
|
|
51
52
|
if (forwarded) {
|
|
@@ -66,6 +67,16 @@ class PostHogInterceptor {
|
|
|
66
67
|
const capture = options?.captureExceptions;
|
|
67
68
|
this.captureExceptions = !!capture;
|
|
68
69
|
this.minStatusToCapture = ('object' == typeof capture ? capture.minStatusToCapture : void 0) ?? 500;
|
|
70
|
+
this.getDistinctId = options?.getDistinctId;
|
|
71
|
+
}
|
|
72
|
+
resolveDistinctId(request) {
|
|
73
|
+
if (!this.getDistinctId) return;
|
|
74
|
+
try {
|
|
75
|
+
const distinctId = this.getDistinctId(request);
|
|
76
|
+
return 'string' == typeof distinctId && distinctId.trim() ? distinctId : void 0;
|
|
77
|
+
} catch {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
69
80
|
}
|
|
70
81
|
intercept(context, next) {
|
|
71
82
|
const httpHost = context.switchToHttp();
|
|
@@ -80,16 +91,24 @@ class PostHogInterceptor {
|
|
|
80
91
|
(0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$request_path', (0, external_url_utils_js_namespaceObject.normalizeRequestPath)(request?.path ?? request?.url, disableCaptureUrlHashes));
|
|
81
92
|
(0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$user_agent', (0, external_tracing_headers_js_namespaceObject.getFirstHeaderValue)(headers['user-agent']));
|
|
82
93
|
(0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$ip', getClientIp(headers, request));
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
94
|
+
return new external_rxjs_namespaceObject.Observable((subscriber)=>{
|
|
95
|
+
const existing = this.posthog.getContext();
|
|
96
|
+
const resolvedDistinctId = this.resolveDistinctId(request);
|
|
97
|
+
const requestDistinctId = resolvedDistinctId ?? distinctId ?? existing?.distinctId;
|
|
98
|
+
const requestSessionId = sessionId ?? existing?.sessionId;
|
|
99
|
+
const copiesDistinctId = void 0 === resolvedDistinctId && void 0 === distinctId && existing?.distinctId !== void 0;
|
|
100
|
+
const copiesSessionId = void 0 === sessionId && existing?.sessionId !== void 0;
|
|
101
|
+
const signal = existing && (copiesDistinctId || copiesSessionId) ? (0, lifetime_js_namespaceObject.getContextLifetimeSignal)(existing) : void 0;
|
|
102
|
+
const contextData = {
|
|
103
|
+
...void 0 !== requestSessionId ? {
|
|
104
|
+
sessionId: requestSessionId
|
|
105
|
+
} : {},
|
|
106
|
+
...void 0 !== requestDistinctId ? {
|
|
107
|
+
distinctId: requestDistinctId
|
|
108
|
+
} : {},
|
|
109
|
+
properties
|
|
110
|
+
};
|
|
111
|
+
return this.posthog.withContext(contextData, ()=>{
|
|
93
112
|
(0, listeners_js_namespaceObject.notifyContextListeners)(this.posthog, true);
|
|
94
113
|
let source = next.handle();
|
|
95
114
|
if (this.captureExceptions) source = source.pipe((0, operators_namespaceObject.catchError)((exception)=>{
|
|
@@ -100,13 +119,18 @@ class PostHogInterceptor {
|
|
|
100
119
|
const additionalProperties = void 0 !== responseStatus ? {
|
|
101
120
|
$response_status_code: responseStatus
|
|
102
121
|
} : void 0;
|
|
103
|
-
|
|
122
|
+
const exceptionDistinctId = copiesDistinctId && signal?.aborted ? void 0 : requestDistinctId;
|
|
123
|
+
this.posthog.captureException(exception, exceptionDistinctId, additionalProperties);
|
|
104
124
|
return (0, external_rxjs_namespaceObject.throwError)(()=>exception);
|
|
105
125
|
}));
|
|
106
126
|
return source.subscribe(subscriber);
|
|
107
127
|
}, {
|
|
108
|
-
fresh: true
|
|
109
|
-
|
|
128
|
+
fresh: true,
|
|
129
|
+
...signal ? {
|
|
130
|
+
signal
|
|
131
|
+
} : {}
|
|
132
|
+
});
|
|
133
|
+
});
|
|
110
134
|
}
|
|
111
135
|
}
|
|
112
136
|
exports.PostHogInterceptor = __webpack_exports__.PostHogInterceptor;
|
|
@@ -4,6 +4,7 @@ import error_tracking from "./error-tracking/index.mjs";
|
|
|
4
4
|
import { addProperty, getFirstHeaderValue, getPostHogCookieReadOptions, getPostHogTracingHeaderValues } from "./tracing-headers.mjs";
|
|
5
5
|
import { normalizeRequestCurrentUrl, normalizeRequestPath } from "./url-utils.mjs";
|
|
6
6
|
import { notifyContextListeners } from "./context/listeners.mjs";
|
|
7
|
+
import { getContextLifetimeSignal } from "./context/lifetime.mjs";
|
|
7
8
|
function getClientIp(headers, request) {
|
|
8
9
|
const forwarded = getFirstHeaderValue(headers['x-forwarded-for']);
|
|
9
10
|
if (forwarded) {
|
|
@@ -24,6 +25,16 @@ class PostHogInterceptor {
|
|
|
24
25
|
const capture = options?.captureExceptions;
|
|
25
26
|
this.captureExceptions = !!capture;
|
|
26
27
|
this.minStatusToCapture = ('object' == typeof capture ? capture.minStatusToCapture : void 0) ?? 500;
|
|
28
|
+
this.getDistinctId = options?.getDistinctId;
|
|
29
|
+
}
|
|
30
|
+
resolveDistinctId(request) {
|
|
31
|
+
if (!this.getDistinctId) return;
|
|
32
|
+
try {
|
|
33
|
+
const distinctId = this.getDistinctId(request);
|
|
34
|
+
return 'string' == typeof distinctId && distinctId.trim() ? distinctId : void 0;
|
|
35
|
+
} catch {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
27
38
|
}
|
|
28
39
|
intercept(context, next) {
|
|
29
40
|
const httpHost = context.switchToHttp();
|
|
@@ -38,16 +49,24 @@ class PostHogInterceptor {
|
|
|
38
49
|
addProperty(properties, '$request_path', normalizeRequestPath(request?.path ?? request?.url, disableCaptureUrlHashes));
|
|
39
50
|
addProperty(properties, '$user_agent', getFirstHeaderValue(headers['user-agent']));
|
|
40
51
|
addProperty(properties, '$ip', getClientIp(headers, request));
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
return new Observable((subscriber)=>{
|
|
53
|
+
const existing = this.posthog.getContext();
|
|
54
|
+
const resolvedDistinctId = this.resolveDistinctId(request);
|
|
55
|
+
const requestDistinctId = resolvedDistinctId ?? distinctId ?? existing?.distinctId;
|
|
56
|
+
const requestSessionId = sessionId ?? existing?.sessionId;
|
|
57
|
+
const copiesDistinctId = void 0 === resolvedDistinctId && void 0 === distinctId && existing?.distinctId !== void 0;
|
|
58
|
+
const copiesSessionId = void 0 === sessionId && existing?.sessionId !== void 0;
|
|
59
|
+
const signal = existing && (copiesDistinctId || copiesSessionId) ? getContextLifetimeSignal(existing) : void 0;
|
|
60
|
+
const contextData = {
|
|
61
|
+
...void 0 !== requestSessionId ? {
|
|
62
|
+
sessionId: requestSessionId
|
|
63
|
+
} : {},
|
|
64
|
+
...void 0 !== requestDistinctId ? {
|
|
65
|
+
distinctId: requestDistinctId
|
|
66
|
+
} : {},
|
|
67
|
+
properties
|
|
68
|
+
};
|
|
69
|
+
return this.posthog.withContext(contextData, ()=>{
|
|
51
70
|
notifyContextListeners(this.posthog, true);
|
|
52
71
|
let source = next.handle();
|
|
53
72
|
if (this.captureExceptions) source = source.pipe(catchError((exception)=>{
|
|
@@ -58,13 +77,18 @@ class PostHogInterceptor {
|
|
|
58
77
|
const additionalProperties = void 0 !== responseStatus ? {
|
|
59
78
|
$response_status_code: responseStatus
|
|
60
79
|
} : void 0;
|
|
61
|
-
|
|
80
|
+
const exceptionDistinctId = copiesDistinctId && signal?.aborted ? void 0 : requestDistinctId;
|
|
81
|
+
this.posthog.captureException(exception, exceptionDistinctId, additionalProperties);
|
|
62
82
|
return throwError(()=>exception);
|
|
63
83
|
}));
|
|
64
84
|
return source.subscribe(subscriber);
|
|
65
85
|
}, {
|
|
66
|
-
fresh: true
|
|
67
|
-
|
|
86
|
+
fresh: true,
|
|
87
|
+
...signal ? {
|
|
88
|
+
signal
|
|
89
|
+
} : {}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
68
92
|
}
|
|
69
93
|
}
|
|
70
94
|
export { PostHogInterceptor };
|
package/dist/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const version = "5.
|
|
1
|
+
export declare const version = "5.68.0";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/dist/version.js
CHANGED
package/dist/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const version = "5.
|
|
1
|
+
const version = "5.68.0";
|
|
2
2
|
export { version };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "posthog-node",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.68.0",
|
|
4
4
|
"bugs": {
|
|
5
5
|
"url": "https://github.com/PostHog/posthog-js/issues"
|
|
6
6
|
},
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"rxjs": "^7.8.0",
|
|
45
45
|
"typescript": "7.0.2",
|
|
46
46
|
"vitest": "^1.6.1",
|
|
47
|
-
"@posthog/
|
|
48
|
-
"@posthog
|
|
47
|
+
"@posthog-tooling/tsconfig-base": "1.1.1",
|
|
48
|
+
"@posthog/types": "^1.423.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"@opentelemetry/api": "^1.2.0",
|
|
@@ -5,7 +5,11 @@ export interface ContextLifetime {
|
|
|
5
5
|
parent?: ContextLifetime
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
// The CommonJS and ESM builds are separate module copies. An app can create its client with one
|
|
9
|
+
// and load an integration with the other, so both share one registry through a global.
|
|
10
|
+
const REGISTRY_KEY = Symbol.for('posthog-node.context-lifetimes')
|
|
11
|
+
const registryHolder = globalThis as typeof globalThis & { [REGISTRY_KEY]?: WeakMap<ContextData, ContextLifetime> }
|
|
12
|
+
const lifetimes = (registryHolder[REGISTRY_KEY] ??= new WeakMap())
|
|
9
13
|
|
|
10
14
|
export function inheritContextLifetime(
|
|
11
15
|
parent: ContextData | undefined,
|
|
@@ -26,3 +30,23 @@ export function isContextActive(context: ContextData): boolean {
|
|
|
26
30
|
}
|
|
27
31
|
return true
|
|
28
32
|
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Returns a signal that aborts when the given context ends, or `undefined` if nothing can end it.
|
|
36
|
+
*
|
|
37
|
+
* A context ends when the AbortSignal given to its `withContext` call aborts. A nested context
|
|
38
|
+
* also ends when any context it is nested in ends, so it can depend on several signals. This
|
|
39
|
+
* combines them into one signal, so a fresh context can take it and end at the same time.
|
|
40
|
+
*/
|
|
41
|
+
export function getContextLifetimeSignal(context: ContextData): AbortSignal | undefined {
|
|
42
|
+
const signals: AbortSignal[] = []
|
|
43
|
+
for (let lifetime = lifetimes.get(context); lifetime; lifetime = lifetime.parent) {
|
|
44
|
+
signals.push(lifetime.signal)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (signals.length === 0) {
|
|
48
|
+
return undefined
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return signals.length === 1 ? signals[0] : AbortSignal.any(signals)
|
|
52
|
+
}
|
package/src/extensions/nestjs.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from './tracing-headers'
|
|
12
12
|
import { normalizeRequestCurrentUrl, normalizeRequestPath } from './url-utils'
|
|
13
13
|
import { notifyContextListeners } from './context/listeners'
|
|
14
|
+
import { getContextLifetimeSignal } from './context/lifetime'
|
|
14
15
|
import { PostHogBackendClient } from '../client'
|
|
15
16
|
|
|
16
17
|
// Local interfaces to avoid runtime dependency on @nestjs/common
|
|
@@ -39,6 +40,12 @@ export interface ExceptionCaptureOptions {
|
|
|
39
40
|
export interface PostHogInterceptorOptions {
|
|
40
41
|
/** Enable exception capture. Pass `true` for defaults or an object to configure. @default false */
|
|
41
42
|
captureExceptions?: boolean | ExceptionCaptureOptions
|
|
43
|
+
/**
|
|
44
|
+
* Returns the distinct ID for a request, such as the signed-in user's ID from your auth layer.
|
|
45
|
+
* It takes precedence over the `X-POSTHOG-DISTINCT-ID` tracing header and over a distinct ID
|
|
46
|
+
* already set in the PostHog context. Return `undefined` to fall back to those.
|
|
47
|
+
*/
|
|
48
|
+
getDistinctId?: (request: any) => string | undefined
|
|
42
49
|
}
|
|
43
50
|
|
|
44
51
|
function getClientIp(headers: IncomingHttpHeaders, request: any): string | undefined {
|
|
@@ -67,12 +74,28 @@ export class PostHogInterceptor implements NestInterceptor {
|
|
|
67
74
|
private posthog: PostHogBackendClient
|
|
68
75
|
private captureExceptions: boolean
|
|
69
76
|
private minStatusToCapture: number
|
|
77
|
+
private getDistinctId?: (request: any) => string | undefined
|
|
70
78
|
|
|
71
79
|
constructor(posthog: PostHogBackendClient, options?: PostHogInterceptorOptions) {
|
|
72
80
|
this.posthog = posthog
|
|
73
81
|
const capture = options?.captureExceptions
|
|
74
82
|
this.captureExceptions = !!capture
|
|
75
83
|
this.minStatusToCapture = (typeof capture === 'object' ? capture.minStatusToCapture : undefined) ?? 500
|
|
84
|
+
this.getDistinctId = options?.getDistinctId
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private resolveDistinctId(request: any): string | undefined {
|
|
88
|
+
if (!this.getDistinctId) {
|
|
89
|
+
return undefined
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
const distinctId = this.getDistinctId(request)
|
|
93
|
+
// Blank IDs fall back; any other ID is kept verbatim, as explicit captures keep it.
|
|
94
|
+
return typeof distinctId === 'string' && distinctId.trim() ? distinctId : undefined
|
|
95
|
+
} catch {
|
|
96
|
+
// A failing resolver must not break the request; fall back to headers and context.
|
|
97
|
+
return undefined
|
|
98
|
+
}
|
|
76
99
|
}
|
|
77
100
|
|
|
78
101
|
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
|
@@ -95,14 +118,34 @@ export class PostHogInterceptor implements NestInterceptor {
|
|
|
95
118
|
addProperty(properties, '$user_agent', getFirstHeaderValue(headers['user-agent']))
|
|
96
119
|
addProperty(properties, '$ip', getClientIp(headers, request))
|
|
97
120
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
121
|
+
return new Observable((subscriber) => {
|
|
122
|
+
// Identity is read when the request subscribes, so it comes from this request's async context.
|
|
123
|
+
// The order is: the app's getDistinctId, then the tracing headers, then identity the app set
|
|
124
|
+
// before this interceptor ran, for example in a middleware. Headers come before that earlier
|
|
125
|
+
// identity, so a context the app entered globally can't override a request's own header.
|
|
126
|
+
const existing = this.posthog.getContext()
|
|
127
|
+
const resolvedDistinctId = this.resolveDistinctId(request)
|
|
128
|
+
const requestDistinctId = resolvedDistinctId ?? distinctId ?? existing?.distinctId
|
|
129
|
+
const requestSessionId = sessionId ?? existing?.sessionId
|
|
130
|
+
|
|
131
|
+
// The app can give withContext an AbortSignal to end that context early, for example when the
|
|
132
|
+
// request it belongs to is cancelled. After the signal aborts, getContext() no longer returns
|
|
133
|
+
// the context. The fresh context below would not see that signal, so identity copied from the
|
|
134
|
+
// earlier context would stay visible to the handler after the app ended it. Passing the earlier
|
|
135
|
+
// context's signal on makes the copied identity end at the same time as the original.
|
|
136
|
+
const copiesDistinctId =
|
|
137
|
+
resolvedDistinctId === undefined && distinctId === undefined && existing?.distinctId !== undefined
|
|
138
|
+
const copiesSessionId = sessionId === undefined && existing?.sessionId !== undefined
|
|
139
|
+
const signal = existing && (copiesDistinctId || copiesSessionId) ? getContextLifetimeSignal(existing) : undefined
|
|
140
|
+
|
|
141
|
+
// Properties always start fresh, so properties from an earlier context never reach this request.
|
|
142
|
+
const contextData = {
|
|
143
|
+
...(requestSessionId !== undefined ? { sessionId: requestSessionId } : {}),
|
|
144
|
+
...(requestDistinctId !== undefined ? { distinctId: requestDistinctId } : {}),
|
|
145
|
+
properties,
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return this.posthog.withContext(
|
|
106
149
|
contextData,
|
|
107
150
|
() => {
|
|
108
151
|
notifyContextListeners(this.posthog, true)
|
|
@@ -121,7 +164,9 @@ export class PostHogInterceptor implements NestInterceptor {
|
|
|
121
164
|
const responseStatus = status ?? response?.statusCode
|
|
122
165
|
const additionalProperties: Record<string, any> | undefined =
|
|
123
166
|
responseStatus !== undefined ? { $response_status_code: responseStatus } : undefined
|
|
124
|
-
this.
|
|
167
|
+
// A copied distinct ID that the app already ended must not label this exception.
|
|
168
|
+
const exceptionDistinctId = copiesDistinctId && signal?.aborted ? undefined : requestDistinctId
|
|
169
|
+
this.posthog.captureException(exception, exceptionDistinctId, additionalProperties)
|
|
125
170
|
return throwError(() => exception)
|
|
126
171
|
})
|
|
127
172
|
)
|
|
@@ -129,8 +174,8 @@ export class PostHogInterceptor implements NestInterceptor {
|
|
|
129
174
|
|
|
130
175
|
return source.subscribe(subscriber)
|
|
131
176
|
},
|
|
132
|
-
{ fresh: true }
|
|
177
|
+
{ fresh: true, ...(signal ? { signal } : {}) }
|
|
133
178
|
)
|
|
134
|
-
)
|
|
179
|
+
})
|
|
135
180
|
}
|
|
136
181
|
}
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "5.
|
|
1
|
+
export const version = "5.68.0"
|