Skip to content

refactor: Convert lib/plugins from CJS to ES module #427

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

Merged
merged 7 commits into from
Apr 10, 2020
Merged
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
1 change: 1 addition & 0 deletions packages/optimizely-sdk/lib/index.browser.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('javascript-sdk', function() {
sinon.stub(configValidator, 'validate');

xhr = sinon.useFakeXMLHttpRequest();
global.XMLHttpRequest = xhr;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, wasn't this removed in an earlier PR? Why add it back?

Copy link
Contributor

Choose a reason for hiding this comment

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

This was not causing any problem last time. but in this PR, when we made changes to the event dispatcher. Its somehow failing the tests. I still dont know the root cause but adding it back resolves the issue

requests = [];
xhr.onCreate = function(req) {
requests.push(req);
Expand Down
1 change: 1 addition & 0 deletions packages/optimizely-sdk/lib/index.browser.umdtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('javascript-sdk', function() {
sinon.stub(Optimizely.prototype, 'close');

xhr = sinon.useFakeXMLHttpRequest();
global.XMLHttpRequest = xhr;
requests = [];
xhr.onCreate = function(req) {
requests.push(req);
Expand Down
18 changes: 8 additions & 10 deletions packages/optimizely-sdk/lib/plugins/error_handler/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, Optimizely
* Copyright 2016, 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,12 +17,10 @@
/**
* Default error handler implementation
*/
module.exports = {
/**
* Handle given exception
* @param {Object} exception An exception object
*/
handleError: function() {
// no-op
},
};
export var handleError = function() {
// no-op
}

export default {
handleError,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, Optimizely
* Copyright 2016, 2020 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,16 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var errorHandler = require('./');
import { assert } from 'chai';

var chai = require('chai');
var assert = chai.assert;
import { handleError } from './';

describe('lib/plugins/error_handler', function() {
describe('APIs', function() {
describe('handleError', function() {
it('should just be a no-op function', function() {
assert.isFunction(errorHandler.handleError);
assert.isFunction(handleError);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017, Optimizely
* Copyright 2016-2017, 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,53 +17,51 @@ var POST_METHOD = 'POST';
var GET_METHOD = 'GET';
var READYSTATE_COMPLETE = 4;

module.exports = {
/**
* Sample event dispatcher implementation for tracking impression and conversions
* Users of the SDK can provide their own implementation
* @param {Object} eventObj
* @param {Function} callback
*/
dispatchEvent: function(eventObj, callback) {
var url = eventObj.url;
var params = eventObj.params;
var req;
if (eventObj.httpVerb === POST_METHOD) {
req = new XMLHttpRequest();
req.open(POST_METHOD, url, true);
req.setRequestHeader('Content-Type', 'application/json');
req.onreadystatechange = function() {
if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') {
try {
callback(params);
} catch (e) {
// TODO: Log this somehow (consider adding a logger to the EventDispatcher interface)
}
/**
* Sample event dispatcher implementation for tracking impression and conversions
* Users of the SDK can provide their own implementation
* @param {Object} eventObj
* @param {Function} callback
*/
export var dispatchEvent = function(eventObj, callback) {
var url = eventObj.url;
var params = eventObj.params;
var req;
if (eventObj.httpVerb === POST_METHOD) {
req = new XMLHttpRequest();
req.open(POST_METHOD, url, true);
req.setRequestHeader('Content-Type', 'application/json');
req.onreadystatechange = function() {
if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') {
try {
callback(params);
} catch (e) {
// TODO: Log this somehow (consider adding a logger to the EventDispatcher interface)
}
};
req.send(JSON.stringify(params));
} else {
// add param for cors headers to be sent by the log endpoint
url += '?wxhr=true';
if (params) {
url += '&' + toQueryString(params);
}
};
req.send(JSON.stringify(params));
} else {
// add param for cors headers to be sent by the log endpoint
url += '?wxhr=true';
if (params) {
url += '&' + toQueryString(params);
}

req = new XMLHttpRequest();
req.open(GET_METHOD, url, true);
req.onreadystatechange = function() {
if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') {
try {
callback();
} catch (e) {
// TODO: Log this somehow (consider adding a logger to the EventDispatcher interface)
}
req = new XMLHttpRequest();
req.open(GET_METHOD, url, true);
req.onreadystatechange = function() {
if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') {
try {
callback();
} catch (e) {
// TODO: Log this somehow (consider adding a logger to the EventDispatcher interface)
}
};
req.send();
}
},
};
}
};
req.send();
}
}

var toQueryString = function(obj) {
return Object.keys(obj)
Expand All @@ -72,3 +70,7 @@ var toQueryString = function(obj) {
})
.join('&');
};

export default {
dispatchEvent,
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017, Optimizely
* Copyright 2016-2017, 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var eventDispatcher = require('./index.browser');
var chai = require('chai');
var assert = chai.assert;
var sinon = require('sinon');
import { assert } from 'chai';
import sinon from 'sinon';

import { dispatchEvent } from './index.browser';

describe('lib/plugins/event_dispatcher/browser', function() {
describe('APIs', function() {
Expand All @@ -25,7 +25,6 @@ describe('lib/plugins/event_dispatcher/browser', function() {
var requests;
beforeEach(function() {
xhr = sinon.useFakeXMLHttpRequest();
global.XMLHttpRequest = xhr;
requests = [];
xhr.onCreate = function(req) {
requests.push(req);
Expand All @@ -48,7 +47,7 @@ describe('lib/plugins/event_dispatcher/browser', function() {
};

var callback = sinon.spy();
eventDispatcher.dispatchEvent(eventObj, callback);
dispatchEvent(eventObj, callback);
assert.strictEqual(1, requests.length);
assert.strictEqual(requests[0].method, 'POST');
assert.strictEqual(requests[0].requestBody, JSON.stringify(eventParams));
Expand All @@ -67,7 +66,7 @@ describe('lib/plugins/event_dispatcher/browser', function() {
};

var callback = sinon.spy();
eventDispatcher.dispatchEvent(eventObj, callback);
dispatchEvent(eventObj, callback);
requests[0].respond([
200,
{},
Expand All @@ -84,7 +83,7 @@ describe('lib/plugins/event_dispatcher/browser', function() {
};

var callback = sinon.spy();
eventDispatcher.dispatchEvent(eventObj, callback);
dispatchEvent(eventObj, callback);
requests[0].respond([200, {}, '{"url":"https://cdn.com/event","httpVerb":"GET"']);
sinon.assert.calledOnce(callback);
done();
Expand Down
86 changes: 44 additions & 42 deletions packages/optimizely-sdk/lib/plugins/event_dispatcher/index.node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018, Optimizely
* Copyright 2016-2018, 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,51 +13,53 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var http = require('http');
var https = require('https');
var url = require('url');
import http from 'http';
import https from 'https';
import url from 'url';

module.exports = {
/**
* Dispatch an HTTP request to the given url and the specified options
* @param {Object} eventObj Event object containing
* @param {string} eventObj.url the url to make the request to
* @param {Object} eventObj.params parameters to pass to the request (i.e. in the POST body)
* @param {string} eventObj.httpVerb the HTTP request method type. only POST is supported.
* @param {function} callback callback to execute
* @return {ClientRequest|undefined} ClientRequest object which made the request, or undefined if no request was made (error)
*/
dispatchEvent: function(eventObj, callback) {
// Non-POST requests not supported
if (eventObj.httpVerb !== 'POST') {
return;
}
/**
* Dispatch an HTTP request to the given url and the specified options
* @param {Object} eventObj Event object containing
* @param {string} eventObj.url the url to make the request to
* @param {Object} eventObj.params parameters to pass to the request (i.e. in the POST body)
* @param {string} eventObj.httpVerb the HTTP request method type. only POST is supported.
* @param {function} callback callback to execute
* @return {ClientRequest|undefined} ClientRequest object which made the request, or undefined if no request was made (error)
*/
export var dispatchEvent = function(eventObj, callback) {
// Non-POST requests not supported
if (eventObj.httpVerb !== 'POST') {
return;
}

var parsedUrl = url.parse(eventObj.url);
var parsedUrl = url.parse(eventObj.url);

var dataString = JSON.stringify(eventObj.params);
var dataString = JSON.stringify(eventObj.params);

var requestOptions = {
host: parsedUrl.host,
path: parsedUrl.path,
method: 'POST',
headers: {
'content-type': 'application/json',
'content-length': dataString.length.toString(),
},
};
var requestOptions = {
host: parsedUrl.host,
path: parsedUrl.path,
method: 'POST',
headers: {
'content-type': 'application/json',
'content-length': dataString.length.toString(),
},
};

var requestCallback = function(response) {
if (response && response.statusCode && response.statusCode >= 200 && response.statusCode < 400) {
callback(response);
}
};
var requestCallback = function(response) {
if (response && response.statusCode && response.statusCode >= 200 && response.statusCode < 400) {
callback(response);
}
};

var req = (parsedUrl.protocol === 'http:' ? http : https).request(requestOptions, requestCallback);
// Add no-op error listener to prevent this from throwing
req.on('error', function() {});
req.write(dataString);
req.end();
return req;
};

var req = (parsedUrl.protocol === 'http:' ? http : https).request(requestOptions, requestCallback);
// Add no-op error listener to prevent this from throwing
req.on('error', function() {});
req.write(dataString);
req.end();
return req;
},
export default {
dispatchEvent,
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018, Optimizely
* Copyright 2016-2018, 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var eventDispatcher = require('./index.node');
var chai = require('chai');
var assert = chai.assert;
var nock = require('nock');
var sinon = require('sinon');
import nock from 'nock';
import sinon from 'sinon';
import { assert } from 'chai';

import { dispatchEvent } from './index.node';

describe('lib/plugins/event_dispatcher/node', function() {
describe('APIs', function() {
Expand Down Expand Up @@ -49,7 +49,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
httpVerb: 'POST',
};

eventDispatcher.dispatchEvent(eventObj, function(resp) {
dispatchEvent(eventObj, function(resp) {
assert.equal(200, resp.statusCode);
done();
});
Expand All @@ -64,8 +64,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
httpVerb: 'POST',
};

eventDispatcher
.dispatchEvent(eventObj, stubCallback.callback)
dispatchEvent(eventObj, stubCallback.callback)
.on('response', function(response) {
sinon.assert.calledOnce(stubCallback.callback);
done();
Expand All @@ -85,7 +84,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
};

var callback = sinon.spy();
eventDispatcher.dispatchEvent(eventObj, callback);
dispatchEvent(eventObj, callback);
sinon.assert.notCalled(callback);
});
});
Expand All @@ -98,7 +97,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
};

var callback = sinon.spy();
eventDispatcher.dispatchEvent(eventObj, callback);
dispatchEvent(eventObj, callback);
sinon.assert.notCalled(callback);
});
});
Expand Down
Loading