Commits

Magnus Hoff committed eb58383

Split library into files for better readability and maintenance

  • Participants
  • Parent commits cb053cf

Comments (0)

Files changed (10)

+var verifyNew = require('./util').verifyNew;
+var Lookup = require('./lookup').Lookup;
+
+
+function DirectLookup(resourceFactory) {
+    verifyNew(this, this.constructor.name);
+    Lookup.call(this);
+
+    this.lookup = function(reqpath, callback) {
+        if (reqpath === '') {
+            resourceFactory(callback);
+        } else {
+            callback(null);
+        }
+    };
+}
+
+DirectLookup.prototype = new Lookup();
+DirectLookup.constructor = DirectLookup;
+
+
+exports.DirectLookup = DirectLookup;
+var path = require('path');
+var fs = require('fs');
+var verifyNew = require('./util').verifyNew;
+var Lookup = require('./lookup').Lookup;
+var FileResource = require('./fileresource').FileResource;
+
+
+function FileLookup(root) {
+    verifyNew(this, this.constructor.name);
+    Lookup.call(this);
+
+    this.lookup = function(reqpath, callback) {
+        var fullpath = path.join(root, reqpath);
+        fs.exists(fullpath, function(exists) {
+            if (exists) {
+                callback(new FileResource(fullpath));
+            } else {
+                callback(null);
+            }
+        });
+    };
+}
+
+FileLookup.prototype = new Lookup();
+FileLookup.constructor = FileLookup;
+
+
+exports.FileLookup = FileLookup;
+var path = require('path');
+var fs = require('fs');
+var verifyNew = require('./util').verifyNew;
+var Resource = require('./resource').Resource;
+
+
+function FileResource(fullpath) {
+    verifyNew(this, this.constructor.name);
+    Resource.call(this);
+
+    var contentTypes = {
+        '.html': 'text/html',
+        '.js': 'text/javascript',
+        '.css': 'text/css'
+    };
+
+    this.http_GET = function(req, res) {
+        var ext = path.extname(fullpath);
+        var contentType = contentTypes[ext] || 'text/plain';
+
+        res.writeHead(200, {'Content-Type': contentType});
+        var f = fs.createReadStream(fullpath);
+        f.pipe(res);
+    };
+}
+
+FileResource.prototype = new Resource();
+FileResource.constructor = FileResource;
+
+
+exports.FileResource = FileResource;
+// Preserve order here to resolve circular dependency with MapLookup:
+exports.Lookup = Lookup;
+
+var verifyNew = require('./util').verifyNew;
+var Resource = require('./resource').Resource;
+var MapLookup = require('./maplookup').MapLookup;
+
+
+function Lookup() {
+    verifyNew(this, this.constructor.name);
+}
+
+Lookup.prototype.doLookup = function (lookupish, path, callback) {
+    if (lookupish === undefined) {
+        callback(null);
+    } else if (lookupish instanceof Lookup) {
+        lookupish.lookup(path, callback);
+    } else if (lookupish instanceof Resource) {
+        var resource = lookupish;
+        callback(resource);
+    } else if (typeof lookupish === 'function') {
+        var resourceFactory = lookupish;
+        resourceFactory(callback);
+    } else if (typeof lookupish === 'object') {
+        var lookup = new MapLookup(lookupish);
+        lookup.lookup(path, callback);
+    } else {
+        throw "Not a lookup or lookup-like object";
+    }
+}
+var util = require('./util');
+var Lookup = require('./lookup').Lookup;
+
+
+function MapLookup(map) {
+    util.verifyNew(this, this.constructor.name);
+    Lookup.call(this);
+
+    this.lookup = function(reqpath, callback) {
+        var splitpath = util.splitOneLevel(reqpath);
+        var dirname = splitpath[0], rest = splitpath[1];
+        var lookup = map[dirname];
+        this.doLookup(lookup, rest, callback);
+    };
+}
+
+MapLookup.prototype = new Lookup();
+MapLookup.constructor = MapLookup;
+
+
+exports.MapLookup = MapLookup;

onelevellookup.js

+var util = require('./util');
+var Lookup = require('./lookup').Lookup;
+
+
+function OneLevelLookup(lookupFactory) {
+    util.verifyNew(this, this.constructor.name);
+    Lookup.call(this);
+
+    this.lookup = function(reqpath, callback) {
+        var split = util.splitOneLevel(reqpath);
+        var nestedLookup = lookupFactory(split[0]);
+        this.doLookup(nestedLookup, split[1], callback);
+    };
+}
+
+OneLevelLookup.prototype = new Lookup();
+OneLevelLookup.constructor = OneLevelLookup;
+
+
+exports.OneLevelLookup = OneLevelLookup;

permanentredirectresource.js

