forked from mdn/yari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.ts
66 lines (62 loc) · 2.15 KB
/
middlewares.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import express from "express";
import {
CSP_VALUE,
PLAYGROUND_UNSAFE_CSP_VALUE,
} from "../libs/constants/index.js";
import { STATIC_ROOT } from "../libs/env/index.js";
import { resolveFundamental } from "../libs/fundamental-redirects/index.js";
import { getLocale } from "../libs/locale-utils/index.js";
// Lowercase every request because every possible file we might have
// on disk is always in lowercase.
// This only helps when you're on a filesystem (e.g. Linux) that is case
// sensitive.
const slugRewrite = (req, res, next) => {
req.url = req.url.toLowerCase();
next();
};
/**
* This function is returns an object with {url:string, status:number}
* if there's some place to redirect to, otherwise an empty object.
*/
const originRequest = (req, res, next) => {
const { url: fundamentalRedirectUrl, status } = resolveFundamental(req.url);
if (fundamentalRedirectUrl && status) {
res.redirect(status, fundamentalRedirectUrl);
} else if (req.url === "/" || req.url.startsWith("/docs/")) {
// Fake it so it becomes like Lambda@Edge
req.headers.cookie = [
{
// The `req.cookies` comes from cookie-parser
value: Object.entries(req.cookies)
.map(([key, value]) => `${key}=${value}`)
.join(";"),
},
];
if (req.headers["accept-language"]) {
// Lambda@Edge expects it to be an array of objects
req.headers["accept-language"] = [
{ value: req.headers["accept-language"] },
];
}
const path = req.url.endsWith("/") ? req.url.slice(0, -1) : req.url;
const locale = getLocale(req);
// The only time we actually want a trailing slash is when the URL is just
// the locale. E.g. `/en-US/` (not `/en-US`)
res.redirect(302, `/${locale}${path || "/"}`);
} else {
next();
}
};
export const staticMiddlewares = [
slugRewrite,
express.static(STATIC_ROOT, {
setHeaders: (res) => {
if (res.req.path.endsWith("/runner.html")) {
res.setHeader("Content-Security-Policy", PLAYGROUND_UNSAFE_CSP_VALUE);
} else {
res.setHeader("Content-Security-Policy", CSP_VALUE);
}
},
}),
];
export const originRequestMiddleware = originRequest;