Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
296 lines (264 loc) · 8.87 KB
/
Copy pathindex.js
File metadata and controls
296 lines (264 loc) · 8.87 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env node
var fs = require('fs'),
connect = require('connect'),
serveIndex = require('serve-index'),
logger = require('morgan'),
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 = {
server: null,
watchers: [],
logLevel: 2
};
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,cachePattern) {
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 injectCandidates = [ "</body>", "</svg>" ];
var injectTag = null;
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) {
if (cachePattern && filepath.search(cachePattern)>=0) {
// console.log("Caching " + filepath);
res.setHeader('Cache-Control', "max-age=3600");
}
var x = path.extname(filepath).toLocaleLowerCase(),
possibleExtensions = [ "", ".html", ".htm", ".xhtml", ".php", ".svg" ];
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");
for (var i = 0; i < injectCandidates.length; ++i) {
if (contents.indexOf(injectCandidates[i]) > -1) {
injectTag = injectCandidates[i];
break;
}
}
if (injectTag === null && LiveServer.logLevel >= 2) {
console.warn("Failed to inject refresh script!".yellow,
"Couldn't find any of the tags ", injectCandidates, "from", filepath);
}
}
}
function error(err) {
if (err.status === 404) return next();
next(err);
}
function inject(stream) {
if (injectTag) {
// 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(injectTag, "i"), INJECTED_CODE + injectTag)).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 watch {array} Paths to exclusively watch for changes
* @param ignore {array} Paths to ignore when watching files for changes
* @param ignorePattern {regexp} Ignore files by RegExp
* @param cachePattern {regexp} Cache files by RegExp (defaults to 1 hour)
* @param open {string} Subpath to open in browser, use false to suppress launch (default: server root)
* @param mount {array} Mount directories onto a route, e.g. [['/components', './node_modules']].
* @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
* @param htpasswd {string} Path to htpasswd file to enable HTTP Basic authentication
*/
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 cachePattern = options.cachePattern || undefined;
var mount = options.mount || [];
var watchPaths = options.watch || [root];
LiveServer.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,cachePattern);
var wait = options.wait || 0;
var browser = options.browser || null;
var htpasswd = options.htpasswd || null;
// Setup a web server
var app = connect();
mount.forEach(function(mountRule) {
var mountPath = path.resolve(process.cwd(), mountRule[1]);
if (!options.watch) // Auto add mount paths to wathing but only if exclusive path option is not given
watchPaths.push(mountPath);
app.use(mountRule[0], staticServer(mountPath));
if (LiveServer.logLevel >= 1)
console.log('Mapping %s to "%s"', mountRule[0], mountPath);
});
app.use(staticServerHandler) // Custom static server
.use(entryPoint(staticServerHandler, file))
.use(serveIndex(root, { icons: true }));
if (LiveServer.logLevel >= 2)
app.use(logger('dev'));
// Use http-auth if configured
var server = null;
if (htpasswd === null) {
server = http.createServer(app);
} else {
var auth = require('http-auth');
var basic = auth.basic({
realm: "Please authorize",
file: htpasswd
});
server = http.createServer(basic, 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.'.yellow, serveURL);
setTimeout(function() {
server.listen(0, host);
}, 1000);
} else {
console.error(e.toString().red);
LiveServer.shutdown();
}
});
// Handle successful server
server.addListener('listening', function(e) {
LiveServer.server = server;
var address = server.address();
var serveHost = address.address === "0.0.0.0" ? "127.0.0.1" : address.address;
var openHost = host === "0.0.0.0" ? "127.0.0.1" : host;
var serveURL = 'http://' + serveHost + ':' + address.port;
var openURL = 'http://' + openHost + ':' + address.port;
// Output
if (LiveServer.logLevel >= 1) {
if (serveURL === openURL)
console.log(("Serving \"%s\" at %s").green, root, serveURL);
else
console.log(("Serving \"%s\" at %s (%s)").green, root, openURL, serveURL);
}
// Launch browser
if (openPath !== null)
open(openURL + 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({
paths: watchPaths,
ignorePaths: options.ignore || false,
ignoreCommonPatterns: true,
ignoreHiddenFiles: true,
ignoreCustomPatterns: options.ignorePattern || null,
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 (LiveServer.logLevel >= 1)
console.log("CSS change detected".magenta, filePath);
} else {
ws.send('reload');
if (LiveServer.logLevel >= 1)
console.log("File change detected".cyan, filePath);
}
});
}
},
next: function(err, watchers) {
if (err)
console.error("Error watching files:".red, err);
LiveServer.watchers = watchers;
}
});
return server;
};
LiveServer.shutdown = function() {
var watchers = LiveServer.watchers;
if (watchers) {
for (var i = 0; i < watchers.length; ++i)
watchers[i].close();
}
var server = LiveServer.server;
if (server)
server.close();
};
module.exports = LiveServer;