Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tv

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tv - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

examples/simple.js

86

lib/index.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc