@metacall/faas
Version:
Reimplementation of MetaCall FaaS platform written in TypeScript.
47 lines (46 loc) • 2.02 kB
JavaScript
;
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) => {
var _a, _b, _c;
if (!((_a = req.params) === null || _a === void 0 ? void 0 : _a.suffix)) {
return next(new appError_1.default('A the deployment name (suffix) is required.', 404));
}
if (!((_b = req.params) === null || _b === void 0 ? void 0 : _b.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 === null || application === void 0 ? void 0 : 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
(_c = application.proc) === null || _c === void 0 ? void 0 : _c.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;