Skip to content

Commit 5573df0

Browse files
committed
Fix issue with linting and adding better checks
1 parent d08fc5e commit 5573df0

File tree

4 files changed

+91
-59
lines changed

4 files changed

+91
-59
lines changed

gruntfile.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ module.exports = function(grunt) {
2323
},
2424
tests: {
2525
src: ['tests/*']
26+
},
27+
"index.js": {
28+
src: ['./index.js']
29+
},
30+
lib: {
31+
src: ['lib/*']
2632
}
2733
},
2834
mochaTest: {
@@ -41,7 +47,6 @@ module.exports = function(grunt) {
4147
grunt.loadNpmTasks('grunt-mocha-test');
4248

4349
// Default task.
44-
grunt.registerTask('default', ['jshint']);
45-
grunt.registerTask('test', ['mochaTest']);
50+
grunt.registerTask('test', [ 'jshint', 'mochaTest' ]);
4651

4752
};

index.js

+36-23
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
var util = require("./util");
2-
var S3;
1+
var util = require("./lib/utils");
2+
var knox = require("knox");
3+
var s3;
34

45
function S3Context(options) {
56
this.readOnly = options.isReadOnly;
67
this.keyPrefix = options.keyPrefix;
7-
};
8+
}
9+
10+
function prefixKey(prefix, key) {
11+
return prefix + "/" + key;
12+
}
813

