mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-25 13:51:34 +00:00
extracting, re-arranging making sure interface don't get in the way
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
service Peer("peer"):
|
alias PeerId: string
|
||||||
-- Get information about the peer
|
|
||||||
identify() -> Info
|
|
||||||
|
|
||||||
|
service Peer("peer"):
|
||||||
-- Get Unix timestamp in milliseconds
|
-- Get Unix timestamp in milliseconds
|
||||||
timestamp_ms() -> u64
|
timestamp_ms() -> u64
|
||||||
|
|
||||||
|
123
packages/@tests/smoke_web/src/_aqua/smoke_test.ts
Normal file
123
packages/@tests/smoke_web/src/_aqua/smoke_test.ts
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.7.2-314
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import type { IFluencePeer } from '@fluencelabs/js-peer/dist/interfaces';
|
||||||
|
import type { CallParams } from '@fluencelabs/js-peer/dist/interfaces/commonTypes';
|
||||||
|
import { callFunction$$, registerService$$ } from '@fluencelabs/js-client.api/dist/compilerSupport/v4';
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface PeerDef {
|
||||||
|
timestamp_ms: (callParams: CallParams<null>) => number | Promise<number>;
|
||||||
|
}
|
||||||
|
export function registerPeer(service: PeerDef): void;
|
||||||
|
export function registerPeer(serviceId: string, service: PeerDef): void;
|
||||||
|
export function registerPeer(peer: IFluencePeer, service: PeerDef): void;
|
||||||
|
export function registerPeer(peer: IFluencePeer, serviceId: string, service: PeerDef): void;
|
||||||
|
|
||||||
|
export function registerPeer(...args: any) {
|
||||||
|
registerService$$(args, {
|
||||||
|
defaultServiceId: 'peer',
|
||||||
|
functions: {
|
||||||
|
tag: 'labeledProduct',
|
||||||
|
fields: {
|
||||||
|
timestamp_ms: {
|
||||||
|
tag: 'arrow',
|
||||||
|
domain: {
|
||||||
|
tag: 'nil',
|
||||||
|
},
|
||||||
|
codomain: {
|
||||||
|
tag: 'unlabeledProduct',
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
tag: 'scalar',
|
||||||
|
name: 'u64',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function getRelayTime(relayPeerId: string, config?: { ttl?: number }): Promise<number>;
|
||||||
|
|
||||||
|
export function getRelayTime(peer: IFluencePeer, relayPeerId: string, config?: { ttl?: number }): Promise<number>;
|
||||||
|
|
||||||
|
export function getRelayTime(...args: any) {
|
||||||
|
let script = `
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "relayPeerId") [] relayPeerId)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call relayPeerId ("peer" "timestamp_ms") [] ts)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [ts])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
return callFunction$$(
|
||||||
|
args,
|
||||||
|
{
|
||||||
|
functionName: 'getRelayTime',
|
||||||
|
arrow: {
|
||||||
|
tag: 'arrow',
|
||||||
|
domain: {
|
||||||
|
tag: 'labeledProduct',
|
||||||
|
fields: {
|
||||||
|
relayPeerId: {
|
||||||
|
tag: 'scalar',
|
||||||
|
name: 'string',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
codomain: {
|
||||||
|
tag: 'unlabeledProduct',
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
tag: 'scalar',
|
||||||
|
name: 'u64',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
names: {
|
||||||
|
relay: '-relay-',
|
||||||
|
getDataSrv: 'getDataSrv',
|
||||||
|
callbackSrv: 'callbackSrv',
|
||||||
|
responseSrv: 'callbackSrv',
|
||||||
|
responseFnName: 'response',
|
||||||
|
errorHandlingSrv: 'errorHandlingSrv',
|
||||||
|
errorFnName: 'error',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
script,
|
||||||
|
);
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
{
|
{
|
||||||
"extends": "../../../tsconfig.json",
|
"extends": "../../../tsconfig.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
|
"module": "CommonJS",
|
||||||
|
"target": "ES5",
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"moduleResolution": "node"
|
"moduleResolution": "node"
|
||||||
},
|
},
|
||||||
|
@ -13,6 +13,14 @@
|
|||||||
".": {
|
".": {
|
||||||
"import": "./dist/index.js",
|
"import": "./dist/index.js",
|
||||||
"types": "./dist/index.d.ts"
|
"types": "./dist/index.d.ts"
|
||||||
|
},
|
||||||
|
"./compilerSupport/v3": {
|
||||||
|
"import": "./dist/compilerSupport/v3.js",
|
||||||
|
"types": "./dist/compilerSupport/v3.d.ts"
|
||||||
|
},
|
||||||
|
"./compilerSupport/v4": {
|
||||||
|
"import": "./dist/compilerSupport/v4.js",
|
||||||
|
"types": "./dist/compilerSupport/v4.d.ts"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -14,15 +14,16 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { FluencePeer } from '@fluencelabs/js-peer/dist/FluencePeer';
|
import { IFluencePeer, isFluencePeer } from '@fluencelabs/js-peer/dist/interfaces/index';
|
||||||
import { FnConfig, FunctionCallDef, ServiceDef } from '@fluencelabs/js-peer/dist/compilerSupport/interface';
|
import { FnConfig, FunctionCallDef, ServiceDef } from '@fluencelabs/js-peer/dist/js-peer/compilerSupport/interface';
|
||||||
import { registerServiceImpl } from '@fluencelabs/js-peer/dist/compilerSupport/registerService';
|
import { registerServiceImpl } from '@fluencelabs/js-peer/dist/js-peer/compilerSupport/registerService';
|
||||||
import { callFunctionImpl, getArgumentTypes } from '@fluencelabs/js-peer/dist/compilerSupport/callFunction';
|
import { callFunctionImpl, getArgumentTypes } from '@fluencelabs/js-peer/dist/js-peer/compilerSupport/callFunction';
|
||||||
|
|
||||||
import { getDefaultPeer } from './util';
|
import { getDefaultPeer } from '../index.js';
|
||||||
|
|
||||||
|
export type { IFluencePeer } from '@fluencelabs/js-peer/dist/interfaces/index';
|
||||||
|
export type { CallParams } from '@fluencelabs/js-peer/dist/interfaces/commonTypes';
|
||||||
|
|
||||||
export { FluencePeer } from '@fluencelabs/js-peer/dist/FluencePeer';
|
|
||||||
export { CallParams } from '@fluencelabs/js-peer/dist/commonTypes';
|
|
||||||
export {
|
export {
|
||||||
ArrayType,
|
ArrayType,
|
||||||
ArrowType,
|
ArrowType,
|
||||||
@ -43,9 +44,9 @@ export {
|
|||||||
StructType,
|
StructType,
|
||||||
TopType,
|
TopType,
|
||||||
UnlabeledProductType,
|
UnlabeledProductType,
|
||||||
} from '@fluencelabs/js-peer/dist/compilerSupport/interface';
|
} from '@fluencelabs/js-peer/dist/js-peer/compilerSupport/interface';
|
||||||
export { callFunctionImpl } from '@fluencelabs/js-peer/dist/compilerSupport/callFunction';
|
export { callFunctionImpl } from '@fluencelabs/js-peer/dist/js-peer/compilerSupport/callFunction';
|
||||||
export { registerServiceImpl } from '@fluencelabs/js-peer/dist/compilerSupport/registerService';
|
export { registerServiceImpl } from '@fluencelabs/js-peer/dist/js-peer/compilerSupport/registerService';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenience function to support Aqua `func` generation backend
|
* Convenience function to support Aqua `func` generation backend
|
||||||
@ -55,8 +56,8 @@ export { registerServiceImpl } from '@fluencelabs/js-peer/dist/compilerSupport/r
|
|||||||
* @param def - function definition generated by the Aqua compiler
|
* @param def - function definition generated by the Aqua compiler
|
||||||
* @param script - air script with function execution logic generated by the Aqua compiler
|
* @param script - air script with function execution logic generated by the Aqua compiler
|
||||||
*/
|
*/
|
||||||
export const callFunction = (rawFnArgs: Array<any>, def: FunctionCallDef, script: string) => {
|
export const callFunction = async (rawFnArgs: Array<any>, def: FunctionCallDef, script: string): Promise<unknown> => {
|
||||||
const { args, peer, config } = extractFunctionArgs(rawFnArgs, def);
|
const { args, peer, config } = await extractFunctionArgs(rawFnArgs, def);
|
||||||
return callFunctionImpl(def, script, config || {}, peer, args);
|
return callFunctionImpl(def, script, config || {}, peer, args);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -67,11 +68,11 @@ export const callFunction = (rawFnArgs: Array<any>, def: FunctionCallDef, script
|
|||||||
* @param args - raw arguments passed by user to the generated function
|
* @param args - raw arguments passed by user to the generated function
|
||||||
* @param def - service definition generated by the Aqua compiler
|
* @param def - service definition generated by the Aqua compiler
|
||||||
*/
|
*/
|
||||||
export function registerService(args: any[], def: ServiceDef) {
|
export const registerService = async (args: any[], def: ServiceDef): Promise<unknown> => {
|
||||||
const { peer, service, serviceId } = extractServiceArgs(args, def.defaultServiceId);
|
const { peer, service, serviceId } = await extractServiceArgs(args, def.defaultServiceId);
|
||||||
|
|
||||||
return registerServiceImpl(peer, def, serviceId, service);
|
return registerServiceImpl(peer, def, serviceId, service);
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Arguments could be passed in one these configurations:
|
* Arguments could be passed in one these configurations:
|
||||||
@ -83,27 +84,27 @@ export function registerService(args: any[], def: ServiceDef) {
|
|||||||
* This function select the appropriate configuration and returns
|
* This function select the appropriate configuration and returns
|
||||||
* arguments in a structured way of: { peer, config, args }
|
* arguments in a structured way of: { peer, config, args }
|
||||||
*/
|
*/
|
||||||
const extractFunctionArgs = (
|
const extractFunctionArgs = async (
|
||||||
args: any[],
|
args: any[],
|
||||||
def: FunctionCallDef,
|
def: FunctionCallDef,
|
||||||
): {
|
): Promise<{
|
||||||
peer: FluencePeer;
|
peer: IFluencePeer;
|
||||||
config?: FnConfig;
|
config?: FnConfig;
|
||||||
args: { [key: string]: any };
|
args: { [key: string]: any };
|
||||||
} => {
|
}> => {
|
||||||
const argumentTypes = getArgumentTypes(def);
|
const argumentTypes = getArgumentTypes(def);
|
||||||
const argumentNames = Object.keys(argumentTypes);
|
const argumentNames = Object.keys(argumentTypes);
|
||||||
const numberOfExpectedArgs = argumentNames.length;
|
const numberOfExpectedArgs = argumentNames.length;
|
||||||
|
|
||||||
let peer: FluencePeer;
|
let peer: IFluencePeer;
|
||||||
let structuredArgs: any[];
|
let structuredArgs: any[];
|
||||||
let config: FnConfig;
|
let config: FnConfig;
|
||||||
if (FluencePeer.isInstance(args[0])) {
|
if (isFluencePeer(args[0])) {
|
||||||
peer = args[0];
|
peer = args[0];
|
||||||
structuredArgs = args.slice(1, numberOfExpectedArgs + 1);
|
structuredArgs = args.slice(1, numberOfExpectedArgs + 1);
|
||||||
config = args[numberOfExpectedArgs + 1];
|
config = args[numberOfExpectedArgs + 1];
|
||||||
} else {
|
} else {
|
||||||
peer = getDefaultPeer();
|
peer = await getDefaultPeer();
|
||||||
structuredArgs = args.slice(0, numberOfExpectedArgs);
|
structuredArgs = args.slice(0, numberOfExpectedArgs);
|
||||||
config = args[numberOfExpectedArgs];
|
config = args[numberOfExpectedArgs];
|
||||||
}
|
}
|
||||||
@ -133,17 +134,17 @@ const extractFunctionArgs = (
|
|||||||
* This function select the appropriate configuration and returns
|
* This function select the appropriate configuration and returns
|
||||||
* arguments in a structured way of: { peer, serviceId, service }
|
* arguments in a structured way of: { peer, serviceId, service }
|
||||||
*/
|
*/
|
||||||
const extractServiceArgs = (
|
const extractServiceArgs = async (
|
||||||
args: any[],
|
args: any[],
|
||||||
defaultServiceId?: string,
|
defaultServiceId?: string,
|
||||||
): { peer: FluencePeer; serviceId: string; service: any } => {
|
): Promise<{ peer: IFluencePeer; serviceId: string; service: any }> => {
|
||||||
let peer: FluencePeer;
|
let peer: IFluencePeer;
|
||||||
let serviceId: any;
|
let serviceId: any;
|
||||||
let service: any;
|
let service: any;
|
||||||
if (FluencePeer.isInstance(args[0])) {
|
if (isFluencePeer(args[0])) {
|
||||||
peer = args[0];
|
peer = args[0];
|
||||||
} else {
|
} else {
|
||||||
peer = getDefaultPeer();
|
peer = await getDefaultPeer();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof args[0] === 'string') {
|
if (typeof args[0] === 'string') {
|
||||||
@ -159,7 +160,7 @@ const extractServiceArgs = (
|
|||||||
// If the first argument is peer, we are checking further. The second argument might either be
|
// 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
|
// 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
|
// 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') {
|
if (!isFluencePeer(args[0]) && typeof args[0] === 'object') {
|
||||||
service = args[0];
|
service = args[0];
|
||||||
} else if (typeof args[1] === 'object') {
|
} else if (typeof args[1] === 'object') {
|
||||||
service = args[1];
|
service = args[1];
|
@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export { FluencePeer } from './v3';
|
export { IFluencePeer } from './v3';
|
||||||
export { CallParams as CallParams$$ } from './v3';
|
export { CallParams as CallParams$$ } from './v3';
|
||||||
export {
|
export {
|
||||||
ArrayType as ArrayType$$,
|
ArrayType as ArrayType$$,
|
21
packages/client/compiler-support/.gitignore
vendored
21
packages/client/compiler-support/.gitignore
vendored
@ -1,21 +0,0 @@
|
|||||||
# Logs
|
|
||||||
logs
|
|
||||||
*.log
|
|
||||||
npm-debug.log*
|
|
||||||
yarn-debug.log*
|
|
||||||
yarn-error.log*
|
|
||||||
lerna-debug.log*
|
|
||||||
|
|
||||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
|
||||||
build/Release
|
|
||||||
bundle/
|
|
||||||
|
|
||||||
dist
|
|
||||||
esm
|
|
||||||
types
|
|
||||||
|
|
||||||
# Dependency directories
|
|
||||||
node_modules/
|
|
||||||
jspm_packages/
|
|
||||||
|
|
||||||
.idea
|
|
@ -1 +0,0 @@
|
|||||||
/dist/
|
|
@ -1,11 +0,0 @@
|
|||||||
# JS Client compiler support
|
|
||||||
|
|
||||||
This package is a part of FluenceJS, the official implementation of the Fluence Peer in typescript. See the [FluenceJS repo](https://github.com/fluencelabs/fluence-js) for more info
|
|
||||||
|
|
||||||
## Contributing
|
|
||||||
|
|
||||||
While the project is still in the early stages of development, you are welcome to track progress and contribute. As the project is undergoing rapid changes, interested contributors should contact the team before embarking on larger pieces of work. All contributors should consult with and agree to our [basic contributing rules](CONTRIBUTING.md).
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
[Apache 2.0](LICENSE)
|
|
@ -1,26 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@fluencelabs/compiler-support",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"description": "TypeScript implementation of Fluence Peer",
|
|
||||||
"main": "./dist/index.js",
|
|
||||||
"typings": "./dist/index.d.ts",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=10",
|
|
||||||
"pnpm": ">=3"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"build": "tsc"
|
|
||||||
},
|
|
||||||
"repository": "https://github.com/fluencelabs/fluence-js",
|
|
||||||
"author": "Fluence Labs",
|
|
||||||
"license": "Apache-2.0",
|
|
||||||
"dependencies": {
|
|
||||||
"@fluencelabs/js-peer": "workspace:*"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@types/node": "16.11.59",
|
|
||||||
"@types/jest": "28.1.0",
|
|
||||||
"jest": "28.1.0",
|
|
||||||
"ts-jest": "28.0.2"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
import type { FluencePeer } from '@fluencelabs/js-peer/dist/FluencePeer';
|
|
||||||
|
|
||||||
export const getDefaultPeer = (): FluencePeer => {
|
|
||||||
// @ts-ignore
|
|
||||||
return globalThis.defaultPeer;
|
|
||||||
};
|
|
@ -1,7 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "../../../tsconfig.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"outDir": "./dist"
|
|
||||||
},
|
|
||||||
"exclude": ["node_modules", "dist"]
|
|
||||||
}
|
|
@ -23,11 +23,70 @@ export type PeerIdB58 = string;
|
|||||||
|
|
||||||
export type ParticleHandler = (particle: string) => void;
|
export type ParticleHandler = (particle: string) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 IFluencePeer {
|
export interface IFluencePeer {
|
||||||
start(config?: PeerConfig): Promise<void>;
|
start(config?: PeerConfig): Promise<void>;
|
||||||
stop(): Promise<void>;
|
stop(): Promise<void>;
|
||||||
|
getStatus(): PeerStatus;
|
||||||
|
|
||||||
|
// TODO: come up with a working interface for
|
||||||
|
// - particle creation
|
||||||
|
// - particle initialization
|
||||||
|
// - service registration
|
||||||
|
internals: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const asFluencePeer = (fluencePeerCandidate: unknown): IFluencePeer => {
|
||||||
|
if (isFluencePeer(fluencePeerCandidate)) {
|
||||||
|
return fluencePeerCandidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('');
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isFluencePeer = (fluencePeerCandidate: unknown): fluencePeerCandidate is IFluencePeer => {
|
||||||
|
if (fluencePeerCandidate && (fluencePeerCandidate as any).__isFluenceAwesome) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for connectivity layer to Fluence Network
|
* Base class for connectivity layer to Fluence Network
|
||||||
*/
|
*/
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
import 'buffer';
|
import 'buffer';
|
||||||
|
|
||||||
import { RelayConnection } from '../connection/index.js';
|
import { RelayConnection } from '../connection/index.js';
|
||||||
import { FluenceConnection, IAvmRunner, IFluencePeer, IMarine } from '../interfaces/index.js';
|
import { FluenceConnection, IAvmRunner, IFluencePeer, IMarine, PeerStatus } from '../interfaces/index.js';
|
||||||
import { KeyPair } from '../keypair/index.js';
|
import { KeyPair } from '../keypair/index.js';
|
||||||
import {
|
import {
|
||||||
CallServiceData,
|
CallServiceData,
|
||||||
@ -42,42 +42,6 @@ import { registerNodeUtils } from './_aqua/node-utils.js';
|
|||||||
import { ConnectionOption, PeerConfig } from '../interfaces/peerConfig.js';
|
import { ConnectionOption, PeerConfig } from '../interfaces/peerConfig.js';
|
||||||
import type { MultiaddrInput } from '@multiformats/multiaddr';
|
import type { MultiaddrInput } from '@multiformats/multiaddr';
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
};
|
|
||||||
|
|
||||||
const DEFAULT_TTL = 7000;
|
const DEFAULT_TTL = 7000;
|
||||||
|
|
||||||
export interface PeerConfig2 extends PeerConfig {
|
export interface PeerConfig2 extends PeerConfig {
|
||||||
@ -135,13 +99,12 @@ export class FluencePeer implements IFluencePeer {
|
|||||||
constructor(private marine: IMarine, private avmRunner: IAvmRunner) {}
|
constructor(private marine: IMarine, private avmRunner: IAvmRunner) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether the object is instance of FluencePeer class
|
* Internal contract to cast unknown objects to IFluencePeer.
|
||||||
* @param obj - object to check if it is FluencePeer
|
* If an unknown object has this property then we assume it is in fact a Peer and it implements IFluencePeer
|
||||||
* @returns true if the object is FluencePeer false otherwise
|
* Check against this variable MUST NOT be coupled with any `FluencePeer` because otherwise it might get bundled
|
||||||
|
* brining a lot of unnecessary stuff alongside with it
|
||||||
*/
|
*/
|
||||||
static isInstance(obj: unknown): obj is FluencePeer {
|
__isFluenceAwesome = true;
|
||||||
return obj instanceof FluencePeer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the peer's status
|
* Get the peer's status
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { ArrowWithoutCallbacks, FnConfig, FunctionCallDef, NonArrowType } from './interface.js';
|
import { ArrowWithoutCallbacks, FnConfig, FunctionCallDef, NonArrowType } from './interface.js';
|
||||||
import { FluencePeer } from '../FluencePeer.js';
|
import { IFluencePeer } from '../../interfaces/index';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
injectRelayService,
|
injectRelayService,
|
||||||
@ -26,7 +26,7 @@ export function callFunctionImpl(
|
|||||||
def: FunctionCallDef,
|
def: FunctionCallDef,
|
||||||
script: string,
|
script: string,
|
||||||
config: FnConfig,
|
config: FnConfig,
|
||||||
peer: FluencePeer,
|
peer: IFluencePeer,
|
||||||
args: { [key: string]: any },
|
args: { [key: string]: any },
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
const argumentTypes = getArgumentTypes(def);
|
const argumentTypes = getArgumentTypes(def);
|
||||||
@ -55,7 +55,7 @@ export function callFunctionImpl(
|
|||||||
|
|
||||||
registerParticleScopeService(peer, particle, errorHandlingService(def, reject));
|
registerParticleScopeService(peer, particle, errorHandlingService(def, reject));
|
||||||
|
|
||||||
peer.internals.initiateParticle(particle, (stage) => {
|
peer.internals.initiateParticle(particle, (stage: any) => {
|
||||||
// If function is void, then it's completed when one of the two conditions is met:
|
// 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')
|
// 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')
|
// 2. All CallRequests are executed, e.g., all variable loading and local function calls are completed (state 'localWorkDone')
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
import type { FluencePeer } from '../FluencePeer.js';
|
import type { IFluencePeer } from '../../interfaces/index';
|
||||||
import { ServiceDef } from './interface.js';
|
import { ServiceDef } from './interface.js';
|
||||||
import { registerGlobalService, userHandlerService } from './services.js';
|
import { registerGlobalService, userHandlerService } from './services.js';
|
||||||
|
|
||||||
export const registerServiceImpl = (
|
export const registerServiceImpl = (
|
||||||
peer: FluencePeer,
|
peer: IFluencePeer,
|
||||||
def: ServiceDef,
|
def: ServiceDef,
|
||||||
serviceId: string | undefined,
|
serviceId: string | undefined,
|
||||||
service: any,
|
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) {
|
if (!peer.getStatus().isInitialized) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Could not register the service because the peer is not initialized. Are you passing the wrong peer to the register function?',
|
'Could not register the service because the peer is not initialized. Are you passing the wrong peer to the register function?',
|
||||||
|
@ -3,7 +3,7 @@ import { match } from 'ts-pattern';
|
|||||||
|
|
||||||
import { Particle } from '../Particle.js';
|
import { Particle } from '../Particle.js';
|
||||||
import { CallParams, CallServiceData, GenericCallServiceHandler, ResultCodes } from '../../interfaces/commonTypes.js';
|
import { CallParams, CallServiceData, GenericCallServiceHandler, ResultCodes } from '../../interfaces/commonTypes.js';
|
||||||
import { FluencePeer } from '../FluencePeer.js';
|
import { IFluencePeer } from '../../interfaces/index';
|
||||||
|
|
||||||
import { aquaArgs2Ts, responseServiceValue2ts, returnType2Aqua, ts2aqua } from './conversions.js';
|
import { aquaArgs2Ts, responseServiceValue2ts, returnType2Aqua, ts2aqua } from './conversions.js';
|
||||||
import { ArrowWithoutCallbacks, FunctionCallConstants, FunctionCallDef, NonArrowType } from './interface.js';
|
import { ArrowWithoutCallbacks, FunctionCallConstants, FunctionCallDef, NonArrowType } from './interface.js';
|
||||||
@ -17,7 +17,7 @@ export interface ServiceDescription {
|
|||||||
/**
|
/**
|
||||||
* Creates a service which injects relay's peer id into aqua space
|
* Creates a service which injects relay's peer id into aqua space
|
||||||
*/
|
*/
|
||||||
export const injectRelayService = (def: FunctionCallDef, peer: FluencePeer) => {
|
export const injectRelayService = (def: FunctionCallDef, peer: IFluencePeer) => {
|
||||||
return {
|
return {
|
||||||
serviceId: def.names.getDataSrv,
|
serviceId: def.names.getDataSrv,
|
||||||
fnName: def.names.relay,
|
fnName: def.names.relay,
|
||||||
@ -162,10 +162,10 @@ const extractCallParams = (req: CallServiceData, arrow: ArrowWithoutCallbacks):
|
|||||||
return callParams;
|
return callParams;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const registerParticleScopeService = (peer: FluencePeer, particle: Particle, service: ServiceDescription) => {
|
export const registerParticleScopeService = (peer: IFluencePeer, particle: Particle, service: ServiceDescription) => {
|
||||||
peer.internals.regHandler.forParticle(particle.id, service.serviceId, service.fnName, service.handler);
|
peer.internals.regHandler.forParticle(particle.id, service.serviceId, service.fnName, service.handler);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const registerGlobalService = (peer: FluencePeer, service: ServiceDescription) => {
|
export const registerGlobalService = (peer: IFluencePeer, service: ServiceDescription) => {
|
||||||
peer.internals.regHandler.common(service.serviceId, service.fnName, service.handler);
|
peer.internals.regHandler.common(service.serviceId, service.fnName, service.handler);
|
||||||
};
|
};
|
||||||
|
5289
pnpm-lock.yaml
generated
5289
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user