Skip to content

Commit

Permalink
Merge bc456a3 into 69d776a
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilgupta345 committed Jul 10, 2019
2 parents 69d776a + bc456a3 commit 075be1a
Show file tree
Hide file tree
Showing 9 changed files with 8,521 additions and 78 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
@@ -1,8 +1,7 @@
language: node_js
node_js:
- '5'
- '4'
- '0.12'
- '0.10'
- '12'
- '10'
- '8'
after_script:
- npm run coveralls
18 changes: 9 additions & 9 deletions index.js
@@ -1,15 +1,15 @@
'use strict';
var path = require('path');
var AWS = require('aws-sdk');
var Promise = require('pinkie-promise');
var utils = require('./lib/utils');
const path = require('path');
const AWS = require('aws-sdk');
const Promise = require('bluebird');
const utils = require('./lib/utils');

module.exports = function (name, alias, opts) {
if (typeof name !== 'string') {
if (!name || typeof name !== 'string') {
return Promise.reject(new Error('Provide a AWS Lambda function name.'));
}

if (typeof alias !== 'string') {
if (!alias || typeof alias !== 'string') {
return Promise.reject(new Error('Provide an alias name.'));
}

Expand All @@ -20,7 +20,7 @@ module.exports = function (name, alias, opts) {

if (opts.awsProfile) {
// Set the `credentials` property if a profile is provided
var objectCredentials = {
const objectCredentials = {
profile: opts.awsProfile
};

Expand All @@ -32,9 +32,9 @@ module.exports = function (name, alias, opts) {
}

// Create a lambda object
var lambda = new AWS.Lambda();
const lambda = new AWS.Lambda();

var options = {
const options = {
FunctionName: name,
Name: alias
};
Expand Down
68 changes: 55 additions & 13 deletions lib/utils.js
@@ -1,24 +1,65 @@
/* eslint-disable prefer-arrow-callback */
'use strict';
var pify = require('pify');
var objectAssign = require('object-assign');
var Promise = require('pinkie-promise');
const objectAssign = require('object-assign');
const Promise = require('bluebird');

const MAX_RECURSION = 10;

function findAllVersions(lambda, opts) {
// Recursive function to continually call the Lambda paginated
// endpoint to get list of versions by function with the specified
// MAX_RECURSION limit in the case that the number of versions
// available for the lambda exceeds the page limit.
function listAllVersions(params = {}, numOfRec = MAX_RECURSION, currentListOfVersions = []) {
if (numOfRec <= 0) {
return {
Versions: currentListOfVersions
};
}

return lambda.listVersionsByFunction(params)
.promise()
.then(function (result) {
if (!result.Versions || result.Versions.length === 0) {
return {
Versions: currentListOfVersions
};
}

const truncatedVersions = result.Versions.concat(currentListOfVersions);
if (!result.NextMarker) {
return {
Versions: truncatedVersions
};
}

return listAllVersions(
objectAssign({}, params, {Marker: result.NextMarker}),
numOfRec - 1,
truncatedVersions
);
});
}

return listAllVersions({FunctionName: opts.FunctionName});
}

function findLatestVersion(lambda, opts) {
if (opts.FunctionVersion) {
// Return the function version if it was provided
return Promise.resolve(opts.FunctionVersion);
}

return pify(lambda.listVersionsByFunction.bind(lambda), Promise)({FunctionName: opts.FunctionName})
return findAllVersions(lambda, opts)
.then(function (data) {
if (!data.Versions || data.Versions.length === 0) {
throw new Error('No versions found.');
return Promise.reject(new Error('No versions found.'));
}

// Sort all the versions
data.Versions.sort(function (a, b) {
var aVersion = a.Version === '$LATEST' ? 0 : parseInt(a.Version, 10);
var bVersion = b.Version === '$LATEST' ? 0 : parseInt(b.Version, 10);
const aVersion = a.Version === '$LATEST' ? 0 : parseInt(a.Version, 10);
const bVersion = b.Version === '$LATEST' ? 0 : parseInt(b.Version, 10);

return bVersion - aVersion;
});
Expand All @@ -28,23 +69,24 @@ function findLatestVersion(lambda, opts) {
}

function updateOrCreate(lambda, opts) {
var options = objectAssign({}, opts);
const options = objectAssign({}, opts);

return findLatestVersion(lambda, opts)
.then(function (version) {
options.FunctionVersion = version;

// Try to update the version first
return pify(lambda.updateAlias.bind(lambda), Promise)(options);
return lambda.updateAlias(options).promise();
})
.catch(function (err) {
if (err.code === 'ResourceNotFoundException') {
.catch(function (error) {
if (error.code === 'ResourceNotFoundException') {
// If the alias does not exist yet, create it
return pify(lambda.createAlias.bind(lambda), Promise)(options);
return lambda.createAlias(options).promise();
}

throw err;
throw error;
});
}

exports.updateOrCreate = updateOrCreate;
exports.MAX_RECURSION = MAX_RECURSION;

0 comments on commit 075be1a

Please sign in to comment.