UNPKG

@comake/skl-js-engine

Version:

Standard Knowledge Language Javascript Engine

58 lines 2.63 kB
"use strict"; /* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/no-empty-function */ /* eslint-disable prefer-arrow-callback */ Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable @typescript-eslint/ban-ts-comment */ // @ts-nocheck const ChildStdioTransport_ts_1 = require("../../transport/stdio/ChildStdioTransport.ts"); // Create a single transport that handles both server and client functionality const transport = new ChildStdioTransport_ts_1.ChildStdioTransport(); transport.setName('jsExecutor-example-process'); // Register methods that the parent can call (server functionality) transport.registerMethod('executeCode', async (message) => { const AsyncFunction = Object.getPrototypeOf(async function () { }).constructor; const executeCode = new AsyncFunction('args', 'console', 'request', 'SKL', ` ${message.code} try { if (typeof ${message.functionName} === 'function') { return await ${message.functionName}(args); } throw new Error('Function name is not defined'); } catch (error) { throw error; } `); // Const sklEngine = new SKLEngine({ // skdsEndpointUrl: 'http://localhost:3000' // }); // Create SKL proxy that converts method calls to request calls const SKL = new Proxy({}, { get(target, prop) { if (typeof prop === 'string') { return async (...args) => { // Convert method call to request call // SKL.getTime() becomes request('getTime', {}, options) const [params = {}, options = {}] = args; return await transport.request(prop, params, options); }; } } }); const result = await executeCode(message.args, console, transport.request.bind(transport), SKL); return { result }; }); transport.registerMethod('ping', async () => 'pong'); // Register a method that demonstrates calling back to the parent (client functionality) // transport.registerMethod('callParent', async(): Promise<{ result: string }> => { // try { // // Call the parent's getTime method // const result = await transport.request('getTime'); // return { result: `Parent getTime returned: ${JSON.stringify(result)}` }; // } catch (error: unknown) { // return { result: `Error calling parent: ${(error as Error).message}` }; // } // }); // Initialize the transport (this sets up stdio communication) await transport.initialize(); //# sourceMappingURL=process.js.map