Created
March 26, 2020 13:04
-
-
Save mzahor/1df39e308dd66af4b6eeceff306d0c28 to your computer and use it in GitHub Desktop.
sendJSON method of SqsProducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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