Skip to content

Commit

Permalink
Now supporting default exported handlers (#192)
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
dsarlo authored Nov 20, 2023
1 parent 153c9e0 commit 343087d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ class ServerlessOfflineSns {
const handlerFnName = fn.handler.substring(handlerFnNameIndex + 1);
const fullHandlerPath = resolve(location, handlerPath);
const handlers = await import(`${url.pathToFileURL(fullHandlerPath)}.js`);
return handlers[handlerFnName];
return handlers[handlerFnName] || handlers.default[handlerFnName];
}

public log(msg, prefix = "INFO[serverless-offline-sns]: ") {
Expand Down
9 changes: 9 additions & 0 deletions test/mock/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ export const asyncHandler = async (evt, ctx) => {
setResult(evt.Records[0].Sns.TopicArn);
return "{}";
};

const defaultExportHandler = (evt, ctx, cb) => {
nPongs += 1;
setPongs(nPongs);
setEvent(evt);
cb(null, "{}");
};

export default { defaultExportHandler };
71 changes: 12 additions & 59 deletions test/spec/sns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ describe("test", () => {
await plugin.hooks["offline-sns:start:end"]();
});

it('should send event to topic ARN', async () => {
plugin = new ServerlessOfflineSns(createServerless(accountId));
const snsAdapter = await plugin.start();
await snsAdapter.publish(
`arn:aws:sns:us-east-1:${accountId}:test-topic`,
"{}"
);
await new Promise((res) => setTimeout(res, 500));
expect(state.getPongs()).to.eq(2);
});

it('should send event to topic ARN', async () => {
plugin = new ServerlessOfflineSns(createServerless(accountId));
const snsAdapter = await plugin.start();
Expand Down Expand Up @@ -237,11 +226,20 @@ describe("test", () => {
await new Promise((res) => setTimeout(res, 100));
expect(state.getPongs()).to.eq(1);
});

it('should support commonjs/default handlers', async () => {
plugin = new ServerlessOfflineSns(createServerless(accountId, "defaultExportHandler"));
const snsAdapter = await plugin.start();
await snsAdapter.publish(
`arn:aws:sns:us-east-1:${accountId}:test-topic`,
"{}"
);
await new Promise((res) => setTimeout(res, 100));
expect(state.getPongs()).to.eq(2);
});

it('should support async handlers with no callback', async () => {
plugin = new ServerlessOfflineSns(createServerless(accountId, "asyncHandler"), {
skipCacheInvalidation: true,
});
plugin = new ServerlessOfflineSns(createServerless(accountId, "asyncHandler"));
const snsAdapter = await plugin.start();
await snsAdapter.publish(
`arn:aws:sns:us-east-1:${accountId}:test-topic-async`,
Expand Down Expand Up @@ -547,51 +545,6 @@ const createServerless = (
};
};

const createServerlessCacheInvalidation = (
accountId: number,
handlerName: string = "pongHandler",
host = null
) => {
return {
config: {},
service: {
custom: {
"serverless-offline-sns": {
debug: true,
port: 4002,
accountId,
host,
invalidateCache: true,
},
},
provider: {
region: "us-east-1",
environment: {
MY_VAR: "MY_VAL",
},
},
functions: {
pong: {
handler: "test/mock/handler." + handlerName,
events: [
{
sns: `arn:aws:sns:us-west-2:${accountId}:test-topic`,
},
],
},
},
resources: {},
},
cli: {
log: (data) => {
if (process.env.DEBUG) {
console.log(data);
}
},
},
};
};

const createServerlessMultiDot = (
accountId: number,
handlerName: string = "pongHandler",
Expand Down

0 comments on commit 343087d

Please sign in to comment.