@statsig/client-core
Advanced tools
Comparing version 0.0.1-beta.9 to 0.0.1-beta.10
{ | ||
"name": "@statsig/client-core", | ||
"version": "0.0.1-beta.9", | ||
"version": "0.0.1-beta.10", | ||
"dependencies": {}, | ||
@@ -5,0 +5,0 @@ "type": "commonjs", |
import { EvaluationOptions } from './StatsigClientBase'; | ||
import { StatsigClientEventEmitterInterface } from './StatsigClientEventEmitter'; | ||
import { StatsigDataAdapter } from './StatsigDataAdapter'; | ||
import { StatsigEvent } from './StatsigEvent'; | ||
@@ -10,2 +11,3 @@ import { DynamicConfig, Experiment, FeatureGate, Layer } from './StatsigTypes'; | ||
shutdown(): Promise<void>; | ||
getDataAdapter(): StatsigDataAdapter; | ||
} | ||
@@ -12,0 +14,0 @@ export interface OnDeviceEvaluationsInterface extends StatsigClientCommonInterface { |
@@ -8,3 +8,4 @@ import { StatsigClientEmitEventFunc } from './StatsigClientBase'; | ||
capture(tag: string, task: () => unknown, emitter?: StatsigClientEmitEventFunc): unknown; | ||
logError(tag: string, error: unknown): void; | ||
private _onError; | ||
} |
@@ -73,2 +73,5 @@ "use strict"; | ||
}; | ||
ErrorBoundary.prototype.logError = function (tag, error) { | ||
this._onError(tag, error); | ||
}; | ||
ErrorBoundary.prototype._onError = function (tag, error, emitter) { | ||
@@ -75,0 +78,0 @@ var _this = this; |
@@ -34,3 +34,2 @@ import { NetworkCore } from './NetworkCore'; | ||
private _sendEventsViaBeacon; | ||
private _isBeaconSupported; | ||
private _saveFailedLogsToStorage; | ||
@@ -37,0 +36,0 @@ private _retryFailedLogs; |
@@ -195,3 +195,3 @@ "use strict"; | ||
api = (_b = (_a = this._options) === null || _a === void 0 ? void 0 : _a.api) !== null && _b !== void 0 ? _b : DEFAULT_API; | ||
if (!(!isInForeground && this._isBeaconSupported())) return [3 /*break*/, 2]; | ||
if (!(!isInForeground && this._network.isBeaconSupported())) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this._sendEventsViaBeacon(api, events)]; | ||
@@ -273,6 +273,2 @@ case 1: | ||
}; | ||
EventLogger.prototype._isBeaconSupported = function () { | ||
return (typeof navigator !== 'undefined' && | ||
typeof (navigator === null || navigator === void 0 ? void 0 : navigator.sendBeacon) === 'function'); | ||
}; | ||
EventLogger.prototype._saveFailedLogsToStorage = function (events) { | ||
@@ -279,0 +275,0 @@ var _a; |
/** Statsig Global should go first */ | ||
import './$_StatsigGlobal'; | ||
import { EventLogger } from './EventLogger'; | ||
import { Log } from './Log'; | ||
import { Storage } from './StorageProvider'; | ||
export * from './$_StatsigGlobal'; | ||
export * from './ClientInterfaces'; | ||
export * from './DataAdapterCore'; | ||
export * from './ErrorBoundary'; | ||
export * from './Hashing'; | ||
export * from './StorageProvider'; | ||
export * from './Log'; | ||
@@ -21,4 +23,5 @@ export * from './Monitoring'; | ||
export * from './StatsigUser'; | ||
export * from './StorageProvider'; | ||
export * from './UUID'; | ||
export * from './VisibilityChangeObserver'; | ||
export { EventLogger }; | ||
export { EventLogger, Storage, Log }; |
@@ -28,3 +28,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EventLogger = void 0; | ||
exports.Log = exports.Storage = exports.EventLogger = void 0; | ||
/** Statsig Global should go first */ | ||
@@ -35,7 +35,10 @@ require("./$_StatsigGlobal"); | ||
var Log_1 = require("./Log"); | ||
Object.defineProperty(exports, "Log", { enumerable: true, get: function () { return Log_1.Log; } }); | ||
var StorageProvider_1 = require("./StorageProvider"); | ||
Object.defineProperty(exports, "Storage", { enumerable: true, get: function () { return StorageProvider_1.Storage; } }); | ||
__exportStar(require("./$_StatsigGlobal"), exports); | ||
__exportStar(require("./ClientInterfaces"), exports); | ||
__exportStar(require("./DataAdapterCore"), exports); | ||
__exportStar(require("./ErrorBoundary"), exports); | ||
__exportStar(require("./Hashing"), exports); | ||
__exportStar(require("./StorageProvider"), exports); | ||
__exportStar(require("./Log"), exports); | ||
@@ -53,4 +56,5 @@ __exportStar(require("./Monitoring"), exports); | ||
__exportStar(require("./StatsigUser"), exports); | ||
__exportStar(require("./StorageProvider"), exports); | ||
__exportStar(require("./UUID"), exports); | ||
__exportStar(require("./VisibilityChangeObserver"), exports); | ||
__STATSIG__ = __assign(__assign({}, (__STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {})), { EventLogger: EventLogger_1.EventLogger, Log: Log_1.Log }); | ||
__STATSIG__ = __assign(__assign({}, (__STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {})), { EventLogger: EventLogger_1.EventLogger, Log: Log_1.Log, Storage: StorageProvider_1.Storage }); |
import { ErrorBoundary } from './ErrorBoundary'; | ||
export declare function monitorClass<T extends new (...args: any[]) => any>(errorBoundary: ErrorBoundary, target: T, instance: unknown): void; | ||
export declare function monitorFunction<T>(errorBoundary: ErrorBoundary, tag: string, func: () => T, instance: unknown): T; | ||
export declare function monitorClass(errorBoundary: ErrorBoundary, instance: object): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.monitorFunction = exports.monitorClass = void 0; | ||
exports.monitorClass = void 0; | ||
var Diagnostics_1 = require("./Diagnostics"); | ||
var StatsigClientBase_1 = require("./StatsigClientBase"); | ||
function monitorClass(errorBoundary, target, instance) { | ||
var methods = Object.getOwnPropertyNames(target.prototype); | ||
function monitorClass(errorBoundary, instance) { | ||
try { | ||
_monitorClassImpl(errorBoundary, instance); | ||
} | ||
catch (error) { | ||
errorBoundary.logError('monitorClass', error); | ||
} | ||
} | ||
exports.monitorClass = monitorClass; | ||
function _monitorFunction(errorBoundary, tag, func, instance) { | ||
var client = instance instanceof StatsigClientBase_1.StatsigClientBase ? instance['emit'] : undefined; | ||
return errorBoundary.capture(tag, function () { return (0, Diagnostics_1.captureDiagnostics)(tag, function () { return func.apply(instance); }); }, client); | ||
} | ||
function _getProtoSafe(instance) { | ||
if (typeof instance === 'object') { | ||
var proto = Object.getPrototypeOf(instance); | ||
return proto && typeof proto === 'object' | ||
? proto | ||
: null; | ||
} | ||
return null; | ||
} | ||
function _getAllInstanceMethodNames(instance) { | ||
var names = new Set(); | ||
var proto = _getProtoSafe(instance); | ||
while (proto && proto !== Object.prototype) { | ||
Object.getOwnPropertyNames(proto) | ||
.filter(function (prop) { return typeof (proto === null || proto === void 0 ? void 0 : proto[prop]) === 'function'; }) | ||
.forEach(function (name) { return names.add(name); }); | ||
proto = Object.getPrototypeOf(proto); | ||
} | ||
return Array.from(names); | ||
} | ||
function _getAllStaticMethodNames(instance) { | ||
var names = new Set(); | ||
var proto = _getProtoSafe(instance); | ||
Object.getOwnPropertyNames((proto === null || proto === void 0 ? void 0 : proto.constructor) || {}) | ||
.filter(function (prop) { | ||
var _a; | ||
return typeof ((_a = proto === null || proto === void 0 ? void 0 : proto.constructor) === null || _a === void 0 ? void 0 : _a[prop]) === 'function'; | ||
}) | ||
.forEach(function (name) { return names.add(name); }); | ||
return Array.from(names); | ||
} | ||
function _monitorClassImpl(errorBoundary, instance) { | ||
var _a; | ||
var obj = instance; | ||
var _loop_1 = function (method) { | ||
if (method === 'constructor' || typeof obj[method] !== 'function') { | ||
if (method === 'constructor') { | ||
return "continue"; | ||
@@ -20,15 +64,24 @@ } | ||
} | ||
return monitorFunction(errorBoundary, method, function () { return original.apply(_this, args); }, instance); | ||
return _monitorFunction(errorBoundary, method, function () { return original.apply(_this, args); }, instance); | ||
}; | ||
}; | ||
for (var _i = 0, methods_1 = methods; _i < methods_1.length; _i++) { | ||
var method = methods_1[_i]; | ||
for (var _i = 0, _b = _getAllInstanceMethodNames(obj); _i < _b.length; _i++) { | ||
var method = _b[_i]; | ||
_loop_1(method); | ||
} | ||
var _loop_2 = function (method) { | ||
var original = (_a = obj === null || obj === void 0 ? void 0 : obj.constructor) === null || _a === void 0 ? void 0 : _a[method]; | ||
(obj === null || obj === void 0 ? void 0 : obj.constructor)[method] = | ||
function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return _monitorFunction(errorBoundary, "".concat(obj.constructor.name, ".").concat(method), function () { return original.apply(obj.constructor, args); }, instance); | ||
}; | ||
}; | ||
for (var _c = 0, _d = _getAllStaticMethodNames(obj); _c < _d.length; _c++) { | ||
var method = _d[_c]; | ||
_loop_2(method); | ||
} | ||
} | ||
exports.monitorClass = monitorClass; | ||
function monitorFunction(errorBoundary, tag, func, instance) { | ||
var client = instance instanceof StatsigClientBase_1.StatsigClientBase ? instance['emit'] : undefined; | ||
return errorBoundary.capture(tag, function () { return (0, Diagnostics_1.captureDiagnostics)(tag, function () { return func.apply(instance); }); }, client); | ||
} | ||
exports.monitorFunction = monitorFunction; |
@@ -25,2 +25,3 @@ import { StatsigClientEmitEventFunc } from './StatsigClientBase'; | ||
get(args: RequestArgs): Promise<NetworkResponse | null>; | ||
isBeaconSupported(): boolean; | ||
beacon(args: RequestArgsWithData): Promise<boolean>; | ||
@@ -27,0 +28,0 @@ private _sendRequest; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || function () { | ||
@@ -72,11 +57,2 @@ __assign = Object.assign || function(t) { | ||
var DEFAULT_TIMEOUT_MS = 10000; | ||
var NetworkError = /** @class */ (function (_super) { | ||
__extends(NetworkError, _super); | ||
function NetworkError(message, errorDescription) { | ||
var _this = _super.call(this, message) || this; | ||
_this.errorDescription = errorDescription; | ||
return _this; | ||
} | ||
return NetworkError; | ||
}(Error)); | ||
var NetworkCore = /** @class */ (function () { | ||
@@ -109,2 +85,6 @@ function NetworkCore(_options, _emitter) { | ||
}; | ||
NetworkCore.prototype.isBeaconSupported = function () { | ||
return (typeof navigator !== 'undefined' && | ||
typeof (navigator === null || navigator === void 0 ? void 0 : navigator.sendBeacon) === 'function'); | ||
}; | ||
NetworkCore.prototype.beacon = function (args) { | ||
@@ -128,3 +108,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var method, body, retries, controller, handle, response, url, text, error_1, errorMessage; | ||
var method, body, retries, controller, handle, response, url, text, err, error_1, errorMessage; | ||
var _this = this; | ||
@@ -155,3 +135,5 @@ return __generator(this, function (_b) { | ||
if (!response.ok) { | ||
throw new NetworkError('Fetch Failure', text); | ||
err = new Error("Failed to fetch: ".concat(url, " ").concat(text)); | ||
err.name = 'NetworkError'; | ||
throw err; | ||
} | ||
@@ -176,3 +158,3 @@ Diagnostics_1.Diagnostics.mark('_sendRequest:response-received', { | ||
(_a = this._emitter) === null || _a === void 0 ? void 0 : _a.call(this, { event: 'error', error: error_1 }); | ||
Log_1.Log.error('A networking error occured.', errorMessage); | ||
Log_1.Log.error('A networking error occured.', errorMessage, error_1); | ||
return [2 /*return*/, null]; | ||
@@ -188,15 +170,10 @@ } | ||
var metadata = StatsigMetadata_1.StatsigMetadataProvider.get(); | ||
var url = new URL(args.url); | ||
url.searchParams.append('k', args.sdkKey); | ||
url.searchParams.append('st', metadata.sdkType); | ||
url.searchParams.append('sv', metadata.sdkVersion); | ||
url.searchParams.append('t', String(Date.now())); | ||
url.searchParams.append('sid', SessionID_1.SessionID.get(args.sdkKey)); | ||
if (args.params) { | ||
Object.entries(args.params).forEach(function (_a) { | ||
var k = _a[0], v = _a[1]; | ||
url.searchParams.append(k, v); | ||
}); | ||
} | ||
return url.toString(); | ||
var params = __assign(__assign({}, args.params), { k: args.sdkKey, st: metadata.sdkType, sv: metadata.sdkVersion, t: String(Date.now()), sid: SessionID_1.SessionID.get(args.sdkKey) }); | ||
var query = Object.entries(params) | ||
.map(function (_a) { | ||
var key = _a[0], value = _a[1]; | ||
return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value)); | ||
}) | ||
.join('&'); | ||
return "".concat(args.url).concat(query ? "?".concat(query) : ''); | ||
}; | ||
@@ -203,0 +180,0 @@ NetworkCore.prototype._getPopulatedBody = function (args) { |
@@ -27,5 +27,5 @@ import './$_StatsigGlobal'; | ||
protected emit(data: StatsigClientEventData): void; | ||
protected _setStatus(newStatus: StatsigLoadingStatus): void; | ||
protected _setStatus(newStatus: StatsigLoadingStatus, values: StatsigDataAdapterResult | null): void; | ||
protected _enqueueExposure(options: EvaluationOptions, exposure: StatsigEventInternal): void; | ||
protected _runPostUpdate(current: StatsigDataAdapterResult | null, user?: StatsigUser): void; | ||
} |
@@ -55,5 +55,5 @@ "use strict"; | ||
}; | ||
StatsigClientBase.prototype._setStatus = function (newStatus) { | ||
StatsigClientBase.prototype._setStatus = function (newStatus, values) { | ||
this.loadingStatus = newStatus; | ||
this.emit({ event: 'status_change', loadingStatus: newStatus }); | ||
this.emit({ event: 'values_updated', status: newStatus, values: values }); | ||
}; | ||
@@ -60,0 +60,0 @@ StatsigClientBase.prototype._enqueueExposure = function (options, exposure) { |
@@ -0,1 +1,2 @@ | ||
import { StatsigDataAdapterResult } from './StatsigDataAdapter'; | ||
import { DynamicConfig, Experiment, FeatureGate, Layer } from './StatsigTypes'; | ||
@@ -6,3 +7,3 @@ export type StatsigLoadingStatus = 'Uninitialized' | 'Loading' | 'Ready'; | ||
* | ||
* `status_change` - When the Statsig clients internal values change as the result of an initialize/update operation. | ||
* `values_updated` - When the Statsig clients internal values change as the result of an initialize/update operation. | ||
* | ||
@@ -21,3 +22,3 @@ * `error` - When an unexpected error occurs within the Statsig client. | ||
*/ | ||
export type StatsigClientEvent = 'status_change' | 'error' | 'logs_flushed' | 'gate_evaluation' | 'dynamic_config_evaluation' | 'experiment_evaluation' | 'layer_evaluation'; | ||
export type StatsigClientEvent = 'values_updated' | 'error' | 'logs_flushed' | 'gate_evaluation' | 'dynamic_config_evaluation' | 'experiment_evaluation' | 'layer_evaluation'; | ||
/** | ||
@@ -29,4 +30,5 @@ * Type representing various events emitted by a Statsig client. | ||
} | { | ||
event: 'status_change'; | ||
loadingStatus: StatsigLoadingStatus; | ||
event: 'values_updated'; | ||
status: StatsigLoadingStatus; | ||
values: StatsigDataAdapterResult | null; | ||
} | { | ||
@@ -33,0 +35,0 @@ event: 'error'; |
import { StatsigOptionsCommon } from './StatsigOptionsCommon'; | ||
import { StatsigUser } from './StatsigUser'; | ||
export type DataSource = 'Uninitialized' | 'Loading' | 'NoValues' | 'Cache' | 'Network' | 'NetworkNotModified' | 'Bootstrap'; | ||
export type DataSource = 'Uninitialized' | 'Loading' | 'NoValues' | 'Cache' | 'Network' | 'NetworkNotModified' | 'Bootstrap' | 'Prefetch'; | ||
export type StatsigDataAdapterResult = { | ||
readonly source: DataSource; | ||
readonly data: string; | ||
readonly receivedAt: number; | ||
}; | ||
export declare const DataAdapterCachePrefix = "statsig.cached"; | ||
/** | ||
@@ -12,3 +14,3 @@ * Describes a type that is used during intialize/update operations of a Statsig client. | ||
* See below to find the default adapters, but know that it is possible to create your | ||
* own StatsigDataAdapter and provide it via {@link StatsigOptions.dataAdapter}. | ||
* own StatsigDataAdapter and provide it via {@link StatsigOptionsCommon.dataAdapter}. | ||
* | ||
@@ -22,2 +24,3 @@ * Defaults: | ||
export type StatsigDataAdapter = { | ||
readonly _setInMemoryCache: (cache: Record<string, StatsigDataAdapterResult>) => void; | ||
/** | ||
@@ -24,0 +27,0 @@ * Called when the StatsigDataAdapter is attached to the Statsig client instance during construction. |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DataAdapterCachePrefix = void 0; | ||
exports.DataAdapterCachePrefix = 'statsig.cached'; |
@@ -15,3 +15,3 @@ "use strict"; | ||
exports.StatsigMetadataProvider = void 0; | ||
var SDK_VERSION = '0.0.1-beta.9'; | ||
var SDK_VERSION = '0.0.1-beta.10'; | ||
var metadata = { | ||
@@ -18,0 +18,0 @@ sdkVersion: SDK_VERSION, |
type StorageProvider = { | ||
getProviderName: () => string; | ||
getItem: (key: string) => Promise<string | null>; | ||
setItem: (key: string, value: string) => Promise<void>; | ||
removeItem: (key: string) => Promise<void>; | ||
getAllKeys: () => Promise<readonly string[]>; | ||
}; | ||
@@ -6,0 +8,0 @@ export declare const Storage: StorageProvider & { |
@@ -43,2 +43,3 @@ "use strict"; | ||
var provider = { | ||
getProviderName: function () { return 'InMemory'; }, | ||
getItem: function (key) { | ||
@@ -56,2 +57,5 @@ var _a; | ||
}, | ||
getAllKeys: function () { | ||
return Promise.resolve(Object.keys(inMemoryStore)); | ||
}, | ||
}; | ||
@@ -61,2 +65,3 @@ try { | ||
provider = { | ||
getProviderName: function () { return 'LocalStorage'; }, | ||
getItem: function (key) { | ||
@@ -73,2 +78,6 @@ return Promise.resolve(localStorage.getItem(key)); | ||
}, | ||
getAllKeys: function () { | ||
var keys = Object.keys(localStorage); | ||
return Promise.resolve(keys); | ||
}, | ||
}; | ||
@@ -81,5 +90,7 @@ } | ||
exports.Storage = { | ||
getProviderName: function () { return provider.getProviderName(); }, | ||
getItem: function (key) { return provider.getItem(key); }, | ||
setItem: function (key, value) { return provider.setItem(key, value); }, | ||
removeItem: function (key) { return provider.removeItem(key); }, | ||
getAllKeys: function () { return provider.getAllKeys(); }, | ||
setProvider: function (newProvider) { | ||
@@ -86,0 +97,0 @@ provider = newProvider; |
99343
52
2252