2023-10-17 22:14:08 +07:00
|
|
|
/**
|
2023-04-03 21:52:40 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-10-17 22:14:08 +07:00
|
|
|
import { promises as fs } from "fs";
|
|
|
|
|
|
|
|
import * as api from "@fluencelabs/aqua-api/aqua-api.js";
|
|
|
|
import {
|
|
|
|
FunctionCallDef,
|
|
|
|
JSONArray,
|
|
|
|
PassedArgs,
|
|
|
|
ServiceDef,
|
|
|
|
} from "@fluencelabs/interfaces";
|
|
|
|
import { Subject, Subscribable } from "rxjs";
|
|
|
|
|
|
|
|
import { ClientPeer, makeClientPeerConfig } from "../clientPeer/ClientPeer.js";
|
2023-10-18 08:38:49 +07:00
|
|
|
import { ClientConfig, RelayOptions } from "../clientPeer/types.js";
|
2023-10-17 22:14:08 +07:00
|
|
|
import { callAquaFunction } from "../compilerSupport/callFunction.js";
|
|
|
|
import { IConnection } from "../connection/interfaces.js";
|
|
|
|
import { DEFAULT_CONFIG, FluencePeer } from "../jsPeer/FluencePeer.js";
|
|
|
|
import { CallServiceResultType } from "../jsServiceHost/interfaces.js";
|
|
|
|
import { JsServiceHost } from "../jsServiceHost/JsServiceHost.js";
|
|
|
|
import { WrapFnIntoServiceCall } from "../jsServiceHost/serviceUtils.js";
|
|
|
|
import { KeyPair } from "../keypair/index.js";
|
|
|
|
import { WasmLoaderFromNpm } from "../marine/deps-loader/node.js";
|
|
|
|
import { MarineBackgroundRunner } from "../marine/worker/index.js";
|
|
|
|
import { WorkerLoader } from "../marine/worker-script/workerLoader.js";
|
|
|
|
import { Particle } from "../particle/Particle.js";
|
2023-04-03 21:52:40 +04:00
|
|
|
|
|
|
|
export const registerHandlersHelper = (
|
2023-10-17 22:14:08 +07:00
|
|
|
peer: FluencePeer,
|
|
|
|
particle: Particle,
|
|
|
|
handlers: Record<
|
|
|
|
string,
|
|
|
|
Record<string, (args: JSONArray) => CallServiceResultType | undefined>
|
|
|
|
>,
|
2023-04-03 21:52:40 +04:00
|
|
|
) => {
|
2023-10-17 22:14:08 +07:00
|
|
|
Object.entries(handlers).forEach(([serviceId, service]) => {
|
|
|
|
Object.entries(service).forEach(([fnName, fn]) => {
|
|
|
|
peer.internals.regHandler.forParticle(
|
|
|
|
particle.id,
|
|
|
|
serviceId,
|
|
|
|
fnName,
|
|
|
|
WrapFnIntoServiceCall(fn),
|
|
|
|
);
|
2023-04-03 21:52:40 +04:00
|
|
|
});
|
2023-10-17 22:14:08 +07:00
|
|
|
});
|
2023-04-03 21:52:40 +04:00
|
|
|
};
|
|
|
|
|
2023-10-17 22:14:08 +07:00
|
|
|
export type CompiledFnCall = (
|
|
|
|
peer: FluencePeer,
|
|
|
|
args: PassedArgs,
|
|
|
|
) => Promise<unknown>;
|
2023-04-03 21:52:40 +04:00
|
|
|
export type CompiledFile = {
|
2023-10-17 22:14:08 +07:00
|
|
|
functions: { [key: string]: CompiledFnCall };
|
|
|
|
services: { [key: string]: ServiceDef };
|
2023-04-03 21:52:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const compileAqua = async (aquaFile: string): Promise<CompiledFile> => {
|
2023-10-17 22:14:08 +07:00
|
|
|
await fs.access(aquaFile);
|
|
|
|
|
|
|
|
const compilationResult = await api.Aqua.compile(
|
|
|
|
new api.Path(aquaFile),
|
|
|
|
[],
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (compilationResult.errors.length > 0) {
|
|
|
|
throw new Error(
|
|
|
|
"Aqua compilation failed. Error: " + compilationResult.errors.join("/n"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const functions = Object.entries(compilationResult.functions)
|
|
|
|
.map(([name, fnInfo]) => {
|
|
|
|
const callFn = (peer: FluencePeer, args: PassedArgs) => {
|
|
|
|
return callAquaFunction({
|
|
|
|
// TODO: Set our compiler here and fix this
|
|
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
|
|
def: fnInfo.funcDef as FunctionCallDef,
|
|
|
|
script: fnInfo.script,
|
|
|
|
config: {},
|
|
|
|
peer: peer,
|
|
|
|
args,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return { [name]: callFn };
|
|
|
|
})
|
|
|
|
.reduce((agg, obj) => {
|
|
|
|
return { ...agg, ...obj };
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
return {
|
|
|
|
functions,
|
|
|
|
// TODO: set our compiler here and fix this
|
|
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
|
|
services: compilationResult.services as Record<string, ServiceDef>,
|
|
|
|
};
|
2023-04-03 21:52:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class NoopConnection implements IConnection {
|
2023-10-17 22:14:08 +07:00
|
|
|
start(): Promise<void> {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
stop(): Promise<void> {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
getRelayPeerId(): string {
|
|
|
|
return "nothing_here";
|
|
|
|
}
|
|
|
|
supportsRelay(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
particleSource: Subscribable<Particle> = new Subject<Particle>();
|
|
|
|
|
|
|
|
sendParticle(): Promise<void> {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2023-04-03 21:52:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
export class TestPeer extends FluencePeer {
|
2023-10-17 22:14:08 +07:00
|
|
|
constructor(keyPair: KeyPair, connection: IConnection) {
|
|
|
|
const workerLoader = new WorkerLoader();
|
|
|
|
|
|
|
|
const controlModuleLoader = new WasmLoaderFromNpm(
|
|
|
|
"@fluencelabs/marine-js",
|
|
|
|
"marine-js.wasm",
|
|
|
|
);
|
|
|
|
|
|
|
|
const avmModuleLoader = new WasmLoaderFromNpm(
|
|
|
|
"@fluencelabs/avm",
|
|
|
|
"avm.wasm",
|
|
|
|
);
|
|
|
|
|
|
|
|
const marine = new MarineBackgroundRunner(
|
|
|
|
workerLoader,
|
|
|
|
controlModuleLoader,
|
|
|
|
avmModuleLoader,
|
|
|
|
);
|
|
|
|
|
|
|
|
const jsHost = new JsServiceHost();
|
|
|
|
super(DEFAULT_CONFIG, keyPair, marine, jsHost, connection);
|
|
|
|
}
|
2023-04-03 21:52:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
export const mkTestPeer = async () => {
|
2023-10-17 22:14:08 +07:00
|
|
|
const kp = await KeyPair.randomEd25519();
|
|
|
|
const conn = new NoopConnection();
|
|
|
|
return new TestPeer(kp, conn);
|
2023-04-03 21:52:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const withPeer = async (action: (p: FluencePeer) => Promise<void>) => {
|
2023-10-17 22:14:08 +07:00
|
|
|
const p = await mkTestPeer();
|
|
|
|
|
|
|
|
try {
|
|
|
|
await p.start();
|
|
|
|
await action(p);
|
|
|
|
} finally {
|
|
|
|
await p.stop();
|
|
|
|
}
|
2023-04-03 21:52:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const withClient = async (
|
2023-10-17 22:14:08 +07:00
|
|
|
relay: RelayOptions,
|
|
|
|
config: ClientConfig,
|
|
|
|
action: (client: ClientPeer) => Promise<void>,
|
2023-04-03 21:52:40 +04:00
|
|
|
) => {
|
2023-10-17 22:14:08 +07:00
|
|
|
const workerLoader = new WorkerLoader();
|
|
|
|
|
|
|
|
const controlModuleLoader = new WasmLoaderFromNpm(
|
|
|
|
"@fluencelabs/marine-js",
|
|
|
|
"marine-js.wasm",
|
|
|
|
);
|
|
|
|
|
|
|
|
const avmModuleLoader = new WasmLoaderFromNpm("@fluencelabs/avm", "avm.wasm");
|
|
|
|
|
|
|
|
const marine = new MarineBackgroundRunner(
|
|
|
|
workerLoader,
|
|
|
|
controlModuleLoader,
|
|
|
|
avmModuleLoader,
|
|
|
|
);
|
|
|
|
|
|
|
|
const { keyPair, peerConfig, relayConfig } = await makeClientPeerConfig(
|
|
|
|
relay,
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
const client = new ClientPeer(peerConfig, relayConfig, keyPair, marine);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await client.connect();
|
|
|
|
await action(client);
|
|
|
|
} finally {
|
|
|
|
await client.disconnect();
|
|
|
|
}
|
2023-04-03 21:52:40 +04:00
|
|
|
};
|