mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-18 10:31:22 +00:00
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:
258
packages/core/interfaces/src/compilerSupport.ts
Normal file
258
packages/core/interfaces/src/compilerSupport.ts
Normal file
@ -0,0 +1,258 @@
|
||||
type SomeNonArrowTypes = ScalarType | OptionType | ArrayType | StructType | TopType | BottomType;
|
||||
|
||||
export type NonArrowType = SomeNonArrowTypes | ProductType<SomeNonArrowTypes>;
|
||||
|
||||
export type TopType = {
|
||||
/**
|
||||
* Type descriptor. Used for pattern-matching
|
||||
*/
|
||||
tag: 'topType';
|
||||
};
|
||||
|
||||
export type BottomType = {
|
||||
/**
|
||||
* Type descriptor. Used for pattern-matching
|
||||
*/
|
||||
tag: 'bottomType';
|
||||
};
|
||||
|
||||
export type OptionType = {
|
||||
/**
|
||||
* Type descriptor. Used for pattern-matching
|
||||
*/
|
||||
tag: 'option';
|
||||
|
||||
/**
|
||||
* Underlying type of the option
|
||||
*/
|
||||
type: NonArrowType;
|
||||
};
|
||||
|
||||
export type NilType = {
|
||||
/**
|
||||
* Type descriptor. Used for pattern-matching
|
||||
*/
|
||||
tag: 'nil';
|
||||
};
|
||||
|
||||
export type ArrayType = {
|
||||
/**
|
||||
* Type descriptor. Used for pattern-matching
|
||||
*/
|
||||
tag: 'array';
|
||||
|
||||
/**
|
||||
* Type of array elements
|
||||
*/
|
||||
type: NonArrowType;
|
||||
};
|
||||
|
||||
/**
|
||||
* All possible scalar type names
|
||||
*/
|
||||
export type ScalarNames =
|
||||
| 'u8'
|
||||
| 'u16'
|
||||
| 'u32'
|
||||
| 'u64'
|
||||
| 'i8'
|
||||
| 'i16'
|
||||
| 'i32'
|
||||
| 'i64'
|
||||
| 'f32'
|
||||
| 'f64'
|
||||
| 'bool'
|
||||
| 'string';
|
||||
|
||||
export type ScalarType = {
|
||||
/**
|
||||
* Type descriptor. Used for pattern-matching
|
||||
*/
|
||||
tag: 'scalar';
|
||||
|
||||
/**
|
||||
* Name of the scalar type
|
||||
*/
|
||||
name: ScalarNames;
|
||||
};
|
||||
|
||||
export type StructType = {
|
||||
/**
|
||||
* Type descriptor. Used for pattern-matching
|
||||
*/
|
||||
tag: 'struct';
|
||||
|
||||
/**
|
||||
* Struct name
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Struct fields
|
||||
*/
|
||||
fields: { [key: string]: NonArrowType };
|
||||
};
|
||||
|
||||
export type LabeledProductType<T> =
|
||||
| {
|
||||
/**
|
||||
* Type descriptor. Used for pattern-matching
|
||||
*/
|
||||
tag: 'labeledProduct';
|
||||
|
||||
/**
|
||||
* Labelled product fields
|
||||
*/
|
||||
fields: { [key: string]: T };
|
||||
}
|
||||
| NilType;
|
||||
|
||||
export type UnlabeledProductType<T> =
|
||||
| {
|
||||
/**
|
||||
* Type descriptor. Used for pattern-matching
|
||||
*/
|
||||
tag: 'unlabeledProduct';
|
||||
|
||||
/**
|
||||
* Items in unlabelled product
|
||||
*/
|
||||
items: Array<T>;
|
||||
}
|
||||
| NilType;
|
||||
|
||||
export type ProductType<T> = UnlabeledProductType<T> | LabeledProductType<T>;
|
||||
|
||||
/**
|
||||
* ArrowType is a profunctor pointing its domain to codomain.
|
||||
* Profunctor means variance: Arrow is contravariant on domain, and variant on codomain.
|
||||
*/
|
||||
export type ArrowType<T> = {
|
||||
/**
|
||||
* Type descriptor. Used for pattern-matching
|
||||
*/
|
||||
tag: 'arrow';
|
||||
|
||||
/**
|
||||
* Where this Arrow is defined
|
||||
*/
|
||||
domain: ProductType<T>;
|
||||
|
||||
/**
|
||||
* Where this Arrow points to
|
||||
*/
|
||||
codomain: UnlabeledProductType<NonArrowType> | NilType;
|
||||
};
|
||||
|
||||
/**
|
||||
* Arrow which domain contains only non-arrow types
|
||||
*/
|
||||
export type ArrowWithoutCallbacks = ArrowType<NonArrowType>;
|
||||
|
||||
/**
|
||||
* Arrow which domain does can contain both non-arrow types and arrows (which themselves cannot contain arrows)
|
||||
*/
|
||||
export type ArrowWithCallbacks = ArrowType<NonArrowType | ArrowWithoutCallbacks>;
|
||||
|
||||
export interface FunctionCallConstants {
|
||||
/**
|
||||
* The name of the relay variable
|
||||
*/
|
||||
relay: string;
|
||||
|
||||
/**
|
||||
* The name of the serviceId used load variables at the beginning of the script
|
||||
*/
|
||||
getDataSrv: string;
|
||||
|
||||
/**
|
||||
* The name of serviceId is used to execute callbacks for the current particle
|
||||
*/
|
||||
callbackSrv: string;
|
||||
|
||||
/**
|
||||
* The name of the serviceId which is called to propagate return value to the generated function caller
|
||||
*/
|
||||
responseSrv: string;
|
||||
|
||||
/**
|
||||
* The name of the functionName which is called to propagate return value to the generated function caller
|
||||
*/
|
||||
responseFnName: string;
|
||||
|
||||
/**
|
||||
* The name of the serviceId which is called to report errors to the generated function caller
|
||||
*/
|
||||
errorHandlingSrv: string;
|
||||
|
||||
/**
|
||||
* The name of the functionName which is called to report errors to the generated function caller
|
||||
*/
|
||||
errorFnName: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Definition of function (`func` instruction) generated by the Aqua compiler
|
||||
*/
|
||||
export interface FunctionCallDef {
|
||||
/**
|
||||
* The name of the function in Aqua language
|
||||
*/
|
||||
functionName: string;
|
||||
|
||||
/**
|
||||
* Underlying arrow which represents function in aqua
|
||||
*/
|
||||
arrow: ArrowWithCallbacks;
|
||||
|
||||
/**
|
||||
* Names of the different entities used in generated air script
|
||||
*/
|
||||
names: FunctionCallConstants;
|
||||
}
|
||||
|
||||
/**
|
||||
* Definition of service registration function (`service` instruction) generated by the Aqua compiler
|
||||
*/
|
||||
export interface ServiceDef {
|
||||
/**
|
||||
* Default service id. If the service has no default id the value should be undefined
|
||||
*/
|
||||
defaultServiceId?: string;
|
||||
|
||||
/**
|
||||
* List of functions which the service consists of
|
||||
*/
|
||||
functions: LabeledProductType<ArrowWithoutCallbacks>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options to configure Aqua function execution
|
||||
*/
|
||||
export interface FnConfig {
|
||||
/**
|
||||
* Sets the TTL (time to live) for particle responsible for the function execution
|
||||
* If the option is not set the default TTL from FluencePeer config is used
|
||||
*/
|
||||
ttl?: number;
|
||||
}
|
||||
|
||||
export const getArgumentTypes = (
|
||||
def: FunctionCallDef,
|
||||
): {
|
||||
[key: string]: NonArrowType | ArrowWithoutCallbacks;
|
||||
} => {
|
||||
if (def.arrow.domain.tag !== 'labeledProduct') {
|
||||
throw new Error('Should be impossible');
|
||||
}
|
||||
|
||||
return def.arrow.domain.fields;
|
||||
};
|
||||
|
||||
export const isReturnTypeVoid = (def: FunctionCallDef): boolean => {
|
||||
if (def.arrow.codomain.tag === 'nil') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return def.arrow.codomain.items.length == 0;
|
||||
};
|
220
packages/core/interfaces/src/fluenceClient.ts
Normal file
220
packages/core/interfaces/src/fluenceClient.ts
Normal file
@ -0,0 +1,220 @@
|
||||
import { SecurityTetraplet } from '@fluencelabs/avm';
|
||||
import type { LogLevel } from '@fluencelabs/marine-js/dist/types';
|
||||
import type { MultiaddrInput } from '@multiformats/multiaddr';
|
||||
import { FnConfig, FunctionCallDef, ServiceDef } from './compilerSupport.js';
|
||||
|
||||
/**
|
||||
* Peer ID's id as a base58 string (multihash/CIDv0).
|
||||
*/
|
||||
export type PeerIdB58 = string;
|
||||
|
||||
/**
|
||||
* Additional information about a service call
|
||||
* @typeparam ArgName
|
||||
*/
|
||||
export interface CallParams<ArgName extends string | null> {
|
||||
/**
|
||||
* The identifier of particle which triggered the call
|
||||
*/
|
||||
particleId: string;
|
||||
|
||||
/**
|
||||
* The peer id which created the particle
|
||||
*/
|
||||
initPeerId: PeerIdB58;
|
||||
|
||||
/**
|
||||
* Particle's timestamp when it was created
|
||||
*/
|
||||
timestamp: number;
|
||||
|
||||
/**
|
||||
* Time to live in milliseconds. The time after the particle should be expired
|
||||
*/
|
||||
ttl: number;
|
||||
|
||||
/**
|
||||
* Particle's signature
|
||||
*/
|
||||
signature?: string;
|
||||
|
||||
/**
|
||||
* Security tetraplets
|
||||
*/
|
||||
tetraplets: ArgName extends string ? Record<ArgName, SecurityTetraplet[]> : Record<string, never>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Node of the Fluence network specified as a pair of node's multiaddr and it's peer id
|
||||
*/
|
||||
type Node = {
|
||||
peerId: PeerIdB58;
|
||||
multiaddr: string;
|
||||
};
|
||||
|
||||
export type RelayOptions = string | MultiaddrInput | Node;
|
||||
|
||||
export type KeyTypes = 'RSA' | 'Ed25519' | 'secp256k1';
|
||||
|
||||
export type KeyPairOptions = {
|
||||
type: 'Ed25519';
|
||||
source: 'random' | Uint8Array;
|
||||
};
|
||||
|
||||
/**
|
||||
* Configuration used when initiating Fluence Client
|
||||
*/
|
||||
export interface ClientOptions {
|
||||
/**
|
||||
* Node in Fluence network to connect to.
|
||||
* Can be in the form of:
|
||||
* - string: multiaddr in string format
|
||||
* - Multiaddr: multiaddr object, @see https://github.com/multiformats/js-multiaddr
|
||||
* - Node: node structure, @see Node
|
||||
* - Implementation of FluenceConnection class, @see FluenceConnection
|
||||
* If not specified the will work locally and would not be able to send or receive particles.
|
||||
*/
|
||||
relay?: RelayOptions;
|
||||
|
||||
/**
|
||||
* Specify the KeyPair to be used to identify the Fluence Peer.
|
||||
* Will be generated randomly if not specified
|
||||
*/
|
||||
keyPair?: KeyPairOptions;
|
||||
|
||||
connectionOptions?: {
|
||||
/**
|
||||
* When the peer established the connection to the network it sends a ping-like message to check if it works correctly.
|
||||
* The options allows to specify the timeout for that message in milliseconds.
|
||||
* If not specified the default timeout will be used
|
||||
CallParams,
|
||||
*/
|
||||
skipCheckConnection?: boolean;
|
||||
|
||||
/**
|
||||
* The dialing timeout in milliseconds
|
||||
*/
|
||||
dialTimeoutMs?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the default TTL for all particles originating from the peer with no TTL specified.
|
||||
* If the originating particle's TTL is defined then that value will be used
|
||||
* If the option is not set default TTL will be 7000
|
||||
*/
|
||||
defaultTtlMs?: number;
|
||||
|
||||
/**
|
||||
* Enables\disabled various debugging features
|
||||
*/
|
||||
debug?: {
|
||||
/**
|
||||
* If set to true, newly initiated particle ids will be printed to console.
|
||||
* Useful to see what particle id is responsible for aqua function
|
||||
*/
|
||||
printParticleId?: boolean;
|
||||
/**
|
||||
* Log level for marine services. By default logging is turned off.
|
||||
*/
|
||||
marineLogLevel?: LogLevel;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Information about Fluence Peer connection.
|
||||
* Represented as object with the following keys:
|
||||
* - `isInitialized`: Is the peer initialized or not.
|
||||
* - `peerId`: Peer Id of the peer. Null if the peer is not initialized
|
||||
* - `isConnected`: Is the peer connected to network or not
|
||||
* - `relayPeerId`: Peer Id of the relay the peer is connected to. If the connection is direct relayPeerId is null
|
||||
* - `isDirect`: True if the peer is connected to the network directly (not through relay)
|
||||
*/
|
||||
export type PeerStatus =
|
||||
| {
|
||||
isInitialized: false;
|
||||
peerId: null;
|
||||
isConnected: false;
|
||||
relayPeerId: null;
|
||||
}
|
||||
| {
|
||||
isInitialized: true;
|
||||
peerId: PeerIdB58;
|
||||
isConnected: false;
|
||||
relayPeerId: null;
|
||||
}
|
||||
| {
|
||||
isInitialized: true;
|
||||
peerId: PeerIdB58;
|
||||
isConnected: true;
|
||||
relayPeerId: PeerIdB58;
|
||||
}
|
||||
| {
|
||||
isInitialized: true;
|
||||
peerId: PeerIdB58;
|
||||
isConnected: true;
|
||||
isDirect: true;
|
||||
relayPeerId: null;
|
||||
};
|
||||
|
||||
export interface IFluenceClient {
|
||||
/**
|
||||
* Get the peer's status
|
||||
*/
|
||||
start(config?: ClientOptions): Promise<void>;
|
||||
|
||||
/**
|
||||
* Un-initializes the peer: stops all the underlying workflows, stops the Aqua VM and disconnects from the Fluence network
|
||||
*/
|
||||
stop(): Promise<void>;
|
||||
|
||||
/**
|
||||
* Get the peer's status
|
||||
*/
|
||||
getStatus(): PeerStatus;
|
||||
|
||||
/**
|
||||
* Return peers SK
|
||||
*/
|
||||
getSk(): Uint8Array;
|
||||
|
||||
// TODO: come up with a working interface for
|
||||
// - particle creation
|
||||
// - particle initialization
|
||||
// - service registration
|
||||
internals: any;
|
||||
|
||||
// TODO: extract this out of Client interface
|
||||
compilerSupport: {
|
||||
callFunction: (args: CallFunctionArgs) => Promise<unknown>;
|
||||
registerService: (args: RegisterServiceArgs) => void;
|
||||
};
|
||||
}
|
||||
|
||||
export interface CallFunctionArgs {
|
||||
def: FunctionCallDef;
|
||||
script: string;
|
||||
config: FnConfig;
|
||||
args: { [key: string]: any };
|
||||
}
|
||||
|
||||
export interface RegisterServiceArgs {
|
||||
def: ServiceDef;
|
||||
serviceId: string | undefined;
|
||||
service: any;
|
||||
}
|
||||
|
||||
export const asFluencePeer = (fluencePeerCandidate: unknown): IFluenceClient => {
|
||||
if (isFluencePeer(fluencePeerCandidate)) {
|
||||
return fluencePeerCandidate;
|
||||
}
|
||||
|
||||
throw new Error(`Argument ${fluencePeerCandidate} is not a Fluence Peer`);
|
||||
};
|
||||
|
||||
export const isFluencePeer = (fluencePeerCandidate: unknown): fluencePeerCandidate is IFluenceClient => {
|
||||
if (fluencePeerCandidate && (fluencePeerCandidate as any).__isFluenceAwesome) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
2
packages/core/interfaces/src/index.ts
Normal file
2
packages/core/interfaces/src/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './compilerSupport.js'
|
||||
export * from './fluenceClient.js'
|
Reference in New Issue
Block a user