Skip to content

Commit

Permalink
feat: ✨ onAfter and onBefore hooks, to mangle input/output
Browse files Browse the repository at this point in the history
  • Loading branch information
puria committed Jan 27, 2021
1 parent a8df30c commit 1af065f
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/master/cspell.schema.json",
"language": "en",
"words": [
"zencodes",
"keypair",
"ecdh",
"Zenroom",
"decidiamo",
"zencode",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ The list of the attributes are:

## 📋 Testing

``yarn test`
`yarn test`

**[🔝 back to top](#toc)**

Expand Down
28 changes: 16 additions & 12 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const {execute} = require('./build/main/index')
const bench = require('nanobench')
const { execute } = require('./build/main/index');
const bench = require('nanobench');

const signPetition = async () => {
const account = `{"username": "Alice"}`
const participants = `{ "participants": [ "jaromil@dyne.org", "puria@dyne.org", "andrea@dyne.org" ] }`
const account = `{"username": "Alice"}`;
const participants = `{ "participants": [ "jaromil@dyne.org", "puria@dyne.org", "andrea@dyne.org" ] }`;

const participant_email = `{"email": "bob@wonder.land", "petition_uid": "More privacy for all!"}`
const participant_email = `{"email": "bob@wonder.land", "petition_uid": "More privacy for all!"}`;
const steps = {
verbose: false,
conf: 'memmanager=lw',
Expand Down Expand Up @@ -156,15 +156,19 @@ and print the 'public key' inside 'keypair'
],
};

(await execute(steps));
await execute(steps);
};

bench('petition flow 200.000 times', function (b) {
b.start()
const tries = 150;

for (var i = 0; i < 100; i++) {
(async () => { await signPetition() })()
bench(`petition flow ${tries} times`, function (b) {
b.start();

for (var i = 0; i < tries; i++) {
(async () => {
await signPetition();
})();
}

b.end()
})
b.end();
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"sinon": "^9.2.3",
"typedoc": "^0.20.16",
"typescript": "^4.0.2",
"wip": "^1.3.0",
"zenroom": "^2.1.0-ee1b243"
},
"plugins": [
Expand Down
34 changes: 34 additions & 0 deletions src/lib/chain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,37 @@ test('keyTransform should work', async (t) => {
const result = await execute(steps);
t.is(result, '{"hello":"world"}');
});

test('callbacks should work', async (t) => {
let before = false;
let after = false;
let afterResult = '';

const steps = {
verbose: true,
steps: [
{
id: 'some',
conf: 'memmanager=lw',
zencode: `Given that I have a 'string' named 'hello'
Then print all data as 'string'`,
keys: JSON.stringify({ hello: 'world' }),
onBefore: () => {
before = true;
},
onAfter: (result: string) => {
after = true;
afterResult = result;
},
keysTransform: (k: string) => {
return JSON.stringify(JSON.parse(k));
},
},
],
};
const result = await execute(steps);
t.true(before);
t.true(after);
t.is(afterResult, '{"hello":"world"}');
t.is(result, '{"hello":"world"}');
});
35 changes: 33 additions & 2 deletions src/lib/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,37 @@ type Step = {
readonly keys?: string;
readonly keysFromStep?: string;
readonly keysTransform?:
| ((data: string) => string)
| ((data: string) => Promise<string>);
| ((keys: string) => string)
| ((keys: string) => Promise<string>);
readonly conf?: string;
readonly onAfter?:
| ((
result: string,
zencode: string,
data: string | undefined,
keys: string | undefined,
conf: string | undefined
) => void)
| ((
result: string,
zencode: string,
data: string | undefined,
keys: string | undefined,
conf: string | undefined
) => Promise<void>);
readonly onBefore?:
| ((
zencode: string,
data: string | undefined,
keys: string | undefined,
conf: string | undefined
) => void)
| ((
zencode: string,
data: string | undefined,
keys: string | undefined,
conf: string | undefined
) => Promise<void>);
};

type Steps = {
Expand Down Expand Up @@ -52,11 +80,14 @@ export const execute = async (steps: Steps): Promise<string> => {
console.log(`TRANSFORMED KEYS: ${keys}`);
}
}
if (step.onBefore) await step.onBefore(step.zencode, data, keys, conf);
const { result, logs } = await zencode_exec(step.zencode, {
data,
keys,
conf,
});
if (step.onAfter)
await step.onAfter(result, step.zencode, data, keys, conf);
results[step.id] = result;
if (steps.verbose) {
console.log(logs);
Expand Down

0 comments on commit 1af065f

Please sign in to comment.