Skip to content

Commit 343087d

Browse files
authored
Now supporting default exported handlers (#192)
- added a test case for default exported handlers - introduced a fallback to use the default exported handler if the handler reference does not exist in the imported handler file (thank you @renejesusgv for bringing this to my attention)
1 parent 153c9e0 commit 343087d

File tree

3 files changed

+22
-60
lines changed

3 files changed

+22
-60
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ class ServerlessOfflineSns {
511511
const handlerFnName = fn.handler.substring(handlerFnNameIndex + 1);
512512
const fullHandlerPath = resolve(location, handlerPath);
513513
const handlers = await import(`${url.pathToFileURL(fullHandlerPath)}.js`);
514-
return handlers[handlerFnName];
514+
return handlers[handlerFnName] || handlers.default[handlerFnName];
515515
}
516516

517517
public log(msg, prefix = "INFO[serverless-offline-sns]: ") {

test/mock/handler.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ export const asyncHandler = async (evt, ctx) => {
2929
setResult(evt.Records[0].Sns.TopicArn);
3030
return "{}";
3131
};
32+
33+
const defaultExportHandler = (evt, ctx, cb) => {
34+
nPongs += 1;
35+
setPongs(nPongs);
36+
setEvent(evt);
37+
cb(null, "{}");
38+
};
39+
40+
export default { defaultExportHandler };

test/spec/sns.ts

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,6 @@ describe("test", () => {
3636
await plugin.hooks["offline-sns:start:end"]();
3737
});
3838

39-
it('should send event to topic ARN', async () => {
40-
plugin = new ServerlessOfflineSns(createServerless(accountId));
41-
const snsAdapter = await plugin.start();
42-
await snsAdapter.publish(
43-
`arn:aws:sns:us-east-1:${accountId}:test-topic`,
44-
"{}"
45-
);
46-
await new Promise((res) => setTimeout(res, 500));
47-
expect(state.getPongs()).to.eq(2);
48-
});
49-
5039
it('should send event to topic ARN', async () => {
5140
plugin = new ServerlessOfflineSns(createServerless(accountId));
5241
const snsAdapter = await plugin.start();
@@ -237,11 +226,20 @@ describe("test", () => {
237226
await new Promise((res) => setTimeout(res, 100));
238227
expect(state.getPongs()).to.eq(1);
239228
});
229+
230+
it('should support commonjs/default handlers', async () => {
231+
plugin = new ServerlessOfflineSns(createServerless(accountId, "defaultExportHandler"));
232+
const snsAdapter = await plugin.start();
233+
await snsAdapter.publish(
234+
`arn:aws:sns:us-east-1:${accountId}:test-topic`,
235+
"{}"
236+
);
237+
await new Promise((res) => setTimeout(res, 100));
238+
expect(state.getPongs()).to.eq(2);
239+
});
240240

241241
it('should support async handlers with no callback', async () => {
242-
plugin = new ServerlessOfflineSns(createServerless(accountId, "asyncHandler"), {
243-
skipCacheInvalidation: true,
244-
});
242+
plugin = new ServerlessOfflineSns(createServerless(accountId, "asyncHandler"));
245243
const snsAdapter = await plugin.start();
246244
await snsAdapter.publish(
247245
`arn:aws:sns:us-east-1:${accountId}:test-topic-async`,
@@ -547,51 +545,6 @@ const createServerless = (
547545
};
548546
};
549547

550-
const createServerlessCacheInvalidation = (
551-
accountId: number,
552-
handlerName: string = "pongHandler",
553-
host = null
554-
) => {
555-
return {
556-
config: {},
557-
service: {
558-
custom: {
559-
"serverless-offline-sns": {
560-
debug: true,
561-
port: 4002,
562-
accountId,
563-
host,
564-
invalidateCache: true,
565-
},
566-
},
567-
provider: {
568-
region: "us-east-1",
569-
environment: {
570-
MY_VAR: "MY_VAL",
571-
},
572-
},
573-
functions: {
574-
pong: {
575-
handler: "test/mock/handler." + handlerName,
576-
events: [
577-
{
578-
sns: `arn:aws:sns:us-west-2:${accountId}:test-topic`,
579-
},
580-
],
581-
},
582-
},
583-
resources: {},
584-
},
585-
cli: {
586-
log: (data) => {
587-
if (process.env.DEBUG) {
588-
console.log(data);
589-
}
590-
},
591-
},
592-
};
593-
};
594-
595548
const createServerlessMultiDot = (
596549
accountId: number,
597550
handlerName: string = "pongHandler",

0 commit comments

Comments
 (0)