Try to make concurrent requests work

This commit is contained in:
Peter Stockings
2025-06-14 22:27:31 +10:00
parent f07f76797e
commit 7b8089e183
2 changed files with 128 additions and 182 deletions

View File

@@ -14,6 +14,7 @@
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"type": "module",
"dependencies": { "dependencies": {
"cheerio": "^1.0.0-rc.12", "cheerio": "^1.0.0-rc.12",
"express": "^4.18.2", "express": "^4.18.2",

309
server.js
View File

@@ -1,201 +1,146 @@
const express = require("express"); // speedy-functions.js
const bodyParser = require("body-parser"); import express from "express";
const { VM } = require("vm2"); import { VM, VMScript } from "vm2";
const { JSDOM } = require("jsdom"); import { JSDOM } from "jsdom";
const cheerio = require("cheerio"); import cheerio from "cheerio";
var FileReader = require("filereader"); import FileReader from "filereader";
const app = express(); // ────── 1. one-time constants ──────────────────────────────────────────
const port = 5000; const PORT = +(process.env.PORT ?? 5000);
const TIMEOUT_MS = 5_000;
app.use(bodyParser.json({ limit: "50mb" })); const HTTP_STATUS_CODES = Object.freeze({
OK: 200,
app.use((req, res, next) => { BAD_REQUEST: 400,
res.header("Access-Control-Allow-Origin", "*"); UNAUTHORIZED: 401,
res.header( FORBIDDEN: 403,
"Access-Control-Allow-Headers", NOT_FOUND: 404,
"Origin, X-Requested-With, Content-Type, Accept" INTERNAL_SERVER_ERROR: 500,
); BAD_GATEWAY: 502,
next(); SERVICE_UNAVAILABLE: 503,
GATEWAY_TIMEOUT: 504,
}); });
const TIMEOUT_MS = 5000; // Set timeout to 5000 milliseconds (5 seconds) const States = Object.freeze({
SUCCESS: "SUCCESS",
NOT_A_FUNCTION: "NOT_A_FUNCTION",
SCRIPT_ERROR: "ERROR",
TIMEOUT: "TIMEOUT",
});
// convenience helpers
const Response = (body = "", headers = {}, status = HTTP_STATUS_CODES.OK) => ({
body,
status,
headers,
});
const JsonResponse = (body = {}, headers = {}, status = HTTP_STATUS_CODES.OK) =>
Response(
JSON.stringify(body),
{ "Content-Type": "application/json", ...headers },
status
);
const HtmlResponse = (body = "", headers = {}, status = HTTP_STATUS_CODES.OK) =>
Response(body, { "Content-Type": "text/html", ...headers }, status);
const TextResponse = (body = "", headers = {}, status = HTTP_STATUS_CODES.OK) =>
Response(body, { "Content-Type": "text/plain", ...headers }, status);
// ────── 2. one-time imports ────────────────────────────────────────────
const fetch = (await import("node-fetch")).default;
// ────── 3. tiny script cache (precompiled) ─────────────────────────────
const scriptCache = new Map(); // code-string -> VMScript
const cachedScript = (code) => {
if (!scriptCache.has(code)) {
scriptCache.set(code, new VMScript(code));
}
return scriptCache.get(code);
};
// ────── 4. shared VM options (lightweight to clone) ────────────────────
const baseSandbox = {
fetch,
parseHTML: (html) => new JSDOM(html).window.document,
JSDOM,
cheerio,
HTTP_STATUS_CODES,
Response,
JsonResponse,
HtmlResponse,
TextResponse,
FileReader,
};
function createVm(logs, requestObject, env, funcName) {
return new VM({
timeout: TIMEOUT_MS,
sandbox: {
...baseSandbox,
console: {
log: (...args) => (
logs.push(args), /* bubble to host */ console.log(...args)
),
error: (...args) => (logs.push(args), console.error(...args)),
},
requestObject,
environment: env,
FUNCTION_NAME: funcName,
},
require: { external: true },
});
}
// ────── 5. evaluator ───────────────────────────────────────────────────
async function executeUserCode( async function executeUserCode(
code, code,
requestObject, requestObject,
environment = {}, env = {},
timeout = TIMEOUT_MS, funcName = "userFunc"
name
) { ) {
const logs = []; const logs = [];
const vm = createVm(
const States = { logs,
SUCCESS: "SUCCESS", requestObject,
NOT_A_FUNCTION: "NOT_A_FUNCTION", JSON.parse(JSON.stringify(env)),
SCRIPT_ERROR: "ERROR", funcName
TIMEOUT: "TIMEOUT", );
};
const HTTP_STATUS_CODES = {
OK: 200,
BAD_REQUEST: 400,
UNAUTHORIZED: 401,
FORBIDDEN: 403,
NOT_FOUND: 404,
INTERNAL_SERVER_ERROR: 500,
BAD_GATEWAY: 502,
SERVICE_UNAVAILABLE: 503,
GATEWAY_TIMEOUT: 504,
};
const Response = (
body = "",
headers = {},
status = HTTP_STATUS_CODES.OK
) => ({
body,
status,
headers,
});
const JsonResponse = (
body = {},
headers = {},
status = HTTP_STATUS_CODES.OK
) => ({
status,
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
...headers,
},
});
const HtmlResponse = (
body = "",
headers = {},
status = HTTP_STATUS_CODES.OK
) => ({
status,
body,
headers: {
"Content-Type": "text/html",
...headers,
},
});
const TextResponse = (
body = "",
headers = {},
status = HTTP_STATUS_CODES.OK
) => ({
status,
body,
headers: {
"Content-Type": "text/plain",
...headers,
},
});
const Result = (status, result, environment) => {
console.log(`Status: ${status}`);
//console.log(`Result: ${JSON.stringify(result, null, 2)}`);
//console.log(`Logs: ${JSON.stringify(logs, null, 2)}`);
//console.log(`Environment (post): ${JSON.stringify(environment, null, 2)}`);
//console.log(`\n`);
return {
status,
result,
logs,
environment,
};
};
//Set Function name as environment variable
const FUNCTION_NAME = name;
// Dynamically import node-fetch
const fetch = await import("node-fetch").then((module) => module.default);
const vm = new VM({
timeout,
sandbox: {
fetch,
parseHTML: async (html) => {
const dom = new JSDOM(html);
return dom.window.document;
},
console: {
log: (...args) => {
logs.push(args);
console.log(...args);
},
error: (...args) => {
logs.push(args);
console.error(...args);
},
},
requestObject,
environment,
JSDOM,
cheerio,
HTTP_STATUS_CODES,
Response,
JsonResponse,
HtmlResponse,
TextResponse,
FUNCTION_NAME,
FileReader,
},
require: {
external: true,
},
});
try { try {
// If the user code is a function that needs to be invoked const userFn = vm.run(cachedScript(code));
const userFunction = vm.run(code); if (typeof userFn !== "function") {
if (typeof userFunction === "function") { return {
console.log(`Function: ${code}`); status: States.NOT_A_FUNCTION,
let requestObjectString = JSON.stringify(requestObject, null, 2); result: null,
console.log(`Request: ${requestObjectString}`); logs,
console.log( environment: env,
`Environment (pre): ${JSON.stringify(environment, null, 2)})` };
);
// Call the user function with request object
let result = await userFunction(requestObject);
return Result(States.SUCCESS, result, environment);
} else {
return Result(States.NOT_A_FUNCTION, null, environment);
} }
const result = await Promise.resolve(userFn(requestObject));
return { status: States.SUCCESS, result, logs, environment: env };
} catch (err) { } catch (err) {
if (err.message === "Script execution timed out.") { const status = /timed out/i.test(err.message)
return Result(States.TIMEOUT, null, environment); ? States.TIMEOUT
} else { : States.SCRIPT_ERROR;
return Result(States.SCRIPT_ERROR, err.message || err, environment); return { status, result: err.message ?? err, logs, environment: env };
}
} }
} }
app.post("/execute", async (req, res) => { // ────── 6. API surface ────────────────────────────────────────────────
const { code, request, environment, name } = req.body; const app = express();
app.use(express.json({ limit: "50mb" }));
const timeout = req.query.timeout || TIMEOUT_MS; app.use((_, res, next) => {
res.set({
const result = await executeUserCode( "Access-Control-Allow-Origin": "*",
code, "Access-Control-Allow-Headers":
request, "Origin, X-Requested-With, Content-Type, Accept",
environment, });
timeout, next();
name
);
res.send(result);
}); });
app.listen(port, () => { app.post("/execute", async ({ body }, res) => {
console.log(`Server listening on port: ${port}`); const { code = "", request = {}, environment = {}, name } = body;
const payload = await executeUserCode(code, request, environment, name);
res.json(payload);
}); });
app.listen(PORT, () => console.log(`⚡ server ready on :${PORT}`));