feat!: Standalone web JS Client (#243)

- Move marine-related part into FJS repo (fixes DXJ-184)
- Move towards component-oriented architecture (fixes DXJ-183)
- Different JS Client distros for node.js and web (fixes DXJ-185)
- Update libp2p to 0.42.2 (fixes DXJ-26)
- Add JS Client API (fixes DXJ-196, fixes DXJ-177, fixes DXJ-60)
- Add Smoke test for JS Client web (fixes DXJ-253)

---------

Co-authored-by: Anatoly Laskaris <github_me@nahsi.dev>
This commit is contained in:
Pavel
2023-02-13 17:41:35 +03:00
committed by GitHub
parent e02c506d7f
commit 9667c4fec6
212 changed files with 16522 additions and 7886 deletions

View File

@ -0,0 +1,222 @@
import { aqua2ts, ts2aqua } from '../conversions.js';
const i32 = { tag: 'scalar', name: 'i32' } as const;
const opt_i32 = {
tag: 'option',
type: i32,
} as const;
const array_i32 = { tag: 'array', type: i32 };
const array_opt_i32 = { tag: 'array', type: opt_i32 };
const labeledProduct = {
tag: 'labeledProduct',
fields: {
a: i32,
b: opt_i32,
c: array_opt_i32,
},
};
const struct = {
tag: 'struct',
name: 'someStruct',
fields: {
a: i32,
b: opt_i32,
c: array_opt_i32,
},
};
const structs = [
{
aqua: {
a: 1,
b: [2],
c: [[1], [2]],
},
ts: {
a: 1,
b: 2,
c: [1, 2],
},
},
{
aqua: {
a: 1,
b: [],
c: [[], [2]],
},
ts: {
a: 1,
b: null,
c: [null, 2],
},
},
];
const labeledProduct2 = {
tag: 'labeledProduct',
fields: {
x: i32,
y: i32,
},
};
const nestedLabeledProductType = {
tag: 'labeledProduct',
fields: {
a: labeledProduct2,
b: {
tag: 'option',
type: labeledProduct2,
},
c: {
tag: 'array',
type: labeledProduct2,
},
},
};
const nestedStructs = [
{
aqua: {
a: {
x: 1,
y: 2,
},
b: [
{
x: 1,
y: 2,
},
],
c: [
{
x: 1,
y: 2,
},
{
x: 3,
y: 4,
},
],
},
ts: {
a: {
x: 1,
y: 2,
},
b: {
x: 1,
y: 2,
},
c: [
{
x: 1,
y: 2,
},
{
x: 3,
y: 4,
},
],
},
},
{
aqua: {
a: {
x: 1,
y: 2,
},
b: [],
c: [],
},
ts: {
a: {
x: 1,
y: 2,
},
b: null,
c: [],
},
},
];
describe('Conversion from aqua to typescript', () => {
test.each`
aqua | ts | type
${1} | ${1} | ${i32}
${[]} | ${null} | ${opt_i32}
${[1]} | ${1} | ${opt_i32}
${[1, 2, 3]} | ${[1, 2, 3]} | ${array_i32}
${[]} | ${[]} | ${array_i32}
${[[1]]} | ${[1]} | ${array_opt_i32}
${[[]]} | ${[null]} | ${array_opt_i32}
${[[1], [2]]} | ${[1, 2]} | ${array_opt_i32}
${[[], [2]]} | ${[null, 2]} | ${array_opt_i32}
${structs[0].aqua} | ${structs[0].ts} | ${labeledProduct}
${structs[1].aqua} | ${structs[1].ts} | ${labeledProduct}
${structs[0].aqua} | ${structs[0].ts} | ${struct}
${structs[1].aqua} | ${structs[1].ts} | ${struct}
${nestedStructs[0].aqua} | ${nestedStructs[0].ts} | ${nestedLabeledProductType}
${nestedStructs[1].aqua} | ${nestedStructs[1].ts} | ${nestedLabeledProductType}
`(
//
'aqua: $aqua. ts: $ts. type: $type',
async ({ aqua, ts, type }) => {
// arrange
// act
const tsFromAqua = aqua2ts(aqua, type);
const aquaFromTs = ts2aqua(ts, type);
// assert
expect(tsFromAqua).toStrictEqual(ts);
expect(aquaFromTs).toStrictEqual(aqua);
},
);
});
describe('Conversion corner cases', () => {
it('Should accept undefined in object entry', () => {
// arrange
const type = {
tag: 'labeledProduct',
fields: {
x: opt_i32,
y: opt_i32,
},
} as const;
const valueInTs = {
x: 1,
};
const valueInAqua = {
x: [1],
y: [],
};
// act
const aqua = ts2aqua(valueInTs, type);
const ts = aqua2ts(valueInAqua, type);
// assert
expect(aqua).toStrictEqual({
x: [1],
y: [],
});
expect(ts).toStrictEqual({
x: 1,
y: null,
});
});
});

View File

@ -0,0 +1,90 @@
import {
ArrowWithoutCallbacks,
FnConfig,
FunctionCallDef,
NonArrowType,
getArgumentTypes,
isReturnTypeVoid,
IFluenceClient,
} from '@fluencelabs/interfaces';
import {
injectRelayService,
registerParticleScopeService,
responseService,
errorHandlingService,
ServiceDescription,
userHandlerService,
injectValueService,
} from './services.js';
/**
* Convenience function which does all the internal work of creating particles
* and making necessary service registrations in order to support Aqua function calls
*
* @param def - function definition generated by the Aqua compiler
* @param script - air script with function execution logic generated by the Aqua compiler
* @param config - options to configure Aqua function execution
* @param peer - Fluence Peer to invoke the function at
* @param args - args in the form of JSON where each key corresponds to the name of the argument
* @returns
*/
export function callFunctionImpl(
def: FunctionCallDef,
script: string,
config: FnConfig,
peer: IFluenceClient,
args: { [key: string]: any },
): Promise<unknown> {
const argumentTypes = getArgumentTypes(def);
const promise = new Promise((resolve, reject) => {
const particle = peer.internals.createNewParticle(script, config?.ttl);
if (particle instanceof Error) {
return reject(particle.message);
}
for (let [name, argVal] of Object.entries(args)) {
const type = argumentTypes[name];
let service: ServiceDescription;
if (type.tag === 'arrow') {
service = userHandlerService(def.names.callbackSrv, [name, type], argVal);
} else {
service = injectValueService(def.names.getDataSrv, name, type, argVal);
}
registerParticleScopeService(peer, particle, service);
}
registerParticleScopeService(peer, particle, responseService(def, resolve));
registerParticleScopeService(peer, particle, injectRelayService(def, peer));
registerParticleScopeService(peer, particle, errorHandlingService(def, reject));
peer.internals.initiateParticle(particle, (stage: any) => {
// If function is void, then it's completed when one of the two conditions is met:
// 1. The particle is sent to the network (state 'sent')
// 2. All CallRequests are executed, e.g., all variable loading and local function calls are completed (state 'localWorkDone')
if (isReturnTypeVoid(def) && (stage.stage === 'sent' || stage.stage === 'localWorkDone')) {
resolve(undefined);
}
if (stage.stage === 'sendingError') {
reject(`Could not send particle for ${def.functionName}: not connected (particle id: ${particle.id})`);
}
if (stage.stage === 'expired') {
reject(`Request timed out after ${particle.ttl} for ${def.functionName} (particle id: ${particle.id})`);
}
if (stage.stage === 'interpreterError') {
reject(
`Script interpretation failed for ${def.functionName}: ${stage.errorMessage} (particle id: ${particle.id})`,
);
}
});
});
return promise;
}

View File

@ -0,0 +1,186 @@
import { jsonify } from '../js-peer/utils.js';
import { match } from 'ts-pattern';
import type { ArrowType, ArrowWithoutCallbacks, NonArrowType } from '@fluencelabs/interfaces';
import { CallServiceData } from '../interfaces/commonTypes.js';
/**
* Convert value from its representation in aqua language to representation in typescript
* @param value - value as represented in aqua
* @param type - definition of the aqua type
* @returns value represented in typescript
*/
export const aqua2ts = (value: any, type: NonArrowType): any => {
const res = match(type)
.with({ tag: 'nil' }, () => {
return null;
})
.with({ tag: 'option' }, (opt) => {
if (value.length === 0) {
return null;
} else {
return aqua2ts(value[0], opt.type);
}
})
// @ts-ignore
.with({ tag: 'scalar' }, { tag: 'bottomType' }, { tag: 'topType' }, () => {
return value;
})
.with({ tag: 'array' }, (arr) => {
return value.map((y: any) => aqua2ts(y, arr.type));
})
.with({ tag: 'struct' }, (x) => {
return Object.entries(x.fields).reduce((agg, [key, type]) => {
const val = aqua2ts(value[key], type);
return { ...agg, [key]: val };
}, {});
})
.with({ tag: 'labeledProduct' }, (x) => {
return Object.entries(x.fields).reduce((agg, [key, type]) => {
const val = aqua2ts(value[key], type);
return { ...agg, [key]: val };
}, {});
})
.with({ tag: 'unlabeledProduct' }, (x) => {
return x.items.map((type, index) => {
return aqua2ts(value[index], type);
});
})
// uncomment to check that every pattern in matched
// .exhaustive();
.otherwise(() => {
throw new Error('Unexpected tag: ' + jsonify(type));
});
return res;
};
/**
* Convert call service arguments list from their aqua representation to representation in typescript
* @param req - call service data
* @param arrow - aqua type definition
* @returns arguments in typescript representation
*/
export const aquaArgs2Ts = (req: CallServiceData, arrow: ArrowWithoutCallbacks) => {
const argTypes = match(arrow.domain)
.with({ tag: 'labeledProduct' }, (x) => {
return Object.values(x.fields);
})
.with({ tag: 'unlabeledProduct' }, (x) => {
return x.items;
})
.with({ tag: 'nil' }, (x) => {
return [];
})
// uncomment to check that every pattern in matched
// .exhaustive()
.otherwise(() => {
throw new Error('Unexpected tag: ' + jsonify(arrow.domain));
});
if (req.args.length !== argTypes.length) {
throw new Error(`incorrect number of arguments, expected: ${argTypes.length}, got: ${req.args.length}`);
}
return req.args.map((arg, index) => {
return aqua2ts(arg, argTypes[index]);
});
};
/**
* Convert value from its typescript representation to representation in aqua
* @param value - the value as represented in typescript
* @param type - definition of the aqua type
* @returns value represented in aqua
*/
export const ts2aqua = (value: any, type: NonArrowType): any => {
const res = match(type)
.with({ tag: 'nil' }, () => {
return null;
})
.with({ tag: 'option' }, (opt) => {
if (value === null || value === undefined) {
return [];
} else {
return [ts2aqua(value, opt.type)];
}
})
// @ts-ignore
.with({ tag: 'scalar' }, { tag: 'bottomType' }, { tag: 'topType' }, () => {
return value;
})
.with({ tag: 'array' }, (arr) => {
return value.map((y: any) => ts2aqua(y, arr.type));
})
.with({ tag: 'struct' }, (x) => {
return Object.entries(x.fields).reduce((agg, [key, type]) => {
const val = ts2aqua(value[key], type);
return { ...agg, [key]: val };
}, {});
})
.with({ tag: 'labeledProduct' }, (x) => {
return Object.entries(x.fields).reduce((agg, [key, type]) => {
const val = ts2aqua(value[key], type);
return { ...agg, [key]: val };
}, {});
})
.with({ tag: 'unlabeledProduct' }, (x) => {
return x.items.map((type, index) => {
return ts2aqua(value[index], type);
});
})
// uncomment to check that every pattern in matched
// .exhaustive()
.otherwise(() => {
throw new Error('Unexpected tag: ' + jsonify(type));
});
return res;
};
/**
* Convert return type of the service from it's typescript representation to representation in aqua
* @param returnValue - the value as represented in typescript
* @param arrowType - the arrow type which describes the service
* @returns - value represented in aqua
*/
export const returnType2Aqua = (returnValue: any, arrowType: ArrowType<NonArrowType>) => {
if (arrowType.codomain.tag === 'nil') {
return {};
}
if (arrowType.codomain.items.length === 0) {
return {};
}
if (arrowType.codomain.items.length === 1) {
return ts2aqua(returnValue, arrowType.codomain.items[0]);
}
return arrowType.codomain.items.map((type, index) => {
return ts2aqua(returnValue[index], type);
});
};
/**
* Converts response value from aqua its representation to representation in typescript
* @param req - call service data
* @param arrow - aqua type definition
* @returns response value in typescript representation
*/
export const responseServiceValue2ts = (req: CallServiceData, arrow: ArrowType<any>) => {
return match(arrow.codomain)
.with({ tag: 'nil' }, () => {
return undefined;
})
.with({ tag: 'unlabeledProduct' }, (x) => {
if (x.items.length === 0) {
return undefined;
}
if (x.items.length === 1) {
return aqua2ts(req.args[0], x.items[0]);
}
return req.args.map((y, index) => aqua2ts(y, x.items[index]));
})
.exhaustive();
};

View File

@ -0,0 +1,46 @@
import type { IFluenceClient, ServiceDef } from '@fluencelabs/interfaces';
import { registerGlobalService, userHandlerService } from './services.js';
export const registerServiceImpl = (
peer: IFluenceClient,
def: ServiceDef,
serviceId: string | undefined,
service: any,
) => {
// TODO: TBH service registration is just putting some stuff into a hashmap
// there should not be such a check at all
if (!peer.getStatus().isInitialized) {
throw new Error(
'Could not register the service because the peer is not initialized. Are you passing the wrong peer to the register function?',
);
}
// Checking for missing keys
const requiredKeys = def.functions.tag === 'nil' ? [] : Object.keys(def.functions.fields);
const incorrectServiceDefinitions = requiredKeys.filter((f) => !(f in service));
if (!!incorrectServiceDefinitions.length) {
throw new Error(
`Error registering service ${serviceId}: missing functions: ` +
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
);
}
if (!serviceId) {
serviceId = def.defaultServiceId;
}
if (!serviceId) {
throw new Error('Service ID must be specified');
}
const singleFunctions = def.functions.tag === 'nil' ? [] : Object.entries(def.functions.fields);
for (let singleFunction of singleFunctions) {
let [name, type] = singleFunction;
// The function has type of (arg1, arg2, arg3, ... , callParams) => CallServiceResultType | void
// Account for the fact that user service might be defined as a class - .bind(...)
const userDefinedHandler = service[name].bind(service);
const serviceDescription = userHandlerService(serviceId, singleFunction, userDefinedHandler);
registerGlobalService(peer, serviceDescription);
}
};

View File

@ -0,0 +1,177 @@
import { SecurityTetraplet } from '@fluencelabs/avm';
import { match } from 'ts-pattern';
import { Particle } from '../js-peer/Particle.js';
import { CallServiceData, GenericCallServiceHandler, ResultCodes } from '../interfaces/commonTypes.js';
import { aquaArgs2Ts, responseServiceValue2ts, returnType2Aqua, ts2aqua } from './conversions.js';
import {
IFluenceClient,
CallParams,
ArrowWithoutCallbacks,
FunctionCallConstants,
FunctionCallDef,
NonArrowType,
} from '@fluencelabs/interfaces';
export interface ServiceDescription {
serviceId: string;
fnName: string;
handler: GenericCallServiceHandler;
}
/**
* Creates a service which injects relay's peer id into aqua space
*/
export const injectRelayService = (def: FunctionCallDef, peer: IFluenceClient) => {
return {
serviceId: def.names.getDataSrv,
fnName: def.names.relay,
handler: () => {
return {
retCode: ResultCodes.success,
result: peer.getStatus().relayPeerId,
};
},
};
};
/**
* Creates a service which injects plain value into aqua space
*/
export const injectValueService = (serviceId: string, fnName: string, valueType: NonArrowType, value: any) => {
return {
serviceId: serviceId,
fnName: fnName,
handler: () => {
return {
retCode: ResultCodes.success,
result: ts2aqua(value, valueType),
};
},
};
};
/**
* Creates a service which is used to return value from aqua function into typescript space
*/
export const responseService = (def: FunctionCallDef, resolveCallback: Function) => {
return {
serviceId: def.names.responseSrv,
fnName: def.names.responseFnName,
handler: (req: CallServiceData) => {
const userFunctionReturn = responseServiceValue2ts(req, def.arrow);
setTimeout(() => {
resolveCallback(userFunctionReturn);
}, 0);
return {
retCode: ResultCodes.success,
result: {},
};
},
};
};
/**
* Creates a service which is used to return errors from aqua function into typescript space
*/
export const errorHandlingService = (def: FunctionCallDef, rejectCallback: Function) => {
return {
serviceId: def.names.errorHandlingSrv,
fnName: def.names.errorFnName,
handler: (req: CallServiceData) => {
const [err, _] = req.args;
setTimeout(() => {
rejectCallback(err);
}, 0);
return {
retCode: ResultCodes.success,
result: {},
};
},
};
};
/**
* Creates a service for user-defined service function handler
*/
export const userHandlerService = (
serviceId: string,
arrowType: [string, ArrowWithoutCallbacks],
userHandler: (...args: Array<unknown>) => Promise<unknown>,
) => {
const [fnName, type] = arrowType;
return {
serviceId,
fnName,
handler: async (req: CallServiceData) => {
const args = [...aquaArgs2Ts(req, type), extractCallParams(req, type)];
const rawResult = await userHandler.apply(null, args);
const result = returnType2Aqua(rawResult, type);
return {
retCode: ResultCodes.success,
result: result,
};
},
};
};
/**
* Converts argument of aqua function to a corresponding service.
* For arguments of non-arrow types the resulting service injects the argument into aqua space.
* For arguments of arrow types the resulting service calls the corresponding function.
*/
export const argToServiceDef = (
arg: any,
argName: string,
argType: NonArrowType | ArrowWithoutCallbacks,
names: FunctionCallConstants,
): ServiceDescription => {
if (argType.tag === 'arrow') {
return userHandlerService(names.callbackSrv, [argName, argType], arg);
} else {
return injectValueService(names.getDataSrv, argName, arg, argType);
}
};
/**
* Extracts call params from from call service data according to aqua type definition
*/
const extractCallParams = (req: CallServiceData, arrow: ArrowWithoutCallbacks): CallParams<any> => {
const names = match(arrow.domain)
.with({ tag: 'nil' }, () => {
return [] as string[];
})
.with({ tag: 'labeledProduct' }, (x) => {
return Object.keys(x.fields);
})
.with({ tag: 'unlabeledProduct' }, (x) => {
return x.items.map((_, index) => 'arg' + index);
})
.exhaustive();
const tetraplets: Record<string, SecurityTetraplet[]> = {};
for (let i = 0; i < req.args.length; i++) {
if (names[i]) {
tetraplets[names[i]] = req.tetraplets[i];
}
}
const callParams = {
...req.particleContext,
tetraplets,
};
return callParams;
};
export const registerParticleScopeService = (peer: IFluenceClient, particle: Particle, service: ServiceDescription) => {
peer.internals.regHandler.forParticle(particle.id, service.serviceId, service.fnName, service.handler);
};
export const registerGlobalService = (peer: IFluenceClient, service: ServiceDescription) => {
peer.internals.regHandler.common(service.serviceId, service.fnName, service.handler);
};