Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

12.6 KB
Raw Blame
import express from "express";
import process from "process";
import compression from "compression";
import fs from "node:fs/promises";
import {createHash} from "node:crypto";
import sirv from "sirv";
import cookieParser from "cookie-parser";
import path from "node:path";
import {fileURLToPath} from "node:url";
import * as nodePath from "node:path";
import * as nodeUrl from "node:url";
import "dotenv/config";
import * as Sentry from "@sentry/node";
import {complianceHandler} from "./src/compliance/proxy.js";
import {sitemapIndexHandler, sitemapEventsHandler, sitemapOrganizersHandler} from "./src/sitemap/proxy.js";
import {htmlSafeJsonStringify} from "./src/utilites/safeScriptJson.js";
import {backendRequestHeaders, licenceSimulation} from "./src/ssr/backendRequestHeaders.js";
import {loggableError} from "./src/ssr/loggableError.js";
import {routeStylesheets} from "./src/ssr/routeStylesheets.js";

async function main() {
    const base = process.env.BASE || "/";
    const port = process.argv.includes("--port")
        ? process.argv[process.argv.indexOf("--port") + 1]
        : process.env.NODE_PORT || 5678;
    const isProduction = process.env.NODE_ENV === "production";

    const __dirname = path.dirname(fileURLToPath(import.meta.url));

    const templateHtml = isProduction
        ? await fs.readFile("./dist/client/index.html", "utf-8")
        : "";

    const clientManifest = isProduction
        ? JSON.parse(await fs.readFile("./dist/client/.vite/manifest.json", "utf-8").catch(() => "{}"))
        : {};

    const app = express();
    app.use(cookieParser());

    app.use('/.well-known', express.static(path.join(__dirname, 'public/.well-known')));

    const widgetPath = isProduction
        ? path.join(__dirname, './dist/client/widget.js')
        : path.join(__dirname, './public/widget.js');
    const loadWidgetScript = async () => {
        const body = await fs.readFile(widgetPath, 'utf-8');
        return {body, etag: `"${createHash('sha1').update(body).digest('base64url')}"`};
    };
    const productionWidgetScript = isProduction ? loadWidgetScript().catch(() => null) : null;

    app.get('/widget.js', compression(), async (req, res) => {
        try {
            const script = isProduction ? await productionWidgetScript : await loadWidgetScript();
            if (!script) {
                return res.status(404).send('');
            }
            res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
            res.setHeader('Cache-Control', isProduction
                ? 'public, max-age=300, stale-while-revalidate=86400'
                : 'no-cache');
            res.setHeader('ETag', script.etag);
            res.setHeader('Access-Control-Allow-Origin', '*');
            res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');
            if (req.fresh) {
                return res.status(304).end();
            }
            return res.status(200).send(script.body);
        } catch (error) {
            return res.status(404).send('');
        }
    });

    const widgetTestPageEnabled = !isProduction || process.env.WIDGET_TEST_PAGE_ENABLED === 'true';

    if (widgetTestPageEnabled) {
        app.get('/widget-test', async (req, res) => {
            try {
                const widgetTestHtml = await fs.readFile(path.join(__dirname, './src/widget-test/index.html'), 'utf-8');
                res.setHeader('Content-Type', 'text/html; charset=utf-8');
                res.setHeader('Cache-Control', 'no-cache');
                res.setHeader('X-Robots-Tag', 'noindex');
                return res.status(200).send(widgetTestHtml);
            } catch (error) {
                return res.status(404).send('');
            }
        });
    }

    let vite;

    if (!isProduction) {
        const {createServer: viteServer} = await import("vite");

        vite = await viteServer({
            server: { middlewareMode: true },
            appType: "custom",
            base,
        });

        app.use(vite.middlewares);
    } else {
        app.use(compression());
        app.use(base, sirv(path.join(__dirname, "./dist/client"), { extensions: [] }));
    }

    const googleConsentDefaults = (consentCookie) => {
        const consent = new URLSearchParams(typeof consentCookie === 'string' ? consentCookie : '');
        if (!consent.has('analytics') && !consent.has('advertising')) {
            return "ad_storage:'denied',ad_user_data:'denied',ad_personalization:'denied',analytics_storage:'denied',wait_for_update:500";
        }
        const advertising = consent.get('advertising') === '1' ? 'granted' : 'denied';
        const analytics = consent.get('analytics') === '1' ? 'granted' : 'denied';
        return `ad_storage:'${advertising}',ad_user_data:'${advertising}',ad_personalization:'${advertising}',analytics_storage:'${analytics}'`;
    };

    const getViteEnvironmentVariables = () => {
        const envVars = {};
        for (const key in process.env) {
            if (key.startsWith('VITE_')) {
                envVars[key] = process.env[key];
            }
        }
        return htmlSafeJsonStringify(envVars);
    };

    app.get('/robots.txt', (req, res) => {
        const frontendUrl = process.env.VITE_FRONTEND_URL || `${req.protocol}://${req.get('host')}`;
        const robotsTxt = `User-agent: *
Allow: /

Sitemap: ${frontendUrl}/sitemap.xml
`;
        res.setHeader('Content-Type', 'text/plain');
        res.setHeader('Cache-Control', 'public, max-age=86400');
        res.status(200).send(robotsTxt);
    });

    app.get('/compliance', complianceHandler);

    app.get('/sitemap.xml', sitemapIndexHandler);
    app.get('/sitemap-events-:page.xml', sitemapEventsHandler);
    app.get('/sitemap-organizers-:page.xml', sitemapOrganizersHandler);

    const nonRenderablePathPattern = /(^|\/)\.[^/]|\.(php|asp|aspx|jsp|cgi|sql|bak|old|zip|tar|gz|rar|7z|env|ini|yml|yaml|conf|log|sh|exe|dll)$/i;

    app.use(async (req, res, next) => {
        if (nonRenderablePathPattern.test(req.originalUrl.split("?")[0])) {
            return res.status(404).type("text/plain").send("Not Found");
        }

        return next();
    });

    const frameProtectedPathPattern = /^\/(auth|manage|account|profile|settings|welcome|admin|sso)(\/|$)/;
    const homepagePreviewPathPattern = /^\/(event|organizer)\/[^/]+\/preview\/*$/;
    const widgetPathPattern = /^\/widget\//;
    const credentialInUrlPathPattern = /^\/(sso\/callback|auth\/accept-invitation\/)/;
    const ssoCallbackPathPattern = /^\/sso\/callback\/?$/;
    const requestPathOf = (req) => {
        const path = req.originalUrl.replace(base, "/").split("?")[0];
        try {
            return decodeURIComponent(path).toLowerCase();
        } catch {
            return path.toLowerCase();
        }
    };

    app.use((req, res, next) => {
        const requestPath = requestPathOf(req);

        if (frameProtectedPathPattern.test(requestPath) || homepagePreviewPathPattern.test(requestPath)) {
            res.setHeader("Content-Security-Policy", "frame-ancestors 'self'");
            res.setHeader("X-Frame-Options", "SAMEORIGIN");
        }

        if (widgetPathPattern.test(requestPath)) {
            res.setHeader("X-Robots-Tag", "noindex, indexifembedded");
        }

        if (credentialInUrlPathPattern.test(requestPath)) {
            res.setHeader("Referrer-Policy", "no-referrer");
            res.setHeader("Cache-Control", "no-store");
        }

        return next();
    });

    app.use(async (req, res) => {
        const url = req.originalUrl.replace(base, "");

        try {
            let template;
            let render;

            if (!isProduction) {
                template = await fs.readFile(path.join(__dirname, "./index.html"), "utf-8");
                template = await vite.transformIndexHtml(url, template);
                render = (await vite.ssrLoadModule("/src/entry.server.tsx")).render;
            } else {
                template = templateHtml;
                render = (await dynamicImport(path.join(__dirname, "./dist/server/entry.server.js"))).render;
            }

            const { appHtml, dehydratedState, helmetContext, themeColors, htmlLang, statusCode, renderErrors, stylesheets } = await render(
                {
                    req,
                    res,
                    backendHeaders: backendRequestHeaders(req),
                    licenceSimulation: licenceSimulation(req),
                    stylesheetsFor: (entries) => routeStylesheets(clientManifest, entries),
                }
            );

            if (statusCode >= 500) {
                renderErrors.forEach((renderError) => {
                    Sentry.captureException(renderError, {
                        tags: { source: "ssr-loader" },
                        extra: { url: req.originalUrl },
                    });
                });
            }
            const stringifiedState = htmlSafeJsonStringify(dehydratedState);
            const stringifiedThemeColors = htmlSafeJsonStringify(themeColors);

            const helmetHtml = Object.values(helmetContext.helmet || {})
                .map((value) => value.toString() || "")
                .join(" ");

            const envVariablesHtml = `<script>window.hievents = ${getViteEnvironmentVariables()};</script>`;

            const headSnippets = [];
            const isSsoCallback = ssoCallbackPathPattern.test(requestPathOf(req));
            if (process.env.VITE_COOKIE_CONSENT_ENABLED === 'true' && !isSsoCallback) {
                headSnippets.push(`<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('consent','default',{${googleConsentDefaults(req.cookies?.hi_cookie_consent)}});</script>`);
            }
            if (process.env.VITE_GOOGLE_ADS_CONVERSION_ID && !isSsoCallback) {
                const conversionId = encodeURIComponent(process.env.VITE_GOOGLE_ADS_CONVERSION_ID);
                headSnippets.push(`
                <script async src="https://www.googletagmanager.com/gtag/js?id=${conversionId}"></script>
                <script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js',new Date());gtag('config','${conversionId}');</script>
            `);
            }
            if (process.env.VITE_FATHOM_SITE_ID && !isSsoCallback) {
                headSnippets.push(`
                <script src="https://cdn.usefathom.com/script.js" data-spa="auto" data-site="${encodeURIComponent(process.env.VITE_FATHOM_SITE_ID)}" defer></script>
            `);
            }

            const stylesheetLinks = stylesheets
                .map((file) => `<link rel="stylesheet" crossorigin href="${base}${file}">`)
                .join("");

            const html = template
                .replace('<html lang="en">', () => `<html lang="${htmlLang}">`)
                .replace("</head>", () => `${stylesheetLinks}</head>`)
                .replace("<!--head-snippets-->", () => headSnippets.join("\n"))
                .replace("<!--app-html-->", () => appHtml)
                .replace("<!--dehydrated-state-->", () => `<script>window.__REHYDRATED_STATE__ = ${stringifiedState};window.__THEME_COLORS__ = ${stringifiedThemeColors};window.__SSR_STYLED__ = ${stylesheets.length > 0}</script>`)
                .replace("<!--environment-variables-->", () => envVariablesHtml)
                .replace(/<!--render-helmet-->.*?<!--\/render-helmet-->/s, () => helmetHtml);

            res.setHeader("Content-Type", "text/html");
            return res.status(statusCode || 200).end(html);
        } catch (error) {
            if (error instanceof Response) {
                if (error.status >= 300 && error.status < 400) {
                    return res.redirect(error.status, error.headers.get("Location") || "/");
                } else {
                    return res.status(error.status).send(await error.text());
                }
            }

            Sentry.captureException(error, {
                tags: { source: "ssr-render" },
                extra: { url: req.originalUrl },
            });
            console.error(loggableError(error));
            res.status(500).send("Internal Server Error");
        }
    });

    Sentry.setupExpressErrorHandler(app);

    app.use((error, req, res, _next) => {
        console.error(loggableError(error));

        if (res.headersSent) {
            return res.end();
        }

        return res.status(500).type("text/plain").send("Internal Server Error");
    });

    app.listen(port, (error) => {
        if (error) {
            throw error;
        }

        console.info(`SSR Serving at http://localhost:${port}`);
    });

    const dynamicImport = async (path) => {
        return import(
            nodePath.isAbsolute(path) ? nodeUrl.pathToFileURL(path).toString() : path
        );
        
    }
}
main();