Skip to content

Commit e527962

Browse files
abetomoDeviaVir
authored andcommitted
Changed the format of event_sources.json (#226)
* Fix to use authenticated `aws` object in main.js * Fix to enable ScheduleEvents to be specified in event_sources.json * Supports new event_sources.json format * modify default value to new format
1 parent 68b9569 commit e527962

File tree

3 files changed

+121
-25
lines changed

3 files changed

+121
-25
lines changed

lib/event_sources.json.example

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
[
1+
{
2+
"EventSourceMappings": [
23
{
3-
"EventSourceArn": "your event source arn",
4-
"StartingPosition": "LATEST",
5-
"BatchSize": 100,
6-
"Enabled": true
4+
"EventSourceArn": "your event source arn",
5+
"StartingPosition": "LATEST",
6+
"BatchSize": 100,
7+
"Enabled": true
78
}
8-
]
9+
],
10+
"ScheduleEvents": [
11+
{
12+
"FunctionArnPrefix": "arn:aws:lambda:us-west-2:XXX:function:",
13+
"ScheduleName": "node-lambda-test-schedule",
14+
"ScheduleState": "ENABLED",
15+
"ScheduleExpression": "rate(1 hour)"
16+
}
17+
]
18+
}

lib/main.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,30 @@ Lambda.prototype._params = function (program, buffer) {
145145

146146
Lambda.prototype._eventSourceList = function (program) {
147147
if (!program.eventSourceFile) {
148-
return [];
148+
return {
149+
EventSourceMappings: [],
150+
ScheduleEvents: []
151+
};
149152
}
150-
try {
151-
return fs.readJsonSync(program.eventSourceFile);
152-
} catch(err) {
153-
throw err;
153+
const list = (function() {
154+
try {
155+
return fs.readJsonSync(program.eventSourceFile);
156+
} catch(err) {
157+
throw err;
158+
}
159+
})();
160+
161+
if (Object.prototype.toString.call(list) === '[object Array]') {
162+
// backward-compatible
163+
return { EventSourceMappings: list };
164+
}
165+
if (!list.EventSourceMappings) {
166+
list.EventSourceMappings = [];
167+
}
168+
if (!list.ScheduleEvents) {
169+
list.ScheduleEvents = [];
154170
}
171+
return list;
155172
};
156173

157174
/**
@@ -607,7 +624,7 @@ Lambda.prototype.deploy = function (program) {
607624
console.log(results);
608625

609626
// Updating event source(s)
610-
_this._updateEventSources(lambda, params.FunctionName, [], eventSourceList, function(err, results) {
627+
_this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function(err, results) {
611628
cb(null, results);
612629
});
613630
});
@@ -632,7 +649,7 @@ Lambda.prototype.deploy = function (program) {
632649
});
633650
},
634651
function(_callback) {
635-
_this._updateEventSources(lambda, params.FunctionName, existingEventSourceList, eventSourceList, function(err, results) {
652+
_this._updateEventSources(lambda, params.FunctionName, existingEventSourceList, eventSourceList.EventSourceMappings, function(err, results) {
636653
_callback(err, results);
637654
});
638655
}

test/main.js

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -507,18 +507,10 @@ describe('node-lambda', function () {
507507
describe('_eventSourceList', function () {
508508
it('program.eventSourceFile is empty value', function () {
509509
program.eventSourceFile = '';
510-
assert.deepEqual(lambda._eventSourceList(program), []);
511-
});
512-
513-
it('program.eventSourceFile is valid value', function () {
514-
program.eventSourceFile = 'event_sources.json';
515-
const expected = [{
516-
BatchSize: 100,
517-
Enabled: true,
518-
EventSourceArn: 'your event source arn',
519-
StartingPosition: 'LATEST',
520-
}];
521-
assert.deepEqual(lambda._eventSourceList(program), expected);
510+
assert.deepEqual(
511+
lambda._eventSourceList(program),
512+
{ EventSourceMappings: [], ScheduleEvents: [] }
513+
);
522514
});
523515

524516
it('program.eventSourceFile is invalid value', function () {
@@ -529,6 +521,83 @@ describe('node-lambda', function () {
529521
"ENOENT: no such file or directory, open '/hoge/fuga'"
530522
);
531523
});
524+
525+
describe('program.eventSourceFile is valid value', function() {
526+
before(function () {
527+
fs.writeFileSync('only_EventSourceMappings.json', JSON.stringify({
528+
EventSourceMappings: [{ test: 1 }]
529+
}));
530+
fs.writeFileSync('only_ScheduleEvents.json', JSON.stringify({
531+
ScheduleEvents: [{ test: 2 }]
532+
}));
533+
});
534+
535+
after(function () {
536+
fs.unlinkSync('only_EventSourceMappings.json');
537+
fs.unlinkSync('only_ScheduleEvents.json');
538+
});
539+
540+
it('only EventSourceMappings', function () {
541+
program.eventSourceFile = 'only_EventSourceMappings.json';
542+
const expected = {
543+
EventSourceMappings: [{ test: 1 }],
544+
ScheduleEvents: [],
545+
};
546+
assert.deepEqual(lambda._eventSourceList(program), expected);
547+
});
548+
549+
it('only ScheduleEvents', function () {
550+
program.eventSourceFile = 'only_ScheduleEvents.json';
551+
const expected = {
552+
EventSourceMappings: [],
553+
ScheduleEvents: [{ test: 2 }],
554+
};
555+
assert.deepEqual(lambda._eventSourceList(program), expected);
556+
});
557+
558+
it('EventSourceMappings & ScheduleEvents', function () {
559+
program.eventSourceFile = 'event_sources.json';
560+
const expected = {
561+
EventSourceMappings: [{
562+
BatchSize: 100,
563+
Enabled: true,
564+
EventSourceArn: 'your event source arn',
565+
StartingPosition: 'LATEST',
566+
}],
567+
ScheduleEvents: [{
568+
FunctionArnPrefix: 'arn:aws:lambda:us-west-2:XXX:function:',
569+
ScheduleName: 'node-lambda-test-schedule',
570+
ScheduleState: 'ENABLED',
571+
ScheduleExpression: 'rate(1 hour)',
572+
}],
573+
};
574+
assert.deepEqual(lambda._eventSourceList(program), expected);
575+
});
576+
});
577+
578+
describe('old style event_sources.json', function () {
579+
const oldStyleValue = [{
580+
BatchSize: 100,
581+
Enabled: true,
582+
EventSourceArn: 'your event source arn',
583+
StartingPosition: 'LATEST',
584+
}];
585+
const fileName = 'event_sources_old_style.json';
586+
587+
before(function () {
588+
fs.writeFileSync(fileName, JSON.stringify(oldStyleValue));
589+
});
590+
591+
after(function () {
592+
fs.unlinkSync(fileName);
593+
});
594+
595+
it('program.eventSourceFile is valid value', function () {
596+
program.eventSourceFile = fileName;
597+
const expected = { EventSourceMappings: oldStyleValue };
598+
assert.deepEqual(lambda._eventSourceList(program), expected);
599+
});
600+
});
532601
});
533602
});
534603

0 commit comments

Comments
 (0)