mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2024-12-05 02:10:18 +00:00
Mass rename (#48)
* Update terminology and namings * Use renamed to `avm` package
This commit is contained in:
parent
9aa077eb4b
commit
054a7bf094
8
package-lock.json
generated
8
package-lock.json
generated
@ -434,10 +434,10 @@
|
||||
"minimist": "^1.2.0"
|
||||
}
|
||||
},
|
||||
"@fluencelabs/air-interpreter": {
|
||||
"version": "0.9.7",
|
||||
"resolved": "https://registry.npmjs.org/@fluencelabs/air-interpreter/-/air-interpreter-0.9.7.tgz",
|
||||
"integrity": "sha512-9aJwfyfJ18BvSSFhTnq8SJZmIAgbyGXUf3vilsMhU6Iql35ZDd8E+5dF2hRkRrqUj2409w7qOtgLbB8JzdG/fw==",
|
||||
"@fluencelabs/avm": {
|
||||
"version": "0.9.6",
|
||||
"resolved": "https://registry.npmjs.org/@fluencelabs/avm/-/avm-0.9.6.tgz",
|
||||
"integrity": "sha512-ptgFqCy6BNfXmbn+eh+NplydleXKdGqXpJ3wMO37oYjBz9UZplCVWRBSicVMzt6yQi9uZyDNbB3x+1SCSYF/zg==",
|
||||
"requires": {
|
||||
"base64-js": "1.5.1"
|
||||
},
|
||||
|
@ -19,7 +19,7 @@
|
||||
"author": "Fluence Labs",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@fluencelabs/air-interpreter": "0.9.7",
|
||||
"@fluencelabs/avm": "0.9.6",
|
||||
"async": "3.2.0",
|
||||
"base64-js": "1.3.1",
|
||||
"bs58": "4.0.1",
|
||||
|
@ -2,7 +2,7 @@ import log from 'loglevel';
|
||||
import Multiaddr from 'multiaddr';
|
||||
import PeerId, { isPeerId } from 'peer-id';
|
||||
|
||||
import { AquaCallHandler } from './internal/AquaHandler';
|
||||
import { CallServiceHandler } from './internal/CallServiceHandler';
|
||||
import { ClientImpl } from './internal/ClientImpl';
|
||||
import { PeerIdB58 } from './internal/commonTypes';
|
||||
import { FluenceConnectionOptions } from './internal/FluenceConnection';
|
||||
@ -34,7 +34,7 @@ export interface FluenceClient {
|
||||
* Please note, that the handler is combined with the handler from RequestFlow before the execution occures.
|
||||
* After this combination, middlewares from RequestFlow are executed before client handler's middlewares.
|
||||
*/
|
||||
readonly aquaCallHandler: AquaCallHandler;
|
||||
readonly callServiceHandler: CallServiceHandler;
|
||||
|
||||
/**
|
||||
* Disconnects the client from the network
|
||||
@ -84,7 +84,7 @@ export const createClient = async (
|
||||
}
|
||||
|
||||
const client = new ClientImpl(peerId);
|
||||
await client.initAquamarineRuntime();
|
||||
await client.initAirInterpreter();
|
||||
|
||||
if (connectTo) {
|
||||
let theAddress: Multiaddr;
|
||||
|
@ -101,7 +101,7 @@ describe('Builtins usage suite', () => {
|
||||
`;
|
||||
|
||||
let resMakingPromise = new Promise((resolve) => {
|
||||
client.aquaCallHandler.on('test', 'test1', (args, _) => {
|
||||
client.callServiceHandler.on('test', 'test1', (args, _) => {
|
||||
resolve([...args]);
|
||||
return {};
|
||||
});
|
||||
|
@ -58,7 +58,7 @@ describe('Typescript usage suite', () => {
|
||||
const client2 = await createClient(nodes[0].multiaddr);
|
||||
|
||||
let resMakingPromise = new Promise((resolve) => {
|
||||
client2.aquaCallHandler.onEvent('test', 'test', (args, _) => {
|
||||
client2.callServiceHandler.onEvent('test', 'test', (args, _) => {
|
||||
resolve([...args]);
|
||||
return {};
|
||||
});
|
||||
@ -241,8 +241,7 @@ describe('Typescript usage suite', () => {
|
||||
|
||||
// assert
|
||||
await expect(res).rejects.toMatchObject({
|
||||
error:
|
||||
"Local service error: ret_code is 1024, error message is '\"The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='peer' fnName='identify' args=''\"'",
|
||||
error: "Local service error: ret_code is 1024, error message is '\"The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='peer' fnName='identify' args=''\"'",
|
||||
instruction: 'call %init_peer_id% ("peer" "identify") [] res',
|
||||
});
|
||||
});
|
||||
|
@ -1,5 +1,4 @@
|
||||
import { AquaCallHandler, errorHandler } from '../../internal/AquaHandler';
|
||||
import { ResultCodes } from '../../internal/commonTypes';
|
||||
import { CallServiceHandler, errorHandler, ResultCodes } from '../../internal/CallServiceHandler';
|
||||
|
||||
const req = () => ({
|
||||
serviceId: 'service',
|
||||
@ -15,10 +14,10 @@ const res = () => ({
|
||||
res,
|
||||
});
|
||||
|
||||
describe('Aqua handler tests', () => {
|
||||
describe('Call service handler tests', () => {
|
||||
it('Should work without middlewares', () => {
|
||||
// arrange
|
||||
const handler = new AquaCallHandler();
|
||||
const handler = new CallServiceHandler();
|
||||
|
||||
// act
|
||||
const res = handler.execute(req());
|
||||
@ -29,7 +28,7 @@ describe('Aqua handler tests', () => {
|
||||
|
||||
it('Should work with no-op middleware', () => {
|
||||
// arrange
|
||||
const handler = new AquaCallHandler();
|
||||
const handler = new CallServiceHandler();
|
||||
handler.use((req, res, next) => {
|
||||
next();
|
||||
});
|
||||
@ -43,7 +42,7 @@ describe('Aqua handler tests', () => {
|
||||
|
||||
it('Should work with two overlapping middlewares', () => {
|
||||
// arrange
|
||||
const handler = new AquaCallHandler();
|
||||
const handler = new CallServiceHandler();
|
||||
handler
|
||||
.use((req, res, next) => {
|
||||
res.result = { hello: 'world' };
|
||||
@ -64,7 +63,7 @@ describe('Aqua handler tests', () => {
|
||||
|
||||
it('Should work with two NON-overlapping middlewares', () => {
|
||||
// arrange
|
||||
const handler = new AquaCallHandler();
|
||||
const handler = new CallServiceHandler();
|
||||
handler
|
||||
.use((req, res, next) => {
|
||||
res.result = {};
|
||||
@ -90,7 +89,7 @@ describe('Aqua handler tests', () => {
|
||||
|
||||
it('Should work with provided error handling middleware', () => {
|
||||
// arrange
|
||||
const handler = new AquaCallHandler();
|
||||
const handler = new CallServiceHandler();
|
||||
|
||||
handler.use(errorHandler);
|
||||
handler.use((req, res, next) => {
|
||||
@ -110,7 +109,7 @@ describe('Aqua handler tests', () => {
|
||||
describe('Service handler tests', () => {
|
||||
it('Should register service function', () => {
|
||||
// arrange
|
||||
const handler = new AquaCallHandler();
|
||||
const handler = new CallServiceHandler();
|
||||
handler.on('service', 'function', (args) => {
|
||||
return { called: args };
|
||||
});
|
||||
@ -132,7 +131,7 @@ describe('Aqua handler tests', () => {
|
||||
|
||||
it('Should UNregister service function', () => {
|
||||
// arrange
|
||||
const handler = new AquaCallHandler();
|
||||
const handler = new CallServiceHandler();
|
||||
const unreg = handler.on('service', 'function', (args) => {
|
||||
return { called: args };
|
||||
});
|
||||
@ -154,7 +153,7 @@ describe('Aqua handler tests', () => {
|
||||
|
||||
it('Should register event', async () => {
|
||||
// arrange
|
||||
const handler = new AquaCallHandler();
|
||||
const handler = new CallServiceHandler();
|
||||
const returnPromise = new Promise((resolve) => {
|
||||
handler.onEvent('service', 'function', (args) => {
|
||||
resolve({ called: args });
|
||||
@ -178,7 +177,7 @@ describe('Aqua handler tests', () => {
|
||||
|
||||
it('Should UNregister event', () => {
|
||||
// arrange
|
||||
const handler = new AquaCallHandler();
|
||||
const handler = new CallServiceHandler();
|
||||
const unreg = handler.onEvent('service', 'function', (args) => {
|
||||
// don't care
|
||||
});
|
||||
@ -200,7 +199,7 @@ describe('Aqua handler tests', () => {
|
||||
|
||||
it('Should register multiple service functions', () => {
|
||||
// arrange
|
||||
const handler = new AquaCallHandler();
|
||||
const handler = new CallServiceHandler();
|
||||
handler.on('service', 'function1', (args) => {
|
||||
return 'called function1';
|
||||
});
|
||||
@ -233,7 +232,7 @@ describe('Aqua handler tests', () => {
|
||||
|
||||
it('Should override previous function registration', () => {
|
||||
// arrange
|
||||
const handler = new AquaCallHandler();
|
||||
const handler = new CallServiceHandler();
|
||||
handler.on('service', 'function', (args) => {
|
||||
return { called: args };
|
||||
});
|
||||
@ -259,11 +258,11 @@ describe('Aqua handler tests', () => {
|
||||
describe('Middleware combination tests', () => {
|
||||
it('Should work with NON overlapping function registration', () => {
|
||||
// arrange
|
||||
const base = new AquaCallHandler();
|
||||
const base = new CallServiceHandler();
|
||||
base.on('service', 'function1', (args) => {
|
||||
return 'called function1';
|
||||
});
|
||||
const another = new AquaCallHandler();
|
||||
const another = new CallServiceHandler();
|
||||
base.on('service', 'function2', (args) => {
|
||||
return 'called function2';
|
||||
});
|
||||
@ -295,11 +294,11 @@ describe('Aqua handler tests', () => {
|
||||
|
||||
it('Should work with overlapping function registration', () => {
|
||||
// arrange
|
||||
const base = new AquaCallHandler();
|
||||
const base = new CallServiceHandler();
|
||||
base.on('service', 'function', (args) => {
|
||||
return { called: args };
|
||||
});
|
||||
const another = new AquaCallHandler();
|
||||
const another = new CallServiceHandler();
|
||||
another.on('service', 'function', (args) => {
|
||||
return 'overridden';
|
||||
});
|
||||
|
@ -1,5 +1,3 @@
|
||||
import PeerId from 'peer-id';
|
||||
import { genUUID } from '../../internal/particle';
|
||||
import { seedToPeerId } from '../../internal/peerIdUtils';
|
||||
import { RequestFlow } from '../../internal/RequestFlow';
|
||||
|
||||
|
@ -39,7 +39,7 @@ describe('== AIR suite', () => {
|
||||
client = await createClient();
|
||||
|
||||
let res;
|
||||
client.aquaCallHandler.on(serviceId, fnName, (args, _) => {
|
||||
client.callServiceHandler.on(serviceId, fnName, (args, _) => {
|
||||
res = args[0];
|
||||
return res;
|
||||
});
|
||||
@ -118,13 +118,13 @@ describe('== AIR suite', () => {
|
||||
|
||||
client = await createClient();
|
||||
|
||||
client.aquaCallHandler.on(makeDataServiceId, makeDataFnName, (args, _) => {
|
||||
client.callServiceHandler.on(makeDataServiceId, makeDataFnName, (args, _) => {
|
||||
return {
|
||||
field: 42,
|
||||
};
|
||||
});
|
||||
let res;
|
||||
client.aquaCallHandler.on(getDataServiceId, getDataFnName, (args, tetraplets) => {
|
||||
client.callServiceHandler.on(getDataServiceId, getDataFnName, (args, tetraplets) => {
|
||||
res = {
|
||||
args: args,
|
||||
tetraplets: tetraplets,
|
||||
@ -156,7 +156,7 @@ describe('== AIR suite', () => {
|
||||
const serviceId1 = 'check1';
|
||||
const fnName1 = 'fn1';
|
||||
let res1;
|
||||
client.aquaCallHandler.on(serviceId1, fnName1, (args, _) => {
|
||||
client.callServiceHandler.on(serviceId1, fnName1, (args, _) => {
|
||||
res1 = args[0];
|
||||
return res1;
|
||||
});
|
||||
@ -164,7 +164,7 @@ describe('== AIR suite', () => {
|
||||
const serviceId2 = 'check2';
|
||||
const fnName2 = 'fn2';
|
||||
let res2;
|
||||
client.aquaCallHandler.on(serviceId2, fnName2, (args, _) => {
|
||||
client.callServiceHandler.on(serviceId2, fnName2, (args, _) => {
|
||||
res2 = args[0];
|
||||
return res2;
|
||||
});
|
||||
@ -172,7 +172,7 @@ describe('== AIR suite', () => {
|
||||
const serviceId3 = 'check3';
|
||||
const fnName3 = 'fn3';
|
||||
let res3;
|
||||
client.aquaCallHandler.on(serviceId3, fnName3, (args, _) => {
|
||||
client.callServiceHandler.on(serviceId3, fnName3, (args, _) => {
|
||||
res3 = args;
|
||||
return res3;
|
||||
});
|
||||
|
@ -1,5 +1,4 @@
|
||||
import { AirInterpreter } from '@fluencelabs/air-interpreter';
|
||||
import { genUUID } from '../../internal/particle';
|
||||
import { AirInterpreter } from '@fluencelabs/avm';
|
||||
|
||||
describe('== AST parsing suite', () => {
|
||||
it('parse simple script and return ast', async function () {
|
||||
|
28
src/api.ts
28
src/api.ts
@ -1,7 +1,7 @@
|
||||
import { SecurityTetraplet } from './internal/commonTypes';
|
||||
import { RequestFlowBuilder } from './internal/RequestFlowBuilder';
|
||||
import { FluenceClient } from './FluenceClient';
|
||||
import { AquaResultType } from './internal/AquaHandler';
|
||||
import { CallServiceResultType } from './internal/CallServiceHandler';
|
||||
import { SecurityTetraplet } from '@fluencelabs/avm';
|
||||
|
||||
/**
|
||||
* The class representing Particle - a data structure used to perform operations on Fluence Network. It originates on some peer in the network, travels the network through a predefined path, triggering function execution along its way.
|
||||
@ -15,7 +15,7 @@ export class Particle {
|
||||
* Creates a particle with specified parameters.
|
||||
* @param { String }script - Air script which defines the execution of a particle – its path, functions it triggers on peers, and so on.
|
||||
* @param { Map<string, any> | Record<string, any> } data - Variables passed to the particle in the form of either JS Map or JS object with keys representing variable names and values representing values correspondingly
|
||||
* @param { [Number]=7000 } ttl - Time to live, a timout after which the particle execution is stopped by Aquamarine.
|
||||
* @param { [Number]=7000 } ttl - Time to live, a timout after which the particle execution is stopped by AVM.
|
||||
*/
|
||||
constructor(script: string, data?: Map<string, any> | Record<string, any>, ttl?: number) {
|
||||
this.script = script;
|
||||
@ -68,19 +68,19 @@ const makeKey = (client: FluenceClient, serviceId: string, fnName: string) => {
|
||||
};
|
||||
|
||||
/**
|
||||
* Registers a function which can be called on the client from Aquamarine. The registration is per client basis.
|
||||
* Registers a function which can be called on the client from AVM. The registration is per client basis.
|
||||
* @param { FluenceClient } client - The Fluence Client instance.
|
||||
* @param { string } serviceId - The identifier of service which would be used to make calls from Aquamarine
|
||||
* @param { string } fnName - The identifier of function which would be used to make calls from Aquamarine
|
||||
* @param { (args: any[], tetraplets: SecurityTetraplet[][]) => object | boolean | number | string } handler - The handler which would be called by Aquamarine infrastructure. The result is any object passed back to Aquamarine
|
||||
* @param { string } serviceId - The identifier of service which would be used to make calls from AVM
|
||||
* @param { string } fnName - The identifier of function which would be used to make calls from AVM
|
||||
* @param { (args: any[], tetraplets: SecurityTetraplet[][]) => object | boolean | number | string } handler - The handler which would be called by AVM. The result is any object passed back to AVM
|
||||
*/
|
||||
export const registerServiceFunction = (
|
||||
client: FluenceClient,
|
||||
serviceId: string,
|
||||
fnName: string,
|
||||
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => AquaResultType,
|
||||
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => CallServiceResultType,
|
||||
) => {
|
||||
const unregister = client.aquaCallHandler.on(serviceId, fnName, handler);
|
||||
const unregister = client.callServiceHandler.on(serviceId, fnName, handler);
|
||||
handlersUnregistratorsMap.set(makeKey(client, serviceId, fnName), unregister);
|
||||
};
|
||||
|
||||
@ -105,12 +105,12 @@ export const unregisterServiceFunction = (
|
||||
};
|
||||
|
||||
/**
|
||||
* Registers an event-like handler for all calls to the specific service\function pair from from Aquamarine. The registration is per client basis. Return a function which when called removes the subscription.
|
||||
* Registers an event-like handler for all calls to the specific service\function pair from AVM. The registration is per client basis. Return a function which when called removes the subscription.
|
||||
* Same as registerServiceFunction which immediately returns empty object.
|
||||
* @param { FluenceClient } client - The Fluence Client instance.
|
||||
* @param { string } serviceId - The identifier of service calls to which from Aquamarine are transformed into events.
|
||||
* @param { string } fnName - The identifier of function calls to which from Aquamarine are transformed into events.
|
||||
* @param { (args: any[], tetraplets: SecurityTetraplet[][]) => object } handler - The handler which would be called by Aquamarine infrastructure
|
||||
* @param { string } serviceId - The identifier of service calls to which from AVM are transformed into events.
|
||||
* @param { string } fnName - The identifier of function calls to which from AVM are transformed into events.
|
||||
* @param { (args: any[], tetraplets: SecurityTetraplet[][]) => object } handler - The handler which would be called by AVM
|
||||
* @returns { Function } - A function which when called removes the subscription.
|
||||
*/
|
||||
export const subscribeToEvent = (
|
||||
@ -139,7 +139,7 @@ export const subscribeToEvent = (
|
||||
* @param { Particle } particle - The particle to send.
|
||||
* @param { string } callbackFnName - The identifier of function which should be used in Air script to pass the data to fetch "promise"
|
||||
* @param { [string]='_callback' } callbackServiceId - The service identifier which should be used in Air script to pass the data to fetch "promise"
|
||||
* @returns { Promise<T> } - A promise which would be resolved with the data returned from Aquamarine
|
||||
* @returns { Promise<T> } - A promise which would be resolved with the data returned from AVM
|
||||
*/
|
||||
export const sendParticleAsFetch = async <T>(
|
||||
client: FluenceClient,
|
||||
|
@ -1,2 +1,2 @@
|
||||
export { RequestFlowBuilder } from './internal/RequestFlowBuilder';
|
||||
export * from './internal/AquaHandler';
|
||||
export * from './internal/CallServiceHandler';
|
||||
|
@ -15,7 +15,8 @@
|
||||
*/
|
||||
|
||||
export { seedToPeerId, peerIdToSeed, generatePeerId } from './internal/peerIdUtils';
|
||||
export { SecurityTetraplet, PeerIdB58 } from './internal/commonTypes';
|
||||
export { PeerIdB58 } from './internal/commonTypes';
|
||||
export { SecurityTetraplet } from '@fluencelabs/avm';
|
||||
export * from './api';
|
||||
export * from './FluenceClient';
|
||||
export * from './internal/builtins';
|
||||
|
@ -1,7 +1,14 @@
|
||||
import { ResultCodes, SecurityTetraplet } from './commonTypes';
|
||||
import { SecurityTetraplet } from '@fluencelabs/avm';
|
||||
|
||||
export enum ResultCodes {
|
||||
success = 0,
|
||||
noServiceFound = 1,
|
||||
exceptionInHandler = 2,
|
||||
unkownError = 1024,
|
||||
}
|
||||
|
||||
/**
|
||||
* Particle context. Contains additional information about particle which triggered `call` air instruction from Aquamarine interpreter
|
||||
* Particle context. Contains additional information about particle which triggered `call` air instruction from AVM
|
||||
*/
|
||||
interface ParticleContext {
|
||||
/**
|
||||
@ -12,9 +19,9 @@ interface ParticleContext {
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the information passed from Aquamarine interpreter when a `call` air instruction is executed on the local peer
|
||||
* Represents the information passed from AVM when a `call` air instruction is executed on the local peer
|
||||
*/
|
||||
interface AquaCall {
|
||||
interface CallServiceData {
|
||||
/**
|
||||
* Service ID as specified in `call` air instruction
|
||||
*/
|
||||
@ -31,7 +38,7 @@ interface AquaCall {
|
||||
args: any[];
|
||||
|
||||
/**
|
||||
* Security Tetraplets recieved from Aquamarine interpreter
|
||||
* Security Tetraplets recieved from AVM
|
||||
*/
|
||||
tetraplets: SecurityTetraplet[][];
|
||||
|
||||
@ -44,50 +51,50 @@ interface AquaCall {
|
||||
}
|
||||
|
||||
/**
|
||||
* Type for all the possible ovjects that can be return to the Aquamarine interpreter
|
||||
* Type for all the possible ovjects that can be return to the AVM
|
||||
*/
|
||||
export type AquaResultType = object | boolean | number | string;
|
||||
export type CallServiceResultType = object | boolean | number | string;
|
||||
|
||||
/**
|
||||
* Represents the result of the `call` air instruction to be returned into Aquamarine interpreter
|
||||
* Represents the result of the `call` air instruction to be returned into AVM
|
||||
*/
|
||||
interface AquaCallResult {
|
||||
interface CallServiceResult {
|
||||
/**
|
||||
* Return code to be returned to Aquamarine interpreter
|
||||
* Return code to be returned to AVM
|
||||
*/
|
||||
retCode: ResultCodes;
|
||||
|
||||
/**
|
||||
* Result object to be returned to Aquamarine interpreter
|
||||
* Result object to be returned to AVM
|
||||
*/
|
||||
result: AquaResultType;
|
||||
result: CallServiceResultType;
|
||||
[x: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Type for the middleware used in AquaCallHandler middleware chain.
|
||||
* Type for the middleware used in CallServiceHandler middleware chain.
|
||||
* In a nutshell middelware is a function of request, response and function to trigger the next middleware in chain.
|
||||
* Each middleware is free to write additional properties to either request or response object.
|
||||
* When the chain finishes the response is passed back to Aquamarine interpreter
|
||||
* @param { AquaCall } req - information about the air `call` instruction
|
||||
* @param { AquaCallResult } resp - response to be passed to Aquamarine interpreter
|
||||
* When the chain finishes the response is passed back to AVM
|
||||
* @param { CallServiceData } req - information about the air `call` instruction
|
||||
* @param { CallServiceResult } resp - response to be passed to AVM
|
||||
* @param { Function } next - function which invokes next middleware in chain
|
||||
*/
|
||||
export type Middleware = (req: AquaCall, resp: AquaCallResult, next: Function) => void;
|
||||
export type Middleware = (req: CallServiceData, resp: CallServiceResult, next: Function) => void;
|
||||
|
||||
/**
|
||||
* Convenience middleware factory. Registeres a handler for a pair of 'serviceId/fnName'.
|
||||
* The return value of the handler is passed back to Aquamarine
|
||||
* @param { string } serviceId - The identifier of service which would be used to make calls from Aquamarine
|
||||
* @param { string } fnName - The identifier of function which would be used to make calls from Aquamarine
|
||||
* @param { (args: any[], tetraplets: SecurityTetraplet[][]) => object } handler - The handler which should handle the call. The result is any object passed back to Aquamarine
|
||||
* The return value of the handler is passed back to AVM
|
||||
* @param { string } serviceId - The identifier of service which would be used to make calls from AVM
|
||||
* @param { string } fnName - The identifier of function which would be used to make calls from AVM
|
||||
* @param { (args: any[], tetraplets: SecurityTetraplet[][]) => object } handler - The handler which should handle the call. The result is any object passed back to AVM
|
||||
*/
|
||||
export const fnHandler = (
|
||||
serviceId: string,
|
||||
fnName: string,
|
||||
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => AquaResultType,
|
||||
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => CallServiceResultType,
|
||||
) => {
|
||||
return (req: AquaCall, resp: AquaCallResult, next: Function): void => {
|
||||
return (req: CallServiceData, resp: CallServiceResult, next: Function): void => {
|
||||
if (req.fnName === fnName && req.serviceId === serviceId) {
|
||||
const res = handler(req.args, req.tetraplets);
|
||||
resp.retCode = ResultCodes.success;
|
||||
@ -100,8 +107,8 @@ export const fnHandler = (
|
||||
/**
|
||||
* Convenience middleware factory. Registeres a handler for a pair of 'serviceId/fnName'.
|
||||
* Similar to @see { @link fnHandler } but instead returns and empty object immediately runs the handler asynchronously
|
||||
* @param { string } serviceId - The identifier of service which would be used to make calls from Aquamarine
|
||||
* @param { string } fnName - The identifier of function which would be used to make calls from Aquamarine
|
||||
* @param { string } serviceId - The identifier of service which would be used to make calls from AVM
|
||||
* @param { string } fnName - The identifier of function which would be used to make calls from AVM
|
||||
* @param { (args: any[], tetraplets: SecurityTetraplet[][]) => void } handler - The handler which should handle the call.
|
||||
*/
|
||||
export const fnAsEventHandler = (
|
||||
@ -109,7 +116,7 @@ export const fnAsEventHandler = (
|
||||
fnName: string,
|
||||
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => void,
|
||||
) => {
|
||||
return (req: AquaCall, resp: AquaCallResult, next: Function): void => {
|
||||
return (req: CallServiceData, resp: CallServiceResult, next: Function): void => {
|
||||
if (req.fnName === fnName && req.serviceId === serviceId) {
|
||||
setTimeout(() => {
|
||||
handler(req.args, req.tetraplets);
|
||||
@ -125,7 +132,7 @@ export const fnAsEventHandler = (
|
||||
/**
|
||||
* Error catching middleware
|
||||
*/
|
||||
export const errorHandler: Middleware = (req: AquaCall, resp: AquaCallResult, next: Function): void => {
|
||||
export const errorHandler: Middleware = (req: CallServiceData, resp: CallServiceResult, next: Function): void => {
|
||||
try {
|
||||
next();
|
||||
} catch (e) {
|
||||
@ -134,23 +141,23 @@ export const errorHandler: Middleware = (req: AquaCall, resp: AquaCallResult, ne
|
||||
}
|
||||
};
|
||||
|
||||
type AquaCallFunction = (req: AquaCall, resp: AquaCallResult) => void;
|
||||
type CallServiceFunction = (req: CallServiceData, resp: CallServiceResult) => void;
|
||||
|
||||
/**
|
||||
* Class defines the handling of a `call` air intruction executed by aquamarine on the local peer.
|
||||
* Class defines the handling of a `call` air intruction executed by AVM on the local peer.
|
||||
* All the execution process is defined by the chain of middlewares - architecture popular among backend web frameworks.
|
||||
* Each middleware has the form of `(req: AquaCall, resp: AquaCallResult, next: Function) => void;`
|
||||
* Each middleware has the form of `(req: Call, resp: CallServiceResult, next: Function) => void;`
|
||||
* A handler starts with an empty middleware chain and does nothing.
|
||||
* To execute the handler use @see { @link execute } function
|
||||
*/
|
||||
export class AquaCallHandler {
|
||||
export class CallServiceHandler {
|
||||
private middlewares: Middleware[] = [];
|
||||
|
||||
/**
|
||||
* Appends middleware to the chain of middlewares
|
||||
* @param { Middleware } middleware
|
||||
*/
|
||||
use(middleware: Middleware): AquaCallHandler {
|
||||
use(middleware: Middleware): CallServiceHandler {
|
||||
this.middlewares.push(middleware);
|
||||
return this;
|
||||
}
|
||||
@ -159,7 +166,7 @@ export class AquaCallHandler {
|
||||
* Removes the middleware from the chain of middlewares
|
||||
* @param { Middleware } middleware
|
||||
*/
|
||||
unUse(middleware: Middleware): AquaCallHandler {
|
||||
unUse(middleware: Middleware): CallServiceHandler {
|
||||
const index = this.middlewares.indexOf(middleware);
|
||||
if (index !== -1) {
|
||||
this.middlewares.splice(index, 1);
|
||||
@ -170,9 +177,9 @@ export class AquaCallHandler {
|
||||
/**
|
||||
* Combine handler with another one. Combintaion is done by copying middleware chain from the argument's handler into current one.
|
||||
* Please note, that current handler's middlewares take precedence over the ones from handler to be combined with
|
||||
* @param { AquaCallHandler } other - AquaCallHandler to be combined with
|
||||
* @param { CallServiceHandler } other - CallServiceHandler to be combined with
|
||||
*/
|
||||
combineWith(other: AquaCallHandler): AquaCallHandler {
|
||||
combineWith(other: CallServiceHandler): CallServiceHandler {
|
||||
this.middlewares = [...this.middlewares, ...other.middlewares];
|
||||
return this;
|
||||
}
|
||||
@ -183,7 +190,7 @@ export class AquaCallHandler {
|
||||
on(
|
||||
serviceId: string,
|
||||
fnName: string,
|
||||
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => AquaResultType,
|
||||
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => CallServiceResultType,
|
||||
): Function {
|
||||
const mw = fnHandler(serviceId, fnName, handler);
|
||||
this.use(mw);
|
||||
@ -210,8 +217,8 @@ export class AquaCallHandler {
|
||||
/**
|
||||
* Collapses middleware chain into a single function.
|
||||
*/
|
||||
buildFunction(): AquaCallFunction {
|
||||
const result = this.middlewares.reduceRight<AquaCallFunction>(
|
||||
buildFunction(): CallServiceFunction {
|
||||
const result = this.middlewares.reduceRight<CallServiceFunction>(
|
||||
(agg, cur) => {
|
||||
return (req, resp) => {
|
||||
cur(req, resp, () => agg(req, resp));
|
||||
@ -224,10 +231,10 @@ export class AquaCallHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the handler with the specified AquaCall request. Return the result response
|
||||
* Executes the handler with the specified Call request. Return the result response
|
||||
*/
|
||||
execute(req: AquaCall): AquaCallResult {
|
||||
const res: AquaCallResult = {
|
||||
execute(req: CallServiceData): CallServiceResult {
|
||||
const res: CallServiceResult = {
|
||||
retCode: ResultCodes.unkownError,
|
||||
result: `The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='${req.serviceId}' fnName='${req.fnName}' args='${req.args}'`,
|
||||
};
|
@ -18,17 +18,17 @@ import * as PeerId from 'peer-id';
|
||||
import Multiaddr from 'multiaddr';
|
||||
import { FluenceConnection, FluenceConnectionOptions } from './FluenceConnection';
|
||||
|
||||
import { CallServiceResult, ParticleHandler, PeerIdB58, SecurityTetraplet } from './commonTypes';
|
||||
import { PeerIdB58 } from './commonTypes';
|
||||
import { FluenceClient } from '../FluenceClient';
|
||||
import { RequestFlow } from './RequestFlow';
|
||||
import { AquaCallHandler, errorHandler, fnHandler } from './AquaHandler';
|
||||
import { CallServiceHandler, errorHandler, fnHandler } from './CallServiceHandler';
|
||||
import { loadRelayFn, loadVariablesService } from './RequestFlowBuilder';
|
||||
import { logParticle, Particle } from './particle';
|
||||
import log from 'loglevel';
|
||||
import { AirInterpreter } from '@fluencelabs/air-interpreter';
|
||||
import { AirInterpreter, CallServiceResult, ParticleHandler, SecurityTetraplet } from '@fluencelabs/avm';
|
||||
|
||||
const makeDefaultClientHandler = (): AquaCallHandler => {
|
||||
const res = new AquaCallHandler();
|
||||
const makeDefaultClientHandler = (): CallServiceHandler => {
|
||||
const res = new CallServiceHandler();
|
||||
res.use(errorHandler);
|
||||
res.use(fnHandler('op', 'identity', (args, _) => args));
|
||||
return res;
|
||||
@ -58,10 +58,10 @@ export class ClientImpl implements FluenceClient {
|
||||
|
||||
constructor(selfPeerIdFull: PeerId) {
|
||||
this.selfPeerIdFull = selfPeerIdFull;
|
||||
this.aquaCallHandler = makeDefaultClientHandler();
|
||||
this.callServiceHandler = makeDefaultClientHandler();
|
||||
}
|
||||
|
||||
aquaCallHandler: AquaCallHandler;
|
||||
callServiceHandler: CallServiceHandler;
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
if (this.connection) {
|
||||
@ -73,7 +73,7 @@ export class ClientImpl implements FluenceClient {
|
||||
});
|
||||
}
|
||||
|
||||
async initAquamarineRuntime(): Promise<void> {
|
||||
async initAirInterpreter(): Promise<void> {
|
||||
this.interpreter = await AirInterpreter.create(
|
||||
this.interpreterCallback.bind(this),
|
||||
this.selfPeerId,
|
||||
@ -114,7 +114,7 @@ export class ClientImpl implements FluenceClient {
|
||||
await request.initState(this.selfPeerIdFull);
|
||||
|
||||
logParticle(log.debug, 'executing local particle', request.getParticle());
|
||||
request.handler.combineWith(this.aquaCallHandler);
|
||||
request.handler.combineWith(this.callServiceHandler);
|
||||
this.requests.set(request.id, request);
|
||||
|
||||
this.processRequest(request);
|
||||
@ -128,7 +128,7 @@ export class ClientImpl implements FluenceClient {
|
||||
request.receiveUpdate(particle);
|
||||
} else {
|
||||
request = RequestFlow.createExternal(particle);
|
||||
request.handler.combineWith(this.aquaCallHandler);
|
||||
request.handler.combineWith(this.callServiceHandler);
|
||||
}
|
||||
this.requests.set(request.id, request);
|
||||
|
||||
|
@ -1,17 +1,24 @@
|
||||
import log, { trace } from 'loglevel';
|
||||
import PeerId from 'peer-id';
|
||||
import { AirInterpreter } from '@fluencelabs/air-interpreter';
|
||||
import { AquaCallHandler } from './AquaHandler';
|
||||
import { InterpreterOutcome, PeerIdB58 } from './commonTypes';
|
||||
import { AirInterpreter } from '@fluencelabs/avm';
|
||||
import { CallServiceHandler } from './CallServiceHandler';
|
||||
import { PeerIdB58 } from './commonTypes';
|
||||
import { FluenceConnection } from './FluenceConnection';
|
||||
import { Particle, genUUID, logParticle } from './particle';
|
||||
|
||||
export const DEFAULT_TTL = 7000;
|
||||
|
||||
interface InterpreterOutcome {
|
||||
ret_code: number;
|
||||
data: Uint8Array;
|
||||
next_peer_pks: string[];
|
||||
error_message: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The class represents the current view (and state) of distributed the particle execution process from client's point of view.
|
||||
* It stores the intermediate particles state during the process. RequestFlow is identified by the id of the particle that is executed during the flow.
|
||||
* Each RequestFlow contains a separate (unique to the current flow) AquaCallHandler where the handling of `call` AIR instruction takes place
|
||||
* Each RequestFlow contains a separate (unique to the current flow) CallServiceHandler where the handling of `call` AIR instruction takes place
|
||||
* Please note, that RequestFlow's is handler is combined with the handler from client before the execution occures.
|
||||
* After the combination middlewares from RequestFlow are executed before client handler's middlewares.
|
||||
*/
|
||||
@ -25,7 +32,7 @@ export class RequestFlow {
|
||||
readonly id: string;
|
||||
readonly isExternal: boolean;
|
||||
readonly script: string;
|
||||
readonly handler = new AquaCallHandler();
|
||||
readonly handler = new CallServiceHandler();
|
||||
|
||||
ttl: number = DEFAULT_TTL;
|
||||
relayPeerId?: PeerIdB58;
|
||||
|
@ -1,5 +1,5 @@
|
||||
import log from 'loglevel';
|
||||
import { AquaCallHandler } from './AquaHandler';
|
||||
import { CallServiceHandler } from './CallServiceHandler';
|
||||
import { DEFAULT_TTL, RequestFlow } from './RequestFlow';
|
||||
|
||||
export const loadVariablesService = 'load';
|
||||
@ -101,7 +101,7 @@ export class RequestFlowBuilder {
|
||||
|
||||
private ttl: number = DEFAULT_TTL;
|
||||
private variables = new Map<string, any>();
|
||||
private handlerConfigs: Array<(handler: AquaCallHandler, request: RequestFlow) => void> = [];
|
||||
private handlerConfigs: Array<(handler: CallServiceHandler, request: RequestFlow) => void> = [];
|
||||
private buildScriptActions: Array<(sb: ScriptBuilder) => void> = [];
|
||||
private onTimeout: () => void;
|
||||
private onError: (error: any) => void;
|
||||
@ -143,7 +143,7 @@ export class RequestFlowBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes necessary defaults when building requests by hand without the Aquamarine language compiler
|
||||
* Removes necessary defaults when building requests by hand without the Aqua language compiler
|
||||
* Removed features include: relay and variable injection, error handling with top-level xor wrap
|
||||
*/
|
||||
disableInjections(): RequestFlowBuilder {
|
||||
@ -245,7 +245,7 @@ export class RequestFlowBuilder {
|
||||
/**
|
||||
* Configure local call handler for the Request Flow
|
||||
*/
|
||||
configHandler(config: (handler: AquaCallHandler, request: RequestFlow) => void): RequestFlowBuilder {
|
||||
configHandler(config: (handler: CallServiceHandler, request: RequestFlow) => void): RequestFlowBuilder {
|
||||
this.handlerConfigs.push(config);
|
||||
return this;
|
||||
}
|
||||
|
@ -14,40 +14,4 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export interface CallServiceResult {
|
||||
ret_code: number;
|
||||
result: string;
|
||||
}
|
||||
|
||||
export type ParticleHandler = (
|
||||
serviceId: string,
|
||||
fnName: string,
|
||||
args: any[],
|
||||
tetraplets: SecurityTetraplet[][],
|
||||
) => CallServiceResult;
|
||||
|
||||
export interface InterpreterOutcome {
|
||||
ret_code: number;
|
||||
data: Uint8Array;
|
||||
next_peer_pks: string[];
|
||||
error_message: string;
|
||||
}
|
||||
|
||||
export interface ResolvedTriplet {
|
||||
peer_pk: string;
|
||||
service_id: string;
|
||||
function_name: string;
|
||||
}
|
||||
|
||||
export interface SecurityTetraplet extends ResolvedTriplet {
|
||||
json_path: string;
|
||||
}
|
||||
|
||||
export type PeerIdB58 = string;
|
||||
|
||||
export enum ResultCodes {
|
||||
success = 0,
|
||||
noServiceFound = 1,
|
||||
exceptionInHandler = 2,
|
||||
unkownError = 1024,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user