From d928b84b87bb3a71cf059dd7611916fba6dda139 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Fri, 6 Sep 2024 13:03:11 -0400 Subject: [PATCH] fix: bytes field should be typed as a Buffer --- tools/src/compileProtos.ts | 5 +++-- tools/test/compileProtos.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/src/compileProtos.ts b/tools/src/compileProtos.ts index 6543a54b5..81c9981be 100644 --- a/tools/src/compileProtos.ts +++ b/tools/src/compileProtos.ts @@ -147,8 +147,9 @@ function updateDtsTypes(dts: string, enums: Set): string { `${typeName}|keyof typeof ${typeName}` ); } else if (typeName === 'Uint8Array') { - // bytes: Uint8Array => Uint8Array|string to allow base64-encoded strings - replaced = replaced.replace(typeName, `${typeName}|string`); + // bytes: Uint8Array => Uint8Array|Buffer|string to allow base64-encoded strings + // and byte field can also come as a Buffer when pbjs uses $util.newBuffer. + replaced = replaced.replace(typeName, `${typeName}|Buffer|string`); } else if (typeName === 'Long') { // Longs can be passed as strings :( // number|Long => number|Long|string diff --git a/tools/test/compileProtos.ts b/tools/test/compileProtos.ts index 93a8286a2..e5704e8c7 100644 --- a/tools/test/compileProtos.ts +++ b/tools/test/compileProtos.ts @@ -242,7 +242,9 @@ describe('compileProtos tool', () => { ts.toString().includes('http://www.apache.org/licenses/LICENSE-2.0') ); assert(ts.toString().includes('longField?: (number|Long|string|null);')); - assert(ts.toString().includes('bytesField?: (Uint8Array|string|null);')); + assert( + ts.toString().includes('bytesField?: (Uint8Array|Buffer|string|null);') + ); assert( ts .toString() @@ -258,7 +260,9 @@ describe('compileProtos tool', () => { ) ); assert(ts.toString().includes('public longField: (number|Long|string);')); - assert(ts.toString().includes('public bytesField: (Uint8Array|string);')); + assert( + ts.toString().includes('public bytesField: (Uint8Array|Buffer|string);') + ); assert( ts .toString()