-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.js
executable file
·181 lines (159 loc) · 5.24 KB
/
serve.js
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
#!/usr/bin/env node
const fs = require('fs/promises');
const http = require('http');
const path = require('path');
const url = require('url');
const mime = require('mime');
const redirectParser = require('netlify-redirect-parser');
const {
parseHeadersFile,
headersForPath
} = require('netlify-cli/src/utils/headers');
const {fdir} = require('fdir');
let host = 'localhost';
let port = 9000;
let dir = path.resolve('public');
let cfg = path.resolve('netlify.toml');
// getambassador.io migration sources
const datawireDomains = {
'http://www.datawire.io/': true,
'https://www.datawire.io/': true,
'http://datawire.io/': true,
'https://datawire.io/': true
};
// getambassador.io migration link
const migrationRedirect = 'https://www.getambassador.io/?utm_source=https://www.datawire.io/';
const server = http.createServer();
async function exists(filepath) {
try {
await fs.stat(filepath);
return true;
} catch {
return false;
}
}
function matchesRedirect(forcefulOnly, requestURL, redirect) {
if (forcefulOnly && !redirect.force) {
return false;
}
// keep getambassador.io on localhost, due to migrations from datawire.io to getambassador.io
if (requestURL.pathname === '/' && requestURL.path === '/' && requestURL.href === '/' &&
datawireDomains[redirect.origin] && migrationRedirect === redirect.to) {
return false;
}
if (redirect.path !== requestURL.pathname && redirect.path !== requestURL.pathname+'/') {
return false;
}
const requestQuery = new URLSearchParams(requestURL.query);
for (const key in redirect.query) {
if (requestQuery.get(key) !== redirect.query[key]) {
return false;
}
}
return true;
}
function doRedirect(requestURL, response, redirect) {
let location = redirect.to;
if (!url.parse(location).search) {
location += (requestURL.search || '');
}
response.writeHead(redirect.status, {
'Location': location,
'Content-Type': 'text/plain',
});
response.end(`Redirecting to ${location}`);
}
const filesOnMemory = {};
const errorLoadingFile = 'There was an error reading the file';
function loadSiteOnMemory(dir) {
const api = new fdir().withFullPaths().crawl(dir);
const files = api.sync();
for (const file of files) {
fs.readFile(file).then(content => {
filesOnMemory[file] = content;
}).catch(() => filesOnMemory[file] = errorLoadingFile);
}
}
loadSiteOnMemory(dir);
const headersFiles = [path.resolve('_headers'), path.resolve(dir, '_headers')];
const headerRules = headersFiles.reduce((headerRules, headersFile) => Object.assign(headerRules, parseHeadersFile(headersFile)), {});
let redirects;
try {
redirects = redirectParser.parseAllRedirects({
redirectsFiles: [path.resolve(dir, '_redirects')],
netlifyConfigPath: cfg,
});
} catch (err) {
process.abort();
}
server.on('request', async (request, response) => {
console.log('srv', request.method, request.url);
const requestURL = url.parse(url.resolve('/', request.url));
if (requestURL.protocol || requestURL.slashes || requestURL.host) {
response.writeHead(400);
response.end('Bad request URL');
}
let redirect = (await redirects).find((redirect) => (matchesRedirect(true, requestURL, redirect)));
if (redirect) {
doRedirect(requestURL, response, redirect);
return;
}
const filepath = path.join(dir, requestURL.pathname).replace(/\/$/, '/index.html');
let content;
try {
if (filesOnMemory[filepath] && filesOnMemory[filepath] !== errorLoadingFile) {
content = filesOnMemory[filepath];
} else {
content = await fs.readFile(filepath);
}
} catch (err) {
if (err.code === 'EISDIR' && !requestURL.pathname.endsWith('/')) {
// All sane webservers should do this. `netlify dev` doesn't.
const location = requestURL.pathname + '/' + (requestURL.search || '');
response.writeHead(302, {
'Location': location,
'Content-Type': 'text/plain',
});
response.end(`Redirecting to ${location}`);
} else if (requestURL.pathname.endsWith('.html') && await exists(filepath.replace(/\.html$/, ''))) {
// This is a weird thing that Netlify does (even if you
// turn off pretty URLs).
const location = requestURL.pathname.replace(/\.html$/, '') + (requestURL.search || '');
response.writeHead(302, {
'Location': location,
'Content-Type': 'text/plain',
});
response.end(`Redirecting to ${location}`);
} else if ((redirect = (await redirects).find((redirect) => (matchesRedirect(false, requestURL, redirect))))) {
doRedirect(requestURL, response, redirect);
} else {
response.writeHead(404, {
'Content-Type': 'text/html',
});
response.end(await fs.readFile(path.resolve(dir, '404.html')));
}
return;
}
const pathHeaderRules = headersForPath(headerRules, requestURL.pathname);
Object.entries(pathHeaderRules).forEach(([key, val]) => {
response.setHeader(key, val);
});
response.writeHead(200, {
'Content-Type': mime.getType(filepath),
});
response.end(content);
});
server.listen(port, host, () => {
const addr = url.format({
protocol: 'http',
hostname: host,
port: port,
pathname: '/',
});
console.log(`----
Serving
directory ${dir}
with config ${cfg}
at address ${addr}
----`);
});