Permalink
Browse files

[minor] Added support for `versions.js` files so you can have a comme…

…nted config. Fixes #6

[minor] Added support for custom connect plugins. (needs tests)
[minor] Started on making some middleware layers optional.

The biggest reason for making these middleware layers optional is that they only add
pointless performance overhead and sometimes even security issues as is in the case
of the REST router. If you don't supply it with a configuration everybody can just
flush your server on demand and see your configuration using the /sync route.
  • Loading branch information...
1 parent 9f6903f commit ac7d4ccb0ab48613d0dd747b89f2a6f1c6f01b11 @3rd-Eden committed Mar 1, 2013
Showing with 54 additions and 13 deletions.
  1. +2 −0 .gitignore
  2. +34 −7 index.js
  3. +1 −1 test/client.test.js
  4. +6 −0 test/fixtures/extra.js
  5. +5 −0 test/server.test.js
  6. +6 −5 versions.json
View
@@ -1 +1,3 @@
node_modules
+*.log
+*.rdb
View
@@ -39,6 +39,7 @@ function Versions(options) {
this.read('../../node_modules/package.json'); // For version number
this.read('./versions.json'); // For our defaults
this.read('../../node_modules/versions.json'); // For their defaults
+ this.read('../../node_modules/versions.js'); // For their defaults
} else {
Object.keys(options.cloned).forEach(function merge(key) {
// Merge in the cloned configuration, silently
@@ -199,16 +200,42 @@ Versions.prototype.listen = function listen(port, callback) {
// Initialize the server configuration.
this.initialize('server');
- // Configure the middleware
+ // Configure the middleware.
this.layer('responseTime');
this.layer('initialize');
this.layer('versioning');
this.layer('conditional');
this.layer('compress');
this.layer('memorize');
- this.layer('static', this.get('static'), { maxAge: this.get('max age') });
- this.layer('rest');
- this.layer('pull');
+
+ // Allow the loading of third party components.
+ if (this.get('plugins')) {
+ this.get('plugins').forEach(function add(plugin) {
+ if ('string' === typeof plugin) {
+ this.layer(plugin);
+ } else {
+ this.layer(plugin.name, plugin.config);
+ }
+ }, this);
+ }
+
+ // Only add static server support if it a `directory` was specified in the
+ // configuration.
+ if (this.get('directory')) {
+ this.layer('static', this.get('static'), { maxAge: this.get('max age') });
+ }
+
+ // Routing should only be enabled with an `auth` configuration.
+ //if (this.get('auth')) {
+ this.layer('rest');
+ //}
+
+ // Only add support for origin pull if we actually have servers configured.
+ //if (this.get('origin servers').length) {
+ this.layer('pull');
+ //}
+
+ // End of the configurable middleware.
this.layer('done');
// Start listening for changes.
@@ -227,7 +254,7 @@ Versions.prototype.listen = function listen(port, callback) {
* @api private
*/
Versions.prototype.initialize = function initialize(type) {
- // Setup our extra configuration values
+ // Setup our extra configuration values.
if (!this.get('static')) {
this.set('static', path.resolve(this.get('root'), this.get('directory')));
}
@@ -240,7 +267,7 @@ Versions.prototype.initialize = function initialize(type) {
this.cache = new Expirable(this.get('expire internal cache'));
this.metrics = require('./measure').collect(this);
- // Setup syncing if provided
+ // Setup syncing if provided.
this.sync();
return this;
};
@@ -257,7 +284,7 @@ Versions.prototype.write = function write(req, res, data) {
var age = this.get('max age')
, body = data.buffer;
- // Check if we have a GZIP version of the content
+ // Check if we have a GZIP version of the content.
if (this.allows('gzip', req) && 'gzip' in data) {
res.setHeader('Content-Encoding', 'gzip');
body = data.gzip;
View
@@ -116,7 +116,7 @@ describe('versions.connect()', function () {
});
});
- describe.only('#prefix', function () {
+ describe('#prefix', function () {
var server , port = portnumbers, api;
before(function () {
View
@@ -0,0 +1,6 @@
+'use strict';
+
+// Should also accept plain javascript files that export a configuration
+module.exports = {
+ "plain": "javascript"
+};
View
@@ -89,6 +89,11 @@ describe('versions()', function () {
versions.logger.removeAllListeners('error');
expect(errors).to.equal(2);
});
+
+ it('reads plain javascript files', function () {
+ versions.read(path.resolve(__dirname, './fixtures/extra.js'));
+ expect(versions.get('plain')).to.equal('javascript');
+ });
});
describe("#clone", function () {
View
@@ -1,14 +1,15 @@
{
+ "aliases": [],
"auth": "",
"blacklisted extensions": [],
"cors": "*",
- "directory": "./public",
+ "directory": "",
"expire internal cache": "1 hour",
- "max age": "30 days",
"log level": "log",
- "version": "0.0.0",
- "sync": false,
+ "max age": "30 days",
"origin servers": [],
+ "plugins": [],
"port": 8080,
- "aliases": []
+ "sync": false,
+ "version": "0.0.0"
}

0 comments on commit ac7d4cc

Please sign in to comment.