Files
ignis/server/routes/proxy.js
2026-03-12 22:32:39 +01:00

46 lines
1.2 KiB
JavaScript

const express = require("express");
const router = express.Router();
// POST /api/proxy - forward a request to an external URL (bypasses browser CORS)
// Used by the requestUrl shim for plugin installation, update checks, etc.
router.post("/", async (req, res) => {
const { url, method, headers, body, binary } = req.body;
if (!url) {
return res.status(400).json({ error: "Missing url" });
}
try {
const fetchOpts = {
method: method || "GET",
headers: headers || {},
};
if (body && method !== "GET" && method !== "HEAD") {
if (binary && typeof body === "string") {
fetchOpts.body = Buffer.from(body, "base64");
} else {
fetchOpts.body = body;
}
}
const upstream = await fetch(url, fetchOpts);
const respBody = Buffer.from(await upstream.arrayBuffer());
// Forward response headers
const respHeaders = {};
upstream.headers.forEach((val, key) => {
respHeaders[key] = val;
});
res.json({
status: upstream.status,
headers: respHeaders,
body: respBody.toString("base64"),
});
} catch (e) {
res.status(502).json({ error: e.message });
}
});
module.exports = router;