Skip to content

Commit

Permalink
First prototype of public engine api. Pending more work to complete t…
Browse files Browse the repository at this point in the history
…he first bundle version for the browser.
  • Loading branch information
facundocabrera committed Jan 15, 2016
1 parent a52b618 commit cdc832f
Show file tree
Hide file tree
Showing 48 changed files with 148 additions and 54 deletions.
12 changes: 11 additions & 1 deletion packages/split-cache/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
.idea
.DS_Store
/coverage
/npm-debug.log
/.idea
*~
/node_modules
*.swp

# transpiled code directories
/src
/test/src
/test/src
1 change: 1 addition & 0 deletions packages/split-cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# SPLIT Cache
1 change: 1 addition & 0 deletions packages/split-cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "split-cache",
"version": "0.0.1",
"description": "Split Engine",
"readme": "README.md",
"author": "Facundo Cabrera <[email protected]>",
"homepage": "https://github.com/splitio/javascript-client",
"license": "Apache-2.0",
Expand Down
12 changes: 12 additions & 0 deletions packages/split-engine/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.idea
.DS_Store
/coverage
/npm-debug.log
/.idea
*~
/node_modules
*.swp

# transpiled code directories
/src
/test/src
7 changes: 7 additions & 0 deletions packages/split-engine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPLIT Engine

This package is responsible of parse the AST of SPLIT DSL in order to be able
to evaluate if a given key is enabled given a feature to be evaluated.

If you are looking for how we manage data storage (AST storage basically), you
should be looking into the package `split-cache`.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

