Skip to content

Commit

Permalink
Merge branch 'AppPress-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
respectTheCode committed Jun 12, 2014
2 parents 94fb030 + 0fc5a28 commit f29d5f1
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 229 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
faux-knox
faux-knox-2
=========

[![Build Status](https://travis-ci.org/wlaurance/faux-knox.png?branch=master)]
(https://travis-ci.org/wlaurance/faux-knox)
![david-dm](https://david-dm.org/wlaurance/faux-knox.png)
[![Build Status](https://travis-ci.org/AppPress/node-faux-knox-2.png?branch=master)]
(https://travis-ci.org/AppPress/node-faux-knox-2)
![david-dm](https://david-dm.org/AppPress/node-faux-knox-2.png)

A mock knox wrapper

Froked from https://github.com/wlaurance/faux-knox

###Installation

`npm install faux-knox`
`npm install faux-knox-2`

###Testing

`npm test`
`npm test`

###API

```js
Expand Down
232 changes: 151 additions & 81 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,154 @@
var fs = require('fs'),
async = require('async'),
utils = require(__dirname + '/utils');

exports.createClient = function(config){
function Client(config){
if (!config) config = {};
if (!config.bucket) {
config.bucket = './';
} else {
if (config.bucket[config.bucket.length - 1] !== '/') {
config.bucket = config.bucket + '/';
}
}
Client.prototype.getFile = function(uri, headers, callback){
if (!callback && typeof(headers) == "function") {
callback = headers;
headers = {};
}
var stream = fs.createReadStream(config.bucket + uri);
function cancelLocalListeners(){
stream.removeListener('error', bad);
stream.removeListener('readable', good);
}
function bad(e){
cancelLocalListeners();
if(e.code === 'ENOENT') {
stream.statusCode = 404;
stream.headers = {};
return callback(null, stream);
}
}
function good(){
stream.headers = {};
stream.statusCode = 200;
cancelLocalListeners();
return callback(null, stream);
}
stream.on('error', bad);
stream.on('readable', good);
};

Client.prototype.putFile = function(from, to, callback){
function checkToPath(cb){
utils.checkToPath(config.bucket + to, cb);
}
function checkFromPath(cb){
fs.stat(from, cb);
}
async.series([checkFromPath, checkToPath], function(err){
if (err) {
return callback(err);
}
var r = fs.createReadStream(from),
w = fs.createWriteStream(config.bucket + to);
w.on('finish', function(){
callback(null, {headers:{}, statusCode:201});
});
w.on('error', function(e){
callback(null, {headers:{}, statusCode:404});
});
r.pipe(w);
});
};
Client.prototype.putBuffer = function(buffer, to, headers, callback){
utils.checkToPath(config.bucket + to, function(){
fs.writeFile(config.bucket + to, buffer, function(err){
if (err) {
return callback(err);
}
return callback(null, {headers:{}, statusCode:201});
});
});
};
Client.prototype.deleteFile = function(file, callback){
fs.unlink(config.bucket + file, function(err){
return callback(null, {headers:{}, statusCode: err ? 404 : 204});
});
};
}
return new Client(config);
var fs = require("fs");
var async = require("async");
var utils = require(__dirname + "/utils");

var Client = module.exports = function (config) {
config = this.config = config || {};

if (!config.bucket) {
config.bucket = "./";
} else {
if (config.bucket[config.bucket.length - 1] !== "/") {
config.bucket = config.bucket + "/";
}
}
};

Client.prototype.getFile = function(uri, headers, callback) {
var self = this;

if (!callback && typeof(headers) == "function") {
callback = headers;
headers = {};
}
var stream = fs.createReadStream(self.config.bucket + uri);
function cancelLocalListeners() {
stream.removeListener("error", bad);
stream.removeListener("readable", good);
}
function bad(e) {
cancelLocalListeners();
if(e.code === "ENOENT") {
stream.statusCode = 404;
stream.headers = {};
return callback(null, stream);
}
}
function good() {
stream.headers = {};
stream.statusCode = 200;
cancelLocalListeners();
return callback(null, stream);
}
stream.on("error", bad);
stream.on("readable", good);
};

Client.prototype.putFile = function(from, to, headers, callback) {
var self = this;

if (typeof(callback) == "undefined") {
callback = headers;
}

async.series([function (cb) {
utils.checkToPath(self.config.bucket + to, cb);
}, function (cb) {
fs.stat(from, cb);
}], function(err) {
if (err) {
return callback(err);
}
var r = fs.createReadStream(from),
w = fs.createWriteStream(self.config.bucket + to);
w.on("finish", function() {
callback(null, {headers:{}, statusCode:201});
});
w.on("error", function(e) {
callback(null, {headers:{}, statusCode:404});
});
r.pipe(w);
});
};
Client.prototype.putBuffer = function(buffer, to, headers, callback) {
var self = this;

utils.checkToPath(self.config.bucket + to, function() {
fs.writeFile(self.config.bucket + to, buffer, function(err) {
if (err) {
return callback(err);
}

return callback(null, {headers:{}, statusCode:201});
});
});
};
Client.prototype.deleteFile = function(file, callback) {
var self = this;

fs.unlink(self.config.bucket + file, function(err) {
return callback(null, {headers:{}, statusCode: err ? 404 : 204});
});
};
Client.prototype.copyFile = function(from, to, callback) {
var self = this;

utils.checkToPath(self.config.bucket + to, function() {
var readStream = fs.createReadStream(self.config.bucket + from);
var writeStream = fs.createWriteStream(self.config.bucket + to);
var isDone = false;
var done = function (err) {
if (isDone) return;
isDone = true;

if (err) {
return callback(err);
}

return callback(null, {headers:{}, statusCode:201});
};

readStream.on("error", done);
writeStream.on("error", done);
writeStream.on("close", function () {
done();
});
readStream.pipe(writeStream);
});
};
Client.prototype.list = function (options, cb) {
var self = this;
async.waterfall([
function (cb) {
if (!options.prefix) {
return cb(new Error("A path prefix must be specified!"));
}
cb();
},
function (cb) {
utils.checkToPath(self.config.bucket + options.prefix, function () {
cb();
});
},
function (cb) {
var walk = require("walk");
var walker = walk.walk(self.config.bucket + options.prefix);
var files = [];

walker.on("file", function (root, stat, next) {
files.push({Key: options.prefix + stat.name});
next();
});

walker.on("end", function () {
cb(null, {Contents: files});
});
}
], cb);
};

module.exports.createClient = function(config) {
return new Client(config);
};


12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "faux-knox",
"version": "0.1.5-1",
"name": "faux-knox-2",
"version": "0.1.9",
"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"
"url": "git://github.com/AppPress/node-faux-knox-2.git"
},
"keywords": [
"knox",
Expand All @@ -17,9 +17,8 @@
"author": "Will S. Laurance",
"license": "BSD",
"readmeFilename": "README.md",
"gitHead": "cf1d54e5cf8ae5e6a6e2142ab42f7eb4a068147a",
"bugs": {
"url": "https://github.com/wlaurance/faux-knox/issues"
"url": "https://github.com/AppPress/node-faux-knox-2/issues"
},
"devDependencies": {
"mocha": "~1.12.0",
Expand All @@ -29,6 +28,7 @@
"underscore": "~1.5.1",
"async": "~0.2.9",
"mkdirp": "~0.3.5",
"rimraf": "~2.2.1"
"rimraf": "~2.2.1",
"walk": "^2.3.1"
}
}
Loading

0 comments on commit f29d5f1

Please sign in to comment.