aws-fanout
Advanced tools
Comparing version 2.0.0-rc.1 to 2.0.0-rc.2
@@ -30,2 +30,3 @@ "use strict"; | ||
stu.flush('../../v1'); | ||
stu.flush('../../v2'); | ||
const { eventLog, policyMap } = stu.mock('aws-sdk', mockAws_1.mockAws); | ||
@@ -32,0 +33,0 @@ const fanout = stu.test('../../index'); |
@@ -61,5 +61,6 @@ "use strict"; | ||
const topicNamePattern = common_prefix_1.default(topicNames) + '*'; | ||
await v2.setQueuePolicyWithPattern(credentials, { | ||
await v2.setQueuePolicy(credentials, { | ||
queueName, | ||
topicNamePattern, | ||
topicNames: [topicNamePattern], | ||
ignoreExistingPolicy: false, | ||
}); | ||
@@ -66,0 +67,0 @@ if (deadLetterQueueName != null) { |
@@ -40,7 +40,2 @@ import { Credentials } from './sdk/types'; | ||
declare const setQueuePolicy: (credentials: Credentials, options: SetQueuePolicyOptions) => Promise<void>; | ||
interface SetQueuePolicyWithPatternOptions { | ||
queueName: string; | ||
topicNamePattern: string; | ||
} | ||
declare const setQueuePolicyWithPattern: (credentials: Credentials, options: SetQueuePolicyWithPatternOptions) => Promise<void>; | ||
interface SetQueueRedrivePolicyOptions { | ||
@@ -57,2 +52,2 @@ queueName: string; | ||
declare const subscribeQueueToTopic: (credentials: Credentials, options: SubscribeQueueToTopicOptions) => Promise<void>; | ||
export { Credentials, createQueue, createTopic, deleteMessage, deleteQueue, deleteTopic, publishMessage, receiveMessage, setQueuePolicy, setQueuePolicyWithPattern, setQueueRedrivePolicy, subscribeQueueToTopic, }; | ||
export { Credentials, createQueue, createTopic, deleteMessage, deleteQueue, deleteTopic, publishMessage, receiveMessage, setQueuePolicy, setQueueRedrivePolicy, subscribeQueueToTopic, }; |
@@ -74,3 +74,11 @@ "use strict"; | ||
const { queueArn, queuePolicy } = await sdk.getQueueAttributes(sqs, queueUrl); | ||
let topicArnList = await Promise.all(topicNames.map((topicName) => sdk.createTopic(sns, topicName))); | ||
let topicArnList = await Promise.all(topicNames.map(async (topicName) => { | ||
if (topicName.includes('*')) { | ||
const arnId = queueArn.match(/^arn:aws:sqs:([^:]*:[^:]*):/)[1]; | ||
const topicArnPattern = `arn:aws:sns:${arnId}:${topicName}`; | ||
return topicArnPattern; | ||
} | ||
const topicArn = await sdk.createTopic(sns, topicName); | ||
return topicArn; | ||
})); | ||
if (ignoreExistingPolicy !== true) { | ||
@@ -88,17 +96,2 @@ const existingTopicArns = parseQueuePolicy_1.default(queuePolicy); | ||
exports.setQueuePolicy = setQueuePolicy; | ||
const setQueuePolicyWithPattern = async (credentials, options) => { | ||
const { queueName, topicNamePattern } = options; | ||
const { sqs } = sdk.withCredentials(credentials); | ||
const queueUrl = await sdk.createQueue(sqs, queueName); | ||
const { queueArn } = await sdk.getQueueAttributes(sqs, queueUrl); | ||
const arnId = queueArn.match(/^arn:aws:sqs:([^:]*:[^:]*):/)[1]; | ||
const topicArnPattern = `arn:aws:sns:${arnId}:${topicNamePattern}`; | ||
await sdk.setQueueAttributes(sqs, queueUrl, { | ||
Policy: buildQueuePolicy_1.default({ | ||
queueArn, | ||
topicArnList: [topicArnPattern], | ||
}), | ||
}); | ||
}; | ||
exports.setQueuePolicyWithPattern = setQueuePolicyWithPattern; | ||
const setQueueRedrivePolicy = async (credentials, options) => { | ||
@@ -105,0 +98,0 @@ const { queueName, deadLetterQueueName, maxReceiveCount } = options; |
{ | ||
"name": "aws-fanout", | ||
"version": "2.0.0-rc.1", | ||
"version": "2.0.0-rc.2", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A library wrapping SNS and SQS to allow for human readable names when using a fanout technique", |
370
README.md
@@ -31,44 +31,57 @@ # AWS-Fanout | ||
## Environment Variables | ||
### `AWS_FANOUT_RATE_LIMIT_MS` | ||
**Default value: `10`** | ||
The API calls to SQS/SNS are rate limited. | ||
By default there is a maximum of request made per 10ms. This results in a | ||
maximum of 100 requests per second. | ||
## API v2 Documentation | ||
- `createQueue(credentials, { queueName })` | ||
- `createTopic(credentials, { topicName })` | ||
- `deleteMessage(credentials, { queueName, receiptHandle })` | ||
- `deleteQueue(credentials, { queueName })` | ||
- `deleteTopic(credentials, { topicName })` | ||
- `publishMessage(credentials, { topicName, message })` | ||
- `receiveMessage(credentials, { queueName, maxNumberOfMessages, visibilityTimeout })` | ||
- `setQueuePolicy(credentials, { queueName, topicNames, ignoreExistingPolicy })` | ||
- `setQueuePolicyWithPattern(credentials, { queueName, topicNamePattern })` | ||
- `setQueueRedrivePolicy(credentials, { queueName, deadLetterQueueName, maxReceiveCount })` | ||
- `subscribeQueueToTopic(credentials, { queueName, topicName })` | ||
- [`createQueue(credentials, { queueName })`](#v2createQueue) | ||
- [`createTopic(credentials, { topicName })`](#v2createTopic) | ||
- [`deleteQueue(credentials, { queueName })`](#v2deleteQueue) | ||
- [`deleteTopic(credentials, { topicName })`](#v2deleteTopic) | ||
- [`publishMessage(credentials, { topicName, message })`](#v2publishMessage) | ||
- [`receiveMessage(credentials, { queueName, maxNumberOfMessages, visibilityTimeout })`](#v2receiveMessage) | ||
- [`deleteMessage(credentials, { queueName, receiptHandle })`](#v2deleteMessage) | ||
- [`setQueuePolicy(credentials, { queueName, topicNames, ignoreExistingPolicy })`](#v2setQueuePolicy) | ||
- [`setQueueRedrivePolicy(credentials, { queueName, deadLetterQueueName, maxReceiveCount })`](#v2setQueueRedrivePolicy) | ||
- [`subscribeQueueToTopic(credentials, { queueName, topicName })`](#v2subscribeQueueToTopic) | ||
## API v1 Documentation | ||
### `v2.createQueue` | ||
- [`deleteMessage(credentials, queueName, receiptHandle)`](#deleteMessage) | ||
- [`deleteQueue(credentials, queueName)`](#deleteQueue) | ||
- [`deleteTopic(credentials, topicName)`](#deleteTopic) | ||
- [`publish(credentials, topicName, message)`](#publish) | ||
- [`receiveMessage(credentials, maxNumberOfMessages, visibilityTimeout, queueName)`](#receiveMessage) | ||
- [`registerQueues(credentials, queueNames)`](#registerQueues) | ||
- [`registerTopics(credentials, topicNames)`](#registerTopics) | ||
- [`subscribeQueueTopicsByTheirPrefix(credentials, topicNames, queueName, [deadLetterQueueName], [maxReceiveCount=5]`](#subscribeQueueTopicsByTheirPrefix) | ||
- [`subscribeQueuesToTopics(credentials, topicNames, queueName, [deadLetterQueueName], [maxReceiveCount=5])`](#subscribeQueuesToTopics) | ||
Create a single queue on SQS. | ||
### `deleteTopic` | ||
- `credentials`: [Credentials](#credentials) | ||
- `options.queueName`: name of the queue to create | ||
Delete a topic. | ||
```typescript | ||
import { createQueue } from 'aws-sdk' | ||
await createQueue(credentials, { | ||
queueName: 'logger' | ||
}) | ||
``` | ||
### `v2.createTopic` | ||
Create a single topic on SNS. | ||
- `credentials`: [Credentials](#credentials) | ||
- `topicName`: name of the topic to delete | ||
- `options.topicName`: name of the topic to create | ||
```typescript | ||
import { deleteTopic } from 'aws-sdk' | ||
import { createTopic } from 'aws-sdk' | ||
const topicName = 'my-topic-name' | ||
await deleteTopic(credentials, topicName) | ||
await createTopic(credentials, { | ||
topicName: 'create-account' | ||
}) | ||
``` | ||
### `deleteQueue` | ||
### `v2.deleteQueue` | ||
@@ -78,3 +91,3 @@ Delete a queue. | ||
- `credentials`: [Credentials](#credentials) | ||
- `queueName`: name of the queue to delete | ||
- `options.queueName`: name of the queue to delete | ||
@@ -84,9 +97,66 @@ ```typescript | ||
const queueName = 'my-queue-name' | ||
await deleteQueue(credentials, { | ||
queueName: 'my-queue-name' | ||
}) | ||
``` | ||
await deleteQueue(credentials, queueName) | ||
### `v2.deleteTopic` | ||
Delete a topic. | ||
- `credentials`: [Credentials](#credentials) | ||
- `options.topicName`: name of the topic to delete | ||
```typescript | ||
import { deleteTopic } from 'aws-sdk' | ||
await deleteTopic(credentials, { | ||
topicName: 'my-topic-name' | ||
}) | ||
``` | ||
### `deleteMessage` | ||
### `v2.publishMessage` | ||
Publish a message with a particular topic. Any queues that are subscribed to | ||
the topic will receive a copy of it. | ||
- `credentials`: [Credentials](#credentials) | ||
- `options.topicName`: name of the topic | ||
- `options.message`: message payload to send, must be a string | ||
```typescript | ||
import { subscribeQueueTopicsByTheirPrefix } from 'aws-sdk' | ||
await publish(credentials, | ||
topicName: 'create', | ||
message: JSON.stringify({ | ||
userId: 123, | ||
email: 'john.smith@example.co.nz' | ||
}) | ||
) | ||
``` | ||
### `v2.receiveMessage` | ||
Listen for messages on the queue. | ||
- `credentials`: [Credentials](#credentials) | ||
- `options.maxNumberOfMessages`: Maximum number of messages to retrieve | ||
- `options.visibilityTimeout`: The duration (in seconds) that the received | ||
messages are hidden from subsequent retrieve requests | ||
- `options.queueName`: Name of the queue to receive messages from | ||
```typescript | ||
import { receiveMessage } from 'aws-sdk' | ||
const messages = await receiveMessage( | ||
credentials, | ||
maxNumberOfMessages: 5, | ||
visibilityTimeout: 15, | ||
queueName: 'actions' | ||
) | ||
``` | ||
### `v2.deleteMessage` | ||
Remove a message from a queue. | ||
@@ -98,4 +168,4 @@ | ||
- `credentials`: [Credentials](#credentials) | ||
- `queueName`: name of the queue to delete the message from | ||
- `receiptHandle`: the receipt handle of the mesage to delete | ||
- `options.queueName`: name of the queue to delete the message from | ||
- `options.receiptHandle`: the receipt handle of the mesage to delete | ||
@@ -107,11 +177,124 @@ ```typescript | ||
const messages = await receiveMessage(credentials, 1, 10, queueName) | ||
const messages = await receiveMessage(credentials, { | ||
maxNumberOfMessages: 1, | ||
visibilityTimeout: 10, | ||
queueName | ||
}) | ||
if (messages.length > 0) { | ||
const receiptHandle = messages[0].ReceiptHandle | ||
await deleteMessage(credentials, queueName, receiptHandle) | ||
await deleteMessage(credentials, { | ||
queueName, | ||
receiptHandle: messages[0].ReceiptHandle | ||
}) | ||
} | ||
``` | ||
### `registerTopics` | ||
### `v2.setQueuePolicy` | ||
Subscribes a queue to a list of topics. | ||
If the queue or topics do not exist, they will be created. | ||
- `credentials`: [Credentials](#credentials) | ||
- `options.queueName`: queue to forward topics to | ||
- `options.topicNames`: list of topics to subscribe to | ||
- `options.ignoreExistingPolicy`: whether to preserve any existing topics that | ||
have previously been allowed to post to this queue. | ||
```typescript | ||
import { subscribeQueuesToTopics } from 'aws-sdk' | ||
await subscribeQueuesToTopics(credentials, { | ||
queueName: 'actions', | ||
topicNames: ['create', 'read', 'update', 'destroy'], | ||
ignoreExistingPolicy: false | ||
}) | ||
``` | ||
If you have a large number of topics to create, you may start hitting the AWS | ||
limit on how large the queue policy can be. Instead you can define the queue | ||
to accept any topic that matches a wildcard pattern. | ||
```typescript | ||
import { subscribeQueuesToTopics } from 'aws-sdk' | ||
await subscribeQueuesToTopics(credentials, { | ||
queueName: 'logger', | ||
topicNames: ['*'], | ||
ignoreExistingPolicy: false | ||
}) | ||
``` | ||
### `v2.setQueueRedrivePolicy` | ||
- `credentials`: [Credentials](#credentials) | ||
- `options.deadLetterQueueName`: (optional) The name of dead-letter queue to | ||
which SQS moves messages after the value of "maxReceiveCount" is exceeded. | ||
- `options.maxReceiveCount`: (optional, default = 5) The number of times a | ||
message is delivered to the source queue before being moved to the | ||
dead-letter queue. When the ReceiveCount for a message exceeds the | ||
maxReceiveCount for a queue, SQS moves the message to the dead-letter-queue. | ||
```typescript | ||
import { setQueueRedrivePolicy } from 'aws-sdk' | ||
await setQueueRedrivePolicy(credentials, { | ||
queueName: 'actions', | ||
deadLetterQueueName: 'deadLetter', | ||
maxReceiveCount: 5 | ||
}) | ||
``` | ||
### `v2.subscribeQueueToTopic` | ||
Subscribe a queue to a topic. | ||
When the topic is published, a copy of it will be sent to the queue. | ||
- `credentials`: [Credentials](#credentials) | ||
- `options.queueName`: name of the queue | ||
- `options.topicName`: name of the topic | ||
```typescript | ||
import { subscribeQueueToTopic } from 'aws-sdk' | ||
await subscribeQueueToTopic(credentials, { | ||
queueName: 'actions', | ||
topicName: 'create' | ||
}) | ||
``` | ||
## API v1 Documentation | ||
- [`registerQueues(credentials, queueNames)`](#v1registerQueues) | ||
- [`registerTopics(credentials, topicNames)`](#v1registerTopics) | ||
- [`deleteQueue(credentials, queueName)`](#v1deleteQueue) | ||
- [`deleteTopic(credentials, topicName)`](#v1deleteTopic) | ||
- [`publish(credentials, topicName, message)`](#v1publish) | ||
- [`receiveMessage(credentials, maxNumberOfMessages, visibilityTimeout, queueName)`](#v1receiveMessage) | ||
- [`deleteMessage(credentials, queueName, receiptHandle)`](#v1deleteMessage) | ||
- [`subscribeQueueTopicsByTheirPrefix(credentials, topicNames, queueName, [deadLetterQueueName], [maxReceiveCount=5]`](#v1subscribeQueueTopicsByTheirPrefix) | ||
- [`subscribeQueuesToTopics(credentials, topicNames, queueName, [deadLetterQueueName], [maxReceiveCount=5])`](#v1subscribeQueuesToTopics) | ||
### `v1.registerQueues` | ||
Create multiple queues on SQS. | ||
- `credentials`: [Credentials](#credentials) | ||
- `queueNames`: list of queues to create | ||
```typescript | ||
import { registerQueues } from 'aws-sdk' | ||
const queueNames = [ | ||
'logs', | ||
'errors', | ||
'actions', | ||
] | ||
await registerQueues(credentials, queueNames) | ||
``` | ||
### `v1.registerTopics` | ||
Create multiple topics on SNS. | ||
@@ -135,23 +318,57 @@ | ||
### `registerQueues` | ||
### `v1.deleteQueue` | ||
Create multiple queues on SNS. | ||
Delete a queue. | ||
- `credentials`: [Credentials](#credentials) | ||
- `queueNames`: list of queues to create | ||
- `queueName`: name of the queue to delete | ||
```typescript | ||
import { registerQueues } from 'aws-sdk' | ||
import { deleteQueue } from 'aws-sdk' | ||
const queueNames = [ | ||
'logs', | ||
'errors', | ||
'actions', | ||
] | ||
const queueName = 'my-queue-name' | ||
await registerQueues(credentials, queueNames) | ||
await deleteQueue(credentials, queueName) | ||
``` | ||
### `receiveMessage` | ||
### `v1.deleteTopic` | ||
Delete a topic. | ||
- `credentials`: [Credentials](#credentials) | ||
- `topicName`: name of the topic to delete | ||
```typescript | ||
import { deleteTopic } from 'aws-sdk' | ||
const topicName = 'my-topic-name' | ||
await deleteTopic(credentials, topicName) | ||
``` | ||
### `v1.publish` | ||
Publish a message with a particular topic. Any queues that are subscribed to | ||
the topic will receive a copy of it. | ||
The message will be serialized using `JSON.stringify`. | ||
- `credentials`: [Credentials](#credentials) | ||
- `topicName`: name of the topic | ||
- `message`: message payload to send | ||
```typescript | ||
import { subscribeQueueTopicsByTheirPrefix } from 'aws-sdk' | ||
const topicName = 'create' | ||
const message = { | ||
userId: 123, | ||
email: 'john.smith@example.co.nz' | ||
} | ||
await publish(credentials, topicName, message) | ||
``` | ||
### `v1.receiveMessage` | ||
Listen for messages on the queue. | ||
@@ -180,4 +397,27 @@ | ||
### `subscribeQueuesToTopics` | ||
### `v1.deleteMessage` | ||
Remove a message from a queue. | ||
After you have finished receiving a message from the queue, you should remove | ||
it so that it does not get sent again. | ||
- `credentials`: [Credentials](#credentials) | ||
- `queueName`: name of the queue to delete the message from | ||
- `receiptHandle`: the receipt handle of the mesage to delete | ||
```typescript | ||
import { receiveMessage, deleteMessage } from 'aws-sdk' | ||
const queueName = 'my-queue-name' | ||
const messages = await receiveMessage(credentials, 1, 10, queueName) | ||
if (messages.length > 0) { | ||
const receiptHandle = messages[0].ReceiptHandle | ||
await deleteMessage(credentials, queueName, receiptHandle) | ||
} | ||
``` | ||
### `v1.subscribeQueuesToTopics` | ||
Subscribes a queue to a list of topics. | ||
@@ -210,3 +450,3 @@ | ||
### `subscribeQueueTopicsByTheirPrefix` | ||
### `v1.subscribeQueueTopicsByTheirPrefix` | ||
@@ -224,3 +464,2 @@ If you have a large number of topics to create, you may start hitting the | ||
```typescript | ||
@@ -243,27 +482,4 @@ import { subscribeQueueTopicsByTheirPrefix } from 'aws-sdk' | ||
### `publish` | ||
## Credentials | ||
Publish a message with a particular topic. Any queues that are subscribed to | ||
the topic will receive a copy of it. | ||
The message will be serialized using `JSON.stringify`. | ||
- `credentials`: [Credentials](#credentials) | ||
- `topicName`: name of the topic | ||
- `message`: message payload to send | ||
```typescript | ||
import { subscribeQueueTopicsByTheirPrefix } from 'aws-sdk' | ||
const topicName = 'create' | ||
const message = { | ||
userId: 123, | ||
email: 'john.smith@example.co.nz' | ||
} | ||
await publish(credentials, topicName, message) | ||
``` | ||
### Credentials | ||
The `credentials` object is passed through to the `SNS`/`SQS` constructor. | ||
@@ -270,0 +486,0 @@ |
@@ -57,2 +57,3 @@ import anyTest, { TestInterface } from 'ava' | ||
stu.flush('../../v1') | ||
stu.flush('../../v2') | ||
@@ -59,0 +60,0 @@ const { eventLog, policyMap } = stu.mock('aws-sdk', mockAws) |
@@ -85,5 +85,6 @@ import commonPrefix from 'common-prefix' | ||
await v2.setQueuePolicyWithPattern(credentials, { | ||
await v2.setQueuePolicy(credentials, { | ||
queueName, | ||
topicNamePattern, | ||
topicNames: [topicNamePattern], | ||
ignoreExistingPolicy: false, | ||
}) | ||
@@ -90,0 +91,0 @@ |
@@ -125,3 +125,12 @@ import buildQueuePolicy from './utils/buildQueuePolicy' | ||
let topicArnList = await Promise.all( | ||
topicNames.map((topicName) => sdk.createTopic(sns, topicName)), | ||
topicNames.map(async (topicName) => { | ||
if (topicName.includes('*')) { | ||
const arnId = queueArn.match(/^arn:aws:sqs:([^:]*:[^:]*):/)[1] | ||
const topicArnPattern = `arn:aws:sns:${arnId}:${topicName}` | ||
return topicArnPattern | ||
} | ||
const topicArn = await sdk.createTopic(sns, topicName) | ||
return topicArn | ||
}), | ||
) | ||
@@ -147,23 +156,2 @@ | ||
const setQueuePolicyWithPattern = async ( | ||
credentials: Credentials, | ||
options: SetQueuePolicyWithPatternOptions, | ||
) => { | ||
const { queueName, topicNamePattern } = options | ||
const { sqs } = sdk.withCredentials(credentials) | ||
const queueUrl = await sdk.createQueue(sqs, queueName) | ||
const { queueArn } = await sdk.getQueueAttributes(sqs, queueUrl) | ||
const arnId = queueArn.match(/^arn:aws:sqs:([^:]*:[^:]*):/)[1] | ||
const topicArnPattern = `arn:aws:sns:${arnId}:${topicNamePattern}` | ||
await sdk.setQueueAttributes(sqs, queueUrl, { | ||
Policy: buildQueuePolicy({ | ||
queueArn, | ||
topicArnList: [topicArnPattern], | ||
}), | ||
}) | ||
} | ||
interface SetQueueRedrivePolicyOptions { | ||
@@ -223,5 +211,4 @@ queueName: string, | ||
setQueuePolicy, | ||
setQueuePolicyWithPattern, | ||
setQueueRedrivePolicy, | ||
subscribeQueueToTopic, | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
829768
495
2685