Skip to content

Commit 98bf936

Browse files
mikechu-optimizelyMike Chu
andauthored
[FSSDK-9987] fix: Conditional ODP instantiation (#902)
* fix: do not instantiate ODP manager if explicit odpOptions.disabled: true * test: correct test for now `undefined` ODP Manager * fix: remove error log for fetchQualifiedSegments this method is often called automatically * revert: return error message to fQS. Plus some lint fixes * docs: update copyright notices --------- Co-authored-by: Mike Chu <michael.chu@optmizely.com>
1 parent d4b7ff8 commit 98bf936

File tree

5 files changed

+73
-45
lines changed

5 files changed

+73
-45
lines changed

lib/index.browser.tests.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
/**
2-
* Copyright 2016-2020, 2022-2023 Optimizely
2+
* Copyright 2016-2020, 2022-2024 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
import logging, { getLogger } from './modules/logging/logger';
1718

1819
import { assert } from 'chai';
@@ -67,7 +68,7 @@ if (!global.window) {
6768
}
6869
}
6970

70-
const pause = (timeoutMilliseconds) => {
71+
const pause = timeoutMilliseconds => {
7172
return new Promise(resolve => setTimeout(resolve, timeoutMilliseconds));
7273
};
7374

@@ -846,19 +847,19 @@ describe('javascript-sdk (Browser)', function() {
846847
const userAgentParser = {
847848
parseUserAgentInfo() {
848849
return {
849-
os: { 'name': 'windows', 'version': '11' },
850-
device: { 'type': 'laptop', 'model': 'thinkpad' },
851-
}
852-
}
853-
}
850+
os: { name: 'windows', version: '11' },
851+
device: { type: 'laptop', model: 'thinkpad' },
852+
};
853+
},
854+
};
854855

855856
const fakeRequestHandler = {
856-
makeRequest: sinon.spy(function (requestUrl, headers, method, data) {
857+
makeRequest: sinon.spy(function(requestUrl, headers, method, data) {
857858
return {
858859
abort: () => {},
859860
responsePromise: Promise.resolve({ statusCode: 200 }),
860-
}
861-
})
861+
};
862+
}),
862863
};
863864

864865
const client = optimizelyFactory.createInstance({
@@ -1047,11 +1048,15 @@ describe('javascript-sdk (Browser)', function() {
10471048
const readyData = await client.onReady();
10481049
assert.equal(readyData.success, true);
10491050
assert.isUndefined(readyData.reason);
1051+
assert.isUndefined(client.odpManager);
1052+
sinon.assert.calledWith(logger.log, optimizelyFactory.enums.LOG_LEVEL.INFO, 'ODP Disabled.');
10501053

10511054
client.sendOdpEvent(ODP_EVENT_ACTION.INITIALIZED);
10521055

1053-
sinon.assert.calledWith(logger.error, 'ODP event send failed.');
1054-
sinon.assert.calledWith(logger.log, optimizelyFactory.enums.LOG_LEVEL.INFO, 'ODP Disabled.');
1056+
sinon.assert.calledWith(
1057+
logger.error,
1058+
optimizelyFactory.enums.ERROR_MESSAGES.ODP_EVENT_FAILED_ODP_MANAGER_MISSING
1059+
);
10551060
});
10561061

10571062
it('should log a warning when attempting to use an event batch size other than 1', async () => {

lib/index.browser.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017, 2019-2023 Optimizely
2+
* Copyright 2016-2017, 2019-2024 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
import logHelper from './modules/logging/logger';
1718
import { getLogger, setErrorHandler, getErrorHandler, LogLevel } from './modules/logging';
1819
import { LocalStoragePendingEventsDispatcher } from './modules/event_processor';
@@ -125,6 +126,11 @@ const createInstance = function(config: Config): Client | null {
125126
notificationCenter,
126127
};
127128

129+
const odpExplicitlyOff = config.odpOptions?.disabled === true;
130+
if (odpExplicitlyOff) {
131+
logger.info(enums.LOG_MESSAGES.ODP_DISABLED);
132+
}
133+
128134
const optimizelyOptions: OptimizelyOptions = {
129135
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
130136
...config,
@@ -135,8 +141,8 @@ const createInstance = function(config: Config): Client | null {
135141
? createHttpPollingDatafileManager(config.sdkKey, logger, config.datafile, config.datafileOptions)
136142
: undefined,
137143
notificationCenter,
138-
isValidInstance: isValidInstance,
139-
odpManager: new BrowserOdpManager({ logger, odpOptions: config.odpOptions }),
144+
isValidInstance,
145+
odpManager: odpExplicitlyOff ? undefined : new BrowserOdpManager({ logger, odpOptions: config.odpOptions }),
140146
};
141147

142148
const optimizely = new Optimizely(optimizelyOptions);

lib/index.node.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
/****************************************************************************
2-
* Copyright 2016-2017, 2019-2023 Optimizely, Inc. and contributors *
2+
* Copyright 2016-2017, 2019-2024 Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
66
* You may obtain a copy of the License at *
77
* *
8-
* https://www.apache.org/licenses/LICENSE-2.0 *
8+
* https://www.apache.org/licenses/LICENSE-2.0 *
99
* *
1010
* Unless required by applicable law or agreed to in writing, software *
1111
* distributed under the License is distributed on an "AS IS" BASIS, *
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
1313
* See the License for the specific language governing permissions and *
1414
* limitations under the License. *
1515
***************************************************************************/
16+
1617
import { getLogger, setErrorHandler, getErrorHandler, LogLevel, setLogHandler, setLogLevel } from './modules/logging';
1718
import Optimizely from './optimizely';
1819
import * as enums from './utils/enums';
@@ -101,6 +102,11 @@ const createInstance = function(config: Config): Client | null {
101102

102103
const eventProcessor = createEventProcessor(eventProcessorConfig);
103104

105+
const odpExplicitlyOff = config.odpOptions?.disabled === true;
106+
if (odpExplicitlyOff) {
107+
logger.info(enums.LOG_MESSAGES.ODP_DISABLED);
108+
}
109+
104110
const optimizelyOptions = {
105111
clientEngine: enums.NODE_CLIENT_ENGINE,
106112
...config,
@@ -111,8 +117,8 @@ const createInstance = function(config: Config): Client | null {
111117
? createHttpPollingDatafileManager(config.sdkKey, logger, config.datafile, config.datafileOptions)
112118
: undefined,
113119
notificationCenter,
114-
isValidInstance: isValidInstance,
115-
odpManager: new NodeOdpManager({ logger, odpOptions: config.odpOptions }),
120+
isValidInstance,
121+
odpManager: odpExplicitlyOff ? undefined : new NodeOdpManager({ logger, odpOptions: config.odpOptions }),
116122
};
117123

118124
return new Optimizely(optimizelyOptions);

lib/index.react_native.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019-2023, Optimizely
2+
* Copyright 2019-2024, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,14 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import {
17-
getLogger,
18-
setErrorHandler,
19-
getErrorHandler,
20-
LogLevel,
21-
setLogHandler,
22-
setLogLevel
23-
} from './modules/logging';
16+
17+
import { getLogger, setErrorHandler, getErrorHandler, LogLevel, setLogHandler, setLogLevel } from './modules/logging';
2418
import * as enums from './utils/enums';
2519
import Optimizely from './optimizely';
2620
import configValidator from './utils/config_validator';
@@ -31,8 +25,7 @@ import eventProcessorConfigValidator from './utils/event_processor_config_valida
3125
import { createNotificationCenter } from './core/notification_center';
3226
import { createEventProcessor } from './plugins/event_processor/index.react_native';
3327
import { OptimizelyDecideOption, Client, Config } from './shared_types';
34-
import { createHttpPollingDatafileManager } from
35-
'./plugins/datafile_manager/react_native_http_polling_datafile_manager';
28+
import { createHttpPollingDatafileManager } from './plugins/datafile_manager/react_native_http_polling_datafile_manager';
3629
import { BrowserOdpManager } from './plugins/odp_manager/index.browser';
3730
import * as commonExports from './common_exports';
3831

@@ -71,7 +64,7 @@ const createInstance = function(config: Config): Client | null {
7164
configValidator.validate(config);
7265
isValidInstance = true;
7366
// eslint-disable-next-line @typescript-eslint/no-explicit-any
74-
} catch (ex: any) {
67+
} catch (ex) {
7568
logger.error(ex);
7669
}
7770

@@ -100,20 +93,27 @@ const createInstance = function(config: Config): Client | null {
10093
batchSize: eventBatchSize,
10194
maxQueueSize: config.eventMaxQueueSize || DEFAULT_EVENT_MAX_QUEUE_SIZE,
10295
notificationCenter,
103-
}
96+
};
10497

10598
const eventProcessor = createEventProcessor(eventProcessorConfig);
10699

100+
const odpExplicitlyOff = config.odpOptions?.disabled === true;
101+
if (odpExplicitlyOff) {
102+
logger.info(enums.LOG_MESSAGES.ODP_DISABLED);
103+
}
104+
107105
const optimizelyOptions = {
108106
clientEngine: enums.REACT_NATIVE_JS_CLIENT_ENGINE,
109107
...config,
110108
eventProcessor,
111109
logger,
112110
errorHandler,
113-
datafileManager: config.sdkKey ? createHttpPollingDatafileManager(config.sdkKey, logger, config.datafile, config.datafileOptions) : undefined,
111+
datafileManager: config.sdkKey
112+
? createHttpPollingDatafileManager(config.sdkKey, logger, config.datafile, config.datafileOptions)
113+
: undefined,
114114
notificationCenter,
115115
isValidInstance: isValidInstance,
116-
odpManager: new BrowserOdpManager({ logger, odpOptions: config.odpOptions }),
116+
odpManager: odpExplicitlyOff ? undefined : new BrowserOdpManager({ logger, odpOptions: config.odpOptions }),
117117
};
118118

119119
// If client engine is react, convert it to react native.
@@ -123,7 +123,7 @@ const createInstance = function(config: Config): Client | null {
123123

124124
return new Optimizely(optimizelyOptions);
125125
// eslint-disable-next-line @typescript-eslint/no-explicit-any
126-
} catch (e: any) {
126+
} catch (e) {
127127
logger.error(e);
128128
return null;
129129
}
@@ -157,4 +157,4 @@ export default {
157157
OptimizelyDecideOption,
158158
};
159159

160-
export * from './export_types'
160+
export * from './export_types';

lib/optimizely/index.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2020-2023, Optimizely, Inc. and contributors *
2+
* Copyright 2020-2024, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -178,10 +178,7 @@ export default class Optimizely implements Client {
178178

179179
const eventProcessorStartedPromise = this.eventProcessor.start();
180180

181-
const dependentPromises: Array<Promise<any>> = [
182-
projectConfigManagerReadyPromise,
183-
eventProcessorStartedPromise,
184-
];
181+
const dependentPromises: Array<Promise<any>> = [projectConfigManagerReadyPromise, eventProcessorStartedPromise];
185182

186183
if (config.odpManager?.initPromise) {
187184
dependentPromises.push(config.odpManager.initPromise);
@@ -778,7 +775,12 @@ export default class Optimizely implements Client {
778775
* type, or null if the feature key is invalid or
779776
* the variable key is invalid
780777
*/
781-
getFeatureVariable(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): FeatureVariableValue {
778+
getFeatureVariable(
779+
featureKey: string,
780+
variableKey: string,
781+
userId: string,
782+
attributes?: UserAttributes
783+
): FeatureVariableValue {
782784
try {
783785
if (!this.isValidInstance()) {
784786
this.logger.log(LOG_LEVEL.ERROR, LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getFeatureVariable');
@@ -1684,7 +1686,12 @@ export default class Optimizely implements Client {
16841686
const projectConfig = this.projectConfigManager.getConfig();
16851687
if (this.odpManager != null && projectConfig != null) {
16861688
this.odpManager.updateSettings(
1687-
new OdpConfig(projectConfig.publicKeyForOdp, projectConfig.hostForOdp, projectConfig.pixelUrlForOdp, projectConfig.allSegments)
1689+
new OdpConfig(
1690+
projectConfig.publicKeyForOdp,
1691+
projectConfig.hostForOdp,
1692+
projectConfig.pixelUrlForOdp,
1693+
projectConfig.allSegments
1694+
)
16881695
);
16891696
}
16901697
}
@@ -1758,6 +1765,10 @@ export default class Optimizely implements Client {
17581765
options?: Array<OptimizelySegmentOption>
17591766
): Promise<string[] | null> {
17601767
if (!this.odpManager) {
1768+
return null;
1769+
}
1770+
1771+
if (!this.odpManager.enabled) {
17611772
this.logger.error(ERROR_MESSAGES.ODP_FETCH_QUALIFIED_SEGMENTS_FAILED_ODP_MANAGER_MISSING);
17621773
return null;
17631774
}
@@ -1769,7 +1780,7 @@ export default class Optimizely implements Client {
17691780
* @returns {string|undefined} Currently provisioned VUID from local ODP Manager or undefined if
17701781
* ODP Manager has not been instantiated yet for any reason.
17711782
*/
1772-
getVuid(): string | undefined {
1783+
public getVuid(): string | undefined {
17731784
if (!this.odpManager) {
17741785
this.logger?.error('Unable to get VUID - ODP Manager is not instantiated yet.');
17751786
return undefined;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy