From e2988ea0b68807ce4418620f29a2c32f432033d9 Mon Sep 17 00:00:00 2001 From: brandly Date: Wed, 16 Sep 2015 15:41:16 -0400 Subject: [PATCH 1/2] update dependency versions --- package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 20eef4a..97534b5 100644 --- a/package.json +++ b/package.json @@ -21,14 +21,14 @@ "url": "https://github.com/AppPress/node-faux-knox/issues" }, "devDependencies": { - "mocha": "~1.21.4", - "should": "~4.0.4" + "mocha": "^2.3.2", + "should": "^7.1.0" }, "dependencies": { - "underscore": "~1.6.0", - "async": "~0.9.0", + "async": "^1.4.2", "mkdirp": "~0.5.0", - "rimraf": "~2.2.1", + "rimraf": "^2.4.3", + "underscore": "^1.8.3", "walk": "^2.3.1" } } From fd8373e016f2d22e4019f64b4f4f3159e21846a3 Mon Sep 17 00:00:00 2001 From: brandly Date: Thu, 17 Sep 2015 00:47:18 -0400 Subject: [PATCH 2/2] implement putStream --- index.js | 58 +++++++++++++++++++++++-- package.json | 2 + test.js | 14 +++++- test_files/path/to/stream/streamed.json | 29 +++++++++++++ utils.js | 21 ++++++--- 5 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 test_files/path/to/stream/streamed.json diff --git a/index.js b/index.js index 920af29..d8637f8 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,22 @@ var async = require("async"); var utils = require(__dirname + "/utils"); var join = require("path").join; var relative = require("path").relative; +var once = require("once"); +var StreamCounter = require("stream-counter"); + +var s3Response = { + '200': { headers: {}, statusCode: 200 }, + '404': { headers: {}, statusCode: 404 } +}; + +function getHeader(headers, headerNameLowerCase) { + for (var header in headers) { + if (header.toLowerCase() === headerNameLowerCase) { + return headers[header]; + } + } + return null; +} var Client = module.exports = function (config) { config = this.config = config || {}; @@ -65,10 +81,10 @@ Client.prototype.putFile = function (from, to, headers, callback) { var w = fs.createWriteStream(self.config.bucket + to); w.on("finish", function () { - callback(null, {headers:{}, statusCode:200}); + callback(null, s3Response['200']); }); w.on("error", function (e) { - callback(null, {headers:{}, statusCode:404}); + callback(null, s3Response['404']); }); r.pipe(w); }); @@ -83,11 +99,45 @@ Client.prototype.putBuffer = function (buffer, to, headers, callback) { return callback(err); } - return callback(null, {headers:{}, statusCode:200}); + return callback(null, s3Response['200']); }); }); }; +Client.prototype.putStream = function (stream, filename, headers, callback) { + var contentLength = getHeader(headers, 'content-length'); + if (contentLength === null) { + process.nextTick(function () { + callback(new Error('You must specify a Content-Length header.')); + }); + return; + } + + callback = once(callback); + var writeLocation = this.config.bucket + filename; + + utils.checkToPathSync(writeLocation); + var writeStream = fs.createWriteStream(writeLocation); + writeStream.on('error', callback); + + var counter = new StreamCounter(); + counter.on('progress', function () { + writeStream.emit('progress', { + percent: counter.bytes / contentLength * 100 | 0, + written: counter.bytes, + total: contentLength + }); + }); + + stream.pipe(counter) + stream.pipe(writeStream) + + writeStream.on('close', function () { + callback(null, s3Response['200']); + }); + return writeStream +} + Client.prototype.deleteFile = function (file, callback) { var self = this; @@ -111,7 +161,7 @@ Client.prototype.copyFile = function (from, to, callback) { return callback(err); } - return callback(null, {headers:{}, statusCode:200}); + return callback(null, s3Response['200']); }; readStream.on("error", done); diff --git a/package.json b/package.json index 97534b5..962f037 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,9 @@ "dependencies": { "async": "^1.4.2", "mkdirp": "~0.5.0", + "once": "^1.3.2", "rimraf": "^2.4.3", + "stream-counter": "^1.0.0", "underscore": "^1.8.3", "walk": "^2.3.1" } diff --git a/test.js b/test.js index 3c908a5..473b89f 100644 --- a/test.js +++ b/test.js @@ -12,7 +12,7 @@ describe("Faux-Knox", function () { knox.should.have.property("createClient").and.be.a.Function; }); it("should support methods", function (done) { - var methods = ["getFile", "putFile", "putBuffer", "deleteFile"]; + var methods = ["getFile", "putFile", "putBuffer", "putStream", "deleteFile"]; function checker(method, callback) { client.should.have.property(method).and.be.a.Function; callback(); @@ -89,6 +89,18 @@ describe("Faux-Knox", function () { }); }); }); + describe("putStream", function () { + it("should put a stream where I tell it to", function (done) { + var readStream = fs.createReadStream("./test_files/path/to/test.json"); + // Faking content-length for now, but should probably test progress events + var headers = {"Content-Length":"100"}; + client.putStream(readStream, 'path/to/stream/streamed.json', headers, function (err, res) { + res.should.have.property("headers").and.be.a.Object; + res.should.have.property("statusCode", 200); + done(); + }); + }); + }); describe("deleteFile", function () { before(function (done) { client.putFile("./test_files/put/fort_knox_tank.jpg", "to/a/new/path/here/tank.jpg", done); diff --git a/test_files/path/to/stream/streamed.json b/test_files/path/to/stream/streamed.json new file mode 100644 index 0000000..743694a --- /dev/null +++ b/test_files/path/to/stream/streamed.json @@ -0,0 +1,29 @@ +{ + "name": "faux-knox", + "version": "0.0.0", + "description": "Mock requests to knox module using file system", + "main": "index.js", + "scripts": { + "test": "./node_modules/.bin/mocha" + }, + "repository": { + "type": "git", + "url": "git://github.com/wlaurance/faux-knox.git" + }, + "keywords": [ + "knox", + "mock" + ], + "author": "Will S. Laurance", + "license": "BSD", + "readmeFilename": "README.md", + "gitHead": "cf1d54e5cf8ae5e6a6e2142ab42f7eb4a068147a", + "bugs": { + "url": "https://github.com/wlaurance/faux-knox/issues" + }, + "devDependencies": { + "mocha": "~1.12.0", + "async": "~0.2.9", + "should": "~1.2.2" + } +} diff --git a/utils.js b/utils.js index f102f1d..91c3cab 100644 --- a/utils.js +++ b/utils.js @@ -2,11 +2,20 @@ var _ = require('underscore'); var mkdirp = require('mkdirp'); var fs = require('fs'); -var checkToPath = module.exports.checkToPath = function (to, cb) { - var splitPath = to.split('/'); - var dirPath = _.initial(splitPath, 1).join('/'); +module.exports.checkToPath = function checkToPath(to, cb) { + var splitPath = to.split('/'); + var dirPath = _.initial(splitPath, 1).join('/'); - fs.exists(dirPath, function(exists){ - return exists ? cb() : mkdirp(dirPath, cb); - }); + fs.exists(dirPath, function(exists){ + return exists ? cb() : mkdirp(dirPath, cb); + }); +}; + +module.exports.checkToPathSync = function checkToPathSync(to) { + var splitPath = to.split('/'); + var dirPath = _.initial(splitPath, 1).join('/'); + + if (!fs.existsSync(dirPath)) { + mkdirp.sync(dirPath); + } };