diff --git a/packages/core/js-client/src/api.ts b/packages/core/js-client/src/api.ts index d153bb4e..031066c1 100644 --- a/packages/core/js-client/src/api.ts +++ b/packages/core/js-client/src/api.ts @@ -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])]; }), ); diff --git a/packages/core/js-client/src/compilerSupport/conversions.ts b/packages/core/js-client/src/compilerSupport/conversions.ts index 6bbd1ff2..a740ef6b 100644 --- a/packages/core/js-client/src/compilerSupport/conversions.ts +++ b/packages/core/js-client/src/compilerSupport/conversions.ts @@ -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 diff --git a/packages/core/js-client/src/services/builtins.ts b/packages/core/js-client/src/services/builtins.ts index b6541fa8..b5e23c61 100644 --- a/packages/core/js-client/src/services/builtins.ts +++ b/packages/core/js-client/src/services/builtins.ts @@ -68,13 +68,15 @@ const parseWithSchema = ( ): [z.infer, 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("")); }), }, diff --git a/packages/core/js-client/src/services/securityGuard.ts b/packages/core/js-client/src/services/securityGuard.ts index 1417e52b..2ebac50b 100644 --- a/packages/core/js-client/src/services/securityGuard.ts +++ b/packages/core/js-client/src/services/securityGuard.ts @@ -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 */