@mparticle/web-sdk
Advanced tools
Comparing version 2.23.9 to 2.24.0
{ | ||
"name": "@mparticle/web-sdk", | ||
"version": "2.23.9", | ||
"version": "2.24.0", | ||
"description": "mParticle core SDK for web applications", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
@@ -6,3 +6,3 @@ import Constants from './constants'; | ||
import KitBlocker from './kitBlocking'; | ||
import { Dictionary, isEmpty } from './utils'; | ||
import { Dictionary, getRampNumber, isEmpty, parseNumber } from './utils'; | ||
import { IUploadObject } from './serverModel'; | ||
@@ -42,5 +42,5 @@ import { MPForwarder } from './forwarders.interfaces'; | ||
if (!this.uploader) { | ||
const millis = mpInstance._Helpers.getFeatureFlag( | ||
const millis: number = parseNumber(mpInstance._Helpers.getFeatureFlag( | ||
Constants.FeatureFlags.EventBatchingIntervalMillis | ||
); | ||
)); | ||
this.uploader = new BatchUploader(mpInstance, millis); | ||
@@ -47,0 +47,0 @@ } |
@@ -129,5 +129,5 @@ import { version } from '../package.json'; | ||
aliasMaxWindow: 90, // Max age of Alias request startTime, in days | ||
uploadInterval: 0, // Maximum milliseconds in between batch uploads, below 500 will mean immediate upload | ||
uploadInterval: 0, // Maximum milliseconds in between batch uploads, below 500 will mean immediate upload. The server returns this as a string, but we are using it as a number internally | ||
}, | ||
DefaultUrls: { | ||
DefaultBaseUrls: { | ||
v1SecureServiceUrl: 'jssdks.mparticle.com/v1/JS/', | ||
@@ -172,2 +172,3 @@ v2SecureServiceUrl: 'jssdks.mparticle.com/v2/JS/', | ||
OfflineStorage: 'offlineStorage', | ||
DirectUrlRouting: 'directURLRouting', | ||
}, | ||
@@ -174,0 +175,0 @@ DefaultInstance: 'default_instance', |
@@ -1529,5 +1529,4 @@ // | ||
mpInstance.Logger = new Logger(config); | ||
mpInstance._Store = new Store(config, mpInstance); | ||
mpInstance._Store = new Store(config, mpInstance, apiKey); | ||
window.mParticle.Store = mpInstance._Store; | ||
mpInstance._Store.devToken = apiKey || null; | ||
mpInstance.Logger.verbose( | ||
@@ -1534,0 +1533,0 @@ Messages.InformationMessages.StartingInitialization |
188
src/store.ts
@@ -23,3 +23,3 @@ import { Batch } from '@mparticle/event-models'; | ||
} from './sdkRuntimeModels'; | ||
import { isNumber, isDataPlanSlug, Dictionary } from './utils'; | ||
import { isNumber, isDataPlanSlug, Dictionary, parseNumber } from './utils'; | ||
import { SDKConsentState } from './consent'; | ||
@@ -47,3 +47,3 @@ import { Kit, MPForwarder } from './forwarders.interfaces'; | ||
package?: string; | ||
flags?: { [key: string]: string | number | boolean }; | ||
flags?: IFeatureFlags; | ||
kitConfigs: IKitConfigs[]; | ||
@@ -94,4 +94,4 @@ kits: Dictionary<Kit>; | ||
for (const prop in Constants.DefaultUrls) { | ||
sdkConfig[prop] = Constants.DefaultUrls[prop]; | ||
for (const prop in Constants.DefaultBaseUrls) { | ||
sdkConfig[prop] = Constants.DefaultBaseUrls[prop]; | ||
} | ||
@@ -116,2 +116,10 @@ | ||
// https://go.mparticle.com/work/SQDSDKS-5954 | ||
export interface IFeatureFlags { | ||
reportBatching?: string; | ||
eventBatchingIntervalMillis?: number; | ||
offlineStorage?: string; | ||
directURLRouting?: boolean; | ||
} | ||
// Temporary Interface until Store can be refactored as a class | ||
@@ -166,3 +174,4 @@ export interface IStore { | ||
config: SDKInitConfig, | ||
mpInstance: MParticleWebSDK | ||
mpInstance: MParticleWebSDK, | ||
apiKey?: string | ||
) { | ||
@@ -217,8 +226,17 @@ const defaultStore: Partial<IStore> = { | ||
} | ||
this.devToken = apiKey || null; | ||
this.integrationDelayTimeoutStart = Date.now(); | ||
// Set configuration to default settings | ||
this.SDKConfig = createSDKConfig(config); | ||
// Set configuration to default settings | ||
if (config) { | ||
if (!config.hasOwnProperty('flags')) { | ||
this.SDKConfig.flags = {}; | ||
} | ||
this.SDKConfig.flags = processFlags(config, this | ||
.SDKConfig as SDKConfig); | ||
if (config.deviceId) { | ||
@@ -235,26 +253,12 @@ this.deviceId = config.deviceId; | ||
if (config.hasOwnProperty('v1SecureServiceUrl')) { | ||
this.SDKConfig.v1SecureServiceUrl = config.v1SecureServiceUrl; | ||
} | ||
const baseUrls: Dictionary<string> = processBaseUrls( | ||
config, | ||
this.SDKConfig.flags, | ||
apiKey | ||
); | ||
if (config.hasOwnProperty('v2SecureServiceUrl')) { | ||
this.SDKConfig.v2SecureServiceUrl = config.v2SecureServiceUrl; | ||
for (const baseUrlKeys in baseUrls) { | ||
this.SDKConfig[baseUrlKeys] = baseUrls[baseUrlKeys]; | ||
} | ||
if (config.hasOwnProperty('v3SecureServiceUrl')) { | ||
this.SDKConfig.v3SecureServiceUrl = config.v3SecureServiceUrl; | ||
} | ||
if (config.hasOwnProperty('identityUrl')) { | ||
this.SDKConfig.identityUrl = config.identityUrl; | ||
} | ||
if (config.hasOwnProperty('aliasUrl')) { | ||
this.SDKConfig.aliasUrl = config.aliasUrl; | ||
} | ||
if (config.hasOwnProperty('configUrl')) { | ||
this.SDKConfig.configUrl = config.configUrl; | ||
} | ||
if (config.hasOwnProperty('logLevel')) { | ||
@@ -422,30 +426,110 @@ this.SDKConfig.logLevel = config.logLevel; | ||
} | ||
} | ||
} | ||
if (!config.hasOwnProperty('flags')) { | ||
this.SDKConfig.flags = {}; | ||
export function processFlags( | ||
config: SDKInitConfig, | ||
SDKConfig: SDKConfig | ||
): IFeatureFlags { | ||
const flags: IFeatureFlags = {}; | ||
const { | ||
ReportBatching, | ||
EventBatchingIntervalMillis, | ||
OfflineStorage, | ||
DirectUrlRouting, | ||
} = Constants.FeatureFlags; | ||
if (!config.flags) { | ||
return {}; | ||
} | ||
// Passed in config flags take priority over defaults | ||
flags[ReportBatching] = config.flags[ReportBatching] || false; | ||
// The server returns stringified numbers, sowe need to parse | ||
flags[EventBatchingIntervalMillis] = | ||
parseNumber(config.flags[EventBatchingIntervalMillis]) || | ||
Constants.DefaultConfig.uploadInterval; | ||
flags[OfflineStorage] = config.flags[OfflineStorage] || '0'; | ||
flags[DirectUrlRouting] = config.flags[DirectUrlRouting] === 'True'; | ||
return flags; | ||
} | ||
export function processBaseUrls( | ||
config: SDKInitConfig, | ||
flags: IFeatureFlags, | ||
apiKey?: string | ||
): Dictionary<string> { | ||
// an API key is not present in a webview only mode. In this case, no baseUrls are needed | ||
if (!apiKey) { | ||
return {}; | ||
} | ||
// Set default baseUrls | ||
let baseUrls: Dictionary<string>; | ||
// When direct URL routing is false, update baseUrls based custom urls | ||
// passed to the config | ||
if (flags.directURLRouting) { | ||
return processDirectBaseUrls(config, apiKey); | ||
} else { | ||
return processCustomBaseUrls(config); | ||
} | ||
} | ||
function processCustomBaseUrls(config: SDKInitConfig): Dictionary<string> { | ||
const defaultBaseUrls: Dictionary<string> = Constants.DefaultBaseUrls; | ||
const newBaseUrls: Dictionary<string> = {}; | ||
// If there is no custo base url, we use the default base url | ||
for (let baseUrlKey in defaultBaseUrls) { | ||
newBaseUrls[baseUrlKey] = | ||
config[baseUrlKey] || defaultBaseUrls[baseUrlKey]; | ||
} | ||
return newBaseUrls; | ||
} | ||
function processDirectBaseUrls( | ||
config: SDKInitConfig, | ||
apiKey: string | ||
): Dictionary { | ||
const defaultBaseUrls = Constants.DefaultBaseUrls; | ||
const directBaseUrls: Dictionary<string> = {}; | ||
// When Direct URL Routing is true, we create a new set of baseUrls that | ||
// include the silo in the urls. mParticle API keys are prefixed with the | ||
// silo and a hyphen (ex. "us1-", "us2-", "eu1-"). us1 was the first silo, | ||
// and before other silos existed, there were no prefixes and all apiKeys | ||
// were us1. As such, if we split on a '-' and the resulting array length | ||
// is 1, then it is an older APIkey that should route to us1. | ||
// When splitKey.length is greater than 1, then splitKey[0] will be | ||
// us1, us2, eu1, au1, or st1, etc as new silos are added | ||
const DEFAULT_SILO = 'us1'; | ||
const splitKey: Array<string> = apiKey.split('-'); | ||
const routingPrefix: string = | ||
splitKey.length <= 1 ? DEFAULT_SILO : splitKey[0]; | ||
for (let baseUrlKey in defaultBaseUrls) { | ||
// Any custom endpoints passed to mpConfig will take priority over direct | ||
// mapping to the silo. The most common use case is a customer provided CNAME. | ||
if (baseUrlKey === 'configUrl') { | ||
directBaseUrls[baseUrlKey] = | ||
config[baseUrlKey] || defaultBaseUrls[baseUrlKey]; | ||
continue; | ||
} | ||
if ( | ||
!this.SDKConfig.flags.hasOwnProperty( | ||
Constants.FeatureFlags.EventBatchingIntervalMillis | ||
) | ||
) { | ||
this.SDKConfig.flags[ | ||
Constants.FeatureFlags.EventBatchingIntervalMillis | ||
] = Constants.DefaultConfig.uploadInterval; | ||
if (config.hasOwnProperty(baseUrlKey)) { | ||
directBaseUrls[baseUrlKey] = config[baseUrlKey]; | ||
} else { | ||
const urlparts = defaultBaseUrls[baseUrlKey].split('.'); | ||
directBaseUrls[baseUrlKey] = [ | ||
urlparts[0], | ||
routingPrefix, | ||
...urlparts.slice(1), | ||
].join('.'); | ||
} | ||
if ( | ||
!this.SDKConfig.flags.hasOwnProperty( | ||
Constants.FeatureFlags.ReportBatching | ||
) | ||
) { | ||
this.SDKConfig.flags[Constants.FeatureFlags.ReportBatching] = false; | ||
} | ||
if ( | ||
!this.SDKConfig.flags.hasOwnProperty( | ||
Constants.FeatureFlags.OfflineStorage | ||
) | ||
) { | ||
this.SDKConfig.flags[Constants.FeatureFlags.OfflineStorage] = 0; | ||
} | ||
} | ||
} | ||
return directBaseUrls; | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1476475
26472