forked from lukejacksonn/servor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servor.js
180 lines (149 loc) · 5.47 KB
/
servor.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
const fs = require('fs');
const url = require('url');
const path = require('path');
const http = require('http');
const http2 = require('http2');
const https = require('https');
const zlib = require('zlib');
const mimeTypes = require('./utils/mimeTypes.js');
const directoryListing = require('./utils/directoryListing.js');
const { fileWatch, usePort, networkIps } = require('./utils/common.js');
module.exports = async ({
root = '.',
module = false,
fallback = module ? 'index.js' : 'index.html',
reload = true,
static = false,
inject = '',
credentials,
port,
} = {}) => {
// Try start on specified port then fail or find a free port
try {
port = await usePort(port || process.env.PORT || 8080);
} catch (e) {
if (port || process.env.PORT) {
console.log('[ERR] The port you have specified is already in use!');
process.exit();
}
port = await usePort();
}
// Configure globals
root = root.startsWith('/') ? root : path.join(process.cwd(), root);
if (!fs.existsSync(root)) {
console.log(`[ERR] Root directory ${root} does not exist!`);
process.exit();
}
if (!fs.statSync(root).isDirectory()) {
console.log(`[ERR] Root directory "${root}" is not directory!`);
process.exit();
}
const reloadClients = [];
const protocol = credentials ? 'https' : 'http';
const server = credentials
? reload
? (cb) => https.createServer(credentials, cb)
: (cb) => http2.createSecureServer(credentials, cb)
: (cb) => http.createServer(cb);
const livereload = reload
? `
<script>
const source = new EventSource('/livereload');
const reload = () => location.reload(true);
source.onmessage = reload;
source.onerror = () => (source.onopen = reload);
console.log('[servor] listening for file changes');
</script>
`
: '';
// Server utility functions
const isRouteRequest = (pathname) => !~pathname.split('/').pop().indexOf('.');
const utf8 = (file) => Buffer.from(file, 'binary').toString('utf8');
const baseDoc = (pathname = '', base = path.join('/', pathname, '/')) =>
`<!doctype html><meta charset="utf-8"/><base href="${base}"/>`;
const sendError = (res, status) => {
res.writeHead(status);
res.write(`${status}`);
res.end();
};
const sendFile = (res, status, file, ext, encoding = 'binary') => {
if (['js', 'css', 'html', 'json', 'xml', 'svg'].includes(ext)) {
res.setHeader('content-encoding', 'gzip');
file = zlib.gzipSync(utf8(file));
encoding = 'utf8';
}
res.writeHead(status, { 'content-type': mimeTypes(ext) });
res.write(file, encoding);
res.end();
};
const sendMessage = (res, channel, data) => {
res.write(`event: ${channel}\nid: 0\ndata: ${data}\n`);
res.write('\n\n');
};
// Respond to reload requests with keep alive
const serveReload = (res) => {
res.writeHead(200, {
connection: 'keep-alive',
'content-type': 'text/event-stream',
'cache-control': 'no-cache',
});
sendMessage(res, 'connected', 'ready');
setInterval(sendMessage, 60000, res, 'ping', 'waiting');
reloadClients.push(res);
};
// Respond to requests with a file extension
const serveStaticFile = (res, pathname) => {
const uri = path.join(root, pathname);
let ext = uri.replace(/^.*[\.\/\\]/, '').toLowerCase();
if (!fs.existsSync(uri)) return sendError(res, 404);
fs.readFile(uri, 'binary', (err, file) =>
err ? sendError(res, 500) : sendFile(res, 200, file, ext)
);
};
// Respond to requests without a file extension
const serveRoute = (res, pathname) => {
const index = static
? path.join(root, pathname, fallback)
: path.join(root, fallback);
if (!fs.existsSync(index) || (pathname.endsWith('/') && pathname !== '/'))
return serveDirectoryListing(res, pathname);
fs.readFile(index, 'binary', (err, file) => {
if (err) return sendError(res, 500);
const status = pathname === '/' || static ? 200 : 301;
if (module) file = `<script type='module'>${file}</script>`;
if (static) file = baseDoc(pathname) + file;
file = file + inject + livereload;
sendFile(res, status, file, 'html');
});
};
// Respond to requests with a trailing slash
const serveDirectoryListing = (res, pathname) => {
const uri = path.join(root, pathname);
if (!fs.existsSync(uri)) return sendError(res, 404);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(baseDoc(pathname) + directoryListing(uri) + livereload);
res.end();
};
// Start the server and route requests
server((req, res) => {
const decodePathname = decodeURI(url.parse(req.url).pathname);
const pathname = path.normalize(decodePathname).replace(/^(\.\.(\/|\\|$))+/, '');
res.setHeader('access-control-allow-origin', '*');
if (reload && pathname === '/livereload') return serveReload(res);
if (!isRouteRequest(pathname)) return serveStaticFile(res, pathname);
return serveRoute(res, pathname);
}).listen(parseInt(port, 10));
// Notify livereload reloadClients on file change
reload &&
fileWatch(root, () => {
while (reloadClients.length > 0)
sendMessage(reloadClients.pop(), 'message', 'reload');
});
// Close socket connections on sigint
process.on('SIGINT', () => {
while (reloadClients.length > 0) reloadClients.pop().end();
process.exit();
});
const x = { url: `${protocol}://localhost:${port}` };
return { ...x, root, protocol, port, ips: networkIps };
};