From f220993770bcb4be585a230b241dc6b68306846b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Tue, 24 Oct 2017 21:46:05 -0500 Subject: [PATCH 1/8] fix: don't require id for FIFO messages (Fixes #23) - Fixes requirement of id for FIFO messages - Created validation for FIFO Messages props - Fixes #23 --- lib/producer.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/producer.js b/lib/producer.js index ff05e0e..8728d97 100644 --- a/lib/producer.js +++ b/lib/producer.js @@ -43,13 +43,21 @@ function isMessageAttributeValid(messageAttribute) { } function entryFromObject(message) { - if (!message.id || !message.body) { - throw new Error('Object messages must have \'id\' and \'body\' props'); + if (!message.body) { + throw new Error('Object messages must have \'body\' prop'); + } + + if (!message.groupId && !message.deduplicationId && !message.id) { + throw new Error('Object messages must have \'id\' prop'); + } + + if ((message.groupId && !message.deduplicationId) || (message.deduplicationId && !message.groupId)) { + throw new Error('FIFO Queue messages must have \'groupId\' and \'deduplicationId\' props'); } var entry = { - Id: message.id, - MessageBody: message.body + MessageBody: message.body, + Id: message.id || message.body }; if (message.delaySeconds) { From 5993a48c167dad84214431d1b265739def16ab55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Tue, 24 Oct 2017 21:48:11 -0500 Subject: [PATCH 2/8] Keep order consistent --- lib/producer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/producer.js b/lib/producer.js index 8728d97..38939d7 100644 --- a/lib/producer.js +++ b/lib/producer.js @@ -56,8 +56,8 @@ function entryFromObject(message) { } var entry = { - MessageBody: message.body, - Id: message.id || message.body + Id: message.id || message.body, + MessageBody: message.body }; if (message.delaySeconds) { From 4dda9cf178acaf34fd2384ddff389d8c2ae00218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Tue, 24 Oct 2017 22:01:56 -0500 Subject: [PATCH 3/8] Update tests --- test/producer.js | 54 ++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/test/producer.js b/test/producer.js index b84e7eb..b9c8fba 100644 --- a/test/producer.js +++ b/test/producer.js @@ -331,13 +331,14 @@ describe('Producer', function () { }); }); - it('returns an error when object messages have invalid queueId param', function (done) { + it('returns an error when object messages have invalid groupId param', function (done) { var errMessage = 'Message.groupId value must be a string'; var message1 = { id: 'id1', body: 'body1', - groupId: 1234 + groupId: 1234, + deduplicationId: '1234' }; producer.send(message1, function (err) { @@ -352,6 +353,7 @@ describe('Producer', function () { var message1 = { id: 'id1', body: 'body1', + groupId: '1234', deduplicationId: 1234 }; @@ -360,31 +362,43 @@ describe('Producer', function () { done(); }); }); - - it('returns an error when object messages are not of shape {id, body}', function (done) { - var errMessage = 'Object messages must have \'id\' and \'body\' props'; + + it('returns an error when fifo messages have no groupId param', function (done) { + var errMessage = 'FIFO Queue messages must have \'groupId\' and \'deduplicationId\' props'; var message1 = { - noId: 'noId1', - body: 'body1' - }; - var message2 = { - id: 'id2', - body: 'body2' + id: 'id1', + body: 'body1', + deduplicationId: '1234' }; - producer.send(['foo', message1, message2], function (err) { + producer.send(message1, function (err) { assert.equal(err.message, errMessage); done(); }); }); - it('returns an error when object messages are not of shape {id, body} 2', function (done) { - var errMessage = 'Object messages must have \'id\' and \'body\' props'; + it('returns an error when fifo messages have no deduplicationId param', function (done) { + var errMessage = 'FIFO Queue messages must have \'groupId\' and \'deduplicationId\' props'; var message1 = { id: 'id1', - noBody: 'noBody1' + body: 'body1', + groupId: '1234', + }; + + producer.send(message1, function (err) { + assert.equal(err.message, errMessage); + done(); + }); + }); + + it('returns an error when object messages are not of shape {id, body}', function (done) { + var errMessage = 'Object messages must have \'id\' prop'; + + var message1 = { + noId: 'noId1', + body: 'body1' }; var message2 = { id: 'id2', @@ -397,16 +411,16 @@ describe('Producer', function () { }); }); - it('returns an error when object messages are not of shape {id, body} 3', function (done) { - var errMessage = 'Object messages must have \'id\' and \'body\' props'; + it('returns an error when object messages are not of shape {id, body} 2', function (done) { + var errMessage = 'Object messages must have \'body\' prop'; var message1 = { id: 'id1', - body: 'body1' + noBody: 'noBody1' }; var message2 = { - noId: 'noId2', - noBody: 'noBody2' + id: 'id2', + body: 'body2' }; producer.send(['foo', message1, message2], function (err) { From 374f1b973e2a7394ffe8ac2ed93b7803732fe4d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Tue, 24 Oct 2017 22:08:11 -0500 Subject: [PATCH 4/8] Update tests according to SDK docs --- test/producer.js | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/test/producer.js b/test/producer.js index b9c8fba..2dd0076 100644 --- a/test/producer.js +++ b/test/producer.js @@ -337,8 +337,7 @@ describe('Producer', function () { var message1 = { id: 'id1', body: 'body1', - groupId: 1234, - deduplicationId: '1234' + groupId: 1234 }; producer.send(message1, function (err) { @@ -364,7 +363,7 @@ describe('Producer', function () { }); it('returns an error when fifo messages have no groupId param', function (done) { - var errMessage = 'FIFO Queue messages must have \'groupId\' and \'deduplicationId\' props'; + var errMessage = 'FIFO Queue messages must have \'groupId\' prop'; var message1 = { id: 'id1', @@ -378,21 +377,6 @@ describe('Producer', function () { }); }); - it('returns an error when fifo messages have no deduplicationId param', function (done) { - var errMessage = 'FIFO Queue messages must have \'groupId\' and \'deduplicationId\' props'; - - var message1 = { - id: 'id1', - body: 'body1', - groupId: '1234', - }; - - producer.send(message1, function (err) { - assert.equal(err.message, errMessage); - done(); - }); - }); - it('returns an error when object messages are not of shape {id, body}', function (done) { var errMessage = 'Object messages must have \'id\' prop'; From d1bd5080b47f2e64cc78b158f8b8c3c53dbec081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Tue, 24 Oct 2017 22:09:37 -0500 Subject: [PATCH 5/8] Update to match SDK docs --- lib/producer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/producer.js b/lib/producer.js index 38939d7..ea6edfa 100644 --- a/lib/producer.js +++ b/lib/producer.js @@ -51,8 +51,8 @@ function entryFromObject(message) { throw new Error('Object messages must have \'id\' prop'); } - if ((message.groupId && !message.deduplicationId) || (message.deduplicationId && !message.groupId)) { - throw new Error('FIFO Queue messages must have \'groupId\' and \'deduplicationId\' props'); + if (message.deduplicationId && !message.groupId) { + throw new Error('FIFO Queue messages must have \'groupId\' prop'); } var entry = { From 085749cd69486bc49c2cbd4381e6efba335aed80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Tue, 31 Oct 2017 08:38:07 -0500 Subject: [PATCH 6/8] Don't add Body as Id --- lib/producer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/producer.js b/lib/producer.js index ea6edfa..9a05bc8 100644 --- a/lib/producer.js +++ b/lib/producer.js @@ -56,9 +56,10 @@ function entryFromObject(message) { } var entry = { - Id: message.id || message.body, MessageBody: message.body }; + + if(message.id) entry.Id = message.id; if (message.delaySeconds) { if ( From a43a07d12f245e6843d5d456376c7ed7794047b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Tue, 31 Oct 2017 08:57:45 -0500 Subject: [PATCH 7/8] Assert Message.id --- lib/producer.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/producer.js b/lib/producer.js index 9a05bc8..a698d80 100644 --- a/lib/producer.js +++ b/lib/producer.js @@ -59,7 +59,12 @@ function entryFromObject(message) { MessageBody: message.body }; - if(message.id) entry.Id = message.id; + if(message.id) { + if (typeof message.id !== 'string') { + throw new Error('Message.id value must be a string'); + } + entry.Id = message.id; + } if (message.delaySeconds) { if ( From a4f1f11fe1b2da2b80a808b1fdcbc809c4273f14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Tue, 31 Oct 2017 09:20:20 -0500 Subject: [PATCH 8/8] Add id type test --- test/producer.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/producer.js b/test/producer.js index 2dd0076..185fc19 100644 --- a/test/producer.js +++ b/test/producer.js @@ -331,6 +331,20 @@ describe('Producer', function () { }); }); + it('returns an error when object messages have invalid id param', function (done) { + var errMessage = 'Message.id value must be a string'; + + var message1 = { + id: 1234, + body: 'body1' + }; + + producer.send(message1, function (err) { + assert.equal(err.message, errMessage); + done(); + }); + }); + it('returns an error when object messages have invalid groupId param', function (done) { var errMessage = 'Message.groupId value must be a string';