Skip to content

Commit 03b6a7b

Browse files
authored
Add airbnb style guide to linter (#72)
1 parent 2990242 commit 03b6a7b

File tree

6 files changed

+1109
-784
lines changed

6 files changed

+1109
-784
lines changed

.eslintrc.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"root": true,
3-
"extends": "eslint:recommended",
3+
"extends": "airbnb-base",
44
"env": {
55
"node": true,
66
"es6": true
77
},
88
"parserOptions": {
9-
"ecmaVersion": 6,
9+
"ecmaVersion": 2018,
1010
"sourceType": "module"
1111
},
1212
"rules": {
@@ -15,6 +15,11 @@
1515
"no-trailing-spaces": 2,
1616
"eol-last": 2,
1717
"space-in-parens": ["error", "never"],
18-
"no-multiple-empty-lines": 1
18+
"no-multiple-empty-lines": 1,
19+
"no-underscore-dangle": 0,
20+
"no-new": 0,
21+
"func-names": 0,
22+
"prefer-destructuring": 0,
23+
"prefer-object-spread": 0
1924
}
2025
}

index.js

Lines changed: 78 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
'use strict';
21
// S3Adapter
32
//
43
// Stores Parse files in AWS S3.
54

6-
var AWS = require('aws-sdk');
7-
var optionsFromArguments = require('./lib/optionsFromArguments');
5+
const AWS = require('aws-sdk');
6+
const optionsFromArguments = require('./lib/optionsFromArguments');
87

98
const awsCredentialsDeprecationNotice = function awsCredentialsDeprecationNotice() {
109
// eslint-disable-next-line no-console
1110
console.warn('Passing AWS credentials to this adapter is now DEPRECATED and will be removed in a future version',
1211
'See: https://github.com/parse-server-modules/parse-server-s3-adapter#aws-credentials for details');
13-
}
12+
};
1413

