@splitsoftware/splitio 10.16.1-rc.0 → 10.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,11 +4,14 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
 
5
5
  exports.__esModule = true;
6
6
  exports.validateAttributes = validateAttributes;
7
+ exports.validateAttributesDeep = validateAttributesDeep;
7
8
 
8
9
  var _lang = require("../lang");
9
10
 
10
11
  var _logger = _interopRequireDefault(require("../logger"));
11
12
 
13
+ var _attribute = require("./attribute");
14
+
12
15
  var log = (0, _logger.default)('');
13
16
 
14
17
  function validateAttributes(maybeAttrs, method) {
@@ -17,4 +20,13 @@ function validateAttributes(maybeAttrs, method) {
17
20
  return maybeAttrs;
18
21
  log.error(method + ": attributes must be a plain object.");
19
22
  return false;
23
+ }
24
+
25
+ function validateAttributesDeep(maybeAttributes, method) {
26
+ if (!validateAttributes(maybeAttributes, method)) return false;
27
+ var result = true;
28
+ Object.keys(maybeAttributes).forEach(function (attributeKey) {
29
+ if (!(0, _attribute.validateAttribute)(attributeKey, maybeAttributes[attributeKey], method)) result = false;
30
+ });
31
+ return result;
20
32
  }
@@ -44,7 +44,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44
44
  See the License for the specific language governing permissions and
45
45
  limitations under the License.
46
46
  **/
47
- var version = '10.16.1-rc.0';
47
+ var version = '10.17.0';
48
48
  var eventsEndpointMatcher = /^\/(testImpressions|metrics|events)/;
49
49
  var authEndpointMatcher = /^\/v2\/auth/;
50
50
  var streamingEndpointMatcher = /^\/(sse|event-stream)/;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@splitsoftware/splitio",
3
- "version": "10.16.1-rc.0",
3
+ "version": "10.17.0",
4
4
  "description": "Split SDK",
5
5
  "files": [
6
6
  "README.md",
@@ -46,7 +46,7 @@
46
46
  "eventsource": "^1.0.7"
47
47
  },
48
48
  "devDependencies": {
49
- "@babel/cli": "^7.14.8",
49
+ "@babel/cli": "^7.15.7",
50
50
  "@babel/core": "^7.13.10",
51
51
  "@babel/plugin-transform-runtime": "^7.13.10",
52
52
  "@babel/preset-env": "^7.13.10",
@@ -55,8 +55,8 @@
55
55
  "copyfiles": "^2.4.1",
56
56
  "cross-env": "^7.0.3",
57
57
  "csv-streamify": "4.0.0",
58
- "eslint": "^6.8.0",
59
- "eslint-plugin-compat": "^3.9.0",
58
+ "eslint": "^8.0.1",
59
+ "eslint-plugin-compat": "^3.13.0",
60
60
  "fetch-mock": "^9.11.0",
61
61
  "karma": "^6.3.2",
62
62
  "karma-chrome-launcher": "^3.1.0",
@@ -94,7 +94,7 @@
94
94
  "check:version": "cross-env NODE_ENV=test tape -r @babel/register src/utils/__tests__/settings/index.spec.js",
95
95
  "test-browser-local": "cross-env NODE_ENV=test karma start karma/local.karma.conf.js",
96
96
  "test-browser-e2e-local": "cross-env NODE_ENV=test karma start karma/e2e.local.karma.conf.js",
97
- "test-browser": "npm run test-browser-unit && npm run test-browser-online && npm run test-browser-offline && npm run test-browser-destroy && npm run test-browser-errors && npm run test-browser-push",
97
+ "test-browser": "npm run test-browser-unit && npm run test-browser-online && npm run test-browser-offline && npm run test-browser-destroy && npm run test-browser-errors && npm run test-browser-push && npm run test-browser-gaintegration",
98
98
  "test-browser-unit": "cross-env NODE_ENV=test karma start karma/ci.karma.conf.js",
99
99
  "test-browser-online": "cross-env NODE_ENV=test karma start karma/e2e.ci.karma.conf.js",
100
100
  "test-browser-offline": "cross-env NODE_ENV=test karma start karma/offline.karma.conf.js",
@@ -113,9 +113,7 @@
113
113
  "pretest-ts-decls": "npm run build-es && npm run build-cjs && npm link",
114
114
  "test-ts-decls": "./scripts/ts-tests.sh",
115
115
  "posttest-ts-decls": "npm unlink && npm install",
116
- "browser-test-suite": "npm run test-browser-ci && npm run test-browser-e2e-ci && npm run test-browser-offline && npm run test-browser-destroy && npm run test-browser-errors && npm run test-browser-gaintegration",
117
- "node-test-suite": "npm run test-node && npm run test-node-e2e && npm run test-node-destroy && npm run test-node-offline && npm run test-node-redis && npm run test-node-errors",
118
- "test": "npm run browser-test-suite && npm run node-test-suite",
116
+ "test": "npm run test-node && npm run test-browser",
119
117
  "publish:rc": "npm run check && npm run build && npm publish --tag canary",
120
118
  "publish:stable": "npm run check && npm run build && npm publish"
121
119
  },
@@ -0,0 +1,112 @@
1
+ import ClientInputValidationLayer from './inputValidation';
2
+ import AttributesCacheInMemory from '../storage/AttributesCache/InMemory';
3
+ import { validateAttributesDeep } from '../utils/inputValidation/attributes';
4
+ import logFactory from '../utils/logger';
5
+ import objectAssign from 'object-assign';
6
+ const log = logFactory('splitio-client');
7
+
8
+ /**
9
+ * Add in memory attributes storage methods and combine them with any attribute received from the getTreatment/s call
10
+ */
11
+ export default function ClientAttributesDecorationLayer(context, isKeyBinded, isTTBinded) {
12
+
13
+ const client = ClientInputValidationLayer(context, isKeyBinded, isTTBinded);
14
+
15
+ const attributeStorage = new AttributesCacheInMemory();
16
+
17
+ // Keep a reference to the original methods
18
+ const clientGetTreatment = client.getTreatment;
19
+ const clientGetTreatmentWithConfig = client.getTreatmentWithConfig;
20
+ const clientGetTreatments = client.getTreatments;
21
+ const clientGetTreatmentsWithConfig = client.getTreatmentsWithConfig;
22
+
23
+ /**
24
+ * Add an attribute to client's in memory attributes storage
25
+ *
26
+ * @param {string} attributeName Attrinute name
27
+ * @param {string, number, boolean, list} attributeValue Attribute value
28
+ * @returns {boolean} true if the attribute was stored and false otherways
29
+ */
30
+ client.setAttribute = (attributeName, attributeValue) => {
31
+ const attribute = {};
32
+ attribute[attributeName] = attributeValue;
33
+ if (!validateAttributesDeep(attribute)) return false;
34
+ log.debug(`stored ${attributeValue} for attribute ${attributeName}`);
35
+ return attributeStorage.setAttribute(attributeName, attributeValue);
36
+ };
37
+
38
+ /**
39
+ * Returns the attribute with the given key
40
+ *
41
+ * @param {string} attributeName Attribute name
42
+ * @returns {Object} Attribute with the given key
43
+ */
44
+ client.getAttribute = (attributeName) => {
45
+ log.debug(`retrieved attribute ${attributeName}`);
46
+ return attributeStorage.getAttribute(attributeName + '');
47
+ };
48
+
49
+ /**
50
+ * Add to client's in memory attributes storage the attributes in 'attributes'
51
+ *
52
+ * @param {Object} attributes Object with attributes to store
53
+ * @returns true if attributes were stored an false otherways
54
+ */
55
+ client.setAttributes = (attributes) => {
56
+ if (!validateAttributesDeep(attributes)) return false;
57
+ return attributeStorage.setAttributes(attributes);
58
+ };
59
+
60
+ /**
61
+ * Return all the attributes stored in client's in memory attributes storage
62
+ *
63
+ * @returns {Object} returns all the stored attributes
64
+ */
65
+ client.getAttributes = () => {
66
+ return attributeStorage.getAll();
67
+ };
68
+
69
+ /**
70
+ * Removes from client's in memory attributes storage the attribute with the given key
71
+ *
72
+ * @param {string} attributeName
73
+ * @returns {boolean} true if attribute was removed and false otherways
74
+ */
75
+ client.removeAttribute = (attributeName) => {
76
+ log.debug(`removed attribute ${attributeName}`);
77
+ return attributeStorage.removeAttribute(attributeName + '');
78
+ };
79
+
80
+ /**
81
+ * Remove all the stored attributes in the client's in memory attribute storage
82
+ */
83
+ client.clearAttributes = () => {
84
+ return attributeStorage.clear();
85
+ };
86
+
87
+ client.getTreatment = (maybeKey, maybeSplit, maybeAttributes) => {
88
+ return clientGetTreatment(maybeKey, maybeSplit, combineAttributes(maybeAttributes));
89
+ };
90
+
91
+ client.getTreatmentWithConfig = (maybeKey, maybeSplit, maybeAttributes) => {
92
+ return clientGetTreatmentWithConfig(maybeKey, maybeSplit, combineAttributes(maybeAttributes));
93
+ };
94
+
95
+ client.getTreatments = (maybeKey, maybeSplits, maybeAttributes) => {
96
+ return clientGetTreatments(maybeKey, maybeSplits, combineAttributes(maybeAttributes));
97
+ };
98
+
99
+ client.getTreatmentsWithConfig = (maybeKey, maybeSplits, maybeAttributes) => {
100
+ return clientGetTreatmentsWithConfig(maybeKey, maybeSplits, combineAttributes(maybeAttributes));
101
+ };
102
+
103
+ function combineAttributes(maybeAttributes) {
104
+ const storedAttributes = attributeStorage.getAll();
105
+ if (Object.keys(storedAttributes).length > 0) {
106
+ return objectAssign({}, storedAttributes, maybeAttributes);
107
+ }
108
+ return maybeAttributes;
109
+ }
110
+
111
+ return client;
112
+ }
@@ -1,5 +1,5 @@
1
1
  import { get } from '../utils/lang';
2
- import ClientWithInputValidationLayer from './inputValidation';
2
+ import ClientAttributesDecorationLayer from './attributesDecoration';
3
3
  import { LOCALHOST_MODE } from '../utils/constants';
4
4
  import {
5
5
  validateKey,
@@ -25,7 +25,7 @@ function BrowserClientFactory(context) {
25
25
  trackBindings.push(tt);
26
26
  }
27
27
 
28
- const client = ClientWithInputValidationLayer(context, true, trackBindings.length > 1);
28
+ const client = ClientAttributesDecorationLayer(context, true, trackBindings.length > 1);
29
29
  client.isBrowserClient = true;
30
30
 
31
31
  // In the browser land, we can bind the key and the traffic type (if provided)
@@ -17,7 +17,6 @@ limitations under the License.
17
17
  import Engine from '../';
18
18
  import thenable from '../../utils/promise/thenable';
19
19
  import * as LabelsConstants from '../../utils/labels';
20
- import { get } from '../../utils/lang';
21
20
  import { CONTROL } from '../../utils/constants';
22
21
 
23
22
  const treatmentException = {
@@ -103,17 +102,17 @@ function getEvaluation(
103
102
  const split = Engine.parse(splitJSON, storage);
104
103
  evaluation = split.getTreatment(key, attributes, evaluateFeature);
105
104
 
106
- // If the storage is async, evaluation and changeNumber will return a thenable
105
+ // If the storage is async and the evaluated split uses segment, evaluation is thenable
107
106
  if (thenable(evaluation)) {
108
107
  return evaluation.then(result => {
109
108
  result.changeNumber = split.getChangeNumber();
110
- result.config = get(splitJSON, `configurations.${result.treatment}`, null);
109
+ result.config = splitJSON.configurations && splitJSON.configurations[result.treatment] || null;
111
110
 
112
111
  return result;
113
112
  });
114
113
  } else {
115
114
  evaluation.changeNumber = split.getChangeNumber(); // Always sync and optional
116
- evaluation.config = get(splitJSON, `configurations.${evaluation.treatment}`, null);
115
+ evaluation.config = splitJSON.configurations && splitJSON.configurations[evaluation.treatment] || null;
117
116
  }
118
117
  }
119
118
 
@@ -0,0 +1,75 @@
1
+ import objectAssign from 'object-assign';
2
+
3
+ class AttributesCacheInMemory {
4
+
5
+ constructor() {
6
+ this.attributesCache = {};
7
+ }
8
+
9
+ /**
10
+ * Create or update the value for the given attribute
11
+ *
12
+ * @param {string} attributeName attribute name
13
+ * @param {Object} attributeValue attribute value
14
+ * @returns {boolean} the attribute was stored
15
+ */
16
+ setAttribute(attributeName, attributeValue) {
17
+ this.attributesCache[attributeName] = attributeValue;
18
+ return true;
19
+ }
20
+
21
+ /**
22
+ * Retrieves the value of a given attribute
23
+ *
24
+ * @param {string} attributeName attribute name
25
+ * @returns {Object?} stored attribute value
26
+ */
27
+ getAttribute(attributeName) {
28
+ return this.attributesCache[attributeName];
29
+ }
30
+
31
+ /**
32
+ * Create or update all the given attributes
33
+ *
34
+ * @param {[string, Object]} attributes attributes to create or update
35
+ * @returns {boolean} attributes were stored
36
+ */
37
+ setAttributes(attributes) {
38
+ this.attributesCache = objectAssign(this.attributesCache, attributes);
39
+ return true;
40
+ }
41
+
42
+ /**
43
+ * Retrieve the full attributes map
44
+ *
45
+ * @returns {Map<string, Object>} stored attributes
46
+ */
47
+ getAll() {
48
+ return this.attributesCache;
49
+ }
50
+
51
+ /**
52
+ * Removes a given attribute from the map
53
+ *
54
+ * @param {string} attributeName attribute to remove
55
+ * @returns {boolean} attribute removed
56
+ */
57
+ removeAttribute(attributeName) {
58
+ if (Object.keys(this.attributesCache).indexOf(attributeName) >= 0) {
59
+ delete this.attributesCache[attributeName];
60
+ return true;
61
+ }
62
+ return false;
63
+ }
64
+
65
+ /**
66
+ * Clears all attributes stored in the SDK
67
+ *
68
+ */
69
+ clear() {
70
+ this.attributesCache = {};
71
+ }
72
+
73
+ }
74
+
75
+ export default AttributesCacheInMemory;
@@ -0,0 +1,22 @@
1
+ import { isString, numberIsFinite, isBoolean } from '../lang';
2
+ import logFactory from '../logger';
3
+ const log = logFactory('');
4
+
5
+ export function validateAttribute(attributeKey, attributeValue, method) {
6
+ if (!isString(attributeKey) || attributeKey.length === 0){
7
+ log.warn(`${method}: you passed an invalid attribute name, attribute name must be a non-empty string.`);
8
+ return false;
9
+ }
10
+
11
+ const isStringVal = isString(attributeValue);
12
+ const isFiniteVal = numberIsFinite(attributeValue);
13
+ const isBoolVal = isBoolean(attributeValue);
14
+ const isArrayVal = Array.isArray(attributeValue);
15
+
16
+ if (!(isStringVal || isFiniteVal || isBoolVal || isArrayVal)) { // If it's not of valid type.
17
+ log.warn(`${method}: you passed an invalid attribute value for ${attributeKey}. Acceptable types are: string, number, boolean and array of strings.`);
18
+ return false;
19
+ }
20
+
21
+ return true;
22
+ }
@@ -1,5 +1,6 @@
1
1
  import { isObject } from '../lang';
2
2
  import logFactory from '../logger';
3
+ import { validateAttribute } from './attribute';
3
4
  const log = logFactory('');
4
5
 
5
6
  export function validateAttributes(maybeAttrs, method) {
@@ -10,3 +11,16 @@ export function validateAttributes(maybeAttrs, method) {
10
11
  log.error(`${method}: attributes must be a plain object.`);
11
12
  return false;
12
13
  }
14
+
15
+ export function validateAttributesDeep(maybeAttributes, method) {
16
+ if (!validateAttributes(maybeAttributes, method)) return false;
17
+
18
+ let result = true;
19
+ Object.keys(maybeAttributes).forEach(attributeKey => {
20
+ if (!validateAttribute(attributeKey, maybeAttributes[attributeKey], method))
21
+ result = false;
22
+ });
23
+
24
+ return result;
25
+
26
+ }
@@ -27,7 +27,7 @@ import { API } from '../../utils/logger';
27
27
  import { STANDALONE_MODE, STORAGE_MEMORY, CONSUMER_MODE, OPTIMIZED } from '../../utils/constants';
28
28
  import validImpressionsMode from './impressionsMode';
29
29
 
30
- const version = '10.16.1-rc.0';
30
+ const version = '10.17.0';
31
31
  const eventsEndpointMatcher = /^\/(testImpressions|metrics|events)/;
32
32
  const authEndpointMatcher = /^\/v2\/auth/;
33
33
  const streamingEndpointMatcher = /^\/(sse|event-stream)/;
package/types/index.d.ts CHANGED
@@ -24,5 +24,5 @@ declare module JsSdk {
24
24
  * The settings parameter should be an object that complies with the SplitIO.IBrowserSettings.
25
25
  * For more information read the corresponding article: @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#configuration}
26
26
  */
27
- export function SplitFactory(settings: SplitIO.IBrowserSettings): SplitIO.ISDK;
27
+ export function SplitFactory(settings: SplitIO.IBrowserSettings): SplitIO.IBrowserSDK;
28
28
  }