server.js
6.93 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
var fs = require('fs');
var qs = require('qs');
var path = require('path');
var util = require('util');
var http = require('http');
var https = require('https');
var events = require('events');
var parse = require('url').parse;
var debug = require('debug')('minilr:server');
var Client = require('./client');
// Middleware fallbacks
var bodyParser = require('body-parser').json()
var queryParser = require('./middleware/query')();
var config = require('../package.json');
function Server(options) {
options = this.options = options || {};
events.EventEmitter.call(this);
options.livereload = options.livereload || require.resolve('livereload-js/dist/livereload.js');
options.port = parseInt(options.port || 35729, 10);
this.on('GET /', this.index.bind(this));
this.on('GET /changed', this.changed.bind(this));
this.on('POST /changed', this.changed.bind(this));
this.on('GET /livereload.js', this.livereload.bind(this));
this.on('GET /kill', this.close.bind(this));
if(options.errorListener) {
this.errorListener = options.errorListener;
}
this.clients = {};
this.configure(options.app);
}
module.exports = Server;
util.inherits(Server, events.EventEmitter);
Server.prototype.configure = function configure(app) {
var self = this;
debug('Configuring %s', app ? 'connect / express application' : 'HTTP server');
if (!app) {
if ((this.options.key && this.options.cert) || this.options.pfx) {
this.server = https.createServer(this.options, this.handler.bind(this));
} else {
this.server = http.createServer(this.handler.bind(this));
}
this.server.on('upgrade', this.websocketify.bind(this));
this.server.on('error', function() {
self.error.apply(self, arguments);
});
return this;
}
this.app = app;
this.app.listen = function(port, done) {
done = done || function() {};
if (port !== self.options.port) {
debug('Warn: LiveReload port is not standard (%d). You are listening on %d', self.options.port, port);
debug('You\'ll need to rely on the LiveReload snippet');
debug('> http://feedback.livereload.com/knowledgebase/articles/86180-how-do-i-add-the-script-tag-manually-');
}
var srv = self.server = http.createServer(app);
srv.on('upgrade', self.websocketify.bind(self));
srv.on('error', function() {
self.error.apply(self, arguments);
});
srv.on('close', self.close.bind(self));
return srv.listen(port, done);
};
return this;
};
Server.prototype.handler = function handler(req, res, next) {
var self = this;
var middleware = typeof next === 'function';
debug('LiveReload handler %s (middleware: %s)', req.url, middleware ? 'on' : 'off');
this.parse(req, res, function(err) {
debug('query parsed', req.body, err);
if (err) return next(err);
self.handle(req, res, next);
});
// req
// .on('end', this.handle.bind(this, req, res))
// .on('data', function(chunk) {
// req.data = req.data || '';
// req.data += chunk;
// });
return this;
};
// Ensure body / query are defined, useful as a fallback when the
// Server is used without express / connect, and shouldn't hurt
// otherwise
Server.prototype.parse = function(req, res, next) {
debug('Parse', req.body, req.query);
bodyParser(req, res, function(err) {
debug('Body parsed', req.body);
if (err) return next(err);
queryParser(req, res, next);
});
};
Server.prototype.handle = function handle(req, res, next) {
var url = parse(req.url);
debug('Request:', req.method, url.href);
var middleware = typeof next === 'function';
// do the routing
var route = req.method + ' ' + url.pathname;
var respond = this.emit(route, req, res);
if (respond) return;
if (!middleware) {
// Only apply content-type on non middleware setup #70
res.setHeader('Content-Type', 'application/json');
} else {
// Middleware ==> next()ing
return next();
}
res.writeHead(404);
res.write(JSON.stringify({
error: 'not_found',
reason: 'no such route'
}));
res.end();
};
Server.prototype.websocketify = function websocketify(req, socket, head) {
var self = this;
var client = new Client(req, socket, head, this.options);
this.clients[client.id] = client;
debug('New LiveReload connection (id: %s)', client.id);
client.on('end', function() {
debug('Destroy client %s (url: %s)', client.id, client.url);
delete self.clients[client.id];
});
};
Server.prototype.listen = function listen(port, host, fn) {
port = parseInt(port || 35729, 10);
this.port = port;
if (typeof host === 'function') {
fn = host;
host = undefined;
}
this.server.listen(port, host, fn);
};
Server.prototype.close = function close(req, res) {
Object.keys(this.clients).forEach(function(id) {
this.clients[id].close();
}, this);
if (this.server._handle) this.server.close(this.emit.bind(this, 'close'));
if (res) res.end();
};
Server.prototype.error = function error(e) {
if(this.errorListener) {
this.errorListener(e);
return
}
console.error();
console.error('... Uhoh. Got error %s ...', e.message);
console.error(e.stack);
if (e.code !== 'EADDRINUSE') return;
console.error();
console.error('You already have a server listening on %s', this.port);
console.error('You should stop it and try again.');
console.error();
};
// Routes
Server.prototype.livereload = function livereload(req, res) {
res.setHeader('Content-Type', 'application/javascript');
fs.createReadStream(this.options.livereload).pipe(res);
};
Server.prototype.changed = function changed(req, res) {
var files = this.param('files', req);
debug('Changed event (Files: %s)', files.join(' '));
var clients = this.notifyClients(files);
if (!res) return;
res.setHeader('Content-Type', 'application/json');
res.write(JSON.stringify({
clients: clients,
files: files
}));
res.end();
};
Server.prototype.notifyClients = function notifyClients(files) {
var clients = Object.keys(this.clients).map(function(id) {
var client = this.clients[id];
debug('Reloading client %s (url: %s)', client.id, client.url);
client.reload(files);
return {
id: client.id,
url: client.url
};
}, this);
return clients;
};
// Lookup param from body / params / query.
Server.prototype.param = function _param(name, req) {
var param;
if (req.body && req.body[name]) param = req.body.files;
else if (req.params && req.params[name]) param = req.params.files;
else if (req.query && req.query[name]) param= req.query.files;
// normalize files array
param = Array.isArray(param) ? param :
typeof param === 'string' ? param.split(/[\s,]/) :
[];
debug('param %s', name, req.body, req.params, req.query, param);
return param;
};
Server.prototype.index = function index(req, res) {
res.setHeader('Content-Type', 'application/json');
res.write(JSON.stringify({
minilr: 'Welcome',
version: config.version
}));
res.end();
};