From a927a6646e8fdddebcb3e13bc8b28b041b3ee40a Mon Sep 17 00:00:00 2001 From: Assaf O Date: Mon, 30 Apr 2018 22:31:39 +0300 Subject: [PATCH] Converter: Fix empty 'bytes' field decoding, now using Buffer where applicable (#1020) --- src/converter.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/converter.js b/src/converter.js index 074610285..f819123f5 100644 --- a/src/converter.js +++ b/src/converter.js @@ -244,9 +244,15 @@ converter.toObject = function toObject(mtype) { ("d%s=o.longs===String?n.toString():o.longs===Number?n.toNumber():n", prop) ("}else") ("d%s=o.longs===String?%j:%i", prop, field.typeDefault.toString(), field.typeDefault.toNumber()); - else if (field.bytes) gen - ("d%s=o.bytes===String?%j:%s", prop, String.fromCharCode.apply(String, field.typeDefault), "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]"); - else gen + else if (field.bytes) { + var arrayDefault = "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]"; + gen + ("if(o.bytes===String)d%s=%j", prop, String.fromCharCode.apply(String, field.typeDefault)) + ("else{") + ("d%s=%s", prop, arrayDefault) + ("if(o.bytes!==Array)d%s=util.newBuffer(d%s)", prop, prop) + ("}") + } else gen ("d%s=%j", prop, field.typeDefault); // also messages (=null) } gen ("}");