Socket
Socket
Sign inDemoInstall

posthog-node

Package Overview
Dependencies
Maintainers
4
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

posthog-node - npm Package Compare versions

Comparing version 3.6.3 to 4.0.0-beta.1

lib/posthog-node/test/test-utils.d.ts

7

CHANGELOG.md

@@ -0,1 +1,8 @@

# 4.0.0-beta.1 - 2024-03-04
- Adds a `disabled` option and the ability to change it later via `posthog.disabled = true`. Useful for disabling PostHog tracking for example in a testing environment without having complex conditional checking
- Fixes some typos in types
- `shutdown` and `shutdownAsync` takes a `shutdownTimeoutMs` param with a default of 30000 (30s). This is the time to wait for flushing events before shutting down the client. If the timeout is reached, the client will be shut down regardless of pending events.
- Adds a new `featureFlagsRequestTimeoutMs` timeout parameter for feature flags which defaults to 3 seconds, updated from the default 10s for all other API calls.
# 3.6.3 - 2024-02-15

@@ -2,0 +9,0 @@

72

lib/index.d.ts
/// <reference types="node" />
declare type PosthogCoreOptions = {
declare type PostHogCoreOptions = {
/** PostHog API host, usually 'https://app.posthog.com' or 'https://eu.posthog.com' */
host?: string;
/** The number of events to queue before sending to PostHog (flushing) */
flushAt?: number;
/** The interval in milliseconds between periodic flushes */
flushInterval?: number;
enable?: boolean;
/** If set to true the SDK is essentially disabled (useful for local environments where you don't want to track anything) */
disabled?: boolean;
/** If set to false the SDK will not track until the `optIn` function is called. */
defaultOptIn?: boolean;
/** Whether to track that `getFeatureFlag` was called (used by Experiments) */
sendFeatureFlagEvent?: boolean;
/** Whether to load feature flags when initialized or not */
preloadFeatureFlags?: boolean;
/** Option to bootstrap the library with given distinctId and feature flags */
bootstrap?: {

@@ -15,6 +24,13 @@ distinctId?: string;

};
/** How many times we will retry HTTP requests. Defaults to 3. */
fetchRetryCount?: number;
/** The delay between HTTP request retries, Defaults to 3 seconds. */
fetchRetryDelay?: number;
/** Timeout in milliseconds for any calls. Defaults to 10 seconds. */
requestTimeout?: number;
/** Timeout in milliseconds for feature flag calls. Defaults to 10 seconds for stateful clients, and 3 seconds for stateless. */
featureFlagsRequestTimeoutMs?: number;
/** For Session Analysis how long before we expire a session (defaults to 30 mins) */
sessionExpirationTimeSeconds?: number;
/** Whether to post events to PostHog in JSON or compressed format */
captureMode?: 'json' | 'form';

@@ -83,3 +99,3 @@ disableGeoip?: boolean;

};
declare type PosthogFlagsAndPayloadsResponse = {
declare type PostHogFlagsAndPayloadsResponse = {
featureFlags: PostHogDecideResponse['featureFlags'];

@@ -93,5 +109,5 @@ featureFlagPayloads: PostHogDecideResponse['featureFlagPayloads'];

interface RetriableOptions {
retryCount?: number;
retryDelay?: number;
retryCheck?: (err: any) => boolean;
retryCount: number;
retryDelay: number;
retryCheck: (err: any) => boolean;
}

@@ -114,7 +130,8 @@

private requestTimeout;
private featureFlagsRequestTimeoutMs;
private captureMode;
private removeDebugCallback?;
private debugMode;
private disableGeoip;
private _optoutOverride;
disabled: boolean;
private defaultOptIn;
private pendingPromises;

@@ -124,2 +141,4 @@ protected _events: SimpleEventEmitter;

protected _retryOptions: RetriableOptions;
protected _initPromise: Promise<void>;
protected _isInitialized: boolean;
abstract fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;

@@ -131,9 +150,11 @@ abstract getLibraryId(): string;

abstract setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void;
constructor(apiKey: string, options?: PosthogCoreOptions);
constructor(apiKey: string, options?: PostHogCoreOptions);
protected wrap(fn: () => void): void;
protected getCommonEventProperties(): any;
get optedOut(): boolean;
optIn(): void;
optOut(): void;
optIn(): Promise<void>;
optOut(): Promise<void>;
on(event: string, cb: (...args: any[]) => void): () => void;
debug(enabled?: boolean): void;
get isDebug(): boolean;
private buildPayload;

@@ -144,13 +165,13 @@ protected addPendingPromise(promise: Promise<any>): void;

***/
protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
protected captureStateless(distinctId: string, event: string, properties?: {
[key: string]: any;
}, options?: PostHogCaptureOptions): this;
}, options?: PostHogCaptureOptions): void;
protected aliasStateless(alias: string, distinctId: string, properties?: {
[key: string]: any;
}, options?: PostHogCaptureOptions): this;
}, options?: PostHogCaptureOptions): void;
/***
*** GROUPS
***/
protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): this;
protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): void;
/***

@@ -176,4 +197,4 @@ *** FEATURE FLAGS

private fetchWithRetry;
shutdownAsync(): Promise<void>;
shutdown(): void;
shutdownAsync(shutdownTimeoutMs?: number): Promise<void>;
shutdown(shutdownTimeoutMs?: number): void;
}

@@ -316,11 +337,12 @@

* a clean shutdown.
*
* @param shutdownTimeoutMs The shutdown timeout, in milliseconds. Defaults to 30000 (30s).
*/
shutdown(): void;
shutdown(shutdownTimeoutMs?: number): void;
};
declare type PostHogOptions = PosthogCoreOptions & {
declare type PostHogOptions = PostHogCoreOptions & {
persistence?: 'memory';
personalApiKey?: string;
featureFlagsPollingInterval?: number;
requestTimeout?: number;
maxCacheSize?: number;

@@ -342,4 +364,4 @@ fetch?: (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>;

getCustomUserAgent(): string;
enable(): void;
disable(): void;
enable(): Promise<void>;
disable(): Promise<void>;
debug(enabled?: boolean): void;

@@ -390,7 +412,7 @@ capture({ distinctId, event, properties, groups, sendFeatureFlags, timestamp, disableGeoip, uuid, }: EventMessage): void;

disableGeoip?: boolean;
}): Promise<PosthogFlagsAndPayloadsResponse>;
}): Promise<PostHogFlagsAndPayloadsResponse>;
groupIdentify({ groupType, groupKey, properties, distinctId, disableGeoip }: GroupIdentifyMessage): void;
reloadFeatureFlags(): Promise<void>;
shutdown(): void;
shutdownAsync(): Promise<void>;
shutdown(shutdownTimeoutMs?: number): void;
shutdownAsync(shutdownTimeoutMs?: number): Promise<void>;
private addLocalPersonAndGroupProperties;

