Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/FluenceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -84,7 +84,7 @@ export const createClient = async (
}

const client = new ClientImpl(peerId);
await client.initAquamarineRuntime();
await client.initAirInterpreter();

if (connectTo) {
let theAddress: Multiaddr;
Expand Down
2 changes: 1 addition & 1 deletion src/__test__/integration/builtins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
});
Expand Down
5 changes: 2 additions & 3 deletions src/__test__/integration/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
});
Expand Down Expand Up @@ -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',
});
});
Expand Down
35 changes: 17 additions & 18 deletions src/__test__/unit/AquaHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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());
Expand All @@ -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();
});
Expand All @@ -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' };
Expand All @@ -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 = {};
Expand All @@ -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) => {
Expand All @@ -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 };
});
Expand All @@ -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 };
});
Expand All @@ -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 });
Expand All @@ -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
});
Expand All @@ -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';
});
Expand Down Expand Up @@ -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 };
});
Expand All @@ -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';
});
Expand Down Expand Up @@ -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';
});
Expand Down
2 changes: 0 additions & 2 deletions src/__test__/unit/RequestFlow.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import PeerId from 'peer-id';
import { genUUID } from '../../internal/particle';
import { seedToPeerId } from '../../internal/peerIdUtils';
import { RequestFlow } from '../../internal/RequestFlow';

Expand Down
12 changes: 6 additions & 6 deletions src/__test__/unit/air.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -156,23 +156,23 @@ 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;
});

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;
});

const serviceId3 = 'check3';
const fnName3 = 'fn3';
let res3;
client.aquaCallHandler.on(serviceId3, fnName3, (args, _) => {
client.callServiceHandler.on(serviceId3, fnName3, (args, _) => {
res3 = args;
return res3;
});
Expand Down
3 changes: 1 addition & 2 deletions src/__test__/unit/ast.spec.ts
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
28 changes: 14 additions & 14 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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);
};

Expand All @@ -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 = (
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/api.unstable.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { RequestFlowBuilder } from './internal/RequestFlowBuilder';
export * from './internal/AquaHandler';
export * from './internal/CallServiceHandler';
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading