-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrtdb.mjs
30 lines (28 loc) · 912 Bytes
/
rtdb.mjs
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
// Adapter for Realtime Database
export const installRtdbRedirect = (app) => app.all("/.lp", async (req, res, next) => {
// Parse ns param
// https://webcode.run/.lp?start=t&ser=88616779&cb=1&v=5&ns=tomlarkworthy|notebook-name;ep
const decoded = parseNS(req.query.ns);
if (decoded) {
req.url = `/observablehq.com/${decoded.namespace ? `@${decoded.namespace}/`: ''}${decoded.notebook};${decoded.deploy}`;
app.handle(req, res);
} else {
next()
}
});
export const parseNS = (ns) => {
const match = /([^|]*)\|([^;]*)(?:;(.*))?$/.exec(ns)
if (!match) return undefined
if (match[1] === 'd') {
return {
notebook: 'd/' + match[2],
deploy: match[3] || "default"
}
} else {
return {
namespace: match[1],
notebook: match[2],
deploy: match[3] || "default"
}
}
}