+var url = require('url');
+var verifyNew = require('./util').verifyNew;
+var Resource = require('./resource').Resource;
+
+
+function PermanentRedirectResource(relativeTargetUrl) {
+    verifyNew(this, this.constructor.name);
+    Resource.call(this);
+
+    function redirect(req, res) {
+        var parsedUrl = url.parse(req.url);
+        var targetPath = url.resolve(parsedUrl.path, relativeTargetUrl);
+        parsedUrl.pathname = targetPath;
+        var targetUrl = url.format(parsedUrl);
+
+        res.writeHead(301, {
+            "Location": targetUrl,
+            "Content-Type": "text/plain"
+        });
+
+        if (req.method !== 'HEAD') {
+            res.write("Go see " + targetUrl);
+        }
+        res.end();
+    }
+
+    this.http_GET = redirect;
+    this.http_HEAD = redirect;
+    this.http_POST = redirect;
+    this.http_PUT = redirect;
+    this.http_DELETE = redirect;
+}
+
+PermanentRedirectResource.prototype = new Resource();
+PermanentRedirectResource.constructor = PermanentRedirectResource;
+
+
+exports.PermanentRedirectResource = PermanentRedirectResource;
 var http = require('http');
 var url = require('url');
 var path = require('path');
-var assert = require('assert');
 var fs = require('fs');
+var Lookup = require('./lookup').Lookup;
 
 
 var module = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf-8'));
 
 
-function verifyNew(that, fname) {
-    if (this === that) throw "Missing new on invocation of constructor " + fname;
-}
-
-
-function splitOneLevel(full) {
-    if (full.length !== 0) assert.equal(full.charAt(0), '/');
-    var i = full.indexOf('/', 1);
-    if (i === -1) {
-        if (full === '/') {
-            return ['/', ''];
-        } else {
-            return [full.slice(1), ''];
-        }
-    } else {
-        return [full.slice(1, i), full.slice(i)];
-    }
-}
-
-
-function Lookup() {
-    verifyNew(this, this.constructor.name);
-}
-
-Lookup.prototype.doLookup = function (lookupish, path, callback) {
-    if (lookupish === undefined) {
-        callback(null);
-    } else if (lookupish instanceof Lookup) {
-        lookupish.lookup(path, callback);
-    } else if (lookupish instanceof Resource) {
-        var resource = lookupish;
-        callback(resource);
-    } else if (typeof lookupish === 'function') {
-        var resourceFactory = lookupish;
-        resourceFactory(callback);
-    } else if (typeof lookupish === 'object') {
-        var lookup = new MapLookup(lookupish);
-        lookup.lookup(path, callback);
-    } else {
-        throw "Not a lookup or lookup-like object";
-    }
-}
-
-
-function Resource() {
-    verifyNew(this, this.constructor.name);
-}
-
-
-function MapLookup(map) {
-    verifyNew(this, this.constructor.name);
-    Lookup.call(this);
-
-    this.lookup = function(reqpath, callback) {
-        var splitpath = splitOneLevel(reqpath);
-        var dirname = splitpath[0], rest = splitpath[1];
-        var lookup = map[dirname];
-        this.doLookup(lookup, rest, callback);
-    };
-}
-
-MapLookup.prototype = new Lookup();
-MapLookup.constructor = MapLookup;
-
-
-function FileResource(fullpath) {
-    verifyNew(this, this.constructor.name);
-    Resource.call(this);
-
-    var contentTypes = {
-        '.html': 'text/html',
-        '.js': 'text/javascript',
-        '.css': 'text/css'
-    };
-
-    this.http_GET = function(req, res) {
-        var ext = path.extname(fullpath);
-        var contentType = contentTypes[ext] || 'text/plain';
-
-        res.writeHead(200, {'Content-Type': contentType});
-        var f = fs.createReadStream(fullpath);
-        f.pipe(res);
-    };
-}
-
-FileResource.prototype = new Resource();
-FileResource.constructor = FileResource;
-
-
-function FileLookup(root) {
-    verifyNew(this, this.constructor.name);
-    Lookup.call(this);
-
-    this.lookup = function(reqpath, callback) {
-        var fullpath = path.join(root, reqpath);
-        fs.exists(fullpath, function(exists) {
-            if (exists) {
-                callback(new FileResource(fullpath));
-            } else {
-                callback(null);
-            }
-        });
-    };
-}
-
-FileLookup.prototype = new Lookup();
-FileLookup.constructor = FileLookup;
-
-
-function OneLevelLookup(lookupFactory) {
-    verifyNew(this, this.constructor.name);
-    Lookup.call(this);
-
-    this.lookup = function(reqpath, callback) {
-        var split = splitOneLevel(reqpath);
-        var nestedLookup = lookupFactory(split[0]);
-        this.doLookup(nestedLookup, split[1], callback);
-    };
-}
-
-OneLevelLookup.prototype = new Lookup();
-OneLevelLookup.constructor = OneLevelLookup;
-
-
-function DirectLookup(resourceFactory) {
-    verifyNew(this, this.constructor.name);
-    Lookup.call(this);
-
-    this.lookup = function(reqpath, callback) {
-        if (reqpath === '') {
-            resourceFactory(callback);
-        } else {
-            callback(null);
-        }
-    };
-}
-
-DirectLookup.prototype = new Lookup();
-DirectLookup.constructor = DirectLookup;
-
-
-function PermanentRedirectResource(relativeTargetUrl) {
-    verifyNew(this, this.constructor.name);
-    Resource.call(this);
-
-    function redirect(req, res) {
-        var parsedUrl = url.parse(req.url);
-        var targetPath = url.resolve(parsedUrl.path, relativeTargetUrl);
-        parsedUrl.pathname = targetPath;
-        var targetUrl = url.format(parsedUrl);
-
-        res.writeHead(301, {
-            "Location": targetUrl,
-            "Content-Type": "text/plain"
-        });
-
-        if (req.method !== 'HEAD') {
-            res.write("Go see " + targetUrl);
-        }
-        res.end();
-    }
-
-    this.http_GET = redirect;
-    this.http_HEAD = redirect;
-    this.http_POST = redirect;
-    this.http_PUT = redirect;
-    this.http_DELETE = redirect;
-}
-
-PermanentRedirectResource.prototype = new Resource();
-PermanentRedirectResource.constructor = PermanentRedirectResource;
+// TODO: Should implement OPTIONS in this library
+// TODO: Should have dynamically configurable allowedMethods per server instance
+var allowedMethods = ["GET", "HEAD", "PUT", "POST", "DELETE"];
 
 
 function methodNotAllowed(res, resource) {
     var allow = [];
     for (var member in resource) {
         if (member.substr(0, 5) === 'http_') {
-            allow.push(member.substr(5));
+            var method = member.substr(5);
+            if (allowedMethods.indexOf(method) !== -1) {
+                allow.push(method);
+            }
         }
     }
 
     res.end("Method Not Allowed");
 }
 
