From 603fcb565343f81bd2f38aabdc143611eff8cb03 Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Thu, 18 Jul 2024 14:53:11 +0200 Subject: [PATCH 1/4] [Fix] Invalid typing for BufferReader/Writer options. (#17) --- src/io/bufferIOBase.ts | 21 +++++++++------------ src/io/bufferReaderWriter.test.ts | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/io/bufferIOBase.ts b/src/io/bufferIOBase.ts index 9da3e85..d0fa8e7 100644 --- a/src/io/bufferIOBase.ts +++ b/src/io/bufferIOBase.ts @@ -5,11 +5,11 @@ export type BufferIOOptions = { /** * @default 0 */ - byteOffset: number; + byteOffset?: number; /** * @default 'system' */ - endianness: Endianness | 'system'; + endianness?: Endianness | 'system'; }; export class BufferIOBase { @@ -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. @@ -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() { diff --git a/src/io/bufferReaderWriter.test.ts b/src/io/bufferReaderWriter.test.ts index 80cecae..60858ab 100644 --- a/src/io/bufferReaderWriter.test.ts +++ b/src/io/bufferReaderWriter.test.ts @@ -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 = []; From 47a879512247a3b161c88566d86bcb1dbbbabba7 Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Thu, 18 Jul 2024 14:54:39 +0200 Subject: [PATCH 2/4] v4.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1661b1a..5f44312 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typed-binary", - "version": "4.0.0", + "version": "4.0.1", "description": "Describe binary structures with full TypeScript support. Encode and decode into pure JavaScript objects.", "packageManager": "pnpm@8.15.8+sha256.691fe176eea9a8a80df20e4976f3dfb44a04841ceb885638fe2a26174f81e65e", "main": "dist/cjs/index.js", From f2c5c52894366dbc7c7fcc93c809e6d59023a93f Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Thu, 18 Jul 2024 15:00:09 +0200 Subject: [PATCH 3/4] Small type error suppression --- src/utilityTypes.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utilityTypes.ts b/src/utilityTypes.ts index 3cb1c8b..2e34af2 100644 --- a/src/utilityTypes.ts +++ b/src/utilityTypes.ts @@ -38,7 +38,8 @@ export type MergeRecords = { * ``` */ export type MergeRecordUnion = { - [K in DistributedKeyOf]: Extract[K]; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + [K in DistributedKeyOf]: Extract[K]; }; export type Parsed< From 16d8c23b89e7002b742955d692943ef2d097c5ff Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Thu, 18 Jul 2024 15:01:34 +0200 Subject: [PATCH 4/4] fix: Publishing with pnpm too --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5f44312..b9421cc 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "lint": "eslint .", "build": "rollup -c", "prepublishOnly": "pnpm lint && pnpm build", - "dryPublish": "npm publish --dry-run", + "dryPublish": "pnpm publish --dry-run", "test": "vitest", "ts-version": "tsc -v" },