/
superstatic.ts
145 lines (132 loc) · 3.66 KB
/
superstatic.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
/**
* This converts Superstatic configuration to Now.json Routes
* See https://github.com/firebase/superstatic#configuration
*/
import { isString } from 'util';
import { parse as parseUrl, format as formatUrl } from 'url';
import { pathToRegexp, compile, Key } from 'path-to-regexp';
import { Route, NowRedirect, NowRewrite, NowHeader } from './types';
export function getCleanUrls(
filePaths: string[]
): { html: string; clean: string }[] {
const htmlFiles = filePaths
.map(toRoute)
.filter(f => f.endsWith('.html'))
.map(f => ({
html: f,
clean: f.slice(0, -5),
}));
return htmlFiles;
}
export function convertCleanUrls(
cleanUrls: boolean,
trailingSlash?: boolean,
status = 308
): Route[] {
const routes: Route[] = [];
if (cleanUrls) {
const loc = trailingSlash ? '/$1/' : '/$1';
routes.push({
src: '^/(?:(.+)/)?index(?:\\.html)?/?$',
headers: { Location: loc },
status,
});
routes.push({
src: '^/(.*)\\.html/?$',
headers: { Location: loc },
status,
});
}
return routes;
}
export function convertRedirects(
redirects: NowRedirect[],
defaultStatus = 308
): Route[] {
return redirects.map(r => {
const { src, segments } = sourceToRegex(r.source);
const loc = replaceSegments(segments, r.destination);
const route: Route = {
src,
headers: { Location: loc },
status: r.statusCode || defaultStatus,
};
return route;
});
}
export function convertRewrites(rewrites: NowRewrite[]): Route[] {
return rewrites.map(r => {
const { src, segments } = sourceToRegex(r.source);
const dest = replaceSegments(segments, r.destination);
const route: Route = { src, dest, check: true };
return route;
});
}
export function convertHeaders(headers: NowHeader[]): Route[] {
return headers.map(h => {
const obj: { [key: string]: string } = {};
h.headers.forEach(kv => {
obj[kv.key] = kv.value;
});
const route: Route = {
src: h.source,
headers: obj,
continue: true,
};
return route;
});
}
export function convertTrailingSlash(enable: boolean, status = 308): Route[] {
const routes: Route[] = [];
if (enable) {
routes.push({
src: '^/(.*[^\\/])$',
headers: { Location: '/$1/' },
status,
});
} else {
routes.push({
src: '^/(.*)\\/$',
headers: { Location: '/$1' },
status,
});
}
return routes;
}
function sourceToRegex(source: string): { src: string; segments: string[] } {
const keys: Key[] = [];
const r = pathToRegexp(source, keys, { strict: true });
const segments = keys.map(k => k.name).filter(isString);
return { src: r.source, segments };
}
function replaceSegments(segments: string[], destination: string): string {
const parsedDestination = parseUrl(destination, true);
let { pathname } = parsedDestination;
pathname = pathname || '';
if (pathname.includes(':') && segments.length > 0) {
const compiler = compile(pathname);
const indexes: { [k: string]: string } = {};
segments.forEach((name, index) => {
indexes[name] = toSegmentDest(index);
});
pathname = compiler(indexes);
destination = formatUrl({
...parsedDestination,
pathname,
});
} else if (segments.length > 0) {
let prefix = '?';
segments.forEach((name, index) => {
destination += `${prefix}${name}=${toSegmentDest(index)}`;
prefix = '&';
});
}
return destination;
}
function toSegmentDest(index: number): string {
const i = index + 1; // js is base 0, regex is base 1
return '$' + i.toString();
}
function toRoute(filePath: string): string {
return filePath.startsWith('/') ? filePath : '/' + filePath;
}