Merge pull request #164 from strongloop/Add_Eslint

Add eslint
This commit is contained in:
Amirali Jafarian 2016-10-19 11:57:30 -04:00 committed by GitHub
commit fed898b1c5
15 changed files with 274 additions and 247 deletions

0
.eslintignore Normal file
View File

14
.eslintrc Normal file
View File

@ -0,0 +1,14 @@
{
"extends": "loopback",
"rules": {
"max-len": ["error", 90, 4, {
"ignoreComments": true,
"ignoreUrls": true,
"ignorePattern": "^\\s*var\\s.+=\\s*(require\\s*\\()|(/)"
}],
// NOTE we should eventually remove this override
// and fix all of those 100+ violations
"one-var": "off",
"no-unused-expressions": "off"
}
}

View File

@ -2,6 +2,7 @@
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
var SG = require('strong-globalize');
SG.SetRootDir(__dirname);

View File

@ -2,6 +2,7 @@
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
var pkgcloud = require('pkgcloud');
@ -13,9 +14,9 @@ function patchBaseClass(cls) {
var proto = cls.prototype;
var found = false;
// Find the prototype that owns the _setProperties method
while (proto
&& proto.constructor !== pkgcloud.storage.Container
&& proto.constructor !== pkgcloud.storage.File) {
while (proto &&
proto.constructor !== pkgcloud.storage.Container &&
proto.constructor !== pkgcloud.storage.File) {
if (proto.hasOwnProperty('_setProperties')) {
found = true;
break;
@ -33,7 +34,7 @@ function patchBaseClass(cls) {
// Pass in some context as non-enumerable properties
Object.defineProperties(receiver, {
client: {value: this.client},
files: {value: this.files}
files: {value: this.files},
});
m1.call(receiver, details);
// Apply the calculated properties to this
@ -43,7 +44,7 @@ function patchBaseClass(cls) {
// Keep references to raw and the calculated properties
this._rawMetadata = details;
this._metadata = receiver; // Use _metadata to avoid conflicts
}
};
proto.toJSON = function() {
return this._metadata;
@ -56,7 +57,6 @@ function patchBaseClass(cls) {
proto.getRawMetadata = function() {
return this._rawMetadata;
};
}
/*!
* Patch the pkgcloud Container/File classes so that the metadata are separately

View File

@ -2,6 +2,7 @@
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
var base = require('pkgcloud').storage;
var util = require('util');

View File

@ -2,6 +2,7 @@
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
var base = require('pkgcloud').storage;
var util = require('util');

View File

@ -3,6 +3,10 @@
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
// Turning on strict for this file breaks;
// disabling strict for this file
/* eslint-disable strict */
// Globalization
var g = require('strong-globalize')();
@ -172,7 +176,7 @@ FileSystemProvider.prototype.upload = function (options, cb) {
var fileOpts = {flags: options.flags || 'w+',
encoding: options.encoding || null,
mode: options.mode || 0666
mode: options.mode || 0666,
};
try {
@ -201,8 +205,8 @@ FileSystemProvider.prototype.download = function (options, cb) {
autoClose: true};
if (options.start) {
fileOpts.start = options.start
fileOpts.end = options.end
fileOpts.start = options.start;
fileOpts.end = options.end;
}
try {

View File

@ -2,6 +2,7 @@
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
var StorageService = require('./storage-service');
/**

View File

@ -2,6 +2,7 @@
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
// Globalization
var g = require('strong-globalize')();
@ -11,7 +12,7 @@ var StringDecoder = require('string_decoder').StringDecoder;
var path = require('path');
var defaultOptions = {
maxFileSize: 10 * 1024 * 1024 // 10 MB
maxFileSize: 10 * 1024 * 1024, // 10 MB
};
/**
@ -35,18 +36,22 @@ exports.upload = function(provider, req, res, options, cb) {
var form = new IncomingForm(options);
var container = options.container || req.params.container;
var fields = {}, files = {};
var fields = {};
var files = {};
form.handlePart = function(part) {
var self = this;
if (part.filename === undefined) {
var value = ''
, decoder = new StringDecoder(this.encoding);
var value = '';
var decoder = new StringDecoder(this.encoding);
part.on('data', function(buffer) {
self._fieldsSize += buffer.length;
if (self._fieldsSize > self.maxFieldsSize) {
self._error(new Error(g.f('{{maxFieldsSize}} exceeded, received %s bytes of field data', self._fieldsSize)));
self._error(new Error(
g.f('{{maxFieldsSize}} exceeded, received %s bytes of field data',
self._fieldsSize
)));
return;
}
value += decoder.write(buffer);
@ -70,7 +75,7 @@ exports.upload = function(provider, req, res, options, cb) {
var file = {
container: container,
name: part.filename,
type: part.mime
type: part.mime,
};
// Options for this file
@ -91,7 +96,11 @@ exports.upload = function(provider, req, res, options, cb) {
}
if (Array.isArray(allowedContentTypes) && allowedContentTypes.length !== 0) {
if (allowedContentTypes.indexOf(file.type) === -1) {
self._error(new Error(g.f('{{contentType}} "%s" is not allowed (Must be in [%s])', file.type, allowedContentTypes.join(', '))));
self._error(new Error(
g.f('{{contentType}} "%s" is not allowed (Must be in [%s])',
file.type,
allowedContentTypes.join(', ')
)));
return;
}
}
@ -121,7 +130,7 @@ exports.upload = function(provider, req, res, options, cb) {
var uploadParams = {
container: container,
remote: file.name,
contentType: file.type
contentType: file.type,
};
if (file.acl) {
uploadParams.acl = file.acl;
@ -161,13 +170,17 @@ exports.upload = function(provider, req, res, options, cb) {
// We are missing some way to tell the provider to cancel upload/multipart upload of the current file.
// - s3-upload-stream doesn't provide a way to do this in it's public interface
// - We could call provider.delete file but it would not delete multipart data
self._error(new Error(g.f('{{maxFileSize}} exceeded, received %s bytes of field data (max is %s)', fileSize, maxFileSize)));
self._error(new Error(
g.f('{{maxFileSize}} exceeded, received %s bytes of field data (max is %s)',
fileSize,
maxFileSize
)));
return;
}
});
}
part.on("end", function() {
part.on('end', function() {
writer.end();
});
part.pipe(writer, {end: false});
@ -177,6 +190,7 @@ exports.upload = function(provider, req, res, options, cb) {
if (err) {
console.error(err);
}
// eslint-disable-next-line no-unused-expressions
cb && cb(err, {files: files, fields: fields});
});
};
@ -191,7 +205,6 @@ function handleError(res, err) {
res.status(500).send({error: err});
}
/**
* Handle download from a container/file.
* @param {Object} provider The storage service provider
@ -203,42 +216,39 @@ function handleError(res, err) {
* @header storageService.download(provider, req, res, container, file, cb)
*/
exports.download = function(provider, req, res, container, file, cb) {
var fileName = path.basename(file);
var params = {
container: container || req && req.params.container,
remote: file || req && req.params.file
remote: file || req && req.params.file,
};
var range = null;
if (req) {
if (req.headers) {
range = req.headers.range || '';
}
if (range) {
provider.getFile(params.container, params.remote, function(err, stats) {
if (err) {
handleError(res, err);
} else {
var total = stats.size;
var parts = range.replace(/bytes=/, "").split("-")
var partialstart = parts[0]
var partialend = parts[1]
var parts = range.replace(/bytes=/, '').split('-');
var partialstart = parts[0];
var partialend = parts[1];
params.start = parseInt(partialstart, 10)
params.end = partialend ? parseInt(partialend, 10) : total - 1
params.start = parseInt(partialstart, 10);
params.end = partialend ? parseInt(partialend, 10) : total - 1;
var chunksize = (params.end - params.start) + 1
var chunksize = (params.end - params.start) + 1;
res.status(206)
res.set("Content-Range", "bytes " + params.start + "-" + params.end + "/" + total);
res.set("Accept-Ranges", "bytes");
res.set("Content-Length", chunksize);
res.status(206);
res.set('Content-Range', 'bytes ' + params.start + '-' + params.end + '/' + total);
res.set('Accept-Ranges', 'bytes');
res.set('Content-Length', chunksize);
var reader = provider.download(params);
@ -253,9 +263,7 @@ exports.download = function(provider, req, res, container, file, cb) {
});
}
});
} else {
var reader = provider.download(params);
res.type(fileName);
@ -271,6 +279,3 @@ exports.download = function(provider, req, res, container, file, cb) {
}
};

View File

@ -2,6 +2,7 @@
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
var factory = require('./factory');
var handler = require('./storage-handler');
@ -45,7 +46,6 @@ function StorageService(options) {
if (options.maxFileSize) {
this.maxFileSize = options.maxFileSize;
}
}
function map(obj) {
@ -260,29 +260,39 @@ StorageService.modelName = 'storage';
StorageService.prototype.getContainers.shared = true;
StorageService.prototype.getContainers.accepts = [];
StorageService.prototype.getContainers.returns = {arg: 'containers', type: 'array', root: true};
StorageService.prototype.getContainers.returns = {
arg: 'containers',
type: 'array',
root: true,
};
StorageService.prototype.getContainers.http =
{verb: 'get', path: '/'};
StorageService.prototype.getContainer.shared = true;
StorageService.prototype.getContainer.accepts = [
{arg: 'container', type: 'string'}
{arg: 'container', type: 'string'},
];
StorageService.prototype.getContainer.returns = {arg: 'container', type: 'object', root: true};
StorageService.prototype.getContainer.returns = {
arg: 'container',
type: 'object', root: true,
};
StorageService.prototype.getContainer.http =
{verb: 'get', path: '/:container'};
StorageService.prototype.createContainer.shared = true;
StorageService.prototype.createContainer.accepts = [
{arg: 'options', type: 'object', http: {source: 'body'}}
{arg: 'options', type: 'object', http: {source: 'body'}},
];
StorageService.prototype.createContainer.returns = {arg: 'container', type: 'object', root: true};
StorageService.prototype.createContainer.returns = {
arg: 'container',
type: 'object', root: true,
};
StorageService.prototype.createContainer.http =
{verb: 'post', path: '/'};
StorageService.prototype.destroyContainer.shared = true;
StorageService.prototype.destroyContainer.accepts = [
{arg: 'container', type: 'string'}
{arg: 'container', type: 'string'},
];
StorageService.prototype.destroyContainer.returns = {};
StorageService.prototype.destroyContainer.http =
@ -290,7 +300,7 @@ StorageService.prototype.destroyContainer.http =
StorageService.prototype.getFiles.shared = true;
StorageService.prototype.getFiles.accepts = [
{arg: 'container', type: 'string'}
{arg: 'container', type: 'string'},
];
StorageService.prototype.getFiles.returns = {arg: 'files', type: 'array', root: true};
StorageService.prototype.getFiles.http =
@ -299,7 +309,7 @@ StorageService.prototype.getFiles.http =
StorageService.prototype.getFile.shared = true;
StorageService.prototype.getFile.accepts = [
{arg: 'container', type: 'string'},
{arg: 'file', type: 'string'}
{arg: 'file', type: 'string'},
];
StorageService.prototype.getFile.returns = {arg: 'file', type: 'object', root: true};
StorageService.prototype.getFile.http =
@ -308,7 +318,7 @@ StorageService.prototype.getFile.http =
StorageService.prototype.removeFile.shared = true;
StorageService.prototype.removeFile.accepts = [
{arg: 'container', type: 'string'},
{arg: 'file', type: 'string'}
{arg: 'file', type: 'string'},
];
StorageService.prototype.removeFile.returns = {};
StorageService.prototype.removeFile.http =
@ -317,7 +327,7 @@ StorageService.prototype.removeFile.http =
StorageService.prototype.upload.shared = true;
StorageService.prototype.upload.accepts = [
{arg: 'req', type: 'object', 'http': {source: 'req'}},
{arg: 'res', type: 'object', 'http': {source: 'res'}}
{arg: 'res', type: 'object', 'http': {source: 'res'}},
];
StorageService.prototype.upload.returns = {arg: 'result', type: 'object'};
StorageService.prototype.upload.http =
@ -328,7 +338,7 @@ StorageService.prototype.download.accepts = [
{arg: 'container', type: 'string', 'http': {source: 'path'}},
{arg: 'file', type: 'string', 'http': {source: 'path'}},
{arg: 'req', type: 'object', 'http': {source: 'req'}},
{arg: 'res', type: 'object', 'http': {source: 'res'}}
{arg: 'res', type: 'object', 'http': {source: 'res'}},
];
StorageService.prototype.download.http =
{verb: 'get', path: '/:container/download/:file'};

View File

@ -4,7 +4,9 @@
"version": "1.9.2",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha --timeout 30000 test/*test.js"
"lint": "eslint .",
"test": "./node_modules/.bin/mocha --timeout 30000 test/*test.js",
"posttest": "npm run lint"
},
"dependencies": {
"async": "^0.9.0",
@ -13,11 +15,13 @@
"strong-globalize": "^2.6.2"
},
"devDependencies": {
"eslint": "^2.13.1",
"eslint-config-loopback": "^4.0.0",
"express": "^4.11.0",
"loopback": "^2.10.0",
"mkdirp": "^0.5.0",
"mocha": "^2.1.0",
"supertest": "^0.15.0",
"mkdirp": "^0.5.0"
"supertest": "^0.15.0"
},
"repository": {
"type": "git",

View File

@ -2,9 +2,10 @@
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
var loopback = require('loopback')
, app = module.exports = loopback();
var loopback = require('loopback');
var app = module.exports = loopback();
var path = require('path');
@ -18,7 +19,7 @@ app.set('port', process.env.PORT || 3000);
var ds = loopback.createDataSource({
connector: require('../index'),
provider: 'filesystem',
root: path.join(__dirname, 'storage')
root: path.join(__dirname, 'storage'),
});
var container = ds.createModel('container');

View File

@ -2,6 +2,7 @@
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
var FileSystemProvider = require('../lib/providers/filesystem/index.js').Client;
@ -20,7 +21,6 @@ function verifyMetadata(fileOrContainer, name) {
}
describe('FileSystem based storage provider', function() {
describe('container apis', function() {
var client = null;
it('should require an existing directory as the root', function(done) {
@ -113,7 +113,7 @@ describe('FileSystem based storage provider', function () {
it('should download a file', function(done) {
var reader = client.download({
container: 'c1',
remote: 'f1.txt'
remote: 'f1.txt',
});
reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')));
reader.on('end', done);
@ -159,8 +159,6 @@ describe('FileSystem based storage provider', function () {
done(err, container);
});
});
});
});

View File

@ -2,18 +2,20 @@
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
var StorageService = require('../lib/storage-service.js');
var assert = require('assert');
var path = require('path');
var storageService = new StorageService({root: path.join(__dirname, 'storage'), provider: 'filesystem'});
var storageService = new StorageService({
root: path.join(__dirname, 'storage'),
provider: 'filesystem',
});
describe('Storage service', function() {
describe('container apis', function() {
it('should return an empty list of containers', function(done) {
storageService.getContainers(function(err, containers) {
assert(!err);
@ -138,8 +140,6 @@ describe('Storage service', function () {
done(err, container);
});
});
});
});

View File

@ -2,6 +2,7 @@
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
var request = require('supertest');
var loopback = require('loopback');
@ -23,7 +24,7 @@ var dsImage = loopback.createDataSource({
},
acl: 'public-read',
allowedContentTypes: ['image/png', 'image/jpeg'],
maxFileSize: 5 * 1024 * 1024
maxFileSize: 5 * 1024 * 1024,
});
var ImageContainer = dsImage.createModel('imageContainer');
@ -32,7 +33,7 @@ app.model(ImageContainer);
var ds = loopback.createDataSource({
connector: require('../lib/storage-connector'),
provider: 'filesystem',
root: path.join(__dirname, 'images')
root: path.join(__dirname, 'images'),
});
var Container = ds.createModel('container', {}, {base: 'Model'});
@ -79,7 +80,6 @@ describe('storage service', function () {
});
it('should create a container', function(done) {
request('http://localhost:' + app.get('port'))
.post('/containers')
.send({name: 'test-container'})
@ -93,7 +93,6 @@ describe('storage service', function () {
});
it('should get a container', function(done) {
request('http://localhost:' + app.get('port'))
.get('/containers/test-container')
.set('Accept', 'application/json')
@ -105,7 +104,6 @@ describe('storage service', function () {
});
it('should list containers', function(done) {
request('http://localhost:' + app.get('port'))
.get('/containers')
.set('Accept', 'application/json')
@ -121,7 +119,6 @@ describe('storage service', function () {
});
it('should delete a container', function(done) {
request('http://localhost:' + app.get('port'))
.del('/containers/test-container')
.set('Accept', 'application/json')
@ -132,7 +129,6 @@ describe('storage service', function () {
});
it('should list containers after delete', function(done) {
request('http://localhost:' + app.get('port'))
.get('/containers')
.set('Accept', 'application/json')
@ -145,7 +141,6 @@ describe('storage service', function () {
});
it('should list files', function(done) {
request('http://localhost:' + app.get('port'))
.get('/containers/album1/files')
.set('Accept', 'application/json')
@ -160,32 +155,30 @@ describe('storage service', function () {
});
it('uploads files', function(done) {
request('http://localhost:' + app.get('port'))
.post('/containers/album1/upload')
.attach('image', path.join(__dirname, './fixtures/test.jpg'))
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, function(err, res) {
assert.deepEqual(res.body, {"result": {"files": {"image": [
{"container": "album1", "name": "test.jpg", "type": "image/jpeg",
"size": 60475}
]}, "fields": {}}});
assert.deepEqual(res.body, {'result': {'files': {'image': [
{'container': 'album1', 'name': 'test.jpg', 'type': 'image/jpeg',
'size': 60475},
]}, 'fields': {}}});
done();
});
});
it('uploads files with renamer', function(done) {
request('http://localhost:' + app.get('port'))
.post('/imageContainers/album1/upload')
.attach('image', path.join(__dirname, './fixtures/test.jpg'))
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, function(err, res) {
assert.deepEqual(res.body, {"result": {"files": {"image": [
{"container": "album1", "name": "image-test.jpg", "originalFilename":"test.jpg", "type": "image/jpeg", "acl":"public-read", "size": 60475}
]}, "fields": {}}});
assert.deepEqual(res.body, {'result': {'files': {'image': [
{'container': 'album1', 'name': 'image-test.jpg', 'originalFilename': 'test.jpg', 'type': 'image/jpeg', 'acl': 'public-read', 'size': 60475},
]}, 'fields': {}}});
done();
});
});
@ -204,7 +197,6 @@ describe('storage service', function () {
});
it('uploads file too large', function(done) {
request('http://localhost:' + app.get('port'))
.post('/imageContainers/album1/upload')
.attach('image', path.join(__dirname, './fixtures/largeImage.jpg'))
@ -219,7 +211,6 @@ describe('storage service', function () {
});
it('should get file by name', function(done) {
request('http://localhost:' + app.get('port'))
.get('/containers/album1/files/test.jpg')
.set('Accept', 'application/json')
@ -231,7 +222,6 @@ describe('storage service', function () {
});
it('should get file by renamed file name', function(done) {
request('http://localhost:' + app.get('port'))
.get('/imageContainers/album1/files/image-test.jpg')
.set('Accept', 'application/json')
@ -243,7 +233,6 @@ describe('storage service', function () {
});
it('downloads files', function(done) {
request('http://localhost:' + app.get('port'))
.get('/containers/album1/download/test.jpg')
.expect('Content-Type', 'image/jpeg')
@ -294,7 +283,6 @@ describe('storage service', function () {
});
it('should delete a file', function(done) {
request('http://localhost:' + app.get('port'))
.del('/containers/album1/files/test.jpg')
.set('Accept', 'application/json')
@ -305,7 +293,6 @@ describe('storage service', function () {
});
it('reports errors if it fails to find the file to download', function(done) {
request('http://localhost:' + app.get('port'))
.get('/containers/album1/download/test_not_exist.jpg')
.expect('Content-Type', /json/)