Skip to content

Commit

Permalink
Merge branch 'release/v0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jokeyrhyme committed Nov 18, 2013
2 parents c39f011 + 5280d8d commit 9803733
Show file tree
Hide file tree
Showing 27 changed files with 499 additions and 123 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ node_js:
before_script:
- npm install -g grunt-cli

script: 'grunt travis'

matrix:
allow_failures:
- node_js: "0.11"
Expand Down
26 changes: 17 additions & 9 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*jslint es5:true, indent:2, maxlen:80, node:true*/
/*jslint indent:2, maxlen:80, node:true*/
'use strict';

module.exports = function (grunt) {
Expand Down Expand Up @@ -34,11 +34,22 @@ module.exports = function (grunt) {
},

mochacov: {
html: {
options: {
reporter: 'html-cov',
output: 'tmp/coverage.html'
}
},
coveralls: {
options: {
coveralls: {
serviceName: 'travis-ci'
}
}
},
options: {
reporter: 'html-cov',
require: ['chai'],
ui: 'tdd',
output: 'tmp/coverage.html'
ui: 'tdd'
},
all: ['test/*.js']
},
Expand All @@ -63,11 +74,8 @@ module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-mocha-cli');
grunt.loadNpmTasks('grunt-mocha-cov');

if (process.env.CI && process.env.TRAVIS) {
grunt.registerTask('test', ['jslint', 'mochacli']);
} else {
grunt.registerTask('test', ['jslint', 'mochacli', 'mochacov']);
}
grunt.registerTask('travis', ['jslint', 'mochacli', 'mochacov:coveralls']);
grunt.registerTask('test', ['jslint', 'mochacli', 'mochacov:html']);

// Default task.
grunt.registerTask('default', ['test']);
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# cdn-sync
[![Build Status](https://travis-ci.org/jokeyrhyme/cdn-sync.png?branch=master,develop)](https://travis-ci.org/jokeyrhyme/cdn-sync)
[![NPM version](https://badge.fury.io/js/cdn-sync.png)](http://badge.fury.io/js/cdn-sync)
[![NPM version](https://badge.fury.io/js/cdn-sync.png)](http://badge.fury.io/js/cdn-sync) [![Coverage Status](https://coveralls.io/repos/jokeyrhyme/cdn-sync/badge.png)](https://coveralls.io/r/jokeyrhyme/cdn-sync)

Synchronise a local directory with AWS CloudFront / S3, maintaining correct headers, etc.

Expand Down
67 changes: 67 additions & 0 deletions doc/cdn-sync.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"targets": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": { "$ref": "#/definitions/target" },
"additionalItems": true
}
},
"required": ["targets"],
"additionalProperties": false,
"definitions": {
"target": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["aws"]
},
"options": { "$ref": "#/definitions/optionsAws" },
"strategy": {
"oneOf": [
{ "$ref": "#/definitions/strategyString" },
{ "$ref": "#/definitions/strategyArray" }
]
}
},
"required": ["type", "options", "strategy"],
"additionalProperties": false
},
"strategyString": {
"type": "string",
"enum": ["clone", "gzip", "gzip-suffix"]
},
"strategyArray": {
"type": "array",
"items": { "$ref": "#/definitions/strategyString" },
"uniqueItems": true,
"minItems": 1
},
"optionsAws": {
"type": "object",
"properties": {
"accessKeyId": {
"type": "string"
},
"secretAccessKey": {
"type": "string"
},
"region": {
"type": "string"
},
"sslEnabled": {
"type": "boolean"
},
"Bucket": {
"type": "string"
}
},
"required": ["accessKeyId", "secretAccessKey", "region", "Bucket"],
"additionalProperties": true
}
}
}
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*jslint es5:true, indent:2, maxlen:80, node:true*/
/*jslint indent:2, maxlen:80, node:true*/
/*jslint nomen:true*/ // Underscore.JS and __dirname
'use strict';
// Node.JS standard modules
Expand Down Expand Up @@ -56,6 +56,9 @@ function testConfig() {
} catch (err) {
cli.fatal(err);
}
config.validate().fail(function (err) {
cli.fatal(err);
});
config.testTargets().fail(function () {
cli.fatal('configured target fails basic tests');
}).done(function () {
Expand Down
3 changes: 2 additions & 1 deletion lib/action.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*jslint es5:true, indent:2, maxlen:80, node:true*/
/*jslint indent:2, maxlen:80, node:true*/
/*jslint nomen:true*/ // Underscore.JS and __dirname
'use strict';

Expand Down Expand Up @@ -55,6 +55,7 @@ Action.prototype.toString = function () {
if (this.doHeaders) { str += '#'; }
if (this.doDelete) { str += '-'; }
str += ' ' + this.path;
if (this.doHeaders) { str += JSON.stringify(this.file.headers); }
return str;
};

Expand Down
5 changes: 4 additions & 1 deletion lib/actionlist.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*jslint es5:true, indent:2, maxlen:80, node:true*/
/*jslint indent:2, maxlen:80, node:true*/
/*jslint nomen:true*/ // Underscore.JS and __dirname
'use strict';

Expand Down Expand Up @@ -56,6 +56,9 @@ ActionList.prototype.compareFileLists = function (local, remote) {
me.push(new Action({ file: file, doUpload: true }));
} else if (file.mime !== otherFile.mime) {
me.push(new Action({ file: file, doHeaders: true }));
} else if (file.headers['Content-Encoding'] !==
otherFile.headers['Content-Encoding']) {
me.push(new Action({ file: file, doHeaders: true }));
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/bufferutils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*jslint es5:true, indent:2, maxlen:80, node:true*/
/*jslint indent:2, maxlen:80, node:true*/
'use strict';
// Node.JS standard modules

Expand Down
53 changes: 16 additions & 37 deletions lib/cdn/aws.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*jslint es5:true, indent:2, maxlen:80, node:true, nomen:true*/
/*jslint indent:2, maxlen:80, node:true, nomen:true*/
'use strict';

// Node.JS standard modules
Expand Down Expand Up @@ -120,7 +120,7 @@ CDN.prototype.executeAction = function (action) {
};

CDN.prototype.executeActions = function (actions, options) {
var self, dfrd, deleteActions, uploadActions, otherActions;
var self, dfrd, deleteActions, otherActions;
self = this;
dfrd = Q.defer();

Expand Down Expand Up @@ -158,38 +158,13 @@ CDN.prototype.executeActions = function (actions, options) {
}
// only doHeader and doUpload actions left now

uploadActions = actions.filter(function (action) {
return action.doUpload && action.path;
});

otherActions = actions.filter(function (action) {
return !action.doUpload && action.path;
});

if (uploadActions.length) {
async.eachLimit(uploadActions, 5, function (action, done) { // perItem
self.executeUpload(action).done(done, done);
}, function (err) { // onComplete
if (err) {
dfrd.reject(err);
} else {
self.executeActions(otherActions).done(function () {
dfrd.resolve();
});
}
});
return dfrd.promise;
}

// only doHeader actions left now

async.eachLimit(actions, 5, function (action, done) { // perItem
self.executeAction(action).done(done, done);
async.eachLimit(actions, 10, function (action, done) { // perItem
self.executeUpload(action).done(done, done);
}, function (err) { // onComplete
if (err) {
dfrd.reject(err);
} else {
dfrd.resolve(actions);
dfrd.resolve();
}
});

Expand Down Expand Up @@ -218,6 +193,9 @@ CDN.prototype.fixFile = function (file) {
return;
}
file.setMIME(res.ContentType);
if (res.ContentEncoding) {
file.headers['Content-Encoding'] = res.ContentEncoding;
}
dfrd.resolve();
});

Expand All @@ -230,7 +208,7 @@ CDN.prototype.fixFiles = function (files) {
self = this;
dfrd = Q.defer();

async.eachLimit(files, 5, function (file, done) { // perItem
async.eachLimit(files, 10, function (file, done) { // perItem
self.fixFile(file).done(done, done);
}, function (err) { // onComplete
if (err) {
Expand All @@ -248,14 +226,13 @@ CDN.prototype.fixFiles = function (files) {
* @param {Object} [options]
* @returns {Promise}
*/
CDN.prototype.listAllFiles = function (options) {
var self, dfrd, files;
CDN.prototype.listAllFiles = function (files, options) {
var self, dfrd;
self = this;
dfrd = Q.defer();
options = options || {
Bucket: this.cfg.Bucket
};
files = new FileList();

this.api.listObjects(options, function (err, res) {
if (err) {
Expand All @@ -279,7 +256,7 @@ CDN.prototype.listAllFiles = function (options) {
});
if (res.IsTruncated) {
options.Marker = files[files.length - 1].path;
self.listFiles(options).done(function () {
self.listAllFiles(files, options).done(function () {
dfrd.resolve(files);
});
} else {
Expand All @@ -290,11 +267,13 @@ CDN.prototype.listAllFiles = function (options) {
};

CDN.prototype.listFiles = function () {
var self, dfrd;
var self, dfrd, files;
self = this;
dfrd = Q.defer();

this.listAllFiles()
files = new FileList();

this.listAllFiles(files)
.then(function (files) {
return self.fixFiles(files);
})
Expand Down
40 changes: 35 additions & 5 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*jslint es5:true, indent:2, maxlen:80, node:true*/
/*jslint indent:2, maxlen:80, node:true*/
/*jslint nomen:true*/ // Underscore.JS and __dirname
'use strict';
// Node.JS standard modules
Expand All @@ -7,19 +7,24 @@ var path = require('path');

// 3rd-party modules

var Q;
var Q, ZSchema;
Q = require('q');
ZSchema = require('z-schema');

// custom modules

var Target = require(path.join(__dirname, 'target'));
var Target, schema;
Target = require(path.join(__dirname, 'target'));
schema = require(path.join(__dirname, '..', 'doc', 'cdn-sync.schema.json'));

// this module

var Config = function (obj) {
if (!obj.targets || !obj.targets.length || !obj.targets.map) {
throw new Error('.cdn-sync.json defines no target CDNs');
if (!obj || !obj.targets || !Array.isArray(obj.targets)) {
throw new TypeError('#/targets: expecting an Array');
}
this.original = JSON.parse(JSON.stringify(obj));

this.targets = obj.targets.map(function (t) {
return new Target(t);
});
Expand All @@ -28,6 +33,31 @@ var Config = function (obj) {

Config.FILENAME = '.cdn-sync.json';

Config.validate = function (obj) {
var validator, dfrd;
dfrd = Q.defer();

validator = new ZSchema();
validator.validate(obj, schema, function (err, report) {
var error;
if (err) {
dfrd.reject(err);
return;
}
if (!report || report.valid) {
dfrd.resolve();
} else {
error = report.errors[0];
dfrd.reject(new TypeError(error.path + ': ' + error.message));
}
});
return dfrd.promise;
};

Config.prototype.validate = function () {
return Config.validate(this.original);
};

Config.prototype.testTargets = function () {
var tests = this.targets.map(function (t) {
return t.test();
Expand Down
Loading

0 comments on commit 9803733

Please sign in to comment.