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 cf4a2b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/entity.ts
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
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

0 comments on commit cf4a2b8

Please sign in to comment.