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

Drop full aws sdk #3

Merged
merged 13 commits into from
Apr 5, 2023
Merged
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
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
node_modules

23 changes: 23 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"env": {
"es2018": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"ecmaVersion": "latest",
"project": ["./eslint.tsconfig.json"]
},
"rules": {
"eol-last": ["error", "always"],
"indent": ["error", 2, { "SwitchCase": 1 }],
"linebreak-style": ["error", "unix"],
"no-console": "off",
"no-trailing-spaces": "error",
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"semi": ["error", "never"]
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
build
node_modules
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.tgz
.eslintrc.json
.git*
.npmignore
eslint.tsconfig.json
node_modules
test
tsconfig.json
yarn.lock
5 changes: 5 additions & 0 deletions eslint.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"exclude": [],
"include": ["./src/**/*.ts", "./test/**/*.ts"]
}
108 changes: 0 additions & 108 deletions index.js

This file was deleted.

31 changes: 23 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"name": "@articulate/sqns",
"version": "0.1.0",
"version": "1.0.0",
"description": "an SQS creator and SNS subscriber",
"main": "index.js",
"main": "./build/index.js",
"scripts": {
"test": "mocha ./test"
"build": "tsc",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"test": "mocha ./test/**/*.spec.ts"
},
"files": [
"build"
],
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/articulate/sqns.git"
Expand All @@ -22,16 +28,25 @@
},
"homepage": "https://github.com/articulate/sqns#readme",
"dependencies": {
"aws-sdk": "^2.527.0",
"ramda": "^0.26.1"
"@aws-sdk/client-sns": "^3.296.0",
"@aws-sdk/client-sqs": "^3.296.0"
},
"devDependencies": {
"aws-sdk-mock": "^4.5.0",
"@types/chai": "^4.3.4",
"@types/chai-as-promised": "^7.1.5",
"@types/mocha": "^10.0.1",
"@types/sinon": "^10.0.13",
"@types/sinon-chai": "^3.2.9",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.56.0",
"aws-sdk-client-mock": "^2.1.1",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^8.0.1",
"mocha": "^6.2.0",
"nock": "^11.3.5",
"sinon": "^7.5.0",
"sinon-chai": "^3.3.0"
"sinon-chai": "^3.3.0",
"ts-node": "^10.9.1",
"typescript": "*"
}
}
172 changes: 172 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import {
CreateQueueCommand,
GetQueueAttributesCommand,
SetQueueAttributesCommand,
SQSClient
} from '@aws-sdk/client-sqs'

import {
SetSubscriptionAttributesCommand,
SNSClient,
SubscribeCommand
} from '@aws-sdk/client-sns'

import type {
CreateQueueCommandInput,
GetQueueAttributesCommandInput,
GetQueueAttributesCommandOutput,
SetQueueAttributesCommandInput,
CreateQueueCommandOutput,
SetQueueAttributesCommandOutput
} from '@aws-sdk/client-sqs'

import type {
SetSubscriptionAttributesCommandInput,
SubscribeCommandInput,
SubscribeCommandOutput,
SetSubscriptionAttributesCommandOutput
} from '@aws-sdk/client-sns'

import {
CreateSqsQueueParams,
SqnsOptions,
TopicOptions
} from './types'

const sqns = async (options: SqnsOptions): Promise<string> => {
const {
region,
queueName,
maxReceiveCount = 3,
topic = {}
} = options

if (region === null || region === undefined || region === '') throw Error('Missing region')
if (queueName === null || queueName === undefined || queueName === '') throw Error('Missing queueName')
lucasadrianof marked this conversation as resolved.
Show resolved Hide resolved

const topicOptions: TopicOptions = {
rawMessageDelivery: true,
...topic
}

const sqsClient = new SQSClient({ region })
const snsClient = new SNSClient({ region })

const createQueue = (params: CreateQueueCommandInput): Promise<CreateQueueCommandOutput> =>
sqsClient.send(new CreateQueueCommand(params))

const getQueueAttributes = (params: GetQueueAttributesCommandInput): Promise<GetQueueAttributesCommandOutput> =>
sqsClient.send(new GetQueueAttributesCommand(params))

const setQueueAttributes = (params: SetQueueAttributesCommandInput): Promise<SetQueueAttributesCommandOutput> =>
sqsClient.send(new SetQueueAttributesCommand(params))

const subscribe = (params: SubscribeCommandInput): Promise<SubscribeCommandOutput> =>
snsClient.send(new SubscribeCommand(params))

const setSubscriptionAttributes = (params: SetSubscriptionAttributesCommandInput): Promise<SetSubscriptionAttributesCommandOutput> =>
snsClient.send(new SetSubscriptionAttributesCommand(params))

const createDeadletterQueue = async (queueName: string): Promise<string | undefined> =>
await createQueue({ QueueName: `${queueName}-DLQ` })
.then(queue => queue.QueueUrl)
.catch(() => undefined)

const getQueueArn = async (QueueUrl: string): Promise<string | undefined> =>
await getQueueAttributes({
QueueUrl,
AttributeNames: ['QueueArn']
})
.then((attributes: GetQueueAttributesCommandOutput) => attributes.Attributes?.QueueArn)
lucasadrianof marked this conversation as resolved.
Show resolved Hide resolved
.catch(() => undefined)

const createSqsQueue = async ({ deadletterQueueArn, queueName }: CreateSqsQueueParams): Promise<string | undefined> =>
await createQueue({
Attributes: {
RedrivePolicy: JSON.stringify({
deadLetterTargetArn: deadletterQueueArn,
maxReceiveCount
})
},
QueueName: queueName
})
.then(queue => queue.QueueUrl)
.catch(() => undefined)


const setSqsQueueAttributes = async (queueUrl: string, queueArn: string, snsTopic: string): Promise<SetQueueAttributesCommandOutput> =>
await setQueueAttributes({
Attributes: {
Policy: JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Principal: '*',
Action: 'SQS:SendMessage',
Resource: queueArn,
Condition: {
ArnEquals: {
'aws:SourceArn': snsTopic
}
}
}
]
})
},
QueueUrl: queueUrl
})

const createTopicSubscription = async (queueArn: string, topicArn: string): Promise<string | undefined> =>
await subscribe({
Endpoint: queueArn,
Protocol: 'sqs',
TopicArn: topicArn
}).then(sub => sub.SubscriptionArn)

const deadletterQueueUrl = await createDeadletterQueue(queueName)

if (deadletterQueueUrl === undefined) {
throw Error(`SQS: Failed to create DQL queue for: ${queueName}`)
}

const deadletterQueueArn = await getQueueArn(deadletterQueueUrl)
if (deadletterQueueArn === undefined) {
throw Error(`SQS: Failed to get arn for: ${deadletterQueueUrl}`)
}

const queueUrl = await createSqsQueue({ deadletterQueueArn, queueName })
if (queueUrl === undefined) {
throw Error(`SQS: Failed to create queue: ${queueName}`)
}

const queueArn = await getQueueArn(queueUrl)
if (queueArn === undefined) {
throw Error(`SQS: Failed to get arn for: ${queueUrl}`)
}

if (topicOptions.arn !== undefined && topicOptions.arn !== null && topicOptions.arn !== '') {
lucasadrianof marked this conversation as resolved.
Show resolved Hide resolved
const subscriptionArn = await createTopicSubscription(queueArn, topicOptions.arn)
await setSqsQueueAttributes(queueUrl, queueArn, topicOptions.arn)

if (topicOptions.filterPolicy) {
await setSubscriptionAttributes({
SubscriptionArn: subscriptionArn,
AttributeName: 'FilterPolicy',
AttributeValue: JSON.stringify(topicOptions.filterPolicy)
})
}

if (topicOptions.rawMessageDelivery === true) {
await setSubscriptionAttributes({
SubscriptionArn: subscriptionArn,
AttributeName: 'RawMessageDelivery',
AttributeValue: 'true'
})
}
}

return queueUrl
}

export = sqns
Loading