Permalink
Browse files

#101 code review changes

  • Loading branch information...
1 parent 3551e9f commit 72ece2ed6c1b10aeb578e9f3fe9caf296d894966 @kof kof committed Oct 10, 2012
Showing with 55 additions and 11 deletions.
  1. +39 −1 lib/client.js
  2. +1 −1 package.json
  3. +15 −9 test/knox.test.js
View
@@ -493,6 +493,42 @@ var LIST_PARAMS = {
};
/**
+ * Normalization map for Client#list.
+ *
+ * @type {Object}
+ */
+
+var RESPONSE_NORMALIZATION = {
+ MaxKeys: Number,
+ IsTruncated: Boolean,
+ LastModified: Date,
+ Size: Number
+};
+
+/**
+ * Convert data we get from S3 xml in Client#list, since every primitive
+ * value there is a string.
+ *
+ * @type {Object}
+ */
+
+function normalizeResponse(data) {
+ for (var key in data) {
+ var Constr = RESPONSE_NORMALIZATION[key];
+
+ if (Constr) {
+ if (Constr === Date) {
+ data[key] = new Date(data[key]);
+ } else {
+ data[key] = Constr(data[key]);
+ }
+ } else if (Array.isArray(data[key])) {
+ data[key].forEach(normalizeResponse);
+ }
+ }
+}
+
+/**
* List up to 1000 objects at a time, with optional `headers`, `params`
* and callback `fn` with a possible exception and the response.
*
@@ -533,10 +569,12 @@ Client.prototype.list = function(params, headers, fn){
new xml2js.Parser({explicitArray: false, explicitRoot: false})
.parseString(xmlStr, function(err, data){
if (err) return fn(err);
+
delete data.$;
+ normalizeResponse(data);
fn(null, data);
});
- })
+ }).on('error', fn);
});
};
View
@@ -2,7 +2,7 @@
"name": "knox",
"description": "Amazon S3 client",
"keywords": ["aws", "amazon", "s3"],
- "version": "0.4.0",
+ "version": "0.3.1",
"author": "TJ Holowaychuk <tj@learnboost.com>",
"contributors": [
"TJ Holowaychuk <tj@learnboost.com>",
View
@@ -335,19 +335,25 @@ module.exports = {
},
'test .list()': function(done){
- client.putFile(jsonFixture, '/list/user1.json', function(err, res){
- client.putFile(jsonFixture, '/list/user2.json', function(err, res){
- client.list({prefix: 'list'}, function(err, res){
+ var files = ['/list/user1.json', '/list/user2.json'];
+
+ client.putFile(jsonFixture, files[0], function(err, res){
+ client.putFile(jsonFixture, files[1], function(err, res){
+ client.list({prefix: 'list'}, function(err, data){
assert.ok(!err);
- assert.equal(res.Prefix, 'list');
- assert.equal(res.IsTruncated, 'false');
- assert.equal(res.MaxKeys, '1000');
- assert.equal(res.Contents.length, 2);
+ assert.equal(data.Prefix, 'list');
+ assert.strictEqual(data.IsTruncated, true);
+ assert.strictEqual(data.MaxKeys, 1000);
+ assert.equal(data.Contents.length, 2);
+ assert.ok(data.Contents[0].LastModified instanceof Date);
+ assert.equal(typeof data.Contents[0].Size, 'number');
assert.deepEqual(
- Object.keys(res.Contents[0]),
+ Object.keys(data.Contents[0]),
['Key', 'LastModified', 'ETag', 'Size', 'Owner', 'StorageClass']
);
- done();
+ client.deleteMultiple(files, function(err, res) {
+ done();
+ });
});
});
});

0 comments on commit 72ece2e

Please sign in to comment.