914
S3Context.prototype.put = function (key, value, callback) {
1015
if(this.readOnly) {
11-
return callback("Error: Write operation on readOnly context.")
16+
return callback("Error: Write operation on readOnly context.");
1217
}
13-
key = this.keyPrefix + "/" + key;
18+
key = prefixKey(this.keyPrefix, key);
1419
// We do extra work to make sure typed arrays survive
15-
// being stored in the db and still get the right prototype later.
20+
// being stored in S3 and still get the right prototype later.
1621
if (Object.prototype.toString.call(value) === "[object Uint8Array]") {
1722
value = {
1823
__isUint8Array: true,
@@ -23,52 +28,59 @@ S3Context.prototype.put = function (key, value, callback) {
2328
var headers = {
2429
'x-amz-acl': 'public-read',
2530
'Content-Length': Buffer.byteLength(value),
31+
'application/type': 'application/json'
2632
};
2733

2834
function onError() {
2935
callback("Error " + res.statusCode);
3036
}
3137

32-
S3.put(key, headers)
38+
s3.put(key, headers)
3339
.on("error", onError)
3440
.on("response", function (res) {
3541
if (res.statusCode !== 200) {
36-
return onError;
42+
onError();
3743
}
3844
callback(null);
3945
})
4046
.end(value);
4147
};
4248

4349
S3Context.prototype.delete = function (key, callback) {
44-
key = this.keyPrefix + "/" + key;
4550
if(this.readOnly) {
46-
return callback("Error: Write operation on readOnly context.")
51+
return callback("Error: Write operation on readOnly context.");
4752
}
48-
S3.del(key).on('response', function (res) {
49-
return callback(null);
53+
key = prefixKey(this.keyPrefix, key);
54+
s3.del(key).on('response', function (res) {
55+
if(res.statusCode === 403) {
56+
return callback("Error: 403. Permission denied");
57+
}
58+
callback(null);
5059
}).end();
5160
};
5261

5362
S3Context.prototype.clear = function (callback) {
5463
if(this.readOnly) {
55-
return callback("Error: Write operation on readOnly context.")
64+
return callback("Error: Write operation on readOnly context.");
5665
}
5766
var options = {
58-
prefix: ""
67+
prefix: this.keyPrefix
5968
};
6069
getAllObjects(options, callback, []);
6170

6271
function getAllObjects(options, callback, aggregate) {
63-
S3.list(options, function (err, data) {
72+
s3.list(options, function (err, data) {
6473
aggregate = aggregate.concat(data.Contents.map(function (content) {
6574
return content.Key;
6675
}));
6776
if (data.IsTruncated) {
6877
options.marker = data.Contents[data.Contents.length - 1].Key;
6978
getAllObjects(options, callback, aggregate);
7079
}
71-
S3.deleteMultiple(aggregate, function (err, res) {
80+
s3.deleteMultiple(aggregate, function (err, res) {
81+
if(res.statusCode === 403) {
82+
return callback("Error 403: Permission deined." + err);
83+
}
7284
return callback(null);
7385
});
7486
});
@@ -77,11 +89,11 @@ S3Context.prototype.clear = function (callback) {
7789
};
7890

7991
S3Context.prototype.get = function (key, callback) {
80-
key = this.keyPrefix + "/" + key;
81-
S3.get(key).on('response', function (res) {
92+
key = prefixKey(this.keyPrefix, key);
93+
s3.get(key).on('response', function (res) {
8294
if (res.statusCode === 404) {
8395
return callback("Error " + res.statusCode);
84-
};
96+
}
8597
var chunks = [];
8698
res.on('data', function (chunk) {
8799
chunks.push(chunk);
@@ -97,7 +109,7 @@ S3Context.prototype.get = function (key, callback) {
97109
}
98110
callback(null, value);
99111
} catch(e) {
100-
callback(e);
112+
return callback(e);
101113
}
102114
});
103115
}).end();
@@ -118,20 +130,21 @@ S3Provider.prototype.open = function(options, callback) {
118130
return;
119131
}
120132
try {
121-
S3 = require("knox").createClient({
133+
s3 = knox.createClient({
122134
bucket: options.bucket,
123135
key: options.key,
124136
secret: options.secret
125137
});
126-
S3.list({ prefix: this.keyPrefix, maxKeys: 1 }, function(err, data) {
138+
s3.list({ prefix: this.keyPrefix, maxKeys: 1 }, function(err, data) {
127139
if(err) {
128140
callback(err);
129141
return;
130142
}
143+
// Check to see if this is the first access or not"
131144
callback(null, data.Contents.length === 0);
132145
});
133146
} catch(e) {
134-
callback("Error: Unable to connect to S3. " + e);
147+
callback("Error: Unable to connect to s3. " + e);
135148
}
136149
};
137150

util/index.js lib/utils.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ exports.u8toArray = function(u8) {
55
array[i] = u8[i];
66
}
77
return array;
8-
}
8+
};
99

1010
exports.guid = function () {
1111
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
12-
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
12+
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
1313
return v.toString(16);
1414
}).toUpperCase();
15-
}
15+
};

tests/test.js

+45-31
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
var expect = require('expect.js'),
22
S3Provider = require(".."),
3-
S3Options = { bucket: "<name>", key: "<S3_KEY>", secret: "<S3_SECRET>" },
4-
guid = require("../util").guid;
5-
6-
if(!S3Provider.isSupported) {
7-
console.log("Skipping Filer.FileSystem.providers.S3 tests, since S3 isn't supported.");
8-
return;
9-
}
3+
S3Options = { bucket: "<bucket_name>", key: "<S3_KEY>", secret: "<S3_SECRET>" },
4+
guid = require("../lib/utils").guid,
5+
randomName,
6+
randomKeyPrefix;
107

118
describe("Filer.FileSystem.providers.S3", function() {
129
it("is supported -- if it isn't, none of these tests can run.", function() {
@@ -24,24 +21,35 @@ describe("Filer.FileSystem.providers.S3", function() {
2421
var _provider;
2522

2623
beforeEach(function() {
27-
_provider = new S3Provider({name: guid(), keyPrefix: guid() });
24+
randomName = guid();
25+
randomKeyPrefix = guid();
26+
_provider = new S3Provider({name: randomName, keyPrefix: randomKeyPrefix });
2827
});
2928

3029
afterEach(function(done){
31-
provider = _provider;
32-
provider.open(S3Options, function(error, firstAccess) {
33-
if (error) {
34-
throw error;
35-
}
36-
var context = provider.getReadWriteContext();
37-
context.clear(function(error) {
38-
if (error) {
39-
throw error;
40-
}
41-
expect(error).not.to.exist;
42-
done();
30+
var s3 = require("knox").createClient(S3Options);
31+
var options = {
32+
prefix: randomKeyPrefix
33+
};
34+
getAllObjects(options, []);
35+
36+
function getAllObjects(options, aggregate) {
37+
s3.list(options, function (err, data) {
38+
aggregate = aggregate.concat(data.Contents.map(function (content) {
39+
return content.Key;
40+
}));
41+
if (data.IsTruncated) {
42+
options.marker = data.Contents[data.Contents.length - 1].Key;
43+
getAllObjects(options, aggregate);
44+
}
45+
s3.deleteMultiple(aggregate, function (err, res) {
46+
if(res.statusCode === 403) {
47+
return callback("Error 403: Permission deined." + err);
48+
}
49+
done();
50+
});
4351
});
44-
});
52+
}
4553
});
4654

4755
it("should open a new S3", function(done) {
@@ -67,6 +75,7 @@ describe("Filer.FileSystem.providers.S3", function() {
6775
if (error) {
6876
throw error;
6977
}
78+
expect(firstAccess).to.be.true;
7079
var context = provider.getReadWriteContext();
7180
context.clear(function(error) {
7281
if (error) {
@@ -85,14 +94,16 @@ describe("Filer.FileSystem.providers.S3", function() {
8594
if(error) {
8695
throw error;
8796
}
97+
expect(firstAccess).to.be.true;
8898
var context = provider.getReadWriteContext();
89-
context.put("key", "data", function(error, result) {
99+
context.put("key", data, function(error) {
90100
if(error) {
91101
throw error;
92102
}
93103
context.get("key", function(error, result) {
94104
expect(error).not.to.exist;
95-
expect(result).to.equal("data");
105+
expect(result).to.exist;
106+
expect(result).to.eql(data);
96107
done();
97108
});
98109
});
@@ -105,13 +116,13 @@ describe("Filer.FileSystem.providers.S3", function() {
105116
if (error) {
106117
throw error;
107118
}
108-
119+
expect(firstAccess).to.be.true;
109120
var context = provider.getReadWriteContext();
110-
context.put("key", "value", function(error, result) {
121+
context.put("key", "value", function(error) {
111122
if (error) {
112123
throw error;
113124
}
114-
context.delete("key", function(error, result) {
125+
context.delete("key", function(error) {
115126
if (error) {
116127
throw error;
117128
}
@@ -134,16 +145,19 @@ describe("Filer.FileSystem.providers.S3", function() {
134145
if (error) {
135146
throw error;
136147
}
148+
expect(firstAccess).to.be.true;
137149
var context = provider.getReadWriteContext();
138-
context.put("key1", data1, function(error, result) {
150+
context.put("key1", data1, function(error) {
139151
if (error) {
140152
throw error;
141153
}
142-
context.put("key2", data2, function(error, result) {
154+
expect(error).not.to.exist;
155+
context.put("key2", data2, function(error) {
143156
if (error) {
144157
throw error;
145158
}
146-
context.clear(function(err) {
159+
expect(error).not.to.exist;
160+
context.clear(function(error) {
147161
if (error) {
148162
throw error;
149163
}
@@ -170,10 +184,10 @@ describe("Filer.FileSystem.providers.S3", function() {
170184
if (error) {
171185
throw error;
172186
}
187+
expect(firstAccess).to.be.true;
173188
var context = provider.getReadOnlyContext();
174-
context.put("key1", data1, function(error, result) {
189+
context.put("key1", data1, function(error) {
175190
expect(error).to.exist;
176-
expect(result).not.to.exist;
177191
done();
178192
});
179193
});

0 commit comments

Comments
 (0)