Comparing version 0.1.5 to 0.1.9
224
index.js
@@ -1,80 +0,154 @@ | ||
var fs = require('fs'), | ||
async = require('async'), | ||
utils = require(__dirname + '/utils'); | ||
var fs = require("fs"); | ||
var async = require("async"); | ||
var 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){ | ||
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); | ||
}; | ||
var Client = module.exports = function (config) { | ||
config = this.config = config || {}; | ||
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); | ||
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); | ||
}; | ||
{ | ||
"name": "faux-knox", | ||
"version": "0.1.5", | ||
"version": "0.1.9", | ||
"description": "Mock requests to knox module using file system", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "git://github.com/wlaurance/faux-knox.git" | ||
"url": "git://github.com/AppPress/node-faux-knox.git" | ||
}, | ||
@@ -21,5 +21,4 @@ "keywords": [ | ||
"readmeFilename": "README.md", | ||
"gitHead": "cf1d54e5cf8ae5e6a6e2142ab42f7eb4a068147a", | ||
"bugs": { | ||
"url": "https://github.com/wlaurance/faux-knox/issues" | ||
"url": "https://github.com/AppPress/node-faux-knox/issues" | ||
}, | ||
@@ -34,4 +33,5 @@ "devDependencies": { | ||
"mkdirp": "~0.3.5", | ||
"rimraf": "~2.2.1" | ||
"rimraf": "~2.2.1", | ||
"walk": "^2.3.1" | ||
} | ||
} |
@@ -1,7 +0,7 @@ | ||
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.png?branch=master)] | ||
(https://travis-ci.org/AppPress/node-faux-knox) | ||
![david-dm](https://david-dm.org/AppPress/node-faux-knox.png) | ||
@@ -16,4 +16,4 @@ A mock knox wrapper | ||
`npm test` | ||
`npm test` | ||
###API | ||
@@ -20,0 +20,0 @@ |
249
test.js
@@ -1,111 +0,142 @@ | ||
var should = require('should'), | ||
fs = require('fs'), | ||
async = require('async'), | ||
rimraf = require('rimraf'), | ||
knox = require('./index'); | ||
var should = require("should"); | ||
var fs = require("fs"); | ||
var async = require("async"); | ||
var rimraf = require("rimraf"); | ||
var knox = require("./index"); | ||
describe('Faux-Knox', function(){ | ||
var client = knox.createClient({bucket:'./test_files'}); | ||
describe('API', function(){ | ||
it('should have a createClient function', function(){ | ||
knox.should.have.property('createClient').be.a('function'); | ||
}); | ||
it('should support methods', function(done){ | ||
var methods = ['getFile', 'putFile', 'putBuffer', 'deleteFile']; | ||
function checker(method, callback){ | ||
client.should.have.property(method).be.a('function'); | ||
callback(); | ||
} | ||
async.each(methods, checker, done); | ||
}); | ||
}); | ||
describe('getFile', function(){ | ||
it('should get a file', function(done){ | ||
client.getFile('path/to/test.json', null, function(err, cres){ | ||
cres.should.have.property('headers').be.a('object'); | ||
cres.should.have.property('statusCode', 200); | ||
function getBuffer(callback){ | ||
var buffer = ""; | ||
cres.on('end', function(){ | ||
callback(null, buffer); | ||
}); | ||
cres.on('data', function(d){ | ||
buffer = buffer + d.toString(); | ||
}); | ||
}; | ||
function getFSFile(callback){ | ||
fs.readFile('./test_files/path/to/test.json', callback); | ||
} | ||
async.parallel([getBuffer, getFSFile], function(err, results){ | ||
should.strictEqual(results[0], results[1].toString()); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should not get a file', function(done){ | ||
client.getFile('path/to/nofile.txt', null, function(err, cres){ | ||
cres.should.have.property('statusCode', 404); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('putFile', function(){ | ||
it('should put a file into bucket', function(done){ | ||
client.putFile('./test_files/put/fort_knox_tank.jpg', 'from/fort/knox/super_tank.jpg', function(err, res){ | ||
res.should.have.property('headers').be.a('object'); | ||
res.should.have.property('statusCode', 201); | ||
fs.exists('./test_files/from/fort/knox/super_tank.jpg', function(existy){ | ||
existy.should.be.true; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should not put a file into bucket', function(done){ | ||
client.putFile('./i/dont/exists.txt', '/dev/null', function(err, res){ | ||
err.should.be.instanceOf(Error); | ||
err.should.have.property('code', 'ENOENT'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('putBuffer', function(){ | ||
it('should put a buffer where I tell it to', function(done){ | ||
var buff = new Buffer(4096); | ||
client.putBuffer(buff, 'from/buffer/land/dev/null.text', {'Content-Type':'text/plain'}, function(err, res){ | ||
res.should.have.property('headers').be.a('object'); | ||
res.should.have.property('statusCode', 201); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('deleteFile', function(){ | ||
before(function(done){ | ||
client.putFile('./test_files/put/fort_knox_tank.jpg', 'to/a/new/path/here/tank.jpg', done); | ||
}); | ||
it('should delete a file', function(done){ | ||
function fileExists(value, callback){ | ||
fs.exists('./test_files/put/fort_knox_tank.jpg', function(exists){ | ||
exists.should.be.value; | ||
callback(); | ||
}); | ||
} | ||
fileExists(true, function(){ | ||
client.deleteFile('to/a/new/path/here/tank.jpg', function(err, res){ | ||
res.should.have.property('headers').be.a('object'); | ||
res.should.have.property('statusCode', 204); | ||
fileExists(false, done); | ||
}); | ||
}); | ||
}); | ||
it('should not delete a file', function(done){ | ||
client.deleteFile('not/a/real/path.js', function(err, res){ | ||
res.should.have.property('headers').be.a('object'); | ||
res.should.have.property('statusCode', 404); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
after(function(done){ | ||
async.each(['./test_files/from', './test_files/to'], rimraf, done); | ||
}); | ||
describe("Faux-Knox", function() { | ||
var client = knox.createClient({bucket:"./test_files"}); | ||
describe("API", function() { | ||
it("should have a createClient function", function() { | ||
knox.should.have.property("createClient").be.a("function"); | ||
}); | ||
it("should support methods", function(done) { | ||
var methods = ["getFile", "putFile", "putBuffer", "deleteFile"]; | ||
function checker(method, callback) { | ||
client.should.have.property(method).be.a("function"); | ||
callback(); | ||
} | ||
async.each(methods, checker, done); | ||
}); | ||
}); | ||
describe("getFile", function() { | ||
it("should get a file", function(done) { | ||
client.getFile("path/to/test.json", null, function(err, cres) { | ||
cres.should.have.property("headers").be.a("object"); | ||
cres.should.have.property("statusCode", 200); | ||
function getBuffer(callback) { | ||
var buffer = ""; | ||
cres.on("end", function() { | ||
callback(null, buffer); | ||
}); | ||
cres.on("data", function(d) { | ||
buffer = buffer + d.toString(); | ||
}); | ||
} | ||
function getFSFile(callback) { | ||
fs.readFile("./test_files/path/to/test.json", callback); | ||
} | ||
async.parallel([getBuffer, getFSFile], function(err, results) { | ||
should.strictEqual(results[0], results[1].toString()); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("should not get a file", function(done) { | ||
client.getFile("path/to/nofile.txt", null, function(err, cres) { | ||
cres.should.have.property("statusCode", 404); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe("putFile", function() { | ||
it("should put a file into bucket", function(done) { | ||
client.putFile("./test_files/put/fort_knox_tank.jpg", "from/fort/knox/super_tank.jpg", function(err, res) { | ||
res.should.have.property("headers").be.a("object"); | ||
res.should.have.property("statusCode", 201); | ||
fs.exists("./test_files/from/fort/knox/super_tank.jpg", function(existy) { | ||
should.strictEqual(existy, true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("should put a file into bucket with headers", function(done) { | ||
client.putFile("./test_files/put/fort_knox_tank.jpg", "from/fort/knox/super_tank_headers.jpg", {header: "value"}, function(err, res) { | ||
res.should.have.property("headers").be.a("object"); | ||
res.should.have.property("statusCode", 201); | ||
fs.exists("./test_files/from/fort/knox/super_tank_headers.jpg", function(existy) { | ||
should.strictEqual(existy, true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("should not put a file into bucket", function(done) { | ||
client.putFile("./i/dont/exists.txt", "/dev/null", function(err, res) { | ||
err.should.be.instanceOf(Error); | ||
err.should.have.property("code", "ENOENT"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe("putBuffer", function() { | ||
it("should put a buffer where I tell it to", function(done) { | ||
var buff = new Buffer(4096); | ||
client.putBuffer(buff, "from/buffer/land/dev/null.text", {"Content-Type":"text/plain"}, function(err, res) { | ||
res.should.have.property("headers").be.a("object"); | ||
res.should.have.property("statusCode", 201); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe("deleteFile", function() { | ||
before(function(done) { | ||
client.putFile("./test_files/put/fort_knox_tank.jpg", "to/a/new/path/here/tank.jpg", done); | ||
}); | ||
it("should delete a file", function(done) { | ||
function fileExists(value, callback) { | ||
fs.exists("./test_files/to/a/new/path/here/tank.jpg", function(exists) { | ||
should.strictEqual(exists, value); | ||
callback(); | ||
}); | ||
} | ||
fileExists(true, function() { | ||
client.deleteFile("to/a/new/path/here/tank.jpg", function(err, res) { | ||
res.should.have.property("headers").be.a("object"); | ||
res.should.have.property("statusCode", 204); | ||
fileExists(false, done); | ||
}); | ||
}); | ||
}); | ||
it("should not delete a file", function(done) { | ||
client.deleteFile("not/a/real/path.js", function(err, res) { | ||
res.should.have.property("headers").be.a("object"); | ||
res.should.have.property("statusCode", 404); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe("copyFile", function () { | ||
before(function(done) { | ||
client.putFile("./test_files/put/fort_knox_tank.jpg", "to/a/new/path/here/tank.jpg", done); | ||
}); | ||
it("should copy a file", function (done) { | ||
function fileExists(value, callback) { | ||
fs.exists("./test_files/to/a/new/path/here/tankCopy.jpg", function(exists) { | ||
should.strictEqual(exists, value); | ||
callback(); | ||
}); | ||
} | ||
fileExists(false, function() { | ||
client.copyFile("to/a/new/path/here/tank.jpg", "to/a/new/path/here/tankCopy.jpg", function(err, res) { | ||
res.should.have.property("headers").be.a("object"); | ||
res.should.have.property("statusCode", 201); | ||
fileExists(true, done); | ||
}); | ||
}); | ||
}); | ||
}); | ||
after(function(done) { | ||
async.each(["./test_files/from", "./test_files/to"], rimraf, done); | ||
}); | ||
}); |
21
utils.js
@@ -1,13 +0,12 @@ | ||
var _ = require('underscore'), | ||
mkdirp = require('mkdirp'), | ||
fs = require('fs'); | ||
var _ = require('underscore'); | ||
var mkdirp = require('mkdirp'); | ||
var fs = require('fs'); | ||
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); | ||
}); | ||
var checkToPath = module.exports.checkToPath = function (to, cb) { | ||
var splitPath = to.split('/'); | ||
var dirPath = _.initial(splitPath, 1).join('/'); | ||
fs.exists(dirPath, function(exists){ | ||
return exists ? cb() : mkdirp(dirPath, cb); | ||
}); | ||
} | ||
exports.checkToPath = checkToPath; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
11816
285
5
2
+ Addedwalk@^2.3.1
+ Addedforeachasync@3.0.0(transitive)
+ Addedwalk@2.3.15(transitive)