genezio
Version:
Command line utility to interact with Genezio infrastructure.
294 lines (269 loc) • 10.7 kB
JavaScript
//const object = new handler.genezio[Object.keys(handler.genezio)[0]]();
/* eslint-disable no-useless-escape */
export const clusterHandlerGenerator = (className) => {
return `
/** This is an auto generated code. This code should not be modified since the file can be overwritten
* if new genezio commands are executed.
*/
import { ${className.replace(/["]/g, "")} as genezioClass } from "./module.mjs";
import { server } from "./local.mjs"
var handler = undefined;
function prepareForSerialization(e) {
if (e instanceof Error) {
const object = { message: e.message, stack: e.stack, info: e.info, code: e.code }
return object;
} else {
console.log(\`Unsupported error type \${typeof e}\`)
return { message: "Unknown error occurred. Check logs for more information!" }
}
}
if (!genezioClass) {
console.error(
'Error! No class found with name ${className}. Make sure you exported it from your file.'
);
handler = async function (event, context) {
return {
statusCode: 500,
body: JSON.stringify({
jsonrpc: "2.0",
error: {
code: -1,
message:
'Error! No class found with name ${className}. Make sure you exported it from your file.',
},
id: 0,
}),
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' }
};
};
} else {
const sendSentryError = async function (err) {
try {
const { createRequire } = await import("module");
const require = createRequire(import.meta.url);
const Sentry = require("@sentry/node");
Sentry.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: process.env.SENTRY_TRACES_SAMPLE_RATE
});
Sentry.captureException(err);
await Sentry.flush();
} catch (e) {}
};
let object;
try {
object = new genezioClass(server);
} catch (error) {
handler = async function (event, context) {
await sendSentryError(error);
return {
statusCode: 500,
body: JSON.stringify({
jsonrpc: "2.0",
error: {
code: -1,
message: \`Constructor call failure: \$\{error.message\}\`
},
id: 0,
}),
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' }
};
};
}
handler = handler ?? async function (event, context) {
if (event.genezioEventType === "cron") {
const method = event.methodName;
if (!object[method]) {
console.error(\`ERROR: Cron method named \$\{method\} does not exist.\`);
return;
}
console.log(
"DEBUG: trigger cron: " + event.cronString + " on method: " + method
);
try {
await object[method]();
} catch (error) {
console.log("ERROR: cron trigger with error: " + error);
}
return;
}
if (event.requestContext.http.method === "OPTIONS") {
const response = {
statusCode: 200,
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' }
};
return response;
}
let body = event.body;
try {
body = JSON.parse(event.body);
} catch (error) {
body = event.body;
}
const components = event.requestContext.http.path.substring(1).split("/");
if (!body || (body && body["jsonrpc"] !== "2.0")) {
if (!components[1]) {
return {
statusCode: 404,
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' },
body: JSON.stringify({ error: "Method not found" }),
};
}
const method = components[1];
const req = {
headers: event.headers,
http: event.requestContext.http,
queryStringParameters: event.queryStringParameters,
timeEpoch: event.requestContext.timeEpoch,
body: event.isBase64Encoded ? Buffer.from(body, "base64") : body,
rawBody: event.body
};
if (!object[method]) {
return {
statusCode: 404,
headers: { "Content-Type": "text/json", 'X-Powered-By': 'genezio' },
body: JSON.stringify({ error: "Method not found" }),
};
}
try {
const response = await object[method](req);
if (!response.statusCode) {
response.statusCode = 200;
}
if (response.body instanceof Buffer) {
response.body = response.body.toString("base64");
return {
...response,
isBase64Encoded: true,
};
} else if (response.body instanceof Object) {
try {
response.body = JSON.stringify(response.body);
} catch (error) { }
}
return response;
} catch (error) {
console.error(error);
return {
statusCode: 500,
body: JSON.stringify({ error: error.message }),
headers: { "Content-Type": "application/json", 'X-Powered-By': 'genezio' },
};
}
} else {
let body = null;
try {
body = JSON.parse(event.body);
} catch (error) {
return {
statusCode: 400,
body: JSON.stringify({
jsonrpc: "2.0",
error: { code: -1, message: "Invalid JSON-RPC request" },
id: 0,
}),
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' }
};
}
if (!body || !body.method || !body.params || !Number.isInteger(body.id)) {
return {
statusCode: 400,
body: JSON.stringify({
jsonrpc: "2.0",
error: { code: -1, message: "Invalid JSON-RPC request" },
id: 0,
}),
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' }
};
}
let method = null;
try {
const methodElems = body.method.split(".");
method = methodElems[1];
} catch (error) {
return {
statusCode: 400,
body: JSON.stringify({
jsonrpc: "2.0",
error: { code: -1, message: "Invalid Genezio JSON-RPC request" },
id: 0,
}),
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' }
};
}
if (!object[method]) {
return {
statusCode: 400,
body: JSON.stringify({
jsonrpc: "2.0",
error: { code: -1, message: "Method not found!" },
id: 0,
}),
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' }
};
}
const requestId = body.id;
const errorPromise = new Promise((resolve) => {
process.removeAllListeners("uncaughtException");
process.on("uncaughtException", async function (err) {
console.error(err);
await sendSentryError(err);
resolve({
statusCode: 200,
body: JSON.stringify({
jsonrpc: "2.0",
error: prepareForSerialization(err),
id: requestId,
}),
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' }
});
});
});
try {
const response = Promise.resolve(object[method](...(body.params || [])))
.then((result) => {
return {
statusCode: 200,
body: JSON.stringify({
jsonrpc: "2.0",
result: result,
error: null,
id: requestId,
}),
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' }
};
})
.catch(async (err) => {
console.error(err);
await sendSentryError(err);
return {
statusCode: 200,
body: JSON.stringify({
jsonrpc: "2.0",
error: prepareForSerialization(err),
id: requestId,
}),
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' }
};
});
const result = await Promise.race([errorPromise, response]);
return result;
} catch (err) {
console.error(err);
await sendSentryError(err);
return {
statusCode: 200,
body: JSON.stringify({
jsonrpc: "2.0",
error: prepareForSerialization(err),
id: requestId,
}),
headers: { 'Content-Type': 'application/json', 'X-Powered-By': 'genezio' }
};
}
}
};
}
export { handler };
`;
};