UNPKG

7.16 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.12.4
2(function() {
3 var Server, chokidar, defaultExclusions, defaultExts, defaultPort, fs, http, https, path, protocol_version, url, ws;
4
5 fs = require('fs');
6
7 path = require('path');
8
9 ws = require('ws');
10
11 http = require('http');
12
13 https = require('https');
14
15 url = require('url');
16
17 chokidar = require('chokidar');
18
19 protocol_version = '7';
20
21 defaultPort = 35729;
22
23 defaultExts = ['html', 'css', 'js', 'png', 'gif', 'jpg', 'php', 'php5', 'py', 'rb', 'erb', 'coffee'];
24
25 defaultExclusions = [/\.git\//, /\.svn\//, /\.hg\//];
26
27 Server = (function() {
28 function Server(config1) {
29 var base, base1, base2, base3, base4, base5, base6, base7;
30 this.config = config1;
31 if (this.config == null) {
32 this.config = {};
33 }
34 if ((base = this.config).version == null) {
35 base.version = protocol_version;
36 }
37 if ((base1 = this.config).port == null) {
38 base1.port = defaultPort;
39 }
40 if ((base2 = this.config).exts == null) {
41 base2.exts = [];
42 }
43 if ((base3 = this.config).exclusions == null) {
44 base3.exclusions = [];
45 }
46 this.config.exts = this.config.exts.concat(defaultExts);
47 this.config.exclusions = this.config.exclusions.concat(defaultExclusions);
48 if ((base4 = this.config).applyCSSLive == null) {
49 base4.applyCSSLive = true;
50 }
51 if ((base5 = this.config).originalPath == null) {
52 base5.originalPath = '';
53 }
54 if ((base6 = this.config).overrideURL == null) {
55 base6.overrideURL = '';
56 }
57 if ((base7 = this.config).usePolling == null) {
58 base7.usePolling = false;
59 }
60 }
61
62 Server.prototype.listen = function(callback) {
63 this.debug("LiveReload is waiting for browser to connect.");
64 this.debug("Protocol version: " + this.config.version + "\nExclusions: " + this.config.exclusions + "\nExtensions: " + this.config.exts + "\nPolling: " + this.config.usePolling + "\n");
65 if (this.config.server) {
66 this.config.server.listen(this.config.port);
67 this.server = new ws.Server({
68 server: this.config.server
69 });
70 } else {
71 this.server = new ws.Server({
72 port: this.config.port
73 });
74 }
75 this.server.on('connection', this.onConnection.bind(this));
76 this.server.on('close', this.onClose.bind(this));
77 if (callback) {
78 return this.server.once('listening', callback);
79 }
80 };
81
82 Server.prototype.onConnection = function(socket) {
83 this.debug("Browser connected.");
84 socket.on('message', (function(_this) {
85 return function(message) {
86 var data, request;
87 _this.debug("Client message: " + message);
88 request = JSON.parse(message);
89 if (request.command === "hello") {
90 _this.debug("Client requested handshake...");
91 _this.debug("Handshaking with client using protocol " + _this.config.version + "...");
92 data = JSON.stringify({
93 command: 'hello',
94 protocols: ['http://livereload.com/protocols/official-7', 'http://livereload.com/protocols/official-8', 'http://livereload.com/protocols/official-9', 'http://livereload.com/protocols/2.x-origin-version-negotiation', 'http://livereload.com/protocols/2.x-remote-control'],
95 serverName: 'node-livereload'
96 });
97 return socket.send(data);
98 }
99 };
100 })(this));
101 socket.on('error', (function(_this) {
102 return function(err) {
103 return _this.debug("Error in client socket: " + err);
104 };
105 })(this));
106 return socket.on('close', (function(_this) {
107 return function(message) {
108 return _this.debug("Client closed connection");
109 };
110 })(this));
111 };
112
113 Server.prototype.onClose = function(socket) {
114 return this.debug("Socket closed.");
115 };
116
117 Server.prototype.watch = function(paths) {
118 this.debug("Watching " + paths + "...");
119 return this.watcher = chokidar.watch(paths, {
120 ignoreInitial: true,
121 ignored: this.config.exclusions,
122 usePolling: this.config.usePolling
123 }).on('add', this.filterRefresh.bind(this)).on('change', this.filterRefresh.bind(this)).on('unlink', this.filterRefresh.bind(this));
124 };
125
126 Server.prototype.filterRefresh = function(filepath) {
127 var delayedRefresh, exts, fileext;
128 exts = this.config.exts;
129 fileext = path.extname(filepath).substring(1);
130 if (exts.indexOf(fileext) !== -1) {
131 if (this.config.delay) {
132 return delayedRefresh = setTimeout((function(_this) {
133 return function() {
134 clearTimeout(delayedRefresh);
135 return _this.refresh(filepath);
136 };
137 })(this), this.config.delay);
138 } else {
139 return this.refresh(filepath);
140 }
141 }
142 };
143
144 Server.prototype.refresh = function(filepath) {
145 var data;
146 this.debug("Reloading: " + filepath);
147 data = JSON.stringify({
148 command: 'reload',
149 path: filepath,
150 liveCSS: this.config.applyCSSLive,
151 liveImg: this.config.applyImgLive,
152 originalPath: this.config.originalPath,
153 overrideURL: this.config.overrideURL
154 });
155 return this.sendAllClients(data);
156 };
157
158 Server.prototype.alert = function(message) {
159 var data;
160 this.debug("Alert: " + message);
161 data = JSON.stringify({
162 command: 'alert',
163 message: message
164 });
165 return this.sendAllClients(data);
166 };
167
168 Server.prototype.sendAllClients = function(data) {
169 var i, len, ref, results, socket;
170 ref = this.server.clients;
171 results = [];
172 for (i = 0, len = ref.length; i < len; i++) {
173 socket = ref[i];
174 results.push(socket.send(data, (function(_this) {
175 return function(error) {
176 if (error) {
177 return _this.debug(error);
178 }
179 };
180 })(this)));
181 }
182 return results;
183 };
184
185 Server.prototype.debug = function(str) {
186 if (this.config.debug) {
187 return console.log(str + "\n");
188 }
189 };
190
191 Server.prototype.close = function() {
192 this.watcher.close();
193 this.server._server.close();
194 return this.server.close();
195 };
196
197 return Server;
198
199 })();
200
201 exports.createServer = function(config, callback) {
202 var app, requestHandler, server;
203 if (config == null) {
204 config = {};
205 }
206 requestHandler = function(req, res) {
207 if (url.parse(req.url).pathname === '/livereload.js') {
208 res.writeHead(200, {
209 'Content-Type': 'text/javascript'
210 });
211 return res.end(fs.readFileSync(__dirname + '/../ext/livereload.js'));
212 }
213 };
214 if (config.https == null) {
215 app = http.createServer(requestHandler);
216 } else {
217 app = https.createServer(config.https, requestHandler);
218 }
219 if (config.server == null) {
220 config.server = app;
221 }
222 server = new Server(config);
223 if (!config.noListen) {
224 server.listen(callback);
225 }
226 return server;
227 };
228
229}).call(this);