Skip to content

Commit

Permalink
fix: Respect view length when writing slice with BufferWriter (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoplaza authored Oct 5, 2024
1 parent e809845 commit 82bde4d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
15 changes: 15 additions & 0 deletions packages/typed-binary/src/io/bufferReaderWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ describe('BufferWriter', () => {
expect(reader.readUint32()).to.eq(8);
expect(reader.readUint32()).to.eq(4_294_967_295);
});

it('respects view length when writing slice (#35)', () => {
// Create a 512 byte ArrayBuffer.
const srcBuffer = new ArrayBuffer(512);

// Create a 100 byte Uint8Array view into the ArrayBuffer at offset 10.
const srcView = new Uint8Array(srcBuffer, 10, 100);

// Write the Uint8Array into a BufferWriter.
const dstBuffer = new ArrayBuffer(512);
const output = new BufferWriter(dstBuffer);
expect(output.currentByteOffset).toEqual(0);
output.writeSlice(srcView);
expect(output.currentByteOffset).toEqual(100);
});
});

describe('BufferReader', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/typed-binary/src/io/bufferWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ export class BufferWriter extends BufferIOBase implements ISerialOutput {

writeSlice(bufferView: ArrayLike<number> & ArrayBufferView): void {
const unwrapped = unwrapBuffer(bufferView);
const srcU8 = new Uint8Array(unwrapped.buffer, unwrapped.byteOffset);

const srcU8 = new Uint8Array(
unwrapped.buffer,
unwrapped.byteOffset,
unwrapped.byteLength,
);

for (const srcByte of srcU8) {
this.uint8View[this.byteOffset++] = srcByte;
}
Expand Down
12 changes: 10 additions & 2 deletions packages/typed-binary/src/io/unwrapBuffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
interface UnwrapBufferResult {
buffer: ArrayBufferLike;
byteOffset: number;
byteLength: number;
}

/**
* Removes up to one layer of view over a buffer.
*/
export function unwrapBuffer(buffer: ArrayBufferLike | ArrayBufferView) {
export function unwrapBuffer(
buffer: ArrayBufferLike | ArrayBufferView,
): UnwrapBufferResult {
let byteOffset = 0;
let innerBuffer = buffer;

Expand All @@ -11,5 +19,5 @@ export function unwrapBuffer(buffer: ArrayBufferLike | ArrayBufferView) {
innerBuffer = innerBuffer.buffer;
}

return { buffer: innerBuffer, byteOffset };
return { buffer: innerBuffer, byteOffset, byteLength: buffer.byteLength };
}

0 comments on commit 82bde4d

Please sign in to comment.