Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #114 from nusantara-cloud/master
Browse files Browse the repository at this point in the history
Several Improved Goodness From Other Forks
  • Loading branch information
niftylettuce committed Oct 24, 2017
2 parents 823c1f7 + a34d488 commit 3a5fe6d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 104 deletions.
140 changes: 42 additions & 98 deletions lib/main.js
Expand Up @@ -24,7 +24,8 @@ var fs = require('fs'),
spawn = require('child_process').spawn,
optipngPath = require('optipng-bin'),
jpegtranPath = require('jpegtran-bin'),
cleanCSS = require('clean-css')
cleanCSS = require('clean-css'),
retry = require('retry')

_.str = require('underscore.string');
_.mixin(_.str.exports());
Expand Down Expand Up @@ -156,10 +157,32 @@ var renderTag = function(options, assets, attributes) {

};

var compile = function(fileName, assets, S3, options, method, type, timestamp, callback) {
var pushFileToS3 = function(S3, text, fileName, headers, callback) {
var finishUpload = function() {
return callback && callback();
};
zlib.gzip(text, function(err, buffer) {
if (err) { throwError(err); }

var operation = retry.operation();
operation.attempt(function(currentAttempt) {
var req = S3.putBuffer(buffer, fileName, headers, function(err, response) {
if (operation.retry(err)) { return; }

if (response.statusCode === 200) {
return finishUpload();
}
if (err) {
return throwError(operation.mainError());
}
throwError('unsuccessful upload of script "' + fileName + '" to S3');
});
req.end();
});
});
}

var compile = function(fileName, assets, S3, options, method, type, timestamp, callback) {
return function(err, results) {
if (err) throwError(err);
var expires = new Date(new Date().getTime() + (31556926 * 1000)).toUTCString();
Expand All @@ -179,29 +202,20 @@ var compile = function(fileName, assets, S3, options, method, type, timestamp, c
case 'uglify':
if (results instanceof Array)
results = results.join("\n");
var final_code = uglify.minify(results, {
fromString: true,
output: {
comments: /license/
}
}).code;
zlib.gzip(final_code, function(err, buffer) {
if (err) throwError(err);
S3.putBuffer(buffer, fileName, headers, function(err, response) {
if (err) return throwError(err);
if (response.statusCode !== 200) {
//return throwError('unsuccessful upload of script "' + fileName + '" to S3');
console.log('unsuccessful upload of script "' + fileName + '" to S3');
return finishUpload();
} else {
logger({
task: 'express-cdn',
message: 'successfully uploaded script "' + fileName + '" to S3'
});
return finishUpload();
try {
var final_code = uglify.minify(results, {
fromString: true,
output: {
comments: /license/
}
});
});
}).code;
pushFileToS3(S3, final_code, fileName, headers, callback);
} catch(err) {
logger({
task: 'express-cdn',
message: 'Failed to minify ' + fileName + ' due to uglify-js error.'})
throw err
}
break;
case 'minify':
if (!(results instanceof Array)) {
Expand Down Expand Up @@ -263,25 +277,7 @@ var compile = function(fileName, assets, S3, options, method, type, timestamp, c

final_code.push(minify);
}

zlib.gzip(final_code.join("\n"), function(err, buffer) {
if (err) throwError(err);
S3.putBuffer(buffer, fileName, headers, function(err, response) {
if (err) throwError(err);
if (response.statusCode !== 200) {
//throwError('unsuccessful upload of stylesheet "' + fileName + '" to S3');
console.log('unsuccessful upload of stylesheet "' + fileName + '" to S3');
return finishUpload();
} else {
logger({
task: 'express-cdn',
message: 'successfully uploaded stylesheet "' + fileName + '" to S3'
});
return finishUpload();
}
});
});

pushFileToS3(S3, final_code.join("\n"), fileName, headers, callback);
break;
case 'optipng':
var img = assets;
Expand All @@ -308,24 +304,7 @@ var compile = function(fileName, assets, S3, options, method, type, timestamp, c
message: 'optipng exited with code ' + code
});
fs.readFile(img, function(err, data) {
zlib.gzip(data, function(err, buffer) {
S3.putBuffer(buffer, fileName, headers, function(err, response) {
if (err) throwError(err);
if (response.statusCode !== 200) {
//throwError('unsuccessful upload of image "' + fileName + '" to S3');
console.log('unsuccessful upload of image "' + fileName + '" to S3');
return finishUpload();
} else {
logger({
task: 'express-cdn',
message: 'successfully uploaded image "' + fileName + '" to S3'
});
// Hack to preserve original timestamp for view helper
fs.utimesSync(img, new Date(timestamp), new Date(timestamp));
return finishUpload();
}
});
});
pushFileToS3(S3, data, fileName, headers, callback);
});
});
break;
Expand All @@ -347,24 +326,7 @@ var compile = function(fileName, assets, S3, options, method, type, timestamp, c
message: 'jpegtran exited with code ' + code
});
fs.readFile(jpg, function(err, data) {
zlib.gzip(data, function(err, buffer) {
S3.putBuffer(buffer, fileName, headers, function(err, response) {
if (err) throwError(err);
if (response.statusCode !== 200) {
//throwError('unsuccessful upload of image "' + fileName + '" to S3');
console.log('unsuccessful upload of image "' + fileName + '" to S3');
return finishUpload();
} else {
logger({
task: 'express-cdn',
message: 'successfully uploaded image "' + fileName + '" to S3'
});
// Hack to preserve original timestamp for view helper
fs.utimesSync(jpg, new Date(timestamp), new Date(timestamp));
return finishUpload();
}
});
});
pushFileToS3(S3, data, fileName, headers, callback);
});
});
break;
Expand All @@ -373,25 +335,7 @@ var compile = function(fileName, assets, S3, options, method, type, timestamp, c
var image = assets.split("?")[0].split("#")[0];
fileName = fileName.split("?")[0].split("#")[0];
fs.readFile(image, function(err, data) {
zlib.gzip(data, function(err, buffer) {
S3.putBuffer(buffer, fileName, headers, function(err, response) {
if (err) throwError(err);
if (response.statusCode !== 200) {
//throwError('unsuccessful upload of image "' + fileName + '" to S3');
console.log('unsuccessful upload of image "' + fileName + '" to S3');
return finishUpload();
} else {
logger('successfully uploaded image "' + fileName + '" to S3');
// Hack to preserve original timestamp for view helper
try {
fs.utimesSync(image, new Date(timestamp), new Date(timestamp));
return finishUpload();
} catch (e) {
return finishUpload();
}
}
});
});
pushFileToS3(S3, data, fileName, headers, callback);
});
break;
}
Expand Down
13 changes: 7 additions & 6 deletions package.json
Expand Up @@ -114,17 +114,18 @@
"underscore.string": "~2.3.1"
},
"dependencies": {
"mime": "~1.2.6",
"underscore": "~1.3.3",
"walk": "~2.2.1",
"async": "~0.2.9",
"uglify-js": "~2.4.3",
"optipng-bin": "3.1.2",
"clean-css": "2.0.2",
"jpegtran-bin": "~3.0.6",
"knox": "~0.9.2",
"mime": "~1.2.6",
"optipng-bin": "3.1.2",
"request": "~2.16.6",
"retry": "^0.10.1",
"uglify-js": "~2.4.3",
"underscore": "~1.3.3",
"underscore.string": "~2.3.1",
"clean-css":"2.0.2"
"walk": "~2.2.1"
},
"readmeFilename": "Readme.md"
}

0 comments on commit 3a5fe6d

Please sign in to comment.