Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IMessageOpts {
}

interface SNSBody {
MessageAttributes: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a proper typing for that and then use that interface in the parseMessageAttributes and related functions (please use type guards to parse properly depending on the type we receive)

Message?: string;
Subject: string;
TopicArn: string;
Expand Down Expand Up @@ -73,18 +74,19 @@ export class Message extends (EventEmitter as new() => MessageEmitter) {
this._opts = opts;
this.raw = opts.msg;
this.body = opts.msg.Body;
this.attributes = parseMessageAttributes(opts.msg.MessageAttributes);
if (opts.unwrapSns) {
const unwrapped: SNSBody = JSON.parse(this.body || EMPTY_BODY);
this.body = unwrapped.Message;
this.subject = unwrapped.Subject;
this.topicArn = unwrapped.TopicArn;
this.attributes = parseMessageAttributes(unwrapped.MessageAttributes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseMessageAttributes expects a map and not a string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also be aware that this will cause an overide of the real message attributes.... so if you send a regular message (not via SNS) this will remove all the attributes. Might be better to merge the attributes (_.extend() for example)

if (this.topicArn) {
this.topicName = unwrapped.TopicArn.substr(unwrapped.TopicArn.lastIndexOf(':') + 1);
}
}
this._squiss = opts.squiss;
this._handled = false;
this.attributes = parseMessageAttributes(opts.msg.MessageAttributes);
this.sqsAttributes = opts.msg.Attributes || {};
this._s3Retriever = opts.s3Retriever;
this._s3Retain = opts.s3Retain;
Expand Down
4 changes: 2 additions & 2 deletions src/attributeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export const parseMessageAttributes = (messageAttributes: SQS.MessageBodyAttribu
};

const parseAttributeValue = (unparsedAttribute: SQS.MessageAttributeValue): IMessageAttribute => {
const type = unparsedAttribute.DataType;
const stringValue = unparsedAttribute.StringValue;
const type = unparsedAttribute.DataType || unparsedAttribute.Type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please parse according to the actual type using type guards

Copy link
Author

@AlexanderKudryavsky AlexanderKudryavsky Feb 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@regevbr
If you include raw in sns, then the structure of the message changes, the attributes are no longer in the body, but it still does not work correctly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you give exampels please. I'm guessing that if you are using the raw option, you don't need the unwrapSns flag turned on

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@regevbr
unwrapSns: false works. In this case, unwrapSns works somehow differently than I expected, the messages structure I posted to the Issue cannot be handled correctly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, so lets try to handle it in this PR

const stringValue = unparsedAttribute.StringValue || unparsedAttribute.Value;
const binaryValue = unparsedAttribute.BinaryValue;

switch (type) {
Expand Down