Skip to content

Commit a81663f

Browse files
alvarowolfxsofisl
andauthored
fix: bytes field should be typed as a Buffer (#1652)
Co-authored-by: sofisl <[email protected]>
1 parent e9b47fc commit a81663f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

tools/src/compileProtos.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ function updateDtsTypes(dts: string, enums: Set<string>): string {
147147
`${typeName}|keyof typeof ${typeName}`
148148
);
149149
} else if (typeName === 'Uint8Array') {
150-
// bytes: Uint8Array => Uint8Array|string to allow base64-encoded strings
151-
replaced = replaced.replace(typeName, `${typeName}|string`);
150+
// bytes: Uint8Array => Uint8Array|Buffer|string to allow base64-encoded strings
151+
// and byte field can also come as a Buffer when pbjs uses $util.newBuffer.
152+
replaced = replaced.replace(typeName, `${typeName}|Buffer|string`);
152153
} else if (typeName === 'Long') {
153154
// Longs can be passed as strings :(
154155
// number|Long => number|Long|string

tools/test/compileProtos.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ describe('compileProtos tool', () => {
242242
ts.toString().includes('http://www.apache.org/licenses/LICENSE-2.0')
243243
);
244244
assert(ts.toString().includes('longField?: (number|Long|string|null);'));
245-
assert(ts.toString().includes('bytesField?: (Uint8Array|string|null);'));
245+
assert(
246+
ts.toString().includes('bytesField?: (Uint8Array|Buffer|string|null);')
247+
);
246248
assert(
247249
ts
248250
.toString()
@@ -258,7 +260,9 @@ describe('compileProtos tool', () => {
258260
)
259261
);
260262
assert(ts.toString().includes('public longField: (number|Long|string);'));
261-
assert(ts.toString().includes('public bytesField: (Uint8Array|string);'));
263+
assert(
264+
ts.toString().includes('public bytesField: (Uint8Array|Buffer|string);')
265+
);
262266
assert(
263267
ts
264268
.toString()

0 commit comments

Comments
 (0)