protein
Advanced tools
Comparing version 0.3.6 to 0.4.0
83
index.js
@@ -1,9 +0,5 @@ | ||
var http = require('http'); | ||
var clone = function(from, to) { | ||
if (!from) return; | ||
var PROTOS = {request: http.IncomingMessage.prototype, response: http.ServerResponse.prototype}; | ||
var NAMES = Object.keys(PROTOS); | ||
var extend = function(to, from) { | ||
if (!from) return to; | ||
to = to || {}; | ||
Object.keys(from).forEach(function(key) { | ||
@@ -18,13 +14,22 @@ var getter = from.__lookupGetter__(key); | ||
}); | ||
return to; | ||
}; | ||
var shorthand = function(proto, name, fn) { | ||
name = name.split('.'); | ||
var injector = function() { | ||
var hash = []; | ||
var protos = []; | ||
var inject = function(obj) { | ||
var i = hash.indexOf(obj.__proto__); | ||
if (name.length !== 2) return proto; | ||
if (!PROTOS[name[0]]) return proto; | ||
if (i === -1) { | ||
i = protos.indexOf(obj.__proto__); | ||
} | ||
if (i === -1) { | ||
protos[protos.push(clone(inject.proto))-1].__proto__ = obj.__proto__; | ||
i = hash.push(obj.__proto__)-1; | ||
} | ||
obj.__proto__ = protos[i]; | ||
}; | ||
fn(proto[name[0]], name[1]); | ||
return proto; | ||
inject.proto = {}; | ||
return inject; | ||
}; | ||
@@ -41,6 +46,13 @@ var onerror = function(err, req, res) { | ||
}; | ||
var protein = function(parent) { | ||
parent = parent || {}; | ||
var protein = function() { | ||
var stack = []; | ||
var onrequest = injector(); | ||
var onresponse = injector(); | ||
var stack = []; | ||
var shorthand = function(define) { | ||
return function(name, fn) { | ||
define(reduce[name.split('.')[0]], name.split('.').pop(), fn); | ||
return reduce; | ||
}; | ||
}; | ||
var reduce = function(req, res, callback) { | ||
@@ -75,7 +87,7 @@ var i = 0; | ||
req.response = res; | ||
req.__proto__ = reduce.request; | ||
onrequest(req); | ||
// set response prototype | ||
res.request = req; | ||
res.__proto__ = reduce.response; | ||
onresponse(res); | ||
@@ -86,6 +98,14 @@ // bootstrap the loop | ||
NAMES.forEach(function(name) { | ||
reduce[name] = {}; | ||
reduce[name].__proto__ = parent[name] || PROTOS[name]; | ||
reduce.request = onrequest.proto; | ||
reduce.response = onresponse.proto; | ||
reduce.getter = shorthand(function(proto, name, fn) { | ||
proto.__defineGetter__(name, fn); | ||
}); | ||
reduce.setter = shorthand(function(proto, name, fn) { | ||
proto.__defineSetter__(name, fn); | ||
}); | ||
reduce.fn = shorthand(function(proto, name, fn) { | ||
proto[name] = fn; | ||
}); | ||
@@ -109,21 +129,6 @@ reduce.using = function(fn) { | ||
} | ||
extend(reduce.request, fn.request); | ||
extend(reduce.response, fn.response); | ||
clone(fn.request, reduce.request); | ||
clone(fn.response, reduce.response); | ||
return reduce; | ||
}; | ||
reduce.fn = function(name, fn) { | ||
return shorthand(reduce, name, function(proto, method) { | ||
proto[method] = fn; | ||
}); | ||
}; | ||
reduce.getter = function(name, fn) { | ||
return shorthand(reduce, name, function(proto, getter) { | ||
proto.__defineGetter__(getter, fn); | ||
}); | ||
}; | ||
reduce.setter = function(name, fn) { | ||
return shorthand(reduce, name, function(proto, setter) { | ||
proto.__defineSetter__(setter, fn); | ||
}); | ||
}; | ||
return reduce; | ||
@@ -130,0 +135,0 @@ }; |
{ | ||
"name":"protein", | ||
"version":"0.3.6", | ||
"version":"0.4.0", | ||
"repository": "git://github.com/mafintosh/protein", | ||
@@ -5,0 +5,0 @@ "description":"Protein is connect compatible middleware with support for prototype methods, getters, and setters", |
@@ -160,10 +160,10 @@ # Protein | ||
(The MIT License) | ||
**This software is licensed under "MIT"** | ||
Copyright (c) 2012 Mathias Buus Madsen <mathiasbuus@gmail.com> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
> Copyright (c) 2012 Mathias Buus Madsen <mathiasbuus@gmail.com> | ||
> | ||
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
> | ||
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
> | ||
> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
15319
16
363
2