sqs-producer 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/producer.d.ts +4 -5
- package/dist/producer.js +10 -9
- package/dist/types.d.ts +4 -3
- package/dist/types.js +8 -8
- package/package.json +2 -2
package/dist/producer.d.ts
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { SendMessageBatchResultEntryList } from 'aws-sdk/clients/sqs';
|
|
1
|
+
import { SQSClient, SendMessageBatchResultEntry } from '@aws-sdk/client-sqs';
|
|
3
2
|
import { Message } from './types';
|
|
4
3
|
interface ProducerOptions {
|
|
5
4
|
queueUrl?: string;
|
|
6
5
|
batchSize?: number;
|
|
7
|
-
sqs?:
|
|
6
|
+
sqs?: SQSClient;
|
|
8
7
|
region?: string;
|
|
9
8
|
}
|
|
10
9
|
export declare class Producer {
|
|
11
10
|
static create: (options: ProducerOptions) => Producer;
|
|
12
11
|
queueUrl: string;
|
|
13
12
|
batchSize: number;
|
|
14
|
-
sqs:
|
|
13
|
+
sqs: SQSClient;
|
|
15
14
|
region?: string;
|
|
16
15
|
constructor(options: ProducerOptions);
|
|
17
16
|
queueSize(): Promise<number>;
|
|
18
|
-
send(messages: string | Message | (string | Message)[]): Promise<
|
|
17
|
+
send(messages: string | Message | (string | Message)[]): Promise<SendMessageBatchResultEntry[]>;
|
|
19
18
|
private validate;
|
|
20
19
|
private sendBatch;
|
|
21
20
|
}
|
package/dist/producer.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Producer = void 0;
|
|
4
|
-
const
|
|
4
|
+
const client_sqs_1 = require("@aws-sdk/client-sqs");
|
|
5
5
|
const types_1 = require("./types");
|
|
6
6
|
const requiredOptions = ['queueUrl'];
|
|
7
7
|
class Producer {
|
|
@@ -11,15 +11,14 @@ class Producer {
|
|
|
11
11
|
this.batchSize = options.batchSize || 10;
|
|
12
12
|
this.sqs =
|
|
13
13
|
options.sqs ||
|
|
14
|
-
new
|
|
14
|
+
new client_sqs_1.SQSClient(Object.assign(Object.assign({}, options), { region: options.region || 'eu-west-1' }));
|
|
15
15
|
}
|
|
16
16
|
async queueSize() {
|
|
17
|
-
const
|
|
18
|
-
.getQueueAttributes({
|
|
17
|
+
const command = new client_sqs_1.GetQueueAttributesCommand({
|
|
19
18
|
QueueUrl: this.queueUrl,
|
|
20
19
|
AttributeNames: ['ApproximateNumberOfMessages']
|
|
21
|
-
})
|
|
22
|
-
|
|
20
|
+
});
|
|
21
|
+
const result = await this.sqs.send(command);
|
|
23
22
|
return Number(result &&
|
|
24
23
|
result.Attributes &&
|
|
25
24
|
result.Attributes.ApproximateNumberOfMessages);
|
|
@@ -42,15 +41,17 @@ class Producer {
|
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
43
|
async sendBatch(failedMessages, successfulMessages, messages, startIndex) {
|
|
44
|
+
var _a;
|
|
45
45
|
const endIndex = startIndex + this.batchSize;
|
|
46
46
|
const batch = messages.slice(startIndex, endIndex);
|
|
47
47
|
const params = {
|
|
48
48
|
QueueUrl: this.queueUrl,
|
|
49
49
|
Entries: batch.map(types_1.toEntry)
|
|
50
50
|
};
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
const
|
|
51
|
+
const command = new client_sqs_1.SendMessageBatchCommand(params);
|
|
52
|
+
const result = await this.sqs.send(command);
|
|
53
|
+
const failedMessagesBatch = failedMessages.concat(((_a = result === null || result === void 0 ? void 0 : result.Failed) === null || _a === void 0 ? void 0 : _a.map((entry) => entry.Id)) || []);
|
|
54
|
+
const successfulMessagesBatch = successfulMessages.concat((result === null || result === void 0 ? void 0 : result.Successful) || []);
|
|
54
55
|
if (endIndex < messages.length) {
|
|
55
56
|
return this.sendBatch(failedMessagesBatch, successfulMessagesBatch, messages, endIndex);
|
|
56
57
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { SendMessageBatchRequestEntry } from 'aws-sdk/clients/sqs';
|
|
1
|
+
import { SendMessageBatchRequestEntry, MessageAttributeValue } from '@aws-sdk/client-sqs';
|
|
3
2
|
export interface Message {
|
|
4
3
|
id: string;
|
|
5
4
|
body: string;
|
|
6
5
|
groupId?: string;
|
|
7
6
|
deduplicationId?: string;
|
|
8
7
|
delaySeconds?: number;
|
|
9
|
-
messageAttributes?:
|
|
8
|
+
messageAttributes?: {
|
|
9
|
+
[key: string]: MessageAttributeValue;
|
|
10
|
+
};
|
|
10
11
|
}
|
|
11
12
|
export declare function toEntry(message: string | Message): SendMessageBatchRequestEntry;
|
package/dist/types.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.toEntry = void 0;
|
|
4
|
-
const
|
|
4
|
+
const validation_1 = require("./validation");
|
|
5
5
|
function entryFromObject(message) {
|
|
6
6
|
if (!message.body) {
|
|
7
7
|
throw new Error(`Object messages must have 'body' prop`);
|
|
@@ -13,7 +13,7 @@ function entryFromObject(message) {
|
|
|
13
13
|
throw new Error(`FIFO Queue messages must have 'groupId' prop`);
|
|
14
14
|
}
|
|
15
15
|
if (message.id) {
|
|
16
|
-
if (!isString(message.id)) {
|
|
16
|
+
if (!(0, validation_1.isString)(message.id)) {
|
|
17
17
|
throw new Error('Message.id value must be a string');
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -30,20 +30,20 @@ function entryFromObject(message) {
|
|
|
30
30
|
entry.DelaySeconds = message.delaySeconds;
|
|
31
31
|
}
|
|
32
32
|
if (message.messageAttributes) {
|
|
33
|
-
if (!isObject(message.messageAttributes)) {
|
|
33
|
+
if (!(0, validation_1.isObject)(message.messageAttributes)) {
|
|
34
34
|
throw new Error('Message.messageAttributes must be an object');
|
|
35
35
|
}
|
|
36
|
-
Object.values(message.messageAttributes).every(isMessageAttributeValid);
|
|
36
|
+
Object.values(message.messageAttributes).every(validation_1.isMessageAttributeValid);
|
|
37
37
|
entry.MessageAttributes = message.messageAttributes;
|
|
38
38
|
}
|
|
39
39
|
if (message.groupId) {
|
|
40
|
-
if (!isString(message.groupId)) {
|
|
40
|
+
if (!(0, validation_1.isString)(message.groupId)) {
|
|
41
41
|
throw new Error('Message.groupId value must be a string');
|
|
42
42
|
}
|
|
43
43
|
entry.MessageGroupId = message.groupId;
|
|
44
44
|
}
|
|
45
45
|
if (message.deduplicationId) {
|
|
46
|
-
if (!isString(message.deduplicationId)) {
|
|
46
|
+
if (!(0, validation_1.isString)(message.deduplicationId)) {
|
|
47
47
|
throw new Error('Message.deduplicationId value must be a string');
|
|
48
48
|
}
|
|
49
49
|
entry.MessageDeduplicationId = message.deduplicationId;
|
|
@@ -57,10 +57,10 @@ function entryFromString(message) {
|
|
|
57
57
|
};
|
|
58
58
|
}
|
|
59
59
|
function toEntry(message) {
|
|
60
|
-
if (isString(message)) {
|
|
60
|
+
if ((0, validation_1.isString)(message)) {
|
|
61
61
|
return entryFromString(message);
|
|
62
62
|
}
|
|
63
|
-
if (isObject(message)) {
|
|
63
|
+
if ((0, validation_1.isObject)(message)) {
|
|
64
64
|
return entryFromObject(message);
|
|
65
65
|
}
|
|
66
66
|
throw new Error('A message can either be an object or a string');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sqs-producer",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "Enqueues messages onto a given SQS queue",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"format": "prettier --loglevel warn --write \"**/*.{js,json,jsx,md,ts,tsx,html}\"",
|
|
15
15
|
"format:check": "prettier --check \"**/*.{js,json,jsx,md,ts,tsx,html}\"",
|
|
16
16
|
"build": "npm run clean && tsc",
|
|
17
|
-
"
|
|
17
|
+
"prepare": "npm run build",
|
|
18
18
|
"pretest": "npm run build",
|
|
19
19
|
"watch": "tsc --watch",
|
|
20
20
|
"clean": "rm -fr dist/*"
|