Skip to content

Commit

Permalink
fix: bytes field should be typed as a Buffer (#1652)
Browse files Browse the repository at this point in the history
Co-authored-by: sofisl <55454395+sofisl@users.noreply.github.com>
  • Loading branch information
alvarowolfx and sofisl authored Feb 5, 2025
1 parent e9b47fc commit a81663f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 3 additions & 2 deletions tools/src/compileProtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ function updateDtsTypes(dts: string, enums: Set<string>): 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
Expand Down
8 changes: 6 additions & 2 deletions tools/test/compileProtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit a81663f

Please sign in to comment.