This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
218 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
.idea | ||
.idea | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"use strict"; | ||
|
||
function process(sqsConfig, event, context) { | ||
// detect if it's an sqs-event at all: | ||
if (sqsConfig.debug) { | ||
console.log('sqs:Event', JSON.stringify(event)); | ||
console.log('sqs:context', context); | ||
} | ||
|
||
if (!Array.isArray(event.Records) || event.Records.length < 1 || event.Records[0].eventSource !== 'aws:sqs') { | ||
console.log('Event does not look like SQS'); | ||
return null; | ||
} | ||
|
||
const records = event.Records; | ||
const recordSourceArn = records[0].eventSourceARN; | ||
for (let routeConfig of sqsConfig.routes) { | ||
if (routeConfig.source instanceof RegExp) { | ||
if (routeConfig.source.test(recordSourceArn)) { | ||
const result = routeConfig.action(records.map(record => record.body) , context); | ||
return result || {}; | ||
} | ||
} else { | ||
if (routeConfig.source === recordSourceArn) { | ||
const result = routeConfig.action(records.map(record => record.body) , context); | ||
return result || {}; | ||
} | ||
} | ||
} | ||
|
||
if (sqsConfig.debug) { | ||
console.log(`No source-match for ${recordSourceArn}`); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
module.exports = process; | ||
|
||
/* | ||
const cfgExample = { | ||
routes:[ | ||
{ | ||
source: /.*\/, | ||
action: (record, context) => service.import(JSON.parse(record.body), context) | ||
} | ||
] | ||
}; | ||
*/ | ||
|
||
|
||
/* this is an example for a standard SQS notification message: | ||
{ | ||
"Records": [ | ||
{ | ||
"messageId": "c80e8021-a70a-42c7-a470-796e1186f753", | ||
"receiptHandle": "AQEBJQ+/u6NsnT5t8Q/VbVxgdUl4TMKZ5FqhksRdIQvLBhwNvADoBxYSOVeCBXdnS9P+erlTtwEALHsnBXynkfPLH3BOUqmgzP25U8kl8eHzq6RAlzrSOfTO8ox9dcp6GLmW33YjO3zkq5VRYyQlJgLCiAZUpY2D4UQcE5D1Vm8RoKfbE+xtVaOctYeINjaQJ1u3mWx9T7tork3uAlOe1uyFjCWU5aPX/1OHhWCGi2EPPZj6vchNqDOJC/Y2k1gkivqCjz1CZl6FlZ7UVPOx3AMoszPuOYZ+Nuqpx2uCE2MHTtMHD8PVjlsWirt56oUr6JPp9aRGo6bitPIOmi4dX0FmuMKD6u/JnuZCp+AXtJVTmSHS8IXt/twsKU7A+fiMK01NtD5msNgVPoe9JbFtlGwvTQ==", | ||
"body": "{\"foo\":\"bar\"}", | ||
"attributes": { | ||
"ApproximateReceiveCount": "3", | ||
"SentTimestamp": "1529104986221", | ||
"SenderId": "594035263019", | ||
"ApproximateFirstReceiveTimestamp": "1529104986230" | ||
}, | ||
"messageAttributes": {}, | ||
"md5OfBody": "9bb58f26192e4ba00f01e2e7b136bbd8", | ||
"eventSource": "aws:sqs", | ||
"eventSourceARN": "arn:aws:sqs:eu-central-1:594035263019:article-import", | ||
"awsRegion": "eu-central-1" | ||
} | ||
] | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"use strict"; | ||
|
||
describe('sqs.processor', () => { | ||
|
||
const sqs = require('../lib/sqs'); | ||
|
||
it('context should be passed through', () => { | ||
const actionSpy = jasmine.createSpy('action'); | ||
|
||
const context = {bla: "blup"}; | ||
const sqsCfg = {routes: [{source: /.*/, action: actionSpy}]}; | ||
const event = {Records: [{eventSource: 'aws:sqs', body: 'B'}]}; | ||
|
||
sqs(sqsCfg, event, context); | ||
|
||
expect(actionSpy).toHaveBeenCalledWith([event.Records[0].body], context); | ||
}); | ||
|
||
it('should ignore event if it is no SQS event', () => { | ||
const sqsCfg = {routes: [{source: /.*/, action: () => 1}]}; | ||
expect(sqs(sqsCfg, {})).toBe(null); | ||
expect(sqs(sqsCfg, {Records: 1})).toBe(null); | ||
expect(sqs(sqsCfg, {Records: []})).toBe(null); | ||
}); | ||
|
||
it('should match null source for ".*"', () => { | ||
const sqsCfg = {routes: [{source: /.*/, action: () => 1}]}; | ||
expect(sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', eventSourceARN: null}]})).toBe(1); | ||
}); | ||
|
||
it('should match empty subject for ".*"', () => { | ||
const sqsCfg = {routes: [{subject: /.*/, action: () => 1}]}; | ||
expect(sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', body: 'B'}]})).toBe(1); | ||
}); | ||
|
||
it('should match source for "/porter/"', () => { | ||
const sqsCfg = {routes: [{source: /porter/, action: () => 1}]}; | ||
expect(sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', eventSourceARN: 'importer'}]})).toBe(1); | ||
}); | ||
|
||
it('should call action with sqs-message', () => { | ||
const sqsCfg = {routes: [{source: /porter/, action: (events) => events}]}; | ||
const event = {Records: [{eventSource: 'aws:sqs', eventSourceARN: 'importer', body: 'B'}]}; | ||
|
||
expect(sqs(sqsCfg, event)).toEqual([event.Records[0].body]); | ||
}); | ||
|
||
it('should call first action with matching subject', () => { | ||
const sqsCfg = { | ||
routes: [ | ||
{source: /^123$/, action: () => 1}, | ||
{source: /123/, action: () => 2}, | ||
{source: /1234/, action: () => 3} | ||
] | ||
}; | ||
const event = {Records: [{eventSource: 'aws:sqs', eventSourceARN: '1234', body: 'B'}]}; | ||
expect(sqs(sqsCfg, event)).toBe(2); | ||
}); | ||
|
||
it('should match complete source', () => { | ||
const sqsCfg = {routes: [{source: 'aws:123:importer', action: () => 1}]}; | ||
expect(sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', eventSourceARN: 'aws:123:importer'}]})).toBe(1); | ||
}); | ||
|
||
it('should not throw error on missing source', () => { | ||
const sqsCfg = {routes: [{action: () => 1}]}; | ||
sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', eventSourceARN: 'importer'}]}); | ||
}); | ||
|
||
it('should fail on missing action', () => { | ||
const sqsCfg = {routes: [{source: /.*/}]}; | ||
try { | ||
sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', eventSourceARN: 'importer'}]}); | ||
fail(); | ||
} catch (e) { | ||
} | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1241,13 +1241,6 @@ jasmine-core@~3.2.0: | |
version "3.2.1" | ||
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-3.2.1.tgz#8e4ff5b861603ee83343f2b49eee6a0ffe9650ce" | ||
|
||
[email protected]: | ||
version "2.3.2" | ||
resolved "https://registry.yarnpkg.com/jasmine-reporters/-/jasmine-reporters-2.3.2.tgz#898818ffc234eb8b3f635d693de4586f95548d43" | ||
dependencies: | ||
mkdirp "^0.5.1" | ||
xmldom "^0.1.22" | ||
|
||
jasmine-terminal-reporter@^1.0.3: | ||
version "1.0.3" | ||
resolved "https://registry.yarnpkg.com/jasmine-terminal-reporter/-/jasmine-terminal-reporter-1.0.3.tgz#896f1ec8fdf4bf6aecdd41c503eda7347f61526b" | ||
|
@@ -2478,10 +2471,6 @@ wrappy@1: | |
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" | ||
|
||
xmldom@^0.1.22: | ||
version "0.1.27" | ||
resolved "https://registry.yarnpkg.com/xmldom/-/xmldom-0.1.27.tgz#d501f97b3bdb403af8ef9ecc20573187aadac0e9" | ||
|
||
xtend@~4.0.0, xtend@~4.0.1: | ||
version "4.0.1" | ||
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af" | ||
|