1514
// Creates an S3 session.
1615
// Providing AWS access, secret keys and bucket are mandatory
1716
// Region will use sane defaults if omitted
18-
function S3Adapter() {
19-
var options = optionsFromArguments(arguments);
17+
function S3Adapter(...args) {
18+
const options = optionsFromArguments(args);
2019
this._region = options.region;
2120
this._bucket = options.bucket;
2221
this._bucketPrefix = options.bucketPrefix;
@@ -27,11 +26,11 @@ function S3Adapter() {
2726
this._globalCacheControl = options.globalCacheControl;
2827
this._encryption = options.ServerSideEncryption;
2928

30-
let s3Options = {
29+
const s3Options = {
3130
params: { Bucket: this._bucket },
3231
region: this._region,
3332
signatureVersion: this._signatureVersion,
34-
globalCacheControl: this._globalCacheControl
33+
globalCacheControl: this._globalCacheControl,
3534
};
3635

3736
if (options.accessKey && options.secretKey) {
@@ -46,8 +45,8 @@ function S3Adapter() {
4645
this._hasBucket = false;
4746
}
4847

49-
S3Adapter.prototype.createBucket = function() {
50-
var promise;
48+
S3Adapter.prototype.createBucket = function () {
49+
let promise;
5150
if (this._hasBucket) {
5251
promise = Promise.resolve();
5352
} else {
@@ -59,118 +58,110 @@ S3Adapter.prototype.createBucket = function() {
5958
});
6059
}
6160
return promise;
62-
}
61+
};
6362

6463
// For a given config object, filename, and data, store a file in S3
6564
// Returns a promise containing the S3 object creation response
66-
S3Adapter.prototype.createFile = function(filename, data, contentType) {
67-
let params = {
65+
S3Adapter.prototype.createFile = function (filename, data, contentType) {
66+
const params = {
6867
Key: this._bucketPrefix + filename,
69-
Body: data
68+
Body: data,
7069
};
7170
if (this._directAccess) {
72-
params.ACL = "public-read"
71+
params.ACL = 'public-read';
7372
}
7473
if (contentType) {
7574
params.ContentType = contentType;
7675
}
77-
if(this._globalCacheControl) {
76+
if (this._globalCacheControl) {
7877
params.CacheControl = this._globalCacheControl;
7978
}
80-
if(this._encryption == 'AES256' || this._encryption == 'aws:kms'){
79+
if (this._encryption === 'AES256' || this._encryption === 'aws:kms') {
8180
params.ServerSideEncryption = this._encryption;
8281
}
83-
return this.createBucket().then(() => {
84-
return new Promise((resolve, reject) => {
85-
this._s3Client.upload(params, (err, data) => {
86-
if (err !== null) {
87-
return reject(err);
88-
}
89-
resolve(data);
90-
});
82+
return this.createBucket().then(() => new Promise((resolve, reject) => {
83+
this._s3Client.upload(params, (err, response) => {
84+
if (err !== null) {
85+
return reject(err);
86+
}
87+
return resolve(response);
9188
});
92-
});
93-
}
89+
}));
90+
};
9491

95-
S3Adapter.prototype.deleteFile = function(filename) {
96-
return this.createBucket().then(() => {
97-
return new Promise((resolve, reject) => {
98-
let params = {
99-
Key: this._bucketPrefix + filename
100-
};
101-
this._s3Client.deleteObject(params, (err, data) =>{
102-
if(err !== null) {
103-
return reject(err);
104-
}
105-
resolve(data);
106-
});
92+
S3Adapter.prototype.deleteFile = function (filename) {
93+
return this.createBucket().then(() => new Promise((resolve, reject) => {
94+
const params = {
95+
Key: this._bucketPrefix + filename,
96+
};
97+
this._s3Client.deleteObject(params, (err, data) => {
98+
if (err !== null) {
99+
return reject(err);
100+
}
101+
return resolve(data);
107102
});
108-
});
109-
}
103+
}));
104+
};
110105

111106
// Search for and return a file if found by filename
112107
// Returns a promise that succeeds with the buffer result from S3
113-
S3Adapter.prototype.getFileData = function(filename) {
114-
let params = {Key: this._bucketPrefix + filename};
115-
return this.createBucket().then(() => {
116-
return new Promise((resolve, reject) => {
117-
this._s3Client.getObject(params, (err, data) => {
118-
if (err !== null) {
119-
return reject(err);
120-
}
121-
// Something happened here...
122-
if (data && !data.Body) {
123-
return reject(data);
124-
}
125-
resolve(data.Body);
126-
});
108+
S3Adapter.prototype.getFileData = function (filename) {
109+
const params = { Key: this._bucketPrefix + filename };
110+
return this.createBucket().then(() => new Promise((resolve, reject) => {
111+
this._s3Client.getObject(params, (err, data) => {
112+
if (err !== null) {
113+
return reject(err);
114+
}
115+
// Something happened here...
116+
if (data && !data.Body) {
117+
return reject(data);
118+
}
119+
return resolve(data.Body);
127120
});
128-
});
129-
}
121+
}));
122+
};
130123

131124
// Generates and returns the location of a file stored in S3 for the given request and filename
132-
// The location is the direct S3 link if the option is set, otherwise we serve the file through parse-server
133-
S3Adapter.prototype.getFileLocation = function(config, filename) {
134-
filename = encodeURIComponent(filename);
125+
// The location is the direct S3 link if the option is set,
126+
// otherwise we serve the file through parse-server
127+
S3Adapter.prototype.getFileLocation = function (config, filename) {
128+
const fileName = encodeURIComponent(filename);
135129
if (this._directAccess) {
136130
if (this._baseUrl && this._baseUrlDirect) {
137-
return `${this._baseUrl}/${filename}`;
138-
} else if (this._baseUrl) {
139-
return `${this._baseUrl}/${this._bucketPrefix + filename}`;
140-
} else {
141-
return `https://${this._bucket}.s3.amazonaws.com/${this._bucketPrefix + filename}`;
131+
return `${this._baseUrl}/${fileName}`;
132+
} if (this._baseUrl) {
133+
return `${this._baseUrl}/${this._bucketPrefix + fileName}`;
142134
}
135+
return `https://${this._bucket}.s3.amazonaws.com/${this._bucketPrefix + fileName}`;
143136
}
144-
return (config.mount + '/files/' + config.applicationId + '/' + filename);
145-
}
137+
return (`${config.mount}/files/${config.applicationId}/${fileName}`);
138+
};
146139

147140
S3Adapter.prototype.handleFileStream = function (filename, req, res) {
148141
const params = {
149142
Key: this._bucketPrefix + filename,
150143
Range: req.get('Range'),
151144
};
152-
return this.createBucket().then(() => {
153-
return new Promise((resolve, reject) => {
154-
this._s3Client.getObject(params, (error, data) => {
155-
if (error !== null) {
156-
return reject(error);
157-
}
158-
if (data && !data.Body) {
159-
return reject(data);
160-
}
161-
res.writeHead(206, {
162-
'Accept-Ranges': data.AcceptRanges,
163-
'Content-Length': data.ContentLength,
164-
'Content-Range': data.ContentRange,
165-
'Content-Type': data.ContentType,
166-
});
167-
res.write(data.Body);
168-
res.end();
169-
resolve(data.Body);
145+
return this.createBucket().then(() => new Promise((resolve, reject) => {
146+
this._s3Client.getObject(params, (error, data) => {
147+
if (error !== null) {
148+
return reject(error);
149+
}
150+
if (data && !data.Body) {
151+
return reject(data);
152+
}
153+
res.writeHead(206, {
154+
'Accept-Ranges': data.AcceptRanges,
155+
'Content-Length': data.ContentLength,
156+
'Content-Range': data.ContentRange,
157+
'Content-Type': data.ContentType,
170158
});
159+
res.write(data.Body);
160+
res.end();
161+
return resolve(data.Body);
171162
});
172-
});
173-
}
163+
}));
164+
};
174165

175166
module.exports = S3Adapter;
176167
module.exports.default = S3Adapter;

lib/optionsFromArguments.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
1-
'use strict';
21

32
const DEFAULT_S3_REGION = 'us-east-1';
43

54
function requiredOrFromEnvironment(options, key, env) {
6-
options[key] = options[key] || process.env[env];
7-
if (!options[key]) {
8-
throw `S3Adapter requires option '${key}' or env. variable ${env}`;
5+
const opts = Object.assign({}, options);
6+
opts[key] = options[key] || process.env[env];
7+
if (!opts[key]) {
8+
throw new Error(`S3Adapter requires option '${key}' or env. variable ${env}`);
99
}
10-
return options;
10+
return opts;
1111
}
1212

1313
function fromEnvironmentOrDefault(options, key, env, defaultValue) {
14-
options[key] = options[key] || process.env[env] || defaultValue;
14+
const opts = Object.assign({}, options);
15+
opts[key] = options[key] || process.env[env] || defaultValue;
1516
// If we used the overrides,
1617
// make sure they take priority
17-
if(options.s3overrides){
18-
if(options.s3overrides[key]){
19-
options[key] = options.s3overrides[key];
20-
}else if (options.s3overrides.params && options.s3overrides.params.Bucket) {
21-
options.bucket = options.s3overrides.params.Bucket;
18+
if (opts.s3overrides) {
19+
if (opts.s3overrides[key]) {
20+
opts[key] = opts.s3overrides[key];
21+
} else if (opts.s3overrides.params && opts.s3overrides.params.Bucket) {
22+
opts.bucket = opts.s3overrides.params.Bucket;
2223
}
2324
}
24-
return options;
25+
return opts;
2526
}
2627

2728
function fromOptionsDictionaryOrDefault(options, key, defaultValue) {
28-
options[key] = options[key] || defaultValue;
29-
return options;
29+
const opts = Object.assign({}, options);
30+
opts[key] = options[key] || defaultValue;
31+
return opts;
3032
}
3133

3234
const optionsFromArguments = function optionsFromArguments(args) {
@@ -35,17 +37,17 @@ const optionsFromArguments = function optionsFromArguments(args) {
3537
let s3overrides = {};
3638
let otherOptions;
3739

38-
if (typeof stringOrOptions == 'string') {
39-
if (args.length == 1) {
40+
if (typeof stringOrOptions === 'string') {
41+
if (args.length === 1) {
4042
options.bucket = stringOrOptions;
41-
} else if (args.length == 2) {
43+
} else if (args.length === 2) {
4244
options.bucket = stringOrOptions;
43-
if (typeof args[1] != 'object') {
45+
if (typeof args[1] !== 'object') {
4446
throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense');
4547
}
4648
otherOptions = args[1];
4749
} else if (args.length > 2) {
48-
if (typeof args[1] != 'string' || typeof args[2] != 'string') {
50+
if (typeof args[1] !== 'string' || typeof args[2] !== 'string') {
4951
throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense');
5052
}
5153
options.accessKey = args[0];
@@ -63,19 +65,17 @@ const optionsFromArguments = function optionsFromArguments(args) {
6365
options.globalCacheControl = otherOptions.globalCacheControl;
6466
options.ServerSideEncryption = otherOptions.ServerSideEncryption;
6567
}
66-
} else {
67-
if (args.length == 1) {
68-
Object.assign(options, stringOrOptions);
69-
} else if (args.length == 2) {
70-
Object.assign(options, stringOrOptions);
71-
s3overrides = args[1];
68+
} else if (args.length === 1) {
69+
Object.assign(options, stringOrOptions);
70+
} else if (args.length === 2) {
71+
Object.assign(options, stringOrOptions);
72+
s3overrides = args[1];
7273

73-
if (s3overrides.params) {
74-
options.bucket = s3overrides.params.Bucket;
75-
}
76-
} else if (args.length > 2) {
77-
throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense');
74+
if (s3overrides.params) {
75+
options.bucket = s3overrides.params.Bucket;
7876
}
77+
} else if (args.length > 2) {
78+
throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense');
7979
}
8080

8181
options = fromOptionsDictionaryOrDefault(options, 's3overrides', s3overrides);
@@ -88,10 +88,9 @@ const optionsFromArguments = function optionsFromArguments(args) {
8888
options = fromEnvironmentOrDefault(options, 'baseUrl', 'S3_BASE_URL', null);
8989
options = fromEnvironmentOrDefault(options, 'baseUrlDirect', 'S3_BASE_URL_DIRECT', false);
9090
options = fromEnvironmentOrDefault(options, 'signatureVersion', 'S3_SIGNATURE_VERSION', 'v4');
91-
options = fromEnvironmentOrDefault(
92-
options, 'globalCacheControl', 'S3_GLOBAL_CACHE_CONTROL', null);
91+
options = fromEnvironmentOrDefault(options, 'globalCacheControl', 'S3_GLOBAL_CACHE_CONTROL', null);
9392

9493
return options;
95-
}
94+
};
9695

9796
module.exports = optionsFromArguments;

0 commit comments

Comments
 (0)