Skip to content

Commit

Permalink
feat: plugin to run zencode
Browse files Browse the repository at this point in the history
  • Loading branch information
albertolerda committed Jan 22, 2024
1 parent 4ebab3d commit 58480f8
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/core/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const getDatakeys = (params: ZenParams, rhs: string): undefined | Jsonable =>
*/
const fetchDatakeys = (params: ZenParams, rhs: string): Jsonable => {
const val = getDatakeys(params, rhs);
if (!val) throw new Error('cannot be undefined');
if (val === undefined) throw new Error('cannot be undefined');
return val;
};

Expand Down
1 change: 1 addition & 0 deletions pkg/zencode/.npmignore
30 changes: 30 additions & 0 deletions pkg/zencode/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@slangroom/zencode",
"version": "1.12.0",
"dependencies": {
"@slangroom/core": "workspace:*",
"@slangroom/shared": "workspace:*"
},
"repository": "https://github.com/dyne/slangroom",
"license": "AGPL-3.0-only",
"type": "module",
"main": "./build/esm/src/index.js",
"types": "./build/esm/src/index.d.ts",
"exports": {
".": {
"import": {
"types": "./build/esm/src/index.d.ts",
"default": "./build/esm/src/index.js"
}
},
"./*": {
"import": {
"types": "./build/esm/src/*.d.ts",
"default": "./build/esm/src/*.js"
}
}
},
"publishConfig": {
"access": "public"
}
}
1 change: 1 addition & 0 deletions pkg/zencode/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@slangroom/zencode/plugin';
24 changes: 24 additions & 0 deletions pkg/zencode/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Plugin } from '@slangroom/core';
import type { JsonableObject } from '@slangroom/shared';
import { zencodeExec } from '@slangroom/shared';

const p = new Plugin();

/**
* @internal
*/
export const zencodeExecPlugin = p.new(['script', 'data', 'keys', 'extra', 'conf'], 'execute zencode', async (ctx) => {
const script = ctx.fetch('script') as string;
const data = (ctx.get('data') || {}) as JsonableObject;
const keys = (ctx.get('keys') || {}) as JsonableObject;
const extra = (ctx.get('extra') || {}) as JsonableObject;
const conf = (ctx.get('conf') || "") as string;

const zout = await zencodeExec(script, { data, keys, extra, conf });

return ctx.pass(zout.result)
});



export const zencode = p;
37 changes: 37 additions & 0 deletions pkg/zencode/test/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import test from 'ava';
import { Slangroom } from '@slangroom/core';
import { zencode } from '@slangroom/zencode';
import type { JsonableObject } from '@slangroom/shared';


test('run zencode', async (t) => {
const scriptCreate = `
Rule unknown ignore
Given I send keys 'neo_keys' and send script 'neo_script' and send data 'neo_data' and send conf 'neo_conf' and send extra 'neo_extra' and execute zencode and output into 'ecdh_public_key'
Given I have a 'string dictionary' named 'ecdh_public_key'
Then print data
`;
const slangroom = new Slangroom(zencode);
const res = await slangroom.execute(scriptCreate, {
keys: {
neo_keys: {
keyring: {
ecdh: "FJ5Esc1koLSH+9pKSdI65tcyH2HowzXMe0UdsqktmZU=",
}
},
neo_conf: "",
neo_data: {},
neo_extra: {},
neo_script: `
Scenario 'ecdh': Create the public key
Given I have the 'keyring'
When I create the ecdh public key
Then print the 'ecdh public key'
`
},
});
t.deepEqual((res.result['ecdh_public_key'] as JsonableObject), {
ecdh_public_key: "BLOYXryyAI7rPuyNbI0/1CfLFd7H/NbX+osqyQHjPR9BPK1lYSPOixZQWvFK+rkkJ+aJbYp6kii2Y3+fZ5vl2MA="
});

});
1 change: 1 addition & 0 deletions pkg/zencode/tsconfig.json
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58480f8

Please sign in to comment.