diff --git a/src/entity.ts b/src/entity.ts index 2ae14ed79..a8c812f8f 100644 --- a/src/entity.ts +++ b/src/entity.ts @@ -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; } diff --git a/system-test/datastore.ts b/system-test/datastore.ts index b2758df00..f9398ffb9 100644 --- a/system-test/datastore.ts +++ b/system-test/datastore.ts @@ -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}); diff --git a/test/entity.ts b/test/entity.ts index 12e0c5138..84ca5b3dd 100644 --- a/test/entity.ts +++ b/test/entity.ts @@ -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);