mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-24 21:31:32 +00:00
Review fixes
This commit is contained in:
@ -14,77 +14,155 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import type { CallAquaFunctionArgs } from "./compilerSupport/callFunction.js";
|
||||
import type {
|
||||
FunctionCallDef,
|
||||
JSONValue,
|
||||
SimpleTypes,
|
||||
ArrowWithoutCallbacks,
|
||||
ServiceDef,
|
||||
} from "@fluencelabs/interfaces";
|
||||
|
||||
import { CallAquaFunctionConfig } from "./compilerSupport/callFunction.js";
|
||||
import {
|
||||
aqua2ts,
|
||||
ts2aqua,
|
||||
wrapFunction,
|
||||
wrapServiceFunction,
|
||||
} from "./compilerSupport/conversions.js";
|
||||
import { ServiceImpl } from "./compilerSupport/types.js";
|
||||
import { FluencePeer } from "./jsPeer/FluencePeer.js";
|
||||
|
||||
import { callAquaFunction, Fluence, registerService } from "./index.js";
|
||||
|
||||
export const isFluencePeer = (
|
||||
fluencePeerCandidate: unknown,
|
||||
): fluencePeerCandidate is FluencePeer => {
|
||||
return fluencePeerCandidate instanceof FluencePeer;
|
||||
};
|
||||
|
||||
type CallAquaFunctionArgsTuned = Pick<CallAquaFunctionArgs, "args" | "script"> &
|
||||
Partial<Pick<CallAquaFunctionArgs, "config" | "peer">>;
|
||||
|
||||
type RegisterServiceArgs = {
|
||||
peer?: FluencePeer;
|
||||
service: ServiceImpl;
|
||||
serviceId: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convenience function to support Aqua `func` generation backend
|
||||
* The compiler only need to generate a call the function and provide the air script
|
||||
* 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
|
||||
*/
|
||||
export const v5_callFunction = async ({
|
||||
config = {},
|
||||
peer,
|
||||
args,
|
||||
script,
|
||||
}: CallAquaFunctionArgsTuned): Promise<unknown> => {
|
||||
if (peer == null) {
|
||||
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()?",
|
||||
);
|
||||
}
|
||||
export const v5_callFunction = async (
|
||||
args: (JSONValue | ((...args: JSONValue[]) => JSONValue))[],
|
||||
def: FunctionCallDef,
|
||||
script: string,
|
||||
): Promise<unknown> => {
|
||||
const argNames = Object.keys(def.arrow);
|
||||
const argCount = argNames.length;
|
||||
|
||||
const functionArgs: Record<string, SimpleTypes | ArrowWithoutCallbacks> =
|
||||
def.arrow.domain.tag === "nil" ? {} : def.arrow.domain.fields;
|
||||
|
||||
let peer: FluencePeer | undefined;
|
||||
|
||||
if (args[0] instanceof FluencePeer) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
} else {
|
||||
peer = Fluence.defaultClient;
|
||||
}
|
||||
|
||||
return callAquaFunction({
|
||||
args,
|
||||
script,
|
||||
config,
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||
const config =
|
||||
argCount < args.length
|
||||
? // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||
(args.pop() as CallAquaFunctionConfig | undefined)
|
||||
: undefined;
|
||||
|
||||
if (peer == null) {
|
||||
throw new Error(
|
||||
"Could not register Aqua service because the client is not initialized. Did you forget to call Fluence.connect()?",
|
||||
);
|
||||
}
|
||||
|
||||
const callArgs = Object.fromEntries<
|
||||
JSONValue | ((...args: JSONValue[]) => JSONValue)
|
||||
>(
|
||||
args.slice(0, argCount).map((arg, i) => {
|
||||
const argSchema = functionArgs[argNames[i]];
|
||||
|
||||
if (argSchema.tag === "arrow") {
|
||||
if (typeof arg !== "function") {
|
||||
throw new Error("Argument and schema doesn't match");
|
||||
}
|
||||
|
||||
const wrappedFunction = wrapFunction(arg, argSchema);
|
||||
|
||||
return [argNames[i], wrappedFunction];
|
||||
}
|
||||
|
||||
if (typeof arg === "function") {
|
||||
throw new Error("Argument and schema doesn't match");
|
||||
}
|
||||
|
||||
return [argNames[i], ts2aqua(arg, argSchema)];
|
||||
}),
|
||||
);
|
||||
|
||||
const params = {
|
||||
peer,
|
||||
args: callArgs,
|
||||
config,
|
||||
};
|
||||
|
||||
const result = await callAquaFunction({
|
||||
script,
|
||||
...params,
|
||||
});
|
||||
|
||||
const valueSchema =
|
||||
def.arrow.codomain.tag === "unlabeledProduct" &&
|
||||
def.arrow.codomain.items.length === 1
|
||||
? def.arrow.codomain.items[0]
|
||||
: def.arrow.codomain;
|
||||
|
||||
return aqua2ts(result, valueSchema);
|
||||
};
|
||||
|
||||
/**
|
||||
* Convenience function to support Aqua `service` generation backend
|
||||
* The compiler only need to generate a call the function and provide the air script
|
||||
* 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
|
||||
*/
|
||||
export const v5_registerService = ({
|
||||
serviceId,
|
||||
service,
|
||||
peer,
|
||||
}: RegisterServiceArgs): void => {
|
||||
if (peer == null) {
|
||||
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()?",
|
||||
);
|
||||
}
|
||||
export const v5_registerService = (args: unknown[], def: ServiceDef): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||
const serviceImpl = args.pop() as ServiceImpl;
|
||||
let peer: FluencePeer | undefined;
|
||||
let serviceId = def.defaultServiceId;
|
||||
|
||||
if (args[0] instanceof FluencePeer) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
} else {
|
||||
peer = Fluence.defaultClient;
|
||||
}
|
||||
|
||||
if (peer == null) {
|
||||
throw new Error(
|
||||
"Could not register Aqua service because the client is not initialized. Did you forget to call Fluence.connect()?",
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof args[0] === "string") {
|
||||
serviceId = args[0];
|
||||
}
|
||||
|
||||
if (serviceId == null) {
|
||||
throw new Error("Service ID is not provided");
|
||||
}
|
||||
|
||||
const serviceSchema = def.functions.tag === "nil" ? {} : def.functions.fields;
|
||||
|
||||
const wrappedServiceImpl = Object.fromEntries(
|
||||
Object.entries(serviceImpl).map(([name, func]) => {
|
||||
return [name, wrapServiceFunction(func, serviceSchema[name])];
|
||||
}),
|
||||
);
|
||||
|
||||
registerService({
|
||||
service,
|
||||
serviceId,
|
||||
service: wrappedServiceImpl,
|
||||
peer,
|
||||
serviceId,
|
||||
});
|
||||
};
|
||||
|
Reference in New Issue
Block a user