From fe107802e3830f70890bb5ca58cc1f5410af4a6d Mon Sep 17 00:00:00 2001 From: Asher Date: Thu, 21 Feb 2019 13:29:08 -0600 Subject: [PATCH] Fix stringifying Uint8Array --- packages/protocol/src/common/util.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/protocol/src/common/util.ts b/packages/protocol/src/common/util.ts index 4c995e42..5cb9b5cc 100644 --- a/packages/protocol/src/common/util.ts +++ b/packages/protocol/src/common/util.ts @@ -31,6 +31,7 @@ export type IEncodingOptionsCallback = IEncodingOptions | ((err: NodeJS.ErrnoExc */ export const stringify = (arg: any): string => { // tslint:disable-line no-any if (arg instanceof Error) { + // Errors don't stringify at all. They just become "{}". return JSON.stringify({ type: "Error", data: { @@ -39,6 +40,15 @@ export const stringify = (arg: any): string => { // tslint:disable-line no-any stack: arg.stack, }, }); + } else if (arg instanceof Uint8Array) { + // With stringify, these get turned into objects with each index becoming a + // key for some reason. Then trying to do something like write that data + // results in [object Object] being written. Stringify them like a Buffer + // instead. + return JSON.stringify({ + type: "Buffer", + data: Array.from(arg), + }); } return JSON.stringify(arg);