Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rest-fs

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rest-fs - npm Package Compare versions

Comparing version 0.1.7 to 0.1.8

155

fileserver.js

@@ -9,9 +9,6 @@ // fileserver

var flow = require('middleware-flow');
var morgan = require('morgan');
var error = require('debug')('rest-fs:error');
error.log = console.error.bind(console);
/* used to modify format out output of data;
input of function is full filepath
*/
var modifyOut = null;
var isJson = mw.req('headers[\'content-type\']').matches(/application\/json/);
var fileserver = function(app) {

@@ -22,10 +19,10 @@ if (!app) {

app.use(flow.mwIf(isJson)
.then(bodyParser.json())
.else(bodyParser.raw()));
app.use(require('express-domain-middleware'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(morgan('combined', {
skip: function () { return process.env.LOG !== 'true'; }
}));
app.get(/^\/.+\/$/, getDir);

@@ -38,2 +35,3 @@ app.get(/^\/.+[^\/]$/, getFile);

app.use(function (err, req, res, next) {
error('uncaught error', err);
res.status(500).send(err);

@@ -51,9 +49,7 @@ });

return:
[
return: list of files/dirs
res.body = [
{
"name" : "file1", // name of dir or file
"path" : "/path/to/file", // path to dir or file
"dir" : false // true if directory
},
"fullFilePath"
}, ...
]

@@ -66,26 +62,21 @@ */

var handList = function (err, files) {
if (err) {
if (err && err.code === 'ENOTDIR') {
// this this is a file, redirect to file path
if (err.code === 'ENOTDIR') {
var originalUrl = url.parse(req.originalUrl);
originalUrl.pathname = originalUrl.pathname.substr(0, originalUrl.pathname.length - 1);
var target = url.format(originalUrl);
res.statusCode = 303;
res.setHeader('Location', target);
res.end('Redirecting to ' + target);
return;
} else if (err.code === 'ENOENT') {
return res.status(404).end();
} else {
return next(err);
var originalUrl = url.parse(req.originalUrl);
originalUrl.pathname = originalUrl.pathname.substr(0, originalUrl.pathname.length - 1);
var target = url.format(originalUrl);
res.statusCode = 303;
res.setHeader('Location', target);
return res.end('Redirecting to ' + target);
}
if (files) {
for (var i = files.length - 1; i >= 0; i--) {
files[i] = formatOutData(req, files[i]);
}
}
for (var i = files.length - 1; i >= 0; i--) {
files[i] = formatOutData(req, files[i]);
}
res.json(files);
sendCode(200, req, res, next, files)(err);
};
if (isRecursive === "true") {
return fileDriver.listAll(dirPath, false, handList);
return fileDriver.listAll(dirPath, handList);
} else {

@@ -104,4 +95,4 @@ return fileDriver.list(dirPath, handList);

return:
content of specified file
return: data of file
res.body = {"content of specified file"}
*/

@@ -112,21 +103,14 @@ var getFile = function (req, res, next) {

fileDriver.readFile(filePath, encoding, function(err, data) {
if (err) {
if (err && err.code === 'EISDIR') {
// this this is a dir, redirect to dir path
if (err.code === 'EISDIR') {
var originalUrl = url.parse(req.originalUrl);
originalUrl.pathname += '/';
var target = url.format(originalUrl);
res.statusCode = 303;
res.setHeader('Location', target);
res.end('Redirecting to ' + target);
return;
} else if (err.code === 'ENOENT') {
return res.status(404).end();
} else {
return next(err);
}
var originalUrl = url.parse(req.originalUrl);
originalUrl.pathname += '/';
var target = url.format(originalUrl);
res.statusCode = 303;
res.setHeader('Location', target);
return res.end('Redirecting to ' + target);
}
res.set('content-type', mime.lookup(filePath));
res.status(200).send(data);
sendCode(200, req, res, next, data)(err);
});

@@ -149,9 +133,10 @@ };

returns: modified resource
{
"name" : "file1", // name of dir or file
"path" : "/path/to/file", // path to dir or file
"dir" : false // true if directory
res.body = {
"fullFilePath" or dir
}
*/
var postFileOrDir = function (req, res, next) {
var dirPath = decodeURI(url.parse(req.url).pathname);
var isDir = dirPath.substr(-1) == '/';
var options = {};
var isJson = false;

@@ -161,6 +146,2 @@ if (typeof req.headers['content-type'] === 'string') {

}
var dirPath = decodeURI(url.parse(req.url).pathname);
var isDir = dirPath.substr(-1) == '/';
var options = {};
// move/rename if newPath exists

@@ -170,5 +151,4 @@ if (req.body.newPath) {

options.mkdirp = req.body.mkdirp || false;
fileDriver.move(dirPath, req.body.newPath, options,
return fileDriver.move(dirPath, req.body.newPath, options,
sendCode(200, req, res, next, formatOutData(req, req.body.newPath)));
return;
}

@@ -178,15 +158,21 @@

var mode = req.body.mode || 511;
fileDriver.mkdir(dirPath, mode,
return fileDriver.mkdir(dirPath, mode,
sendCode(201, req, res, next, formatOutData(req, dirPath)));
} else {
options.encoding = req.body.encoding || 'utf8';
options.mode = req.body.mode || 438;
}
if (!isJson) {
return fileDriver.writeFileStream(dirPath, req);
}
var data = req.body.content || '';
fileDriver.writeFile(dirPath, data, options,
if (!isJson) {
// default is to not clobber
options.encoding = req.query.encoding || 'utf8';
options.mode = req.query.mode || 438;
options.flags = req.query.clobber === 'true' ? 'w' : 'wx';
return fileDriver.writeFileStream(dirPath, req, options,
sendCode(201, req, res, next, formatOutData(req, dirPath)));
}
options.encoding = req.body.encoding || 'utf8';
options.mode = req.body.mode || 438;
var data = req.body.content || '';
fileDriver.writeFile(dirPath, data, options,
sendCode(201, req, res, next, formatOutData(req, dirPath)));
};

@@ -203,6 +189,4 @@

returns: modified resource
{
"name" : "file1", // name of dir or file
"path" : "/path/to/file", // path to dir or file
"dir" : false // true if directory
res.body = {
"fullFilePath"
}

@@ -235,3 +219,3 @@ */

return:
{}
res.body = {}
*/

@@ -241,5 +225,3 @@ var delDir = function (req, res, next) {

var clobber = req.body.clobber || false;
fileDriver.rmdir(dirPath, clobber,
sendCode(200, req, res, next, formatOutData(req, dirPath))
);
fileDriver.rmdir(dirPath, clobber, sendCode(200, req, res, next, {}));
};

@@ -252,9 +234,7 @@

return:
{}
res.body = {}
*/
var delFile = function (req, res, next) {
var dirPath = decodeURI(url.parse(req.url).pathname);
fileDriver.unlink(dirPath,
sendCode(200, req, res, next, formatOutData(req, dirPath))
);
fileDriver.unlink(dirPath, sendCode(200, req, res, next, {}));
};

@@ -276,2 +256,3 @@

if (err) {
error('ERROR', req.url, err);
code = 500;

@@ -284,11 +265,11 @@ out = err;

code = 403;
} if (err.code === 'ENOTDIR' || err.code === 'EISDIR') {
} if (err.code === 'ENOTDIR' ||
err.code === 'EISDIR') {
code = 400;
} if (err.code === 'ENOTEMPTY' ||
err.code === 'EEXIST' ||
err.code === 'EINVAL') {
err.code === 'EEXIST' ||
err.code === 'EINVAL') {
code = 409;
}
}
res.status(code).send(out);

@@ -295,0 +276,0 @@ };

@@ -10,3 +10,3 @@ // fsDriver

// returns array of files and dir. trailing slash determines type.
var listAll = function(reqDir, onlyDir, cb) {
var listAll = function(reqDir, cb) {
var finder = findit(reqDir);

@@ -19,7 +19,5 @@ var files = [];

if (!onlyDir) {
finder.on('file', function (file, stat) {
files.push(file);
});
}
finder.on('file', function (file, stat) {
files.push(file);
});

@@ -36,5 +34,4 @@ finder.on('end', function () {

fs.readdir(reqDir, function (err, files) {
if (err) {
return cb(err);
}
if (err) { return cb(err); }
if (files.length === 0) {

@@ -45,6 +42,9 @@ return cb(null, []);

return function (err, stat) {
if (err) {
return cb(err);
// here we do something special. if stat failes we know that there is something here
// but we might not have permissons. show it as a file.
var isDir = false;
if (!err) {
isDir = stat.isDirectory() ? '/' : '';
}
var file = path.join(reqDir, files[index], stat.isDirectory() ? '/' : '');
var file = path.join(reqDir, files[index], isDir);
filesList.push(file);

@@ -98,9 +98,8 @@ cnt++;

var writeFileStream = function(filepath, stream, options, cb) {
try {
var file = fs.createWriteStream(filepath, options);
stream.pipe(file);
var file = fs.createWriteStream(filepath, options);
file.on('error', cb);
file.on('finish', function() {
cb();
} catch(err) {
cb(err);
}
});
stream.pipe(file);
};

@@ -127,9 +126,7 @@

// workaround for ncp for dirs. should error if we trying to mv into own dir
fs.stat(oldPath, function(err, stats) {
if (err) {
return cb(err);
}
else if (stats.isDirectory() &&
if (err) { return cb(err); }
if (stats.isDirectory() &&
~newPath.indexOf(oldPath) &&

@@ -140,11 +137,12 @@ newPath.split("/").length > oldPath.split("/").length) {

return cb(err);
} else if (opts.clobber) {
}
if (opts.clobber) {
// also work around bug for clobber in dir
rm(newPath, function(err) {
return rm(newPath, function(err) {
if (err) { return cb(err); }
return mv(oldPath, newPath, opts, cb);
mv(oldPath, newPath, opts, cb);
});
} else {
return mv(oldPath, newPath, opts, cb);
}
return mv(oldPath, newPath, opts, cb);
});

@@ -151,0 +149,0 @@ };

{
"name": "rest-fs",
"version": "0.1.7",
"version": "0.1.8",
"description": "restful fileserver",

@@ -36,3 +36,5 @@ "main": "fileserver.js",

"dat-middleware": "^1.8.1",
"middleware-flow": "^0.5.1"
"middleware-flow": "^0.5.1",
"morgan": "^1.2.3",
"debug": "^1.0.4"
},

@@ -39,0 +41,0 @@ "devDependencies": {

@@ -24,2 +24,8 @@ rest-fs

`LOG=true`
for access log
`DEBUG=*`
for debug info
```

@@ -46,12 +52,5 @@ app = require('express')();

returns:
returns: list of full file or folder paths (trailing slash tells if dir)
```
[
{
"name" : "file1", // name of dir or file
"path" : "/path/to/file", // path to dir or file
"dir" : false // true if directory
},
...
]
res.body = [ { "fullDirPath" }, ... ]
```

@@ -68,3 +67,3 @@

returns:
content of specified file
res.body = { "file content" }

@@ -85,9 +84,10 @@

returns: modified resource
*optional for stream*<br>
query.clobber = overwrite if exist
query.mode = permissions of file (defaults: file 438(0666) dir 511(0777))<br>
query.encoding = default utf8
returns: modified resource. (trailing slash tells if dir)
```
{
"name" : "file1", // name of dir or file
"path" : "/path/to/file", // path to dir or file
"dir" : false // true if directory
}
req.body = { "fullFileOrDirPath" }
```

@@ -103,9 +103,5 @@

returns: modified resource
returns: modified resource (trailing slash tells if dir)
```
{
"name" : "file1", // name of dir or file
"path" : "/path/to/file", // path to dir or file
"dir" : false // true if directory
}
req.body = { "fullFilePath" }
```

@@ -120,3 +116,3 @@

```
{}
req.body = {}
```

@@ -131,3 +127,3 @@

```
{}
req.body = {}
```

@@ -88,3 +88,4 @@ var Lab = require('lab');

return cb(err);
} else if (200 !== res.statusCode) {
}
if (200 !== res.statusCode) {
return cb(res.body);

@@ -178,5 +179,5 @@ }

}, function (err, data) {
if (err) {
return done(err);
} else if (!~data.indexOf(testText)) {
if (err) { return done(err); }
if (!~data.indexOf(testText)) {
return done(new Error('incorrect data'));

@@ -197,5 +198,5 @@ } else {

}, function (err, data) {
if (err) {
return done(err);
} else if (!~data.indexOf(testText)) {
if (err) { return done(err); }
if (!~data.indexOf(testText)) {
return done(new Error('incorrect data'));

@@ -274,5 +275,4 @@ } else {

createFile(filepath, function(err) {
if (err) {
return done(err);
}
if (err) { return done(err); }
rmFile(filepath, done);

@@ -285,5 +285,4 @@ });

createFile(filepath, function(err) {
if (err) {
return done(err);
}
if (err) { return done(err); }
rmFile(filepath + '/', function (err, res) {

@@ -299,5 +298,4 @@ if (err) { return done(err); }

rmFile(baseFolder+'/fake.txt', function (err, res) {
if (err) {
return done(err);
}
if (err) { return done(err); }
Lab.expect(res.statusCode).to.equal(404);

@@ -310,5 +308,4 @@ return done();

rmFile(baseFolder, function (err, res) {
if (err) {
return done(err);
}
if (err) { return done(err); }
if (res.statusCode === 400 || res.statusCode === 403){

@@ -350,5 +347,5 @@ return done();

.end(function(err, res){
if (err) {
return done(err);
} else if (!~fileContent.indexOf(res.text)) {
if (err) { return done(err); }
if (!~fileContent.indexOf(res.text)) {
return done(new Error('file read wrong data'));

@@ -366,5 +363,5 @@ }

.end(function(err, res){
if (err) {
return done(err);
} else if (!~fileContent.indexOf(res.text)) {
if (err) { return done(err); }
if (!~fileContent.indexOf(res.text)) {
return done(new Error('file read wrong data'));

@@ -381,5 +378,5 @@ }

.end(function(err, res){
if (err) {
return done(err);
} else if (res.text !== "") {
if (err) { return done(err); }
if (res.text !== "") {
return done(new Error('file should be empty'));

@@ -396,5 +393,5 @@ }

.end(function(err, res){
if (err) {
return done(err);
} else if (!~res.text.indexOf('Redirecting to '+file2path)) {
if (err) { return done(err); }
if (!~res.text.indexOf('Redirecting to '+file2path)) {
return done(new Error('not redirecting'));

@@ -411,5 +408,5 @@ }

.end(function(err, res){
if (err) {
return done(err);
} else if (!~res.text.indexOf('Redirecting to '+file1path)) {
if (err) { return done(err); }
if (!~res.text.indexOf('Redirecting to '+file1path)) {
return done(new Error('not redirecting'));

@@ -426,5 +423,3 @@ }

.end(function(err, res){
if (err) {
return done(err);
}
if (err) { return done(err); }
return done();

@@ -524,2 +519,14 @@ });

lab.experiment('stream tests', function () {
var app;
lab.beforeEach(cleanBase);
lab.beforeEach(function(done){
app = server.listen(testPort,done);
});
lab.afterEach(function(done){
app.close(done);
});
lab.after(function (done) {
rimraf(baseFolder, done);
});
lab.test('POST - stream file', function (done) {

@@ -531,20 +538,20 @@ var dataFile = baseFolder+'/data.txt';

fs.writeFileSync(dataFile, testText);
var app = server.listen(testPort,
function() {
fs.createReadStream(dataFile)
.pipe(request.post('http://localhost:'+testPort+testFile))
.on('end', function(err, res) {
app.close(function() {
if (err) {
return done(err);
}
var data = fs.readFileSync(testFile);
Lab.expect(data.toString()).to.equal(testText);
done();
});
});
var r = request({
url: 'http://localhost:'+testPort+testFile,
method: 'POST',
pool: false
}, function(err, res) {
if (err) { return done(err); }
Lab.expect(res.statusCode).to.equal(201);
Lab.expect(res.body).to.equal(testFile);
var data = fs.readFileSync(testFile);
Lab.expect(data.toString()).to.equal(testText);
done();
});
fs.createReadStream(dataFile).pipe(r);
});
lab.test('POST - stream existing file', function (done) {
lab.test('POST - stream existing file with clobber', function (done) {
var dataFile = baseFolder+'/data.txt';

@@ -556,18 +563,182 @@ var testText = 'lots of text';

fs.writeFileSync(testFile, testText);
var app = server.listen(testPort,
function() {
fs.createReadStream(dataFile)
.pipe(request.post('http://localhost:'+testPort+testFile))
.on('end', function(err, res) {
app.close(function() {
if (err) {
return done(err);
}
var data = fs.readFileSync(testFile);
Lab.expect(data.toString()).to.equal(testText);
done();
});
});
var r = request({
url: 'http://localhost:'+testPort+testFile,
qs: {
clobber: true
},
method: 'POST',
pool: false
}, function(err, res) {
if (err) { return done(err); }
Lab.expect(res.statusCode).to.equal(201);
Lab.expect(res.body).to.equal(testFile);
var data = fs.readFileSync(testFile);
Lab.expect(data.toString()).to.equal(testText);
return done();
});
fs.createReadStream(dataFile).pipe(r);
});
lab.test('POST - stream file with clobber, mode, and encoding', function (done) {
var dataFile = baseFolder+'/data.txt';
var testText = 'lots of text';
var testFile = baseFolder+'/stream_test.txt';
fs.writeFileSync(dataFile, testText);
var r = request({
url: 'http://localhost:'+testPort+testFile,
qs: {
clobber: true,
mode: '0666',
encoding: 'utf8'
},
method: 'POST',
pool: false
}, function(err, res) {
if (err) { return done(err); }
Lab.expect(res.statusCode).to.equal(201);
Lab.expect(res.body).to.equal(testFile);
var data = fs.readFileSync(testFile);
Lab.expect(data.toString()).to.equal(testText);
return done();
});
fs.createReadStream(dataFile).pipe(r);
});
lab.test('POST - stream existing file with out clobber', function (done) {
var dataFile = baseFolder+'/data.txt';
var testText = 'lots of text';
var testFile = baseFolder+'/stream_test.txt';
fs.writeFileSync(dataFile, testText);
fs.writeFileSync(testFile, testText);
var data = fs.readFileSync(testFile);
Lab.expect(data.toString()).to.equal(testText);
var r = request({
url: 'http://localhost:'+testPort+testFile,
method: 'POST',
pool: false
}, function(err, res) {
if (err) { return done(err); }
Lab.expect(res.statusCode).to.equal(409);
return done();
});
fs.createReadStream(dataFile).pipe(r);
});
lab.test('POST - stream over folder', function (done) {
var dataFile = baseFolder+'/data.txt';
var testText = 'lots of text';
var testFile = baseFolder+'/stream_test';
fs.writeFileSync(dataFile, testText);
fs.mkdirSync(testFile);
var r = request({
url: 'http://localhost:'+testPort+testFile,
method: 'POST',
pool: false
}, function(err, res) {
if (err) { return done(err); }
Lab.expect(res.statusCode).to.equal(409);
done();
});
fs.createReadStream(dataFile).pipe(r);
});
lab.test('POST - stream over folder with clobber', function (done) {
var dataFile = baseFolder+'/data.txt';
var testText = 'lots of text';
var testFile = baseFolder+'/stream_test';
fs.writeFileSync(dataFile, testText);
fs.mkdirSync(testFile);
var r = request({
url: 'http://localhost:'+testPort+testFile,
qs: {
clobber: true
},
method: 'POST',
pool: false
}, function(err, res) {
if (err) { return done(err); }
Lab.expect(res.statusCode).to.equal(400);
done();
});
fs.createReadStream(dataFile).pipe(r);
});
lab.test('POST - stream to path which does not exist', function (done) {
var dataFile = baseFolder+'/data.txt';
var testText = 'lots of text';
var testFile = baseFolder+'/folder/newfile.txt';
fs.writeFileSync(dataFile, testText);
var r = request({
url: 'http://localhost:'+testPort+testFile,
method: 'POST',
pool: false
}, function(err, res) {
if (err) { return done(err); }
Lab.expect(res.statusCode).to.equal(404);
done();
});
fs.createReadStream(dataFile).pipe(r);
});
lab.test('POST - stream to path which does not exist with clobber', function (done) {
var dataFile = baseFolder+'/data.txt';
var testText = 'lots of text';
var testFile = baseFolder+'/folder/newfile.txt';
fs.writeFileSync(dataFile, testText);
var r = request({
url: 'http://localhost:'+testPort+testFile,
qs: {
clobber: true
},
method: 'POST',
pool: false
}, function(err, res) {
if (err) { return done(err); }
Lab.expect(res.statusCode).to.equal(404);
done();
});
fs.createReadStream(dataFile).pipe(r);
});
lab.test('POST - stream to path with different headers', function (done) {
var dataFile = baseFolder+'/data.txt';
var testText = 'lots of text';
var testFile = baseFolder+'/folder/newfile.txt';
fs.writeFileSync(dataFile, testText);
var r = request({
url: 'http://localhost:'+testPort+testFile,
method: 'POST',
pool: false,
headers: {
'content-type': 'application/json'
}
}, function(err, res) {
if (err) { return done(err); }
Lab.expect(res.statusCode).to.equal(500);
done();
});
fs.createReadStream(dataFile).pipe(r);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc