UNPKG

@metacall/faas

Version:

Reimplementation of MetaCall FaaS platform written in TypeScript.

46 lines (45 loc) 1.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.callFunction = void 0; const app_1 = require("../app"); const appError_1 = __importDefault(require("../utils/appError")); const invoke_1 = require("../utils/invoke"); const protocol_1 = require("../worker/protocol"); const callFunction = (req, res, next) => { if (!req.params?.suffix) { return next(new appError_1.default('A the deployment name (suffix) is required.', 404)); } if (!req.params?.func) { return next(new appError_1.default('A function name is required in the path; i.e: /call/sum.', 404)); } const { suffix, func } = req.params; const args = Object.values(req.body); const application = app_1.Applications[suffix]; // Check if the application exists and it is running if (!application?.proc) { return res .status(404) .send(`Oops! It looks like the application '${suffix}' has not been deployed yet. Please deploy it before you can call its functions.`); } // Enqueue the call with a specific id, in order to be able to resolve the // promise later on when the message is received in the process message handler application.proc?.send({ type: protocol_1.WorkerMessageType.Invoke, data: { id: invoke_1.invokeQueue.push({ resolve: (data) => { res.send(data); }, reject: (error) => { res.status(500).send(error); } }), name: func, args } }); }; exports.callFunction = callFunction;