Update js-sdk section

This commit is contained in:
Pavel Murygin
2021-09-10 20:08:58 +03:00
parent 128b226739
commit bc1785e463
18 changed files with 31462 additions and 319 deletions

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"exec": "node -r ts-node/register src/index.ts",
"start": "node -r ts-node/register src/index.ts",
"compile-aqua": "aqua --import . -i ./aqua/ -o ./src/_aqua",
"watch-aqua": "chokidar \"**/*.aqua\" -c \"npm run compile-aqua\""
},

View File

@ -1,139 +1,167 @@
/**
*
* This file is auto-generated. Do not edit manually: changes may be erased.
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
* Aqua version: 0.3.0-222
* Aqua version: 0.3.0-226
*
*/
import { FluencePeer } from "@fluencelabs/fluence";
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
import {
ResultCodes,
RequestFlow,
RequestFlowBuilder,
CallParams,
} from "@fluencelabs/fluence/dist/internal/compilerSupport/v1";
ResultCodes,
RequestFlow,
RequestFlowBuilder,
CallParams,
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
// Services
export interface CalcDef {
add: (n: number, callParams: CallParams<"n">) => void;
divide: (n: number, callParams: CallParams<"n">) => void;
getResult: (callParams: CallParams<null>) => number;
multiply: (n: number, callParams: CallParams<"n">) => void;
reset: (callParams: CallParams<null>) => void;
subtract: (n: number, callParams: CallParams<"n">) => void;
}
export interface CalcDef {
add: (n: number, callParams: CallParams<'n'>) => void;
divide: (n: number, callParams: CallParams<'n'>) => void;
getResult: (callParams: CallParams<null>) => number;
multiply: (n: number, callParams: CallParams<'n'>) => void;
reset: (callParams: CallParams<null>) => void;
subtract: (n: number, callParams: CallParams<'n'>) => void;
}
export function registerCalc(service: CalcDef): void;
export function registerCalc(service: CalcDef): void;
export function registerCalc(serviceId: string, service: CalcDef): void;
export function registerCalc(peer: FluencePeer, service: CalcDef): void;
export function registerCalc(
peer: FluencePeer,
serviceId: string,
service: CalcDef
): void;
export function registerCalc(...args) {
let peer: FluencePeer;
let serviceId;
let service;
if (args[0] instanceof FluencePeer) {
peer = args[0];
} else {
peer = FluencePeer.default;
}
if (typeof args[0] === "string") {
serviceId = args[0];
} else if (typeof args[1] === "string") {
serviceId = args[1];
} else {
serviceId = "calc";
}
if (!(args[0] instanceof FluencePeer) && typeof args[0] === "object") {
service = args[0];
} else if (typeof args[1] === "object") {
service = args[1];
} else {
service = args[2];
}
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
export function registerCalc(peer: FluencePeer, serviceId: string, service: CalcDef): void;
export function registerCalc(...args: any) {
let peer: FluencePeer;
let serviceId: any;
let service: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
} else {
peer = Fluence.getPeer();
}
if (req.fnName === "add") {
const callParams = {
...req.particleContext,
tetraplets: {
n: req.tetraplets[0],
},
};
resp.retCode = ResultCodes.success;
service.add(req.args[0], callParams);
resp.result = {};
}
if (req.fnName === "divide") {
const callParams = {
...req.particleContext,
tetraplets: {
n: req.tetraplets[0],
},
};
resp.retCode = ResultCodes.success;
service.divide(req.args[0], callParams);
resp.result = {};
}
if (req.fnName === "getResult") {
const callParams = {
...req.particleContext,
tetraplets: {},
};
resp.retCode = ResultCodes.success;
resp.result = service.getResult(callParams);
}
if (req.fnName === "multiply") {
const callParams = {
...req.particleContext,
tetraplets: {
n: req.tetraplets[0],
},
};
resp.retCode = ResultCodes.success;
service.multiply(req.args[0], callParams);
resp.result = {};
}
if (req.fnName === "reset") {
const callParams = {
...req.particleContext,
tetraplets: {},
};
resp.retCode = ResultCodes.success;
service.reset(callParams);
resp.result = {};
}
if (req.fnName === "subtract") {
const callParams = {
...req.particleContext,
tetraplets: {
n: req.tetraplets[0],
},
};
resp.retCode = ResultCodes.success;
service.subtract(req.args[0], callParams);
resp.result = {};
}
next();
});
if (typeof args[0] === 'string') {
serviceId = args[0];
} else if (typeof args[1] === 'string') {
serviceId = args[1];
}
else {
serviceId = "calc"
}
// Figuring out which overload is the service.
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
// If the first argument is peer, we are checking further. The second argument might either be
// an object, that it must be the service object
// or a string, which is the service id. In that case the service is the third argument
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
service = args[0];
} else if (typeof args[1] === 'object') {
service = args[1];
} else {
service = args[2];
}
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
}
if (req.fnName === 'add') {
const callParams = {
...req.particleContext,
tetraplets: {
n: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
service.add(req.args[0], callParams); resp.result = {}
}
if (req.fnName === 'divide') {
const callParams = {
...req.particleContext,
tetraplets: {
n: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
service.divide(req.args[0], callParams); resp.result = {}
}
if (req.fnName === 'getResult') {
const callParams = {
...req.particleContext,
tetraplets: {
},
};
resp.retCode = ResultCodes.success;
resp.result = service.getResult(callParams)
}
if (req.fnName === 'multiply') {
const callParams = {
...req.particleContext,
tetraplets: {
n: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
service.multiply(req.args[0], callParams); resp.result = {}
}
if (req.fnName === 'reset') {
const callParams = {
...req.particleContext,
tetraplets: {
},
};
resp.retCode = ResultCodes.success;
service.reset(callParams); resp.result = {}
}
if (req.fnName === 'subtract') {
const callParams = {
...req.particleContext,
tetraplets: {
n: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
service.subtract(req.args[0], callParams); resp.result = {}
}
next();
});
}
// Functions

View File

@ -3,10 +3,10 @@
* This file is auto-generated. Do not edit manually: changes may be erased.
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
* Aqua version: 0.3.0-222
* Aqua version: 0.3.0-226
*
*/
import { FluencePeer } from '@fluencelabs/fluence';
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
import {
ResultCodes,
RequestFlow,
@ -22,15 +22,15 @@ import {
export function demoCalculation(config?: {ttl?: number}) : Promise<number>;
export function demoCalculation(peer: FluencePeer, config?: {ttl?: number}) : Promise<number>;
export function demoCalculation(...args) {
export function demoCalculation(...args: any) {
let peer: FluencePeer;
let config;
if (args[0] instanceof FluencePeer) {
let config: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
config = args[1];
} else {
peer = FluencePeer.default;
peer = Fluence.getPeer();
config = args[0];
}
@ -94,7 +94,7 @@ import {
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return peer.connectionInfo.connectedRelay ;
return peer.getStatus().relayPeerId;
});
h.onEvent('callbackSrv', 'response', (args) => {

View File

@ -1,4 +1,4 @@
import { FluencePeer } from "@fluencelabs/fluence";
import { Fluence } from "@fluencelabs/fluence";
import { krasnodar } from "@fluencelabs/fluence-network-environment";
import { registerCalc, CalcDef } from "./_aqua/calc";
@ -41,19 +41,19 @@ const keypress = async () => {
};
async function main() {
await FluencePeer.default.init({
await Fluence.start({
connectTo: krasnodar[0],
});
registerCalc(new Calc());
console.log("application started");
console.log("peer id is: ", FluencePeer.default.connectionInfo.selfPeerId);
console.log("relay is: ", FluencePeer.default.connectionInfo.connectedRelay);
console.log("peer id is: ", Fluence.getStatus().peerId);
console.log("relay is: ", Fluence.getStatus().relayPeerId);
console.log("press any key to continue");
await keypress();
await FluencePeer.default.uninit();
await Fluence.stop();
}
main();