-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.ts
260 lines (236 loc) · 7.22 KB
/
static.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import path from "node:path";
import express from "express";
import compression from "compression";
import cookieParser from "cookie-parser";
import { staticMiddlewares } from "./middlewares.js";
import { resolveFundamental } from "../content/index.js";
import { BUILD_OUT_ROOT } from "../libs/env/index.js";
const app = express();
app.use(express.json());
app.use(compression());
app.use(express.urlencoded({ extended: true }));
app.use((req, res, next) => {
// If we have a fundamental redirect mimic out Lambda@Edge and redirect.
const { url: fundamentalRedirectUrl, status } = resolveFundamental(req.url);
if (fundamentalRedirectUrl && status) {
return res.redirect(status, fundamentalRedirectUrl);
}
return next();
});
app.get("/", async (req, res) => {
res.redirect(302, "/en-US/");
});
app.use(staticMiddlewares);
app.use(cookieParser());
// This endpoint exists solely to accompany the headless tests.
// They will trigger XHR requests to `/api/v1/search?....`
// and use different values as a way to make expectations.
app.get("/api/v1/search", async (req, res) => {
const page = parseInt((req.query.page as string) || "1");
const PAGE_SIZE = 10;
const documents = [];
const metadata = {
took_ms: 0.1,
total: {
value: 0,
relation: "eq",
},
size: PAGE_SIZE,
page,
};
const suggestions = [];
// This stuff exists purely to end-to-end test mock the search
// API which would otherwise go to Kuma.
if (req.query.q && req.query.q === "FOO") {
metadata.total.value = 1;
documents.push({
mdn_url: "/en-us/docs/Web/Foo",
score: 1.1,
title: "Foo",
locale: "en-US",
slug: "Web/Foo",
popularity: 0.1,
summary: "No summary",
highlight: {
body: null,
title: null,
},
});
} else if (req.query.q && /SERIAL\(\d+\)/.test(req.query.q as string)) {
// A search for `?q=SERIAL(123)` will pretend it found 123 matches.
// But paginate them and just make sure the URL and title are different.
const serial = parseInt(
(req.query.q as string).match(/SERIAL\((\d+)\)/)[1]
);
metadata.total.value = serial;
for (const i of Array(Math.max(PAGE_SIZE, serial)).keys()) {
documents.push({
mdn_url: `/en-us/docs/Web/Serial${i}`,
score: 1.1,
title: `Serial ${i}`,
locale: "en-US",
slug: `Web/Serial${i}`,
popularity: 1 - i / 10,
summary: "No summary",
highlight: {
body: null,
title: null,
},
});
}
}
res.json({
documents,
metadata,
suggestions,
});
});
const mockWhoamiDatabase = new Map();
app.get("/api/v1/whoami", async (req, res) => {
const sessionid = req.cookies.sessionid;
const context =
sessionid && mockWhoamiDatabase.has(sessionid)
? mockWhoamiDatabase.get(sessionid)
: {};
res.json(context);
});
const mockSettingsDatabase = new Map();
app.get("/api/v1/settings/", async (req, res) => {
const defaultContext = { locale: "en-US" };
if (!req.cookies.sessionid) {
res.status(403).send("oh no you don't");
} else {
if (mockSettingsDatabase.has(req.cookies.sessionid)) {
res.json(
Object.assign(
{},
defaultContext,
mockSettingsDatabase.get(req.cookies.sessionid)
)
);
} else {
res.json(defaultContext);
}
}
});
app.post("/api/v1/settings/", async (req, res) => {
if (!req.cookies.sessionid) {
res.status(403).send("oh no you don't");
} else {
mockSettingsDatabase.set(req.cookies.sessionid, {
locale: req.body.locale,
});
res.json({ ok: true });
}
});
const mockBookmarksDatabase = new Map();
app.post("/api/v1/plus/collection/", async (req, res) => {
// Toggle
const url = req.query.url as string;
if (!url) {
return res.status(400).send("missing url");
}
const bookmarks = mockBookmarksDatabase.get(req.cookies.sessionid) || [];
const found = bookmarks.find((bookmark) => bookmark.url === req.query.url);
if (found) {
const newBookmarks = bookmarks.filter((bookmark) => bookmark.url !== url);
mockBookmarksDatabase.set(req.cookies.sessionid, newBookmarks);
} else {
const makeTitle = (url) =>
`Title of ${url.charAt(0).toUpperCase()}${url.slice(1).toLowerCase()}`;
bookmarks.push({
id: Math.max(...[0, ...bookmarks.map((b) => b.id)]) + 1,
url,
created: new Date(),
parents: url
.split("/")
.slice(3)
.map((uri, i) => {
const prefix = url
.split("/")
.slice(0, 3 + i)
.join("/");
return { uri: `${prefix}/${uri}`, title: makeTitle(uri) };
}),
title: makeTitle(url),
});
mockBookmarksDatabase.set(req.cookies.sessionid, bookmarks);
}
res.status(201).json({ OK: true });
});
app.get("/api/v1/plus/collection/", async (req, res) => {
if (!req.cookies.sessionid) {
res.status(403).send("oh no you don't");
} else {
const bookmarks = mockBookmarksDatabase.get(req.cookies.sessionid) || [];
if (req.query.url) {
// Toggled?
const found = bookmarks.find(
(bookmark) => bookmark.url === req.query.url
);
res.json({
bookmarked: found
? { id: found.id, created: found.created.toISOString() }
: null,
});
} else {
// Return all (paginated)
const page = parseInt((req.query.page as string) || "1", 10);
const pageSize = 5;
const m = 0;
const n = pageSize;
res.json({
items: bookmarks.slice(m, n).map((bookmark) => {
return {
...bookmark,
created: bookmark.created.toISOString(),
};
}),
metadata: {
total: bookmarks.length,
page: page,
per_page: pageSize,
},
});
}
}
});
app.get("/users/fxa/login/authenticate/", async (req, res) => {
const userId = `${Math.ceil(10000 * Math.random())}`;
res.cookie("sessionid", userId, {
maxAge: 5 * 60 * 1000,
});
mockWhoamiDatabase.set(userId, {
username: `my-${userId}`,
is_authenticated: true,
email: `${userId}@example.com`,
// Note! Everyone who manages to sign in becomes a subscriber.
// This is probably not ideal. Perhaps more ideal would be that
// they sign in and become a subscriber later. But as of Aug 2021, it's
// not obvious how we are going to integrate authentication with
// with authorization. I.e. use of the subscription platform and FxA.
is_subscriber: true,
});
res.redirect(req.query.next as string);
});
app.post("/users/fxa/login/logout/", async (req, res) => {
if (!req.cookies.sessionid) {
res.status(403).send("oh no you don't");
} else {
res.clearCookie("sessionid");
mockWhoamiDatabase.delete(req.cookies.sessionid);
res.redirect(`/en-US/`);
}
});
// To mimic what CloudFront does.
app.get("/*", async (req, res) => {
console.log(`Don't know how to mock: ${req.path}`, req.query);
res
.status(404)
.sendFile(path.join(BUILD_OUT_ROOT, "en-us", "_spas", "404.html"));
});
const HOST = process.env.SERVER_HOST || undefined;
const PORT = parseInt(process.env.SERVER_PORT || "5042");
app.listen(PORT, HOST, () =>
console.log(`Listening on ${HOST ? `${HOST}:` : "port "}${PORT}`)
);