Skip to content

Commit

Permalink
Use base58 to encode json (#33)
Browse files Browse the repository at this point in the history
* use base58 to encode json

* fix base58 term to base64

* allow null json field
  • Loading branch information
shawn-cx-li authored Sep 28, 2020
1 parent 90e475c commit 604c3d4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
21 changes: 21 additions & 0 deletions base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SNS -> SQS subscription could not filter message
// if the message contains a stringified JSON
// use base64 to encode str instead
// https://stackoverflow.com/questions/59853890/sns-subscription-filter-policies-do-not-seem-to-work-when-a-binary-message-attri

function encodeJson(json) {
const jsonStr = JSON.stringify(json);
const buff = Buffer.from(jsonStr);
return buff.toString("base64");
}

function decodeJson(base64) {
const buff = Buffer.from(base64, "base64");
const jsonStr = buff.toString("utf-8");
return JSON.parse(jsonStr);
}

module.exports = {
encodeJson,
decodeJson
};
27 changes: 14 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const AWS = require("aws-sdk");
const { encodeJson, decodeJson } = require("./base64");

const sns = new AWS.SNS({
apiVersion: "2010-03-31",
Expand Down Expand Up @@ -134,13 +135,6 @@ function dispatch({ type, uri, id, checksum, source, message, json }) {
throw new InvalidEventMessageError("event message is required");
}

let jsonStr;
try {
jsonStr = JSON.stringify(Object.assign({ type, id }, json));
} catch (e) {
throw new InvalidEventJsonError("event json is invalid");
}

const messageAttributes = {
type: {
DataType: "String",
Expand All @@ -153,10 +147,6 @@ function dispatch({ type, uri, id, checksum, source, message, json }) {
source: {
DataType: "String",
StringValue: source
},
json: {
DataType: "String",
StringValue: jsonStr
}
};

Expand All @@ -174,6 +164,17 @@ function dispatch({ type, uri, id, checksum, source, message, json }) {
};
}

if (json) {
try {
messageAttributes.json = {
DataType: "String",
StringValue: encodeJson(json)
};
} catch (e) {
throw new InvalidEventJsonError("event json is invalid");
}
}

const eventParams = {
MessageAttributes: messageAttributes,
TopicArn: process.env.AWS_SNS_TOPIC_ARN,
Expand Down Expand Up @@ -245,11 +246,11 @@ function mapAttributes(data) {
if (data.MessageAttributes[currentValue]) {
if (currentValue === "json") {
try {
accumulator[currentValue] = JSON.parse(
accumulator[currentValue] = decodeJson(
data.MessageAttributes[currentValue].Value
);
} catch (e) {
throw new InvalidEventJsonError(`unable to parse json`);
console.log(e);
}
} else {
accumulator[currentValue] = data.MessageAttributes[currentValue].Value;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@luxuryescapes/lib-events",
"version": "1.0.32",
"version": "1.0.33",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/jest && yarn run lint",
Expand Down

0 comments on commit 604c3d4

Please sign in to comment.