Skip to content

Instantly share code, notes, and snippets.

@mzahor
Created March 26, 2020 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzahor/1df39e308dd66af4b6eeceff306d0c28 to your computer and use it in GitHub Desktop.
Save mzahor/1df39e308dd66af4b6eeceff306d0c28 to your computer and use it in GitHub Desktop.
sendJSON method of SqsProducer
async sendJSON(message: object, options: SqsMessageOptions = {}): Promise<any> {
const messageBody = JSON.stringify(message);
const msgSize = Buffer.byteLength(messageBody, 'utf-8');
if ((msgSize > MAX_SQS_MESSAGE_SIZE && this.largePayloadThoughS3) || this.allPayloadThoughS3) {
const payloadId = uuid();
const payloadKey = `${payloadId}.json`;
const s3Response = await this.s3
.upload({
Bucket: this.s3Bucket,
Body: messageBody,
Key: payloadKey,
ContentType: 'application/json',
})
.promise();
const sqsResponse = await this.sqs
.sendMessage({
QueueUrl: this.queueUrl,
MessageBody: JSON.stringify({
S3Payload: {
Id: payloadId,
Bucket: s3Response.Bucket,
Key: s3Response.Key,
Location: s3Response.Location,
},
} as PayloadMeta),
DelaySeconds: options.DelaySeconds,
MessageDeduplicationId: options.MessageDeduplicationId,
MessageGroupId: options.MessageGroupId,
})
.promise();
return {
s3Response,
sqsResponse,
};
} else if (msgSize > MAX_SQS_MESSAGE_SIZE) {
throw new Error("Message is too big. Use 'largePayloadThoughS3' option to send large payloads though S3.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment