Permalink
Browse files

Remove obsoleted Class, use inherit/decorate for Router/Response/Requ…

…est. Cleanup require in various places.
  • Loading branch information...
1 parent e544bba commit e9ffb6f4a0bfe72c8349ac5bfddaae4749bd3184 @Benvie Benvie committed Aug 5, 2012
Showing with 137 additions and 152 deletions.
  1. +12 −11 lib/index.js
  2. +61 −0 lib/router/Router.js
  3. +0 −11 lib/router/http.json
  4. +6 −57 lib/router/index.js
  5. +22 −19 lib/router/request.js
  6. +20 −18 lib/router/response.js
  7. +6 −5 lib/settings.js
  8. +1 −22 lib/utils.js
  9. +9 −9 lib/window.js
View
@@ -1,13 +1,15 @@
-var bindings = require('./bindings'),
- settings = require('./settings'),
+var Router = require('./router').Router,
+ Window = require('./window'),
+ App = require('./bindings').App,
+ NativeWindow = require('./bindings').NativeWindow,
+ WindowSettings = require('./settings').WindowSettings,
+ AppSettings = require('./settings').AppSettings;
+
+var init = require('./bindings').init,
decorate = require('./utils').decorate,
isObject = require('./utils').isObject,
- staticRouter = require('./router/static-router');
+ staticRouter = require('./router').staticRouter;
-var Router = require('./router'),
- Window = require('./window'),
- App = require('./bindings').App,
- NativeWindow = require('./bindings').NativeWindow;
var concat = Array.prototype.concat,
_createWindow = App.prototype.createWindow;
@@ -38,8 +40,8 @@ decorate(App.prototype, [
options = { url: options };
}
}
- if (!(options instanceof settings.WindowSettings)) {
- options = new settings.WindowSettings(options);
+ if (!(options instanceof WindowSettings)) {
+ options = new WindowSettings(options);
}
var nativeWindow = _createWindow.call(this, options),
@@ -74,7 +76,7 @@ decorate(App.prototype, [
]);
-var app = module.exports = bindings.init(new settings.AppSettings);
+var app = init(new AppSettings);
decorate(app, {
screenWidth: App.screenWidth,
@@ -91,4 +93,3 @@ app.on('exit',function(){
});
module.exports = app;
-
View
@@ -0,0 +1,61 @@
+var Response = require('./Response'),
+ Request = require('./Request'),
+ decorate = require('../utils').decorate;
+
+var methods = ['get', 'post'];
+
+
+module.exports = Router;
+
+function Router(emitter){
+ var self = this;
+
+ this.handlers = { all: [] };
+ for (var i=0; i < methods.length; i++) {
+ this.handlers[methods[i]] = [];
+ }
+
+ emitter.on('request',function(request, callback){
+ self.handle(new Request(request), new Response(callback));
+ });
+}
+
+decorate(Router.prototype, [
+ function handle(request, response){
+ var listeners = this.handlers[request.method] || [],
+ index = -1;
+ void function next(){
+ if (++index === listeners.length) {
+ response.send(500, 'Internal Server Error');
+ } else {
+ listeners[index](request, response, next);
+ }
+ }();
+ },
+ function use(middleware){
+ for (var i=0; i < methods.length; i++) {
+ this.handlers[methods[i]].push(middleware);
+ }
+ return this;
+ },
+ function all(route, callback) {
+ for (var i=0; i < methods.length; i++) {
+ this[methods[i]](route, callback);
+ }
+ return this;
+ }
+]);
+
+methods.forEach(function(method){
+ Router.prototype[method] = function(route, callback) {
+ this.handlers[method].push(function(request, response, next){
+ if (request.method.toLowerCase() === method && request.pathname === route) {
+ callback(request, response, next);
+ } else {
+ next();
+ }
+ });
+ return this;
+ }
+});
+
View
@@ -1,11 +0,0 @@
-{
- "statusCodes": {
- "200": "OK",
- "304": "Not Modified",
- "400": "Bad Request",
- "401": "Unauthorized",
- "403": "Forbidden",
- "404": "Not Found",
- "500": "Internal Server Error"
- }
-}
View
@@ -1,57 +1,6 @@
-var Response = require('./response'),
- Request = require('./request'),
- Class = require('../utils').Class;
-
-var methods = ['get', 'post'];
-
-var Router = module.exports = new Class({
- constructor: function Router(emitter){
- var self = this;
-
- this.handlers = { all: [] };
- for (var i=0; i < methods.length; i++) {
- this.handlers[methods[i]] = [];
- }
-
- emitter.on('request',function(request, callback){
- self.handle(new Request(request), new Response(callback));
- });
- },
- handle: function handle(request, response){
- var listeners = this.handlers[request.method] || [],
- index = -1;
- void function next(){
- if (++index === listeners.length) {
- response.send(500, 'Internal Server Error');
- } else {
- listeners[index](request, response, next);
- }
- }();
- },
- use: function use(middleware){
- for (var i=0; i < methods.length; i++) {
- this.handlers[methods[i]].push(middleware);
- }
- return this;
- },
- all: function(route, callback) {
- for (var i=0; i < methods.length; i++) {
- this[methods[i]](route, callback);
- }
- return this;
- }
-});
-
-methods.forEach(function(method){
- Router.prototype[method] = function(route, callback) {
- this.handlers[method].push(function(request, response, next){
- if (request.method.toLowerCase() === method && request.pathname === route) {
- callback(request, response, next);
- } else {
- next();
- }
- });
- return this;
- }
-});
-
+module.exports = {
+ Response: require('./Response'),
+ Request: require('./Request'),
+ Router: require('./Router'),
+ staticRouter: require('./static-router')
+};
View
@@ -1,26 +1,29 @@
-var querystring = require('querystring'),
- url = require('url'),
- Class = require('../utils').Class;
+var ClientRequest = require('http').ClientRequest,
+ querystring = require('querystring'),
+ url = require('url'),
+ inherit = require('../utils').inherit;
-module.exports = new Class({
- __proto__: require('http').ClientRequest.prototype,
- constructor: function Request(request){
- var parsed = url.parse(request.url, true);
- this.url = request.url.replace(/^http(s)?:\/\/([^\/]*)\/$/,'');
- this.headers = request.headers;
- this.method = request.method.toLowerCase();
- this.pathname = parsed.pathname;
- this.hash = parsed.hash;
- this.params = parsed.query;
- this.data = this.body = querystring.parse(request.post);
- },
- param: function param(index){
+module.exports = Request;
+
+function Request(request){
+ var parsed = url.parse(request.url, true);
+ this.url = request.url.replace(/^http(s)?:\/\/([^\/]*)\/$/,'');
+ this.headers = request.headers;
+ this.method = request.method.toLowerCase();
+ this.pathname = parsed.pathname;
+ this.hash = parsed.hash;
+ this.params = parsed.query;
+ this.data = this.body = querystring.parse(request.post);
+}
+
+inherit(Request, ClientRequest, [
+ function param(index){
return this.params[index];
},
- post: function post(index){
+ function post(index){
return this.data[index];
},
- get: function get(key){
+ function get(key){
return this.headers[key];
}
-});
+]);
@@ -1,19 +1,24 @@
-var fs = require('fs'),
- path = require('path'),
- mime = require('mime'),
- Class = require('../utils').Class;
+var ServerResponse = require('http').ServerResponse,
+ STATUS_CODES = require('http').STATUS_CODES,
+ fs = require('fs'),
+ path = require('path'),
+ mime = require('mime'),
+ inherit = require('../utils').inherit;
-
-var statusCodes = require('./http.json').statusCodes;
var EMPTY = new Buffer(0);
-module.exports = new Class({
- __proto__: require('http').ServerResponse.prototype,
- constructor: function Response(callback){
- this.callback = callback;
+module.exports = Response;
+
+function Response(callback){
+ this.callback = callback;
+}
+
+inherit(Response, ServerResponse, [
+ function end(data){
+ return this.send(data);
},
- send: function send(status, mimetype, data){
+ function send(status, mimetype, data){
if (arguments.length === 1) {
if (typeof status === 'number') {
this.status = status;
@@ -32,14 +37,11 @@ module.exports = new Class({
this.dispatch();
},
- end: function end(data){
- return this.send(data);
- },
- dispatch: function dispatch(){
+ function dispatch(){
this.status || (this.status = 200);
this.mimetype || (this.mimetype = 'text/html');
this.data || (this.data = EMPTY);
- this.responseCode = statusCodes[this.status];
+ this.responseCode = STATUS_CODES[this.status];
if (!Buffer.isBuffer(this.data)) {
if (this.data !== null && typeof this.data === 'object') {
@@ -54,7 +56,7 @@ module.exports = new Class({
this.callback(this.status, this.responseCode, this.mimetype, this.data);
},
- sendFile: function sendFile(status, filepath){
+ function sendFile(status, filepath){
var self = this;
if (arguments.length === 2) {
this.path = path.resolve(filepath);
@@ -79,4 +81,4 @@ module.exports = new Class({
}
});
}
-});
+]);
View
@@ -1,16 +1,17 @@
-var fs = require('fs'),
- path = require('path'),
- decorate = require('./utils').decorate,
- constants = require('./utils').constants,
+var fs = require('fs'),
+ path = require('path'),
+ decorate = require('./utils').decorate,
+ constants = require('./utils').constants,
platformPath = require('./bindings').platformPath;
+
+
module.exports = {
AppSettings: AppSettings,
WindowSettings: WindowSettings,
IconGroup: IconGroup
};
-
function AppSettings(o){
for (var k in Object(o)) {
this[k] = o[k];
View
@@ -1,8 +1,7 @@
-var fs = require('fs'),
+var fs = require('fs'),
path = require('path');
module.exports = {
- Class: Class,
decorate: decorate,
isObject: isObject,
inherit: inherit,
@@ -40,26 +39,6 @@ var hiddenDesc = { enumerable: false },
readonlyVal = Definable.readonly = new Definable({ enumerable: true, configurable: true, writable: false });
-function Class(proto){
- var Ctor = proto.constructor || (proto.constructor = Object);
- var Super = proto.super || Object;
- this.__proto__ = Super.prototype;
- Ctor.__proto__ = Super;
- Ctor.prototype = this;
- getOwnNames(proto).forEach(function(key){
- if (key !== 'super') {
- var desc = getOwnDescriptor(proto, key);
- if (key[0] === '_' || key === 'constructor') {
- desc.enumerable = false;
- key[0] === '_' && (key = key.slice(1));
- }
- defineProperty(this, key, desc);
- }
- }, this);
- return Ctor;
-}
-
-
function decorate(o){
var a, b, c, d,
hidden = arguments[1] === true,
View
@@ -1,16 +1,16 @@
-var NativeWindow = require('./bindings').NativeWindow,
- App = require('./bindings').App,
- Ephemeral = require('./handlers').Ephemeral,
- ReferenceType = require('./handlers').ReferenceType,
- EventEmitter = process.EventEmitter,
+var NativeWindow = require('./bindings').NativeWindow,
+ App = require('./bindings').App,
+ Ephemeral = require('./handlers').Ephemeral,
+ ReferenceType = require('./handlers').ReferenceType,
+ EventEmitter = require('events').EventEmitter;
- decorate = require('./utils').decorate,
- inherit = require('./utils').inherit,
- bridge = require('./bridge');
+var decorate = require('./utils').decorate,
+ inherit = require('./utils').inherit,
+ bridge = require('./bridge');
-module.exports = Window;
+module.exports = Window;
function Window(nativeWindow){
if (!(this instanceof Window))

0 comments on commit e9ffb6f

Please sign in to comment.