Skip to content

Commit

Permalink
fix: storing an empty Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Nov 21, 2020
1 parent 96ecdfa commit 398b614
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,10 @@ export namespace entity {
}

if (value instanceof Buffer) {
valueProto.blobValue = value;
// Convert the buffer to a base 64 string to workaround a bug of
// protobufs encoding empty buffer.
// See https://github.com/googleapis/nodejs-datastore/issues/755
valueProto.blobValue = value.toString('base64');
return valueProto;
}

Expand Down
14 changes: 14 additions & 0 deletions system-test/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@ describe('Datastore', () => {
await datastore.delete(datastore.key(['Post', assignedId as string]));
});

it('should save/get/delete an empty buffer', async () => {
const postKey = datastore.key(['Post']);
const data = {
buf: Buffer.from([]),
};
await datastore.save({key: postKey, data});
const assignedId = postKey.id;
assert(assignedId);
const [entity] = await datastore.get(postKey);
delete entity[datastore.KEY];
assert.deepStrictEqual(entity, data);
await datastore.delete(datastore.key(['Post', assignedId as string]));
});

it('should save/get/delete with a generated key id', async () => {
const postKey = datastore.key('Post');
await datastore.save({key: postKey, data: post});
Expand Down
2 changes: 1 addition & 1 deletion test/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ describe('entity', () => {
const value = Buffer.from('Hi');

const expectedValueProto = {
blobValue: value,
blobValue: value.toString('base64'),
};

assert.deepStrictEqual(entity.encodeValue(value), expectedValueProto);
Expand Down

0 comments on commit 398b614

Please sign in to comment.