From 32b8db9a573814ce07ad25ae1db96ead56d0c700 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 9 Jun 2017 19:18:05 +0900 Subject: [PATCH 1/9] Replace async with Promise --- lib/main.js | 33 ++++++++++++++-------- test/main.js | 78 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 66 insertions(+), 45 deletions(-) diff --git a/lib/main.js b/lib/main.js index 45716deb..c1b9e41e 100644 --- a/lib/main.js +++ b/lib/main.js @@ -668,29 +668,38 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE } } - return async.map(updateEventSourceList, function (updateEventSource, _cb) { + return Promise.all(updateEventSourceList.map((updateEventSource) => { switch (updateEventSource['type']) { case 'create': delete updateEventSource['type'] - lambda.createEventSourceMapping(updateEventSource, function (err, data) { - return _cb(err, data) + return new Promise((resolve, reject) => { + lambda.createEventSourceMapping(updateEventSource, (err, data) => { + if (err) return reject(err) + resolve(data) + }) }) - break case 'update': delete updateEventSource['type'] - lambda.updateEventSourceMapping(updateEventSource, function (err, data) { - return _cb(err, data) + return new Promise((resolve, reject) => { + lambda.updateEventSourceMapping(updateEventSource, (err, data) => { + if (err) return reject(err) + resolve(data) + }) }) - break case 'delete': delete updateEventSource['type'] - lambda.deleteEventSourceMapping(updateEventSource, function (err, data) { - return _cb(err, data) + return new Promise((resolve, reject) => { + lambda.deleteEventSourceMapping(updateEventSource, (err, data) => { + if (err) return reject(err) + resolve(data) + }) }) - break } - }, function (err, results) { - return cb(err, results) + return Promise.resolve() + })).then((data) => { + cb(null, data) + }).catch((err) => { + cb(err) }) } diff --git a/test/main.js b/test/main.js index a34a235f..54283bee 100644 --- a/test/main.js +++ b/test/main.js @@ -874,50 +874,62 @@ describe('lib/main', function () { ) }) - it('simple test with mock (In case of new addition)', (done) => { + it('simple test with mock (In case of new addition)', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) - lambda._updateEventSources( - awsLambda, - 'functionName', - [], - eventSourceList.EventSourceMappings, - (err, results) => { - assert.isUndefined(err) - assert.deepEqual(results, [lambdaMockSettings.createEventSourceMapping]) - done() + return new Promise((resolve) => { + lambda._updateEventSources( + awsLambda, + 'functionName', + [], + eventSourceList.EventSourceMappings, + (err, results) => resolve({ err: err, results: results }) + ) + }).then((actual) => { + const expected = { + err: null, + results: [lambdaMockSettings.createEventSourceMapping] } - ) + assert.deepEqual(actual, expected) + }) }) - it('simple test with mock (In case of deletion)', (done) => { - lambda._updateEventSources( - awsLambda, - 'functionName', - lambdaMockSettings.listEventSourceMappings.EventSourceMappings, - {}, - (err, results) => { - assert.isUndefined(err) - assert.deepEqual(results, [lambdaMockSettings.deleteEventSourceMapping]) - done() + it('simple test with mock (In case of deletion)', () => { + return new Promise((resolve) => { + lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + {}, + (err, results) => resolve({ err: err, results: results }) + ) + }).then((actual) => { + const expected = { + err: null, + results: [lambdaMockSettings.deleteEventSourceMapping] } - ) + assert.deepEqual(actual, expected) + }) }) - it('simple test with mock (In case of update)', (done) => { + it('simple test with mock (In case of update)', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) - lambda._updateEventSources( - awsLambda, - 'functionName', - lambdaMockSettings.listEventSourceMappings.EventSourceMappings, - eventSourceList.EventSourceMappings, - (err, results) => { - assert.isUndefined(err) - assert.deepEqual(results, [lambdaMockSettings.updateEventSourceMapping]) - done() + return new Promise((resolve) => { + lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + eventSourceList.EventSourceMappings, + (err, results) => resolve({ err: err, results: results }) + ) + }).then((actual) => { + const expected = { + err: null, + results: [lambdaMockSettings.updateEventSourceMapping] } - ) + assert.deepEqual(actual, expected) + }) }) }) From 99641807bb6bd12a0c1c2fb0e0577ac2bb43dc02 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 9 Jun 2017 19:19:20 +0900 Subject: [PATCH 2/9] Modify to arrow function --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index c1b9e41e..0a82310c 100644 --- a/lib/main.js +++ b/lib/main.js @@ -614,7 +614,7 @@ Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) { }) } -Lambda.prototype._updateEventSources = function (lambda, functionName, existingEventSourceList, eventSourceList, cb) { +Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList, cb) => { if (eventSourceList == null) { return cb(null, []) } From bdacf9766aca94fd42cb6e24126dc48eef4bf2be Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 9 Jun 2017 19:19:34 +0900 Subject: [PATCH 3/9] Modify from var to const --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index 0a82310c..808b7ea5 100644 --- a/lib/main.js +++ b/lib/main.js @@ -618,7 +618,7 @@ Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourc if (eventSourceList == null) { return cb(null, []) } - var updateEventSourceList = [] + const updateEventSourceList = [] // Checking new and update event sources for (let i in eventSourceList) { let isExisting = false From 4be72088cf8138d0b4a05bc6a131f25009960187 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 14 Jun 2017 21:10:22 +0900 Subject: [PATCH 4/9] Modify _uploadExisting returns Promise --- lib/main.js | 63 ++++++++++++++++++++++++++-------------------------- test/main.js | 6 ++--- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/lib/main.js b/lib/main.js index 4d7b6014..6b0c00e4 100644 --- a/lib/main.js +++ b/lib/main.js @@ -465,38 +465,37 @@ Lambda.prototype._setRunTimeEnvironmentVars = function (program) { } Lambda.prototype._uploadExisting = (lambda, params, cb) => { - const request = lambda.updateFunctionCode({ - 'FunctionName': params.FunctionName, - 'ZipFile': params.Code.ZipFile, - 'Publish': params.Publish - }, (err, data) => { - if (err) { - return cb(err, data) - } - - return lambda.updateFunctionConfiguration({ + return new Promise((resolve, reject) => { + const request = lambda.updateFunctionCode({ 'FunctionName': params.FunctionName, - 'Description': params.Description, - 'Handler': params.Handler, - 'MemorySize': params.MemorySize, - 'Role': params.Role, - 'Timeout': params.Timeout, - 'Runtime': params.Runtime, - 'VpcConfig': params.VpcConfig, - 'Environment': params.Environment, - 'DeadLetterConfig': params.DeadLetterConfig, - 'TracingConfig': params.TracingConfig - }, (err, data) => { - return cb(err, data) + 'ZipFile': params.Code.ZipFile, + 'Publish': params.Publish + }, (err) => { + if (err) return reject(err) + + lambda.updateFunctionConfiguration({ + 'FunctionName': params.FunctionName, + 'Description': params.Description, + 'Handler': params.Handler, + 'MemorySize': params.MemorySize, + 'Role': params.Role, + 'Timeout': params.Timeout, + 'Runtime': params.Runtime, + 'VpcConfig': params.VpcConfig, + 'Environment': params.Environment, + 'DeadLetterConfig': params.DeadLetterConfig, + 'TracingConfig': params.TracingConfig + }, (err, data) => { + if (err) return reject(err) + resolve(data) + }) }) - }) - request.on('retry', (response) => { - console.log(response.error.message) - console.log('=> Retrying') + request.on('retry', (response) => { + console.log(response.error.message) + console.log('=> Retrying') + }) }) - - return request } Lambda.prototype._uploadNew = (lambda, params, cb) => { @@ -868,10 +867,7 @@ Lambda.prototype.deploy = function (program) { // From now on, callback will not be used. return Promise.all([ new Promise((resolve, reject) => { - _this._uploadExisting(lambda, params, (err, results) => { - if (err) { - throw err - } + _this._uploadExisting(lambda, params).then((results) => { console.log('=> Zip file(s) done uploading. Results follow: ') console.log(results) _this._updateScheduleEvents( @@ -883,8 +879,11 @@ Lambda.prototype.deploy = function (program) { resolve(results) } ) + }).catch((err) => { + reject(err) }) }), + new Promise((resolve, reject) => { _this._updateEventSources( lambda, diff --git a/test/main.js b/test/main.js index 5cc0518a..5347ab41 100644 --- a/test/main.js +++ b/test/main.js @@ -1007,12 +1007,10 @@ describe('lib/main', function () { }) describe('_uploadExisting', () => { - it('simple test with mock', (done) => { + it('simple test with mock', () => { const params = lambda._params(program, null) - lambda._uploadExisting(awsLambda, params, (err, results) => { - assert.isNull(err) + return lambda._uploadExisting(awsLambda, params).then((results) => { assert.deepEqual(results, lambdaMockSettings.updateFunctionConfiguration) - done() }) }) }) From f2c20c8ad639488ff2e523370b0995b8dd339822 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 14 Jun 2017 21:21:34 +0900 Subject: [PATCH 5/9] Modify _uploadNew returns Promise --- lib/main.js | 24 +++++++++++++----------- test/main.js | 6 ++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/main.js b/lib/main.js index 6b0c00e4..7e97f0e5 100644 --- a/lib/main.js +++ b/lib/main.js @@ -498,14 +498,17 @@ Lambda.prototype._uploadExisting = (lambda, params, cb) => { }) } -Lambda.prototype._uploadNew = (lambda, params, cb) => { - const request = lambda.createFunction(params, (err, data) => cb(err, data)) - request.on('retry', (response) => { - console.log(response.error.message) - console.log('=> Retrying') +Lambda.prototype._uploadNew = (lambda, params) => { + return new Promise((resolve, reject) => { + const request = lambda.createFunction(params, (err, data) => { + if (err) return reject(err) + resolve(data) + }) + request.on('retry', (response) => { + console.log(response.error.message) + console.log('=> Retrying') + }) }) - - return request } Lambda.prototype._readArchive = function (program, archiveCallback) { @@ -814,10 +817,7 @@ Lambda.prototype.deploy = function (program) { }, (err) => { if (err) { // Function does not exist - return _this._uploadNew(lambda, params, function (err, results) { - if (err) { - throw err - } + return _this._uploadNew(lambda, params).then((results) => { console.log('=> Zip file(s) done uploading. Results follow: ') console.log(results) @@ -852,6 +852,8 @@ Lambda.prototype.deploy = function (program) { }).catch((err) => { cb(err) }) + }).catch((err) => { + throw(err) }) } diff --git a/test/main.js b/test/main.js index 5347ab41..bde43e58 100644 --- a/test/main.js +++ b/test/main.js @@ -996,12 +996,10 @@ describe('lib/main', function () { }) describe('_uploadNew', () => { - it('simple test with mock', (done) => { + it('simple test with mock', () => { const params = lambda._params(program, null) - lambda._uploadNew(awsLambda, params, (err, results) => { - assert.isNull(err) + return lambda._uploadNew(awsLambda, params, (results) => { assert.deepEqual(results, lambdaMockSettings.createFunction) - done() }) }) }) From 83d6fc04424676b220a2f32b1250337c3b295130 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 14 Jun 2017 21:22:47 +0900 Subject: [PATCH 6/9] Space after "throw" --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index 7e97f0e5..2f2e0dc9 100644 --- a/lib/main.js +++ b/lib/main.js @@ -853,7 +853,7 @@ Lambda.prototype.deploy = function (program) { cb(err) }) }).catch((err) => { - throw(err) + throw err }) } From 9bf097e05f146137e3fdd2af179127701d99c9b6 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 15 Jun 2017 11:03:07 +0900 Subject: [PATCH 7/9] Modify _updateScheduleEvents returns Promise --- lib/main.js | 52 ++++++++++++++++++++-------------------------------- test/main.js | 41 ++++++++++++++++------------------------- 2 files changed, 36 insertions(+), 57 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2f2e0dc9..2d6368ff 100644 --- a/lib/main.js +++ b/lib/main.js @@ -705,9 +705,9 @@ Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourc }) } -Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleList, cb) => { +Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleList) => { if (scheduleList == null) { - return new Promise(resolve => cb(null, [])) + return Promise.resolve([]) } const paramsList = scheduleList.map((schedule) => @@ -722,9 +722,9 @@ Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleL // Since `scheduleEvents.add(params)` returns only `{}` if it succeeds // it is not very meaningful. // Therefore, return the params used for execution - cb(null, paramsList) + return Promise.resolve(paramsList) }).catch((err) => { - cb(err) + return Promise.reject(err) }) } @@ -836,24 +836,18 @@ Lambda.prototype.deploy = function (program) { } ) }), - new Promise((resolve, reject) => { - _this._updateScheduleEvents( - scheduleEvents, - results.FunctionArn, - eventSourceList.ScheduleEvents, - (err, results) => { - if (err) return reject(err) - resolve(results) - } - ) - }) + _this._updateScheduleEvents( + scheduleEvents, + results.FunctionArn, + eventSourceList.ScheduleEvents + ) ]).then((results) => { cb(null, results) }).catch((err) => { cb(err) }) }).catch((err) => { - throw err + return Promise.reject(err) }) } @@ -868,22 +862,16 @@ Lambda.prototype.deploy = function (program) { // This code is on its way to Promise. // From now on, callback will not be used. return Promise.all([ - new Promise((resolve, reject) => { - _this._uploadExisting(lambda, params).then((results) => { - console.log('=> Zip file(s) done uploading. Results follow: ') - console.log(results) - _this._updateScheduleEvents( - scheduleEvents, - results.FunctionArn, - eventSourceList.ScheduleEvents, - (err, results) => { - if (err) return reject(err) - resolve(results) - } - ) - }).catch((err) => { - reject(err) - }) + _this._uploadExisting(lambda, params).then((results) => { + console.log('=> Zip file(s) done uploading. Results follow: ') + console.log(results) + return _this._updateScheduleEvents( + scheduleEvents, + results.FunctionArn, + eventSourceList.ScheduleEvents + ) + }).catch((err) => { + return Promise.reject(err) }), new Promise((resolve, reject) => { diff --git a/test/main.js b/test/main.js index bde43e58..6bbe2f3b 100644 --- a/test/main.js +++ b/test/main.js @@ -956,41 +956,32 @@ describe('lib/main', function () { after(() => fs.unlinkSync('event_sources.json')) - it('program.eventSourceFile is empty value', (done) => { + it('program.eventSourceFile is empty value', () => { program.eventSourceFile = '' const eventSourceList = lambda._eventSourceList(program) - lambda._updateScheduleEvents( + return lambda._updateScheduleEvents( schedule, '', - eventSourceList.ScheduleEvents, - (err, results) => { - assert.isNull(err) - assert.deepEqual(results, []) - done() - } - ) + eventSourceList.ScheduleEvents + ).then((results) => { + assert.deepEqual(results, []) + }) }) it('simple test with mock', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) const functionArn = 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function' - return new Promise((resolve) => { - lambda._updateScheduleEvents( - schedule, - functionArn, - eventSourceList.ScheduleEvents, - (err, results) => resolve({ err: err, results: results }) - ) - }).then((actual) => { - const expected = { - err: null, - results: [Object.assign( - eventSourcesJsonValue.ScheduleEvents[0], - { FunctionArn: functionArn } - )] - } - assert.deepEqual(actual, expected) + return lambda._updateScheduleEvents( + schedule, + functionArn, + eventSourceList.ScheduleEvents + ).then((results) => { + const expected = [Object.assign( + eventSourcesJsonValue.ScheduleEvents[0], + { FunctionArn: functionArn } + )] + assert.deepEqual(results, expected) }) }) }) From ace14396c3ef36c27a1321854617e3f1d23419b7 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 15 Jun 2017 11:19:14 +0900 Subject: [PATCH 8/9] Modify _updateEventSources returns Promise --- lib/main.js | 45 +++++++++++------------------- test/main.js | 78 ++++++++++++++++++---------------------------------- 2 files changed, 43 insertions(+), 80 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2d6368ff..ca15e694 100644 --- a/lib/main.js +++ b/lib/main.js @@ -616,9 +616,9 @@ Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) { }) } -Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList, cb) => { +Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList) => { if (eventSourceList == null) { - return new Promise(resolve => cb(null, [])) + return Promise.resolve([]) } const updateEventSourceList = [] // Checking new and update event sources @@ -699,9 +699,9 @@ Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourc } return Promise.resolve() })).then((data) => { - cb(null, data) + return Promise.resolve(data) }).catch((err) => { - cb(err) + return Promise.reject(err) }) } @@ -824,18 +824,12 @@ Lambda.prototype.deploy = function (program) { // This code is on its way to Promise. // From now on, callback will not be used. return Promise.all([ - new Promise((resolve, reject) => { - _this._updateEventSources( - lambda, - params.FunctionName, - [], - eventSourceList.EventSourceMappings, - (err, results) => { - if (err) return reject(err) - resolve(results) - } - ) - }), + _this._updateEventSources( + lambda, + params.FunctionName, + [], + eventSourceList.EventSourceMappings + ), _this._updateScheduleEvents( scheduleEvents, results.FunctionArn, @@ -873,19 +867,12 @@ Lambda.prototype.deploy = function (program) { }).catch((err) => { return Promise.reject(err) }), - - new Promise((resolve, reject) => { - _this._updateEventSources( - lambda, - params.FunctionName, - existingEventSourceList, - eventSourceList.EventSourceMappings, - (err, results) => { - if (err) return reject(err) - resolve(results) - } - ) - }) + _this._updateEventSources( + lambda, + params.FunctionName, + existingEventSourceList, + eventSourceList.EventSourceMappings + ) ]).then((results) => { cb(null, results) }).catch((err) => { diff --git a/test/main.js b/test/main.js index 6bbe2f3b..a6700e7d 100644 --- a/test/main.js +++ b/test/main.js @@ -858,77 +858,53 @@ describe('lib/main', function () { after(() => fs.unlinkSync('event_sources.json')) - it('program.eventSourceFile is empty value', (done) => { + it('program.eventSourceFile is empty value', () => { program.eventSourceFile = '' const eventSourceList = lambda._eventSourceList(program) - lambda._updateEventSources( + return lambda._updateEventSources( awsLambda, '', [], - eventSourceList.EventSourceMappings, - (err, results) => { - assert.isNull(err) - assert.deepEqual(results, []) - done() - } - ) + eventSourceList.EventSourceMappings + ).then((results) => { + assert.deepEqual(results, []) + }) }) it('simple test with mock (In case of new addition)', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) - return new Promise((resolve) => { - lambda._updateEventSources( - awsLambda, - 'functionName', - [], - eventSourceList.EventSourceMappings, - (err, results) => resolve({ err: err, results: results }) - ) - }).then((actual) => { - const expected = { - err: null, - results: [lambdaMockSettings.createEventSourceMapping] - } - assert.deepEqual(actual, expected) + return lambda._updateEventSources( + awsLambda, + 'functionName', + [], + eventSourceList.EventSourceMappings + ).then((results) => { + assert.deepEqual(results, [lambdaMockSettings.createEventSourceMapping]) }) }) it('simple test with mock (In case of deletion)', () => { - return new Promise((resolve) => { - lambda._updateEventSources( - awsLambda, - 'functionName', - lambdaMockSettings.listEventSourceMappings.EventSourceMappings, - {}, - (err, results) => resolve({ err: err, results: results }) - ) - }).then((actual) => { - const expected = { - err: null, - results: [lambdaMockSettings.deleteEventSourceMapping] - } - assert.deepEqual(actual, expected) + return lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + {} + ).then((results) => { + assert.deepEqual(results, [lambdaMockSettings.deleteEventSourceMapping]) }) }) it('simple test with mock (In case of update)', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) - return new Promise((resolve) => { - lambda._updateEventSources( - awsLambda, - 'functionName', - lambdaMockSettings.listEventSourceMappings.EventSourceMappings, - eventSourceList.EventSourceMappings, - (err, results) => resolve({ err: err, results: results }) - ) - }).then((actual) => { - const expected = { - err: null, - results: [lambdaMockSettings.updateEventSourceMapping] - } - assert.deepEqual(actual, expected) + return lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + eventSourceList.EventSourceMappings + ).then((results) => { + assert.deepEqual(results, [lambdaMockSettings.updateEventSourceMapping]) }) }) }) From 950d207ca85f4ac1370079b03d459fa46dcad234 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 15 Jun 2017 11:29:18 +0900 Subject: [PATCH 9/9] Remove unnecessary arguments --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index ca15e694..7e5bc6e8 100644 --- a/lib/main.js +++ b/lib/main.js @@ -464,7 +464,7 @@ Lambda.prototype._setRunTimeEnvironmentVars = function (program) { } } -Lambda.prototype._uploadExisting = (lambda, params, cb) => { +Lambda.prototype._uploadExisting = (lambda, params) => { return new Promise((resolve, reject) => { const request = lambda.updateFunctionCode({ 'FunctionName': params.FunctionName,