posthog-node
Advanced tools
| import type { IncomingHttpHeaders } from 'node:http'; | ||
| type HeaderValue = IncomingHttpHeaders[string]; | ||
| export declare const POSTHOG_TRACING_HEADERS: { | ||
| readonly sessionId: "x-posthog-session-id"; | ||
| readonly distinctId: "x-posthog-distinct-id"; | ||
| }; | ||
| export interface PostHogTracingHeaderValues { | ||
| sessionId?: string; | ||
| distinctId?: string; | ||
| } | ||
| export declare function addProperty(properties: Record<string, any>, key: string, value: unknown): void; | ||
| export declare function getFirstHeaderValue(value: HeaderValue): string | undefined; | ||
| export declare function sanitizeTracingHeaderValue(value: HeaderValue): string | undefined; | ||
| export declare function getPostHogTracingHeaderValues(headers?: IncomingHttpHeaders): PostHogTracingHeaderValues; | ||
| export {}; | ||
| //# sourceMappingURL=tracing-headers.d.ts.map |
| {"version":3,"file":"tracing-headers.d.ts","sourceRoot":"","sources":["../../src/extensions/tracing-headers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAA;AAOpD,KAAK,WAAW,GAAG,mBAAmB,CAAC,MAAM,CAAC,CAAA;AAE9C,eAAO,MAAM,uBAAuB;;;CAG1B,CAAA;AAEV,MAAM,WAAW,0BAA0B;IACzC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,wBAAgB,WAAW,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAI9F;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,CAE1E;AAED,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,CAqBjF;AAED,wBAAgB,6BAA6B,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,0BAA0B,CAYvG"} |
| "use strict"; | ||
| var __webpack_require__ = {}; | ||
| (()=>{ | ||
| __webpack_require__.d = (exports1, definition)=>{ | ||
| for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, { | ||
| enumerable: true, | ||
| get: definition[key] | ||
| }); | ||
| }; | ||
| })(); | ||
| (()=>{ | ||
| __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop); | ||
| })(); | ||
| (()=>{ | ||
| __webpack_require__.r = (exports1)=>{ | ||
| if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, { | ||
| value: 'Module' | ||
| }); | ||
| Object.defineProperty(exports1, '__esModule', { | ||
| value: true | ||
| }); | ||
| }; | ||
| })(); | ||
| var __webpack_exports__ = {}; | ||
| __webpack_require__.r(__webpack_exports__); | ||
| __webpack_require__.d(__webpack_exports__, { | ||
| POSTHOG_TRACING_HEADERS: ()=>POSTHOG_TRACING_HEADERS, | ||
| addProperty: ()=>addProperty, | ||
| getFirstHeaderValue: ()=>getFirstHeaderValue, | ||
| getPostHogTracingHeaderValues: ()=>getPostHogTracingHeaderValues, | ||
| sanitizeTracingHeaderValue: ()=>sanitizeTracingHeaderValue | ||
| }); | ||
| const TRACING_HEADER_MAX_LENGTH = 1000; | ||
| const TRACING_HEADER_CONTROL_CHARS_REGEX = /[\x00-\x1f\x7f-\x9f]/g; | ||
| const POSTHOG_TRACING_HEADERS = { | ||
| sessionId: 'x-posthog-session-id', | ||
| distinctId: 'x-posthog-distinct-id' | ||
| }; | ||
| function addProperty(properties, key, value) { | ||
| if (null != value && '' !== value) properties[key] = value; | ||
| } | ||
| function getFirstHeaderValue(value) { | ||
| return Array.isArray(value) ? value[0] : value; | ||
| } | ||
| function sanitizeTracingHeaderValue(value) { | ||
| if (Array.isArray(value)) { | ||
| for (const item of value){ | ||
| const sanitized = sanitizeTracingHeaderValue(item); | ||
| if (void 0 !== sanitized) return sanitized; | ||
| } | ||
| return; | ||
| } | ||
| if ('string' != typeof value) return; | ||
| const sanitized = value.replace(TRACING_HEADER_CONTROL_CHARS_REGEX, '').trim(); | ||
| if (!sanitized) return; | ||
| return sanitized.length > TRACING_HEADER_MAX_LENGTH ? sanitized.slice(0, TRACING_HEADER_MAX_LENGTH) : sanitized; | ||
| } | ||
| function getPostHogTracingHeaderValues(headers) { | ||
| if (!headers) return {}; | ||
| const sessionId = sanitizeTracingHeaderValue(headers[POSTHOG_TRACING_HEADERS.sessionId]); | ||
| const distinctId = sanitizeTracingHeaderValue(headers[POSTHOG_TRACING_HEADERS.distinctId]); | ||
| return { | ||
| ...void 0 !== sessionId ? { | ||
| sessionId | ||
| } : {}, | ||
| ...void 0 !== distinctId ? { | ||
| distinctId | ||
| } : {} | ||
| }; | ||
| } | ||
| exports.POSTHOG_TRACING_HEADERS = __webpack_exports__.POSTHOG_TRACING_HEADERS; | ||
| exports.addProperty = __webpack_exports__.addProperty; | ||
| exports.getFirstHeaderValue = __webpack_exports__.getFirstHeaderValue; | ||
| exports.getPostHogTracingHeaderValues = __webpack_exports__.getPostHogTracingHeaderValues; | ||
| exports.sanitizeTracingHeaderValue = __webpack_exports__.sanitizeTracingHeaderValue; | ||
| for(var __webpack_i__ in __webpack_exports__)if (-1 === [ | ||
| "POSTHOG_TRACING_HEADERS", | ||
| "addProperty", | ||
| "getFirstHeaderValue", | ||
| "getPostHogTracingHeaderValues", | ||
| "sanitizeTracingHeaderValue" | ||
| ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__]; | ||
| Object.defineProperty(exports, '__esModule', { | ||
| value: true | ||
| }); |
| const TRACING_HEADER_MAX_LENGTH = 1000; | ||
| const TRACING_HEADER_CONTROL_CHARS_REGEX = /[\x00-\x1f\x7f-\x9f]/g; | ||
| const POSTHOG_TRACING_HEADERS = { | ||
| sessionId: 'x-posthog-session-id', | ||
| distinctId: 'x-posthog-distinct-id' | ||
| }; | ||
| function addProperty(properties, key, value) { | ||
| if (null != value && '' !== value) properties[key] = value; | ||
| } | ||
| function getFirstHeaderValue(value) { | ||
| return Array.isArray(value) ? value[0] : value; | ||
| } | ||
| function sanitizeTracingHeaderValue(value) { | ||
| if (Array.isArray(value)) { | ||
| for (const item of value){ | ||
| const sanitized = sanitizeTracingHeaderValue(item); | ||
| if (void 0 !== sanitized) return sanitized; | ||
| } | ||
| return; | ||
| } | ||
| if ('string' != typeof value) return; | ||
| const sanitized = value.replace(TRACING_HEADER_CONTROL_CHARS_REGEX, '').trim(); | ||
| if (!sanitized) return; | ||
| return sanitized.length > TRACING_HEADER_MAX_LENGTH ? sanitized.slice(0, TRACING_HEADER_MAX_LENGTH) : sanitized; | ||
| } | ||
| function getPostHogTracingHeaderValues(headers) { | ||
| if (!headers) return {}; | ||
| const sessionId = sanitizeTracingHeaderValue(headers[POSTHOG_TRACING_HEADERS.sessionId]); | ||
| const distinctId = sanitizeTracingHeaderValue(headers[POSTHOG_TRACING_HEADERS.distinctId]); | ||
| return { | ||
| ...void 0 !== sessionId ? { | ||
| sessionId | ||
| } : {}, | ||
| ...void 0 !== distinctId ? { | ||
| distinctId | ||
| } : {} | ||
| }; | ||
| } | ||
| export { POSTHOG_TRACING_HEADERS, addProperty, getFirstHeaderValue, getPostHogTracingHeaderValues, sanitizeTracingHeaderValue }; |
| import type { IncomingHttpHeaders } from 'node:http' | ||
| const TRACING_HEADER_MAX_LENGTH = 1000 | ||
| // Remove C0 controls, DEL, and C1 controls from PostHog tracing IDs only. | ||
| // eslint-disable-next-line no-control-regex | ||
| const TRACING_HEADER_CONTROL_CHARS_REGEX = /[\x00-\x1f\x7f-\x9f]/g | ||
| type HeaderValue = IncomingHttpHeaders[string] | ||
| export const POSTHOG_TRACING_HEADERS = { | ||
| sessionId: 'x-posthog-session-id', | ||
| distinctId: 'x-posthog-distinct-id', | ||
| } as const | ||
| export interface PostHogTracingHeaderValues { | ||
| sessionId?: string | ||
| distinctId?: string | ||
| } | ||
| export function addProperty(properties: Record<string, any>, key: string, value: unknown): void { | ||
| if (value !== undefined && value !== null && value !== '') { | ||
| properties[key] = value | ||
| } | ||
| } | ||
| export function getFirstHeaderValue(value: HeaderValue): string | undefined { | ||
| return Array.isArray(value) ? value[0] : value | ||
| } | ||
| export function sanitizeTracingHeaderValue(value: HeaderValue): string | undefined { | ||
| if (Array.isArray(value)) { | ||
| for (const item of value) { | ||
| const sanitized = sanitizeTracingHeaderValue(item) | ||
| if (sanitized !== undefined) { | ||
| return sanitized | ||
| } | ||
| } | ||
| return undefined | ||
| } | ||
| if (typeof value !== 'string') { | ||
| return undefined | ||
| } | ||
| const sanitized = value.replace(TRACING_HEADER_CONTROL_CHARS_REGEX, '').trim() | ||
| if (!sanitized) { | ||
| return undefined | ||
| } | ||
| return sanitized.length > TRACING_HEADER_MAX_LENGTH ? sanitized.slice(0, TRACING_HEADER_MAX_LENGTH) : sanitized | ||
| } | ||
| export function getPostHogTracingHeaderValues(headers?: IncomingHttpHeaders): PostHogTracingHeaderValues { | ||
| if (!headers) { | ||
| return {} | ||
| } | ||
| const sessionId = sanitizeTracingHeaderValue(headers[POSTHOG_TRACING_HEADERS.sessionId]) | ||
| const distinctId = sanitizeTracingHeaderValue(headers[POSTHOG_TRACING_HEADERS.distinctId]) | ||
| return { | ||
| ...(sessionId !== undefined ? { sessionId } : {}), | ||
| ...(distinctId !== undefined ? { distinctId } : {}), | ||
| } | ||
| } |
@@ -13,2 +13,5 @@ import { PostHogBackendClient } from '../client'; | ||
| } | ||
| export declare function setupExpressRequestContext(_posthog: PostHogBackendClient, app: { | ||
| use: (middleware: ExpressMiddleware) => unknown; | ||
| }): void; | ||
| export declare function setupExpressErrorHandler(_posthog: PostHogBackendClient, app: { | ||
@@ -15,0 +18,0 @@ use: (middleware: ExpressMiddleware | ExpressErrorMiddleware) => unknown; |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"express.d.ts","sourceRoot":"","sources":["../../src/extensions/express.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,E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AE,QAAQ,EACb,IAAI,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,KACnC,IAAI,CAAA;AAET,UAAU,eAAgB,SAAQ,KAAK;IACrC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACxB,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IAC5B,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IAC7B,MAAM,CAAC,EAAE;QACP,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;KAC7B,CAAA;CACF;AA4BD,wBAAgB,0BAA0B,CACxC,QAAQ,EAAE,oBAAoB,EAC9B,GAAG,EAAE;IACH,GAAG,EAAE,CAAC,UAAU,EAAE,iBAAiB,KAAK,OAAO,CAAA;CAChD,GACA,IAAI,CAEN;AAQD,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,oBAAoB,EAC9B,GAAG,EAAE;IACH,GAAG,EAAE,CAAC,UAAU,EAAE,iBAAiB,GAAG,sBAAsB,KAAK,OAAO,CAAA;CACzE,GACA,IAAI,CAEN"} |
@@ -36,2 +36,3 @@ "use strict"; | ||
| __webpack_require__.d(__webpack_exports__, { | ||
| setupExpressRequestContext: ()=>setupExpressRequestContext, | ||
| setupExpressErrorHandler: ()=>setupExpressErrorHandler | ||
@@ -41,2 +42,37 @@ }); | ||
| var index_js_default = /*#__PURE__*/ __webpack_require__.n(index_js_namespaceObject); | ||
| const external_tracing_headers_js_namespaceObject = require("./tracing-headers.js"); | ||
| function getClientIp(req) { | ||
| const forwarded = (0, external_tracing_headers_js_namespaceObject.getFirstHeaderValue)(req.headers['x-forwarded-for']); | ||
| if (forwarded) { | ||
| const ip = forwarded.split(',')[0].trim(); | ||
| if (ip) return ip; | ||
| } | ||
| return req.socket?.remoteAddress; | ||
| } | ||
| function buildRequestContextData(req) { | ||
| const { sessionId, distinctId } = (0, external_tracing_headers_js_namespaceObject.getPostHogTracingHeaderValues)(req.headers); | ||
| const properties = {}; | ||
| (0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$current_url', req.originalUrl || req.url); | ||
| (0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$request_method', req.method); | ||
| (0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$request_path', req.path); | ||
| (0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$user_agent', (0, external_tracing_headers_js_namespaceObject.getFirstHeaderValue)(req.headers['user-agent'])); | ||
| (0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$ip', getClientIp(req)); | ||
| return { | ||
| ...void 0 !== sessionId ? { | ||
| sessionId | ||
| } : {}, | ||
| ...void 0 !== distinctId ? { | ||
| distinctId | ||
| } : {}, | ||
| properties | ||
| }; | ||
| } | ||
| function setupExpressRequestContext(_posthog, app) { | ||
| app.use(posthogRequestContext(_posthog)); | ||
| } | ||
| function posthogRequestContext(posthog) { | ||
| return (req, _res, next)=>{ | ||
| posthog.withContext(buildRequestContextData(req), ()=>next()); | ||
| }; | ||
| } | ||
| function setupExpressErrorHandler(_posthog, app) { | ||
@@ -48,4 +84,3 @@ app.use(posthogErrorHandler(_posthog)); | ||
| if (index_js_default().isPreviouslyCapturedError(error)) return void next(error); | ||
| const sessionId = req.headers['x-posthog-session-id']; | ||
| const distinctId = req.headers['x-posthog-distinct-id']; | ||
| const contextData = buildRequestContextData(req); | ||
| const syntheticException = new Error('Synthetic exception'); | ||
@@ -59,11 +94,10 @@ const hint = { | ||
| }; | ||
| posthog.addPendingPromise(index_js_default().buildEventMessage(error, hint, distinctId, { | ||
| $session_id: sessionId, | ||
| $current_url: req.url, | ||
| $request_method: req.method, | ||
| $request_path: req.path, | ||
| $user_agent: req.headers['user-agent'], | ||
| $response_status_code: res.statusCode, | ||
| $ip: req.headers['x-forwarded-for'] || req?.socket?.remoteAddress | ||
| }).then((msg)=>{ | ||
| const additionalProperties = { | ||
| ...void 0 !== contextData.sessionId ? { | ||
| $session_id: contextData.sessionId | ||
| } : {}, | ||
| ...contextData.properties || {}, | ||
| $response_status_code: res.statusCode | ||
| }; | ||
| posthog.addPendingPromise(index_js_default().buildEventMessage(error, hint, contextData.distinctId, additionalProperties).then((msg)=>{ | ||
| posthog.capture(msg); | ||
@@ -75,4 +109,6 @@ })); | ||
| exports.setupExpressErrorHandler = __webpack_exports__.setupExpressErrorHandler; | ||
| exports.setupExpressRequestContext = __webpack_exports__.setupExpressRequestContext; | ||
| for(var __webpack_i__ in __webpack_exports__)if (-1 === [ | ||
| "setupExpressErrorHandler" | ||
| "setupExpressErrorHandler", | ||
| "setupExpressRequestContext" | ||
| ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__]; | ||
@@ -79,0 +115,0 @@ Object.defineProperty(exports, '__esModule', { |
| import error_tracking from "./error-tracking/index.mjs"; | ||
| import { addProperty, getFirstHeaderValue, getPostHogTracingHeaderValues } from "./tracing-headers.mjs"; | ||
| function getClientIp(req) { | ||
| const forwarded = getFirstHeaderValue(req.headers['x-forwarded-for']); | ||
| if (forwarded) { | ||
| const ip = forwarded.split(',')[0].trim(); | ||
| if (ip) return ip; | ||
| } | ||
| return req.socket?.remoteAddress; | ||
| } | ||
| function buildRequestContextData(req) { | ||
| const { sessionId, distinctId } = getPostHogTracingHeaderValues(req.headers); | ||
| const properties = {}; | ||
| addProperty(properties, '$current_url', req.originalUrl || req.url); | ||
| addProperty(properties, '$request_method', req.method); | ||
| addProperty(properties, '$request_path', req.path); | ||
| addProperty(properties, '$user_agent', getFirstHeaderValue(req.headers['user-agent'])); | ||
| addProperty(properties, '$ip', getClientIp(req)); | ||
| return { | ||
| ...void 0 !== sessionId ? { | ||
| sessionId | ||
| } : {}, | ||
| ...void 0 !== distinctId ? { | ||
| distinctId | ||
| } : {}, | ||
| properties | ||
| }; | ||
| } | ||
| function setupExpressRequestContext(_posthog, app) { | ||
| app.use(posthogRequestContext(_posthog)); | ||
| } | ||
| function posthogRequestContext(posthog) { | ||
| return (req, _res, next)=>{ | ||
| posthog.withContext(buildRequestContextData(req), ()=>next()); | ||
| }; | ||
| } | ||
| function setupExpressErrorHandler(_posthog, app) { | ||
@@ -8,4 +43,3 @@ app.use(posthogErrorHandler(_posthog)); | ||
| if (error_tracking.isPreviouslyCapturedError(error)) return void next(error); | ||
| const sessionId = req.headers['x-posthog-session-id']; | ||
| const distinctId = req.headers['x-posthog-distinct-id']; | ||
| const contextData = buildRequestContextData(req); | ||
| const syntheticException = new Error('Synthetic exception'); | ||
@@ -19,11 +53,10 @@ const hint = { | ||
| }; | ||
| posthog.addPendingPromise(error_tracking.buildEventMessage(error, hint, distinctId, { | ||
| $session_id: sessionId, | ||
| $current_url: req.url, | ||
| $request_method: req.method, | ||
| $request_path: req.path, | ||
| $user_agent: req.headers['user-agent'], | ||
| $response_status_code: res.statusCode, | ||
| $ip: req.headers['x-forwarded-for'] || req?.socket?.remoteAddress | ||
| }).then((msg)=>{ | ||
| const additionalProperties = { | ||
| ...void 0 !== contextData.sessionId ? { | ||
| $session_id: contextData.sessionId | ||
| } : {}, | ||
| ...contextData.properties || {}, | ||
| $response_status_code: res.statusCode | ||
| }; | ||
| posthog.addPendingPromise(error_tracking.buildEventMessage(error, hint, contextData.distinctId, additionalProperties).then((msg)=>{ | ||
| posthog.capture(msg); | ||
@@ -34,2 +67,2 @@ })); | ||
| } | ||
| export { setupExpressErrorHandler }; | ||
| export { setupExpressErrorHandler, setupExpressRequestContext }; |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"nestjs.d.ts","sourceRoot":"","sources":["../../src/extensions/nestjs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAc,MAAM,MAAM,CAAA;AAI7C,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;IAC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AAI,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;gBAEtB,OAAO,EAAE,oBAAoB,EAAE,OAAO,CAAC,EAAE,yBAAyB;IAO9E,SAAS,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC,GAAG,CAAC;CA+CzE"} |
@@ -42,6 +42,7 @@ "use strict"; | ||
| var index_js_default = /*#__PURE__*/ __webpack_require__.n(index_js_namespaceObject); | ||
| const external_tracing_headers_js_namespaceObject = require("./tracing-headers.js"); | ||
| function getClientIp(headers, request) { | ||
| const forwarded = headers['x-forwarded-for']; | ||
| const forwarded = (0, external_tracing_headers_js_namespaceObject.getFirstHeaderValue)(headers['x-forwarded-for']); | ||
| if (forwarded) { | ||
| const ip = String(forwarded).split(',')[0].trim(); | ||
| const ip = forwarded.split(',')[0].trim(); | ||
| if (ip) return ip; | ||
@@ -69,16 +70,17 @@ } | ||
| const headers = request?.headers ?? {}; | ||
| const sessionId = headers['x-posthog-session-id']; | ||
| const windowId = headers['x-posthog-window-id']; | ||
| const distinctId = headers['x-posthog-distinct-id']; | ||
| const { sessionId, distinctId } = (0, external_tracing_headers_js_namespaceObject.getPostHogTracingHeaderValues)(headers); | ||
| const properties = {}; | ||
| (0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$current_url', request?.url); | ||
| (0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$request_method', request?.method); | ||
| (0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$request_path', request?.path ?? request?.url); | ||
| (0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$user_agent', (0, external_tracing_headers_js_namespaceObject.getFirstHeaderValue)(headers['user-agent'])); | ||
| (0, external_tracing_headers_js_namespaceObject.addProperty)(properties, '$ip', getClientIp(headers, request)); | ||
| const contextData = { | ||
| sessionId, | ||
| distinctId, | ||
| properties: { | ||
| $current_url: request?.url, | ||
| $request_method: request?.method, | ||
| $request_path: request?.path ?? request?.url, | ||
| $window_id: windowId, | ||
| $user_agent: headers['user-agent'], | ||
| $ip: getClientIp(headers, request) | ||
| } | ||
| ...void 0 !== sessionId ? { | ||
| sessionId | ||
| } : {}, | ||
| ...void 0 !== distinctId ? { | ||
| distinctId | ||
| } : {}, | ||
| properties | ||
| }; | ||
@@ -85,0 +87,0 @@ this.posthog.enterContext(contextData); |
| import { throwError } from "rxjs"; | ||
| import { catchError } from "rxjs/operators"; | ||
| import error_tracking from "./error-tracking/index.mjs"; | ||
| import { addProperty, getFirstHeaderValue, getPostHogTracingHeaderValues } from "./tracing-headers.mjs"; | ||
| function getClientIp(headers, request) { | ||
| const forwarded = headers['x-forwarded-for']; | ||
| const forwarded = getFirstHeaderValue(headers['x-forwarded-for']); | ||
| if (forwarded) { | ||
| const ip = String(forwarded).split(',')[0].trim(); | ||
| const ip = forwarded.split(',')[0].trim(); | ||
| if (ip) return ip; | ||
@@ -30,16 +31,17 @@ } | ||
| const headers = request?.headers ?? {}; | ||
| const sessionId = headers['x-posthog-session-id']; | ||
| const windowId = headers['x-posthog-window-id']; | ||
| const distinctId = headers['x-posthog-distinct-id']; | ||
| const { sessionId, distinctId } = getPostHogTracingHeaderValues(headers); | ||
| const properties = {}; | ||
| addProperty(properties, '$current_url', request?.url); | ||
| addProperty(properties, '$request_method', request?.method); | ||
| addProperty(properties, '$request_path', request?.path ?? request?.url); | ||
| addProperty(properties, '$user_agent', getFirstHeaderValue(headers['user-agent'])); | ||
| addProperty(properties, '$ip', getClientIp(headers, request)); | ||
| const contextData = { | ||
| sessionId, | ||
| distinctId, | ||
| properties: { | ||
| $current_url: request?.url, | ||
| $request_method: request?.method, | ||
| $request_path: request?.path ?? request?.url, | ||
| $window_id: windowId, | ||
| $user_agent: headers['user-agent'], | ||
| $ip: getClientIp(headers, request) | ||
| } | ||
| ...void 0 !== sessionId ? { | ||
| sessionId | ||
| } : {}, | ||
| ...void 0 !== distinctId ? { | ||
| distinctId | ||
| } : {}, | ||
| properties | ||
| }; | ||
@@ -46,0 +48,0 @@ this.posthog.enterContext(contextData); |
@@ -1,2 +0,2 @@ | ||
| export declare const version = "5.30.8"; | ||
| export declare const version = "5.31.0"; | ||
| //# sourceMappingURL=version.d.ts.map |
+1
-1
@@ -29,3 +29,3 @@ "use strict"; | ||
| }); | ||
| const version = '5.30.8'; | ||
| const version = '5.31.0'; | ||
| exports.version = __webpack_exports__.version; | ||
@@ -32,0 +32,0 @@ for(var __webpack_i__ in __webpack_exports__)if (-1 === [ |
+1
-1
@@ -1,2 +0,2 @@ | ||
| const version = '5.30.8'; | ||
| const version = '5.31.0'; | ||
| export { version }; |
+1
-1
| { | ||
| "name": "posthog-node", | ||
| "version": "5.30.8", | ||
| "version": "5.31.0", | ||
| "description": "PostHog Node.js integration", | ||
@@ -5,0 +5,0 @@ "repository": { |
| import ErrorTracking from './error-tracking' | ||
| import { PostHogBackendClient } from '../client' | ||
| import { ErrorTracking as CoreErrorTracking } from '@posthog/core' | ||
| import { addProperty, getFirstHeaderValue, getPostHogTracingHeaderValues } from './tracing-headers' | ||
| import type { Request, Response } from 'express' | ||
| import type { ContextData } from './context/types' | ||
@@ -24,2 +26,43 @@ type ExpressMiddleware = (req: Request, res: Response, next: () => void) => void | ||
| function getClientIp(req: Request): string | undefined { | ||
| const forwarded = getFirstHeaderValue(req.headers['x-forwarded-for']) | ||
| if (forwarded) { | ||
| const ip = forwarded.split(',')[0].trim() | ||
| if (ip) return ip | ||
| } | ||
| return req.socket?.remoteAddress | ||
| } | ||
| function buildRequestContextData(req: Request): Partial<ContextData> { | ||
| const { sessionId, distinctId } = getPostHogTracingHeaderValues(req.headers) | ||
| const properties: Record<string, any> = {} | ||
| addProperty(properties, '$current_url', req.originalUrl || req.url) | ||
| addProperty(properties, '$request_method', req.method) | ||
| addProperty(properties, '$request_path', req.path) | ||
| addProperty(properties, '$user_agent', getFirstHeaderValue(req.headers['user-agent'])) | ||
| addProperty(properties, '$ip', getClientIp(req)) | ||
| return { | ||
| ...(sessionId !== undefined ? { sessionId } : {}), | ||
| ...(distinctId !== undefined ? { distinctId } : {}), | ||
| properties, | ||
| } | ||
| } | ||
| export function setupExpressRequestContext( | ||
| _posthog: PostHogBackendClient, | ||
| app: { | ||
| use: (middleware: ExpressMiddleware) => unknown | ||
| } | ||
| ): void { | ||
| app.use(posthogRequestContext(_posthog)) | ||
| } | ||
| function posthogRequestContext(posthog: PostHogBackendClient): ExpressMiddleware { | ||
| return (req, _res, next): void => { | ||
| posthog.withContext(buildRequestContextData(req), () => next()) | ||
| } | ||
| } | ||
| export function setupExpressErrorHandler( | ||
@@ -41,17 +84,13 @@ _posthog: PostHogBackendClient, | ||
| const sessionId: string | undefined = req.headers['x-posthog-session-id'] as string | undefined | ||
| const distinctId: string | undefined = req.headers['x-posthog-distinct-id'] as string | undefined | ||
| const contextData = buildRequestContextData(req) | ||
| const syntheticException = new Error('Synthetic exception') | ||
| const hint: CoreErrorTracking.EventHint = { mechanism: { type: 'middleware', handled: false }, syntheticException } | ||
| const additionalProperties: Record<string, any> = { | ||
| ...(contextData.sessionId !== undefined ? { $session_id: contextData.sessionId } : {}), | ||
| ...(contextData.properties || {}), | ||
| $response_status_code: res.statusCode, | ||
| } | ||
| posthog.addPendingPromise( | ||
| ErrorTracking.buildEventMessage(error, hint, distinctId, { | ||
| $session_id: sessionId, | ||
| $current_url: req.url, | ||
| $request_method: req.method, | ||
| $request_path: req.path, | ||
| $user_agent: req.headers['user-agent'], | ||
| $response_status_code: res.statusCode, | ||
| $ip: req.headers['x-forwarded-for'] || req?.socket?.remoteAddress, | ||
| }).then((msg) => { | ||
| ErrorTracking.buildEventMessage(error, hint, contextData.distinctId, additionalProperties).then((msg) => { | ||
| posthog.capture(msg) | ||
@@ -58,0 +97,0 @@ }) |
+17
-17
@@ -0,1 +1,2 @@ | ||
| import type { IncomingHttpHeaders } from 'node:http' | ||
| import { Observable, throwError } from 'rxjs' | ||
@@ -5,2 +6,3 @@ import { catchError } from 'rxjs/operators' | ||
| import ErrorTracking from './error-tracking' | ||
| import { addProperty, getFirstHeaderValue, getPostHogTracingHeaderValues } from './tracing-headers' | ||
| import { PostHogBackendClient } from '../client' | ||
@@ -36,6 +38,6 @@ | ||
| function getClientIp(headers: Record<string, any>, request: any): string | undefined { | ||
| const forwarded = headers['x-forwarded-for'] | ||
| function getClientIp(headers: IncomingHttpHeaders, request: any): string | undefined { | ||
| const forwarded = getFirstHeaderValue(headers['x-forwarded-for']) | ||
| if (forwarded) { | ||
| const ip = String(forwarded).split(',')[0].trim() | ||
| const ip = forwarded.split(',')[0].trim() | ||
| if (ip) return ip | ||
@@ -76,18 +78,16 @@ } | ||
| const headers = request?.headers ?? {} | ||
| const sessionId: string | undefined = headers['x-posthog-session-id'] | ||
| const windowId: string | undefined = headers['x-posthog-window-id'] | ||
| const distinctId: string | undefined = headers['x-posthog-distinct-id'] | ||
| const headers = (request?.headers ?? {}) as IncomingHttpHeaders | ||
| const { sessionId, distinctId } = getPostHogTracingHeaderValues(headers) | ||
| const properties: Record<string, any> = {} | ||
| addProperty(properties, '$current_url', request?.url) | ||
| addProperty(properties, '$request_method', request?.method) | ||
| addProperty(properties, '$request_path', request?.path ?? request?.url) | ||
| addProperty(properties, '$user_agent', getFirstHeaderValue(headers['user-agent'])) | ||
| addProperty(properties, '$ip', getClientIp(headers, request)) | ||
| const contextData = { | ||
| sessionId, | ||
| distinctId, | ||
| properties: { | ||
| $current_url: request?.url, | ||
| $request_method: request?.method, | ||
| $request_path: request?.path ?? request?.url, | ||
| $window_id: windowId, | ||
| $user_agent: headers['user-agent'], | ||
| $ip: getClientIp(headers, request), | ||
| }, | ||
| ...(sessionId !== undefined ? { sessionId } : {}), | ||
| ...(distinctId !== undefined ? { distinctId } : {}), | ||
| properties, | ||
| } | ||
@@ -94,0 +94,0 @@ |
+1
-1
@@ -1,1 +0,1 @@ | ||
| export const version = '5.30.8' | ||
| export const version = '5.31.0' |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
556550
2.56%118
4.42%12378
2.48%