Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Convention shift

  • Loading branch information...
commit f5a4d6917d961c2f89e20001f229dfb32e567a1c 1 parent 307b36a
Paul Serby authored
20 examples/connect/server.js
View
@@ -1,13 +1,13 @@
-var
- version = '0.1',
- connect = require('connect'),
- versionator = require('../../');
+var version = '0.1'
+ , connect = require('connect')
+ , versionator = require('../../')
+ ;
connect(
- versionator.createBasic('v' + version).middleware,
- connect.static(__dirname + '/public', { maxAge: 2592000000 }),
- function(req, res) {
- res.setHeader('Content-Type', 'text/html');
- res.end('<script src="/js/v' + version + '/example.js"></script>');
- }
+ versionator.createBasic('v' + version).middleware,
+ connect.static(__dirname + '/public', { maxAge: 2592000000 }),
+ function(req, res) {
+ res.setHeader('Content-Type', 'text/html');
+ res.end('<script src="/js/v' + version + '/example.js"></script>');
+ }
).listen(3000);
6 examples/express-mapped/app.js
View
@@ -33,18 +33,18 @@ versionator.createMapFromPath(__dirname + '/public', function(error, staticFileM
.use(express.bodyParser())
.use(express.methodOverride())
.use(mappedVersion.middleware)
- .use(stylus.middleware({
+ .use(stylus.middleware({
src: __dirname + '/public/',
compile: stylusCompile }))
.use(app.router)
.use(express.static(__dirname + '/public', { maxAge: 2592000000 }));
});
-
+
app.configure('development', function(){
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
// Routes
14 examples/express/app.js
View
@@ -1,8 +1,8 @@
-var
- express = require('express'),
- stylus = require('stylus'),
- versionator = require('../../'),
- app = module.exports = express.createServer();
+var express = require('express')
+ , stylus = require('stylus')
+ , versionator = require('../../')
+ , app = module.exports = express.createServer()
+ ;
app.version = '0.1';
@@ -32,7 +32,7 @@ app.configure(function(){
.use(express.bodyParser())
.use(express.methodOverride())
.use(basic.middleware)
- .use(stylus.middleware({
+ .use(stylus.middleware({
src: __dirname + '/public/',
compile: stylusCompile }))
.use(app.router)
@@ -40,7 +40,7 @@ app.configure(function(){
});
app.configure('development', function(){
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
// Routes
30 lib/map-path.js
View
@@ -1,14 +1,14 @@
-var
- url = require('url'),
- async = require('async'),
- crypto = require('crypto'),
- fs = require('fs'),
- path = require('path'),
- _ = require('underscore'),
- walker = require('walker');
+var url = require('url')
+ , async = require('async')
+ , crypto = require('crypto')
+ , fs = require('fs')
+ , path = require('path')
+ , _ = require('underscore')
+ , walker = require('walker')
+;
module.exports = function(dirPath, params, callback) {
-
+
var
files = [],
options = {
@@ -44,13 +44,13 @@ module.exports = function(dirPath, params, callback) {
var hashMap = Object.create(null);
async.forEach(files, function(filename, fileCallback) {
hash(filename, function(error, fileHash) {
-
+
var
urlPath = path.normalize('/' + filename.substring(dirPath.length + 1)),
pos = urlPath.lastIndexOf('/');
-
- hashMap[urlPath] =
- urlPath.substring(0, pos) + '/' + fileHash +
+
+ hashMap[urlPath] =
+ urlPath.substring(0, pos) + '/' + fileHash +
urlPath.substring(pos) || url;
fileCallback(error);
@@ -62,7 +62,7 @@ module.exports = function(dirPath, params, callback) {
callback(undefined, hashMap);
});
}
-
+
if (options.fileList) {
for (var i in options.fileList) {
files.push(options.fileList[i]);
@@ -71,7 +71,7 @@ module.exports = function(dirPath, params, callback) {
return;
}
// else
-
+
walker(dirPath)
.on('file', function(filename) {
files.push(filename);
5 lib/middleware/basic.js
View
@@ -1,6 +1,5 @@
-var
- url = require('url'),
- path = require('path');
+var url = require('url')
+ , path = require('path');
module.exports = function(version) {
3  lib/middleware/mapped.js
View
@@ -1,7 +1,6 @@
module.exports = function(mappedPaths) {
- var
- reverseMap = Object.create(null);
+ var reverseMap = Object.create(null);
if (typeof mappedPaths !== 'object') {
throw new Error('A hash map of paths is required');
123 test/basic-versionator.test.js
View
@@ -1,85 +1,84 @@
-var
- appEngine,
- assert = require('assert'),
- request = require('request'),
- versionator = require('../'),
- appEngine = require('connect'),
- http = require('http');
+var appEngine
+ , assert = require('assert')
+ , request = require('request')
+ , versionator = require('../')
+ , appEngine = require('connect')
+ ;
describe('versionator', function() {
- describe('#createBasic', function() {
+ describe('#createBasic', function() {
- it('throws an error if no version is passed', function() {
- assert.throws(function() {
- versionator.createBasic();
- }, /version is required/);
- });
+ it('throws an error if no version is passed', function() {
+ assert.throws(function() {
+ versionator.createBasic();
+ }, /version is required/);
+ });
- it('returns a middleware function', function() {
- versionator.createBasic('0.0.0').middleware.should.be.a('function');
- });
+ it('returns a middleware function', function() {
+ versionator.createBasic('0.0.0').middleware.should.be.a('function');
+ });
- describe('#versionPath', function() {
- it('should inject the version correctly', function() {
- var basic = versionator.createBasic('v0.0.0');
- basic.versionPath('/js/test.js').should.eql('/js/v0.0.0/test.js');
- });
- it('strings without a \'/\' will be left unchanged' , function() {
- var basic = versionator.createBasic('v0.0.0');
- basic.versionPath('Hello this is an odd path').should.eql('Hello this is an odd path');
- });
+ describe('#versionPath', function() {
+ it('should inject the version correctly', function() {
+ var basic = versionator.createBasic('v0.0.0');
+ basic.versionPath('/js/test.js').should.eql('/js/v0.0.0/test.js');
+ });
+ it('strings without a \'/\' will be left unchanged' , function() {
+ var basic = versionator.createBasic('v0.0.0');
+ basic.versionPath('Hello this is an odd path').should.eql('Hello this is an odd path');
+ });
- it('should convert all urls in an array', function() {
- var basic = versionator.createBasic('v0.0.0');
- basic.versionPath(['/js/test.js']).should.eql(['/js/v0.0.0/test.js']);
- });
+ it('should convert all urls in an array', function() {
+ var basic = versionator.createBasic('v0.0.0');
+ basic.versionPath(['/js/test.js']).should.eql(['/js/v0.0.0/test.js']);
+ });
- it('should return an empty array when passed an empty url', function() {
- var basic = versionator.createBasic('v0.0.0');
- basic.versionPath([]).should.eql([]);
- });
- });
+ it('should return an empty array when passed an empty url', function() {
+ var basic = versionator.createBasic('v0.0.0');
+ basic.versionPath([]).should.eql([]);
+ });
+ });
- });
+ });
- describe('basic middleware', function() {
+ describe('basic middleware', function() {
- function startServer(port) {
- var app = appEngine.createServer(
- versionator.createBasic('v0.1.2').middleware,
- function(req, res, next) {
- res.end(req.url);
- }
- );
+ function startServer(port) {
+ var app = appEngine.createServer(
+ versionator.createBasic('v0.1.2').middleware,
+ function(req, res, next) {
+ res.end(req.url);
+ }
+ );
- return app.listen(port);
- }
+ return app.listen(port);
+ }
- it('req.url has version removed from pathname', function(done) {
+ it('req.url has version removed from pathname', function(done) {
- var app = startServer(9898);
+ var app = startServer(9898);
- request('http://localhost:9898/images/v0.1.2/sprite.png', function(error, response, data) {
- data.should.eql('/images/sprite.png');
- app.close();
- done();
- });
- });
+ request('http://localhost:9898/images/v0.1.2/sprite.png', function(error, response, data) {
+ data.should.eql('/images/sprite.png');
+ app.close();
+ done();
+ });
+ });
- it('req.url is unchanged if no version match is found', function(done) {
+ it('req.url is unchanged if no version match is found', function(done) {
- var app = startServer(9899);
+ var app = startServer(9899);
- request('http://localhost:9899/images/sprite.png', function(error, response, data) {
- data.should.eql('/images/sprite.png');
- app.close();
- done();
- });
- });
+ request('http://localhost:9899/images/sprite.png', function(error, response, data) {
+ data.should.eql('/images/sprite.png');
+ app.close();
+ done();
+ });
+ });
- });
+ });
});
126 test/map-path.test.js
View
@@ -1,97 +1,97 @@
-var
- assert = require('assert'),
- request = require('request'),
- versionator = require('../'),
- appEngine = require('connect'),
- fs = require('fs'),
- mkdirp = require('mkdirp'),
- async = require('async');
+var assert = require('assert')
+ , request = require('request')
+ , versionator = require('../')
+ , appEngine = require('connect')
+ , fs = require('fs')
+ , mkdirp = require('mkdirp')
+ , async = require('async')
+ ;
function createFiles(dirPath, files, callback) {
- var fns = [
- async.apply(mkdirp, dirPath + '/sub')
- ];
+ var fns = [
+ async.apply(mkdirp, dirPath + '/sub')
+ ];
- Object.keys(files).forEach(function(filename) {
- fns.push(async.apply(fs.writeFile, dirPath + '/' + filename, files[filename]));
- });
+ Object.keys(files).forEach(function(filename) {
+ fns.push(async.apply(fs.writeFile, dirPath + '/' + filename, files[filename]));
+ });
- async.series(fns, callback);
+ async.series(fns, callback);
}
function removeFiles(dirPath, files, callback) {
- var fns = [];
+ var fns = [];
- Object.keys(files).forEach(function(filename) {
- fns.push(async.apply(fs.unlink, dirPath + '/' + filename));
- });
+ Object.keys(files).forEach(function(filename) {
+ fns.push(async.apply(fs.unlink, dirPath + '/' + filename));
+ });
- fns.push(async.apply(fs.rmdir, dirPath + '/sub'));
- fns.push(async.apply(fs.rmdir, dirPath));
+ fns.push(async.apply(fs.rmdir, dirPath + '/sub'));
+ fns.push(async.apply(fs.rmdir, dirPath));
- async.series(fns, callback);
+ async.series(fns, callback);
}
describe('versionator', function() {
- var
- tmpPath = '/tmp/versionator-test',
- files = {
- 'a': '',
- 'b': 'Hello',
- 'c': 'World!',
- 'sub/a': 'hi'
- };
+ var
+ tmpPath = '/tmp/versionator-test',
+ files = {
+ 'a': '',
+ 'b': 'Hello',
+ 'c': 'World!',
+ 'sub/a': 'hi'
+ };
- before(function(done) {
- createFiles(tmpPath, files, function() {
- done();
- });
- });
+ before(function(done) {
+ createFiles(tmpPath, files, function() {
+ done();
+ });
+ });
- describe('map path validation', function() {
+ describe('map path validation', function() {
- it('should error if an invalid path is given', function() {
+ it('should error if an invalid path is given', function() {
- versionator.createMapFromPath('', function(error) {
- error.should.be.instanceof(Error).with.property('message').eql('Path is required');
- });
- });
+ versionator.createMapFromPath('', function(error) {
+ error.should.be.instanceof(Error).with.property('message').eql('Path is required');
+ });
+ });
- it('should correctly walk directory and create hashes', function(done) {
+ it('should correctly walk directory and create hashes', function(done) {
- versionator.createMapFromPath(tmpPath, function(error, results) {
+ versionator.createMapFromPath(tmpPath, function(error, results) {
- var a = {
- '/a': '/d41d8cd98f00b204e9800998ecf8427e/a',
- '/b': '/8b1a9953c4611296a827abf8c47804d7/b',
- '/c': '/e509465ef513154988e088d6ad3c21bf/c',
- '/sub/a': '/sub/49f68a5c8493ec2c0bf489821c21fc3b/a' }.should.eql(results);
+ var a = {
+ '/a': '/d41d8cd98f00b204e9800998ecf8427e/a',
+ '/b': '/8b1a9953c4611296a827abf8c47804d7/b',
+ '/c': '/e509465ef513154988e088d6ad3c21bf/c',
+ '/sub/a': '/sub/49f68a5c8493ec2c0bf489821c21fc3b/a' }.should.eql(results);
- done();
- });
+ done();
+ });
- });
+ });
- it('should accept fileList and create hashes', function(done) {
+ it('should accept fileList and create hashes', function(done) {
var fileList = [
tmpPath + '/c',
tmpPath + '/sub/a'
];
- versionator.createMapFromPath(tmpPath, {'fileList': fileList}, function(error, results) {
+ versionator.createMapFromPath(tmpPath, {'fileList': fileList}, function(error, results) {
- var a = {
- '/c': '/e509465ef513154988e088d6ad3c21bf/c',
- '/sub/a': '/sub/49f68a5c8493ec2c0bf489821c21fc3b/a' }.should.eql(results);
+ var a = {
+ '/c': '/e509465ef513154988e088d6ad3c21bf/c',
+ '/sub/a': '/sub/49f68a5c8493ec2c0bf489821c21fc3b/a' }.should.eql(results);
- done();
- });
+ done();
+ });
- });
+ });
- });
+ });
- after(function(done) {
- removeFiles(tmpPath, files, done);
- });
+ after(function(done) {
+ removeFiles(tmpPath, files, done);
+ });
});
195 test/mapped-versionator.test.js
View
@@ -1,109 +1,108 @@
-var
- appEngine,
- assert = require('assert'),
- request = require('request'),
- versionator = require('../'),
- appEngine = require('connect'),
- http = require('http');
+var appEngine
+ , assert = require('assert')
+ , request = require('request')
+ , versionator = require('../')
+ , appEngine = require('connect')
+ ;
describe('versionator', function() {
- describe('#createMapped', function() {
-
- it('throws an error if no map is passed', function() {
-
- (function() {
- versionator.createMapped();
- }).should.throw('A hash map of paths is required');
-
- });
-
- it('returns a middleware function', function() {
- versionator.createMapped({}).middleware.should.be.a('function');
- });
-
- describe('#versionPath', function() {
- it('should inject the mapped url correctly', function() {
- var mapped = versionator.createMapped({ '/js/test.js': '/js/HASH/test.js'});
- mapped.versionPath('/js/test.js').should.eql('/js/HASH/test.js');
- });
- it('should accept a modified map', function() {
- var mapped = versionator.createMapped({ '/js/test.js': '/js/HASH/test.js'});
- mapped.modifyMap({ '/js/test.js': '/js/OTHERHASH/test.js'});
- mapped.versionPath('/js/test.js').should.eql('/js/OTHERHASH/test.js');
- });
- it('strings without a \'/\' will be left unchanged' , function() {
- var mapped = versionator.createMapped({ '/js/test.js': '/js/HASH/test.js'});
- mapped.versionPath('Hello this is an odd path').should.eql('Hello this is an odd path');
- });
-
- it('should convert all urls in an array', function() {
- var mapped = versionator.createMapped({ '/js/test.js': '/js/HASH/test.js'});
- mapped.versionPath(['/js/test.js']).should.eql(['/js/HASH/test.js']);
- });
-
- it('should return an empty array when passed an empty url', function() {
- var basic = versionator.createBasic('v0.0.0');
- basic.versionPath([]).should.eql([]);
- });
- });
-
- });
-
- describe('mapped middleware', function() {
-
- function startServer(map, port) {
- var mapped = versionator.createMapped(map);
- var app = appEngine.createServer(
- mapped.middleware,
- function(req, res, next) {
- res.end(req.url);
- }
- );
-
- return {
- app: app.listen(port),
- mapped: mapped
- };
- }
-
- it('req.url is unchanged if no version match is found', function(done) {
-
- var app = startServer({}, 9900).app;
-
- request('http://localhost:9900/images/sprite.png', function(error, response, data) {
- data.should.eql('/images/sprite.png');
- app.close();
- done();
- });
- });
-
- it('req.url mapped url is mapped correctly', function(done) {
-
- var app = startServer({'/images/sprite.png': '/images/VERSIONHASH/sprite.png' }, 9901).app;
-
- request('http://localhost:9901/images/VERSIONHASH/sprite.png', function(error, response, data) {
- data.should.eql('/images/sprite.png');
- app.close();
- done();
- });
- });
-
- it('req.url mapped url is mapped correctly after hash change', function(done) {
-
- var appObj = startServer({'/images/sprite.png': '/images/VERSIONHASH/sprite.png' }, 9902);
+ describe('#createMapped', function() {
+
+ it('throws an error if no map is passed', function() {
+
+ (function() {
+ versionator.createMapped();
+ }).should.throw('A hash map of paths is required');
+
+ });
+
+ it('returns a middleware function', function() {
+ versionator.createMapped({}).middleware.should.be.a('function');
+ });
+
+ describe('#versionPath', function() {
+ it('should inject the mapped url correctly', function() {
+ var mapped = versionator.createMapped({ '/js/test.js': '/js/HASH/test.js'});
+ mapped.versionPath('/js/test.js').should.eql('/js/HASH/test.js');
+ });
+ it('should accept a modified map', function() {
+ var mapped = versionator.createMapped({ '/js/test.js': '/js/HASH/test.js'});
+ mapped.modifyMap({ '/js/test.js': '/js/OTHERHASH/test.js'});
+ mapped.versionPath('/js/test.js').should.eql('/js/OTHERHASH/test.js');
+ });
+ it('strings without a \'/\' will be left unchanged' , function() {
+ var mapped = versionator.createMapped({ '/js/test.js': '/js/HASH/test.js'});
+ mapped.versionPath('Hello this is an odd path').should.eql('Hello this is an odd path');
+ });
+
+ it('should convert all urls in an array', function() {
+ var mapped = versionator.createMapped({ '/js/test.js': '/js/HASH/test.js'});
+ mapped.versionPath(['/js/test.js']).should.eql(['/js/HASH/test.js']);
+ });
+
+ it('should return an empty array when passed an empty url', function() {
+ var basic = versionator.createBasic('v0.0.0');
+ basic.versionPath([]).should.eql([]);
+ });
+ });
+
+ });
+
+ describe('mapped middleware', function() {
+
+ function startServer(map, port) {
+ var mapped = versionator.createMapped(map);
+ var app = appEngine.createServer(
+ mapped.middleware,
+ function(req, res, next) {
+ res.end(req.url);
+ }
+ );
+
+ return {
+ app: app.listen(port),
+ mapped: mapped
+ };
+ }
+
+ it('req.url is unchanged if no version match is found', function(done) {
+
+ var app = startServer({}, 9900).app;
+
+ request('http://localhost:9900/images/sprite.png', function(error, response, data) {
+ data.should.eql('/images/sprite.png');
+ app.close();
+ done();
+ });
+ });
+
+ it('req.url mapped url is mapped correctly', function(done) {
+
+ var app = startServer({'/images/sprite.png': '/images/VERSIONHASH/sprite.png' }, 9901).app;
+
+ request('http://localhost:9901/images/VERSIONHASH/sprite.png', function(error, response, data) {
+ data.should.eql('/images/sprite.png');
+ app.close();
+ done();
+ });
+ });
+
+ it('req.url mapped url is mapped correctly after hash change', function(done) {
+
+ var appObj = startServer({'/images/sprite.png': '/images/VERSIONHASH/sprite.png' }, 9902);
var app = appObj.app;
var mapped = appObj.mapped;
mapped.modifyMap({ '/images/sprite.png': '/images/OTHERHASH/sprite.png' });
- request('http://localhost:9902/images/OTHERHASH/sprite.png', function(error, response, data) {
- data.should.eql('/images/sprite.png');
- app.close();
- done();
- });
- });
+ request('http://localhost:9902/images/OTHERHASH/sprite.png', function(error, response, data) {
+ data.should.eql('/images/sprite.png');
+ app.close();
+ done();
+ });
+ });
- });
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.