202 lines
5.7 KiB
TypeScript
Raw Normal View History

/**
feat!: Unify all packages (#327) * * Separate marine worker as a package * Trying to fix tests * Finalizing test fixes * fix: rename back to Fluence CLI (#320) chore: rename back to Fluence CLI * fix(deps): update dependency @fluencelabs/avm to v0.43.1 (#322) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore: release master (#324) * chore: release master * chore: Regenerate pnpm lock file * feat: use marine-js 0.7.2 (#321) * use marine-js 0.5.0 * increace some timeouts * increace some timeouts * use latest marine + remove larger timeouts * propagate CallParameters type * use marine 0.7.2 * Temp use node 18 and 20 * Comment out node 20.x --------- Co-authored-by: Anatoly Laskaris <github_me@nahsi.dev> * chore: Fix test with node 18/20 error message (#323) * Fix test with node 18/20 error message * Run tests on node 18 and 20 * Enhance description * Fix type and obj property --------- Co-authored-by: Anatoly Laskaris <github_me@nahsi.dev> * * Separate marine worker as a package * Trying to fix tests * Finalizing test fixes * * Refactoring packages. * Using CDN to load .wasm deps. * Setting up tests for new architecture * Fix almost all tests * Fix last strange test * Remove package specific packages * Remove avm class as it looks excessive * marine worker new version * misc refactoring/remove console.log's * Rename package js-peer to js-client * Move service info to marine worker * Change CDN path * Fix worker race confition * Remove buffer type * Remove turned off headless mode in platform tests * Remove async keyword to make tests pass * Remove util package * Make js-client.api package just reexport interface from js-client main package * Update package info in CI * Fix review comments * Remove test entry from marine-worker package * Misc fixes * Fix worker type * Add fetchers * Specify correct versions for js-client package * Set first ver for js-client * Update libp2p and related dep versions to the latest * Build all deps into package itself * Fix review * Refine package * Fix comment * Update packages/core/js-client/src/fetchers/browser.ts * Update packages/core/js-client/src/fetchers/index.ts * Update packages/core/js-client/src/fetchers/node.ts * Update packages/core/js-client/src/jsPeer/FluencePeer.ts * Update packages/core/js-client/src/keypair/__test__/KeyPair.spec.ts * Update packages/core/js-client/src/jsPeer/FluencePeer.ts Co-authored-by: shamsartem <shamsartem@gmail.com> * Delete outdated file * Need types for build to work * Inline func call * Add comments to replacement lines. P.S. we can remove some of them after update libp2p --------- Co-authored-by: shamsartem <shamsartem@gmail.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: fluencebot <116741523+fluencebot@users.noreply.github.com> Co-authored-by: Valery Antopol <valery.antopol@gmail.com> Co-authored-by: Anatoly Laskaris <github_me@nahsi.dev>
2023-08-25 00:15:49 +07: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-11-15 22:00:08 +07:00
import type {
FunctionCallDef,
JSONValue,
SimpleTypes,
ArrowWithoutCallbacks,
ServiceDef,
} from "@fluencelabs/interfaces";
import { CallAquaFunctionConfig } from "./compilerSupport/callFunction.js";
import {
aqua2ts,
ts2aqua,
wrapFunction,
} from "./compilerSupport/conversions.js";
import { ServiceImpl } from "./compilerSupport/types.js";
import { FluencePeer } from "./jsPeer/FluencePeer.js";
import { callAquaFunction, Fluence, registerService } from "./index.js";
2023-11-16 20:10:18 +07:00
const isAquaConfig = (
config: JSONValue | ServiceImpl[string] | undefined,
): config is CallAquaFunctionConfig => {
return (
typeof config === "object" &&
config !== null &&
!Array.isArray(config) &&
["undefined", "number"].includes(typeof config["ttl"])
);
};
/**
* Convenience function to support Aqua `func` generation backend
2023-11-15 22:00:08 +07:00
* The compiler only need to generate a call the function and provide the corresponding definitions and the air script
*
* @param args - raw arguments passed by user to the generated function
* @param def - function definition generated by the Aqua compiler
* @param script - air script with function execution logic generated by the Aqua compiler
*/
2023-11-15 22:00:08 +07:00
export const v5_callFunction = async (
2023-11-16 22:12:40 +07:00
args: [
client: FluencePeer | (JSONValue | ServiceImpl[string]),
...args: (JSONValue | ServiceImpl[string])[],
],
2023-11-15 22:00:08 +07:00
def: FunctionCallDef,
script: string,
): Promise<unknown> => {
2023-11-16 22:12:40 +07:00
const [peer, ...rest] = args;
if (!(peer instanceof FluencePeer)) {
await v5_callFunction([getDefaultPeer(), ...rest], def, script);
return;
}
2023-11-15 22:00:08 +07:00
const argNames = Object.keys(def.arrow);
2023-11-16 20:10:18 +07:00
const schemaArgCount = argNames.length;
2023-11-15 22:00:08 +07:00
2023-11-16 20:10:18 +07:00
type FunctionArg = SimpleTypes | ArrowWithoutCallbacks;
const schemaFunctionArgs: Record<string, FunctionArg> =
2023-11-15 22:00:08 +07:00
def.arrow.domain.tag === "nil" ? {} : def.arrow.domain.fields;
2023-11-16 20:10:18 +07:00
// if args more than expected in schema (schemaArgCount) then last arg is config
2023-11-16 22:12:40 +07:00
const config = schemaArgCount < rest.length ? rest.pop() : undefined;
2023-11-16 20:10:18 +07:00
if (!isAquaConfig(config)) {
throw new Error("Config should be object type");
}
2023-11-16 17:49:28 +07:00
const callArgs = Object.fromEntries<JSONValue | ServiceImpl[string]>(
2023-11-16 22:12:40 +07:00
rest.slice(0, schemaArgCount).map((arg, i) => {
2023-11-16 20:10:18 +07:00
const argSchema = schemaFunctionArgs[argNames[i]];
2023-11-15 22:00:08 +07:00
if (argSchema.tag === "arrow") {
if (typeof arg !== "function") {
2023-11-16 20:10:18 +07:00
throw new Error("Argument and schema don't match");
2023-11-15 22:00:08 +07:00
}
const wrappedFunction = wrapFunction(arg, argSchema);
return [argNames[i], wrappedFunction];
}
if (typeof arg === "function") {
2023-11-16 20:10:18 +07:00
throw new Error("Argument and schema don't match");
2023-11-15 22:00:08 +07:00
}
return [argNames[i], ts2aqua(arg, argSchema)];
}),
);
2023-11-15 23:10:33 +07:00
const returnTypeVoid =
def.arrow.codomain.tag === "nil" || def.arrow.codomain.items.length === 0;
2023-11-15 22:00:08 +07:00
const params = {
peer,
2023-11-15 22:00:08 +07:00
args: callArgs,
config,
};
const result = await callAquaFunction({
script,
...params,
2023-11-15 23:10:33 +07:00
fireAndForget: returnTypeVoid,
});
2023-11-15 22:00:08 +07:00
2023-11-16 20:10:18 +07:00
const returnSchema =
2023-11-15 22:00:08 +07:00
def.arrow.codomain.tag === "unlabeledProduct" &&
def.arrow.codomain.items.length === 1
? def.arrow.codomain.items[0]
: def.arrow.codomain;
2023-11-16 20:10:18 +07:00
return aqua2ts(result, returnSchema);
};
2023-11-16 22:12:40 +07:00
const getDefaultPeer = (): FluencePeer => {
if (Fluence.defaultClient == null) {
throw new Error(
"Could not register Aqua service because the client is not initialized. Did you forget to call Fluence.connect()?",
);
}
return Fluence.defaultClient;
};
const getDefaultServiceId = (def: ServiceDef) => {
if (def.defaultServiceId == null) {
throw new Error("Service ID is not provided");
}
return def.defaultServiceId;
};
type RegisterServiceType =
| [ServiceImpl]
| [string, ServiceImpl]
| [FluencePeer, ServiceImpl]
| [FluencePeer, string, ServiceImpl];
/**
* Convenience function to support Aqua `service` generation backend
2023-11-15 22:00:08 +07:00
* The compiler only need to generate a call the function and provide the corresponding definitions and the air script
* @param args - raw arguments passed by user to the generated function
* @param def - service definition generated by the Aqua compiler
*/
2023-11-16 22:12:40 +07:00
export const v5_registerService = (
args: RegisterServiceType,
def: ServiceDef,
): void => {
if (args.length === 1) {
v5_registerService(
[getDefaultPeer(), getDefaultServiceId(def), args[0]],
def,
2023-11-15 22:00:08 +07:00
);
2023-11-16 22:12:40 +07:00
return;
2023-11-16 22:58:59 +07:00
}
if (args.length === 2) {
2023-11-16 22:12:40 +07:00
if (args[0] instanceof FluencePeer) {
v5_registerService([args[0], getDefaultServiceId(def), args[1]], def);
return;
2023-11-16 20:10:18 +07:00
}
2023-11-16 22:58:59 +07:00
v5_registerService([getDefaultPeer(), args[0], args[1]], def);
return;
2023-11-15 22:00:08 +07:00
}
2023-11-16 22:12:40 +07:00
const [peer, serviceId, serviceImpl] = args;
2023-11-15 22:00:08 +07:00
2023-11-16 20:10:18 +07:00
// Schema for every function in service
2023-11-15 22:00:08 +07:00
const serviceSchema = def.functions.tag === "nil" ? {} : def.functions.fields;
2023-11-16 20:10:18 +07:00
// Wrapping service impl to convert their args ts -> aqua and backwards
2023-11-15 22:00:08 +07:00
const wrappedServiceImpl = Object.fromEntries(
Object.entries(serviceImpl).map(([name, func]) => {
2023-11-16 17:49:28 +07:00
return [name, wrapFunction(func, serviceSchema[name])];
2023-11-15 22:00:08 +07:00
}),
);
registerService({
2023-11-15 22:00:08 +07:00
service: wrappedServiceImpl,
peer,
2023-11-15 22:00:08 +07:00
serviceId,
});
};