X Tutup
Skip to content

Commit fe10780

Browse files
committed
Fix stringifying Uint8Array
1 parent e4150de commit fe10780

File tree

1 file changed

+10
-0
lines changed
  • packages/protocol/src/common

1 file changed

+10
-0
lines changed

packages/protocol/src/common/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type IEncodingOptionsCallback = IEncodingOptions | ((err: NodeJS.ErrnoExc
3131
*/
3232
export const stringify = (arg: any): string => { // tslint:disable-line no-any
3333
if (arg instanceof Error) {
34+
// Errors don't stringify at all. They just become "{}".
3435
return JSON.stringify({
3536
type: "Error",
3637
data: {
@@ -39,6 +40,15 @@ export const stringify = (arg: any): string => { // tslint:disable-line no-any
3940
stack: arg.stack,
4041
},
4142
});
43+
} else if (arg instanceof Uint8Array) {
44+
// With stringify, these get turned into objects with each index becoming a
45+
// key for some reason. Then trying to do something like write that data
46+
// results in [object Object] being written. Stringify them like a Buffer
47+
// instead.
48+
return JSON.stringify({
49+
type: "Buffer",
50+
data: Array.from(arg),
51+
});
4252
}
4353

4454
return JSON.stringify(arg);

0 commit comments

Comments
 (0)
X Tutup