Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class PbfReader {
*/
constructor(buf) {
this.buf = ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
this.dataView = new DataView(this.buf.buffer);
this.dataView = new DataView(this.buf.buffer, this.buf.byteOffset, this.buf.byteLength);
this.pos = 0;
this.type = 0;
this._valueStart = -1;
Expand Down Expand Up @@ -225,7 +225,7 @@ export class PbfWriter {
*/
constructor(buf = new Uint8Array(16)) {
this.buf = ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
this.dataView = new DataView(this.buf.buffer);
this.dataView = new DataView(this.buf.buffer, this.buf.byteOffset, this.buf.byteLength);
this.pos = 0;
this.length = this.buf.length;
}
Expand Down
27 changes: 27 additions & 0 deletions test/pbf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,33 @@ test('readDouble', () => {
assert.equal(Math.round(buf.readDouble() * 1e10) / 1e10, 12345.6789012345);
});

test('reads fixed-width fields from a view with a non-zero byteOffset', () => {
const backing = new ArrayBuffer(64);
const offset = 16;
const view = new Uint8Array(backing, offset, 48);
const dv = new DataView(backing, offset);
dv.setFloat64(0, 12345.6789012345, true);
dv.setFloat32(8, 42.5, true);
dv.setUint32(12, 0xdeadbeef, true);

const reader = new PbfReader(view);
assert.equal(Math.round(reader.readDouble() * 1e10) / 1e10, 12345.6789012345);
assert.equal(reader.readFloat(), 42.5);
assert.equal(reader.readFixed32(), 0xdeadbeef);
});

test('writes fixed-width fields to a view with a non-zero byteOffset', () => {
const backing = new ArrayBuffer(64);
const writer = new PbfWriter(new Uint8Array(backing, 16, 48));
writer.writeDoubleField(1, 12345.6789012345);

const reader = new PbfReader(new Uint8Array(writer.finish()));
reader.readFields((tag) => {
assert.equal(tag, 1);
assert.equal(Math.round(reader.readDouble() * 1e10) / 1e10, 12345.6789012345);
});
});

test('readPacked and writePacked', () => {
const testNumbers2 = testNumbers.slice(0, 10);

Expand Down
Loading