mirror of
https://github.com/Nystik-gh/ignis.git
synced 2026-06-17 04:35:53 +00:00
48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
const express = require("express");
|
|
|
|
const router = express.Router();
|
|
|
|
// POST /api/proxy - forward a request to an external URL to bypass CORS
|
|
// Used by the requestUrl shim for plugin installation, 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;
|