+function dispatchToResource(req, res, resource) {
+    var functionObject = resource[functionName];
 
-// TODO: Should implement OPTIONS in this library
-// TODO: Should have dynamically configurable allowedMethods per server instance
-var allowedMethods = ["GET", "HEAD", "PUT", "POST", "DELETE"];
-function dispatchToResource(req, res, resource) {
-    if (allowedMethods.indexOf(req.method) === -1) methodNotAllowed(res, resource);
-
-    var functionName = 'http_' + req.method;
-    var functionObject = resource[functionName];
+    if (allowedMethods.indexOf(req.method) !== -1) {
+        var functionName = 'http_' + req.method;
+        functionObject = resource[functionName];
+    } else {
+        functionObject = undefined;
+    }
 
     if (typeof functionObject === 'function') functionObject.call(resource, req, res);
     else methodNotAllowed(res, resource);
 }
 
 
-exports.splitOneLevel = splitOneLevel;
+exports.splitOneLevel = require('./util').splitOneLevel;
 
 exports.Lookup = Lookup;
-exports.MapLookup = MapLookup;
-exports.FileLookup = FileLookup;
-exports.DirectLookup = DirectLookup;
-exports.OneLevelLookup = OneLevelLookup;
+exports.MapLookup = require('./maplookup').MapLookup;
+exports.FileLookup = require('./filelookup').FileLookup;
+exports.DirectLookup = require('./directlookup').DirectLookup;
+exports.OneLevelLookup = require('./onelevellookup').OneLevelLookup;
 
-exports.Resource = Resource;
-exports.FileResource = FileResource;
-exports.PermanentRedirectResource = PermanentRedirectResource;
+exports.Resource = require('./resource').Resource;
+exports.FileResource = require('./fileresource').FileResource;
+exports.PermanentRedirectResource = require('./permanentredirectresource').PermanentRedirectResource;
 
 exports.handleRequest = handleRequest;
 exports.handleRequestFunction = handleRequestFunction;
+var verifyNew = require('./util').verifyNew;
+
+
+function Resource() {
+    verifyNew(this, this.constructor.name);
+}
+
+
+exports.Resource = Resource;
+var assert = require('assert');
+
+
+function verifyNew(that, fname) {
+    if (this === that) throw "Missing new on invocation of constructor " + fname;
+}
+
+function splitOneLevel(full) {
+    if (full.length !== 0) assert.equal(full.charAt(0), '/');
+    var i = full.indexOf('/', 1);
+    if (i === -1) {
+        if (full === '/') {
+            return ['/', ''];
+        } else {
+            return [full.slice(1), ''];
+        }
+    } else {
+        return [full.slice(1, i), full.slice(i)];
+    }
+}
+
+
+exports.verifyNew = verifyNew;
+exports.splitOneLevel = splitOneLevel;