Skip to content

Commit 13b8a7a

Browse files
author
cgohlke
committed
cleanup $ refactoring
1 parent fb59ec7 commit 13b8a7a

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22

33
function handler(routeConfig) {
4-
const processorMapping = extractProcessorMapping(routeConfig);
4+
const eventProcessorMapping = extractEventProcessorMapping(routeConfig);
55
return (event, context, callback) => {
6-
for (const processorName of processorMapping.keys()) {
6+
for (const eventProcessorName of eventProcessorMapping.keys()) {
77

88
try {
99
// the contract of 'processors' is as follows:
@@ -13,7 +13,7 @@ function handler(routeConfig) {
1313
// - throws Error: the 'error.toString()' is taken as the error message of processing the event
1414
// - returns object: this is taken as the result of processing the event
1515
// - returns promise: when the promise is resolved, this is taken as the result of processing the event
16-
const result = processorMapping.get(processorName)(routeConfig[processorName], event);
16+
const result = eventProcessorMapping.get(eventProcessorName)(routeConfig[eventProcessorName], event);
1717
if (result) {
1818
// be resilient against a processor returning a value instead of a promise:
1919
return Promise.resolve(result)
@@ -31,17 +31,17 @@ function handler(routeConfig) {
3131
return;
3232
}
3333
}
34-
callback('No processor found to handle this kind of event!');
34+
callback('No event processor found to handle this kind of event!');
3535
}
3636
}
3737

38-
function extractProcessorMapping(routeConfig) {
38+
function extractEventProcessorMapping(routeConfig) {
3939
const processorMap = new Map();
4040
for (let key of Object.keys(routeConfig)) {
4141
try {
4242
processorMap.set(key, require(`./lib/${key}`));
4343
} catch (error) {
44-
throw new Error(`The processor '${key}', that is mentioned in the routerConfig, cannot be instantiated (${error.toString()})`);
44+
throw new Error(`The event processor '${key}', that is mentioned in the routerConfig, cannot be instantiated (${error.toString()})`);
4545
}
4646
}
4747
return processorMap;

lib/proxyIntegration.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ const NO_MATCHING_ACTION = request => {
77
}
88
};
99

10-
function route(proxyIntegrationConfig, event) {
10+
function process(proxyIntegrationConfig, event) {
11+
//validate config
12+
if (!Array.isArray(proxyIntegrationConfig.routes) || proxyIntegrationConfig.routes.length < 1) {
13+
throw new Error('proxyIntegration.routes must not be empty');
14+
}
15+
1116
// detect if it's an http-call at all:
12-
if (!event.httpMethod) {
17+
if (!event.httpMethod || !event.path) {
1318
return null;
1419
}
1520
const headers = Object.assign({
@@ -18,19 +23,12 @@ function route(proxyIntegrationConfig, event) {
1823
if (proxyIntegrationConfig.cors) {
1924
headers["Access-Control-Allow-Origin"] = "*";
2025
}
26+
2127
// assure necessary values have sane defaults:
22-
event.path = event.path || '';
2328
const errorMapping = proxyIntegrationConfig.errorMapping || {};
2429
errorMapping['NO_MATCHING_ACTION'] = 404;
25-
proxyIntegrationConfig.routes = proxyIntegrationConfig.routes || [];
26-
// ugly hack: if host is from 'Custom Domain Name Mapping', then event.path has the value '/basepath/resource-path/';
27-
// if host is from amazonaws.com, then event.path is just '/resource-path':
28-
const apiId = event.requestContext ? event.requestContext.apiId : null; // the apiId that is the first part of the amazonaws.com-host
29-
if ((apiId && event.headers && event.headers.Host && event.headers.Host.substring(0, apiId.length) != apiId)) {
30-
// remove first path element:
31-
const groups = /\/[^\/]+(.*)/.exec(event.path) || [null, null];
32-
event.path = groups[1] || '/';
33-
}
30+
31+
event.path = normalizeRequestPath(event);
3432

3533
try {
3634
const actionConfig = findMatchingActionConfig(event.httpMethod, event.path, proxyIntegrationConfig) || {action: NO_MATCHING_ACTION};
@@ -49,6 +47,18 @@ function route(proxyIntegrationConfig, event) {
4947
}
5048
}
5149

50+
function normalizeRequestPath(event) {
51+
// ugly hack: if host is from API-Gateway 'Custom Domain Name Mapping', then event.path has the value '/basepath/resource-path/';
52+
// if host is from amazonaws.com, then event.path is just '/resource-path':
53+
const apiId = event.requestContext ? event.requestContext.apiId : null; // the apiId that is the first part of the amazonaws.com-host
54+
if ((apiId && event.headers && event.headers.Host && event.headers.Host.substring(0, apiId.length) != apiId)) {
55+
// remove first path element:
56+
const groups = /\/[^\/]+(.*)/.exec(event.path) || [null, null];
57+
return groups[1] || '/';
58+
}
59+
60+
return event.path;
61+
}
5262

5363
function convertError(error, errorMapping, headers) {
5464
if (typeof error.reason === 'string' && typeof error.message === 'string' && errorMapping && errorMapping[error.reason]) {
@@ -95,4 +105,4 @@ function extractPathNames(pathExpression) {
95105
return pathNames && pathNames.length > 0 ? pathNames.slice(1) : null;
96106
}
97107

98-
module.exports = route;
108+
module.exports = process;

lib/sns.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
"use strict";
22

3-
function route(snsConfig, event) {
3+
function process(snsConfig, event) {
44
// detect if it's an sns-event at all:
55
if(snsConfig.debug){
66
console.log('sns:Event', JSON.stringify(event));
77
}
8+
89
if (!Array.isArray(event.Records) || event.Records.length<1 || !event.Records[0].Sns) {
910
console.log('Event does not look like SNS');
1011
return null;
1112
}
13+
1214
const sns = event.Records[0].Sns;
1315
for(let routeConfig of snsConfig.routes){
1416
if(routeConfig.subject instanceof RegExp){
@@ -20,13 +22,15 @@ function route(snsConfig, event) {
2022
console.log(`SNS-Route with subject-regex '${routeConfig.subject}' is not a Regex; it is ignored!`);
2123
}
2224
}
25+
2326
if (snsConfig.debug) {
2427
console.log(`No subject-match for ${sns.Subject}`);
2528
}
29+
2630
return null;
2731
}
2832

29-
module.exports = route;
33+
module.exports = process;
3034

3135
/*
3236
const cfgExample = {

test/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('processor.configuration', () => {
3636
lib1: {},
3737
lib2: {}
3838
})({}, {}, cb);
39-
expect(cb).toHaveBeenCalledWith(jasmine.stringMatching(/^No processor found/));
39+
expect(cb).toHaveBeenCalledWith(jasmine.stringMatching(/^No event processor found/));
4040
});
4141

4242
});

test/proxyIntegration.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ describe('proxyIntegration.routeHandler', () => {
183183
paths: {}
184184
});
185185
});
186-
it('should return error for no route found', (done) => {
187-
proxyIntegration({routes: []}, {httpMethod: 'GET'}).then(result => {
186+
it('should return error for no process found', (done) => {
187+
proxyIntegration({routes: [{}]}, {httpMethod: 'GET', path: '/'}).then(result => {
188188
expect(result).toEqual({
189189
statusCode: 404,
190190
body: jasmine.stringMatching(/Could not find/),
@@ -194,7 +194,7 @@ describe('proxyIntegration.routeHandler', () => {
194194
});
195195
});
196196
it('should return null if it is not an http request', () => {
197-
const result = proxyIntegration({routes: []}, {});
197+
const result = proxyIntegration({routes: [{}]}, {});
198198
expect(result).toBe(null);
199199
});
200200
forEach([

0 commit comments

Comments
 (0)