Admiral-Piett / goaws

AWS (SQS/SNS) Clone for Development testing
MIT License
788 stars 146 forks source link

SNS: Socket hang up error when passing DataType and StringValue in MessageAttributes - works with Type and Value #329

Open mattcobley opened 1 month ago

mattcobley commented 1 month ago

Hi,

Getting a socket hang up error when trying to publish an SNS message that contains MessageAttributes using the @aws-sdk/client-sns package, version 3.485.0.

The error is:

[2024-10-23T16:57:21.775Z] ERROR: Error (err.code=ECONNRESET)
    TimeoutError: socket hang up
        at Socket.socketOnEnd (node:_http_client:535:25)
        at Socket.emit (node:events:531:35)
        at Socket.emit (node:domain:488:12)
        at endReadableNT (node:internal/streams/readable:1696:12)
        at processTicksAndRejections (node:internal/process/task_queues:82:21)

This is essentially the code that we're using and it works with the real AWS SNS:

  const snsClient = new SNSClient({
    credentials,
    endpoint,
    region,
  })

  const buildMessage = (message) => JSON.stringify({...message, tenantId})
  const buildAttributes = ({message: {type}, attributes}) => ({
    ...attributes,
    type: {
      DataType: "String",  // Note this property
      StringValue: type,    // and this one
    },
  })

  return {
    publish({message, attributes = undefined}) {
      return snsClient
        .send(
          new PublishCommand({
            Message: buildMessage(message),
            TopicArn: sns.topicArn,
            MessageAttributes: buildAttributes({message, attributes}),
          })
        )
  })
}

What I did notice is this definition here (https://github.com/Admiral-Piett/goaws/blob/master/app/sns.go#L7) in your codebase that specifies Type and Value instead of DataType and StringValue, and if I change my code to set Type and Value in the MessageAttributes then it works as expected, but of course these are not the current property names to use (https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-sns/Interface/MessageAttributeValue/).

I did also notice this TODO in the codebase where in another location we've got DataType and StringValue etc. again: https://github.com/Admiral-Piett/goaws/blob/master/app/models/models.go#L27

Admiral-Piett commented 1 month ago

Alright, I think I snuck this into my latest PR, for better or worse. It implements PublishBatch, and it'll be in the next release so look out for v0.5.1, and that'll be soon.

All I really learned was the MessageAttributes are an absolute mess and I need to redo it. So I'll need you to check your flow once deployed? If it's still no good, I'll put this on my short list to figure out.

Admiral-Piett commented 3 weeks ago

@mattcobley v0.5.1 is out! Let me know if that worked for you or if you're still having issues when you can!