@@ -57,39 +57,43 @@ export const stringify = (arg: any, isError?: boolean): string => { // tslint:di
5757 * Parse an event argument.
5858 */
5959export const parse = ( arg : string ) : any => { // tslint:disable-line no-any
60- if ( ! arg ) {
61- return arg ;
62- }
63-
64- const result = JSON . parse ( arg ) ;
60+ const convert = ( value : any ) : any => { // tslint:disable-line no-any
61+ if ( value && value . data && value . type ) {
62+ switch ( value . type ) {
63+ // JSON.stringify turns a Buffer into an object but JSON.parse doesn't
64+ // turn it back, it just remains an object.
65+ case "Buffer" :
66+ if ( Array . isArray ( value . data ) ) {
67+ return Buffer . from ( value ) ;
68+ }
69+ break ;
70+ // Errors apparently can't be stringified, so we do something similar to
71+ // what happens to buffers and stringify them as regular objects.
72+ case "Error" :
73+ if ( value . data . message ) {
74+ const error = new Error ( value . data . message ) ;
75+ // TODO: Can we set the stack? Doing so seems to make it into an
76+ // "invalid object".
77+ if ( typeof value . data . code !== "undefined" ) {
78+ ( error as NodeJS . ErrnoException ) . code = value . data . code ;
79+ }
80+ // tslint:disable-next-line no-any
81+ ( error as any ) . originalStack = value . data . stack ;
6582
66- if ( result && result . data && result . type ) {
67- switch ( result . type ) {
68- // JSON.stringify turns a Buffer into an object but JSON.parse doesn't
69- // turn it back, it just remains an object.
70- case "Buffer" :
71- if ( Array . isArray ( result . data ) ) {
72- return Buffer . from ( result ) ;
73- }
74- break ;
75- // Errors apparently can't be stringified, so we do something similar to
76- // what happens to buffers and stringify them as regular objects.
77- case "Error" :
78- if ( result . data . message ) {
79- const error = new Error ( result . data . message ) ;
80- // TODO: Can we set the stack? Doing so seems to make it into an
81- // "invalid object".
82- if ( typeof result . data . code !== "undefined" ) {
83- ( error as NodeJS . ErrnoException ) . code = result . data . code ;
83+ return error ;
8484 }
85- // tslint:disable-next-line no-any
86- ( error as any ) . originalStack = result . data . stack ;
85+ break ;
86+ }
87+ }
8788
88- return error ;
89- }
90- break ;
89+ if ( value && typeof value === "object" ) {
90+ Object . keys ( value ) . forEach ( ( key ) => {
91+ value [ key ] = convert ( value [ key ] ) ;
92+ } ) ;
9193 }
92- }
9394
94- return result ;
95+ return value ;
96+ } ;
97+
98+ return arg ? convert ( JSON . parse ( arg ) ) : arg ;
9599} ;
0 commit comments