Skip to content

Commit

Permalink
Merge pull request hapijs#634 from mikeal/domains
Browse files Browse the repository at this point in the history
Domains
  • Loading branch information
Eran Hammer committed Mar 4, 2013
2 parents 9d86d2a + 2b9ecb4 commit 61b388a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ var internals = {


// Export public modules
module.exports = function (host, port, options) {
return new module.exports.Server(host, port, options);
};

internals.export = function () {

for (var key in internals.modules) {
if (internals.modules.hasOwnProperty(key)) {
exports[key] = exports[key.charAt(0).toUpperCase() + key.slice(1)] = internals.modules[key];
module.exports[key] = module.exports[key.charAt(0).toUpperCase() + key.slice(1)] = internals.modules[key];
}
}
};
Expand Down
5 changes: 3 additions & 2 deletions lib/response/obj.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Load modules

var Cacheable = require('./cacheable');
var SafeStringify = require('json-stringify-safe')
var Utils = require('../utils');


// Declare internals

var internals = {};
Expand All @@ -17,6 +17,7 @@ exports = module.exports = internals.Obj = function (object, type, encoding) {
this.variety = 'obj';
this.varieties.obj = true;

// Convert immediately to snapshot content
this.raw = object; // Can change if reference is modified
this.update(type, encoding); // Convert immediately to snapshot content

Expand All @@ -28,7 +29,7 @@ Utils.inherits(internals.Obj, Cacheable);

internals.Obj.prototype.update = function (type, encoding) {

this._payload = [JSON.stringify(this.raw)];
this._payload = [SafeStringify(this.raw)];
this._headers['Content-Type'] = type || this._headers['Content-Type'] || 'application/json';
this._flags.encoding = encoding || 'utf-8';
};
32 changes: 29 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var Events = require('events');
var Http = require('http');
var Https = require('https');
var Domain = require('domain');
var Shot = require('shot');
var Auth = require('./auth');
var Catbox = require('catbox');
Expand Down Expand Up @@ -42,6 +43,11 @@ module.exports = internals.Server = function (/* host, port, options */) {
};

var args = {};

_arguments = Array.prototype.slice.call(arguments)
while (typeof _arguments[_arguments.length - 1] === 'undefined' && arguments.length !== 0) {
_arguments.pop()
}

for (var a = 0, al = arguments.length; a < al; ++a) {
if (arguments[a] === undefined) {
Expand All @@ -51,7 +57,7 @@ module.exports = internals.Server = function (/* host, port, options */) {
var key = argMap[type];
Utils.assert(key, 'Bad server constructor arguments: no match for arg type ' + type);
Utils.assert(!args[key], 'Bad server constructor arguments: duplicated arg type: ' + type);
args[key] = arguments[a];
args[key] = _arguments[a];
}

// Set basic configuration
Expand Down Expand Up @@ -145,7 +151,7 @@ module.exports = internals.Server = function (/* host, port, options */) {
if (this.settings.auth) {
this.auth = new Auth(this, this.settings.auth);
}

this.__defineGetter__('plugin', this._plugin);

return this;
Expand All @@ -159,6 +165,11 @@ internals.Server.prototype._dispatch = function (options) {
var self = this;

return function (req, res) {

var d = Domain.createDomain();
d.add(req);
d.add(res);
d.enter();

// Create request object

Expand Down Expand Up @@ -306,7 +317,6 @@ internals.Server.prototype._route = function (configs, env) {
this._router.add(configs, env);
};


internals.Server.prototype.state = internals.Server.prototype.addState = function (name, options) {

Utils.assert(name && typeof name === 'string', 'Invalid name');
Expand Down Expand Up @@ -406,6 +416,22 @@ internals.Server.prototype.helper = internals.Server.prototype.addHelper = funct
this.helpers[name] = helper;
};

// Mikeal's smaller api
function RouteAPI (inst) {
this.inst = inst;
};
RouteAPI.prototype.get = function (path, handler) {
return this.inst.addRoute({method: 'GET', path:path, handler:handler});
};
RouteAPI.prototype.put = function (path, handler) {
return this.inst.addRoute({method: 'PUT', path:path, handler:handler});
};
RouteAPI.prototype.post = function (path, handler) {
return this.inst.addRoute({method: 'POST', path:path, handler:handler});
};
RouteAPI.prototype.delete = function (path, handler) {
return this.inst.addRoute({method: 'DELETE', path:path, handler:handler});
};

internals.generateKey = function (args) {

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"formidable": "1.0.x",
"mime": "1.2.x",
"catbox": "0.1.x",
"json-stringify-safe": "~3.0.0",
"cryptiles": "0.1.x",
"iron": "0.2.x",
"lru-cache": "2.2.x",
Expand Down

0 comments on commit 61b388a

Please sign in to comment.