forked from tapio/live-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (190 loc) · 6.16 KB
/
index.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env node
var fs = require('fs'),
connect = require('connect'),
WebSocket = require('faye-websocket'),
path = require('path'),
url = require('url'),
http = require('http'),
send = require('send'),
open = require('opn'),
es = require("event-stream"),
watchr = require('watchr');
require('colors');
var INJECTED_CODE = fs.readFileSync(path.join(__dirname, "injected.html"), "utf8");
var LiveServer = {};
function escape(html){
return String(html)
.replace(/&(?!\w+;)/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
// Based on connect.static(), but streamlined and with added code injecter
function staticServer(root) {
return function(req, res, next) {
if (req.method !== "GET" && req.method !== "HEAD") return next();
var reqpath = url.parse(req.url).pathname;
var hasNoOrigin = !req.headers.origin;
var doInject = false;
function directory() {
var pathname = url.parse(req.originalUrl).pathname;
res.statusCode = 301;
res.setHeader('Location', pathname + '/');
res.end('Redirecting to ' + escape(pathname) + '/');
}
function file(filepath, stat) {
var x = path.extname(filepath).toLocaleLowerCase(),
possibleExtensions = ["", ".html", ".htm", ".xhtml", ".php"];
if (hasNoOrigin && (possibleExtensions.indexOf(x) > -1)) {
// TODO: Sync file read here is not nice, but we need to determine if the html should be injected or not
var contents = fs.readFileSync(filepath, "utf8");
doInject = contents.indexOf("</body>") > -1;
}
}
function error(err) {
if (err.status === 404) return next();
next(err);
}
function inject(stream) {
if (doInject) {
// We need to modify the length given to browser
var len = INJECTED_CODE.length + res.getHeader('Content-Length');
res.setHeader('Content-Length', len);
var originalPipe = stream.pipe;
stream.pipe = function(res) {
originalPipe.call(stream, es.replace(new RegExp("</body>", "i"), INJECTED_CODE + "</body>")).pipe(res);
};
}
}
send(req, reqpath, { root: root })
.on('error', error)
.on('directory', directory)
.on('file', file)
.on('stream', inject)
.pipe(res);
};
}
/**
* Rewrite request URL and pass it back to the static handler.
* @param staticHandler {function} Next handler
* @param file {string} Path to the entry point file
*/
function entryPoint(staticHandler, file) {
if (!file) return function(req, res, next) { next(); };
return function(req, res, next) {
req.url = "/" + file;
staticHandler(req, res, next);
};
}
/**
* Start a live server with parameters given as an object
* @param host {string} Address to bind to (default: 0.0.0.0)
* @param port {number} Port number (default: 8080)
* @param root {string} Path to root directory (default: cwd)
* @param open {string} Subpath to open in browser, use false to suppress launch (default: server root)
* @param logLevel {number} 0 = errors only, 1 = some, 2 = lots
* @param file {string} Path to the entry point file
* @param wait {number} Server will wait for all changes, before reloading
*/
LiveServer.start = function(options) {
options = options || {};
var host = options.host || '0.0.0.0';
var port = options.port !== undefined ? options.port : 8080; // 0 means random
var root = options.root || process.cwd();
var logLevel = options.logLevel === undefined ? 2 : options.logLevel;
var openPath = (options.open === undefined || options.open === true) ?
"" : ((options.open === null || options.open === false) ? null : options.open);
if (options.noBrowser) openPath = null; // Backwards compatibility with 0.7.0
var file = options.file;
var staticServerHandler = staticServer(root);
var wait = options.wait || 0;
var browser = options.browser || null;
// Setup a web server
var app = connect()
.use(staticServerHandler) // Custom static server
.use(entryPoint(staticServerHandler, file))
.use(connect.directory(root, { icons: true }));
if (logLevel >= 2)
app.use(connect.logger('dev'));
var server = http.createServer(app);
// Handle server startup errors
server.addListener('error', function(e) {
if (e.code === 'EADDRINUSE') {
var serveURL = 'http://' + host + ':' + port;
console.log('%s is already in use. Trying another port.'.red, serveURL);
setTimeout(function() {
server.listen(0, host);
}, 1000);
}
});
// Handle successful server
server.addListener('listening', function(e) {
var address = server.address();
var serveHost = address.address === "0.0.0.0" ? "127.0.0.1" : address.address;
var serveURL = 'http://' + serveHost + ':' + address.port;
// Output
if (logLevel >= 1) {
console.log(("Serving \"%s\" at %s").green, root, serveURL);
}
// Launch browser
if (openPath !== null)
open(serveURL + openPath, {app: browser});
});
// Setup server to listen at port
server.listen(port, host);
// WebSocket
var clients = [];
server.addListener('upgrade', function(request, socket, head) {
var ws = new WebSocket(request, socket, head);
ws.onopen = function() { ws.send('connected'); };
if (wait > 0) {
(function(ws) {
var wssend = ws.send;
var waitTimeout;
ws.send = function() {
var args = arguments;
if (waitTimeout) clearTimeout(waitTimeout);
waitTimeout = setTimeout(function(){
wssend.apply(ws, args);
}, wait);
};
})(ws);
}
ws.onclose = function() {
clients = clients.filter(function (x) {
return x !== ws;
});
};
clients.push(ws);
});
// Setup file watcher
watchr.watch({
path: root,
ignorePaths: options.ignore || false,
ignoreCommonPatterns: true,
ignoreHiddenFiles: true,
preferredMethods: [ 'watchFile', 'watch' ],
interval: 1407,
listeners: {
error: function(err) {
console.log("ERROR:".red, err);
},
change: function(eventName, filePath, fileCurrentStat, filePreviousStat) {
clients.forEach(function(ws) {
if (!ws) return;
if (path.extname(filePath) === ".css") {
ws.send('refreshcss');
if (logLevel >= 1)
console.log("CSS change detected".magenta);
} else {
ws.send('reload');
if (logLevel >= 1)
console.log("File change detected".cyan);
}
});
}
}
});
return server;
};
module.exports = LiveServer;