diff --git a/index.js b/index.js index bb1edaa..e2110da 100644 --- a/index.js +++ b/index.js @@ -107,6 +107,13 @@ module.exports = { }, fetchRevisions: function(context) { + if (this.readConfig('disableFetchRevisions')) { + this.log('fetchRevisions - not fetching any revisions', { verbose: true }); + return { + activations: [] + }; + } + return this._list(context) .then(function(revisions) { return { @@ -116,6 +123,12 @@ module.exports = { }, fetchInitialRevisions: function(context) { + + if (this.readConfig('fetchOnlyRelevantRevisions')) { + this.log('fetchInitialRevisions - fetching only enough revisions to find the active revision', { verbose: true }); + return this._fetchActiveRevision(); + } + return this._list(context) .then(function(revisions) { return { @@ -124,6 +137,24 @@ module.exports = { }); }, + _fetchActiveRevision: function() { + var bucket = this.readConfig('bucket'); + var prefix = this.readConfig('prefix'); + var filePattern = this.readConfig('filePattern'); + + var options = { + bucket: bucket, + prefix: prefix, + filePattern: filePattern, + }; + + var s3 = new this.S3({ plugin: this }); + return s3.getActiveRevision(options) + .then((activeRevision) => { + return { initialRevisions: [activeRevision] }; + }); + }, + _list: function(/* context */) { var bucket = this.readConfig('bucket'); var prefix = this.readConfig('prefix'); diff --git a/lib/s3.js b/lib/s3.js index c821371..1d10d91 100644 --- a/lib/s3.js +++ b/lib/s3.js @@ -152,6 +152,36 @@ module.exports = CoreObject.extend({ .then((response) => response.Contents.find((element) => element.Key === revisionPrefix)); }, + getActiveRevision(options) { + var client = this._client; + var bucket = options.bucket; + var prefix = options.prefix; + var revisionPrefix = joinUriSegments(prefix, options.filePattern + ":"); + var indexKey = joinUriSegments(prefix, options.filePattern); + + return headObject(client, { Bucket: bucket, Key: indexKey }) + .then((current) => { + if (current === undefined) { + return; + } + + return this.listAllObjects( + { Bucket: bucket, Prefix: revisionPrefix }, + (element) => element.ETag === current.ETag + ) + .then((elements) => { + let activeRevision = elements.find((element) => element.ETag === current.ETag); + if (!activeRevision) { + return; + } + + var revision = activeRevision.Key.substr(revisionPrefix.length); + return { active: true, revision, timestamp: activeRevision.LastModified }; + }) + }); + + }, + fetchRevisions: function(options) { var client = this._client; var bucket = options.bucket; @@ -174,7 +204,7 @@ module.exports = CoreObject.extend({ }); }, - listAllObjects: function(options) { + listAllObjects: function(options, until) { var client = this._client; var listObjects = RSVP.denodeify(client.listObjects.bind(client)); var allRevisions = []; @@ -183,13 +213,14 @@ module.exports = CoreObject.extend({ return listObjects(options).then(function(response) { [].push.apply(allRevisions, response.Contents); - if (response.IsTruncated) { - var nextMarker = response.Contents[response.Contents.length - 1].Key; - options.Marker = nextMarker; - return listObjectRecursively(options); - } else { + var isComplete = !response.IsTruncated || (until && response.Contents.some(until)); + + if (isComplete) { return allRevisions; } + var nextMarker = response.Contents[response.Contents.length - 1].Key; + options.Marker = nextMarker; + return listObjectRecursively(options); }); }