Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index.ts #213

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,15 @@ class ServerlessOfflineSns {
);
}

this.log(`Creating topic: "${topicName}" for fn "${fnName}"`);
const data = await this.snsAdapter.createTopic(topicName);
const { Topics } = await this.snsAdapter.listTopics();

let data = Topics.find((sub) => sub.TopicArn.split(":").pop() === topicName);

if(!data || !data.TopicArn){
this.log(`Creating topic: "${topicName}" for fn "${fnName}"`);
data = await this.snsAdapter.createTopic(topicName);
}

this.debug("topic: " + JSON.stringify(data));
const handler = await this.createHandler(fnName, fn, lambdasLocation);
await this.snsAdapter.subscribe(
Expand Down Expand Up @@ -566,4 +573,4 @@ class ServerlessOfflineSns {
}
}

export default ServerlessOfflineSns;
export default ServerlessOfflineSns;
28 changes: 17 additions & 11 deletions test/spec/sns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("test", () => {
"{}"
);
await new Promise((res) => setTimeout(res, 100));
expect(state.getPongs()).to.eq(2);
expect(state.getPongs()).to.eq(1);
});

it("should send event to target ARN", async () => {
Expand All @@ -54,7 +54,7 @@ describe("test", () => {
"{}"
);
await new Promise((res) => setTimeout(res, 100));
expect(state.getPongs()).to.eq(2);
expect(state.getPongs()).to.eq(1);
});

it("should send event with pseudo parameters", async () => {
Expand All @@ -65,7 +65,7 @@ describe("test", () => {
"{}"
);
await new Promise((res) => setTimeout(res, 100));
expect(state.getPongs()).to.eq(2);
expect(state.getPongs()).to.eq(1);
});

it("should send event with MessageAttributes and subject", async () => {
Expand Down Expand Up @@ -184,7 +184,7 @@ describe("test", () => {
`arn:aws:sns:us-east-1:${accountId}:test-topic`,
"{}"
);
expect(await state.getResult()).to.eq("MY_VAL");
expect(await state.getResult()).to.eq(`arn:aws:sns:us-east-1:${accountId}:test-topic`);
});

it("should read env variable for function", async () => {
Expand All @@ -196,7 +196,7 @@ describe("test", () => {
`arn:aws:sns:us-east-1:${accountId}:test-topic-2`,
"{}"
);
expect(await state.getResult()).to.eq("TEST");
expect(await state.getResult()).to.eq(`arn:aws:sns:us-east-1:${accountId}:test-topic-2`);
});

it("should convert pseudo param on load", async () => {
Expand Down Expand Up @@ -232,7 +232,7 @@ describe("test", () => {
"{}"
);
await new Promise((res) => setTimeout(res, 100));
expect(state.getPongs()).to.eq(2);
expect(state.getPongs()).to.eq(1);
});

it('should support async handlers with no callback', async () => {
Expand Down Expand Up @@ -428,7 +428,7 @@ const createServerless = (
provider: {
region: "us-east-1",
environment: {
MY_VAR: "MY_VAL",
MY_VAR: `arn:aws:sns:us-east-1:${accountId}:test-topic`,
},
},
functions: {
Expand All @@ -437,7 +437,7 @@ const createServerless = (
handler: "test/mock/handler." + handlerName,
events: [
{
sns: `arn:aws:sns:us-east-1:${accountId}:test-topic`,
sns: `arn:aws:sns:us-east-1:${accountId}:test-topic-async`,
},
],
},
Expand All @@ -456,7 +456,7 @@ const createServerless = (
name: "this-is-auto-created-when-using-serverless",
handler: "test/mock/handler." + handlerName,
environment: {
MY_VAR: "TEST",
MY_VAR: `arn:aws:sns:us-east-1:${accountId}:test-topic-2`,
},
events: [
{
Expand All @@ -478,6 +478,9 @@ const createServerless = (
},
pong5: {
handler: "test/mock/handler." + handlerName,
environment: {
MY_VAR: `arn:aws:sns:us-east-1:#{AWS::AccountId}:test-topic-async`,
},
events: [
{
sns: {
Expand Down Expand Up @@ -558,7 +561,7 @@ const createServerlessMultiDot = (
provider: {
region: "us-east-1",
environment: {
MY_VAR: "MY_VAL",
MY_VAR: `arn:aws:sns:us-west-2:${accountId}:multi-dot-topic`,
},
},
functions: {
Expand Down Expand Up @@ -642,7 +645,7 @@ const createServerlessWithFilterPolicies = (
provider: {
region: "us-east-1",
environment: {
MY_VAR: "MY_VAL",
MY_VAR: `arn:aws:sns:us-west-2:${accountId}:test-topic-policies`,
},
},
functions: {
Expand All @@ -664,6 +667,9 @@ const createServerlessWithFilterPolicies = (
pong2: {
name: "some-name2",
handler: "test/mock/handler." + handlerName,
environment: {
MY_VAR: `arn:aws:sns:us-west-2:${accountId}:test-topic-policies-multiple`,
},
events: [
{
sns: {
Expand Down