@@ -397,0 +419,0 @@ }

@@ -1,2 +0,2 @@

import { PostHogFetchOptions, PostHogFetchResponse, PostHogAutocaptureElement, PostHogDecideResponse, PosthogCoreOptions, PostHogEventProperties, PostHogPersistedProperty, PostHogCaptureOptions, JsonType } from './types';
import { PostHogFetchOptions, PostHogFetchResponse, PostHogAutocaptureElement, PostHogDecideResponse, PostHogCoreOptions, PostHogEventProperties, PostHogPersistedProperty, PostHogCaptureOptions, JsonType } from './types';
import { RetriableOptions } from './utils';

@@ -12,7 +12,8 @@ export * as utils from './utils';

private requestTimeout;
private featureFlagsRequestTimeoutMs;
private captureMode;
private removeDebugCallback?;
private debugMode;
private disableGeoip;
private _optoutOverride;
disabled: boolean;
private defaultOptIn;
private pendingPromises;

@@ -22,2 +23,4 @@ protected _events: SimpleEventEmitter;

protected _retryOptions: RetriableOptions;
protected _initPromise: Promise<void>;
protected _isInitialized: boolean;
abstract fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;

@@ -29,9 +32,11 @@ abstract getLibraryId(): string;

abstract setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void;
constructor(apiKey: string, options?: PosthogCoreOptions);
constructor(apiKey: string, options?: PostHogCoreOptions);
protected wrap(fn: () => void): void;
protected getCommonEventProperties(): any;
get optedOut(): boolean;
optIn(): void;
optOut(): void;
optIn(): Promise<void>;
optOut(): Promise<void>;
on(event: string, cb: (...args: any[]) => void): () => void;
debug(enabled?: boolean): void;
get isDebug(): boolean;
private buildPayload;