function andContext(predicates) {
function andContext(predicates /*: Array<function> */) {

return function andCombinerEvaluator(key, seed) {
return function andCombinerEvaluator(key /*: string */, seed /*: number */) {
for (let evaluator of predicates) {
if (evaluator(key, seed)) return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var murmur = require('murmurhash-js');
var engine = {
/**
* Defines how much error we could have at the moment we run percentage calculations.
* => For now, we consider 0.1% acceptable.
*/
TOLERANCE: 0.1,
TOLERANCE: 1, // For now, we consider 1% acceptable.

/**
* Get the treatment name given a key, and the seed of the feature.
Expand All @@ -19,8 +18,8 @@ var engine = {
*
* @return {boolean}
*/
isOn(key, seed, partitions) {
return partitions.get(partitionTypes.enum.ON) >= (murmur(key, seed) % 100 + 1);
isOn(key /*: string */, seed /*: number */, partitions /*: Map */) {
return partitions.get(partitionTypes.enum.ON) >= (murmur(key, seed) % 100);
}
};

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "split-parser",
"name": "split-engine",
"version": "0.0.1",
"description": "Split AST parser",
"description": "Split Engine",
"readme": "README.md",
"author": "Facundo Cabrera <[email protected]>",
"homepage": "https://github.com/splitio/javascript-client",
"license": "Apache-2.0",
"repository": "https://github.com/splitio/javascript-client/tree/master/packages/split-parser",
"repository": "https://github.com/splitio/javascript-client/tree/master/packages/split-engine",
"main": "src/index.js",
"scripts": {
"build": "babel es6 --out-dir src --source-maps",
Expand All @@ -19,12 +20,12 @@
"babel-plugin-syntax-object-rest-spread": "^6.3.13",
"babel-plugin-transform-object-rest-spread": "^6.3.13",
"babel-preset-es2015": "^6.3.13",
"tape": "^4.2.2",
"murmurhash-js": "^1.0.0"
},
"devDependencies": {
"eslint": "^1.10.3",
"jscs": "^2.7.0"
"jscs": "^2.7.0",
"tape": "^4.2.2"
},
"babel": {
"presets": [
Expand Down
27 changes: 27 additions & 0 deletions packages/split-engine/test/es6/combiners/and.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var tape = require('tape');
var andCombinerFactory = require('split-engine/src/combiners/and');

tape('', (assert) => {
let inputKey = 'sample';
let inputSeed = 1234;
let evaluationResult = true;

function evaluator(key, seed) {
assert.true(key === inputKey, 'key should be equals');
assert.true(seed === inputSeed, 'seed should be equals');

return evaluationResult;
}

let predicates = [evaluator];

let andCombinerEvaluator = andCombinerFactory(predicates);

assert.true(
andCombinerEvaluator(inputKey, inputSeed) === evaluationResult,
`evaluator should return ${evaluationResult}`
);
assert.end();
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var tape = require('tape');
var engine = require('split-parser/src/engine');
var partitionTypes = require('split-parser/src/partitions/types');
var engine = require('split-engine/src/engine');
var partitionTypes = require('split-engine/src/partitions/types');
var keys = require('./mocks/1000_keys_10_chart_length');

tape('The engine should evaluates always true', function (assert) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var matcherTypes = require('split-parser/src/matchers/types');
var matcherFactory = require('split-parser/src/matchers');
var matcherTypes = require('split-engine/src/matchers/types');
var matcherFactory = require('split-engine/src/matchers');
var tape = require('tape');

tape('Matcher ALL should always return true', function (assert) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var matcherTypes = require('split-parser/src/matchers/types');
var matcherFactory = require('split-parser/src/matchers');
var matcherTypes = require('split-engine/src/matchers/types');
var matcherFactory = require('split-engine/src/matchers');
var tape = require('tape');

tape('Matcher WHITELIST should return true ONLY when the key is defined', function (assert) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var parser = require('split-parser/src/parser/condition');
var parser = require('split-engine/src/parser/condition');
var tape = require('tape');

tape('if user is in segment all 100%:on', function (assert) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var transform = require('split-parser/src/transforms/partitions');
var partitionTypes = require('split-parser/src/partitions/types');
var transform = require('split-engine/src/transforms/partitions');
var partitionTypes = require('split-engine/src/partitions/types');
var tape = require('tape');

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

let transform = require('split-parser/src/transforms/whitelist');
let transform = require('split-engine/src/transforms/whitelist');
let tape = require('tape');

tape('a whitelist Array should be casted into a Set', function (assert) {
Expand Down
2 changes: 0 additions & 2 deletions packages/split-parser/.gitignore

This file was deleted.

10 changes: 9 additions & 1 deletion packages/split/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
.idea
.DS_Store
/coverage
/npm-debug.log
/.idea
*~
/node_modules
*.swp
/src
/test/src
/test/src
1 change: 1 addition & 0 deletions packages/split/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# SPLIT SDK
24 changes: 24 additions & 0 deletions packages/split/es6/core/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

var writer = require('split-cache').writer;

var core = {
schedule(fn /*: function */, delay /*: number */, ...params /*:? Array<any> */) {
setTimeout(fn, delay, ...params);
},

start(authorizationKey /*: string */) {
return writer(authorizationKey).then(storage => {
if (process.env.NODE_ENV === 'development') {
storage.print();
}

// fire cache updater each 5 seconds
this.schedule(writer, 5000, authorizationKey);

return storage;
});
}
};

module.exports = core;
1 change: 1 addition & 0 deletions packages/split/es6/core/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./browser');
4 changes: 4 additions & 0 deletions packages/split/es6/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "node.js",
"browser": "browser.js"
}
18 changes: 18 additions & 0 deletions packages/split/es6/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var core = require('./core');

function splitter(authorizationKey /*: string */) {
return core.start(authorizationKey).then(storage => {
return {
/**
* Evaluates is a given userId is enabled for a given feature.
*/
isOn(userId /*: string */, featureName /*: string */) {
return storage.getSplit(featureName).evaluate(userId);
}
};
});
}

module.exports = splitter;
26 changes: 0 additions & 26 deletions packages/split/index.js

This file was deleted.

6 changes: 6 additions & 0 deletions packages/split/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "split",
"version": "0.0.1",
"description": "Split Engine",
"readme": "README.md",
"author": "Facundo Cabrera <[email protected]>",
"homepage": "https://github.com/splitio/javascript-client",
"license": "Apache-2.0",
Expand All @@ -23,9 +24,14 @@
"tape": "^4.2.2"
},
"devDependencies": {
"browserify": "^13.0.0",
"eslint": "^1.10.3",
"jscs": "^2.7.0"
},
"engines": {
"node": ">=0.12",
"npm" : "^3"
},
"babel": {
"presets": [
"es2015"
Expand Down
8 changes: 5 additions & 3 deletions scripts/development.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/usr/bin/env bash

npm link packages/split
npm link packages/split-engine
npm link packages/split-cache
npm link packages/split-parser
npm link packages/split

cd packages/split-cache
rm -rf src/
npm install
npm run watch &
npm run watch-test &
cd -

cd packages/split-parser
cd packages/split-engine
rm -rf src/
npm install
npm run watch &
npm run watch-test &
cd -

0 comments on commit cdc832f

Please sign in to comment.