Skip to content
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

Fix data corruption when storing binary data #386

Merged
merged 3 commits into from
Jul 17, 2018

Conversation

peecky
Copy link
Contributor

@peecky peecky commented Jul 15, 2018

Hello, I fixed a bug of storing corrupted binary data.

For example, following code did not properly store the value.

const schema = new dynamoose.Schema({
    key: { type: String },
    value: { type: Buffer }
});
const Model = dynamoose.model('KVS', schema);

const s = Buffer.from([0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0xd3, 0x61, 0x60, 0x60]);
console.log(s); // <Buffer 1f 8b 08 00 00 00 00 00 00 13 d3 61 60 60>

await Model.batchPut([{ key: 'key1', value: s }]);
const c = (await Model.get({ key: 'key1' })).value;
console.log(c); // <Buffer 1f ef bf bd 08 00 00 00 00 00 00 13 ef bf bd 61 60 60>

Because not every binary data can be converted to UTF-8 string, also the AWS SDK supports both Buffer and string for type B, it is better not to convert the value to string when the type is B and the value is Buffer instance.

@coveralls
Copy link

coveralls commented Jul 15, 2018

Pull Request Test Coverage Report for Build 584

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.3%) to 84.256%

Files with Coverage Reduction New Missed Lines %
lib/Table.js 1 78.14%
Totals Coverage Status
Change from base Build 581: 0.3%
Covered Lines: 1849
Relevant Lines: 2124

💛 - Coveralls

@fishcharlie
Copy link
Member

@peecky Shouldn't this include some tests as well?

@peecky
Copy link
Contributor Author

peecky commented Jul 16, 2018

@fishcharlie Thanks. :)

Copy link
Member

@fishcharlie fishcharlie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You reported this as a bug in Dynamoose. So the broken version should be made into a separate (new) test. Without your fix this test should fail. With your fix the test should pass. It should be a 100% fully unique test.

test/Model.js Outdated
@@ -18,6 +18,10 @@ var Cats = {};
var ONE_YEAR = 365*24*60*60; // 1 years in seconds
var NINE_YEARS = 9*ONE_YEAR; // 9 years in seconds

var imageData = Buffer.from([0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0xd3, 0x61, 0x60, 0x60]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines should be included within a test and not globally scoped.

test/Model.js Outdated
@@ -319,6 +325,7 @@ describe('Model', function (){
model.should.have.property('id', 1);
model.should.have.property('name', 'Fluffy');
model.should.have.property('vet', { address: '12 somewhere', name: 'theVet' });
model.should.have.property('profileImage', imageData);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole section should probably be included in a separate test, not this one.

@fishcharlie
Copy link
Member

fishcharlie commented Jul 16, 2018

@peecky Thanks for this! Just submitted a review with some more changes 😃

@fishcharlie
Copy link
Member

@peecky This looks good to me. My only other concern is that this could be a breaking change to some users. What are your thoughts on this?

@peecky
Copy link
Contributor Author

peecky commented Jul 17, 2018

I can’t imagine any cases that the current version is useful or works well but new version breaks the working.

Strictly speaking, however, it is a breaking change. It might be better to apply this change to the next major version. The bug is still avoidable on the current version by setting toDynamo.

const schema = new dynamoose.Schema({
    key: { type: String },
    value: { type: Buffer, toDynamo: x => ({ B: x }) }
});

@fishcharlie fishcharlie self-requested a review July 17, 2018 19:52
Copy link
Member

@fishcharlie fishcharlie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM to be released in v1.0

@fishcharlie fishcharlie merged commit d285c1e into dynamoose:master Jul 17, 2018
@fishcharlie fishcharlie mentioned this pull request Jul 17, 2018
@fishcharlie
Copy link
Member

@peecky We will get this in for version 1.0. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants