New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@amplitude/experiment-js-client

Package Overview
Dependencies
Maintainers
20
Versions
89
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@amplitude/experiment-js-client - npm Package Compare versions

Comparing version

to
1.9.0-beta.4

202

dist/experiment.es2015.js

@@ -240,3 +240,4 @@ import { AnalyticsConnector } from '@amplitude/analytics-connector';

const isFallback = (source) => {
return (source === VariantSource.FallbackInline ||
return (!source ||
source === VariantSource.FallbackInline ||
source === VariantSource.FallbackConfig ||

@@ -534,2 +535,11 @@ source === VariantSource.SecondaryInitialVariants);

};
const isNullUndefinedOrEmpty = (value) => {
if (isNullOrUndefined(value))
return true;
return value && Object.keys(value).length === 0;
};
const isLocalEvaluationMode = (flag) => {
var _a;
return ((_a = flag === null || flag === void 0 ? void 0 : flag.metadata) === null || _a === void 0 ? void 0 : _a.evaluationMode) === 'local';
};

@@ -711,3 +721,3 @@ class Backoff {

* configurations and is ready to locally evaluate flags when the
* {@link variant} function is called. This function also starts polling
* {@link variant} function is called. This function then starts polling
* for flag configuration updates at an interval.

@@ -718,8 +728,5 @@ *

* If you also have remote evaluation flags, use {@link fetch} to evaluate
* those flags remotely and await both the promises.
* those flags remotely and await both the promises. For example, to await
* both local and remote evaluation readiness:
*
* <p />
*
* For example, to await both local and remote evaluation readiness:
*
* <pre>

@@ -809,3 +816,2 @@ * await Promise.all([client.start(), client.fetch()]);

variant(key, fallback) {
var _a;
if (!this.apiKey) {

@@ -815,5 +821,6 @@ return { value: undefined };

const flag = this.flags.get(key);
let { source, variant } = this.variantAndSource(key, fallback);
if (((_a = flag === null || flag === void 0 ? void 0 : flag.metadata) === null || _a === void 0 ? void 0 : _a.evaluationMode) === 'local' ||
(flag && isFallback(source))) {
const sourceVariant = this.variantAndSource(key, fallback);
let variant = sourceVariant === null || sourceVariant === void 0 ? void 0 : sourceVariant.variant;
let source = sourceVariant === null || sourceVariant === void 0 ? void 0 : sourceVariant.source;
if (isLocalEvaluationMode(flag) || (!sourceVariant && flag)) {
variant = this.evaluate([flag.key])[key];

@@ -825,4 +832,4 @@ source = VariantSource.LocalEvaluation;

}
this.debug(`[Experiment] variant for ${key} is ${variant.value}`);
return variant;
this.debug(`[Experiment] variant for ${key} is ${variant === null || variant === void 0 ? void 0 : variant.value}`);
return variant || {};
}

@@ -841,7 +848,7 @@ /**

exposure(key) {
var _a;
const flag = this.flags.get(key);
let { source, variant } = this.variantAndSource(key, null);
if (((_a = flag === null || flag === void 0 ? void 0 : flag.metadata) === null || _a === void 0 ? void 0 : _a.evaluationMode) === 'local' ||
(flag && isFallback(source))) {
const sourceVariant = this.variantAndSource(key);
let variant = sourceVariant === null || sourceVariant === void 0 ? void 0 : sourceVariant.variant;
let source = sourceVariant === null || sourceVariant === void 0 ? void 0 : sourceVariant.source;
if (isLocalEvaluationMode(flag) || !sourceVariant) {
variant = this.evaluate([flag.key])[key];

@@ -866,3 +873,9 @@ source = VariantSource.LocalEvaluation;

const evaluatedVariants = this.evaluate();
return Object.assign(Object.assign(Object.assign({}, this.secondaryVariants()), evaluatedVariants), this.sourceVariants());
for (const flagKey in evaluatedVariants) {
const flag = this.flags.get(flagKey);
if (!isLocalEvaluationMode(flag)) {
delete evaluatedVariants[flagKey];
}
}
return Object.assign(Object.assign(Object.assign({}, this.secondaryVariants()), this.sourceVariants()), evaluatedVariants);
}

@@ -947,23 +960,40 @@ /**

}
// For source = LocalStorage, fallback order goes:
// 1. Local Storage (primary)
// 2. Inline function fallback
// 3. InitialFlags (secondary)
// 4. Config fallback
// 5. Source default.
//
// For source = InitialVariants, fallback order goes:
// 1. InitialFlags (primary)
// 2. Local Storage (secondary)
// 3. Inline function fallback
// 4. Config fallback
//
// If there is a default variant and no fallback, return the default
// variant with preference for source over secondary default.
variantAndSource(key, fallback) {
if (this.config.source === Source.InitialVariants) {
// for source = InitialVariants, fallback order goes:
// 1. InitialFlags
// 2. Local Storage
// 3. Function fallback
// 4. Config fallback
const sourceVariant = this.sourceVariants()[key];
if (!isNullOrUndefined(sourceVariant)) {
return {
variant: this.convertVariant(sourceVariant),
source: VariantSource.InitialVariants,
};
}
const secondaryVariant = this.secondaryVariants()[key];
if (!isNullOrUndefined(secondaryVariant)) {
return {
variant: this.convertVariant(secondaryVariant),
source: VariantSource.SecondaryLocalStorage,
};
}
var _a, _b;
let defaultVariantSource = undefined;
// Primary source.
const primarySource = this.config.source === Source.LocalStorage
? VariantSource.LocalStorage
: VariantSource.InitialVariants;
const primaryVariant = this.sourceVariants()[key];
const primaryDefault = (_a = primaryVariant === null || primaryVariant === void 0 ? void 0 : primaryVariant.metadata) === null || _a === void 0 ? void 0 : _a.default;
if (!isNullOrUndefined(primaryVariant) && !primaryDefault) {
return {
variant: this.convertVariant(primaryVariant),
source: primarySource,
};
}
else if (primaryDefault && !defaultVariantSource) {
defaultVariantSource = {
variant: this.convertVariant(primaryVariant),
source: primarySource,
};
}
// Handle inline function fallback for local storage source
if (this.config.source === Source.LocalStorage) {
if (!isNullOrUndefined(fallback)) {

@@ -975,20 +1005,23 @@ return {

}
}
// Secondary source.
const secondarySource = this.config.source === Source.LocalStorage
? VariantSource.SecondaryInitialVariants
: VariantSource.SecondaryLocalStorage;
const secondaryVariant = this.secondaryVariants()[key];
const secondaryDefault = (_b = secondaryVariant === null || secondaryVariant === void 0 ? void 0 : secondaryVariant.metadata) === null || _b === void 0 ? void 0 : _b.default;
if (!isNullOrUndefined(secondaryVariant) && !secondaryDefault) {
return {
variant: this.convertVariant(this.config.fallbackVariant),
source: VariantSource.FallbackConfig,
variant: this.convertVariant(secondaryVariant),
source: secondarySource,
};
}
else {
// for source = LocalStorage, fallback order goes:
// 1. Local Storage
// 2. Function fallback
// 3. InitialFlags
// 4. Config fallback
const sourceVariant = this.sourceVariants()[key];
if (!isNullOrUndefined(sourceVariant)) {
return {
variant: this.convertVariant(sourceVariant),
source: VariantSource.LocalStorage,
};
}
else if (secondaryDefault && !defaultVariantSource) {
defaultVariantSource = {
variant: this.convertVariant(secondaryVariant),
source: secondarySource,
};
}
// Handle inline function fallback for initial variants source
if (this.config.source === Source.InitialVariants) {
if (!isNullOrUndefined(fallback)) {

@@ -1000,14 +1033,13 @@ return {

}
const secondaryVariant = this.secondaryVariants()[key];
if (!isNullOrUndefined(secondaryVariant)) {
return {
variant: this.convertVariant(secondaryVariant),
source: VariantSource.SecondaryInitialVariants,
};
}
return {
variant: this.convertVariant(this.config.fallbackVariant),
source: VariantSource.FallbackConfig,
};
}
// Configured fallback, or default variant
const fallbackVariant = this.convertVariant(this.config.fallbackVariant);
const fallbackSourceVariant = {
variant: fallbackVariant,
source: VariantSource.FallbackConfig,
};
if (!isNullUndefinedOrEmpty(fallbackVariant)) {
return fallbackSourceVariant;
}
return defaultVariantSource;
}

@@ -1149,17 +1181,2 @@ fetchInternal(user, timeoutMillis, retry, options) {

}
translateToEvaluationVariant(variant) {
if (!variant) {
return {};
}
let metadata = undefined;
if (variant.expKey) {
metadata = { experimentKey: variant.expKey };
}
return {
key: variant.key || variant.value,
value: variant.value,
payload: variant.payload,
metadata: metadata,
};
}
sourceVariants() {

@@ -1182,22 +1199,21 @@ if (this.config.source == Source.LocalStorage) {

exposureInternal(key, variant, source) {
var _a, _b, _c;
var _a, _b;
this.legacyExposureInternal(key, variant, source);
const exposure = { flag_key: key };
// Do not track exposure for fallback variants.
if (isFallback(source)) {
(_a = this.exposureTrackingProvider) === null || _a === void 0 ? void 0 : _a.track(exposure);
return;
}
else {
if (variant === null || variant === void 0 ? void 0 : variant.expKey)
exposure.experiment_key = variant === null || variant === void 0 ? void 0 : variant.expKey;
if (!((_b = variant === null || variant === void 0 ? void 0 : variant.metadata) === null || _b === void 0 ? void 0 : _b.default)) {
if (variant === null || variant === void 0 ? void 0 : variant.key) {
exposure.variant = variant.key;
}
else if (variant === null || variant === void 0 ? void 0 : variant.value) {
exposure.variant = variant.value;
}
if (variant === null || variant === void 0 ? void 0 : variant.expKey)
exposure.experiment_key = variant === null || variant === void 0 ? void 0 : variant.expKey;
if (!((_a = variant === null || variant === void 0 ? void 0 : variant.metadata) === null || _a === void 0 ? void 0 : _a.default)) {
if (variant === null || variant === void 0 ? void 0 : variant.key) {
exposure.variant = variant.key;
}
exposure.metadata = variant === null || variant === void 0 ? void 0 : variant.metadata;
(_c = this.exposureTrackingProvider) === null || _c === void 0 ? void 0 : _c.track(exposure);
else if (variant === null || variant === void 0 ? void 0 : variant.value) {
exposure.variant = variant.value;
}
}
exposure.metadata = variant === null || variant === void 0 ? void 0 : variant.metadata;
(_b = this.exposureTrackingProvider) === null || _b === void 0 ? void 0 : _b.track(exposure);
}

@@ -1204,0 +1220,0 @@ legacyExposureInternal(key, variant, source) {

@@ -45,3 +45,3 @@ /**

* configurations and is ready to locally evaluate flags when the
* {@link variant} function is called. This function also starts polling
* {@link variant} function is called. This function then starts polling
* for flag configuration updates at an interval.

@@ -52,8 +52,5 @@ *

* If you also have remote evaluation flags, use {@link fetch} to evaluate
* those flags remotely and await both the promises.
* those flags remotely and await both the promises. For example, to await
* both local and remote evaluation readiness:
*
* <p />
*
* For example, to await both local and remote evaluation readiness:
*
* <pre>

@@ -180,3 +177,2 @@ * await Promise.all([client.start(), client.fetch()]);

private translateFromEvaluationVariant;
private translateToEvaluationVariant;
private sourceVariants;

@@ -183,0 +179,0 @@ private secondaryVariants;

@@ -42,2 +42,2 @@ /**

*/
export declare const isFallback: (source: VariantSource) => boolean;
export declare const isFallback: (source: VariantSource | undefined) => boolean;

@@ -0,1 +1,4 @@

import { EvaluationFlag } from '@amplitude/experiment-core';
export declare const isNullOrUndefined: (value: unknown) => boolean;
export declare const isNullUndefinedOrEmpty: (value: unknown) => boolean;
export declare const isLocalEvaluationMode: (flag: EvaluationFlag | undefined) => boolean;
{
"name": "@amplitude/experiment-js-client",
"version": "1.9.0-beta.3",
"version": "1.9.0-beta.4",
"description": "Amplitude Experiment Javascript Client SDK",

@@ -37,3 +37,3 @@ "keywords": [

"dependencies": {
"@amplitude/experiment-core": "^0.5.0",
"@amplitude/experiment-core": "^0.6.0",
"@amplitude/analytics-connector": "^1.4.8",

@@ -40,0 +40,0 @@ "@amplitude/ua-parser-js": "^0.7.31",

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

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