2023-10-17 22:14:08 +07:00
|
|
|
/**
|
2023-08-25 00:15:49 +07:00
|
|
|
* Copyright 2023 Fluence Labs Limited
|
2023-02-13 17:41:35 +03:00
|
|
|
*
|
|
|
|
* 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";
|
2023-11-14 04:55:50 +07:00
|
|
|
import { ServiceImpl } from "./compilerSupport/types.js";
|
2023-10-17 22:14:08 +07:00
|
|
|
import { FluencePeer } from "./jsPeer/FluencePeer.js";
|
|
|
|
|
|
|
|
import { callAquaFunction, Fluence, registerService } from "./index.js";
|
2023-09-05 21:38:59 +07:00
|
|
|
|
2023-02-13 17:41:35 +03:00
|
|
|
/**
|
2023-11-14 04:55:50 +07:00
|
|
|
* 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-02-13 17:41:35 +03:00
|
|
|
*/
|
2023-11-15 22:00:08 +07:00
|
|
|
export const v5_callFunction = async (
|
2023-11-16 17:49:28 +07:00
|
|
|
args: (JSONValue | ServiceImpl[string])[],
|
2023-11-15 22:00:08 +07:00
|
|
|
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;
|
2023-02-13 17:41:35 +03:00
|
|
|
|
2023-11-15 22:00:08 +07:00
|
|
|
if (args[0] instanceof FluencePeer) {
|
|
|
|
peer = args[0];
|
|
|
|
args = args.slice(1);
|
|
|
|
} else {
|
2023-10-17 22:14:08 +07:00
|
|
|
peer = Fluence.defaultClient;
|
|
|
|
}
|
2023-02-13 17:41:35 +03:00
|
|
|
|
2023-11-15 22:00:08 +07:00
|
|
|
// 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()?",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-16 17:49:28 +07:00
|
|
|
const callArgs = Object.fromEntries<JSONValue | ServiceImpl[string]>(
|
2023-11-15 22:00:08 +07:00
|
|
|
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)];
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
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 = {
|
2023-11-14 04:55:50 +07:00
|
|
|
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-14 04:55:50 +07:00
|
|
|
});
|
2023-11-15 22:00:08 +07:00
|
|
|
|
|
|
|
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);
|
2023-11-14 04:55:50 +07:00
|
|
|
};
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
/**
|
2023-11-14 04:55:50 +07:00
|
|
|
* 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-02-13 17:41:35 +03:00
|
|
|
*/
|
2023-11-15 22:00:08 +07:00
|
|
|
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;
|
2023-02-13 17:41:35 +03:00
|
|
|
|
2023-11-15 22:00:08 +07:00
|
|
|
if (args[0] instanceof FluencePeer) {
|
|
|
|
peer = args[0];
|
|
|
|
args = args.slice(1);
|
|
|
|
} else {
|
2023-10-17 22:14:08 +07:00
|
|
|
peer = Fluence.defaultClient;
|
|
|
|
}
|
|
|
|
|
2023-11-15 22:00:08 +07:00
|
|
|
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]) => {
|
2023-11-16 17:49:28 +07:00
|
|
|
return [name, wrapFunction(func, serviceSchema[name])];
|
2023-11-15 22:00:08 +07:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2023-11-14 04:55:50 +07:00
|
|
|
registerService({
|
2023-11-15 22:00:08 +07:00
|
|
|
service: wrappedServiceImpl,
|
2023-11-14 04:55:50 +07:00
|
|
|
peer,
|
2023-11-15 22:00:08 +07:00
|
|
|
serviceId,
|
2023-11-14 04:55:50 +07:00
|
|
|
});
|
2023-02-13 17:41:35 +03:00
|
|
|
};
|