posthog-node 5.67.2 → 5.68.1

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.
@@ -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;AAID,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"}
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 lifetimes = new WeakMap();
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 lifetimes = new WeakMap();
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
- export { inheritContextLifetime, isContextActive };
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 };
@@ -1 +1 @@
1
- {"version":3,"file":"feature-flags.d.ts","sourceRoot":"","sources":["../../../src/extensions/feature-flags/feature-flags.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,4BAA4B,EAG5B,kBAAkB,EAClB,aAAa,EACd,MAAM,aAAa,CAAA;AACpB,OAAO,KAAK,EACV,gBAAgB,EAChB,QAAQ,EACR,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,EACrB,MAAM,eAAe,CAAA;AACtB,OAAO,EAML,sBAAsB,EAItB,uCAAuC,EAGxC,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,2BAA2B,EAA2B,wBAAwB,EAAE,MAAM,SAAS,CAAA;AAkBxG,KAAK,oBAAoB,GAAG,OAAO,GAAG,UAAU,GAAG,sBAAsB,CAAA;AAEzE,cAAM,WAAY,SAAQ,KAAK;IAC7B,YAAY,OAAO,EAAE,MAAM,EAM1B;CACF;AAWD,cAAM,wBAAyB,SAAQ,KAAK;IAIxC,QAAQ,CAAC,MAAM,EAAE,oBAAoB;IAHvC,YACE,OAAO,EAAE,MAAM,EAEN,MAA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,CAE/C;IAED;;;;;OAKG;IACH,OAAO,CAAC,kBAAkB;IAU1B;;;OAGG;IACH,OAAO,CAAC,YAAY;IAQpB;;;OAGG;IACH,OAAO,CAAC,YAAY;IAMd,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC,CAiLvC;IAED,OAAO,CAAC,+BAA+B;IAoBjC,8BAA8B,IAAI,OAAO,CAAC,oBAAoB,CAAC,CAuDpE;IAEK,UAAU,CAAC,SAAS,GAAE,MAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAqBzD;CACF;AA2ED,iBAAS,aAAa,CACpB,QAAQ,EAAE,oBAAoB,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,EACpD,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACnC,YAAY,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,EACpC,uBAAuB,CAAC,EAAE,MAAM,GAC/B,OAAO,CAET;AAED,iBAAS,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAE5D;AAkLD,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,uCAAuC,EACvC,WAAW,EACX,sBAAsB,EACtB,wBAAwB,EACxB,WAAW,GACZ,CAAA"}
@@ -713,10 +713,11 @@ async function matchCohort(property, propertyValues, cohortProperties, debugMode
713
713
  return 'not_in' === property.operator ? !inCohort : inCohort;
714
714
  }
715
715
  async function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties, debugMode = false, flagDependencyEvaluator, propertyMatchingVersion) {
716
- if (!propertyGroup) return true;
717
- const propertyGroupType = propertyGroup.type;
716
+ if (!propertyGroup || 0 === Object.keys(propertyGroup).length) return true;
717
+ const propertyGroupType = 'property' === propertyGroup.type ? 'AND' : propertyGroup.type;
718
+ if ('AND' !== propertyGroupType && 'OR' !== propertyGroupType) throw new RequiresServerEvaluation(`Unsupported cohort property group type: ${propertyGroup.type}`);
718
719
  const properties = propertyGroup.values;
719
- if (!properties || 0 === properties.length) return true;
720
+ if (!properties || 0 === properties.length) return 'AND' === propertyGroupType;
720
721
  let inconclusiveReason;
721
722
  let regexError;
722
723
  if ('values' in properties[0]) {
@@ -675,10 +675,11 @@ async function matchCohort(property, propertyValues, cohortProperties, debugMode
675
675
  return 'not_in' === property.operator ? !inCohort : inCohort;
676
676
  }
677
677
  async function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties, debugMode = false, flagDependencyEvaluator, propertyMatchingVersion) {
678
- if (!propertyGroup) return true;
679
- const propertyGroupType = propertyGroup.type;
678
+ if (!propertyGroup || 0 === Object.keys(propertyGroup).length) return true;
679
+ const propertyGroupType = 'property' === propertyGroup.type ? 'AND' : propertyGroup.type;
680
+ if ('AND' !== propertyGroupType && 'OR' !== propertyGroupType) throw new RequiresServerEvaluation(`Unsupported cohort property group type: ${propertyGroup.type}`);
680
681
  const properties = propertyGroup.values;
681
- if (!properties || 0 === properties.length) return true;
682
+ if (!properties || 0 === properties.length) return 'AND' === propertyGroupType;
682
683
  let inconclusiveReason;
683
684
  let regexError;
684
685
  if ('values' in properties[0]) {
@@ -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;AAY7C,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;CACtD;AAwBD,qBAAa,kBAAmB,YAAW,eAAe;IACxD,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,iBAAiB,CAAS;IAClC,OAAO,CAAC,kBAAkB,CAAQ;IAElC,YAAY,OAAO,EAAE,oBAAoB,EAAE,OAAO,CAAC,EAAE,yBAAyB,EAK7E;IAED,SAAS,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC,GAAG,CAAC,CAyDvE;CACF"}
1
+ {"version":3,"file":"nestjs.d.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E,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
- const contextData = {
84
- ...void 0 !== sessionId ? {
85
- sessionId
86
- } : {},
87
- ...void 0 !== distinctId ? {
88
- distinctId
89
- } : {},
90
- properties
91
- };
92
- return new external_rxjs_namespaceObject.Observable((subscriber)=>this.posthog.withContext(contextData, ()=>{
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
- this.posthog.captureException(exception, distinctId, additionalProperties);
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
- const contextData = {
42
- ...void 0 !== sessionId ? {
43
- sessionId
44
- } : {},
45
- ...void 0 !== distinctId ? {
46
- distinctId
47
- } : {},
48
- properties
49
- };
50
- return new Observable((subscriber)=>this.posthog.withContext(contextData, ()=>{
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
- this.posthog.captureException(exception, distinctId, additionalProperties);
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.67.2";
1
+ export declare const version = "5.68.1";
2
2
  //# sourceMappingURL=version.d.ts.map
package/dist/version.js CHANGED
@@ -27,7 +27,7 @@ var __webpack_require__ = {};
27
27
  })();
28
28
  var __webpack_exports__ = {};
29
29
  __webpack_require__.r(__webpack_exports__);
30
- const version = "5.67.2";
30
+ const version = "5.68.1";
31
31
  __webpack_require__.d(__webpack_exports__, {}, {
32
32
  version: version
33
33
  });
package/dist/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- const version = "5.67.2";
1
+ const version = "5.68.1";
2
2
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-node",
3
- "version": "5.67.2",
3
+ "version": "5.68.1",
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/types": "^1.423.0",
48
- "@posthog-tooling/tsconfig-base": "1.1.1"
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
- const lifetimes = new WeakMap<ContextData, ContextLifetime>()
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
+ }
@@ -1497,16 +1497,19 @@ async function matchPropertyGroup(
1497
1497
  flagDependencyEvaluator?: FlagDependencyEvaluator,
1498
1498
  propertyMatchingVersion?: number
1499
1499
  ): Promise<boolean> {
1500
- if (!propertyGroup) {
1500
+ // The backend's canonical empty PropertyGroup is {}.
1501
+ if (!propertyGroup || Object.keys(propertyGroup).length === 0) {
1501
1502
  return true
1502
1503
  }
1503
1504
 
1504
- const propertyGroupType = propertyGroup.type
1505
+ const propertyGroupType = (propertyGroup.type as string) === 'property' ? 'AND' : propertyGroup.type
1506
+ if (propertyGroupType !== 'AND' && propertyGroupType !== 'OR') {
1507
+ throw new RequiresServerEvaluation(`Unsupported cohort property group type: ${propertyGroup.type}`)
1508
+ }
1505
1509
  const properties = propertyGroup.values
1506
1510
 
1507
1511
  if (!properties || properties.length === 0) {
1508
- // empty groups are no-ops, always match
1509
- return true
1512
+ return propertyGroupType === 'AND'
1510
1513
  }
1511
1514
 
1512
1515
  let inconclusiveReason: UnresolvedFlagReason | undefined = undefined
@@ -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
- const contextData = {
99
- ...(sessionId !== undefined ? { sessionId } : {}),
100
- ...(distinctId !== undefined ? { distinctId } : {}),
101
- properties,
102
- }
103
-
104
- return new Observable((subscriber) =>
105
- this.posthog.withContext(
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.posthog.captureException(exception, distinctId, additionalProperties)
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.67.2"
1
+ export const version = "5.68.1"