-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add json field #31
Merged
Merged
Add json field #31
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ const sns = new AWS.SNS({ | |
region: process.env.AWS_SNS_REGION | ||
}); | ||
|
||
// Amazon SNS currently allows a maximum size of 256 KB for published messages. | ||
const MAX_EVENT_MESSAGE_SIZE = 256 * 1024; | ||
|
||
const typeList = [ | ||
"ORDER_PENDING", | ||
"ORDER_COMPLETED", | ||
|
@@ -98,7 +101,23 @@ class InvalidEventMessageError extends Error { | |
} | ||
} | ||
|
||
function dispatch({ type, uri, id, checksum, source, message }) { | ||
class InvalidEventJsonError extends Error { | ||
constructor(message, status) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
|
||
class InvalidEventSizeError extends Error { | ||
constructor(message, status) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
|
||
function dispatch({ type, uri, id, checksum, source, message, json }) { | ||
if (!types[type]) { | ||
throw new InvalidEventTypeError(`invalid event type '${type}'`); | ||
} | ||
|
@@ -115,6 +134,13 @@ function dispatch({ type, uri, id, checksum, source, message }) { | |
throw new InvalidEventMessageError("event message is required"); | ||
} | ||
|
||
let JsonStr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe lowercase There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for the pick up, fixed |
||
try { | ||
JsonStr = JSON.stringify(json); | ||
} catch (e) { | ||
throw new InvalidEventJsonError("event json is invalid"); | ||
} | ||
|
||
const messageAttributes = { | ||
type: { | ||
DataType: "String", | ||
|
@@ -127,6 +153,10 @@ function dispatch({ type, uri, id, checksum, source, message }) { | |
source: { | ||
DataType: "String", | ||
StringValue: source | ||
}, | ||
json: { | ||
DataType: "String", | ||
StringValue: JsonStr | ||
} | ||
}; | ||
|
||
|
@@ -150,6 +180,13 @@ function dispatch({ type, uri, id, checksum, source, message }) { | |
Message: message | ||
}; | ||
|
||
if ( | ||
Buffer.byteLength(JSON.stringify(eventParams), "utf8") > | ||
MAX_EVENT_MESSAGE_SIZE | ||
) { | ||
throw new InvalidEventSizeError("json message exceeded limit of 256KB"); | ||
} | ||
|
||
if (process.env.IGNORE_EVENTS == "true") { | ||
return Promise.resolve(); | ||
} | ||
|
@@ -191,7 +228,9 @@ function deleteMessage(message) { | |
function getAttributes(body) { | ||
const bodyJson = JSON.parse(body); | ||
// handle s3 upload event | ||
if (bodyJson.Records) return bodyJson.Records; | ||
// currently we can only one record per s3 event | ||
// https://stackoverflow.com/questions/40765699/how-many-records-can-be-in-s3-put-event-lambda-trigger/40767563#40767563 | ||
if (bodyJson.Records) return bodyJson.Records[0]; | ||
else if (bodyJson.MessageAttributes) { | ||
// handle sns message | ||
return mapAttributes(bodyJson); | ||
|
@@ -204,12 +243,22 @@ function getAttributes(body) { | |
function mapAttributes(data) { | ||
const attributeReducer = (accumulator, currentValue) => { | ||
if (data.MessageAttributes[currentValue]) { | ||
accumulator[currentValue] = data.MessageAttributes[currentValue].Value; | ||
if (currentValue === "json") { | ||
try { | ||
accumulator[currentValue] = JSON.parse( | ||
data.MessageAttributes[currentValue].Value | ||
); | ||
} catch (e) { | ||
throw new InvalidEventJsonError(`unable to parse json`); | ||
} | ||
} else { | ||
accumulator[currentValue] = data.MessageAttributes[currentValue].Value; | ||
} | ||
} | ||
return accumulator; | ||
}; | ||
|
||
const attributeList = ["type", "uri", "id", "checksum"]; | ||
const attributeList = ["type", "uri", "id", "checksum", "json"]; | ||
|
||
return attributeList.reduce(attributeReducer, {}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you multiply by 1024?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's 256 kilobyte limitation, so
MAX_EVENT_MESSAGE_SIZE
means the maximum bytes we can have