mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-27 06:41:32 +00:00
feat: Cleaning up technical debts (#295)
This commit is contained in:
@ -29,13 +29,12 @@ import { getFluenceInterface } from '../util.js';
|
||||
* @param def - function definition generated by the Aqua compiler
|
||||
* @param script - air script with function execution logic generated by the Aqua compiler
|
||||
*/
|
||||
export const callFunction = async (rawFnArgs: Array<any>, def: FunctionCallDef, script: string): Promise<unknown> => {
|
||||
export const v5_callFunction = async (
|
||||
rawFnArgs: Array<any>,
|
||||
def: FunctionCallDef,
|
||||
script: string,
|
||||
): Promise<unknown> => {
|
||||
const { args, client: peer, config } = await extractFunctionArgs(rawFnArgs, def);
|
||||
if (peer.internals.getConnectionState() !== 'connected') {
|
||||
throw new Error(
|
||||
'Could not call the Aqua function because client is disconnected. Did you forget to call Fluence.connect()?',
|
||||
);
|
||||
}
|
||||
|
||||
const fluence = await getFluenceInterface();
|
||||
return fluence.callAquaFunction({
|
||||
@ -53,17 +52,9 @@ export const callFunction = async (rawFnArgs: Array<any>, def: FunctionCallDef,
|
||||
* @param args - raw arguments passed by user to the generated function
|
||||
* @param def - service definition generated by the Aqua compiler
|
||||
*/
|
||||
export const registerService = async (args: any[], def: ServiceDef): Promise<unknown> => {
|
||||
export const v5_registerService = async (args: any[], def: ServiceDef): Promise<unknown> => {
|
||||
const { peer, service, serviceId } = await extractServiceArgs(args, def.defaultServiceId);
|
||||
|
||||
// TODO: TBH service registration is just putting some stuff into a hashmap
|
||||
// there should not be such a check at all
|
||||
if (peer.internals.getConnectionState() !== 'connected') {
|
||||
throw new Error(
|
||||
'Could not register Aqua service because the client is disconnected. Did you forget to call Fluence.connect()?',
|
||||
);
|
||||
}
|
||||
|
||||
const fluence = await getFluenceInterface();
|
||||
return fluence.registerService({
|
||||
def,
|
||||
@ -104,6 +95,11 @@ const extractFunctionArgs = async (
|
||||
config = args[numberOfExpectedArgs + 1];
|
||||
} else {
|
||||
const fluence = await getFluenceInterface();
|
||||
if (!fluence.defaultClient) {
|
||||
throw new Error(
|
||||
'Could not register Aqua service because the client is not initialized. Did you forget to call Fluence.connect()?',
|
||||
);
|
||||
}
|
||||
peer = fluence.defaultClient;
|
||||
structuredArgs = args.slice(0, numberOfExpectedArgs);
|
||||
config = args[numberOfExpectedArgs];
|
||||
@ -145,6 +141,11 @@ const extractServiceArgs = async (
|
||||
peer = args[0];
|
||||
} else {
|
||||
const fluence = await getFluenceInterface();
|
||||
if (!fluence.defaultClient) {
|
||||
throw new Error(
|
||||
'Could not register Aqua service because the client is not initialized. Did you forget to call Fluence.connect()?',
|
||||
);
|
||||
}
|
||||
peer = fluence.defaultClient;
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,28 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { getFluenceInterface, getFluenceInterfaceFromGlobalThis } from './util.js';
|
||||
import {
|
||||
IFluenceClient,
|
||||
ClientOptions,
|
||||
ClientConfig,
|
||||
RelayOptions,
|
||||
ConnectionState,
|
||||
ConnectionStates,
|
||||
CallAquaFunctionType,
|
||||
RegisterServiceType,
|
||||
} from '@fluencelabs/interfaces';
|
||||
export type { IFluenceClient, ClientOptions, CallParams } from '@fluencelabs/interfaces';
|
||||
export type { IFluenceClient, ClientConfig, CallParams } from '@fluencelabs/interfaces';
|
||||
|
||||
export {
|
||||
ArrayType,
|
||||
@ -14,7 +30,6 @@ export {
|
||||
ArrowWithCallbacks,
|
||||
ArrowWithoutCallbacks,
|
||||
BottomType,
|
||||
FnConfig,
|
||||
FunctionCallConstants,
|
||||
FunctionCallDef,
|
||||
LabeledProductType,
|
||||
@ -28,12 +43,15 @@ export {
|
||||
StructType,
|
||||
TopType,
|
||||
UnlabeledProductType,
|
||||
CallAquaFunctionType,
|
||||
CallAquaFunctionArgs,
|
||||
PassedArgs,
|
||||
FnConfig,
|
||||
RegisterServiceType,
|
||||
RegisterServiceArgs,
|
||||
} from '@fluencelabs/interfaces';
|
||||
|
||||
export {
|
||||
callFunction as v5_callFunction,
|
||||
registerService as v5_registerService,
|
||||
} from './compilerSupport/implementation.js';
|
||||
export { v5_callFunction, v5_registerService } from './compilerSupport/implementation.js';
|
||||
|
||||
/**
|
||||
* Public interface to Fluence Network
|
||||
@ -42,11 +60,12 @@ export const Fluence = {
|
||||
/**
|
||||
* Connect to the Fluence network
|
||||
* @param relay - relay node to connect to
|
||||
* @param options - client options
|
||||
* @param config - client configuration
|
||||
*/
|
||||
connect: async (relay: RelayOptions, options?: ClientOptions): Promise<void> => {
|
||||
connect: async (relay: RelayOptions, config?: ClientConfig): Promise<void> => {
|
||||
const fluence = await getFluenceInterface();
|
||||
return fluence.defaultClient.connect(relay, options);
|
||||
const client = await fluence.clientFactory(relay, config);
|
||||
fluence.defaultClient = client;
|
||||
},
|
||||
|
||||
/**
|
||||
@ -54,7 +73,8 @@ export const Fluence = {
|
||||
*/
|
||||
disconnect: async (): Promise<void> => {
|
||||
const fluence = await getFluenceInterface();
|
||||
return fluence.defaultClient.disconnect();
|
||||
await fluence.defaultClient?.disconnect();
|
||||
fluence.defaultClient = undefined;
|
||||
},
|
||||
|
||||
/**
|
||||
@ -62,11 +82,13 @@ export const Fluence = {
|
||||
*/
|
||||
onConnectionStateChange(handler: (state: ConnectionState) => void): ConnectionState {
|
||||
const optimisticResult = getFluenceInterfaceFromGlobalThis();
|
||||
if (optimisticResult) {
|
||||
if (optimisticResult && optimisticResult.defaultClient) {
|
||||
return optimisticResult.defaultClient.onConnectionStateChange(handler);
|
||||
}
|
||||
|
||||
getFluenceInterface().then((fluence) => fluence.defaultClient.onConnectionStateChange(handler));
|
||||
getFluenceInterface().then((fluence) => {
|
||||
fluence.defaultClient?.onConnectionStateChange(handler);
|
||||
});
|
||||
|
||||
return 'disconnected';
|
||||
},
|
||||
@ -77,6 +99,9 @@ export const Fluence = {
|
||||
*/
|
||||
getClient: async (): Promise<IFluenceClient> => {
|
||||
const fluence = await getFluenceInterface();
|
||||
if (!fluence.defaultClient) {
|
||||
throw new Error('Fluence client is not initialized. Call Fluence.connect() first');
|
||||
}
|
||||
return fluence.defaultClient;
|
||||
},
|
||||
};
|
||||
@ -85,7 +110,23 @@ export const Fluence = {
|
||||
* Low level API. Generally you need Fluence.connect() instead.
|
||||
* @returns IFluenceClient instance
|
||||
*/
|
||||
export const createClient = async (): Promise<IFluenceClient> => {
|
||||
export const createClient = async (relay: RelayOptions, config?: ClientConfig): Promise<IFluenceClient> => {
|
||||
const fluence = await getFluenceInterface();
|
||||
return fluence.clientFactory();
|
||||
return await fluence.clientFactory(relay, config);
|
||||
};
|
||||
|
||||
/**
|
||||
* Low level API. Generally you should use code generated by the Aqua compiler.
|
||||
*/
|
||||
export const callAquaFunction: CallAquaFunctionType = async (args) => {
|
||||
const fluence = await getFluenceInterface();
|
||||
return await fluence.callAquaFunction(args);
|
||||
};
|
||||
|
||||
/**
|
||||
* Low level API. Generally you should use code generated by the Aqua compiler.
|
||||
*/
|
||||
export const registerService: RegisterServiceType = async (args) => {
|
||||
const fluence = await getFluenceInterface();
|
||||
return await fluence.registerService(args);
|
||||
};
|
||||
|
@ -1,10 +1,31 @@
|
||||
import type { CallAquaFunction, IFluenceClient, RegisterService } from '@fluencelabs/interfaces';
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import type {
|
||||
CallAquaFunctionType,
|
||||
ClientConfig,
|
||||
IFluenceClient,
|
||||
RegisterServiceType,
|
||||
RelayOptions,
|
||||
} from '@fluencelabs/interfaces';
|
||||
|
||||
type PublicFluenceInterface = {
|
||||
clientFactory: () => IFluenceClient;
|
||||
defaultClient: IFluenceClient;
|
||||
callAquaFunction: CallAquaFunction;
|
||||
registerService: RegisterService;
|
||||
defaultClient: IFluenceClient | undefined;
|
||||
clientFactory: (relay: RelayOptions, config?: ClientConfig) => Promise<IFluenceClient>;
|
||||
callAquaFunction: CallAquaFunctionType;
|
||||
registerService: RegisterServiceType;
|
||||
};
|
||||
|
||||
export const getFluenceInterfaceFromGlobalThis = (): PublicFluenceInterface | undefined => {
|
||||
|
@ -1,33 +1,34 @@
|
||||
{
|
||||
"name": "@fluencelabs/js-client.node",
|
||||
"version": "0.6.7",
|
||||
"description": "TypeScript implementation of Fluence Peer",
|
||||
"main": "./dist/index.js",
|
||||
"typings": "./dist/index.d.ts",
|
||||
"engines": {
|
||||
"node": ">=10",
|
||||
"pnpm": ">=3"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts"
|
||||
"name": "@fluencelabs/js-client.node",
|
||||
"version": "0.6.7",
|
||||
"description": "TypeScript implementation of Fluence Peer",
|
||||
"main": "./dist/index.js",
|
||||
"typings": "./dist/index.d.ts",
|
||||
"engines": {
|
||||
"node": ">=10",
|
||||
"pnpm": ">=3"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts"
|
||||
}
|
||||
},
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "tsc"
|
||||
},
|
||||
"repository": "https://github.com/fluencelabs/fluence-js",
|
||||
"author": "Fluence Labs",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@fluencelabs/js-peer": "0.8.6",
|
||||
"@fluencelabs/interfaces": "0.7.4",
|
||||
"@fluencelabs/avm": "0.35.4",
|
||||
"@fluencelabs/marine-js": "0.3.45",
|
||||
"platform": "1.3.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/platform": "1.3.4"
|
||||
}
|
||||
},
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "tsc"
|
||||
},
|
||||
"repository": "https://github.com/fluencelabs/fluence-js",
|
||||
"author": "Fluence Labs",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@fluencelabs/js-peer": "0.8.6",
|
||||
"@fluencelabs/avm": "0.35.4",
|
||||
"@fluencelabs/marine-js": "0.3.45",
|
||||
"platform": "1.3.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/platform": "1.3.4"
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,29 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import * as platform from 'platform';
|
||||
|
||||
import { FluencePeer } from '@fluencelabs/js-peer/dist/js-peer/FluencePeer.js';
|
||||
import type { RelayOptions, ClientConfig, IFluenceClient } from '@fluencelabs/interfaces';
|
||||
import { ClientPeer, makeClientPeerConfig } from '@fluencelabs/js-peer/dist/clientPeer/ClientPeer.js';
|
||||
import { callAquaFunction } from '@fluencelabs/js-peer/dist/compilerSupport/callFunction.js';
|
||||
import { registerService } from '@fluencelabs/js-peer/dist/compilerSupport/registerService.js';
|
||||
import { MarineBasedAvmRunner } from '@fluencelabs/js-peer/dist/js-peer/avm.js';
|
||||
import { MarineBasedAvmRunner } from '@fluencelabs/js-peer/dist/jsPeer/avm.js';
|
||||
import { MarineBackgroundRunner } from '@fluencelabs/js-peer/dist/marine/worker/index.js';
|
||||
import { WasmLoaderFromNpm } from '@fluencelabs/js-peer/dist/marine/deps-loader/node.js';
|
||||
import { WorkerLoader } from '@fluencelabs/js-peer/dist/marine/worker-script/workerLoader.js';
|
||||
import { doRegisterNodeUtils } from '@fluencelabs/js-peer/dist/services/NodeUtils.js';
|
||||
|
||||
throwIfNotSupported();
|
||||
|
||||
@ -21,19 +38,26 @@ export const defaultNames = {
|
||||
},
|
||||
};
|
||||
|
||||
export const createClient = () => {
|
||||
const createClient = async (relay: RelayOptions, config: ClientConfig): Promise<IFluenceClient> => {
|
||||
const workerLoader = new WorkerLoader();
|
||||
const controlModuleLoader = new WasmLoaderFromNpm(defaultNames.marine.package, defaultNames.marine.file);
|
||||
const avmModuleLoader = new WasmLoaderFromNpm(defaultNames.avm.package, defaultNames.avm.file);
|
||||
|
||||
const marine = new MarineBackgroundRunner(workerLoader, controlModuleLoader);
|
||||
const avm = new MarineBasedAvmRunner(marine, avmModuleLoader);
|
||||
return new FluencePeer(marine, avm);
|
||||
const { keyPair, peerConfig, relayConfig } = await makeClientPeerConfig(relay, config);
|
||||
const client: IFluenceClient = new ClientPeer(peerConfig, relayConfig, keyPair, marine, avm);
|
||||
registerNodeOnlyServices(client);
|
||||
await client.connect();
|
||||
return client;
|
||||
};
|
||||
|
||||
function registerNodeOnlyServices(client: IFluenceClient) {
|
||||
doRegisterNodeUtils(client);
|
||||
}
|
||||
|
||||
const publicFluenceInterface = {
|
||||
clientFactory: createClient,
|
||||
defaultClient: createClient(),
|
||||
callAquaFunction,
|
||||
registerService,
|
||||
};
|
||||
|
@ -1,36 +1,37 @@
|
||||
{
|
||||
"name": "@fluencelabs/js-client.web.standalone",
|
||||
"version": "0.13.6",
|
||||
"description": "TypeScript implementation of Fluence Peer",
|
||||
"main": "./dist/index.js",
|
||||
"typings": "./dist/index.d.ts",
|
||||
"engines": {
|
||||
"node": ">=10",
|
||||
"pnpm": ">=3"
|
||||
},
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "node --loader ts-node/esm ./build.ts"
|
||||
},
|
||||
"repository": "https://github.com/fluencelabs/fluence-js",
|
||||
"author": "Fluence Labs",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@fluencelabs/js-peer": "0.8.6",
|
||||
"buffer": "6.0.3",
|
||||
"process": "0.11.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@fluencelabs/avm": "0.35.4",
|
||||
"@fluencelabs/marine-js": "0.3.45",
|
||||
"@types/node": "16.11.59",
|
||||
"@types/jest": "28.1.0",
|
||||
"jest": "28.1.0",
|
||||
"ts-jest": "28.0.2",
|
||||
"js-base64": "3.7.5",
|
||||
"@rollup/plugin-inject": "5.0.3",
|
||||
"vite-plugin-replace": "0.1.1",
|
||||
"vite": "4.0.4",
|
||||
"vite-tsconfig-paths": "4.0.3"
|
||||
}
|
||||
"name": "@fluencelabs/js-client.web.standalone",
|
||||
"version": "0.13.6",
|
||||
"description": "TypeScript implementation of Fluence Peer",
|
||||
"main": "./dist/index.js",
|
||||
"typings": "./dist/index.d.ts",
|
||||
"engines": {
|
||||
"node": ">=10",
|
||||
"pnpm": ">=3"
|
||||
},
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "node --loader ts-node/esm ./build.ts"
|
||||
},
|
||||
"repository": "https://github.com/fluencelabs/fluence-js",
|
||||
"author": "Fluence Labs",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@fluencelabs/js-peer": "0.8.6",
|
||||
"@fluencelabs/interfaces": "0.7.4",
|
||||
"buffer": "6.0.3",
|
||||
"process": "0.11.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@fluencelabs/avm": "0.35.4",
|
||||
"@fluencelabs/marine-js": "0.3.45",
|
||||
"@types/node": "16.11.59",
|
||||
"@types/jest": "28.1.0",
|
||||
"jest": "28.1.0",
|
||||
"ts-jest": "28.0.2",
|
||||
"js-base64": "3.7.5",
|
||||
"@rollup/plugin-inject": "5.0.3",
|
||||
"vite-plugin-replace": "0.1.1",
|
||||
"vite": "4.0.4",
|
||||
"vite-tsconfig-paths": "4.0.3"
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +1,41 @@
|
||||
import { FluencePeer } from '@fluencelabs/js-peer/dist/js-peer/FluencePeer.js';
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import type { RelayOptions, ClientConfig, IFluenceClient } from '@fluencelabs/interfaces';
|
||||
import { ClientPeer, makeClientPeerConfig } from '@fluencelabs/js-peer/dist/clientPeer/ClientPeer.js';
|
||||
import { callAquaFunction } from '@fluencelabs/js-peer/dist/compilerSupport/callFunction.js';
|
||||
import { registerService } from '@fluencelabs/js-peer/dist/compilerSupport/registerService.js';
|
||||
import { MarineBasedAvmRunner } from '@fluencelabs/js-peer/dist/js-peer/avm.js';
|
||||
import { MarineBasedAvmRunner } from '@fluencelabs/js-peer/dist/jsPeer/avm.js';
|
||||
import { MarineBackgroundRunner } from '@fluencelabs/js-peer/dist/marine/worker';
|
||||
import { InlinedWorkerLoader, InlinedWasmLoader } from '@fluencelabs/js-peer/dist/marine/deps-loader/common.js';
|
||||
|
||||
const createClient = () => {
|
||||
const createClient = async (relay: RelayOptions, config: ClientConfig): Promise<IFluenceClient> => {
|
||||
const workerLoader = new InlinedWorkerLoader('___worker___');
|
||||
const controlModuleLoader = new InlinedWasmLoader('___marine___');
|
||||
const avmModuleLoader = new InlinedWasmLoader('___avm___');
|
||||
|
||||
const marine = new MarineBackgroundRunner(workerLoader, controlModuleLoader);
|
||||
const avm = new MarineBasedAvmRunner(marine, avmModuleLoader);
|
||||
return new FluencePeer(marine, avm);
|
||||
const { keyPair, peerConfig, relayConfig } = await makeClientPeerConfig(relay, config);
|
||||
const client: IFluenceClient = new ClientPeer(peerConfig, relayConfig, keyPair, marine, avm);
|
||||
await client.connect();
|
||||
return client;
|
||||
};
|
||||
|
||||
const publicFluenceInterface = {
|
||||
clientFactory: createClient,
|
||||
defaultClient: createClient(),
|
||||
callAquaFunction,
|
||||
registerService,
|
||||
};
|
||||
|
@ -8,6 +8,7 @@
|
||||
"node": ">=10",
|
||||
"pnpm": ">=3"
|
||||
},
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "tsc"
|
||||
},
|
||||
@ -15,7 +16,8 @@
|
||||
"author": "Fluence Labs",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@fluencelabs/js-peer": "0.7.0"
|
||||
"@fluencelabs/js-peer": "0.8.6",
|
||||
"@fluencelabs/interfaces": "0.7.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "16.11.59",
|
@ -1,23 +1,50 @@
|
||||
import { MarineBackgroundRunner } from '@fluencelabs/marine.background-runner';
|
||||
import { MarineBasedAvmRunner } from '@fluencelabs/js-peer/dist/avm';
|
||||
import { marineLogFunction } from '@fluencelabs/js-peer/dist/utils';
|
||||
import { FluencePeer } from '@fluencelabs/js-peer/dist/FluencePeer';
|
||||
import { InlinedWorkerLoader, WasmWebLoader } from '@fluencelabs/marine.deps-loader.web';
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import type { RelayOptions, ClientConfig, IFluenceClient } from '@fluencelabs/interfaces';
|
||||
import { ClientPeer, makeClientPeerConfig } from '@fluencelabs/js-peer/dist/clientPeer/ClientPeer.js';
|
||||
import { callAquaFunction } from '@fluencelabs/js-peer/dist/compilerSupport/callFunction.js';
|
||||
import { registerService } from '@fluencelabs/js-peer/dist/compilerSupport/registerService.js';
|
||||
import { MarineBasedAvmRunner } from '@fluencelabs/js-peer/dist/jsPeer/avm.js';
|
||||
import { MarineBackgroundRunner } from '@fluencelabs/js-peer/dist/marine/worker';
|
||||
import { WasmLoaderFromUrl, WorkerLoaderFromUrl } from '@fluencelabs/js-peer/dist/marine/deps-loader/web.js';
|
||||
|
||||
export const defaultNames = {
|
||||
avm: 'avm.wasm',
|
||||
const defaultNames = {
|
||||
marine: 'marine-js.wasm',
|
||||
avm: 'avm.wasm',
|
||||
worker: 'worker-script.js',
|
||||
};
|
||||
|
||||
export const makeDefaultPeer = () => {
|
||||
const workerLoader = new InlinedWorkerLoader();
|
||||
const controlModuleLoader = new WasmWebLoader(defaultNames.marine);
|
||||
const avmModuleLoader = new WasmWebLoader(defaultNames.avm);
|
||||
const createClient = async (relay: RelayOptions, config: ClientConfig): Promise<IFluenceClient> => {
|
||||
const workerLoader = new WorkerLoaderFromUrl(defaultNames.worker);
|
||||
const controlModuleLoader = new WasmLoaderFromUrl(defaultNames.marine);
|
||||
const avmModuleLoader = new WasmLoaderFromUrl(defaultNames.avm);
|
||||
|
||||
const marine = new MarineBackgroundRunner(workerLoader, controlModuleLoader, marineLogFunction);
|
||||
const avm = new MarineBasedAvmRunner(marine, avmModuleLoader, undefined);
|
||||
return new FluencePeer(marine, avm);
|
||||
const marine = new MarineBackgroundRunner(workerLoader, controlModuleLoader);
|
||||
const avm = new MarineBasedAvmRunner(marine, avmModuleLoader);
|
||||
const { keyPair, peerConfig, relayConfig } = await makeClientPeerConfig(relay, config);
|
||||
const client: IFluenceClient = new ClientPeer(peerConfig, relayConfig, keyPair, marine, avm);
|
||||
await client.connect();
|
||||
return client;
|
||||
};
|
||||
|
||||
const publicFluenceInterface = {
|
||||
clientFactory: createClient,
|
||||
callAquaFunction,
|
||||
registerService,
|
||||
};
|
||||
|
||||
// @ts-ignore
|
||||
globalThis.defaultPeer = makeDefaultPeer();
|
||||
globalThis.fluence = publicFluenceInterface;
|
||||
|
Reference in New Issue
Block a user