Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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