@statsig/client-core
Advanced tools
Comparing version 0.0.1-beta.40 to 0.0.1-beta.41
{ | ||
"name": "@statsig/client-core", | ||
"version": "0.0.1-beta.40", | ||
"version": "0.0.1-beta.41", | ||
"dependencies": {}, | ||
@@ -5,0 +5,0 @@ "type": "commonjs", |
@@ -6,3 +6,4 @@ import { DataAdapterAsyncOptions, DataAdapterResult } from './StatsigDataAdapter'; | ||
private _adapterName; | ||
private _cacheSuffix; | ||
protected _cacheSuffix: string; | ||
protected _options: AnyStatsigOptions | null; | ||
private _sdkKey; | ||
@@ -12,3 +13,3 @@ private _inMemoryCache; | ||
protected constructor(_adapterName: string, _cacheSuffix: string); | ||
attach(sdkKey: string, _options: AnyStatsigOptions | null): void; | ||
attach(sdkKey: string, options: AnyStatsigOptions | null): void; | ||
getDataSync(user?: StatsigUser | undefined): DataAdapterResult | null; | ||
@@ -25,5 +26,5 @@ setData(data: string, user?: StatsigUser): void; | ||
protected abstract _fetchFromNetwork(current: string | null, user?: StatsigUser, options?: DataAdapterAsyncOptions): Promise<string | null>; | ||
protected abstract _getCacheKey(user?: StatsigUser): string; | ||
private _fetchAndPrepFromNetwork; | ||
protected _getSdkKey(): string; | ||
protected _getCacheKey(user?: StatsigUser): string; | ||
protected _addToInMemoryCache(cacheKey: string, result: DataAdapterResult): void; | ||
@@ -30,0 +31,0 @@ private _loadFromCache; |
@@ -14,3 +14,2 @@ "use strict"; | ||
const Log_1 = require("./Log"); | ||
const StatsigDataAdapter_1 = require("./StatsigDataAdapter"); | ||
const StatsigUser_1 = require("./StatsigUser"); | ||
@@ -24,2 +23,3 @@ const StorageProvider_1 = require("./StorageProvider"); | ||
this._cacheSuffix = _cacheSuffix; | ||
this._options = null; | ||
this._sdkKey = null; | ||
@@ -29,4 +29,5 @@ this._inMemoryCache = {}; | ||
} | ||
attach(sdkKey, _options) { | ||
attach(sdkKey, options) { | ||
this._sdkKey = sdkKey; | ||
this._options = options; | ||
} | ||
@@ -47,3 +48,5 @@ getDataSync(user) { | ||
setData(data, user) { | ||
const cacheKey = this._getCacheKey(user); | ||
var _a; | ||
const normalized = user && (0, StatsigUser_1._normalizeUser)(user, (_a = this._options) === null || _a === void 0 ? void 0 : _a.environment); | ||
const cacheKey = this._getCacheKey(normalized); | ||
this._addToInMemoryCache(cacheKey, { | ||
@@ -123,6 +126,2 @@ source: 'Bootstrap', | ||
} | ||
_getCacheKey(user) { | ||
const key = (0, StatsigUser_1.getUserStorageKey)(this._getSdkKey(), user); | ||
return `${StatsigDataAdapter_1.DataAdapterCachePrefix}.${this._cacheSuffix}.${key}`; | ||
} | ||
_addToInMemoryCache(cacheKey, result) { | ||
@@ -129,0 +128,0 @@ const entries = Object.entries(this._inMemoryCache); |
@@ -11,3 +11,3 @@ import { NetworkCore } from './NetworkCore'; | ||
private _queue; | ||
private _flushTimer; | ||
private _flushIntervalId; | ||
private _lastExposureTimeMap; | ||
@@ -14,0 +14,0 @@ private _nonExposedChecks; |
@@ -19,3 +19,2 @@ "use strict"; | ||
const StorageProvider_1 = require("./StorageProvider"); | ||
const TypedJsonParse_1 = require("./TypedJsonParse"); | ||
const UrlOverrides_1 = require("./UrlOverrides"); | ||
@@ -54,9 +53,10 @@ const VisibilityObserving_1 = require("./VisibilityObserving"); | ||
const logger = EVENT_LOGGER_MAP[_sdkKey]; | ||
if (!logger) { | ||
if (logger._flushIntervalId !== intervalId) { | ||
clearInterval(intervalId); | ||
return; | ||
} | ||
_safeFlushAndForget(_sdkKey); | ||
else { | ||
_safeFlushAndForget(_sdkKey); | ||
} | ||
}, flushInterval); | ||
this._flushTimer = intervalId; | ||
this._flushIntervalId = intervalId; | ||
const config = _options === null || _options === void 0 ? void 0 : _options.networkConfig; | ||
@@ -95,5 +95,5 @@ this._logEventUrl = (0, UrlOverrides_1._getOverridableUrl)(config === null || config === void 0 ? void 0 : config.logEventUrl, config === null || config === void 0 ? void 0 : config.api, '/rgstr', NetworkConfig_1.NetworkDefault.eventsApi); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this._flushTimer) { | ||
clearInterval(this._flushTimer); | ||
this._flushTimer = null; | ||
if (this._flushIntervalId) { | ||
clearInterval(this._flushIntervalId); | ||
this._flushIntervalId = null; | ||
} | ||
@@ -182,2 +182,3 @@ yield this.flush(); | ||
_sendEventsViaPost(events) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -195,6 +196,4 @@ const result = yield this._network.post({ | ||
}); | ||
const response = (result === null || result === void 0 ? void 0 : result.body) | ||
? (0, TypedJsonParse_1.typedJsonParse)(result.body, 'success', 'Failed to parse SendEventsResponse') | ||
: null; | ||
return { success: (response === null || response === void 0 ? void 0 : response.success) === true }; | ||
const code = (_a = result === null || result === void 0 ? void 0 : result.code) !== null && _a !== void 0 ? _a : -1; | ||
return { success: code >= 200 && code < 300 }; | ||
}); | ||
@@ -201,0 +200,0 @@ } |
@@ -7,2 +7,3 @@ /** Statsig Global should go first */ | ||
export * from './$_StatsigGlobal'; | ||
export * from './CacheKey'; | ||
export * from './ClientInterfaces'; | ||
@@ -9,0 +10,0 @@ export * from './DataAdapterCore'; |
@@ -29,2 +29,3 @@ "use strict"; | ||
__exportStar(require("./$_StatsigGlobal"), exports); | ||
__exportStar(require("./CacheKey"), exports); | ||
__exportStar(require("./ClientInterfaces"), exports); | ||
@@ -31,0 +32,0 @@ __exportStar(require("./DataAdapterCore"), exports); |
@@ -22,2 +22,3 @@ "use strict"; | ||
const StatsigMetadata_1 = require("./StatsigMetadata"); | ||
const VisibilityObserving_1 = require("./VisibilityObserving"); | ||
const DEFAULT_TIMEOUT_MS = 10000; | ||
@@ -69,2 +70,3 @@ class NetworkCore { | ||
let response = null; | ||
const keepalive = (0, VisibilityObserving_1._isUnloading)(); | ||
try { | ||
@@ -77,3 +79,3 @@ const config = { | ||
priority: args.priority, | ||
keepalive: true, | ||
keepalive, | ||
}; | ||
@@ -85,3 +87,3 @@ const func = (_e = (_d = (_c = this._options) === null || _c === void 0 ? void 0 : _c.networkConfig) === null || _d === void 0 ? void 0 : _d.networkOverrideFunc) !== null && _e !== void 0 ? _e : fetch; | ||
const text = yield response.text().catch(() => 'No Text'); | ||
const err = new Error(`Failed to fetch: ${url} ${text}`); | ||
const err = new Error(`NetworkError: ${url} ${text}`); | ||
err.name = 'NetworkError'; | ||
@@ -88,0 +90,0 @@ throw err; |
@@ -13,3 +13,3 @@ "use strict"; | ||
exports.StatsigSession = exports.SessionID = void 0; | ||
const Hashing_1 = require("./Hashing"); | ||
const CacheKey_1 = require("./CacheKey"); | ||
const Log_1 = require("./Log"); | ||
@@ -84,3 +84,3 @@ const StorageProvider_1 = require("./StorageProvider"); | ||
function _getSessionIDStorageKey(sdkKey) { | ||
return `statsig.session_id.${(0, Hashing_1.DJB2)(sdkKey)}`; | ||
return `statsig.session_id.${(0, CacheKey_1._getStorageKey)(sdkKey)}`; | ||
} | ||
@@ -87,0 +87,0 @@ function _persistToStorage(session, sdkKey) { |
@@ -13,3 +13,3 @@ "use strict"; | ||
exports.StableID = void 0; | ||
const Hashing_1 = require("./Hashing"); | ||
const CacheKey_1 = require("./CacheKey"); | ||
const Log_1 = require("./Log"); | ||
@@ -39,3 +39,3 @@ const StorageProvider_1 = require("./StorageProvider"); | ||
function _getStableIDStorageKey(sdkKey) { | ||
return `statsig.stable_id.${(0, Hashing_1.DJB2)(sdkKey)}`; | ||
return `statsig.stable_id.${(0, CacheKey_1._getStorageKey)(sdkKey)}`; | ||
} | ||
@@ -42,0 +42,0 @@ function _persistToStorage(stableID, sdkKey) { |
@@ -66,2 +66,3 @@ import './$_StatsigGlobal'; | ||
protected _enqueueExposure(name: string, exposure: StatsigEventInternal, options?: EvaluationOptionsCommon): void; | ||
protected abstract _primeReadyRipcord(): void; | ||
} |
@@ -27,6 +27,3 @@ "use strict"; | ||
const emitter = this.$emt.bind(this); | ||
const statsigGlobal = (0, __StatsigGlobal_1._getStatsigGlobal)(); | ||
const instances = (_a = statsigGlobal.instances) !== null && _a !== void 0 ? _a : {}; | ||
const inst = this; | ||
(options === null || options === void 0 ? void 0 : options.logLevel) && (Log_1.Log.level = options === null || options === void 0 ? void 0 : options.logLevel); | ||
(options === null || options === void 0 ? void 0 : options.logLevel) != null && (Log_1.Log.level = options.logLevel); | ||
(options === null || options === void 0 ? void 0 : options.disableStorage) && StorageProvider_1.Storage._setDisabled(true); | ||
@@ -41,13 +38,19 @@ (options === null || options === void 0 ? void 0 : options.overrideStableID) && | ||
this._options = options !== null && options !== void 0 ? options : {}; | ||
this._overrideAdapter = (_b = options === null || options === void 0 ? void 0 : options.overrideAdapter) !== null && _b !== void 0 ? _b : null; | ||
this._overrideAdapter = (_a = options === null || options === void 0 ? void 0 : options.overrideAdapter) !== null && _a !== void 0 ? _a : null; | ||
this._logger = new EventLogger_1.EventLogger(sdkKey, emitter, network, options); | ||
if (instances[sdkKey] != null && (0, SafeJs_1._isBrowserEnv)()) { | ||
Log_1.Log.warn('Creating multiple Statsig clients with the same SDK key can lead to unexpected behavior. Multi-instance support requires different SDK keys.'); | ||
if ((0, SafeJs_1._isBrowserEnv)()) { | ||
const statsigGlobal = (0, __StatsigGlobal_1._getStatsigGlobal)(); | ||
const instances = (_b = statsigGlobal.instances) !== null && _b !== void 0 ? _b : {}; | ||
const inst = this; | ||
if (instances[sdkKey] != null) { | ||
Log_1.Log.warn('Creating multiple Statsig clients with the same SDK key can lead to unexpected behavior. Multi-instance support requires different SDK keys.'); | ||
} | ||
instances[sdkKey] = inst; | ||
statsigGlobal.lastInstance = inst; | ||
statsigGlobal.instances = instances; | ||
__STATSIG__ = statsigGlobal; | ||
} | ||
instances[sdkKey] = inst; | ||
statsigGlobal.lastInstance = inst; | ||
statsigGlobal.instances = instances; | ||
__STATSIG__ = statsigGlobal; | ||
this.dataAdapter = adapter; | ||
this.dataAdapter.attach(sdkKey, options); | ||
this._primeReadyRipcord(); | ||
} | ||
@@ -54,0 +57,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
export declare const SDK_VERSION = "0.0.1-beta.40"; | ||
export declare const SDK_VERSION = "0.0.1-beta.41"; | ||
export type StatsigMetadata = { | ||
@@ -3,0 +3,0 @@ readonly [key: string]: string | undefined; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.StatsigMetadataProvider = exports.SDK_VERSION = void 0; | ||
exports.SDK_VERSION = '0.0.1-beta.40'; | ||
exports.SDK_VERSION = '0.0.1-beta.41'; | ||
let metadata = { | ||
@@ -6,0 +6,0 @@ sdkVersion: exports.SDK_VERSION, |
@@ -18,5 +18,4 @@ import type { StatsigEnvironment } from './StatsigOptionsCommon'; | ||
}; | ||
export declare function normalizeUser(original: StatsigUser, environment?: StatsigEnvironment): StatsigUser; | ||
export declare function getUserStorageKey(sdkKey: string, user?: StatsigUser): string; | ||
export declare function _normalizeUser(original: StatsigUser, environment?: StatsigEnvironment): StatsigUser; | ||
export declare function getUnitIDFromUser(user: StatsigUser, idType: string): string | undefined; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getUnitIDFromUser = exports.getUserStorageKey = exports.normalizeUser = void 0; | ||
const Hashing_1 = require("./Hashing"); | ||
function normalizeUser(original, environment) { | ||
exports.getUnitIDFromUser = exports._normalizeUser = void 0; | ||
function _normalizeUser(original, environment) { | ||
try { | ||
@@ -17,7 +16,3 @@ const copy = JSON.parse(JSON.stringify(original)); | ||
} | ||
exports.normalizeUser = normalizeUser; | ||
function getUserStorageKey(sdkKey, user) { | ||
return (0, Hashing_1.DJB2Object)({ sdkKey, user }); | ||
} | ||
exports.getUserStorageKey = getUserStorageKey; | ||
exports._normalizeUser = _normalizeUser; | ||
function getUnitIDFromUser(user, idType) { | ||
@@ -24,0 +19,0 @@ var _a, _b, _c; |
@@ -11,5 +11,7 @@ "use strict"; | ||
}; | ||
var _a; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports._setObjectInStorage = exports._getObjectFromStorage = exports.Storage = void 0; | ||
const Log_1 = require("./Log"); | ||
const SafeJs_1 = require("./SafeJs"); | ||
const inMemoryStore = {}; | ||
@@ -41,21 +43,22 @@ const _resolve = (input) => Promise.resolve(input); | ||
try { | ||
if (typeof window !== 'undefined' && 'localStorage' in window) { | ||
const win = (0, SafeJs_1._getWindowSafe)(); | ||
if (typeof ((_a = win === null || win === void 0 ? void 0 : win.localStorage) === null || _a === void 0 ? void 0 : _a.getItem) === 'function') { | ||
_localStorageProvider = { | ||
_getProviderName: () => 'LocalStorage', | ||
_getItemSync(key) { | ||
return localStorage.getItem(key); | ||
return win.localStorage.getItem(key); | ||
}, | ||
_getItem(key) { | ||
return _resolve(localStorage.getItem(key)); | ||
return _resolve(win.localStorage.getItem(key)); | ||
}, | ||
_setItem(key, value) { | ||
localStorage.setItem(key, value); | ||
win.localStorage.setItem(key, value); | ||
return _resolve(); | ||
}, | ||
_removeItem(key) { | ||
localStorage.removeItem(key); | ||
win.localStorage.removeItem(key); | ||
return _resolve(); | ||
}, | ||
_getAllKeys() { | ||
const keys = Object.keys(localStorage); | ||
const keys = Object.keys(win.localStorage); | ||
return _resolve(keys); | ||
@@ -62,0 +65,0 @@ }, |
@@ -6,4 +6,5 @@ declare const FOREGROUND = "foreground"; | ||
export declare const _isCurrentlyVisible: () => boolean; | ||
export declare const _isUnloading: () => boolean; | ||
export declare const _subscribeToVisiblityChanged: (listener: VisibilityChangedCallback) => void; | ||
export declare const _notifyVisibilityChanged: (visibility: Visibility) => void; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports._notifyVisibilityChanged = exports._subscribeToVisiblityChanged = exports._isCurrentlyVisible = void 0; | ||
exports._notifyVisibilityChanged = exports._subscribeToVisiblityChanged = exports._isUnloading = exports._isCurrentlyVisible = void 0; | ||
const SafeJs_1 = require("./SafeJs"); | ||
@@ -9,2 +9,3 @@ const FOREGROUND = 'foreground'; | ||
let current = FOREGROUND; | ||
let isUnloading = false; | ||
const _isCurrentlyVisible = () => { | ||
@@ -14,2 +15,4 @@ return current === FOREGROUND; | ||
exports._isCurrentlyVisible = _isCurrentlyVisible; | ||
const _isUnloading = () => isUnloading; | ||
exports._isUnloading = _isUnloading; | ||
const _subscribeToVisiblityChanged = (listener) => { | ||
@@ -27,7 +30,13 @@ LISTENERS.unshift(listener); | ||
exports._notifyVisibilityChanged = _notifyVisibilityChanged; | ||
(0, SafeJs_1._addWindowEventListenerSafe)('focus', () => (0, exports._notifyVisibilityChanged)(FOREGROUND)); | ||
(0, SafeJs_1._addWindowEventListenerSafe)('focus', () => { | ||
isUnloading = false; | ||
(0, exports._notifyVisibilityChanged)(FOREGROUND); | ||
}); | ||
(0, SafeJs_1._addWindowEventListenerSafe)('blur', () => (0, exports._notifyVisibilityChanged)(BACKGROUND)); | ||
(0, SafeJs_1._addWindowEventListenerSafe)('beforeunload', () => (0, exports._notifyVisibilityChanged)(BACKGROUND)); | ||
(0, SafeJs_1._addWindowEventListenerSafe)('beforeunload', () => { | ||
isUnloading = true; | ||
(0, exports._notifyVisibilityChanged)(BACKGROUND); | ||
}); | ||
(0, SafeJs_1._addDocumentEventListenerSafe)('visibilitychange', () => { | ||
(0, exports._notifyVisibilityChanged)(document.visibilityState === 'visible' ? FOREGROUND : BACKGROUND); | ||
}); |
113938
74
2653