Fix stringifying Uint8Array

This commit is contained in:
Asher 2019-02-21 13:29:08 -06:00
parent e4150de154
commit fe107802e3
No known key found for this signature in database
GPG Key ID: 7BB4BA9C783D2BBC
1 changed files with 10 additions and 0 deletions

View File

@ -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);