Comparing version 0.0.7 to 0.0.8
@@ -0,0 +0,0 @@ |
@@ -1,19 +0,30 @@ | ||
var util = require('util'); | ||
var Server = require('./server'); | ||
var Client = require('./client'); | ||
var debug = require('debug')('tinylr'); | ||
// Need to keep track of LR servers when notifying | ||
var servers = []; | ||
module.exports = tinylr; | ||
// Expose Server / Client objects | ||
tinylr.Server = Server; | ||
tinylr.Client = Client; | ||
// and the middleware helpers | ||
tinylr.middleware = middleware; | ||
tinylr.changed = changed; | ||
// Main entry point | ||
function tinylr(opts) { | ||
return new Server(opts); | ||
var srv = new Server(opts); | ||
servers.push(srv); | ||
return srv; | ||
} | ||
var util = require('util'); | ||
// A facade to Server#handle | ||
function middleware(opts) { | ||
var srv = new Server(opts); | ||
servers.push(srv); | ||
return function tinylr(req, res, next) { | ||
@@ -23,1 +34,14 @@ srv.handler(req, res, next); | ||
} | ||
// Changed helper, helps with notifying the server of a file change | ||
function changed(done) { | ||
var files = [].slice.call(arguments); | ||
if (files[files.length - 1] === 'function') done = files.pop(); | ||
done = typeof done === 'function' ? done : function() {}; | ||
debug('Notifying %d servers - Files: ', servers.length, files); | ||
servers.forEach(function(srv) { | ||
var params = { params: { files: files }}; | ||
srv && srv.changed(params); | ||
}); | ||
done(); | ||
} |
@@ -330,2 +330,3 @@ /*! Copyright (c) 2010-2012 Andrey TarantsovCopyright (c) 2010-2012 Andrey Tarantsov */ | ||
this.handshake_timeout = 5000; | ||
this.animate = false; | ||
} | ||
@@ -510,3 +511,3 @@ Options.prototype.set = function(name, value) { | ||
if (options.liveImg) { | ||
if (path.match(/\.(jpe?g|png|gif)$/i)) { | ||
if (path.match(/\.(jpe?g|png|gif|svg)$/i)) { | ||
this.reloadImages(path); | ||
@@ -923,5 +924,5 @@ return; | ||
LiveReload.prototype.performReload = function(message) { | ||
var _ref, _ref2; | ||
var _base, _ref, _ref2; | ||
this.log("LiveReload received reload request for " + message.path + "."); | ||
return this.reloader.reload(message.path, { | ||
this.reloader.reload(message.path, { | ||
liveCSS: (_ref = message.liveCSS) != null ? _ref : true, | ||
@@ -933,2 +934,3 @@ liveImg: (_ref2 = message.liveImg) != null ? _ref2 : true, | ||
}); | ||
return typeof (_base = this.listeners).reload === "function" ? _base.reload() : void 0; | ||
}; | ||
@@ -987,2 +989,20 @@ | ||
LiveReload.prototype.setUpCSSTransitions = function() { | ||
var cssText, head, prefixer, styleNode; | ||
prefixer = function(declaration) { | ||
return (['-webkit-', '-moz-', ''].map(function(item) { | ||
return "" + item + declaration; | ||
})).join(' '); | ||
}; | ||
head = document.getElementsByTagName('head')[0]; | ||
styleNode = document.createElement("style"); | ||
cssText = ".livereload-reloaded * { " + (prefixer('transition: all 280ms ease-out;')) + " }"; | ||
if (styleNode.styleSheet) { | ||
styleNode.styleSheet.cssText = cssText; | ||
} else { | ||
styleNode.appendChild(document.createTextNode(cssText)); | ||
} | ||
return head.appendChild(styleNode); | ||
}; | ||
return LiveReload; | ||
@@ -1012,4 +1032,15 @@ | ||
}; | ||
LessPlugin.prototype.clearGeneratedStyles = function() { | ||
var _i, _ref, style; | ||
_ref = document.getElementsByTagName('style'); | ||
for (var _i = _ref.length - 1; _i >= 0; _i--) { | ||
style = _ref[_i]; | ||
if (style.id && style.id.match(/less\:/)) { | ||
style.parentNode.removeChild(style); | ||
} | ||
}; | ||
} | ||
LessPlugin.prototype.reloadLess = function(path) { | ||
var link, links, _i, _len; | ||
this.clearGeneratedStyles(); | ||
links = (function() { | ||
@@ -1047,5 +1078,15 @@ var _i, _len, _ref, _results; | ||
// startup | ||
var CustomEvents, LiveReload, k; | ||
var CustomEvents, LiveReload, k, parent = window; | ||
CustomEvents = __customevents; | ||
LiveReload = window.LiveReload = new (__livereload.LiveReload)(window); | ||
try { | ||
while (parent !== parent.parent) { | ||
parent = parent.parent; | ||
if (parent.LiveReload !== undefined) { | ||
parent.LiveReload.shutDown(); | ||
} | ||
} | ||
} catch (e) { | ||
// Mostly a security exception about same-origin problem | ||
} | ||
for (k in window) { | ||
@@ -1061,2 +1102,5 @@ if (k.match(/^LiveReloadPlugin/)) { | ||
LiveReload.on('connect', function() { | ||
if (!!/true|1$/.test(LiveReload.options.animate)) { | ||
LiveReload.setUpCSSTransitions(); | ||
} | ||
return CustomEvents.fire(document, 'LiveReloadConnect'); | ||
@@ -1067,2 +1111,12 @@ }); | ||
}); | ||
LiveReload.on('reload', function() { | ||
var existingHtmlClass, html, reloadedClass, _ref; | ||
html = document.body.parentNode; | ||
reloadedClass = ' livereload-reloaded'; | ||
existingHtmlClass = (_ref = html.getAttribute('class')) != null ? _ref : ''; | ||
html.setAttribute('class', "" + (existingHtmlClass.replace(reloadedClass, '')) + " " + reloadedClass); | ||
return setTimeout((function() { | ||
return html.setAttribute('class', existingHtmlClass.replace(reloadedClass, '')); | ||
}), 300); | ||
}); | ||
CustomEvents.bind(document, 'LiveReloadShutDown', function() { | ||
@@ -1069,0 +1123,0 @@ return LiveReload.shutDown(); |
@@ -1,13 +0,17 @@ | ||
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')('tinylr:server'); | ||
var Client = require('./client'); | ||
var constants = require('constants'); | ||
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')('tinylr:server'); | ||
var Client = require('./client'); | ||
var constants = require('constants'); | ||
// Middleware fallbacks | ||
var bodyParser = require('body-parser')() | ||
var queryParser = require('./middleware/query')(); | ||
var config = require('../package.json'); | ||
@@ -29,3 +33,2 @@ | ||
this.clients = {}; | ||
this.configure(options.app); | ||
@@ -41,7 +44,6 @@ } | ||
if(!app) { | ||
if (!app) { | ||
if ((this.options.key && this.options.cert) || this.options.pfx) { | ||
this.server = https.createServer(this.options, this.handler.bind(this)); | ||
} | ||
else { | ||
} else { | ||
this.server = http.createServer(this.handler.bind(this)); | ||
@@ -60,3 +62,3 @@ } | ||
done = done || function() {}; | ||
if(port !== self.options.port) { | ||
if (port !== self.options.port) { | ||
debug('Warn: LiveReload port is not standard (%d). You are listening on %d', self.options.port, port); | ||
@@ -78,17 +80,18 @@ debug('You\'ll need to rely on the LiveReload snippet'); | ||
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'); | ||
if(middleware) { | ||
if(!req.params && req.query) req.params = req.query; | ||
this.handle(req, res, next); | ||
return this; | ||
} | ||
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; | ||
}); | ||
// req | ||
// .on('end', this.handle.bind(this, req, res)) | ||
// .on('data', function(chunk) { | ||
// req.data = req.data || ''; | ||
// req.data += chunk; | ||
// }); | ||
@@ -98,16 +101,20 @@ 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'; | ||
req.body = {}; | ||
req.params = {}; | ||
try { | ||
req.body = JSON.parse(req.data); | ||
} catch(e) {} | ||
if(url.query) req.params = qs.parse(url.query); | ||
// todo: parse Accept header | ||
res.setHeader('Content-Type', 'application/json'); | ||
@@ -118,4 +125,4 @@ | ||
var respond = this.emit(route, req, res); | ||
if(respond) return; | ||
if(middleware) return next(); | ||
if (respond) return; | ||
if (middleware) return next(); | ||
@@ -148,4 +155,2 @@ res.writeHead(404); | ||
Server.prototype.close = function close(req, res) { | ||
if(res) res.end(); | ||
Object.keys(this.clients).forEach(function(id) { | ||
@@ -155,3 +160,6 @@ this.clients[id].close(); | ||
if(this.server._handle) this.server.close(this.emit.bind(this, 'close')); | ||
if (this.server._handle) this.server.close(this.emit.bind(this, 'close')); | ||
if (res) res.end(); | ||
}; | ||
@@ -164,3 +172,3 @@ | ||
if(e.code !== constants.EADDRINUSE) return; | ||
if (e.code !== constants.EADDRINUSE) return; | ||
console.error(); | ||
@@ -179,13 +187,18 @@ console.error('You already have a server listening on %s', this.port); | ||
Server.prototype.changed = function changed(req, res) { | ||
var files = []; | ||
if(req && req.body && req.body.files) files = req.body.files; | ||
if(req && req.params && req.params.files) files = req.params.files; | ||
var files = this.param('files', req); | ||
// normalize files array | ||
files = Array.isArray(files) ? files : | ||
typeof files === 'string' ? files.split(/[\s,]/) : | ||
[]; | ||
debug('Changed event (Files: %s)', files.join(' ')); | ||
var clients = this.notifyClients(files); | ||
if (!res) return; | ||
debug('Changed event (Files: %s)', files.join(' ')); | ||
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) { | ||
@@ -201,10 +214,19 @@ var client = this.clients[id]; | ||
if(!res) return; | ||
return clients; | ||
}; | ||
res.write(JSON.stringify({ | ||
clients: clients, | ||
files: files | ||
})); | ||
// 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; | ||
res.end(); | ||
// 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; | ||
}; | ||
@@ -211,0 +233,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "Tiny LiveReload server, background-friendly", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"homepage": "https://github.com/mklabs/tiny-lr", | ||
@@ -15,18 +15,30 @@ "repository": { | ||
"prepublish:": "npm test", | ||
"test": "mocha --reporter list", | ||
"test": "mocha --reporter spec test/wd test", | ||
"test-debug": "DEBUG=tinylr:* mocha --reporter list", | ||
"test-debug-all": "DEBUG=* mocha --reporter list" | ||
"test-debug-all": "DEBUG=* mocha --reporter list", | ||
"pretest": "npm run phantom-start", | ||
"posttest": "npm run phantom-stop", | ||
"phantom-start": "sh scripts/phantom-start", | ||
"phantom-stop": "sh scripts/phantom-stop", | ||
"serve": "node examples/express/server.js", | ||
"post-change": "sh scripts/post-change", | ||
"get-change": "curl http://localhost:35729/changed?files=site.css" | ||
}, | ||
"dependencies": { | ||
"qs": "~0.5.2", | ||
"faye-websocket": "~0.4.3", | ||
"qs": "^0.6.6", | ||
"faye-websocket": "^0.7.2", | ||
"noptify": "~0.0.3", | ||
"debug": "~0.8.0" | ||
"debug": "^0.8.1", | ||
"body-parser": "^1.2.0", | ||
"parseurl": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.7.1", | ||
"request": "~2.12.0", | ||
"supertest": "~0.4.2", | ||
"express": "~3.0.6", | ||
"connect": "~2.7.2" | ||
"mocha": "^1.18.2", | ||
"request": "^2.34.0", | ||
"supertest": "^0.12.0", | ||
"express": "^4.1.1", | ||
"connect": "^2.14.5", | ||
"body-parser": "^1.0.2", | ||
"phantomjs": "^1.9.7-5", | ||
"wd": "^0.2.21" | ||
}, | ||
@@ -33,0 +45,0 @@ "config": { |
@@ -1,3 +0,2 @@ | ||
tiny-lr | ||
------- | ||
# tiny-lr [![Build Status](https://travis-ci.org/mklabs/tiny-lr.svg?branch=master)](https://travis-ci.org/mklabs/tiny-lr) | ||
@@ -7,3 +6,2 @@ This script manages a tiny [LiveReload](http://livereload.com/) server | ||
[![Build Status](https://travis-ci.org/mklabs/tiny-lr.svg?branch=master)](https://travis-ci.org/mklabs/tiny-lr) | ||
[![NPM](https://nodei.co/npm/tiny-lr.png?compact=true)](https://nodei.co/npm/tiny-lr/) | ||
@@ -34,2 +32,6 @@ | ||
# from a JSON file | ||
node -pe 'JSON.stringify({ files: ["some.css", "files.css"] })' > files.json | ||
curl -X POST -d @files.json http://localhost:35729 | ||
As for the livereload client, you need to install the browser extension: | ||
@@ -74,8 +76,3 @@ http://feedback.livereload.com/knowledgebase/articles/86242-how-do-i-install-and-use-the-browser-extensions- | ||
tinylr().listen(port, function() { | ||
if(err) { | ||
// deal with err | ||
return; | ||
} | ||
console.log('... Listening on %s (pid: %s) ...', port); | ||
console.log('... Listening on %s ...', port); | ||
}) | ||
@@ -106,3 +103,3 @@ ``` | ||
To use as a connect / express middleware, tiny-lr needs query / | ||
bodyParse middlewares prior in the stack. | ||
bodyParser middlewares prior in the stack (to handle POST requests) | ||
@@ -113,12 +110,21 @@ Any handled requests ends at the tinylr level, not found and errors are | ||
```js | ||
var port = process.env.LR_PORT || process.env.PORT || 35729; | ||
var path = require('path'); | ||
var express = require('express'); | ||
var tinylr = require('tiny-lr'); | ||
var body = require('body-parser'); | ||
var app = express(); | ||
// This binds both express app and tinylr on the same port | ||
var app = express(); | ||
app.use(express.query()) | ||
.use(express.bodyParser()) | ||
app | ||
.use(body()) | ||
.use(tinylr.middleware({ app: app })) | ||
.use(express.static(path.resolve('./'))) | ||
.use(express.directory(path.resolve('./'))) | ||
.listen(35729, function() { | ||
console.log('Listening on %d', 35729); | ||
}) | ||
.listen(port, function() { | ||
console.log('listening on %d', port); | ||
}); | ||
``` | ||
@@ -132,4 +138,3 @@ | ||
You can also start two different servers, one on your app port, the | ||
other listening on the LiveReload port. Check the | ||
`examples/express/server.js` file to see how. | ||
other listening on the LiveReload port. | ||
@@ -136,0 +141,0 @@ ### Using grunt |
@@ -9,13 +9,8 @@ | ||
var listen = require('./helpers/listen'); | ||
describe('tiny-lr', function() { | ||
before(function(done) { | ||
this.app = new Server; | ||
this.server = this.app.server; | ||
this.request = request(this.server) | ||
.get('/') | ||
.expect(200, done); | ||
}); | ||
before(listen()); | ||
it('accepts ws clients', function(done) { | ||
@@ -22,0 +17,0 @@ var url = parse(this.request.url); |
var http = require('http'); | ||
var assert = require('assert'); | ||
var connect = require('connect'); | ||
var express = require('express'); | ||
var request = require('supertest'); | ||
var debug = require('debug')('tinylr:test'); | ||
var Server = require('..').Server; | ||
var http = require('http'); | ||
var assert = require('assert'); | ||
var connect = require('connect'); | ||
var express = require('express'); | ||
var request = require('supertest'); | ||
var debug = require('debug')('tinylr:test'); | ||
var bodyParser = require('body-parser'); | ||
var Server = require('..').Server; | ||
@@ -14,3 +15,3 @@ var npmenv = process.env; | ||
describe('Connect Middleware', suite('Connect Middleware', connect())); | ||
// describe('Connect Middleware', suite('Connect Middleware', connect())); | ||
describe('Express Middleware', suite('Express Middleware', express())); | ||
@@ -26,4 +27,3 @@ | ||
this.app | ||
.use(connect.query()) | ||
.use(connect.bodyParser()) | ||
.use(bodyParser()) | ||
.use(this.lr.handler.bind(this.lr)); | ||
@@ -53,4 +53,4 @@ | ||
.get('/whatev') | ||
.expect('Content-Type', 'text/plain') | ||
.expect('Cannot GET /whatev') | ||
.expect('Content-Type', 'text/html') | ||
.expect(/Cannot GET \/whatev/) | ||
.expect(404, done); | ||
@@ -90,3 +90,3 @@ }); | ||
it.skip('with no clients, some files', function(done) { | ||
it('with no clients, some files', function(done) { | ||
var data = { clients: [], files: ['cat.css', 'sed.css', 'ack.js'] }; | ||
@@ -98,3 +98,3 @@ | ||
.expect('Content-Type', /json/) | ||
.expect(JSON.stringify(data)) | ||
// .expect(JSON.stringify(data)) | ||
.expect(200, done); | ||
@@ -113,3 +113,3 @@ }); | ||
describe.skip('GET /kill', function() { | ||
describe('GET /kill', function() { | ||
it('shutdown the server', function(done) { | ||
@@ -119,7 +119,3 @@ var server = this.server; | ||
.get('/kill') | ||
.expect(200, function(err) { | ||
if(err) return done(err); | ||
assert.ok(!server._handle); | ||
done(); | ||
}); | ||
.expect(200, done); | ||
}); | ||
@@ -126,0 +122,0 @@ }); |
@@ -7,8 +7,7 @@ | ||
var listen = require('./helpers/listen'); | ||
describe('tiny-lr', function() { | ||
beforeEach(function() { | ||
this.app = new Server; | ||
this.server = this.app.server; | ||
}); | ||
before(listen()); | ||
@@ -68,2 +67,3 @@ describe('GET /', function() { | ||
.post('/changed') | ||
// .type('json') | ||
.send({ files: data.files }) | ||
@@ -87,8 +87,8 @@ .expect('Content-Type', /json/) | ||
it('shutdown the server', function(done) { | ||
var server = this.server; | ||
request(server) | ||
var srv = this.server; | ||
request(srv) | ||
.get('/kill') | ||
.expect(200, function(err) { | ||
if(err) return done(err); | ||
assert.ok(!server._handle); | ||
assert.ok(!srv._handle); | ||
done(); | ||
@@ -95,0 +95,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74490
27
1689
371
6
8
15
+ Addedbody-parser@^1.2.0
+ Addedparseurl@^1.0.1
+ Addedbody-parser@1.20.3(transitive)
+ Addedbytes@3.1.2(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedcontent-type@1.0.5(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addeddestroy@1.2.0(transitive)
+ Addedee-first@1.1.1(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfaye-websocket@0.7.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addedhttp-parser-js@0.5.8(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedmedia-typer@0.3.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedms@2.0.0(transitive)
+ Addedobject-inspect@1.13.2(transitive)
+ Addedon-finished@2.4.1(transitive)
+ Addedparseurl@1.3.3(transitive)
+ Addedqs@0.6.66.13.0(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedtype-is@1.6.18(transitive)
+ Addedunpipe@1.0.0(transitive)
+ Addedwebsocket-driver@0.7.4(transitive)
+ Addedwebsocket-extensions@0.1.4(transitive)
- Removedfaye-websocket@0.4.4(transitive)
- Removedqs@0.5.6(transitive)
Updateddebug@^0.8.1
Updatedfaye-websocket@^0.7.2
Updatedqs@^0.6.6