Skip to content

feat(utils): Add PollingConfigCache and ClientCache #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 22 additions & 49 deletions packages/optimizely-sdk/lib/index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,34 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var fns = require('./utils/fns');
var configValidator = require('./utils/config_validator');
var defaultErrorHandler = require('./plugins/error_handler');
var defaultEventDispatcher = require('./plugins/event_dispatcher/index.browser');
var enums = require('./utils/enums');
var logger = require('./plugins/logger');
var Optimizely = require('./optimizely');

var MODULE_NAME = 'INDEX';
var { PollingConfigCache } = require('./utils/config_cache');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is supposed to be an overridable component, I think it should be in the plugins directory as with the rest of the overridable components. Also, I was thinking we can even make this it's own package and later provide an entry point in here that allows users to not include that package (not that we can't do it without making it a separate package). Just drawing inspiration from other open source projects that split up in multiple packages like React and Apollo

var { ClientCache } = require('./utils/client_cache');

/**
* Entry point into the Optimizely Node testing SDK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is no longer valid :)

*/
module.exports = {
/**
* Creates an instance of the Optimizely class
* @param {Object} config
* @param {Object} config.datafile
* @param {Object} config.errorHandler
* @param {Object} config.eventDispatcher
* @param {Object} config.logger
* @param {Object} config.logLevel
* @param {Object} config.userProfileService
* @return {Object} the Optimizely object
*/
createInstance: function(config) {
var logLevel = 'logLevel' in config ? config.logLevel : enums.LOG_LEVEL.INFO;
var defaultLogger = logger.createLogger({ logLevel: enums.LOG_LEVEL.INFO });
if (config) {
try {
configValidator.validate(config);
config.isValidInstance = true;
} catch (ex) {
var errorMessage = MODULE_NAME + ':' + ex.message;
if (config.logger) {
config.logger.log(enums.LOG_LEVEL.ERROR, errorMessage);
} else {
defaultLogger.log(enums.LOG_LEVEL.ERROR, errorMessage);
}
config.isValidInstance = false;
}
}
createInstance: Optimizely.createInstance,

PollingConfigCache: PollingConfigCache(browserRequester),
ClientCache,
};

if (config.skipJSONValidation == null) {
config.skipJSONValidation = true;
}
/**
* The function that PollingConfigCache should use by default to update a config.
*/
async function browserRequester(url, headers) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move this into utils or even plugins (with the idea that we'll let you configure your own requester in a later version)? Let's try not to bloat the entry point file

// Currently broken, see https://optimizely.atlassian.net/browse/E2-3008
const response = await window.fetch(url, { headers, mode: 'cors' });

config = fns.assignIn({
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
clientVersion: enums.CLIENT_VERSION,
errorHandler: defaultErrorHandler,
eventDispatcher: defaultEventDispatcher,
logger: logger.createLogger({ logLevel: logLevel })
}, config);
return {
body: await response.text(),
headers: Array.from(response.headers.entries()).reduce((acc, [k, v]) => {
acc[k] = v;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use proper variable names

return acc;
}, {}),
statusCode: response.status,
};
}

return new Optimizely(config);
}
};
65 changes: 18 additions & 47 deletions packages/optimizely-sdk/lib/index.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
* limitations under the License. *
***************************************************************************/

var configValidator = require('./utils/config_validator');
var defaultErrorHandler = require('./plugins/error_handler');
var defaultEventDispatcher = require('./plugins/event_dispatcher/index.node');
var enums = require('./utils/enums');
var fns = require('./utils/fns');
var jsonSchemaValidator = require('./utils/json_schema_validator');
var logger = require('./plugins/logger');
var sprintf = require('sprintf');
var rp = require('request-promise-native');

var Optimizely = require('./optimizely');
var { ConfigCache } = require('./utils/config_cache');
var { ClientCache } = require('./utils/client_cache');

var Optimizely = require('./optimizely');

