mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-18 18:41:21 +00:00
feat(js-client)!: Adding strictes eslint and ts config to all packages [fixes DXJ-464] (#355)
* introduce eslint * Fix all eslint errors * Eslint fix and some touches * Fix tests * Fix misc errors * change semver * change semver #2 * Fix path * Fix path #2 * freeze lock file in CI * fix package install * Fix formatting of surrounding files * Add empty prettier config * Fix formatting * Fix build errors * Remove unused deps * remove changelog from formatting * deps cleanup * make resource importers async * Refactor * Fix error message * remove comment * more refactoring * Update packages/core/js-client/src/compilerSupport/registerService.ts Co-authored-by: shamsartem <shamsartem@gmail.com> * refactoring * refactoring fix * optimize import * Update packages/@tests/smoke/node/src/index.ts Co-authored-by: shamsartem <shamsartem@gmail.com> * Revert package * Fix pnpm lock * Lint-fix * Fix CI * Update tests * Fix build * Fix import * Use forked threads dep * Use fixed version * Update threads * Fix lint * Fix test * Fix test * Add polyfill for assert * Add subpath import * Fix tests * Fix deps --------- Co-authored-by: shamsartem <shamsartem@gmail.com>
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
/*
|
||||
/**
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@ -14,135 +14,199 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import * as api from '@fluencelabs/aqua-api/aqua-api.js';
|
||||
import { promises as fs } from "fs";
|
||||
|
||||
import { promises as fs } from 'fs';
|
||||
import { DEFAULT_CONFIG, FluencePeer, PeerConfig } from '../jsPeer/FluencePeer.js';
|
||||
import { Particle } from '../particle/Particle.js';
|
||||
import { ClientConfig, IFluenceClient, RelayOptions, ServiceDef } from '@fluencelabs/interfaces';
|
||||
import { callAquaFunction } from '../compilerSupport/callFunction.js';
|
||||
import * as api from "@fluencelabs/aqua-api/aqua-api.js";
|
||||
import {
|
||||
ClientConfig,
|
||||
FunctionCallDef,
|
||||
JSONArray,
|
||||
PassedArgs,
|
||||
RelayOptions,
|
||||
ServiceDef,
|
||||
} from "@fluencelabs/interfaces";
|
||||
import { Subject, Subscribable } from "rxjs";
|
||||
|
||||
import { MarineBackgroundRunner } from '../marine/worker/index.js';
|
||||
import { WorkerLoader } from '../marine/worker-script/workerLoader.js';
|
||||
import { KeyPair } from '../keypair/index.js';
|
||||
import { Subject, Subscribable } from 'rxjs';
|
||||
import { WrapFnIntoServiceCall } from '../jsServiceHost/serviceUtils.js';
|
||||
import { JsServiceHost } from '../jsServiceHost/JsServiceHost.js';
|
||||
import { ClientPeer, makeClientPeerConfig } from '../clientPeer/ClientPeer.js';
|
||||
import { WasmLoaderFromNpm, WorkerLoaderFromNpm } from '../marine/deps-loader/node.js';
|
||||
import { IConnection } from '../connection/interfaces.js';
|
||||
import { ClientPeer, makeClientPeerConfig } from "../clientPeer/ClientPeer.js";
|
||||
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";
|
||||
|
||||
export const registerHandlersHelper = (
|
||||
peer: FluencePeer,
|
||||
particle: Particle,
|
||||
handlers: Record<string, Record<string, any>>,
|
||||
peer: FluencePeer,
|
||||
particle: Particle,
|
||||
handlers: Record<
|
||||
string,
|
||||
Record<string, (args: JSONArray) => CallServiceResultType | undefined>
|
||||
>,
|
||||
) => {
|
||||
Object.entries(handlers).forEach(([serviceId, service]) => {
|
||||
Object.entries(service).forEach(([fnName, fn]) => {
|
||||
peer.internals.regHandler.forParticle(particle.id, serviceId, fnName, WrapFnIntoServiceCall(fn));
|
||||
});
|
||||
Object.entries(handlers).forEach(([serviceId, service]) => {
|
||||
Object.entries(service).forEach(([fnName, fn]) => {
|
||||
peer.internals.regHandler.forParticle(
|
||||
particle.id,
|
||||
serviceId,
|
||||
fnName,
|
||||
WrapFnIntoServiceCall(fn),
|
||||
);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export type CompiledFnCall = (peer: IFluenceClient, args: { [key: string]: any }) => Promise<unknown>;
|
||||
export type CompiledFnCall = (
|
||||
peer: FluencePeer,
|
||||
args: PassedArgs,
|
||||
) => Promise<unknown>;
|
||||
export type CompiledFile = {
|
||||
functions: { [key: string]: CompiledFnCall };
|
||||
services: { [key: string]: ServiceDef };
|
||||
functions: { [key: string]: CompiledFnCall };
|
||||
services: { [key: string]: ServiceDef };
|
||||
};
|
||||
|
||||
export const compileAqua = async (aquaFile: string): Promise<CompiledFile> => {
|
||||
await fs.access(aquaFile);
|
||||
await fs.access(aquaFile);
|
||||
|
||||
const compilationResult = await api.Aqua.compile(new api.Path(aquaFile), [], undefined);
|
||||
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'));
|
||||
}
|
||||
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: IFluenceClient, args: { [key: string]: any }) => {
|
||||
return callAquaFunction({
|
||||
def: fnInfo.funcDef,
|
||||
script: fnInfo.script,
|
||||
config: {},
|
||||
peer: peer,
|
||||
args,
|
||||
});
|
||||
};
|
||||
return { [name]: callFn };
|
||||
})
|
||||
.reduce((agg, obj) => {
|
||||
return { ...agg, ...obj };
|
||||
}, {});
|
||||
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 { functions, services: compilationResult.services };
|
||||
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>,
|
||||
};
|
||||
};
|
||||
|
||||
class NoopConnection implements IConnection {
|
||||
start(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
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>();
|
||||
stop(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
sendParticle(nextPeerIds: string[], particle: Particle): 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();
|
||||
}
|
||||
}
|
||||
|
||||
export class TestPeer extends FluencePeer {
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
export const mkTestPeer = async () => {
|
||||
const kp = await KeyPair.randomEd25519();
|
||||
const conn = new NoopConnection();
|
||||
return new TestPeer(kp, conn);
|
||||
const kp = await KeyPair.randomEd25519();
|
||||
const conn = new NoopConnection();
|
||||
return new TestPeer(kp, conn);
|
||||
};
|
||||
|
||||
export const withPeer = async (action: (p: FluencePeer) => Promise<void>) => {
|
||||
const p = await mkTestPeer();
|
||||
try {
|
||||
await p.start();
|
||||
await action(p);
|
||||
} finally {
|
||||
await p.stop();
|
||||
}
|
||||
const p = await mkTestPeer();
|
||||
|
||||
try {
|
||||
await p.start();
|
||||
await action(p);
|
||||
} finally {
|
||||
await p.stop();
|
||||
}
|
||||
};
|
||||
|
||||
export const withClient = async (
|
||||
relay: RelayOptions,
|
||||
config: ClientConfig,
|
||||
action: (client: ClientPeer) => Promise<void>,
|
||||
relay: RelayOptions,
|
||||
config: ClientConfig,
|
||||
action: (client: ClientPeer) => Promise<void>,
|
||||
) => {
|
||||
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();
|
||||
}
|
||||
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();
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user