Review fixes

This commit is contained in:
Akim Mamedov 2023-11-17 23:28:21 +07:00
parent 22a0cc27d6
commit 56620ab6fa
4 changed files with 18 additions and 13 deletions

View File

@ -28,7 +28,7 @@ import {
aqua2js,
SchemaValidationError,
js2aqua,
wrapFunction,
wrapJsFunction,
} from "./compilerSupport/conversions.js";
import { ServiceImpl } from "./compilerSupport/types.js";
import { FluencePeer } from "./jsPeer/FluencePeer.js";
@ -103,7 +103,7 @@ export const v5_callFunction = async (
);
}
return [argName, wrapFunction(arg, argSchema)];
return [argName, wrapJsFunction(arg, argSchema)];
}
if (typeof arg === "function") {
@ -204,7 +204,7 @@ export const v5_registerService = (
// Wrapping service impl to convert their args ts -> aqua and backwards
const wrappedServiceImpl = Object.fromEntries(
Object.entries(serviceImpl).map(([name, func]) => {
return [name, wrapFunction(func, serviceSchema[name])];
return [name, wrapJsFunction(func, serviceSchema[name])];
}),
);

View File

@ -202,7 +202,9 @@ export function js2aqua(
}
}
export const wrapFunction = (
// Wrapping function, converting its arguments to aqua before call and back to js after call.
// It makes callbacks and service functions defined by user operate on js types seamlessly
export const wrapJsFunction = (
func: ServiceImpl[string],
schema:
| ArrowWithoutCallbacks

View File

@ -68,13 +68,15 @@ const parseWithSchema = <T extends z.ZodTypeAny>(
): [z.infer<T>, null] | [null, string] => {
const result = schema.safeParse(req.args, {
errorMap: (issue, ctx) => {
if (issue.code === z.ZodIssueCode.invalid_type) {
if (issue.path.length === 1 && typeof issue.path[0] === "number") {
const [arg] = issue.path;
return {
message: `Argument ${arg} expected to be of type ${issue.expected}, Got ${issue.received}`,
};
}
if (
issue.code === z.ZodIssueCode.invalid_type &&
issue.path.length === 1 &&
typeof issue.path[0] === "number"
) {
const [arg] = issue.path;
return {
message: `Argument ${arg} expected to be of type ${issue.expected}, Got ${issue.received}`,
};
}
if (issue.code === z.ZodIssueCode.too_big) {
@ -340,8 +342,7 @@ export const builtInServices: Record<
}),
concat_strings: withSchema(z.array(z.string()))((args) => {
const res = "".concat(...args);
return success(res);
return success(args.join(""));
}),
},

View File

@ -19,6 +19,8 @@ import { PeerIdB58 } from "@fluencelabs/interfaces";
import { ParticleContext } from "../jsServiceHost/interfaces.js";
// Helpers for validating service function
/**
* A predicate of call params for sig service's sign method which determines whether signing operation is allowed or not
*/