diff --git a/package-lock.json b/package-lock.json index 8fadebb87..416a9b7ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@splitsoftware/splitio", - "version": "11.11.1", + "version": "11.11.2-rc.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@splitsoftware/splitio", - "version": "11.11.1", + "version": "11.11.2-rc.0", "license": "Apache-2.0", "dependencies": { - "@splitsoftware/splitio-commons": "2.12.0", + "@splitsoftware/splitio-commons": "3.0.0-rc.0", "bloom-filters": "^3.0.4", "ioredis": "^4.28.0 || ^5.0.0", "js-yaml": "^4.1.1", @@ -335,20 +335,28 @@ "dev": true }, "node_modules/@splitsoftware/splitio-commons": { - "version": "2.12.0", - "resolved": "https://registry.npmjs.org/@splitsoftware/splitio-commons/-/splitio-commons-2.12.0.tgz", - "integrity": "sha512-RN6CCeTZHV0oHTudQd+VVqVnJ6vUDgo95RUN4Lez2UM5Z/YVz9MQxtRoHcLYwCQEDf60yoTMB6bwHd8WlamgkQ==", + "version": "3.0.0-rc.0", + "resolved": "https://registry.npmjs.org/@splitsoftware/splitio-commons/-/splitio-commons-3.0.0-rc.0.tgz", + "integrity": "sha512-ZtwPsZHZp1yNAkCFzaPH708ntMaaI7CEbIyIdZeGLwINdWyIunAF29bYslqwuqmtaNd2X9El0Z6YbkqtDoKNMw==", "license": "Apache-2.0", "dependencies": { "@types/ioredis": "^4.28.0", "tslib": "^2.3.1" }, "peerDependencies": { - "ioredis": "^4.28.0 || ^5.0.0" + "bloom-filters": "^3.0.0", + "ioredis": "^4.28.0 || ^5.0.0", + "node-fetch": "^2.7.0" }, "peerDependenciesMeta": { + "bloom-filters": { + "optional": true + }, "ioredis": { "optional": true + }, + "node-fetch": { + "optional": true } } }, @@ -1205,6 +1213,7 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/bloom-filters/-/bloom-filters-3.0.4.tgz", "integrity": "sha512-BdnPWo2OpYhlvuP2fRzJBdioMCkm7Zp0HCf8NJgF5Mbyqy7VQ/CnTiVWMMyq4EZCBHwj0Kq6098gW2/3RsZsrA==", + "peer": true, "dependencies": { "@types/seedrandom": "^3.0.8", "base64-arraybuffer": "^1.0.2", @@ -5065,6 +5074,7 @@ "version": "2.7.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "peer": true, "dependencies": { "whatwg-url": "^5.0.0" }, @@ -8781,9 +8791,9 @@ "dev": true }, "@splitsoftware/splitio-commons": { - "version": "2.12.0", - "resolved": "https://registry.npmjs.org/@splitsoftware/splitio-commons/-/splitio-commons-2.12.0.tgz", - "integrity": "sha512-RN6CCeTZHV0oHTudQd+VVqVnJ6vUDgo95RUN4Lez2UM5Z/YVz9MQxtRoHcLYwCQEDf60yoTMB6bwHd8WlamgkQ==", + "version": "3.0.0-rc.0", + "resolved": "https://registry.npmjs.org/@splitsoftware/splitio-commons/-/splitio-commons-3.0.0-rc.0.tgz", + "integrity": "sha512-ZtwPsZHZp1yNAkCFzaPH708ntMaaI7CEbIyIdZeGLwINdWyIunAF29bYslqwuqmtaNd2X9El0Z6YbkqtDoKNMw==", "requires": { "@types/ioredis": "^4.28.0", "tslib": "^2.3.1" @@ -9465,6 +9475,7 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/bloom-filters/-/bloom-filters-3.0.4.tgz", "integrity": "sha512-BdnPWo2OpYhlvuP2fRzJBdioMCkm7Zp0HCf8NJgF5Mbyqy7VQ/CnTiVWMMyq4EZCBHwj0Kq6098gW2/3RsZsrA==", + "peer": true, "requires": { "@types/seedrandom": "^3.0.8", "base64-arraybuffer": "^1.0.2", @@ -12287,6 +12298,7 @@ "version": "2.7.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "peer": true, "requires": { "whatwg-url": "^5.0.0" }, diff --git a/package.json b/package.json index f7768e73f..b9812c81b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@splitsoftware/splitio", - "version": "11.11.1", + "version": "11.11.2-rc.0", "description": "Split SDK", "files": [ "README.md", @@ -38,7 +38,7 @@ "node": ">=14.0.0" }, "dependencies": { - "@splitsoftware/splitio-commons": "2.12.0", + "@splitsoftware/splitio-commons": "3.0.0-rc.0", "bloom-filters": "^3.0.4", "ioredis": "^4.28.0 || ^5.0.0", "js-yaml": "^4.1.1", @@ -99,7 +99,7 @@ "test-browser-e2e-push": "cross-env NODE_ENV=test karma start karma/e2e.push.karma.conf.js", "test-node": "npm run test-node-unit && npm run test-node-e2e", "test-node-unit": "cross-env NODE_ENV=test tape -r ./ts-node.register \"src/*/**/__tests__/**/!(browser).spec.js\" | tap-min", - "test-node-e2e": "npm run test-node-e2e-online && npm run test-node-e2e-offline && npm run test-node-e2e-destroy && npm run test-node-e2e-errorCatching && npm run test-node-e2e-push && npm run test-node-e2e-redis", + "test-node-e2e": "npm run test-node-e2e-online && npm run test-node-e2e-offline && npm run test-node-e2e-destroy && npm run test-node-e2e-errorCatching && npm run test-node-e2e-push", "test-node-e2e-online": "cross-env NODE_ENV=test tape -r ./ts-node.register src/__tests__/online/node.spec.js | tap-min", "test-node-e2e-offline": "cross-env NODE_ENV=test tape -r ./ts-node.register src/__tests__/offline/node.spec.js | tap-min", "test-node-e2e-destroy": "cross-env NODE_ENV=test tape -r ./ts-node.register src/__tests__/destroy/node.spec.js | tap-min", diff --git a/src/__tests__/browserSuites/ready-promise.spec.js b/src/__tests__/browserSuites/ready-promise.spec.js index abd2dcd0d..1916284e3 100644 --- a/src/__tests__/browserSuites/ready-promise.spec.js +++ b/src/__tests__/browserSuites/ready-promise.spec.js @@ -31,7 +31,7 @@ function assertGetTreatmentWhenReady(assert, client) { function assertGetTreatmentControlNotReady(assert, client) { consoleSpy.log.resetHistory(); assert.equal(client.getTreatment('hierarchical_splits_test'), 'control', 'We should get control if client is not ready.'); - assert.true(consoleSpy.log.calledWithExactly('[WARN] splitio => getTreatment: the SDK is not ready to evaluate. Results may be incorrect for feature flag hierarchical_splits_test. Make sure to wait for SDK readiness before using this method.'), 'Telling us that calling getTreatment would return CONTROL since SDK is not ready at this point.'); + assert.true(consoleSpy.log.calledWithExactly('[WARN] splitio => getTreatment: the SDK is not ready to evaluate. Results may be incorrect. Make sure to wait for SDK readiness before using this method.'), 'Telling us that calling getTreatment would return CONTROL since SDK is not ready at this point.'); } function assertGetTreatmentControlNotReadyOnDestroy(assert, client) { diff --git a/src/__tests__/mocks/splitchanges.real.withSegments.json b/src/__tests__/mocks/splitchanges.real.withSegments.json index ad766b311..3981f780b 100644 --- a/src/__tests__/mocks/splitchanges.real.withSegments.json +++ b/src/__tests__/mocks/splitchanges.real.withSegments.json @@ -66,57 +66,6 @@ } ], "configurations": {} - }, - { - "trafficTypeName": "user", - "name": "real_split_2", - "trafficAllocation": 100, - "trafficAllocationSeed": -1427479928, - "seed": 769174959, - "status": "ACTIVE", - "killed": false, - "defaultTreatment": "on", - "changeNumber": 1550099287990, - "algo": 2, - "conditions": [ - { - "conditionType": "ROLLOUT", - "matcherGroup": { - "combiner": "AND", - "matchers": [ - { - "keySelector": { - "trafficType": "user", - "attribute": null - }, - "matcherType": "ALL_KEYS", - "negate": false, - "userDefinedSegmentMatcherData": null, - "whitelistMatcherData": null, - "unaryNumericMatcherData": null, - "betweenMatcherData": null, - "booleanMatcherData": null, - "dependencyMatcherData": null, - "stringMatcherData": null - } - ] - }, - "partitions": [ - { - "treatment": "on", - "size": 100 - }, - { - "treatment": "off", - "size": 0 - } - ], - "label": "default rule" - } - ], - "configurations": { - "on": "{\"color\":\"brown\",\"dimensions\":{\"height\":12,\"width\":14},\"text\":{\"inner\":\"click me\"}}" - } } ], "s": -1, diff --git a/src/__tests__/nodeSuites/push-fallback.spec.js b/src/__tests__/nodeSuites/push-fallback.spec.js index 61065c7d3..76edbd8c0 100644 --- a/src/__tests__/nodeSuites/push-fallback.spec.js +++ b/src/__tests__/nodeSuites/push-fallback.spec.js @@ -27,7 +27,7 @@ import streamingResetMessage from '../mocks/message.STREAMING_RESET.json'; import { nearlyEqual, mockSegmentChanges, url } from '../testUtils'; import EventSourceMock, { setMockListener } from '../testUtils/eventSourceMock'; -import { __setEventSource } from '../../platform/getEventSource/node'; +import { __setEventSource } from '@splitsoftware/splitio-commons/src/platform/getEventSource/node'; import { SplitFactory } from '../../'; import { settingsFactory } from '../../settings'; diff --git a/src/__tests__/nodeSuites/push-flag-sets.spec.js b/src/__tests__/nodeSuites/push-flag-sets.spec.js index e8a6fa6d5..a7c4c8fe3 100644 --- a/src/__tests__/nodeSuites/push-flag-sets.spec.js +++ b/src/__tests__/nodeSuites/push-flag-sets.spec.js @@ -1,6 +1,6 @@ import { SplitFactory } from '../..'; import EventSourceMock, { setMockListener } from '../testUtils/eventSourceMock'; -import { __setEventSource } from '../../platform/getEventSource/node'; +import { __setEventSource } from '@splitsoftware/splitio-commons/src/platform/getEventSource/node'; import { mockSegmentChanges } from '../testUtils'; import notification1 from '../mocks/message.SPLIT_UPDATE.FS.1.json'; diff --git a/src/__tests__/nodeSuites/push-initialization-nopush.spec.js b/src/__tests__/nodeSuites/push-initialization-nopush.spec.js index e06397592..fc7876a54 100644 --- a/src/__tests__/nodeSuites/push-initialization-nopush.spec.js +++ b/src/__tests__/nodeSuites/push-initialization-nopush.spec.js @@ -8,7 +8,7 @@ import authInvalidCredentials from '../mocks/auth.invalidCredentials.txt'; import authNoUserSpecified from '../mocks/auth.noUserSpecified.txt'; import { nearlyEqual, url } from '../testUtils'; -import { __setEventSource, __restore } from '../../platform/getEventSource/node'; +import { __setEventSource, __restore } from '@splitsoftware/splitio-commons/src/platform/getEventSource/node'; import EventSourceMock, { setMockListener } from '../testUtils/eventSourceMock'; const baseUrls = { diff --git a/src/__tests__/nodeSuites/push-initialization-retries.spec.js b/src/__tests__/nodeSuites/push-initialization-retries.spec.js index 94fc9d7c0..b81639ac3 100644 --- a/src/__tests__/nodeSuites/push-initialization-retries.spec.js +++ b/src/__tests__/nodeSuites/push-initialization-retries.spec.js @@ -7,7 +7,7 @@ import authPushBadToken from '../mocks/auth.pushBadToken.json'; import { nearlyEqual, url } from '../testUtils'; import EventSourceMock, { setMockListener } from '../testUtils/eventSourceMock'; -import { __setEventSource } from '../../platform/getEventSource/node'; +import { __setEventSource } from '@splitsoftware/splitio-commons/src/platform/getEventSource/node'; import { SplitFactory } from '../../'; import { settingsFactory } from '../../settings'; diff --git a/src/__tests__/nodeSuites/push-refresh-token.spec.js b/src/__tests__/nodeSuites/push-refresh-token.spec.js index 148bc45b7..a34f78a71 100644 --- a/src/__tests__/nodeSuites/push-refresh-token.spec.js +++ b/src/__tests__/nodeSuites/push-refresh-token.spec.js @@ -7,7 +7,7 @@ import authPushDisabled from '../mocks/auth.pushDisabled.json'; import { nearlyEqual, mockSegmentChanges, url } from '../testUtils'; import EventSourceMock, { setMockListener } from '../testUtils/eventSourceMock'; -import { __setEventSource } from '../../platform/getEventSource/node'; +import { __setEventSource } from '@splitsoftware/splitio-commons/src/platform/getEventSource/node'; import { SplitFactory } from '../../'; import { settingsFactory } from '../../settings'; diff --git a/src/__tests__/nodeSuites/push-synchronization-retries.spec.js b/src/__tests__/nodeSuites/push-synchronization-retries.spec.js index 71b97474d..9fcd1f571 100644 --- a/src/__tests__/nodeSuites/push-synchronization-retries.spec.js +++ b/src/__tests__/nodeSuites/push-synchronization-retries.spec.js @@ -13,7 +13,7 @@ import { nearlyEqual, mockSegmentChanges, url } from '../testUtils'; import { Backoff } from '@splitsoftware/splitio-commons/src/utils/Backoff'; import EventSourceMock, { setMockListener } from '../testUtils/eventSourceMock'; -import { __setEventSource } from '../../platform/getEventSource/node'; +import { __setEventSource } from '@splitsoftware/splitio-commons/src/platform/getEventSource/node'; import { SplitFactory } from '../../'; import { settingsFactory } from '../../settings'; diff --git a/src/__tests__/nodeSuites/push-synchronization.spec.js b/src/__tests__/nodeSuites/push-synchronization.spec.js index fee427fe5..f8c06a09a 100644 --- a/src/__tests__/nodeSuites/push-synchronization.spec.js +++ b/src/__tests__/nodeSuites/push-synchronization.spec.js @@ -30,7 +30,7 @@ import authPushEnabled from '../mocks/auth.pushEnabled.node.json'; import { nearlyEqual, mockSegmentChanges, url, hasNoCacheHeader } from '../testUtils'; import EventSourceMock, { setMockListener } from '../testUtils/eventSourceMock'; -import { __setEventSource } from '../../platform/getEventSource/node'; +import { __setEventSource } from '@splitsoftware/splitio-commons/src/platform/getEventSource/node'; import { SplitFactory } from '../../'; import { settingsFactory } from '../../settings'; diff --git a/src/__tests__/nodeSuites/ready-promise.spec.js b/src/__tests__/nodeSuites/ready-promise.spec.js index bdcfd2737..2f7f85527 100644 --- a/src/__tests__/nodeSuites/ready-promise.spec.js +++ b/src/__tests__/nodeSuites/ready-promise.spec.js @@ -28,7 +28,7 @@ function assertGetTreatmentWhenReady(assert, client, key) { function assertGetTreatmentControlNotReady(assert, client, key) { consoleSpy.log.resetHistory(); assert.equal(client.getTreatment(key, 'hierarchical_splits_test'), 'control', 'We should get control if client is not ready.'); - assert.true(consoleSpy.log.calledWithExactly('[WARN] splitio => getTreatment: the SDK is not ready to evaluate. Results may be incorrect for feature flag hierarchical_splits_test. Make sure to wait for SDK readiness before using this method.'), 'Telling us that calling getTreatment would return CONTROL since SDK is not ready at this point.'); + assert.true(consoleSpy.log.calledWithExactly('[WARN] splitio => getTreatment: the SDK is not ready to evaluate. Results may be incorrect. Make sure to wait for SDK readiness before using this method.'), 'Telling us that calling getTreatment would return CONTROL since SDK is not ready at this point.'); } function assertGetTreatmentControlNotReadyOnDestroy(assert, client, key) { diff --git a/src/__tests__/testUtils/nodeFetchMock.js b/src/__tests__/testUtils/nodeFetchMock.js index 6965a9561..db30d9b53 100644 --- a/src/__tests__/testUtils/nodeFetchMock.js +++ b/src/__tests__/testUtils/nodeFetchMock.js @@ -1,5 +1,5 @@ import fetchMock from 'fetch-mock'; -import { __setFetch } from '../../platform/getFetch/node'; +import { __setFetch } from '@splitsoftware/splitio-commons/src/platform/getFetch/node'; const sandboxFetchMock = fetchMock.sandbox(); diff --git a/src/factory/browser.js b/src/factory/browser.js index da9ca826c..dbc14482d 100644 --- a/src/factory/browser.js +++ b/src/factory/browser.js @@ -14,7 +14,7 @@ import { createUserConsentAPI } from '@splitsoftware/splitio-commons/src/consent import { localhostFromObjectFactory } from '@splitsoftware/splitio-commons/src/sync/offline/LocalhostFromObject'; import { settingsFactory } from '../settings/browser'; -import { platform, SignalListener } from '../platform'; +import { platform } from '../platform'; const syncManagerOnlineCSFactory = syncManagerOnlineFactory(pollingManagerCSFactory, pushManagerFactory); @@ -47,8 +47,6 @@ function getModules(settings) { sdkClientMethodFactory: sdkClientMethodCSFactory, - SignalListener, - impressionsObserverFactory: impressionObserverCSFactory, extraProps: (params) => { diff --git a/src/factory/node.js b/src/factory/node.js index ae4ff1c29..14e30c448 100644 --- a/src/factory/node.js +++ b/src/factory/node.js @@ -14,8 +14,8 @@ import { isConsumerMode } from '@splitsoftware/splitio-commons/src/utils/setting import { localhostFromFileFactory } from '../sync/offline/LocalhostFromFile'; import { settingsFactory } from '../settings/node'; -import { platform, SignalListener } from '../platform'; -import { bloomFilterFactory } from '../platform/filter/bloomFilter'; +import { platform } from '../platform'; +import { bloomFilterFactory } from '@splitsoftware/splitio-commons/src/utils/filter/bloomFilter'; const syncManagerOnlineSSFactory = syncManagerOnlineFactory(pollingManagerSSFactory, pushManagerFactory); @@ -46,8 +46,6 @@ function getModules(settings) { sdkClientMethodFactory, - SignalListener, - impressionsObserverFactory: impressionObserverSSFactory, filterAdapterFactory: bloomFilterFactory, diff --git a/src/platform/EventEmitter.js b/src/platform/EventEmitter.js deleted file mode 100644 index 2f173c20a..000000000 --- a/src/platform/EventEmitter.js +++ /dev/null @@ -1,444 +0,0 @@ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -// Adaptation of "events" library (https://www.npmjs.com/package/events) -// exported as an ES module instead of CommonJS, to avoid extra configuration steps when using -// the ESM build of the SDK with tools that doesn't support CommonJS by default (e.g. Rollup). - -var R = typeof Reflect === 'object' ? Reflect : null; -var ReflectApply = R && typeof R.apply === 'function' - ? R.apply - : function ReflectApply(target, receiver, args) { - return Function.prototype.apply.call(target, receiver, args); - }; - -var ReflectOwnKeys; -if (R && typeof R.ownKeys === 'function') { - ReflectOwnKeys = R.ownKeys; -} else if (Object.getOwnPropertySymbols) { - ReflectOwnKeys = function ReflectOwnKeys(target) { - return Object.getOwnPropertyNames(target) - .concat(Object.getOwnPropertySymbols(target)); - }; -} else { - ReflectOwnKeys = function ReflectOwnKeys(target) { - return Object.getOwnPropertyNames(target); - }; -} - -function ProcessEmitWarning(warning) { - if (console && console.warn) console.warn(warning); -} - -// eslint-disable-next-line compat/compat -var NumberIsNaN = Number.isNaN || function NumberIsNaN(value) { - return value !== value; -}; - -export function EventEmitter() { - EventEmitter.init.call(this); -} - -EventEmitter.prototype._events = undefined; -EventEmitter.prototype._eventsCount = 0; -EventEmitter.prototype._maxListeners = undefined; - -// By default EventEmitters will print a warning if more than 10 listeners are -// added to it. This is a useful default which helps finding memory leaks. -var defaultMaxListeners = 10; - -function checkListener(listener) { - if (typeof listener !== 'function') { - throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener); - } -} - -Object.defineProperty(EventEmitter, 'defaultMaxListeners', { - enumerable: true, - get: function () { - return defaultMaxListeners; - }, - set: function (arg) { - if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) { - throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + arg + '.'); - } - defaultMaxListeners = arg; - } -}); - -EventEmitter.init = function () { - - if (this._events === undefined || - this._events === Object.getPrototypeOf(this)._events) { - this._events = Object.create(null); - this._eventsCount = 0; - } - - this._maxListeners = this._maxListeners || undefined; -}; - -// Obviously not all Emitters should be limited to 10. This function allows -// that to be increased. Set to zero for unlimited. -EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { - if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) { - throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + '.'); - } - this._maxListeners = n; - return this; -}; - -function _getMaxListeners(that) { - if (that._maxListeners === undefined) - return EventEmitter.defaultMaxListeners; - return that._maxListeners; -} - -EventEmitter.prototype.getMaxListeners = function getMaxListeners() { - return _getMaxListeners(this); -}; - -EventEmitter.prototype.emit = function emit(type) { - var args = []; - for (var i = 1; i < arguments.length; i++) args.push(arguments[i]); - var doError = (type === 'error'); - - var events = this._events; - if (events !== undefined) - doError = (doError && events.error === undefined); - else if (!doError) - return false; - - // If there is no 'error' event listener then throw. - if (doError) { - var er; - if (args.length > 0) - er = args[0]; - if (er instanceof Error) { - // Note: The comments on the `throw` lines are intentional, they show - // up in Node.js output if this results in an unhandled exception. - throw er; // Unhandled 'error' event - } - // At least give some kind of context to the user - var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : '')); - err.context = er; - throw err; // Unhandled 'error' event - } - - var handler = events[type]; - - if (handler === undefined) - return false; - - if (typeof handler === 'function') { - ReflectApply(handler, this, args); - } else { - var len = handler.length; - var listeners = arrayClone(handler, len); // eslint-disable-next-line no-redeclare - for (var i = 0; i < len; ++i) - ReflectApply(listeners[i], this, args); - } - - return true; -}; - -function _addListener(target, type, listener, prepend) { - var m; - var events; - var existing; - - checkListener(listener); - - events = target._events; - if (events === undefined) { - events = target._events = Object.create(null); - target._eventsCount = 0; - } else { - // To avoid recursion in the case that type === "newListener"! Before - // adding it to the listeners, first emit "newListener". - if (events.newListener !== undefined) { - target.emit('newListener', type, listener.listener ? listener.listener : listener); - - // Re-assign `events` because a newListener handler could have caused the - // this._events to be assigned to a new object - events = target._events; - } - existing = events[type]; - } - - if (existing === undefined) { - // Optimize the case of one listener. Don't need the extra array object. - existing = events[type] = listener; - ++target._eventsCount; - } else { - if (typeof existing === 'function') { - // Adding the second element, need to change to array. - existing = events[type] = - prepend ? [listener, existing] : [existing, listener]; - // If we've already got an array, just append. - } else if (prepend) { - existing.unshift(listener); - } else { - existing.push(listener); - } - - // Check for listener leak - m = _getMaxListeners(target); - if (m > 0 && existing.length > m && !existing.warned) { - existing.warned = true; - // No error code for this since it is a Warning - // eslint-disable-next-line no-restricted-syntax - var w = new Error('Possible EventEmitter memory leak detected. ' + - existing.length + ' ' + String(type) + ' listeners ' + - 'added. Use emitter.setMaxListeners() to ' + - 'increase limit'); - w.name = 'MaxListenersExceededWarning'; - w.emitter = target; - w.type = type; - w.count = existing.length; - ProcessEmitWarning(w); - } - } - - return target; -} - -EventEmitter.prototype.addListener = function addListener(type, listener) { - return _addListener(this, type, listener, false); -}; - -EventEmitter.prototype.on = EventEmitter.prototype.addListener; - -EventEmitter.prototype.prependListener = - function prependListener(type, listener) { - return _addListener(this, type, listener, true); - }; - -function onceWrapper() { - if (!this.fired) { - this.target.removeListener(this.type, this.wrapFn); - this.fired = true; - if (arguments.length === 0) - return this.listener.call(this.target); - return this.listener.apply(this.target, arguments); - } -} - -function _onceWrap(target, type, listener) { - var state = { fired: false, wrapFn: undefined, target: target, type: type, listener: listener }; - var wrapped = onceWrapper.bind(state); - wrapped.listener = listener; - state.wrapFn = wrapped; - return wrapped; -} - -EventEmitter.prototype.once = function once(type, listener) { - checkListener(listener); - this.on(type, _onceWrap(this, type, listener)); - return this; -}; - -EventEmitter.prototype.prependOnceListener = - function prependOnceListener(type, listener) { - checkListener(listener); - this.prependListener(type, _onceWrap(this, type, listener)); - return this; - }; - -// Emits a 'removeListener' event if and only if the listener was removed. -EventEmitter.prototype.removeListener = - function removeListener(type, listener) { - var list, events, position, i, originalListener; - - checkListener(listener); - - events = this._events; - if (events === undefined) - return this; - - list = events[type]; - if (list === undefined) - return this; - - if (list === listener || list.listener === listener) { - if (--this._eventsCount === 0) - this._events = Object.create(null); - else { - delete events[type]; - if (events.removeListener) - this.emit('removeListener', type, list.listener || listener); - } - } else if (typeof list !== 'function') { - position = -1; - - for (i = list.length - 1; i >= 0; i--) { - if (list[i] === listener || list[i].listener === listener) { - originalListener = list[i].listener; - position = i; - break; - } - } - - if (position < 0) - return this; - - if (position === 0) - list.shift(); - else { - spliceOne(list, position); - } - - if (list.length === 1) - events[type] = list[0]; - - if (events.removeListener !== undefined) - this.emit('removeListener', type, originalListener || listener); - } - - return this; - }; - -EventEmitter.prototype.off = EventEmitter.prototype.removeListener; - -EventEmitter.prototype.removeAllListeners = - function removeAllListeners(type) { - var listeners, events, i; - - events = this._events; - if (events === undefined) - return this; - - // not listening for removeListener, no need to emit - if (events.removeListener === undefined) { - if (arguments.length === 0) { - this._events = Object.create(null); - this._eventsCount = 0; - } else if (events[type] !== undefined) { - if (--this._eventsCount === 0) - this._events = Object.create(null); - else - delete events[type]; - } - return this; - } - - // emit removeListener for all listeners on all events - if (arguments.length === 0) { - var keys = Object.keys(events); - var key; - for (i = 0; i < keys.length; ++i) { - key = keys[i]; - if (key === 'removeListener') continue; - this.removeAllListeners(key); - } - this.removeAllListeners('removeListener'); - this._events = Object.create(null); - this._eventsCount = 0; - return this; - } - - listeners = events[type]; - - if (typeof listeners === 'function') { - this.removeListener(type, listeners); - } else if (listeners !== undefined) { - // LIFO order - for (i = listeners.length - 1; i >= 0; i--) { - this.removeListener(type, listeners[i]); - } - } - - return this; - }; - -function _listeners(target, type, unwrap) { - var events = target._events; - - if (events === undefined) - return []; - - var evlistener = events[type]; - if (evlistener === undefined) - return []; - - if (typeof evlistener === 'function') - return unwrap ? [evlistener.listener || evlistener] : [evlistener]; - - return unwrap ? - unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length); -} - -EventEmitter.prototype.listeners = function listeners(type) { - return _listeners(this, type, true); -}; - -EventEmitter.prototype.rawListeners = function rawListeners(type) { - return _listeners(this, type, false); -}; - -EventEmitter.listenerCount = function (emitter, type) { - if (typeof emitter.listenerCount === 'function') { - return emitter.listenerCount(type); - } else { - return listenerCount.call(emitter, type); - } -}; - -EventEmitter.prototype.listenerCount = listenerCount; -function listenerCount(type) { - var events = this._events; - - if (events !== undefined) { - var evlistener = events[type]; - - if (typeof evlistener === 'function') { - return 1; - } else if (evlistener !== undefined) { - return evlistener.length; - } - } - - return 0; -} - -EventEmitter.prototype.eventNames = function eventNames() { - return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : []; -}; - -function arrayClone(arr, n) { - var copy = new Array(n); - for (var i = 0; i < n; ++i) - copy[i] = arr[i]; - return copy; -} - -function spliceOne(list, index) { - for (; index + 1 < list.length; index++) - list[index] = list[index + 1]; - list.pop(); -} - -function unwrapListeners(arr) { - var ret = new Array(arr.length); - for (var i = 0; i < ret.length; ++i) { - ret[i] = arr[i].listener || arr[i]; - } - return ret; -} diff --git a/src/platform/browser.js b/src/platform/browser.js index 9432f1d60..3074b1970 100644 --- a/src/platform/browser.js +++ b/src/platform/browser.js @@ -1,14 +1,19 @@ -import { EventEmitter } from './EventEmitter'; -import { getFetch } from '../platform/getFetch/browser'; -import { getEventSource } from '../platform/getEventSource/browser'; +import { EventEmitter } from '@splitsoftware/splitio-commons/src/utils/EventEmitter'; +import { getEventSource } from '@splitsoftware/splitio-commons/src/platform/getEventSource/browser'; import { BrowserSignalListener } from '@splitsoftware/splitio-commons/src/listeners/browser'; import { now } from '@splitsoftware/splitio-commons/src/utils/timeTracker/now/browser'; +import unfetch from 'unfetch'; + +// @TODO: Breaking change (drop support for old browsers without native fetch): replace with '@splitsoftware/splitio-commons/src/platform/getFetch/browser' and remove `unfetch` dependency +export function getFetch() { + return typeof fetch === 'function' ? fetch : unfetch; +} + export const platform = { getFetch, getEventSource, EventEmitter, - now + now, + SignalListener: BrowserSignalListener, }; - -export const SignalListener = BrowserSignalListener; diff --git a/src/platform/filter/__tests__/bloomFilter.spec.js b/src/platform/filter/__tests__/bloomFilter.spec.js deleted file mode 100644 index 2a097adae..000000000 --- a/src/platform/filter/__tests__/bloomFilter.spec.js +++ /dev/null @@ -1,22 +0,0 @@ -import tape from 'tape-catch'; -import { bloomFilterFactory } from '../bloomFilter'; - -tape('Bloom filter', (assert) => { - - const bloomFilter = bloomFilterFactory(); - - assert.true(bloomFilter.add('feature','key')); - assert.false(bloomFilter.contains('feature1','key')); - assert.true(bloomFilter.contains('feature','key')); - - bloomFilter.clear(); - - assert.false(bloomFilter.contains('feature','key')); - - assert.true(bloomFilter.add('feature2','key')); - assert.false(bloomFilter.contains('feature3','key')); - assert.true(bloomFilter.contains('feature2','key')); - - assert.end(); - -}); diff --git a/src/platform/filter/bloomFilter.js b/src/platform/filter/bloomFilter.js deleted file mode 100644 index 2d3663bde..000000000 --- a/src/platform/filter/bloomFilter.js +++ /dev/null @@ -1,33 +0,0 @@ -import { BloomFilter } from 'bloom-filters'; - -const EXPECTED_INSERTIONS = 10000000; -const ERROR_RATE = 0.01; -const REFRESH_RATE = 24 * 60 * 60000; // 24HS - -export function bloomFilterFactory(expectedInsertions = EXPECTED_INSERTIONS, errorRate = ERROR_RATE, refreshRate = REFRESH_RATE) { - let filter = BloomFilter.create(expectedInsertions, errorRate); - - return { - - refreshRate: refreshRate, - - add(key, value) { - const data = `${key}:${value}`; - if (filter.has(data)) { - return false; - } - filter.add(data); - return true; - }, - - contains(key, value) { - const data = `${key}:${value}`; - return filter.has(data); - }, - - clear() { - filter = BloomFilter.create(expectedInsertions, errorRate); - } - - }; -} diff --git a/src/platform/getEventSource/__tests__/browser.spec.js b/src/platform/getEventSource/__tests__/browser.spec.js deleted file mode 100644 index 82e760c62..000000000 --- a/src/platform/getEventSource/__tests__/browser.spec.js +++ /dev/null @@ -1,8 +0,0 @@ -import tape from 'tape-catch'; -import { getEventSource } from '../browser'; - -tape('getEventSource returns global EventSource in Browser', assert => { - assert.equal(getEventSource(), EventSource); - - assert.end(); -}); diff --git a/src/platform/getEventSource/__tests__/node.spec.js b/src/platform/getEventSource/__tests__/node.spec.js deleted file mode 100644 index 27736468b..000000000 --- a/src/platform/getEventSource/__tests__/node.spec.js +++ /dev/null @@ -1,8 +0,0 @@ -import tape from 'tape-catch'; -import { getEventSource } from '../node'; - -tape('getEventSource returns eventsource module in Node', assert => { - assert.equal(getEventSource(), require('../eventsource')); - - assert.end(); -}); diff --git a/src/platform/getEventSource/browser.js b/src/platform/getEventSource/browser.js deleted file mode 100644 index 780a01287..000000000 --- a/src/platform/getEventSource/browser.js +++ /dev/null @@ -1,3 +0,0 @@ -export function getEventSource() { - return typeof EventSource === 'function' ? EventSource : undefined; -} diff --git a/src/platform/getEventSource/eventsource.js b/src/platform/getEventSource/eventsource.js deleted file mode 100644 index 36a23d3f3..000000000 --- a/src/platform/getEventSource/eventsource.js +++ /dev/null @@ -1,519 +0,0 @@ -/* eslint-disable no-prototype-builtins */ -/* eslint-disable no-restricted-syntax */ -/* -Modified version of "eventsource" v1.1.2 package (https://www.npmjs.com/package/eventsource/v/1.1.2) -that accepts a custom agent. - -The MIT License - -Copyright (c) EventSource GitHub organization - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ -var parse = require('url').parse; -var events = require('events'); -var https = require('https'); -var http = require('http'); -var util = require('util'); - -var httpsOptions = [ - 'pfx', 'key', 'passphrase', 'cert', 'ca', 'ciphers', - 'rejectUnauthorized', 'secureProtocol', 'servername', 'checkServerIdentity' -]; - -var bom = [239, 187, 191]; -var colon = 58; -var space = 32; -var lineFeed = 10; -var carriageReturn = 13; - -function hasBom(buf) { - return bom.every(function (charCode, index) { - return buf[index] === charCode; - }); -} - -/** - * Creates a new EventSource object - * - * @param {String} url the URL to which to connect - * @param {Object} [eventSourceInitDict] extra init params. See README for details. - * @api public - **/ -function EventSource(url, eventSourceInitDict) { - var readyState = EventSource.CONNECTING; - var headers = eventSourceInitDict && eventSourceInitDict.headers; - var hasNewOrigin = false; - Object.defineProperty(this, 'readyState', { - get: function () { - return readyState; - } - }); - - Object.defineProperty(this, 'url', { - get: function () { - return url; - } - }); - - var self = this; - self.reconnectInterval = 1000; - self.connectionInProgress = false; - - var reconnectUrl = null; - - function onConnectionClosed(message) { - if (readyState === EventSource.CLOSED) return; - readyState = EventSource.CONNECTING; - _emit('error', new Event('error', { message: message })); - - // The url may have been changed by a temporary redirect. If that's the case, - // revert it now, and flag that we are no longer pointing to a new origin - if (reconnectUrl) { - url = reconnectUrl; - reconnectUrl = null; - hasNewOrigin = false; - } - setTimeout(function () { - if (readyState !== EventSource.CONNECTING || self.connectionInProgress) { - return; - } - self.connectionInProgress = true; - connect(); - }, self.reconnectInterval); - } - - var req; - var lastEventId = ''; - if (headers && headers['Last-Event-ID']) { - lastEventId = headers['Last-Event-ID']; - delete headers['Last-Event-ID']; - } - - var discardTrailingNewline = false; - var data = ''; - var eventName = ''; - - function connect() { - var options = parse(url); - var isSecure = options.protocol === 'https:'; - options.headers = { 'Cache-Control': 'no-cache', 'Accept': 'text/event-stream' }; - if (lastEventId) options.headers['Last-Event-ID'] = lastEventId; - if (headers) { - var reqHeaders = hasNewOrigin ? removeUnsafeHeaders(headers) : headers; - for (var i in reqHeaders) { - var header = reqHeaders[i]; - if (header) { - options.headers[i] = header; - } - } - } - - // Legacy: this should be specified as `eventSourceInitDict.https.rejectUnauthorized`, - // but for now exists as a backwards-compatibility layer - options.rejectUnauthorized = !(eventSourceInitDict && !eventSourceInitDict.rejectUnauthorized); - - if (eventSourceInitDict && eventSourceInitDict.createConnection !== undefined) { - options.createConnection = eventSourceInitDict.createConnection; - } - - // If specify agent, use it. - if (eventSourceInitDict && eventSourceInitDict.agent !== undefined) { - options.agent = eventSourceInitDict.agent; - } - - // If specify http proxy, make the request to sent to the proxy server, - // and include the original url in path and Host headers - var useProxy = eventSourceInitDict && eventSourceInitDict.proxy; - if (useProxy) { - var proxy = parse(eventSourceInitDict.proxy); - isSecure = proxy.protocol === 'https:'; - - options.protocol = isSecure ? 'https:' : 'http:'; - options.path = url; - options.headers.Host = options.host; - options.hostname = proxy.hostname; - options.host = proxy.host; - options.port = proxy.port; - } - - // If https options are specified, merge them into the request options - if (eventSourceInitDict && eventSourceInitDict.https) { - for (var optName in eventSourceInitDict.https) { - if (httpsOptions.indexOf(optName) === -1) { - continue; - } - - var option = eventSourceInitDict.https[optName]; - if (option !== undefined) { - options[optName] = option; - } - } - } - - // Pass this on to the XHR - if (eventSourceInitDict && eventSourceInitDict.withCredentials !== undefined) { - options.withCredentials = eventSourceInitDict.withCredentials; - } - - req = (isSecure ? https : http).request(options, function (res) { - self.connectionInProgress = false; - // Handle HTTP errors - if (res.statusCode === 500 || res.statusCode === 502 || res.statusCode === 503 || res.statusCode === 504) { - _emit('error', new Event('error', { status: res.statusCode, message: res.statusMessage })); - onConnectionClosed(); - return; - } - - // Handle HTTP redirects - if (res.statusCode === 301 || res.statusCode === 302 || res.statusCode === 307) { - var location = res.headers.location; - if (!location) { - // Server sent redirect response without Location header. - _emit('error', new Event('error', { status: res.statusCode, message: res.statusMessage })); - return; - } - var prevOrigin = getOrigin(url); - var nextOrigin = getOrigin(location); - hasNewOrigin = prevOrigin !== nextOrigin; - if (res.statusCode === 307) reconnectUrl = url; - url = location; - process.nextTick(connect); - return; - } - - if (res.statusCode !== 200) { - _emit('error', new Event('error', { status: res.statusCode, message: res.statusMessage })); - return self.close(); - } - - readyState = EventSource.OPEN; - res.on('close', function () { - res.removeAllListeners('close'); - res.removeAllListeners('end'); - onConnectionClosed(); - }); - - res.on('end', function () { - res.removeAllListeners('close'); - res.removeAllListeners('end'); - onConnectionClosed(); - }); - _emit('open', new Event('open')); - - // text/event-stream parser adapted from webkit's - // Source/WebCore/page/EventSource.cpp - var isFirst = true; - var buf; - var startingPos = 0; - var startingFieldLength = -1; - res.on('data', function (chunk) { - buf = buf ? Buffer.concat([buf, chunk]) : chunk; - if (isFirst && hasBom(buf)) { - buf = buf.slice(bom.length); - } - - isFirst = false; - var pos = 0; - var length = buf.length; - - while (pos < length) { - if (discardTrailingNewline) { - if (buf[pos] === lineFeed) { - ++pos; - } - discardTrailingNewline = false; - } - - var lineLength = -1; - var fieldLength = startingFieldLength; - var c; - - for (var i = startingPos; lineLength < 0 && i < length; ++i) { - c = buf[i]; - if (c === colon) { - if (fieldLength < 0) { - fieldLength = i - pos; - } - } else if (c === carriageReturn) { - discardTrailingNewline = true; - lineLength = i - pos; - } else if (c === lineFeed) { - lineLength = i - pos; - } - } - - if (lineLength < 0) { - startingPos = length - pos; - startingFieldLength = fieldLength; - break; - } else { - startingPos = 0; - startingFieldLength = -1; - } - - parseEventStreamLine(buf, pos, fieldLength, lineLength); - - pos += lineLength + 1; - } - - if (pos === length) { - buf = void 0; - } else if (pos > 0) { - buf = buf.slice(pos); - } - }); - }); - - req.on('error', function (err) { - self.connectionInProgress = false; - onConnectionClosed(err.message); - }); - - if (req.setNoDelay) req.setNoDelay(true); - req.end(); - } - - connect(); - - function _emit() { - if (self.listeners(arguments[0]).length > 0) { - self.emit.apply(self, arguments); - } - } - - this._close = function () { - if (readyState === EventSource.CLOSED) return; - readyState = EventSource.CLOSED; - if (req.abort) req.abort(); - if (req.xhr && req.xhr.abort) req.xhr.abort(); - }; - - function parseEventStreamLine(buf, pos, fieldLength, lineLength) { - if (lineLength === 0) { - if (data.length > 0) { - var type = eventName || 'message'; - _emit(type, new MessageEvent(type, { - data: data.slice(0, -1), // remove trailing newline - lastEventId: lastEventId, - origin: getOrigin(url) - })); - data = ''; - } - eventName = void 0; - } else if (fieldLength > 0) { - var noValue = fieldLength < 0; - var step = 0; - var field = buf.slice(pos, pos + (noValue ? lineLength : fieldLength)).toString(); - - if (noValue) { - step = lineLength; - } else if (buf[pos + fieldLength + 1] !== space) { - step = fieldLength + 1; - } else { - step = fieldLength + 2; - } - pos += step; - - var valueLength = lineLength - step; - var value = buf.slice(pos, pos + valueLength).toString(); - - if (field === 'data') { - data += value + '\n'; - } else if (field === 'event') { - eventName = value; - } else if (field === 'id') { - lastEventId = value; - } else if (field === 'retry') { - var retry = parseInt(value, 10); - if (!Number.isNaN(retry)) { - self.reconnectInterval = retry; - } - } - } - } -} - -module.exports = EventSource; - -util.inherits(EventSource, events.EventEmitter); -EventSource.prototype.constructor = EventSource; // make stacktraces readable - -['open', 'error', 'message'].forEach(function (method) { - Object.defineProperty(EventSource.prototype, 'on' + method, { - /** - * Returns the current listener - * - * @return {Mixed} the set function or undefined - * @api private - */ - get: function get() { - var listener = this.listeners(method)[0]; - return listener ? (listener._listener ? listener._listener : listener) : undefined; - }, - - /** - * Start listening for events - * - * @param {Function} listener the listener - * @return {Mixed} the set function or undefined - * @api private - */ - set: function set(listener) { - this.removeAllListeners(method); - this.addEventListener(method, listener); - } - }); -}); - -/** - * Ready states - */ -Object.defineProperty(EventSource, 'CONNECTING', { enumerable: true, value: 0 }); -Object.defineProperty(EventSource, 'OPEN', { enumerable: true, value: 1 }); -Object.defineProperty(EventSource, 'CLOSED', { enumerable: true, value: 2 }); - -EventSource.prototype.CONNECTING = 0; -EventSource.prototype.OPEN = 1; -EventSource.prototype.CLOSED = 2; - -/** - * Closes the connection, if one is made, and sets the readyState attribute to 2 (closed) - * - * @see https://developer.mozilla.org/en-US/docs/Web/API/EventSource/close - * @api public - */ -EventSource.prototype.close = function () { - this._close(); -}; - -/** - * Emulates the W3C Browser based WebSocket interface using addEventListener. - * - * @param {String} type A string representing the event type to listen out for - * @param {Function} listener callback - * @see https://developer.mozilla.org/en/DOM/element.addEventListener - * @see http://dev.w3.org/html5/websockets/#the-websocket-interface - * @api public - */ -EventSource.prototype.addEventListener = function addEventListener(type, listener) { - if (typeof listener === 'function') { - // store a reference so we can return the original function again - listener._listener = listener; - this.on(type, listener); - } -}; - -/** - * Emulates the W3C Browser based WebSocket interface using dispatchEvent. - * - * @param {Event} event An event to be dispatched - * @see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent - * @api public - */ -EventSource.prototype.dispatchEvent = function dispatchEvent(event) { - if (!event.type) { - throw new Error('UNSPECIFIED_EVENT_TYPE_ERR'); - } - // if event is instance of an CustomEvent (or has 'details' property), - // send the detail object as the payload for the event - this.emit(event.type, event.detail); -}; - -/** - * Emulates the W3C Browser based WebSocket interface using removeEventListener. - * - * @param {String} type A string representing the event type to remove - * @param {Function} listener callback - * @see https://developer.mozilla.org/en/DOM/element.removeEventListener - * @see http://dev.w3.org/html5/websockets/#the-websocket-interface - * @api public - */ -EventSource.prototype.removeEventListener = function removeEventListener(type, listener) { - if (typeof listener === 'function') { - listener._listener = undefined; - this.removeListener(type, listener); - } -}; - -/** - * W3C Event - * - * @see http://www.w3.org/TR/DOM-Level-3-Events/#interface-Event - * @api private - */ -function Event(type, optionalProperties) { - Object.defineProperty(this, 'type', { writable: false, value: type, enumerable: true }); - if (optionalProperties) { - for (var f in optionalProperties) { - if (optionalProperties.hasOwnProperty(f)) { - Object.defineProperty(this, f, { writable: false, value: optionalProperties[f], enumerable: true }); - } - } - } -} - -/** - * W3C MessageEvent - * - * @see http://www.w3.org/TR/webmessaging/#event-definitions - * @api private - */ -function MessageEvent(type, eventInitDict) { - Object.defineProperty(this, 'type', { writable: false, value: type, enumerable: true }); - for (var f in eventInitDict) { - if (eventInitDict.hasOwnProperty(f)) { - Object.defineProperty(this, f, { writable: false, value: eventInitDict[f], enumerable: true }); - } - } -} - -/** - * Returns a new object of headers that does not include any authorization and cookie headers - * - * @param {Object} headers An object of headers ({[headerName]: headerValue}) - * @return {Object} a new object of headers - * @api private - */ -function removeUnsafeHeaders(headers) { - var safe = {}; - for (var key in headers) { - if (/^(cookie|authorization)$/i.test(key)) { - continue; - } - - safe[key] = headers[key]; - } - - return safe; -} - -/** - * Transform an URL to a valid origin value. - * - * @param {String|Object} url URL to transform to it's origin. - * @returns {String} The origin. - * @api private - */ -function getOrigin(url) { - if (typeof url === 'string') url = parse(url); - if (!url.protocol || !url.hostname) return 'null'; - return (url.protocol + '//' + url.host).toLowerCase(); -} diff --git a/src/platform/getEventSource/node.js b/src/platform/getEventSource/node.js deleted file mode 100644 index 4ef41d0af..000000000 --- a/src/platform/getEventSource/node.js +++ /dev/null @@ -1,20 +0,0 @@ -let __isCustom = false; -let __eventSource = undefined; - -// This function is only exposed for testing purposes. -export function __setEventSource(eventSource) { - __eventSource = eventSource; - __isCustom = true; -} -export function __restore() { - __isCustom = false; -} - -export function getEventSource() { - // returns EventSource at `eventsource` package. If not available, return global EventSource or undefined - try { - return __isCustom ? __eventSource : require('./eventsource'); - } catch (error) { - return typeof EventSource === 'function' ? EventSource : undefined; - } -} diff --git a/src/platform/getEventSource/package.json b/src/platform/getEventSource/package.json deleted file mode 100644 index a19625907..000000000 --- a/src/platform/getEventSource/package.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "main": "./node.js", - "browser": "./browser.js" -} diff --git a/src/platform/getFetch/__tests__/browser.spec.js b/src/platform/getFetch/__tests__/browser.spec.js deleted file mode 100644 index a9499ded0..000000000 --- a/src/platform/getFetch/__tests__/browser.spec.js +++ /dev/null @@ -1,8 +0,0 @@ -import tape from 'tape-catch'; -import { getFetch } from '../browser'; - -tape('getFetch returns global fetch in Browser', assert => { - assert.equal(getFetch(), fetch); - - assert.end(); -}); diff --git a/src/platform/getFetch/__tests__/node.spec.js b/src/platform/getFetch/__tests__/node.spec.js deleted file mode 100644 index 144d8880c..000000000 --- a/src/platform/getFetch/__tests__/node.spec.js +++ /dev/null @@ -1,8 +0,0 @@ -import tape from 'tape-catch'; -import { getFetch } from '../node'; - -tape('getFetch returns node-fetch module in Node', assert => { - assert.equal(getFetch(), require('node-fetch')); - - assert.end(); -}); diff --git a/src/platform/getFetch/browser.js b/src/platform/getFetch/browser.js deleted file mode 100644 index 5db3b9484..000000000 --- a/src/platform/getFetch/browser.js +++ /dev/null @@ -1,5 +0,0 @@ -import unfetch from 'unfetch'; - -export function getFetch() { - return typeof fetch === 'function' ? fetch : unfetch; -} diff --git a/src/platform/getFetch/node.js b/src/platform/getFetch/node.js deleted file mode 100644 index b1b97c02a..000000000 --- a/src/platform/getFetch/node.js +++ /dev/null @@ -1,24 +0,0 @@ -let nodeFetch; - -try { - nodeFetch = require('node-fetch'); - - // Handle node-fetch issue https://github.com/node-fetch/node-fetch/issues/1037 - if (typeof nodeFetch !== 'function') nodeFetch = nodeFetch.default; - -} catch (error) { - // Try to access global fetch if `node-fetch` package couldn't be imported (e.g., not in a Node environment) - nodeFetch = typeof fetch === 'function' ? fetch : undefined; -} - -// This function is only exposed for testing purposes. -export function __setFetch(fetch) { - nodeFetch = fetch; -} - -/** - * Retrieves 'node-fetch', a Fetch API polyfill for Node.js, with fallback to global 'fetch' if available. - */ -export function getFetch() { - return nodeFetch; -} diff --git a/src/platform/getFetch/package.json b/src/platform/getFetch/package.json deleted file mode 100644 index a19625907..000000000 --- a/src/platform/getFetch/package.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "main": "./node.js", - "browser": "./browser.js" -} diff --git a/src/platform/getOptions/__tests__/node.spec.js b/src/platform/getOptions/__tests__/node.spec.js deleted file mode 100644 index eaeb2b5b7..000000000 --- a/src/platform/getOptions/__tests__/node.spec.js +++ /dev/null @@ -1,25 +0,0 @@ -import tape from 'tape-catch'; -import { settingsFactory } from '../../../settings/node'; -import { getOptions } from '../node'; - -tape('getOptions returns an object with a custom agent if all urls are https', assert => { - const settings = settingsFactory({}); - assert.true(typeof getOptions(settings).agent === 'object'); - - assert.end(); -}); - -tape('getOptions returns undefined if some url is not https', assert => { - const settings = settingsFactory({ urls: { sdk: 'http://sdk.split.io' } }); - assert.equal(getOptions(settings), undefined); - - assert.end(); -}); - -tape('getOptions returns the provided options from settings', assert => { - const customRequestOptions = { agent: false }; - const settings = settingsFactory({ sync: { requestOptions: customRequestOptions } }); - assert.equal(getOptions(settings), customRequestOptions); - - assert.end(); -}); diff --git a/src/platform/getOptions/node.js b/src/platform/getOptions/node.js deleted file mode 100644 index 7c693af08..000000000 --- a/src/platform/getOptions/node.js +++ /dev/null @@ -1,23 +0,0 @@ -// @TODO -// 1- handle multiple protocols automatically -// 2- destroy it once the sdk is destroyed -import https from 'https'; - -import { find } from '@splitsoftware/splitio-commons/src/utils/lang'; - -const agent = new https.Agent({ - keepAlive: true, - keepAliveMsecs: 1500 -}); - -export function getOptions(settings) { - // User provided options take precedence - if (settings.sync.requestOptions) return settings.sync.requestOptions; - - // If some URL is not HTTPS, we don't use the agent, to let the SDK connect to HTTP endpoints - if (find(settings.urls, url => !url.startsWith('https:'))) return; - - return { - agent - }; -} diff --git a/src/platform/node.js b/src/platform/node.js index 8572e70f1..cdfc2f33a 100644 --- a/src/platform/node.js +++ b/src/platform/node.js @@ -1,16 +1 @@ -import EventEmitter from 'events'; -import { getFetch } from '../platform/getFetch/node'; -import { getEventSource } from '../platform/getEventSource/node'; -import { getOptions } from '../platform/getOptions/node'; -import { NodeSignalListener } from '@splitsoftware/splitio-commons/src/listeners/node'; -import { now } from '@splitsoftware/splitio-commons/src/utils/timeTracker/now/node'; - -export const platform = { - getFetch, - getEventSource, - getOptions, - EventEmitter, - now -}; - -export const SignalListener = NodeSignalListener; +export { platform } from '@splitsoftware/splitio-commons/src/platform/node'; diff --git a/src/settings/defaults/version.js b/src/settings/defaults/version.js index ed7de9b26..75e148172 100644 --- a/src/settings/defaults/version.js +++ b/src/settings/defaults/version.js @@ -1 +1 @@ -export const packageVersion = '11.11.1'; +export const packageVersion = '11.11.2-rc.0';