Comparing version 0.2.0 to 0.3.0
@@ -14,54 +14,76 @@ // Load modules | ||
internals.defaults = { | ||
debugEndpoint: '/debug/console', | ||
queryKey: 'debug' | ||
endpoint: '/debug/console', | ||
queryKey: 'debug', | ||
template: 'index' | ||
}; | ||
exports.register = function (pack, options, next) { | ||
exports.register = function (plugin, options, next) { | ||
var settings = Hoek.applyToDefaults(internals.defaults, options || {}); | ||
var tv = new Tv(settings); | ||
var html = tv.getMarkup(); | ||
pack.route({ | ||
method: 'GET', | ||
path: settings.debugEndpoint, | ||
config: { | ||
auth: false, // In case defaults are set otherwise | ||
handler: function () { | ||
tv.start(function () { | ||
return this.reply(html); | ||
} | ||
} | ||
}); | ||
var context = { | ||
endpoint: settings.endpoint | ||
}; | ||
pack.ext('onRequest', function (request, next) { | ||
plugin.views({ | ||
engines: { html: 'handlebars' }, | ||
path: './templates' | ||
}); | ||
var key = settings.queryKey; | ||
if (!request.query[key]) { | ||
return next(); | ||
} | ||
plugin.api({ | ||
queryKey: settings.queryKey, | ||
uri: 'ws://' + tv.settings.host + ':' + tv.settings.port, | ||
endpoint: settings.endpoint | ||
}); | ||
request.plugins.tv = { debugId: request.query[key] }; | ||
delete request.query[key]; | ||
context.host = tv.settings.host; | ||
context.port = tv.settings.port; | ||
delete request.url.search; | ||
delete request.url.query[key]; | ||
plugin.route({ | ||
method: 'GET', | ||
path: settings.endpoint, | ||
config: { | ||
auth: false, // In case defaults are set otherwise | ||
handler: function () { | ||
request.setUrl(Url.format(request.url)); | ||
this.reply.view(settings.template, context); | ||
} | ||
} | ||
}); | ||
return next(); | ||
}); | ||
plugin.route({ | ||
method: 'GET', | ||
path: settings.endpoint + '/{file*2}', | ||
handler: { directory: { path: '../public', listing: false, index: false } } | ||
}); | ||
pack.events.on('request', function (request, report) { | ||
plugin.ext('onRequest', function (request, extNext) { | ||
tv.report(report, request.plugins.tv && request.plugins.tv.debugId); | ||
}); | ||
var key = request.server.plugins.tv.queryKey; | ||
if (!request.query[key]) { | ||
return extNext(); | ||
} | ||
return next(); | ||
}; | ||
request.plugins.tv = { debugId: request.query[key] }; | ||
delete request.query[key]; | ||
delete request.url.search; | ||
delete request.url.query[key]; | ||
request.setUrl(Url.format(request.url)); | ||
return extNext(); | ||
}); | ||
plugin.events.on('request', function (request, report) { | ||
tv.report(report, request.plugins.tv && request.plugins.tv.debugId); | ||
}); | ||
next(); | ||
}); | ||
}; |
// Load modules | ||
var Os = require('os'); | ||
var Fs = require('fs'); | ||
var Handlebars = require('handlebars'); | ||
var Ws = require('ws'); | ||
var Hoek = require('hoek'); | ||
var Http = require('http'); | ||
@@ -16,5 +14,4 @@ | ||
internals.defaults = { | ||
websocketPort: 3000, | ||
host: '0.0.0.0', | ||
indexTemplatePath: __dirname + '/../templates/index.html' | ||
port: 3000, | ||
host: '0.0.0.0' | ||
}; | ||
@@ -26,11 +23,21 @@ | ||
Hoek.assert(this.constructor === internals.Tv, 'Tv must be instantiated using new'); | ||
var self = this; | ||
this.settings = Hoek.applyToDefaults(internals.defaults, config || {}); | ||
this._subscribers = {}; // Map: debug session -> [ subscriber ] | ||
this._subscribers = {}; // Map: debug session -> [ subscriber ] | ||
}; | ||
var indexTemplateSource = this.settings.indexTemplate || Fs.readFileSync(this.settings.indexTemplatePath, 'utf8'); | ||
this._compiledIndexTemplate = Handlebars.compile(indexTemplateSource); | ||
var ws = new Ws.Server({ host: this.settings.host, port: this.settings.websocketPort }); | ||
internals.Tv.prototype.start = function (callback) { | ||
var self = this; | ||
callback = callback || function () { }; | ||
var server = Http.createServer(function (req, res) { | ||
res.writeHead(501, {'Content-Type': 'text/plain'}); | ||
res.end('Not implemented'); | ||
}); | ||
var ws = new Ws.Server({ server: server }); | ||
ws.on('connection', function (socket) { | ||
@@ -48,2 +55,11 @@ | ||
}); | ||
server.listen(this.settings.port, this.settings.host, function () { | ||
var address = server.address(); // Update the port and host with what was actually bound | ||
self.settings.port = address.port; | ||
self.settings.host = self.settings.host || address.address; | ||
callback(); | ||
}); | ||
}; | ||
@@ -83,12 +99,1 @@ | ||
}; | ||
internals.Tv.prototype.getMarkup = function () { | ||
var host = (this.settings.host !== '0.0.0.0') ? this.settings.host : Os.hostname(); | ||
var port = this.settings.websocketPort; | ||
var data = { host: host, port: port }; | ||
return this._compiledIndexTemplate(data) | ||
}; | ||
{ | ||
"name": "tv", | ||
"description": "Interactive debug console plugin for hapi", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"author": "Eran Hammer <eran@hueniverse.com> (http://hueniverse.com)", | ||
@@ -20,15 +20,15 @@ "contributors":[ | ||
"engines": { | ||
"node": "0.8.x" | ||
"node": "0.10.x" | ||
}, | ||
"dependencies": { | ||
"hoek": "0.7.x", | ||
"ws": "0.4.x", | ||
"handlebars": "1.0.x" | ||
"hoek": "0.8.x", | ||
"ws": "0.4.x" | ||
}, | ||
"peerDependencies": { | ||
"hapi": "0.16.x" | ||
"hapi": "1.x.x" | ||
}, | ||
"devDependencies": { | ||
"hapi": "0.16.x", | ||
"lab": "0.0.x", | ||
"hapi": "1.x.x", | ||
"handlebars": "1.0.x", | ||
"lab": "0.1.x", | ||
"complexity-report": "0.x.x" | ||
@@ -35,0 +35,0 @@ }, |
@@ -0,0 +0,0 @@ <a href="https://github.com/spumko"><img src="https://raw.github.com/spumko/spumko/master/images/from.png" align="right" /></a> |
@@ -29,6 +29,6 @@ // Load modules | ||
var options = { | ||
websocketPort: 3007 | ||
port: 0 | ||
}; | ||
server = new Hapi.Server(); | ||
server = new Hapi.Server(0); | ||
@@ -40,7 +40,7 @@ server.route({ | ||
return this.reply('1'); | ||
this.reply('1'); | ||
} | ||
}); | ||
server.plugin.allow({ ext: true }).require('../', options, function (err) { | ||
server.pack.allow({ ext: true, views: true }).require('../', options, function (err) { | ||
@@ -54,5 +54,6 @@ expect(err).to.not.exist; | ||
server.inject({ method: 'GET', url: '/debug/console' }, function (res) { | ||
server.inject('/debug/console', function (res) { | ||
expect(res.result).to.contain('<!DOCTYPE html>'); | ||
expect(res.statusCode).to.equal(200); | ||
expect(res.result).to.contain('Debug Console'); | ||
done(); | ||
@@ -64,3 +65,3 @@ }); | ||
var ws = new Ws('ws://localhost:3007'); | ||
var ws = new Ws(server.plugins.tv.uri); | ||
@@ -73,3 +74,3 @@ ws.on('open', function () { | ||
server.inject({ method: 'GET', url: '/?debug=123' }, function (res) { | ||
server.inject('/?debug=123', function (res) { | ||
@@ -76,0 +77,0 @@ expect(res.result).to.equal('1'); |
// Load modules | ||
var Lab = require('lab'); | ||
var Http = require('http'); | ||
var Ws = require('ws'); | ||
@@ -41,3 +42,3 @@ var Tv = require('../lib/tv'); | ||
var tv = new Tv({ websocketPort: 3001 }); | ||
var tv = new Tv({ port: 0 }); | ||
}; | ||
@@ -54,3 +55,3 @@ | ||
expect(tv.settings.host).to.equal('0.0.0.0'); | ||
expect(tv.settings.websocketPort).to.equal(3000); | ||
expect(tv.settings.port).to.equal(3000); | ||
done(); | ||
@@ -61,6 +62,6 @@ }); | ||
var tv = new Tv({ host: 'localhost', websocketPort: 3002 }); | ||
var tv = new Tv({ host: 'localhost', port: 3002 }); | ||
expect(tv.settings.host).to.equal('localhost'); | ||
expect(tv.settings.websocketPort).to.equal(3002); | ||
expect(tv.settings.port).to.equal(3002); | ||
done(); | ||
@@ -71,16 +72,48 @@ }); | ||
var config = { host: 'localhost', websocketPort: 3010 } | ||
var config = { host: 'localhost', port: 0 }; | ||
var tv = new Tv(config); | ||
var ws = new Ws("ws://" + config.host + ':' + config.websocketPort); | ||
ws.readyState = Ws.OPEN; | ||
tv.start(function () { | ||
ws.on('open', function () { | ||
var ws = new Ws("ws://" + tv.settings.host + ':' + tv.settings.port); | ||
ws.readyState = Ws.OPEN; | ||
ws.send("test1"); | ||
setTimeout(function () { | ||
ws.on('open', function () { | ||
expect(tv._subscribers["test1"]).to.exist; | ||
ws.send("test1"); | ||
setTimeout(function () { | ||
expect(tv._subscribers["test1"]).to.exist; | ||
done(); | ||
}, 100); | ||
}); | ||
}); | ||
}); | ||
it('responds with not implemented when making a non-ws request', function (done) { | ||
var config = { host: 'localhost', port: 0 }; | ||
var tv = new Tv(config); | ||
tv.start(function () { | ||
var options = { | ||
hostname: tv.settings.host, | ||
port: tv.settings.port, | ||
path: '/', | ||
method: 'GET' | ||
}; | ||
var req = Http.request(options, function (res) { | ||
expect(res.statusCode).to.equal(501); | ||
done(); | ||
}, 100); | ||
}); | ||
req.once('error', function (err) { | ||
done(); | ||
}); | ||
req.end(); | ||
}); | ||
@@ -94,3 +127,3 @@ }); | ||
var tv = new Tv({ websocketPort: 3003 }); | ||
var tv = new Tv({ port: 0 }); | ||
tv._subscribers['*'] = [{ | ||
@@ -111,3 +144,3 @@ readyState: Ws.OPEN, | ||
var tv = new Tv({ websocketPort: 3004 }); | ||
var tv = new Tv({ port: 0 }); | ||
tv._subscribers['*'] = [{ | ||
@@ -136,3 +169,3 @@ readyState: Ws.OPEN, | ||
var tv = new Tv({ websocketPort: 3005 }); | ||
var tv = new Tv({ port: 0 }); | ||
tv._subscribers['*'] = [{ | ||
@@ -150,16 +183,2 @@ readyState: 'none', | ||
}); | ||
describe('#getMarkup', function () { | ||
it('includes the hostname and port in the source', function (done) { | ||
var tv = new Tv({ host: 'localhost', websocketPort: 3006 }); | ||
var html = tv.getMarkup(); | ||
expect(html).to.contain('localhost'); | ||
expect(html).to.contain('3006'); | ||
done(); | ||
}); | ||
}); | ||
}); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance 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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
171864
3
20
2531
1
4
3
+ Addedalce@1.0.0(transitive)
+ Addedboom@1.2.1(transitive)
+ Addedcatbox@1.2.0(transitive)
+ Addedconfidence@0.8.1(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedcryptiles@1.0.1(transitive)
+ Addedesprima@1.0.4(transitive)
+ Addedestraverse@1.3.2(transitive)
+ Addedhapi@1.20.0(transitive)
+ Addedhawk@1.1.2(transitive)
+ Addedhoek@0.8.51.5.22.16.3(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addediron@1.0.0(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedjoi@2.9.0(transitive)
+ Addedjoi-v1@1.2.5(transitive)
+ Addedlru-cache@2.3.1(transitive)
+ Addedminimist@0.0.10(transitive)
+ Addedmultiparty@3.0.0(transitive)
+ Addedoptimist@0.6.1(transitive)
+ Addedreadable-stream@1.1.14(transitive)
+ Addedsemver@2.0.11(transitive)
+ Addedshot@1.7.0(transitive)
+ Addedsntp@1.0.9(transitive)
+ Addedstream-counter@0.2.0(transitive)
+ Addedstring_decoder@0.10.31(transitive)
- Removedhandlebars@1.0.x
- Removedamdefine@1.0.1(transitive)
- Removedasync@0.1.22(transitive)
- Removedaws-sign@0.2.1(transitive)
- Removedboom@0.3.8(transitive)
- Removedcatbox@0.4.0(transitive)
- Removedcombined-stream@0.0.7(transitive)
- Removedcookie-jar@0.2.0(transitive)
- Removedcryptiles@0.1.3(transitive)
- Removeddelayed-stream@0.0.5(transitive)
- Removedforever-agent@0.2.0(transitive)
- Removedform-data@0.0.10(transitive)
- Removedformidable@1.0.17(transitive)
- Removedhandlebars@1.0.12(transitive)
- Removedhapi@0.16.0(transitive)
- Removedhawk@0.10.20.11.1(transitive)
- Removedhoek@0.6.20.7.6(transitive)
- Removediron@0.2.4(transitive)
- Removedjoi@0.2.6(transitive)
- Removedjson-stringify-safe@3.0.0(transitive)
- Removedlru-cache@2.2.4(transitive)
- Removednode-uuid@1.4.8(transitive)
- Removedoauth-sign@0.2.0(transitive)
- Removedoptimist@0.3.7(transitive)
- Removedoz@0.1.2(transitive)
- Removedqs@0.5.6(transitive)
- Removedrequest@2.16.6(transitive)
- Removedshot@0.1.3(transitive)
- Removedsntp@0.1.4(transitive)
- Removedsource-map@0.1.43(transitive)
- Removedtunnel-agent@0.2.0(transitive)
- Removeduglify-js@2.3.6(transitive)
Updatedhoek@0.8.x