Skip to content
46 changes: 45 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ Lambda.prototype.run = function (program) {
const event = require(path.join(process.cwd(), program.eventFile))
const context = require(path.join(process.cwd(), program.contextFile))

this._runHandler(handler, event, program, context)
if (Object.prototype.toString.call(event) !== '[object Array]') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not Array.isArray()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I'll try!

return this._runHandler(handler, event, program, context)
}
this._runMultipleHandlers(event)
}

Lambda.prototype._runHandler = (handler, event, program, context) => {
Expand Down Expand Up @@ -95,6 +98,47 @@ Lambda.prototype._runHandler = (handler, event, program, context) => {
handler(event, context, callback)
}

Lambda.prototype._runMultipleHandlers = (events) => {
console.log(`!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Usually you will receive a single Object from AWS Lambda.
We added support for event.json to contain an array,
so you can easily test run multiple events.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
`)

const _argv = process.argv
const eventFileOptionIndex = (() => {
const index = _argv.indexOf('-j')
if (index >= 0) return index
return _argv.indexOf('--eventFile')
})()

// In order to reproduce the logic of callbackWaitsForEmptyEventLoop,
// we are going to execute `node-lambda run`.
events.forEach((event, i) => {
const tmpEventFile = `.${i}_tmp_event.json`
const command = () => {
if (eventFileOptionIndex === -1) {
return _argv.concat(['-j', tmpEventFile]).join(' ')
}
_argv[eventFileOptionIndex + 1] = tmpEventFile
return _argv.join(' ')
}

fs.writeFileSync(tmpEventFile, JSON.stringify(event))
exec(command(), {
maxBuffer: maxBufferSize,
env: process.env
}, (err, stdout, stderr) => {
console.log('>>> Event:', event, '<<<')
if (err) console.error(err)
console.log(stdout)
console.log(stderr)
fs.unlinkSync(tmpEventFile)
})
})
}

Lambda.prototype._params = function (program, buffer) {
var params = {
FunctionName: program.functionName +
Expand Down
31 changes: 29 additions & 2 deletions test/node-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('bin/node-lambda', () => {
'--handler', '__test.handler',
'--eventFile', 'event.json'
])
var stdoutString = ''
var stderrString = ''
let stdoutString = ''
let stderrString = ''
run.stdout.on('data', (data) => {
stdoutString += data.toString().replace(/\r|\n/g, '')
})
Expand Down Expand Up @@ -169,5 +169,32 @@ describe('bin/node-lambda', () => {
}, done)
})
})

describe('node-lambda run (Multiple events))', () => {
const eventObj = [{
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
callbackCode: 'callback(null);',
no: 1
}, {
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
callbackCode: 'callback(null);',
no: 2
}, {
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
callbackCode: 'callback(null);',
no: 3
}]

it('`node-lambda run` exitCode is `0`', (done) => {
_generateEventFile(eventObj)
_testMain({
stdoutRegExp: / no: [123] .+ no: [123] .+ no: [123] .+Success:/,
exitCode: 0
}, done)
})
})
})
})