From 80c55e4f30b720243ea220cb33ff33ebad761e76 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 15 Nov 2018 23:40:48 +0000 Subject: [PATCH] Repair error in Decoder.nextbytes --- implementations/javascript/package.json | 2 +- implementations/javascript/src/codec.js | 2 +- implementations/javascript/test/test-codec.js | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/implementations/javascript/package.json b/implementations/javascript/package.json index 7cab7ad..692d070 100644 --- a/implementations/javascript/package.json +++ b/implementations/javascript/package.json @@ -1,6 +1,6 @@ { "name": "preserves", - "version": "0.0.2", + "version": "0.0.3", "description": "Experimental data serialization format", "homepage": "https://gitlab.com/tonyg/preserves", "license": "MIT", diff --git a/implementations/javascript/src/codec.js b/implementations/javascript/src/codec.js index 4f08317..25595c1 100644 --- a/implementations/javascript/src/codec.js +++ b/implementations/javascript/src/codec.js @@ -56,7 +56,7 @@ class Decoder { const start = this.advance(n); if (this.index > this.packet.length) throw new DecodeError("Short packet"); // ^ NOTE: greater-than, not greater-than-or-equal-to. - return new DataView(this.packet.buffer, start, n); + return new DataView(this.packet.buffer, this.packet.byteOffset + start, n); } nextvalues(n) { diff --git a/implementations/javascript/test/test-codec.js b/implementations/javascript/test/test-codec.js index 3b3c74e..0d5f93c 100644 --- a/implementations/javascript/test/test-codec.js +++ b/implementations/javascript/test/test-codec.js @@ -141,3 +141,11 @@ describe('hex samples', () => { }); }); }); + +describe('parsing from subarray', () => { + it('should maintain alignment of nextbytes', () => { + const u = Uint8Array.of(57, 57, 57, 57, 83, 51, 51, 51); + const bs = Bytes.from(u.subarray(4)); + expect(new Decoder(bs).next()).to.equal("333"); + }); +});