@metacall/faas
Version:
Reimplementation of MetaCall FaaS platform written in TypeScript.
92 lines (91 loc) • 3.2 kB
JavaScript
;
/* eslint-disable */
Object.defineProperty(exports, "__esModule", { value: true });
const package_1 = require("@metacall/protocol/package");
const fs_1 = require("fs");
const metacall_1 = require("metacall");
const os_1 = require("os");
const path_1 = require("path");
const protocol_1 = require("./protocol");
const functions = {};
const createMetacallJsonFiles = async (path, jsons) => {
for (const el of jsons) {
const filePath = path_1.join(path, `metacall-${el.language_id}.json`);
await fs_1.promises.writeFile(filePath, JSON.stringify(el));
}
};
const loadDeployment = (resource, jsonPaths) => {
const deployment = {
status: 'create',
prefix: os_1.hostname(),
suffix: resource.id,
version: 'v1',
packages: {},
ports: []
};
for (const path of jsonPaths) {
// Load the json into metacall
const fullPath = path_1.join(resource.path, path);
const exports = metacall_1.metacall_load_from_configuration_export(fullPath);
// Get the inspect information
const inspect = metacall_1.metacall_inspect();
const json = require(fullPath);
if (!json.language_id) {
throw new Error(`language_id not found in ${path}`);
}
deployment.packages[json.language_id] =
inspect[json.language_id];
// Store the functions
Object.keys(exports).forEach(func => {
functions[func] = exports[func];
});
}
return deployment;
};
const handleDeployment = async (resource) => {
// Check if the deploy comes with extra JSONs and store them
if (resource.jsons.length > 0) {
const jsonPaths = await createMetacallJsonFiles(resource.path, resource.jsons);
}
// List all files except by the ignored ones
const filesPaths = await package_1.findFilesPath(resource.path);
// Get the JSONs from the list of files
const jsonPaths = package_1.findMetaCallJsons(filesPaths);
// Deploy the JSONs
return loadDeployment(resource, jsonPaths);
};
process.on('message', (payload) => {
switch (payload.type) {
// Handle deploy load
case protocol_1.WorkerMessageType.Load: {
const resource = payload.data;
handleDeployment(resource)
.then(app => {
if (process.send) {
process.send({
type: protocol_1.WorkerMessageType.MetaData,
data: app
});
}
})
.catch(() => process.exit(1)); // TODO: Handle this with a message?
break;
}
// Handle invoke function
case protocol_1.WorkerMessageType.Invoke: {
const fn = payload.data;
if (process.send) {
process.send({
type: protocol_1.WorkerMessageType.InvokeResult,
data: {
id: fn.id,
result: functions[fn.name](...fn.args)
}
});
}
break;
}
default:
break;
}
});