Expand All @@ -31,44 +28,18 @@ var MODULE_NAME = 'INDEX';
* Entry point into the Optimizely Node testing SDK
*/
module.exports = {
/**
* Creates an instance of the Optimizely class
* @param {Object} config
* @param {Object} config.datafile
* @param {Object} config.errorHandler
* @param {Object} config.eventDispatcher
* @param {Object} config.jsonSchemaValidator
* @param {Object} config.logger
* @param {Object} config.userProfileService
* @return {Object} the Optimizely object
*/
createInstance: function(config) {
var defaultLogger = logger.createNoOpLogger();
if (config) {
try {
configValidator.validate(config);
config.isValidInstance = true;
} catch (ex) {
if (config.logger) {
config.logger.log(enums.LOG_LEVEL.ERROR, sprintf('%s: %s', MODULE_NAME, ex.message));
} else {
var simpleLogger = logger.createLogger({logLevel: 4});
simpleLogger.log(enums.LOG_LEVEL.ERROR, sprintf('%s: %s', MODULE_NAME, ex.message));
}
config.isValidInstance = false;
}
}

config = fns.assign({
clientEngine: enums.NODE_CLIENT_ENGINE,
clientVersion: enums.CLIENT_VERSION,
errorHandler: defaultErrorHandler,
eventDispatcher: defaultEventDispatcher,
jsonSchemaValidator: jsonSchemaValidator,
logger: defaultLogger,
skipJSONValidation: false
}, config);
createInstance: Optimizely.createInstance,

return new Optimizely(config);
}
PollingConfigCache: PollingConfigCache(nodejsRequester),
ClientCache,
};

async function nodejsRequester(url, headers) {
return rp({
uri: url,
headers,
resolveWithFullResponse: true,
simple: false, // Allow non-2xx responses (such as 304 Not Modified) to fulfill
});
}

57 changes: 56 additions & 1 deletion packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var decisionService = require('../core/decision_service');
var enums = require('../utils/enums');
var eventBuilder = require('../core/event_builder/index.js');
var eventTagsValidator = require('../utils/event_tags_validator');
var logger = require('../plugins/logger');
var notificationCenter = require('../core/notification_center');
var projectConfig = require('../core/project_config');
var projectConfigSchema = require('./project_config_schema');
Expand All @@ -34,6 +35,12 @@ var MODULE_NAME = 'OPTIMIZELY';
var DECISION_SOURCES = enums.DECISION_SOURCES;
var FEATURE_VARIABLE_TYPES = enums.FEATURE_VARIABLE_TYPES;

var configValidator = require('../utils/config_validator');
var defaultErrorHandler = require('../plugins/error_handler');
var defaultEventDispatcher = require('../plugins/event_dispatcher/index.browser');
var MODULE_NAME = 'INDEX';


/**
* The Optimizely class
* @param {Object} config
Expand Down Expand Up @@ -660,4 +667,52 @@ Optimizely.prototype.getFeatureVariableString = function(featureKey, variableKey
return this._getFeatureVariableForType(featureKey, variableKey, FEATURE_VARIABLE_TYPES.STRING, userId, attributes);
};

module.exports = Optimizely;
/**
* Creates an instance of the Optimizely class
* @param {Object} config
* @param {Object} config.datafile
* @param {Object} config.errorHandler
* @param {Object} config.eventDispatcher
* @param {Object} config.logger
* @param {Object} config.logLevel
* @param {Object} config.userProfileService
* @return {Object} the Optimizely object
*/
function createInstance(config) {
var logLevel = 'logLevel' in config ? config.logLevel : enums.LOG_LEVEL.INFO;
var defaultLogger = logger.createLogger({ logLevel: enums.LOG_LEVEL.INFO });
if (config) {
try {
configValidator.validate(config);
config.isValidInstance = true;
} catch (ex) {
var errorMessage = MODULE_NAME + ':' + ex.message;
if (config.logger) {
config.logger.log(enums.LOG_LEVEL.ERROR, errorMessage);
} else {
defaultLogger.log(enums.LOG_LEVEL.ERROR, errorMessage);
}
config.isValidInstance = false;
}
}

if (config.skipJSONValidation == null) {
config.skipJSONValidation = true;
}

config = fns.assignIn({
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to vary by entrypoint because it lets the backend know whether it is a node or browser client.

clientVersion: enums.CLIENT_VERSION,
errorHandler: defaultErrorHandler,
eventDispatcher: defaultEventDispatcher,
logger: logger.createLogger({ logLevel: logLevel })
}, config);

return new Optimizely(config);
}

module.exports = {
Optimizely,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason you export the class as well? I don't think you are using it.

createInstance,
};

Loading