From 604c3d43c053c3efde95f2e3bef6999c36637024 Mon Sep 17 00:00:00 2001 From: Shawn Li Date: Mon, 28 Sep 2020 11:12:12 +1000 Subject: [PATCH] Use base58 to encode json (#33) * use base58 to encode json * fix base58 term to base64 * allow null json field --- base64.js | 21 +++++++++++++++++++++ index.js | 27 ++++++++++++++------------- package.json | 2 +- 3 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 base64.js diff --git a/base64.js b/base64.js new file mode 100644 index 0000000..feb0d3c --- /dev/null +++ b/base64.js @@ -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 +}; diff --git a/index.js b/index.js index 4b98919..843ef67 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const AWS = require("aws-sdk"); +const { encodeJson, decodeJson } = require("./base64"); const sns = new AWS.SNS({ apiVersion: "2010-03-31", @@ -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", @@ -153,10 +147,6 @@ function dispatch({ type, uri, id, checksum, source, message, json }) { source: { DataType: "String", StringValue: source - }, - json: { - DataType: "String", - StringValue: jsonStr } }; @@ -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, @@ -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; diff --git a/package.json b/package.json index 5c55a83..886ef83 100644 --- a/package.json +++ b/package.json @@ -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",