From 594b379a77317bee80b3eb8c9197c7935065dbbf Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 19 Nov 2018 16:55:14 +0000 Subject: [PATCH] Ability to feed JS Decoder --- implementations/javascript/src/codec.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/implementations/javascript/src/codec.js b/implementations/javascript/src/codec.js index 61482e0..d2cf154 100644 --- a/implementations/javascript/src/codec.js +++ b/implementations/javascript/src/codec.js @@ -18,11 +18,18 @@ class ShortPacket extends DecodeError {} class Decoder { constructor(packet, options) { options = options || {}; - this.packet = packet._view || packet; // strip off Bytes wrapper, if any + this.packet = packet + ? (packet._view || packet) // strip off Bytes wrapper, if any + : new Uint8Array(0); this.index = 0; this.shortForms = options.shortForms || {}; } + write(data) { + this.packet = Bytes.concat([this.packet.slice(this.index), data])._view; + this.index = 0; + } + nextbyte() { if (this.index >= this.packet.length) throw new ShortPacket("Short packet"); // ^ NOTE: greater-than-or-equal-to, not greater-than. @@ -174,6 +181,19 @@ class Decoder { return this.decodecollection(minor, this.nextvalues(this.wirelength(arg))); } } + + try_next() { + const start = this.index; + try { + return this.next(); + } catch (e) { + if (e instanceof ShortPacket) { + this.index = start; + return void 0; + } + throw e; + } + } } class Encoder {