npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sqs-extended-client

v1.0.0

Published

Extended SQS client library for managing large AWS SQS message payloads using S3

Downloads

16,327

Readme

SQS Extended Client

A library for managing large AWS SQS message payloads using S3. In particular it supports message payloads that exceed the 256KB SQS limit. It is largely a Javascript version of the Amazon SQS Extended Client Library for Java, although not an exact copy.

This module is intended to be used within node18.x lambda runtime environment, as AWS will remove support for node 16.x in the distant future.

If this module will be used in a node16.x lambda runtime, @aws-sdk/client-s3 and @aws-sdk/client-sqs dependencies will have to be bundled into the consumer app.

version 1.0.0

Breaking change!

  • Module uses now only aws-sdk v3
  • SqsExtendedClient() do not require passing aws sdk sqs and s3 clients as a parameters anymore, as it uses aws-sdk v3 internally.

Install

To install the SQS Extended Client run:

npm install sqs-extended-client

Usage

The SQS Extended Client uses SQS and S3 instances from the AWS SDK v3. In order to send messages a bucketName is required, which is the S3 bucket where the message payloads will be stored:

const SqsExtendedClient = require('sqs-extended-client');

const sqsExtendedClient = new SqsExtendedClient(
    {
        // required for send message
        bucketName: '/* your bucket name */',
        // other configuration options
        sqsClientConfig: {}, 
        s3ClientConfig: {}
    }
);

The SQS Extended Client is used exactly as an SQS instance from the AWS SDK. It supports all the message level functions:

changeMessageVisibility()
changeMessageVisibilityBatch()
deleteMessage()
deleteMessageBatch()
sendMessage()
sendMessageBatch()
receiveMessage()

// e.g.
const response = await sqsExtendedClient.receiveMessage({
    QueueUrl: queueUrl,
});

For bucket level functions (e.g. createBucket) use the SDK S3 instance directly.

Note that for sendMessageBatch() only the size of each message is considered, not the overall batch size. For this reason it is recommended to either use alwaysUseS3: true or reduce the message size threshold proportionally to the maximum batch size (e.g. messageSizeThreshold: 26214) when sending batches.

Options

The SQS Extended Client supports the following options:

  • bucketName - S3 bucket where message payloads are stored (required for sending messages)
  • sqsClientConfig - optional SQS client options
  • s3ClientConfig - optional S3 client options
  • alwaysUseS3 - flag indicating that messages payloads should always be stored in S3 regardless of size (default: false)
  • messageSizeThreshold - maximum size in bytes for message payloads before they are stored in S3 (default: 262144)
  • sendTransform - see Transforms section
  • receiveTransform - see Transforms section

Note that the use of transforms overrides the alwaysUseS3 and messageSizeThreshold options.

Transforms

The SQS Extended Client allows transforms to be specified that control which elements from the message are stored in S3 and what remains as the SQS message body. By default the whole payload is uploaded to S3 if the message is over the size threshold, the transforms override this behaviour.

There are two transform functions:

  • sendTransform - Splits a message into an object containing the messageBody to send to SQS and the s3Content to store in S3.
  • receiveTransform - Recombines the message received from SQS and s3Content retrieved from S3 into the full message body.

For example, the following transforms split only the largeItem property from a JSON message body to store in S3. The rest of the message body is passed to SQS:

const sendTransform = (sqsMessage) => {
    const { largeItem, ...messageBody } = sqsMessage.MessageBody;
    return {
        s3Content: largeItem,
        messageBody: JSON.stringify(messageBody),
    };
};

const receiveTransform = (sqsMessage, s3Content) => ({
    ...JSON.parse(sqsMessage.Body),
    largeItem: s3Content,
});

const sqsExtendedClient = new SqsExtendedClient(
    {
        bucketName: '/* your bucket name */',
        sendTransform,
        receiveTransform,
    }
);

Middleware

If using Middy middleware with AWS Lambda then the SQS Extended Client provides a middleware implementation:

const middy = require('@middy/core');
const SqsExtendedClient = require('sqs-extended-client');

const handler = middy(/* Lambda event handler */)
    .use(new SqsExtendedClient().middleware());

Test

To execute the unit tests run:

npm install
npm run test

The system tests require AWS Localstack to be installed and started. The system tests can then be run using:

npm run system-test

Examples

sender.js - AWS Lambda function which send message.

receiver.js - AWS Lambda function which receives messages from sqs.

License

The MIT License (MIT)

Copyright (c) 2020 DVLA

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.