@@ -42,13 +47,13 @@ protected addPendingPromise(promise: Promise<any>): void;

***/
protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
protected captureStateless(distinctId: string, event: string, properties?: {
[key: string]: any;
}, options?: PostHogCaptureOptions): this;
}, options?: PostHogCaptureOptions): void;
protected aliasStateless(alias: string, distinctId: string, properties?: {
[key: string]: any;
}, options?: PostHogCaptureOptions): this;
}, options?: PostHogCaptureOptions): void;
/***
*** GROUPS
***/
protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): this;
protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): void;
/***

@@ -74,4 +79,4 @@ *** FEATURE FLAGS

private fetchWithRetry;
shutdownAsync(): Promise<void>;
shutdown(): void;
shutdownAsync(shutdownTimeoutMs?: number): Promise<void>;
shutdown(shutdownTimeoutMs?: number): void;
}

@@ -84,4 +89,4 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {

protected sessionProps: PostHogEventProperties;
constructor(apiKey: string, options?: PosthogCoreOptions);
protected setupBootstrap(options?: Partial<PosthogCoreOptions>): void;
constructor(apiKey: string, options?: PostHogCoreOptions);
protected setupBootstrap(options?: Partial<PostHogCoreOptions>): void;
private get props();

@@ -94,11 +99,20 @@ private set props(value);

protected getCommonEventProperties(): any;
enrichProperties(properties?: PostHogEventProperties): any;
getSessionId(): string | undefined;
private enrichProperties;
/**
* * @returns {string} The stored session ID for the current session. This may be an empty string if the client is not yet fully initialized.
*/
getSessionId(): string;
resetSessionId(): void;
/**
* * @returns {string} The stored anonymous ID. This may be an empty string if the client is not yet fully initialized.
*/
getAnonymousId(): string;
/**
* * @returns {string} The stored distinct ID. This may be an empty string if the client is not yet fully initialized.
*/
getDistinctId(): string;
unregister(property: string): void;
unregister(property: string): Promise<void>;
register(properties: {
[key: string]: any;
}): void;
}): Promise<void>;
registerForSession(properties: {

@@ -111,8 +125,8 @@ [key: string]: any;

***/
identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
capture(event: string, properties?: {
[key: string]: any;
}, options?: PostHogCaptureOptions): this;
alias(alias: string): this;
autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
}, options?: PostHogCaptureOptions): void;
alias(alias: string): void;
autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
/***

@@ -123,5 +137,5 @@ *** GROUPS

[type: string]: string | number;
}): this;
group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
groupIdentify(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
}): void;
group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
groupIdentify(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
/***

@@ -132,3 +146,3 @@ * PROPERTIES

[type: string]: string;
}): this;
}): void;
resetPersonPropertiesForFlags(): void;

@@ -138,6 +152,6 @@ /** @deprecated - Renamed to setPersonPropertiesForFlags */

[type: string]: string;
}): this;
}): void;
setGroupPropertiesForFlags(properties: {
[type: string]: Record<string, string>;
}): this;
}): void;
resetGroupPropertiesForFlags(): void;

@@ -147,3 +161,3 @@ /** @deprecated - Renamed to setGroupPropertiesForFlags */

[type: string]: Record<string, string>;
}): this;
}): void;
/***

@@ -169,5 +183,5 @@ *** FEATURE FLAGS

onFeatureFlag(key: string, cb: (value: string | boolean) => void): () => void;
overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): void;
overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): Promise<void>;
}
export * from './types';
export { LZString };
/// <reference types="node" />
export declare type PosthogCoreOptions = {
export declare type PostHogCoreOptions = {
/** PostHog API host, usually 'https://app.posthog.com' or 'https://eu.posthog.com' */
host?: string;
/** The number of events to queue before sending to PostHog (flushing) */
flushAt?: number;
/** The interval in milliseconds between periodic flushes */
flushInterval?: number;
enable?: boolean;
/** If set to true the SDK is essentially disabled (useful for local environments where you don't want to track anything) */
disabled?: boolean;
/** If set to false the SDK will not track until the `optIn` function is called. */
defaultOptIn?: boolean;
/** Whether to track that `getFeatureFlag` was called (used by Experiments) */
sendFeatureFlagEvent?: boolean;
/** Whether to load feature flags when initialized or not */
preloadFeatureFlags?: boolean;
/** Option to bootstrap the library with given distinctId and feature flags */
bootstrap?: {

@@ -15,6 +24,13 @@ distinctId?: string;

};
/** How many times we will retry HTTP requests. Defaults to 3. */
fetchRetryCount?: number;
/** The delay between HTTP request retries, Defaults to 3 seconds. */
fetchRetryDelay?: number;
/** Timeout in milliseconds for any calls. Defaults to 10 seconds. */
requestTimeout?: number;
/** Timeout in milliseconds for feature flag calls. Defaults to 10 seconds for stateful clients, and 3 seconds for stateless. */
featureFlagsRequestTimeoutMs?: number;
/** For Session Analysis how long before we expire a session (defaults to 30 mins) */
sessionExpirationTimeSeconds?: number;
/** Whether to post events to PostHog in JSON or compressed format */
captureMode?: 'json' | 'form';

@@ -97,3 +113,3 @@ disableGeoip?: boolean;

};
export declare type PosthogFlagsAndPayloadsResponse = {
export declare type PostHogFlagsAndPayloadsResponse = {
featureFlags: PostHogDecideResponse['featureFlags'];

@@ -100,0 +116,0 @@ featureFlagPayloads: PostHogDecideResponse['featureFlagPayloads'];

export declare function assert(truthyValue: any, message: string): void;
export declare function removeTrailingSlash(url: string): string;
export interface RetriableOptions {
retryCount?: number;
retryDelay?: number;
retryCheck?: (err: any) => boolean;
retryCount: number;
retryDelay: number;
retryCheck: (err: any) => boolean;
}
export declare function retriable<T>(fn: () => Promise<T>, props?: RetriableOptions): Promise<T>;
export declare function retriable<T>(fn: () => Promise<T>, props: RetriableOptions): Promise<T>;
export declare function currentTimestamp(): number;
export declare function currentISOTime(): string;
export declare function safeSetTimeout(fn: () => void, timeout: number): any;
export declare const isPromise: (obj: any) => obj is Promise<any>;

@@ -1,8 +0,7 @@

import { JsonType, PosthogCoreOptions, PostHogCoreStateless, PostHogFetchOptions, PostHogFetchResponse, PosthogFlagsAndPayloadsResponse, PostHogPersistedProperty } from '../../posthog-core/src';
import { JsonType, PostHogCoreOptions, PostHogCoreStateless, PostHogFetchOptions, PostHogFetchResponse, PostHogFlagsAndPayloadsResponse, PostHogPersistedProperty } from '../../posthog-core/src';
import { EventMessage, GroupIdentifyMessage, IdentifyMessage, PostHogNodeV1 } from './types';
export declare type PostHogOptions = PosthogCoreOptions & {
export declare type PostHogOptions = PostHogCoreOptions & {
persistence?: 'memory';
personalApiKey?: string;
featureFlagsPollingInterval?: number;
requestTimeout?: number;
maxCacheSize?: number;

@@ -24,4 +23,4 @@ fetch?: (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>;

getCustomUserAgent(): string;
enable(): void;
disable(): void;
enable(): Promise<void>;
disable(): Promise<void>;
debug(enabled?: boolean): void;

@@ -72,8 +71,8 @@ capture({ distinctId, event, properties, groups, sendFeatureFlags, timestamp, disableGeoip, uuid, }: EventMessage): void;

disableGeoip?: boolean;
}): Promise<PosthogFlagsAndPayloadsResponse>;
}): Promise<PostHogFlagsAndPayloadsResponse>;
groupIdentify({ groupType, groupKey, properties, distinctId, disableGeoip }: GroupIdentifyMessage): void;
reloadFeatureFlags(): Promise<void>;
shutdown(): void;
shutdownAsync(): Promise<void>;
shutdown(shutdownTimeoutMs?: number): void;
shutdownAsync(shutdownTimeoutMs?: number): Promise<void>;
private addLocalPersonAndGroupProperties;
}

@@ -175,4 +175,6 @@ import { JsonType } from '../../posthog-core/src';

