Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:walmartlabs/hapi into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
thegoleffect committed Aug 31, 2012
2 parents c636a76 + 86945f4 commit cf406d2
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 115 deletions.
4 changes: 2 additions & 2 deletions examples/config/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ internals.onPostRoute = function (request, next) {
internals.onUnknownRoute = function (request, next) {

Hapi.Log.info('onUnknownRoute');
request._raw.res.writeHead(404);
request._raw.res.end();
request.raw.res.writeHead(404);
request.raw.res.end();
next();
};

Expand Down
43 changes: 10 additions & 33 deletions lib/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ exports.Client = Client = function (options) {

this.settings = options;

// Compile rules

this.rules = Rules.compile(this.settings.rules);
if (this.rules instanceof Error) {

Utils.abort('Bad rule: ' + this.rules.message);
};

// Create internal connection

var imp = (this.settings.engineType === 'redis' ? Redis : null);
Expand Down Expand Up @@ -65,25 +57,21 @@ Client.prototype.stop = function () {
};


Client.prototype.get = function (/* key, rule, callback */) {
Client.prototype.get = function (key, rules, callback) {

var self = this;

var key = arguments[0];
var rule = (arguments.length === 3 ? arguments[1] : null);
var callback = (arguments.length === 3 ? arguments[2] : arguments[1])

if (this.connection) {

if (Rules.isCached(key, rule || this.rules)) {
if (Rules.isCached(key, rules)) {

this.connection.get(key, function (err, item, created) {

if (err === null) {

if (item) {

if (Rules.isExpired(key, rule || self.rules, created) === false) {
if (Rules.isExpired(key, rules, created) === false) {

// TODO: Implement stale

Expand Down Expand Up @@ -122,23 +110,18 @@ Client.prototype.get = function (/* key, rule, callback */) {
};


Client.prototype.set = function (/* key, value, rule, callback */) {

var key = arguments[0];
var value = arguments[1];
var rule = (arguments.length === 4 ? arguments[2] : null);
var callback = (arguments.length === 4 ? arguments[3] : arguments[2])
Client.prototype.set = function (key, value, rules, callback) {

if (this.connection) {

var expiresInSec = Rules.expireInSec(key, rule || this.rules);
var expiresInSec = Rules.expireInSec(key, rules);
if (expiresInSec !== null) {

this.connection.set(key, value, expiresInSec, callback);
}
else {

// Not cachable (or bad rule)
// Not cachable (or bad rules)
callback(null);
}
}
Expand All @@ -164,30 +147,24 @@ Client.prototype.drop = function (key, callback) {
};


Client.prototype.compile = function (rule) {

return Rules.compile(rule);
};


exports.Set = Set = function (config, cache) {
exports.Set = Set = function (rules, cache) {

this.cache = cache;
this.rule = this.cache.compile(config);
this.rules = Rules.compile(rules);

return this;
};


Set.prototype.get = function (key, callback) {

this.cache.get(key, this.rule, callback);
this.cache.get(key, this.rules, callback);
};


Set.prototype.set = function (key, value, callback) {

this.cache.get(key, value, this.rule, callback);
this.cache.set(key, value, this.rules, callback);
};


Expand Down
73 changes: 63 additions & 10 deletions lib/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,86 @@

// Load modules

var Err = require('./error');
var Os = require('os');
var Websocket = require('ws');
var Log = require('./log');


// Declare internals

var internals = {

events: {} // Map: debug session -> events array
host: null,
port: null,
subscribers: {} // Map: debug session -> [ subscriber ]
};


exports.report = function (session, event) {

internals.events[session] = internals.events[session] || [];
internals.events[session].push(event);
};
var subscribers = internals.subscribers[session];
var valid = [];
if (subscribers) {

for (var i = 0, il = subscribers.length; i < il; ++i) {

try {

if (subscribers[i].readyState === Websocket.OPEN) {

exports.session = function (request) {
subscribers[i].send(JSON.stringify(event, null, 4));
valid.push(subscribers[i]);
}
}
catch (err) {

if (internals.events[request.params.id]) {
// Remove subscriber on any send error
}
}

request.reply(internals.events[request.params.id]);
internals.subscribers[session] = valid;
}
else {
};


exports.initialize = function (host, port) {

internals.host = (host !== '0.0.0.0' ? host : Os.hostname());
internals.port = port;

var ws = new Websocket.Server({ host: host, port: port });
ws.on('connection', function (socket) {

socket.on('message', function (message) {

request.reply(Err.notFound());
internals.subscribers[message] = internals.subscribers[message] || [];
internals.subscribers[message].push(socket);
Log.info('Debug subscription requested: ' + message);
});
});
};


exports.console = {

authentication: 'none',

handler: function (request) {

var html = '<!DOCTYPE html><html lang="en"><head><title>Debug Console</title><meta http-equiv="Content-Language" content="en-us"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body>' +
'<script>\n' +
'function htmlEscape(string) { return string.replace(/&/g,"&amp;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/"/g,"&quot;");}\n' +
'var ws = new WebSocket("ws://' + internals.host + ':' + internals.port + '");\n' +
'ws.onopen = function() {};\n' +
'ws.onmessage = function(message) { document.getElementById("stream").innerHTML += htmlEscape(message.data) + "<p />" };\n' +
'ws.onclose = function() {};\n' +
'</script>\n' +
'<input id="session" /><button id="subscribe" onclick="ws.send(document.getElementById(\'session\').value);">Subscribe</button>' +
'<pre><div id="stream"></div></pre></body></html>';

request.reply(html);
}
};



3 changes: 2 additions & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ exports.cache = {

exports.debug = {

debugEndpoint: '/debug/session',
websocketPort: 3000,
debugEndpoint: '/debug/console',
queryKey: 'debug'
};

10 changes: 5 additions & 5 deletions lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ internals.pack = function (ts, level, msg, request) {

if (typeof request !== 'undefined' &&
request &&
request._raw &&
request._raw.req) {
request.raw &&
request.raw.req) {

if (typeof request._raw.req === 'object') {
if (typeof request.raw.req === 'object') {

package.cdr = Qs.stringify(request._raw.req);
package.cdr = Qs.stringify(request.raw.req);
}
else {

package.cdr = request._raw.req;
package.cdr = request.raw.req;
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/monitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ Monitor.prototype._request = function (client, url) {

return function (request) {

var req = request._raw.req;
var res = request._raw.res;
var req = request.raw.req;
var res = request.raw.res;

var modified_headers = Utils.clone(req.headers);

Expand Down
12 changes: 6 additions & 6 deletions lib/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exports.read = function (request, config, next) {

// Check content type (defaults to 'application/json')

var req = request._raw.req;
var req = request.raw.req;
var contentType = req.headers['content-type'];
var mime = (contentType ? contentType.split(';')[0] : 'application/json');
var parserFunc = null;
Expand Down Expand Up @@ -80,16 +80,16 @@ exports.read = function (request, config, next) {

if (isBailed) {

return;
return; // next() already called
}

request.rawBody = payload;

if (level === 'parse') {

if (payload) {
request.payload = {};

request.payload = {};
if (payload) {

try {

Expand All @@ -99,10 +99,10 @@ exports.read = function (request, config, next) {

return next(Err.badRequest('Invalid JSON body'));
}

next();
}
}

return next();
});
};

45 changes: 28 additions & 17 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ exports = module.exports = Request = function (server, req, res) {
// userId
// reply()

// Private members
// Semi-public members

this._raw = {
this.response = {}; // { result, error, options }
this.raw = {

req: req,
res: res
};

// Private members

this._log = [];
this._response = {}; // { result, error, options }
this._analytics = {

startTime: now
Expand All @@ -68,12 +70,6 @@ exports = module.exports = Request = function (server, req, res) {
if (this.query[this.server.settings.debug.queryKey]) {

this._debug = this.query[this.server.settings.debug.queryKey];

delete this.url.search;
delete this.query[this.server.settings.debug.queryKey];

this._raw.req.url = Url.format(this.url);
this.setUrl(this._raw.req.url);
}
}

Expand All @@ -83,10 +79,10 @@ exports = module.exports = Request = function (server, req, res) {

method: this.method,
url: this.url.href,
agent: this._raw.req.headers['user-agent']
agent: this.raw.req.headers['user-agent']
};

this.log('http', 'received', '', about, now)
this.log('http', 'received', about, now)

return this;
};
Expand All @@ -108,7 +104,7 @@ Request.prototype.setMethod = function (method) {
};


Request.prototype.log = function (category, event, message, data, timestamp) {
Request.prototype.log = function (category, event, data, timestamp) {

// Prepare log item

Expand All @@ -119,11 +115,6 @@ Request.prototype.log = function (category, event, message, data, timestamp) {
category: category,
event: event
};

if (message) {

item.message = message;
}

if (data) {

Expand All @@ -143,3 +134,23 @@ Request.prototype.log = function (category, event, message, data, timestamp) {
};


exports.processDebug = function (request, config, next) {

// Extract session debugging information

if (request.server.settings.debug) {

if (request.query[request.server.settings.debug.queryKey]) {

delete request.url.search;
delete request.query[request.server.settings.debug.queryKey];

request.raw.req.url = Url.format(request.url);
request.setUrl(request.raw.req.url);
}
}

next();
};


Loading

0 comments on commit cf406d2

Please sign in to comment.