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 { encode } = require("html-entities");
|
|
|
|
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-11-16 18:07:58 +08:00
|
|
|
const { shouldLoadPluginTestJs } = require("discourse-plugins");
|
2022-06-29 03:20:14 +08:00
|
|
|
const { Buffer } = require("node:buffer");
|
|
|
|
const { cwd, env } = require("node:process");
|
2021-05-07 21:59:45 +08:00
|
|
|
|
|
|
|
// via https://stackoverflow.com/a/6248722/165668
|
|
|
|
function generateUID() {
|
|
|
|
let firstPart = (Math.random() * 46656) | 0; // eslint-disable-line no-bitwise
|
|
|
|
let secondPart = (Math.random() * 46656) | 0; // eslint-disable-line no-bitwise
|
|
|
|
firstPart = ("000" + firstPart.toString(36)).slice(-3);
|
|
|
|
secondPart = ("000" + secondPart.toString(36)).slice(-3);
|
|
|
|
return firstPart + secondPart;
|
|
|
|
}
|
2021-03-09 23:09:35 +08:00
|
|
|
|
|
|
|
function htmlTag(buffer, bootstrap) {
|
|
|
|
let classList = "";
|
|
|
|
if (bootstrap.html_classes) {
|
|
|
|
classList = ` class="${bootstrap.html_classes}"`;
|
|
|
|
}
|
|
|
|
buffer.push(`<html lang="${bootstrap.html_lang}"${classList}>`);
|
|
|
|
}
|
|
|
|
|
2021-10-05 04:13:01 +08:00
|
|
|
function head(buffer, bootstrap, headers, baseURL) {
|
2021-03-09 23:09:35 +08:00
|
|
|
if (bootstrap.csrf_token) {
|
2021-04-30 18:26:48 +08:00
|
|
|
buffer.push(`<meta name="csrf-param" content="authenticity_token">`);
|
|
|
|
buffer.push(`<meta name="csrf-token" content="${bootstrap.csrf_token}">`);
|
2021-03-09 23:09:35 +08:00
|
|
|
}
|
2021-07-02 22:43:10 +08:00
|
|
|
|
2021-06-18 10:16:26 +08:00
|
|
|
if (bootstrap.theme_id) {
|
2021-03-09 23:09:35 +08:00
|
|
|
buffer.push(
|
2021-06-18 10:16:26 +08:00
|
|
|
`<meta name="discourse_theme_id" content="${bootstrap.theme_id}">`
|
2021-03-09 23:09:35 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-02 22:43:10 +08:00
|
|
|
if (bootstrap.theme_color) {
|
|
|
|
buffer.push(`<meta name="theme-color" content="${bootstrap.theme_color}">`);
|
|
|
|
}
|
|
|
|
|
2021-07-29 21:01:11 +08:00
|
|
|
if (bootstrap.authentication_data) {
|
|
|
|
buffer.push(
|
|
|
|
`<meta id="data-authentication" data-authentication-data="${encode(
|
|
|
|
bootstrap.authentication_data
|
|
|
|
)}">`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-09 23:09:35 +08:00
|
|
|
let setupData = "";
|
|
|
|
Object.keys(bootstrap.setup_data).forEach((sd) => {
|
|
|
|
let val = bootstrap.setup_data[sd];
|
|
|
|
if (val) {
|
|
|
|
if (Array.isArray(val)) {
|
|
|
|
val = JSON.stringify(val);
|
|
|
|
} else {
|
|
|
|
val = val.toString();
|
|
|
|
}
|
|
|
|
setupData += ` data-${sd.replace(/\_/g, "-")}="${encode(val)}"`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
buffer.push(`<meta id="data-discourse-setup"${setupData} />`);
|
|
|
|
|
2021-10-05 04:13:01 +08:00
|
|
|
if (bootstrap.preloaded.currentUser) {
|
2022-06-17 20:50:21 +08:00
|
|
|
const user = JSON.parse(bootstrap.preloaded.currentUser);
|
|
|
|
let { admin, staff } = user;
|
|
|
|
|
2021-10-05 04:13:01 +08:00
|
|
|
if (staff) {
|
2022-06-20 09:47:37 +08:00
|
|
|
buffer.push(`<script defer src="${baseURL}assets/admin.js"></script>`);
|
2021-10-05 04:13:01 +08:00
|
|
|
}
|
2022-06-17 20:50:21 +08:00
|
|
|
|
|
|
|
if (admin) {
|
2022-06-20 16:59:42 +08:00
|
|
|
buffer.push(`<script defer src="${baseURL}assets/wizard.js"></script>`);
|
2022-06-17 20:50:21 +08:00
|
|
|
}
|
2021-10-05 04:13:01 +08:00
|
|
|
}
|
|
|
|
|
2021-03-09 23:09:35 +08:00
|
|
|
bootstrap.plugin_js.forEach((src) =>
|
2022-06-20 09:47:37 +08:00
|
|
|
buffer.push(`<script defer src="${src}"></script>`)
|
2021-03-09 23:09:35 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
buffer.push(bootstrap.theme_html.translations);
|
|
|
|
buffer.push(bootstrap.theme_html.js);
|
|
|
|
buffer.push(bootstrap.theme_html.head_tag);
|
|
|
|
buffer.push(bootstrap.html.before_head_close);
|
|
|
|
}
|
|
|
|
|
2021-03-12 03:40:25 +08:00
|
|
|
function localeScript(buffer, bootstrap) {
|
2022-06-20 09:47:37 +08:00
|
|
|
buffer.push(`<script defer src="${bootstrap.locale_script}"></script>`);
|
2021-03-12 03:40:25 +08:00
|
|
|
}
|
|
|
|
|
2021-03-09 23:09:35 +08:00
|
|
|
function beforeScriptLoad(buffer, bootstrap) {
|
|
|
|
buffer.push(bootstrap.html.before_script_load);
|
2021-03-12 03:40:25 +08:00
|
|
|
localeScript(buffer, bootstrap);
|
2021-03-09 23:09:35 +08:00
|
|
|
(bootstrap.extra_locales || []).forEach((l) =>
|
2022-06-20 09:47:37 +08:00
|
|
|
buffer.push(`<script defer src="${l}"></script>`)
|
2021-03-09 23:09:35 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-05 00:23:09 +08:00
|
|
|
function discoursePreloadStylesheets(buffer, bootstrap) {
|
|
|
|
(bootstrap.stylesheets || []).forEach((s) => {
|
|
|
|
let link = `<link rel="preload" as="style" href="${s.href}">`;
|
|
|
|
buffer.push(link);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-20 10:37:28 +08:00
|
|
|
function discourseStylesheets(buffer, bootstrap) {
|
|
|
|
(bootstrap.stylesheets || []).forEach((s) => {
|
|
|
|
let attrs = [];
|
|
|
|
if (s.media) {
|
|
|
|
attrs.push(`media="${s.media}"`);
|
|
|
|
}
|
|
|
|
if (s.target) {
|
|
|
|
attrs.push(`data-target="${s.target}"`);
|
|
|
|
}
|
|
|
|
if (s.theme_id) {
|
|
|
|
attrs.push(`data-theme-id="${s.theme_id}"`);
|
|
|
|
}
|
|
|
|
if (s.class) {
|
|
|
|
attrs.push(`class="${s.class}"`);
|
|
|
|
}
|
|
|
|
let link = `<link rel="stylesheet" type="text/css" href="${
|
|
|
|
s.href
|
|
|
|
}" ${attrs.join(" ")}>`;
|
|
|
|
buffer.push(link);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-09 23:09:35 +08:00
|
|
|
function body(buffer, bootstrap) {
|
|
|
|
buffer.push(bootstrap.theme_html.header);
|
|
|
|
buffer.push(bootstrap.html.header);
|
|
|
|
}
|
|
|
|
|
2021-05-07 20:49:47 +08:00
|
|
|
function bodyFooter(buffer, bootstrap, headers) {
|
2021-03-09 23:09:35 +08:00
|
|
|
buffer.push(bootstrap.theme_html.body_tag);
|
|
|
|
buffer.push(bootstrap.html.before_body_close);
|
2021-05-06 23:54:17 +08:00
|
|
|
|
2021-05-07 21:59:45 +08:00
|
|
|
let v = generateUID();
|
2021-05-06 23:54:17 +08:00
|
|
|
buffer.push(`
|
2022-07-14 19:03:43 +08:00
|
|
|
<script
|
|
|
|
async
|
|
|
|
type="text/javascript"
|
|
|
|
id="mini-profiler"
|
|
|
|
src="/mini-profiler-resources/includes.js?v=${v}"
|
|
|
|
data-css-url="/mini-profiler-resources/includes.css?v=${v}"
|
|
|
|
data-version="${v}"
|
|
|
|
data-path="/mini-profiler-resources/"
|
|
|
|
data-horizontal-position="right"
|
|
|
|
data-vertical-position="top"
|
|
|
|
data-trivial="false"
|
|
|
|
data-children="false"
|
|
|
|
data-max-traces="20"
|
|
|
|
data-controls="false"
|
|
|
|
data-total-sql-count="false"
|
|
|
|
data-authorized="true"
|
|
|
|
data-toggle-shortcut="alt+p"
|
|
|
|
data-start-hidden="false"
|
|
|
|
data-collapse-results="true"
|
|
|
|
data-html-container="body"
|
|
|
|
data-hidden-custom-fields="x"
|
|
|
|
data-ids="${headers.get("x-miniprofiler-ids")}"
|
|
|
|
></script>
|
|
|
|
`);
|
2021-03-09 23:09:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function hiddenLoginForm(buffer, bootstrap) {
|
|
|
|
if (!bootstrap.preloaded.currentUser) {
|
|
|
|
buffer.push(`
|
|
|
|
<form id='hidden-login-form' method="post" action="${bootstrap.login_path}" style="display: none;">
|
|
|
|
<input name="username" type="text" id="signin_username">
|
|
|
|
<input name="password" type="password" id="signin_password">
|
|
|
|
<input name="redirect" type="hidden">
|
|
|
|
<input type="submit" id="signin-button">
|
|
|
|
</form>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function preloaded(buffer, bootstrap) {
|
|
|
|
buffer.push(
|
|
|
|
`<div class="hidden" id="data-preloaded" data-preloaded="${encode(
|
|
|
|
JSON.stringify(bootstrap.preloaded)
|
|
|
|
)}"></div>`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const BUILDERS = {
|
|
|
|
"html-tag": htmlTag,
|
|
|
|
"before-script-load": beforeScriptLoad,
|
2022-07-05 00:23:09 +08:00
|
|
|
"discourse-preload-stylesheets": discoursePreloadStylesheets,
|
2021-06-17 01:45:02 +08:00
|
|
|
head,
|
|
|
|
body,
|
2022-06-20 10:37:28 +08:00
|
|
|
"discourse-stylesheets": discourseStylesheets,
|
2021-03-09 23:09:35 +08:00
|
|
|
"hidden-login-form": hiddenLoginForm,
|
2021-06-17 01:45:02 +08:00
|
|
|
preloaded,
|
2021-03-09 23:09:35 +08:00
|
|
|
"body-footer": bodyFooter,
|
2021-03-12 03:40:25 +08:00
|
|
|
"locale-script": localeScript,
|
2021-03-09 23:09:35 +08:00
|
|
|
};
|
|
|
|
|
2021-10-05 04:13:01 +08:00
|
|
|
function replaceIn(bootstrap, template, id, headers, baseURL) {
|
2021-03-09 23:09:35 +08:00
|
|
|
let buffer = [];
|
2021-10-05 04:13:01 +08:00
|
|
|
BUILDERS[id](buffer, bootstrap, headers, baseURL);
|
2021-03-09 23:09:35 +08:00
|
|
|
let contents = buffer.filter((b) => b && b.length > 0).join("\n");
|
|
|
|
|
2021-03-19 21:32:46 +08:00
|
|
|
return template.replace(`<bootstrap-content key="${id}">`, contents);
|
2021-03-09 23:09:35 +08:00
|
|
|
}
|
|
|
|
|
2021-12-02 00:10:40 +08:00
|
|
|
function extractPreloadJson(html) {
|
|
|
|
const dom = new JSDOM(html);
|
2021-12-02 05:04:56 +08:00
|
|
|
const dataElement = dom.window.document.querySelector("#data-preloaded");
|
|
|
|
|
|
|
|
if (!dataElement || !dataElement.dataset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataElement.dataset.preloaded;
|
2021-12-02 00:10:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function applyBootstrap(bootstrap, template, response, baseURL, preload) {
|
|
|
|
bootstrap.preloaded = Object.assign(JSON.parse(preload), bootstrap.preloaded);
|
2021-06-17 01:45:02 +08:00
|
|
|
|
2021-03-09 23:09:35 +08:00
|
|
|
Object.keys(BUILDERS).forEach((id) => {
|
2021-12-01 00:40:32 +08:00
|
|
|
template = replaceIn(bootstrap, template, id, response.headers, baseURL);
|
2021-03-09 23:09:35 +08:00
|
|
|
});
|
|
|
|
return template;
|
|
|
|
}
|
|
|
|
|
2021-12-02 00:10:40 +08:00
|
|
|
async function buildFromBootstrap(proxy, baseURL, req, response, preload) {
|
2021-11-24 06:31:54 +08:00
|
|
|
try {
|
2022-08-22 16:56:39 +08:00
|
|
|
const template = await fsPromises.readFile(
|
2022-06-29 03:20:14 +08:00
|
|
|
path.join(cwd(), "dist", "index.html"),
|
2021-11-24 06:31:54 +08:00
|
|
|
"utf8"
|
|
|
|
);
|
2021-05-18 02:51:36 +08:00
|
|
|
|
2021-12-02 00:10:40 +08:00
|
|
|
let url = new URL(`${proxy}${baseURL}bootstrap.json`);
|
|
|
|
url.searchParams.append("for_url", req.url);
|
2021-11-24 06:31:54 +08:00
|
|
|
|
2022-08-15 21:43:43 +08:00
|
|
|
const forUrlSearchParams = new URL(req.url, "https://dummy-origin.invalid")
|
|
|
|
.searchParams;
|
2022-08-16 00:56:38 +08:00
|
|
|
|
|
|
|
const mobileView = forUrlSearchParams.get("mobile_view");
|
|
|
|
if (mobileView) {
|
|
|
|
url.searchParams.append("mobile_view", mobileView);
|
|
|
|
}
|
|
|
|
|
2022-08-15 21:43:43 +08:00
|
|
|
const reqUrlSafeMode = forUrlSearchParams.get("safe_mode");
|
|
|
|
if (reqUrlSafeMode) {
|
|
|
|
url.searchParams.append("safe_mode", reqUrlSafeMode);
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:59:56 +08:00
|
|
|
const navigationMenu = forUrlSearchParams.get("navigation_menu");
|
|
|
|
if (navigationMenu) {
|
|
|
|
url.searchParams.append("navigation_menu", navigationMenu);
|
2023-01-13 06:47:58 +08:00
|
|
|
}
|
|
|
|
|
2022-08-15 21:43:43 +08:00
|
|
|
const reqUrlPreviewThemeId = forUrlSearchParams.get("preview_theme_id");
|
2022-08-16 00:56:38 +08:00
|
|
|
if (reqUrlPreviewThemeId) {
|
|
|
|
url.searchParams.append("preview_theme_id", reqUrlPreviewThemeId);
|
|
|
|
}
|
2022-08-15 21:43:43 +08:00
|
|
|
|
2022-11-17 01:58:38 +08:00
|
|
|
const { default: fetch } = await import("node-fetch");
|
2021-12-01 00:40:32 +08:00
|
|
|
const res = await fetch(url, { headers: req.headers });
|
|
|
|
const json = await res.json();
|
2021-11-24 06:31:54 +08:00
|
|
|
|
2021-12-02 00:10:40 +08:00
|
|
|
return applyBootstrap(json.bootstrap, template, response, baseURL, preload);
|
2021-11-24 06:31:54 +08:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(
|
|
|
|
`Could not get ${proxy}${baseURL}bootstrap.json\n\n${error}`
|
2021-03-09 23:09:35 +08:00
|
|
|
);
|
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 = [
|
|
|
|
`http://${originalHost}/assets/`,
|
|
|
|
`http://${originalHost}/ember-cli-live-reload.js`,
|
|
|
|
`http://${originalHost}/_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) {
|
|
|
|
const responseText = await response.text();
|
|
|
|
const preloadJson = isHTML ? extractPreloadJson(responseText) : null;
|
|
|
|
|
|
|
|
if (preloadJson) {
|
|
|
|
const html = await buildFromBootstrap(
|
|
|
|
proxy,
|
|
|
|
baseURL,
|
|
|
|
req,
|
|
|
|
response,
|
|
|
|
extractPreloadJson(responseText)
|
|
|
|
);
|
|
|
|
res.set("content-type", "text/html");
|
|
|
|
res.send(html);
|
|
|
|
} else {
|
|
|
|
res.send(responseText);
|
|
|
|
}
|
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;
|
|
|
|
},
|
|
|
|
|
2022-06-29 03:20:14 +08:00
|
|
|
contentFor(type, config) {
|
2022-01-12 05:06:48 +08:00
|
|
|
if (shouldLoadPluginTestJs() && type === "test-plugin-js") {
|
2022-08-22 16:56:39 +08:00
|
|
|
const scripts = [];
|
|
|
|
|
2022-09-21 19:38:02 +08:00
|
|
|
const pluginInfos = this.app.project
|
|
|
|
.findAddonByName("discourse-plugins")
|
|
|
|
.pluginInfos();
|
|
|
|
|
|
|
|
for (const {
|
|
|
|
pluginName,
|
|
|
|
directoryName,
|
|
|
|
hasJs,
|
|
|
|
hasAdminJs,
|
|
|
|
} of pluginInfos) {
|
|
|
|
if (hasJs) {
|
|
|
|
scripts.push({
|
|
|
|
src: `plugins/${directoryName}.js`,
|
|
|
|
name: pluginName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.existsSync(`../plugins/${directoryName}_extras.js.erb`)) {
|
|
|
|
scripts.push({
|
|
|
|
src: `plugins/${directoryName}_extras.js`,
|
|
|
|
name: pluginName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasAdminJs) {
|
|
|
|
scripts.push({
|
|
|
|
src: `plugins/${directoryName}_admin.js`,
|
|
|
|
name: pluginName,
|
|
|
|
});
|
2022-08-22 16:56:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return scripts
|
2022-08-23 17:25:07 +08:00
|
|
|
.map(
|
|
|
|
({ src, name }) =>
|
|
|
|
`<script src="${config.rootURL}assets/${src}" data-discourse-plugin="${name}"></script>`
|
|
|
|
)
|
2022-08-22 16:56:39 +08:00
|
|
|
.join("\n");
|
2022-01-12 05:06:48 +08:00
|
|
|
} else if (shouldLoadPluginTestJs() && type === "test-plugin-tests-js") {
|
2022-09-21 19:38:02 +08:00
|
|
|
return this.app.project
|
|
|
|
.findAddonByName("discourse-plugins")
|
|
|
|
.pluginInfos()
|
|
|
|
.filter(({ hasTests }) => hasTests)
|
|
|
|
.map(
|
|
|
|
({ directoryName, pluginName }) =>
|
|
|
|
`<script src="${config.rootURL}assets/plugins/test/${directoryName}_tests.js" data-discourse-plugin="${pluginName}"></script>`
|
|
|
|
)
|
|
|
|
.join("\n");
|
2022-10-20 01:10:06 +08:00
|
|
|
} else if (shouldLoadPluginTestJs() && type === "test-plugin-css") {
|
|
|
|
return `<link rel="stylesheet" href="${config.rootURL}bootstrap/plugin-css-for-tests.css" data-discourse-plugin="_all" />`;
|
2022-01-12 05:06:48 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-03-09 23:09:35 +08:00
|
|
|
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) {
|
2021-04-15 03:25:49 +08:00
|
|
|
return next();
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-06-29 03:20:14 +08:00
|
|
|
if (request.path.startsWith("/_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
|
|
|
},
|
|
|
|
};
|