Permalink
Browse files

use q to avoid callback hell and fix comparison bug

  • Loading branch information...
1 parent 307abb6 commit 254d4199d207b85c2d8afd7238dce4fd54c41585 @thomaspeklak committed Jul 18, 2013
Showing with 94 additions and 67 deletions.
  1. +24 −21 lib/plan-tasks.js
  2. +2 −1 package.json
  3. +68 −45 test/integration-test.js
View
45 lib/plan-tasks.js
@@ -7,6 +7,21 @@ var gm = require("gm");
var manipulationMethods = require("./manipulation-methods");
var Queue = require("./process-queue");
var mime = require("mime");
+var Q = require("q");
+var fsStat = Q.denodeify(fs.stat);
+
+var fsExists = function (file) {
+ var defer = Q.defer();
+ fs.exists(file, function (exists) {
+ if (exists) {
+ defer.resolve();
+ } else {
+ defer.reject(new Error('ENOFILE'));
+ }
+ });
+
+ return defer.promise;
+};
var planTasks = function (preset) {
var tasks = preset.tasks;
@@ -56,28 +71,16 @@ var planTasks = function (preset) {
var fileExistsAndIsValid = function (req, cb) {
var sourceFile = getSourceFile(req);
var targetFile = getTargetFilePath(req);
- fs.exists(sourceFile, function (exists) {
- if (!exists) return cb(404);
- fs.exists(targetFile, function (exists) {
- if (!exists) return cb(null, false);
- fs.stat(sourceFile, function (err, sourceStat) {
- if (err) {
- console.error(err);
- return cb(500);
- }
- var sourceCtime = sourceStat.ctime;
- fs.stat(targetFile, function (err, targetStat) {
- if (err) {
- console.error(err);
- return cb(err);
- }
- var targetCtime = targetStat.ctime;
-
- return cb(null, targetCtime > sourceCtime);
- });
- });
+
+ Q.allSettled([fsExists(sourceFile), fsExists(targetFile)])
+ .then(function (data) {
+ if (data[0].state == 'rejected') return cb(404);
+ if (data[1].state == 'rejected') return cb(null, null);
+
+ return Q.all([fsStat(sourceFile), fsStat(targetFile)]).then(function (stats) {
+ cb(null, stats[1].ctime < stats[0].ctime);
+ }, function () { cb(500); });
});
- });
};
var queue = new Queue();
View
3 package.json
@@ -23,7 +23,8 @@
"mkdirp": "0.3.x",
"express": ">3.1.0 <3.4.0",
"async": "0.2.x",
- "mime": "1.2.x"
+ "mime": "1.2.x",
+ "q": "~0.9.6"
},
"devDependencies": {
"mocha": "1.10.x",
View
113 test/integration-test.js
@@ -134,7 +134,7 @@ describe("resizer", function () {
.expect(200, checkFile);
});
- it("should save images with degradeed quality", function (done) {
+ it("should save images with degraded quality", function (done) {
var app = express();
app.use(express.static(__dirname + "/test-images"));
@@ -159,57 +159,14 @@ describe("resizer", function () {
.quality(80)
.to("/test-out-images");
+ app.use(express.static(__dirname + "/test-images"));
app.use(resizer.app);
app.locals.namePath.should.exist;
app.locals.namePath("/test-images/sub-dir/test.jpg").should.eql("/test-out-images/sub-dir/test.jpg");
app.locals.nameImage.should.exist;
app.locals.nameImage("/test-images/test.jpg", "Alt Text").should.eql('<img src="/test-out-images/test.jpg” alt="Alt Text">');
});
- it("should provide an app method to clear a preset", function (done) {
- var app = express();
- var resizer = new Resizer(__dirname);
- resizer.attach("name")
- .from("/test-images")
- .quality(80)
- .to("/test-out-images");
-
- app.use(resizer.app);
- app.resizer.clearName.should.exist;
-
- var checkFile = function () {
- app.resizer.clearName(function () {
- var file = "/test-out-images/profile.png";
- fs.exists(file, function (exists) {
- exists.should.be.false;
- done();
- });
- });
- };
- request(app)
- .get("/test-out-images/profile.png")
- .expect(200, checkFile);
- });
-
- it("should provide an app method to clear a file", function (done) {
- var checkFile = function () {
- var file = "/test-images/profile.png";
- app.resizer.clear(file, done);
- };
- var app = express();
- var resizer = new Resizer(__dirname);
- resizer.attach("name")
- .from("/test-images")
- .quality(80)
- .to("/test-out-images");
-
- app.use(resizer.app);
- app.resizer.clear.should.exist;
- request(app)
- .get("/test-out-images/profile.png")
- .expect(200, checkFile);
- });
-
it("should return images when requed in parallel", function (done) {
var app = express();
var count = 0;
@@ -267,4 +224,70 @@ describe("resizer", function () {
.expect(200, cleanup(__dirname + "/test-out-images/profile.png", done));
});
+ it("should provide an app method to clear a preset", function (done) {
+ var app = express();
+ var resizer = new Resizer(__dirname);
+ resizer.attach("name")
+ .from("/test-images")
+ .quality(80)
+ .to("/test-out-images");
+
+ app.use(express.static(__dirname + "/test-images"));
+ app.use(resizer.app);
+ app.resizer.clearName.should.exist;
+
+ var checkFile = function () {
+ app.resizer.clearName(function () {
+ var file = "/test-out-images/profile.png";
+ fs.exists(file, function (exists) {
+ exists.should.be.false;
+ done();
+ });
+ });
+ };
+ request(app)
+ .get("/test-out-images/profile.png")
+ .expect(200, checkFile);
+ });
+
+ it("should provide an app method to clear a file", function (done) {
+ var checkFile = function () {
+ var file = "/test-images/profile.png";
+ app.resizer.clear(file, function () {
+ var file = "/test-out-images/profile.png";
+ fs.exists(file, function (exists) {
+ exists.should.be.false;
+ done();
+ });
+ });
+ };
+ var app = express();
+ var resizer = new Resizer(__dirname);
+ resizer.attach("name")
+ .from("/test-images")
+ .quality(80)
+ .to("/test-out-images");
+
+ app.use(express.static(__dirname));
+ app.use(resizer.app);
+ app.resizer.clear.should.exist;
+ request(app)
+ .get("/test-out-images/profile.png")
+ .expect(200, checkFile);
+ });
+
+ it("should return a 404 if file not present", function (done) {
+ var app = express();
+
+ var resizer = new Resizer();
+ resizer.attach("name")
+ .publicDir(__dirname)
+ .from("/test-images")
+ .to("/test-out-images");
+ app.use(resizer.app);
+ app.use(express.static(__dirname + "/test-images"));
+ request(app)
+ .get("/test-out-images/non-existent.png")
+ .expect(404, done);
+ });
});

0 comments on commit 254d419

Please sign in to comment.