Browse files

pass an info object to all callbacks as third argument

  • Loading branch information...
1 parent 0756542 commit 71243d03fd2ab80a1a819fe21262417d350d84da @tellnes committed Apr 26, 2012
Showing with 107 additions and 129 deletions.
  1. +107 −129 lib/index.js
View
236 lib/index.js
@@ -37,31 +37,61 @@ function listOptsToPath(path, opts) {
return path + (qs ? '?' + qs : '');
}
-function listBodyToInfo(that, opts, DataConstr, body) {
+function listFactory(that, DataConstr, opts, cb) {
+ if (arguments.length == 3) {
+ cb = opts;
+ opts = {};
+ }
- var info = {};
- info.isTruncated = body.IsTruncated == 'true';
- info.marker = body.Maker;
- info.nextMarker = body.NextMarker;
- info.maxItems = body.MaxItems;
+ return function(err, info) {
+ if (err) return cb(err, null, info);
- var listKey = DataConstr.name + 'Summary';
- if (opts.streaming) {
- listKey = 'Streaming' + listKey;
- }
+ var body = info.body;
+
+ info.isTruncated = body.IsTruncated == 'true';
+ info.marker = body.Maker;
+ info.nextMarker = body.NextMarker;
+ info.maxItems = body.MaxItems;
+
+ var listKey = DataConstr.name + 'Summary';
+ if (opts.streaming) {
+ listKey = 'Streaming' + listKey;
+ }
+
+ var list = body[listKey];
+ if (!list) {
+ list = [];
+ } else if (!Array.isArray(list)) {
+ list = [list];
+ }
- var list = body[listKey];
- if (!list) {
- list = [];
- } else if (!Array.isArray(list)) {
- list = [list];
+ opts.isList = true;
+
+ info.list = list.map(function(data) {
+ return new DataConstr(that, opts, data);
+ });
+
+ cb(null, info.list, info);
+ };
+}
+
+function itemFactory(that, DataConstr, opts, cb) {
+ if (arguments.length == 3) {
+ cb = opts;
+ opts = {};
}
- info.list = list.map(function(data) {
- return new DataConstr(that, opts, data);
- });
+ return function(err, info) {
+ if (err) return cb(err, null, info);
- return info;
+ var item = new DataConstr(that, opts, info.body);
+
+ if (info.etag) {
+ item.etag = info.etag;
+ }
+
+ cb(null, item, info);
+ };
}
function toUnixTime(time) {
@@ -107,6 +137,8 @@ CloudFront.prototype.request = function(method, path, headers, cb) {
headers.authorization = 'AWS ' + this.key + ':' + signature;
+ var info = {};
+
var req = https.request({
host: CloudFront.endpoint,
headers: headers,
@@ -115,7 +147,13 @@ CloudFront.prototype.request = function(method, path, headers, cb) {
});
req.on('response', function(res) {
- if (res.statusCode === 204) return cb(null, null, res);
+ info.res = res;
+ info.requestId = res.headers['x-amzn-requestid'];
+ if (res.headers.etag) {
+ info.etag = res.headers.etag;
+ }
+
+ if (res.statusCode === 204) return cb(null, info);
var parser = new xml2js.Parser();
res.setEncoding('utf8');
@@ -133,14 +171,18 @@ CloudFront.prototype.request = function(method, path, headers, cb) {
}
}
- cb(err, body, res);
+ info.body = body;
+
+ cb(err, info);
});
parser.on("error", function(err) {
- cb(err);
+ cb(err, info);
});
});
+ info.req = req;
+
return req;
};
@@ -209,13 +251,9 @@ CloudFront.prototype.createDistribution = function(config, callerReference, cb)
cb = callerReference;
}
- var self = this;
- var req = this.request('POST', (config.streaming ? 'streaming-' : '') + 'distribution', function(err, body) {
- if (err) return cb(err);
-
- var item = new Distribution(self, {}, body);
- cb(null, item);
- });
+ var req = this.request('POST', (config.streaming ? 'streaming-' : '') + 'distribution',
+ itemFactory(this, Distribution, cb)
+ );
req.write(this.generateDistributionXml(config));
req.end();
};
@@ -226,13 +264,9 @@ CloudFront.prototype.getDistribution = function(distribution, opts, cb) {
opts = {};
}
- var self = this;
- this.request('GET', (opts.streaming ? 'streaming-' : '') + 'distribution/' + distribution, function(err, body) {
- if (err) return cb(err);
-
- var item = new Distribution(self, {}, body);
- cb(null, item);
- }).end();
+ this.request('GET', (opts.streaming ? 'streaming-' : '') + 'distribution/' + distribution,
+ itemFactory(this, Distribution, cb)
+ ).end();
};
CloudFront.prototype.getDistributionConfig = function(distribution, opts, cb) {
@@ -241,16 +275,9 @@ CloudFront.prototype.getDistributionConfig = function(distribution, opts, cb) {
opts = {};
}
- var self = this;
- this.request('GET', (opts.streaming ? 'streaming-' : '') + 'distribution/' + distribution + '/config', function(err, body, res) {
- if (err) return cb(err);
-
- var item = new DistributionConfig(self, {
- distribution: distribution
- }, body);
- item.etag = res.headers.etag;
- cb(null, item);
- }).end();
+ this.request('GET', (opts.streaming ? 'streaming-' : '') + 'distribution/' + distribution + '/config',
+ itemFactory(this, DistributionConfig, {distribution: distribution}, cb)
+ ).end();
};
CloudFront.prototype.setDistributionConfig = function(distribution, config, etag, cb) {
@@ -264,16 +291,10 @@ CloudFront.prototype.setDistributionConfig = function(distribution, config, etag
throw new Error('Config must include etag');
}
- var self = this;
- var req = this.request('PUT', (config.streaming ? 'streaming-' : '') + 'distribution/' + distribution + '/config', {
- 'If-Match': config.etag
- }, function(err, body, res) {
- if (err) return cb(err);
-
- var item = new Distribution(self, {}, body);
- item.etag = res.headers.etag;
- cb(null, item);
- });
+ var req = this.request('PUT', (config.streaming ? 'streaming-' : '') + 'distribution/' + distribution + '/config',
+ {'If-Match': config.etag},
+ itemFactory(this, Distribution, cb)
+ );
req.write(this.generateDistributionXml(config.config || config));
req.end();
};
@@ -289,8 +310,8 @@ CloudFront.prototype.deleteDistribution = function(distribution, etag, opts, cb)
}
etag = String(etag.etag || etag);
- this.request('DELETE', (opts.streaming ? 'streaming-' : '') + 'distribution/' + distribution, {'If-Match': etag}, function(err, body, res) {
- cb(err);
+ this.request('DELETE', (opts.streaming ? 'streaming-' : '') + 'distribution/' + distribution, {'If-Match': etag}, function(err, info) {
+ cb(err, null, info);
}).end();
};
@@ -300,16 +321,9 @@ CloudFront.prototype.listDistributions = function(opts, cb) {
opts = {};
}
- var self = this;
- this.request('GET', listOptsToPath('distribution', opts), function(err, body) {
- if (err) return cb(err);
-
- var info = listBodyToInfo(self, {
- streaming: opts.streaming,
- isList: true
- }, Distribution, body);
- cb(null, info.list, info);
- }).end();
+ this.request('GET', listOptsToPath('distribution', opts),
+ listFactory(this, Distribution, {streaming: opts.streaming}, cb)
+ ).end();
};
/* Invalidation */
@@ -326,29 +340,17 @@ CloudFront.prototype.createInvalidation = function(distribution, callerReference
throw new TypeError('First argument must be a valid distribution id');
}
- var self = this;
- var req = this.request('POST', 'distribution/' + distribution + '/invalidation', function(err, body) {
- if (err) return cb(err);
-
- var item = new Invalidation(self, {
- distribution: distribution
- }, body);
- cb(null, item);
- });
+ var req = this.request('POST', 'distribution/' + distribution + '/invalidation',
+ itemFactory(this, Invalidation, {distribution: distribution}, cb)
+ );
req.write(this.generateInvalidationXml(callerReference, paths));
req.end();
};
CloudFront.prototype.getInvalidation = function(distribution, id, cb) {
- var self = this;
- this.request('GET', 'distribution/' + distribution + '/invalidation/' + id, function(err, body) {
- if (err) return cb(err);
-
- var item = new Invalidation(self, {
- distribution: distribution
- }, body);
- cb(null, item);
- }).end();
+ this.request('GET', 'distribution/' + distribution + '/invalidation/' + id,
+ itemFactory(this, Invalidation, {distribution: distribution}, cb)
+ ).end();
};
CloudFront.prototype.listInvalidations = function(distribution, opts, cb) {
@@ -357,15 +359,9 @@ CloudFront.prototype.listInvalidations = function(distribution, opts, cb) {
opts = {};
}
- var self = this;
- this.request('GET', listOptsToPath('distribution/' + distribution + '/invalidation', opts), function(err, body) {
- if (err) return cb(err);
-
- var info = listBodyToInfo(self, {
- distribution: distribution
- }, Invalidation, body);
- cb(null, info.list, info);
- }).end();
+ this.request('GET', listOptsToPath('distribution/' + distribution + '/invalidation', opts),
+ listFactory(this, Invalidation, {distribution: distribution}, cb)
+ ).end();
};
CloudFront.prototype.generateOriginAccessIdentityXml = function(callerReference, comment) {
@@ -384,26 +380,18 @@ CloudFront.prototype.generateOriginAccessIdentityXml = function(callerReference,
CloudFront.prototype.createOAI =
CloudFront.prototype.createOriginAccessIdentity = function(callerReference, comment, cb) {
- var self = this;
- var req = this.request('POST', 'origin-access-identity/cloudfront', function(err, body) {
- if (err) return cb(err);
-
- var item = new CloudFrontOriginAccessIdentity(self, {}, body);
- cb(null, item);
- });
+ var req = this.request('POST', 'origin-access-identity/cloudfront',
+ itemFactory(this, CloudFrontOriginAccessIdentity, cb)
+ );
req.write(this.generateOriginAccessIdentityXml(callerReference, comment));
req.end();
};
CloudFront.prototype.getOAI =
CloudFront.prototype.getOriginAccessIdentity = function(id, cb) {
- var self = this;
- this.request('GET', 'origin-access-identity/cloudfront/' + id, function(err, body, res) {
- if (err) return cb(err);
-
- var item = new CloudFrontOriginAccessIdentity(self, {etag: res.headers.etag}, body);
- cb(null, item);
- }).end();
+ this.request('GET', 'origin-access-identity/cloudfront/' + id,
+ itemFactory(this, CloudFrontOriginAccessIdentity, cb)
+ ).end();
};
CloudFront.prototype.listOAIs =
@@ -416,15 +404,9 @@ CloudFront.prototype.listOriginAccessIdentities = function(opts, cb) {
// Make sure that the streaming prefix is not added.
opts.streaming = false;
- var self = this;
- this.request('GET', listOptsToPath('origin-access-identity/cloudfront', opts), function(err, body) {
- if (err) return cb(err);
-
- var info = listBodyToInfo(self, {
- isList: true
- }, CloudFrontOriginAccessIdentity, body);
- cb(null, info.list, info);
- }).end();
+ this.request('GET', listOptsToPath('origin-access-identity/cloudfront', opts),
+ listFactory(this, CloudFrontOriginAccessIdentity, cb)
+ ).end();
};
/**
@@ -731,10 +713,6 @@ function CloudFrontOriginAccessIdentity(client, info, data) {
if (oai.CallerReference) {
this.callerReference = oai.CallerReference;
}
-
- if (info.etag) {
- this.etag = info.etag;
- }
}
util.inherits(CloudFrontOriginAccessIdentity, CFObject);
@@ -746,13 +724,13 @@ CloudFrontOriginAccessIdentity.prototype.setComment = function(comment, cb) {
var self = this;
var req = this.client.request('PUT', 'origin-access-identity/cloudfront/' + this.id + '/config', {
'If-Match': this.etag
- }, function(err, body, res) {
- if (err) return cb(err);
+ }, function(err, info) {
+ if (err) return cb(err, null, info);
- self.comment = body.CloudFrontOriginAccessIdentityConfig.Comment;
- self.etag = res.headers.etag;
+ self.comment = info.body.CloudFrontOriginAccessIdentityConfig.Comment;
+ self.etag = info.etag;
- cb(null, self);
+ cb(null, self, info);
});
req.write(this.client.generateOriginAccessIdentityXml(this.callerReference, comment));
req.end();
@@ -763,8 +741,8 @@ CloudFrontOriginAccessIdentity.prototype.delete = function(cb) {
throw new Error('Missing etag. Did you use CloudFront#listOriginAccessIdentities?');
}
- this.client.request('DELETE', 'origin-access-identity/cloudfront/' + this.id, {'If-Match': this.etag}, function(err, body, res) {
- cb(err);
+ this.client.request('DELETE', 'origin-access-identity/cloudfront/' + this.id, {'If-Match': this.etag}, function(err, info) {
+ cb(err, null, info);
}).end();
};

0 comments on commit 71243d0

Please sign in to comment.