2021-03-09 23:09:35 +08:00
|
|
|
"use strict";
|
|
|
|
|
2021-11-24 06:31:54 +08:00
|
|
|
const express = require("express");
|
2021-03-09 23:09:35 +08:00
|
|
|
const cleanBaseURL = require("clean-base-url");
|
|
|
|
const path = require("path");
|
2022-08-22 16:56:39 +08:00
|
|
|
const fs = require("fs");
|
|
|
|
const fsPromises = fs.promises;
|
2021-12-02 00:10:40 +08:00
|
|
|
const { JSDOM } = require("jsdom");
|
2022-06-29 03:20:14 +08:00
|
|
|
const { Buffer } = require("node:buffer");
|
2023-11-10 19:16:06 +08:00
|
|
|
const { env } = require("node:process");
|
|
|
|
const { glob } = require("glob");
|
|
|
|
|
|
|
|
async function listDistAssets() {
|
|
|
|
const files = await glob("**/*.js", { nodir: true, cwd: "dist/assets" });
|
|
|
|
return new Set(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateScriptReferences({
|
|
|
|
chunkInfos,
|
|
|
|
dom,
|
|
|
|
selector,
|
|
|
|
attribute,
|
|
|
|
baseURL,
|
|
|
|
distAssets,
|
|
|
|
}) {
|
|
|
|
const elements = dom.window.document.querySelectorAll(selector);
|
|
|
|
const handledEntrypoints = new Set();
|
|
|
|
|
|
|
|
for (const el of elements) {
|
|
|
|
const entrypointName = el.dataset.discourseEntrypoint;
|
|
|
|
|
|
|
|
if (handledEntrypoints.has(entrypointName)) {
|
|
|
|
el.remove();
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-02 22:43:10 +08:00
|
|
|
|
2023-11-10 19:16:06 +08:00
|
|
|
let chunks = chunkInfos[`assets/${entrypointName}.js`]?.assets;
|
2021-07-29 21:01:11 +08:00
|
|
|
|
2023-11-10 19:16:06 +08:00
|
|
|
if (!chunks) {
|
|
|
|
if (distAssets.has(`${entrypointName}.js`)) {
|
|
|
|
chunks = [`assets/${entrypointName}.js`];
|
2021-03-09 23:09:35 +08:00
|
|
|
} else {
|
2023-11-10 19:16:06 +08:00
|
|
|
// Not an ember-cli asset, do not rewrite
|
|
|
|
continue;
|
2021-03-09 23:09:35 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-05 04:13:01 +08:00
|
|
|
|
2023-11-10 19:16:06 +08:00
|
|
|
const newElements = chunks.map((chunk) => {
|
|
|
|
const newElement = el.cloneNode(true);
|
|
|
|
newElement[attribute] = `${baseURL}${chunk}`;
|
|
|
|
newElement.dataset.emberCliRewritten = "true";
|
|
|
|
|
|
|
|
return newElement;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (
|
|
|
|
entrypointName === "discourse" &&
|
|
|
|
el.tagName.toLowerCase() === "script"
|
|
|
|
) {
|
|
|
|
const liveReload = dom.window.document.createElement("script");
|
|
|
|
liveReload.setAttribute("async", "");
|
|
|
|
liveReload.src = `${baseURL}ember-cli-live-reload.js`;
|
|
|
|
newElements.unshift(liveReload);
|
2022-06-20 10:37:28 +08:00
|
|
|
}
|
|
|
|
|
2023-11-10 19:16:06 +08:00
|
|
|
el.replaceWith(...newElements);
|
2021-03-09 23:09:35 +08:00
|
|
|
|
2023-11-10 19:16:06 +08:00
|
|
|
handledEntrypoints.add(entrypointName);
|
2021-11-24 06:31:54 +08:00
|
|
|
}
|
2021-03-09 23:09:35 +08:00
|
|
|
}
|
|
|
|
|
2021-11-24 06:31:54 +08:00
|
|
|
async function handleRequest(proxy, baseURL, req, res) {
|
2022-06-29 03:20:14 +08:00
|
|
|
// x-forwarded-host is used in e.g. GitHub CodeSpaces
|
|
|
|
let originalHost = req.headers["x-forwarded-host"] || req.headers.host;
|
|
|
|
|
|
|
|
if (env["FORWARD_HOST"] === "true") {
|
|
|
|
if (/^localhost(\:|$)/.test(originalHost)) {
|
|
|
|
// Can't access default site in multisite via "localhost", redirect to 127.0.0.1
|
|
|
|
res.redirect(
|
|
|
|
307,
|
|
|
|
`http://${originalHost.replace("localhost", "127.0.0.1")}${req.path}`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
req.headers.host = originalHost;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
req.headers.host = new URL(proxy).host;
|
|
|
|
}
|
2021-11-24 06:31:54 +08:00
|
|
|
|
|
|
|
if (req.headers["Origin"]) {
|
|
|
|
req.headers["Origin"] = req.headers["Origin"]
|
|
|
|
.replace(req.headers.host, originalHost)
|
|
|
|
.replace(/^https/, "http");
|
2021-04-28 02:40:17 +08:00
|
|
|
}
|
|
|
|
|
2021-11-24 06:31:54 +08:00
|
|
|
if (req.headers["Referer"]) {
|
|
|
|
req.headers["Referer"] = req.headers["Referer"]
|
|
|
|
.replace(req.headers.host, originalHost)
|
|
|
|
.replace(/^https/, "http");
|
|
|
|
}
|
2021-04-23 22:24:42 +08:00
|
|
|
|
2021-11-24 06:31:54 +08:00
|
|
|
let url = `${proxy}${req.path}`;
|
|
|
|
const queryLoc = req.url.indexOf("?");
|
|
|
|
if (queryLoc !== -1) {
|
2022-04-01 23:35:17 +08:00
|
|
|
url += req.url.slice(queryLoc);
|
2021-11-24 06:31:54 +08:00
|
|
|
}
|
2021-04-23 22:24:42 +08:00
|
|
|
|
2021-11-24 06:31:54 +08:00
|
|
|
if (req.method === "GET") {
|
|
|
|
req.headers["X-Discourse-Ember-CLI"] = "true";
|
|
|
|
}
|
|
|
|
|
2022-11-17 01:58:38 +08:00
|
|
|
const { default: fetch } = await import("node-fetch");
|
2021-12-01 00:40:32 +08:00
|
|
|
const response = await fetch(url, {
|
|
|
|
method: req.method,
|
|
|
|
body: /GET|HEAD/.test(req.method) ? null : req.body,
|
|
|
|
headers: req.headers,
|
2021-12-02 23:03:45 +08:00
|
|
|
redirect: "manual",
|
2021-12-01 00:40:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
response.headers.forEach((value, header) => {
|
2022-12-05 23:01:55 +08:00
|
|
|
if (header === "set-cookie") {
|
|
|
|
// Special handling to get array of multiple Set-Cookie header values
|
|
|
|
// per https://github.com/node-fetch/node-fetch/issues/251#issuecomment-428143940
|
|
|
|
res.set("set-cookie", response.headers.raw()["set-cookie"]);
|
|
|
|
} else {
|
|
|
|
res.set(header, value);
|
|
|
|
}
|
2021-12-01 00:40:32 +08:00
|
|
|
});
|
2021-11-24 06:31:54 +08:00
|
|
|
res.set("content-encoding", null);
|
|
|
|
|
2021-12-01 00:40:32 +08:00
|
|
|
const location = response.headers.get("location");
|
2021-11-24 06:31:54 +08:00
|
|
|
if (location) {
|
2021-11-24 19:52:25 +08:00
|
|
|
const newLocation = location.replace(proxy, `http://${originalHost}`);
|
2021-11-24 06:31:54 +08:00
|
|
|
res.set("location", newLocation);
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:40:32 +08:00
|
|
|
const csp = response.headers.get("content-security-policy");
|
2021-11-24 19:52:25 +08:00
|
|
|
if (csp) {
|
2021-12-02 00:10:40 +08:00
|
|
|
const emberCliAdditions = [
|
2023-06-12 18:49:26 +08:00
|
|
|
`http://${originalHost}${baseURL}assets/`,
|
|
|
|
`http://${originalHost}${baseURL}ember-cli-live-reload.js`,
|
|
|
|
`http://${originalHost}${baseURL}_lr/`,
|
2022-06-29 03:20:14 +08:00
|
|
|
].join(" ");
|
|
|
|
|
2021-12-02 00:10:40 +08:00
|
|
|
const newCSP = csp
|
2022-06-29 03:20:14 +08:00
|
|
|
.replaceAll(proxy, `http://${originalHost}`)
|
2022-08-16 00:56:23 +08:00
|
|
|
.replaceAll("script-src ", `script-src ${emberCliAdditions} `);
|
2022-06-29 03:20:14 +08:00
|
|
|
|
2021-11-24 19:52:25 +08:00
|
|
|
res.set("content-security-policy", newCSP);
|
|
|
|
}
|
|
|
|
|
2021-12-02 05:04:56 +08:00
|
|
|
const contentType = response.headers.get("content-type");
|
2022-06-29 03:20:14 +08:00
|
|
|
const isHTML = contentType?.startsWith("text/html");
|
|
|
|
|
|
|
|
res.status(response.status);
|
|
|
|
|
|
|
|
if (isHTML) {
|
2023-11-10 19:16:06 +08:00
|
|
|
const [responseText, chunkInfoText, distAssets] = await Promise.all([
|
|
|
|
response.text(),
|
|
|
|
fsPromises.readFile("dist/assets.json", "utf-8"),
|
|
|
|
listDistAssets(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const chunkInfos = JSON.parse(chunkInfoText);
|
|
|
|
|
|
|
|
const dom = new JSDOM(responseText);
|
|
|
|
|
|
|
|
updateScriptReferences({
|
|
|
|
chunkInfos,
|
|
|
|
dom,
|
|
|
|
selector: "script[data-discourse-entrypoint]",
|
|
|
|
attribute: "src",
|
|
|
|
baseURL,
|
|
|
|
distAssets,
|
|
|
|
});
|
|
|
|
|
|
|
|
updateScriptReferences({
|
|
|
|
chunkInfos,
|
|
|
|
dom,
|
|
|
|
selector: "link[rel=preload][data-discourse-entrypoint]",
|
|
|
|
attribute: "href",
|
|
|
|
baseURL,
|
|
|
|
distAssets,
|
|
|
|
});
|
|
|
|
|
|
|
|
res.send(dom.serialize());
|
2021-11-24 06:31:54 +08:00
|
|
|
} else {
|
2022-06-29 03:20:14 +08:00
|
|
|
res.send(Buffer.from(await response.arrayBuffer()));
|
2021-04-23 22:24:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 23:09:35 +08:00
|
|
|
module.exports = {
|
|
|
|
name: require("./package").name,
|
|
|
|
|
|
|
|
isDevelopingAddon() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
serverMiddleware(config) {
|
2021-11-24 06:31:54 +08:00
|
|
|
const app = config.app;
|
|
|
|
let { proxy, rootURL, baseURL } = config.options;
|
2021-03-09 23:09:35 +08:00
|
|
|
|
2021-04-23 22:24:42 +08:00
|
|
|
if (!proxy) {
|
2021-11-24 06:31:54 +08:00
|
|
|
// eslint-disable-next-line no-console
|
2021-04-23 22:24:42 +08:00
|
|
|
console.error(`
|
|
|
|
Discourse can't be run without a \`--proxy\` setting, because it needs a Rails application
|
|
|
|
to serve API requests. For example:
|
|
|
|
|
|
|
|
yarn run ember serve --proxy "http://localhost:3000"\n`);
|
|
|
|
throw "--proxy argument is required";
|
|
|
|
}
|
|
|
|
|
2021-11-24 06:31:54 +08:00
|
|
|
baseURL = rootURL === "" ? "/" : cleanBaseURL(rootURL || baseURL);
|
2021-03-09 23:09:35 +08:00
|
|
|
|
2021-11-25 07:45:55 +08:00
|
|
|
const rawMiddleware = express.raw({ type: () => true, limit: "100mb" });
|
2023-01-17 17:54:33 +08:00
|
|
|
const pathRestrictedRawMiddleware = (req, res, next) => {
|
|
|
|
if (this.shouldHandleRequest(req, baseURL)) {
|
|
|
|
return rawMiddleware(req, res, next);
|
|
|
|
} else {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
};
|
2021-11-24 19:52:25 +08:00
|
|
|
|
2022-09-12 21:46:50 +08:00
|
|
|
app.use(
|
|
|
|
"/favicon.ico",
|
2022-09-20 08:53:18 +08:00
|
|
|
express.static(
|
|
|
|
path.join(
|
|
|
|
__dirname,
|
|
|
|
"../../../../../../public/images/discourse-logo-sketch-small.png"
|
|
|
|
)
|
|
|
|
)
|
2022-09-12 21:46:50 +08:00
|
|
|
);
|
|
|
|
|
2023-01-17 17:54:33 +08:00
|
|
|
app.use(pathRestrictedRawMiddleware, async (req, res, next) => {
|
2021-03-09 23:09:35 +08:00
|
|
|
try {
|
2023-01-17 17:54:33 +08:00
|
|
|
if (this.shouldHandleRequest(req, baseURL)) {
|
2021-11-24 06:31:54 +08:00
|
|
|
await handleRequest(proxy, baseURL, req, res);
|
2022-06-29 03:20:14 +08:00
|
|
|
} else {
|
|
|
|
// Fixes issues when using e.g. "localhost" instead of loopback IP address
|
|
|
|
req.headers.host = "127.0.0.1";
|
2021-03-09 23:09:35 +08:00
|
|
|
}
|
2021-11-24 06:31:54 +08:00
|
|
|
} catch (error) {
|
|
|
|
res.send(`
|
|
|
|
<html>
|
2021-12-02 18:58:03 +08:00
|
|
|
<h1>Discourse Ember CLI Proxy Error</h1>
|
|
|
|
<pre><code>${error.stack}</code></pre>
|
2021-11-24 06:31:54 +08:00
|
|
|
</html>
|
|
|
|
`);
|
2021-03-09 23:09:35 +08:00
|
|
|
} finally {
|
2021-04-23 22:24:42 +08:00
|
|
|
if (!res.headersSent) {
|
2023-11-28 17:54:13 +08:00
|
|
|
next();
|
2021-04-15 03:25:49 +08:00
|
|
|
}
|
2021-03-09 23:09:35 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2023-01-17 17:54:33 +08:00
|
|
|
shouldHandleRequest(request, baseURL) {
|
2022-06-29 03:20:14 +08:00
|
|
|
if (
|
2022-07-11 19:14:44 +08:00
|
|
|
[
|
2022-12-02 08:33:56 +08:00
|
|
|
`${baseURL}tests/index.html`,
|
|
|
|
`${baseURL}ember-cli-live-reload.js`,
|
|
|
|
`${baseURL}testem.js`,
|
|
|
|
`${baseURL}assets/test-i18n.js`,
|
2022-07-11 19:14:44 +08:00
|
|
|
].includes(request.path)
|
2022-06-29 03:20:14 +08:00
|
|
|
) {
|
2021-11-24 22:34:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-02 01:43:33 +08:00
|
|
|
// All JS assets are served by Ember CLI, except for
|
|
|
|
// plugin assets which end in _extra.js
|
|
|
|
if (
|
|
|
|
request.path.startsWith(`${baseURL}assets/`) &&
|
|
|
|
!request.path.endsWith("_extra.js")
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-12 18:49:26 +08:00
|
|
|
if (request.path.startsWith(`${baseURL}_lr/`)) {
|
2021-11-25 01:54:15 +08:00
|
|
|
return false;
|
2021-03-09 23:09:35 +08:00
|
|
|
}
|
|
|
|
|
2023-01-17 17:54:33 +08:00
|
|
|
if (request.path.startsWith(`${baseURL}message-bus/`)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-29 03:20:14 +08:00
|
|
|
return true;
|
2021-03-09 23:09:35 +08:00
|
|
|
},
|
|
|
|
};
|