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: storing an empty Buffer #767

Merged
merged 1 commit into from
Nov 25, 2020
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.
Jump to
Jump to file
Failed to load files.
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