-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathobservable.mjs
70 lines (63 loc) · 3.11 KB
/
observable.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
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
export const pattern = '(/regions/:region)?/observablehq.com((/d/:id)|(/@:owner/(:notebook)(/:suffix(\\d+))?))(@(:version))?((;|%3B)(:name([^;/]+)))?((;|%3B)(:correlation([^;/]+)))?(/:path(*))?';
export function decode(req) {
if (req.params.suffix) req.params.notebook = `${req.params.notebook}/${req.params.suffix}`;
const notebook = req.params.id ? `d/${req.params.id}` : req.params.notebook
let userURL = "/" + (req.params.path || '');
const name = req.params.name || 'default';
const endpointURL =
`/observablehq.com` +
`${req.params.id ? `/d/${req.params.id}`:`/@${req.params.owner}/${req.params.notebook}`}` +
`${req.params.version ? `@${req.params.version}`: ''}` +
`${req.params.name ? `;${req.params.name}` : ``}`;
return {
notebook,
path: userURL,
name: name,
endpointURL,
...(req.params.correlation && {correlation: req.params.correlation}),
...(req.params.owner && {namespace: req.params.owner}),
...(req.params.version && {version: req.params.version})
};
}
export function parseEndpointURL(endpointURL) {
const match = /\/(?<codehost>[^/]*)\/(?:d\/(?<id>[a-z0-9]+)|@(?<namespace>[a-z0-9]+)\/(?<notebook>[a-z0-9-]+))(?:@(?<version>[0-9]+))?(?:;(?<name>[^;/]+))?(?:;(?<correlation>[^;/]+))?/.exec(endpointURL);
if (!match) return undefined;
return {
codehost: match.groups.codehost,
id: match.groups.id,
namespace: match.groups.namespace,
notebook: match.groups.notebook,
...(match.groups.correlation && {correlation: match.groups.correlation}),
...(match.groups.version && {version: Number.parseInt(match.groups.version)}),
name: match.groups.name || 'default',
};
}
export function notebookURL(req, {api_key = undefined} = {}) {
const versionSuffix = req.params.version ? `@${req.params.version}` : '';
const local = req.hostname === 'localhost';
return api_key ?
(req.params.id)
? `https://webcode.run/observablehq.com/@endpointservices/embed/d/${req.params.id}${versionSuffix}?api_key=${api_key}`
: `https://webcode.run/observablehq.com/@endpointservices/embed/@${req.params.owner}/${req.params.notebook}${versionSuffix}?api_key=${api_key}`:
(req.params.id)
? `https://observablehq.com/embed/${req.params.id}${versionSuffix}`
: `https://observablehq.com/embed/@${req.params.owner}/${req.params.notebook}${versionSuffix}`;
}
// Could the webbrowser page be running the supplied endpoint URL?
export function canHost(embedURL, endpointURL) {
return endpointURL.replace('/observablehq.com/', '').includes(embedURL.replace('https://observablehq.com/embed/', ''))
}
export function createCellRequest(req) {
const hasBody = Object.keys(req.body).length !== 0;
return {
id: req.id,
baseUrl: req.requestConfig.endpointURL,
url: req.requestConfig.path,
method: req.method,
...hasBody && {body: req.body.toString()},
...(req.cookies && {cookies: req.cookies}),
query: req.query || {},
headers: req.headers || {},
ip: req.ip,
};
}