fix: JS-client bugs and tech debt [fixes DXJ-520] (#374)

Fix various bugs and a pair of TODO's
This commit is contained in:
Akim
2023-11-06 17:42:24 +07:00
committed by GitHub
parent 2589e39113
commit b460491fbd
28 changed files with 121 additions and 210 deletions

View File

@ -14,7 +14,6 @@
* limitations under the License.
*/
import type { VersionedPackage } from "@fluencelabs/js-client-isomorphic";
import { fetchResource } from "@fluencelabs/js-client-isomorphic/fetcher";
import { getWorker } from "@fluencelabs/js-client-isomorphic/worker-resolver";
@ -27,17 +26,9 @@ import {
import { callAquaFunction } from "./compilerSupport/callFunction.js";
import { registerService } from "./compilerSupport/registerService.js";
import { MarineBackgroundRunner } from "./marine/worker/index.js";
import versions from "./versions.js";
const DEFAULT_CDN_URL = "https://unpkg.com";
const getVersionedPackage = (pkg: keyof typeof versions): VersionedPackage => {
return {
name: pkg,
version: versions[pkg],
};
};
const createClient = async (
relay: RelayOptions,
config: ClientConfig = {},
@ -46,7 +37,7 @@ const createClient = async (
const fetchMarineJsWasm = async () => {
const resource = await fetchResource(
getVersionedPackage("@fluencelabs/marine-js"),
"@fluencelabs/marine-js",
"/dist/marine-js.wasm",
CDNUrl,
);
@ -56,7 +47,7 @@ const createClient = async (
const fetchAvmWasm = async () => {
const resource = await fetchResource(
getVersionedPackage("@fluencelabs/avm"),
"@fluencelabs/avm",
"/dist/avm.wasm",
CDNUrl,
);
@ -70,10 +61,7 @@ const createClient = async (
const marine = new MarineBackgroundRunner(
{
async getValue() {
return getWorker(
getVersionedPackage("@fluencelabs/marine-worker"),
CDNUrl,
);
return getWorker("@fluencelabs/marine-worker", CDNUrl);
},
start() {
return Promise.resolve(undefined);
@ -178,7 +166,13 @@ export const Fluence: FluencePublicApi = {
},
};
export type { ClientConfig, IFluenceClient } from "./clientPeer/types.js";
export type {
ClientConfig,
IFluenceClient,
ConnectionState,
RelayOptions,
KeyPairOptions,
} from "./clientPeer/types.js";
export { v5_callFunction, v5_registerService } from "./api.js";

View File

@ -22,7 +22,6 @@ import {
KeyPairFormat,
serializeAvmArgs,
} from "@fluencelabs/avm";
import { defaultCallParameters } from "@fluencelabs/marine-js/dist/types";
import { fromUint8Array } from "js-base64";
import {
concatMap,
@ -201,12 +200,7 @@ export abstract class FluencePeer {
new Error("Can't use avm: peer is not initialized");
}
const res = await this.marineHost.callService(
"avm",
"ast",
[air],
defaultCallParameters,
);
const res = await this.marineHost.callService("avm", "ast", [air]);
if (!isString(res)) {
throw new Error(
@ -435,7 +429,6 @@ export abstract class FluencePeer {
"avm",
"invoke",
args,
defaultCallParameters,
);
avmCallResult = deserializeAvmResult(res);
@ -654,7 +647,6 @@ export abstract class FluencePeer {
req.serviceId,
req.fnName,
req.args,
defaultCallParameters,
);
return {

View File

@ -99,5 +99,5 @@ describe("FluencePeer flow tests", () => {
expect(res).toEqual(expect.arrayContaining(["test1", "test1"]));
});
}, 1500);
}, 1800);
});

View File

@ -20,9 +20,10 @@ import {
RunParameters,
} from "@fluencelabs/avm";
import { JSONObject, JSONValue, JSONArray } from "@fluencelabs/interfaces";
import { CallParameters } from "@fluencelabs/marine-worker";
import type { Worker as WorkerImplementation } from "@fluencelabs/threads/master";
import { IStartable, CallParameters } from "../util/commonTypes.js";
import { IStartable } from "../util/commonTypes.js";
/**
* Contract for marine host implementations. Marine host is responsible for creating calling and removing marine services
@ -53,7 +54,7 @@ export interface IMarineHost extends IStartable {
serviceId: string,
functionName: string,
args: JSONArray | JSONObject,
callParams: CallParameters,
callParams?: CallParameters,
): Promise<JSONValue>;
}

View File

@ -16,12 +16,11 @@
import { JSONValue } from "@fluencelabs/interfaces";
import type {
JSONArray,
JSONObject,
MarineBackgroundInterface,
LogFunction,
JSONValueNonNullable,
CallParameters,
} from "@fluencelabs/marine-js/dist/types";
import { LogFunction, logLevelToEnv } from "@fluencelabs/marine-js/dist/types";
import type { MarineBackgroundInterface } from "@fluencelabs/marine-worker";
} from "@fluencelabs/marine-worker";
import { ModuleThread, Thread, spawn } from "@fluencelabs/threads/master";
import { MarineLogger, marineLogger } from "../../util/logger.js";
@ -94,18 +93,15 @@ export class MarineBackgroundRunner implements IMarineHost {
throw new Error("Worker is not initialized");
}
// The logging level is controlled by the environment variable passed to enable debug logs.
// We enable all possible log levels passing the control for exact printouts to the logger
const env = logLevelToEnv("info");
this.loggers.set(serviceId, marineLogger(serviceId));
await this.workerThread.createService(serviceModule, serviceId, env);
await this.workerThread.createService(serviceModule, serviceId);
}
async callService(
serviceId: string,
functionName: string,
args: JSONArray | JSONObject,
callParams: CallParameters,
args: Array<JSONValueNonNullable> | Record<string, JSONValueNonNullable>,
callParams?: CallParameters,
): Promise<JSONValue> {
if (this.workerThread == null) {
throw new Error("Worker is not initialized");

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
export type { CallParameters } from "@fluencelabs/marine-js/dist/types";
export interface IStartable {
start(): Promise<void>;
stop(): Promise<void>;

View File

@ -16,7 +16,7 @@
import { promises as fs } from "fs";
import * as api from "@fluencelabs/aqua-api/aqua-api.js";
import { Path, Aqua } from "@fluencelabs/aqua-api/aqua-api.js";
import {
FunctionCallDef,
JSONArray,
@ -68,11 +68,16 @@ export type CompiledFile = {
services: { [key: string]: ServiceDef };
};
interface FunctionInfo {
script: string;
funcDef: FunctionCallDef;
}
export const compileAqua = async (aquaFile: string): Promise<CompiledFile> => {
await fs.access(aquaFile);
const compilationResult = await api.Aqua.compile(
new api.Path(aquaFile),
const compilationResult = await Aqua.compile(
new Path(aquaFile),
[],
undefined,
);
@ -84,12 +89,10 @@ export const compileAqua = async (aquaFile: string): Promise<CompiledFile> => {
}
const functions = Object.entries(compilationResult.functions)
.map(([name, fnInfo]) => {
.map(([name, fnInfo]: [string, FunctionInfo]) => {
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,
def: fnInfo.funcDef,
script: fnInfo.script,
config: {},
peer: peer,
@ -105,9 +108,7 @@ export const compileAqua = async (aquaFile: string): Promise<CompiledFile> => {
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>,
services: compilationResult.services,
};
};