Skip to content

Commit

Permalink
[Fix] Invalid typing for BufferReader/Writer options. (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoplaza authored Jul 18, 2024
1 parent ad5b013 commit 603fcb5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/io/bufferIOBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export type BufferIOOptions = {
/**
* @default 0
*/
byteOffset: number;
byteOffset?: number;
/**
* @default 'system'
*/
endianness: Endianness | 'system';
endianness?: Endianness | 'system';
};

export class BufferIOBase {
Expand All @@ -25,7 +25,13 @@ export class BufferIOBase {
public readonly endianness: Endianness;

constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) {
this.byteOffset = options?.byteOffset ?? 0;
const { byteOffset = 0, endianness = 'system' } = options ?? {};

this.byteOffset = byteOffset;

const systemEndianness = getSystemEndianness();
this.endianness = endianness === 'system' ? systemEndianness : endianness;
this.switchEndianness = this.endianness !== systemEndianness;

if (typeof Buffer !== 'undefined' && buffer instanceof Buffer) {
// Getting rid of the outer shell, which causes the Uint8Array line to create a copy, instead of a view.
Expand All @@ -40,15 +46,6 @@ export class BufferIOBase {
this.helperUint32View = new Uint32Array(helperBuffer);
this.helperFloatView = new Float32Array(helperBuffer);
this.helperByteView = new Uint8Array(helperBuffer);

const systemEndianness = getSystemEndianness();

this.endianness =
!options || options.endianness === 'system'
? systemEndianness
: options.endianness;

this.switchEndianness = this.endianness !== systemEndianness;
}

get currentByteOffset() {
Expand Down
21 changes: 21 additions & 0 deletions src/io/bufferReaderWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,29 @@ import { describe, it, expect } from 'vitest';
import { BufferWriter } from './bufferWriter';
import { BufferReader } from './bufferReader';
import { randBetween, randIntBetween } from '../test/random';
import { getSystemEndianness } from '../util';

describe('BufferWriter/BufferReader', () => {
it('parses options correctly', () => {
const buffer = Buffer.alloc(16);

const noOptions = new BufferWriter(buffer);
expect(noOptions.endianness).toEqual(getSystemEndianness());
expect(noOptions.currentByteOffset).toEqual(0);

const withByteOffset = new BufferWriter(buffer, { byteOffset: 16 });
expect(withByteOffset.endianness).toEqual(getSystemEndianness());
expect(withByteOffset.currentByteOffset).toEqual(16);

const withBigEndian = new BufferWriter(buffer, { endianness: 'big' });
expect(withBigEndian.endianness).toEqual('big');
expect(withBigEndian.currentByteOffset).toEqual(0);

const withLittleEndian = new BufferWriter(buffer, { endianness: 'little' });
expect(withLittleEndian.endianness).toEqual('little');
expect(withLittleEndian.currentByteOffset).toEqual(0);
});

it('should encode and decode int sequence', () => {
// Generating random int sequence
const intList = [];
Expand Down

0 comments on commit 603fcb5

Please sign in to comment.