* a clean shutdown.
*
* @param shutdownTimeoutMs The shutdown timeout, in milliseconds. Defaults to 30000 (30s).
*/
shutdown(): void;
shutdown(shutdownTimeoutMs?: number): void;
};
{
"name": "posthog-node",
"version": "3.6.3",
"version": "4.0.0-beta.1",
"description": "PostHog Node.js integration",

@@ -5,0 +5,0 @@ "repository": {

@@ -5,7 +5,8 @@ import { version } from '../package.json'

JsonType,
PosthogCoreOptions,
PostHogCoreOptions,
PostHogCoreStateless,
PostHogDecideResponse,
PostHogFetchOptions,
PostHogFetchResponse,
PosthogFlagsAndPayloadsResponse,
PostHogFlagsAndPayloadsResponse,
PostHogPersistedProperty,

@@ -18,9 +19,7 @@ } from '../../posthog-core/src'

export type PostHogOptions = PosthogCoreOptions & {
export type PostHogOptions = PostHogCoreOptions & {
persistence?: 'memory'
personalApiKey?: string
// The interval in milliseconds between polls for refreshing feature flag definitions
// The interval in milliseconds between polls for refreshing feature flag definitions. Defaults to 30 seconds.
featureFlagsPollingInterval?: number
// Timeout in milliseconds for any calls. Defaults to 10 seconds.
requestTimeout?: number
// Maximum size of cache that deduplicates $feature_flag_called calls per user.

@@ -92,7 +91,7 @@ maxCacheSize?: number

enable(): void {
enable(): Promise<void> {
return super.optIn()
}
disable(): void {
disable(): Promise<void> {
return super.optOut()

@@ -120,2 +119,10 @@ }

const _getFlags = (
distinctId: EventMessage['distinctId'],
groups: EventMessage['groups'],
disableGeoip: EventMessage['disableGeoip']
): Promise<PostHogDecideResponse['featureFlags'] | undefined> => {
return super.getFeatureFlagsStateless(distinctId, groups, undefined, undefined, disableGeoip)
}
// :TRICKY: If we flush, or need to shut down, to not lose events we want this promise to resolve before we flush

@@ -126,3 +133,4 @@ const capturePromise = Promise.resolve()

// If we are sending feature flags, we need to make sure we have the latest flags
return await super.getFeatureFlagsStateless(distinctId, groups, undefined, undefined, disableGeoip)
// return await super.getFeatureFlagsStateless(distinctId, groups, undefined, undefined, disableGeoip)
return await _getFlags(distinctId, groups, disableGeoip)
}

@@ -389,3 +397,3 @@

}
): Promise<PosthogFlagsAndPayloadsResponse> {
): Promise<PostHogFlagsAndPayloadsResponse> {
const { groups, disableGeoip } = options || {}

@@ -454,9 +462,9 @@ let { onlyEvaluateLocally, personProperties, groupProperties } = options || {}

shutdown(): void {
void this.shutdownAsync()
shutdown(shutdownTimeoutMs?: number): void {
void this.shutdownAsync(shutdownTimeoutMs)
}
async shutdownAsync(): Promise<void> {
async shutdownAsync(shutdownTimeoutMs?: number): Promise<void> {
this.featureFlagsPoller?.stopPoller()
return super.shutdownAsync()
return super.shutdownAsync(shutdownTimeoutMs)
}

@@ -463,0 +471,0 @@

@@ -201,4 +201,6 @@ import { JsonType } from '../../posthog-core/src'

* a clean shutdown.
*
* @param shutdownTimeoutMs The shutdown timeout, in milliseconds. Defaults to 30000 (30s).
*/
shutdown(): void
shutdown(shutdownTimeoutMs?: number): void
}

@@ -5,3 +5,3 @@ // import { PostHog } from '../'

import fetch from '../src/fetch'
import { anyDecideCall, anyLocalEvalCall, apiImplementation } from './feature-flags.spec'
import { anyDecideCall, anyLocalEvalCall, apiImplementation } from './test-utils'
import { waitForPromises, wait } from '../../posthog-core/test/test-utils/test-utils'

@@ -264,3 +264,2 @@ import { randomUUID } from 'crypto'

batchEvents = getLastBatchEvents()
console.warn(batchEvents)
expect(batchEvents?.[0].properties).toEqual({

@@ -267,0 +266,0 @@ $groups: { org: 123 },

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc