From 1e778654da527bceb416b7785b630d4b7c7b403b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:09:06 +0000 Subject: [PATCH 1/4] Initial plan From c0191402e476049a6822ca8a59793f421290067b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:15:53 +0000 Subject: [PATCH 2/4] Add peek() method and deprecate peak() with backward compatibility Co-authored-by: streamich <9773803+streamich@users.noreply.github.com> --- src/buffers/Reader.ts | 10 +- src/buffers/StreamingOctetReader.ts | 10 +- src/buffers/StreamingReader.ts | 10 +- src/buffers/__tests__/peak-peek.spec.ts | 120 ++++++++++++++++++++++++ src/buffers/types.ts | 6 ++ 5 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 src/buffers/__tests__/peak-peek.spec.ts diff --git a/src/buffers/Reader.ts b/src/buffers/Reader.ts index 160e9cd..fa6de91 100644 --- a/src/buffers/Reader.ts +++ b/src/buffers/Reader.ts @@ -12,10 +12,18 @@ export class Reader implements IReader, IReaderResettable { this.view = new DataView(uint8.buffer, uint8.byteOffset, uint8.length); } - public peak(): number { + public peek(): number { return this.view.getUint8(this.x); } + /** + * Get current byte value without advancing the cursor. + * @deprecated Use peek() instead. + */ + public peak(): number { + return this.peek(); + } + public skip(length: number): void { this.x += length; } diff --git a/src/buffers/StreamingOctetReader.ts b/src/buffers/StreamingOctetReader.ts index b254a75..300c47c 100644 --- a/src/buffers/StreamingOctetReader.ts +++ b/src/buffers/StreamingOctetReader.ts @@ -129,11 +129,19 @@ export class StreamingOctetReader { this.skipUnsafe(n); } - public peak(): number { + public peek(): number { this.assertSize(1); return this.chunks[0]![this.x]; } + /** + * Get current byte value without advancing the cursor. + * @deprecated Use peek() instead. + */ + public peak(): number { + return this.peek(); + } + public utf8(length: number, mask: [number, number, number, number], maskIndex: number): string { this.assertSize(length); let i = 0; diff --git a/src/buffers/StreamingReader.ts b/src/buffers/StreamingReader.ts index 1695b73..03d214e 100644 --- a/src/buffers/StreamingReader.ts +++ b/src/buffers/StreamingReader.ts @@ -68,11 +68,19 @@ export class StreamingReader implements IReader, IReaderResettable { this.dx = x - this.writer.x0; } - public peak(): number { + public peek(): number { this.assertSize(1); return this.view.getUint8(this.x); } + /** + * Get current byte value without advancing the cursor. + * @deprecated Use peek() instead. + */ + public peak(): number { + return this.peek(); + } + public skip(length: number): void { this.assertSize(length); this.x += length; diff --git a/src/buffers/__tests__/peak-peek.spec.ts b/src/buffers/__tests__/peak-peek.spec.ts new file mode 100644 index 0000000..b9ceae7 --- /dev/null +++ b/src/buffers/__tests__/peak-peek.spec.ts @@ -0,0 +1,120 @@ +import {Reader} from '../Reader'; +import {StreamingReader} from '../StreamingReader'; +import {StreamingOctetReader} from '../StreamingOctetReader'; + +describe('peak() and peek() methods', () => { + describe('Reader', () => { + test('peak() returns current byte without advancing cursor', () => { + const reader = new Reader(); + reader.reset(new Uint8Array([1, 2, 3, 4, 5])); + + expect(reader.x).toBe(0); + expect(reader.peak()).toBe(1); + expect(reader.x).toBe(0); // cursor should not advance + + reader.u8(); // advance cursor + expect(reader.x).toBe(1); + expect(reader.peak()).toBe(2); + expect(reader.x).toBe(1); // cursor should not advance + }); + + test('peek() returns current byte without advancing cursor', () => { + const reader = new Reader(); + reader.reset(new Uint8Array([1, 2, 3, 4, 5])); + + expect(reader.x).toBe(0); + expect(reader.peek()).toBe(1); + expect(reader.x).toBe(0); // cursor should not advance + + reader.u8(); // advance cursor + expect(reader.x).toBe(1); + expect(reader.peek()).toBe(2); + expect(reader.x).toBe(1); // cursor should not advance + }); + + test('peak() and peek() return the same value', () => { + const reader = new Reader(); + reader.reset(new Uint8Array([42, 100, 255])); + + expect(reader.peak()).toBe(reader.peek()); + reader.u8(); // advance cursor + expect(reader.peak()).toBe(reader.peek()); + reader.u8(); // advance cursor + expect(reader.peak()).toBe(reader.peek()); + }); + }); + + describe('StreamingReader', () => { + test('peak() returns current byte without advancing cursor', () => { + const reader = new StreamingReader(); + reader.push(new Uint8Array([10, 20, 30])); + + expect(reader.x).toBe(0); + expect(reader.peak()).toBe(10); + expect(reader.x).toBe(0); // cursor should not advance + + reader.u8(); // advance cursor + expect(reader.x).toBe(1); + expect(reader.peak()).toBe(20); + expect(reader.x).toBe(1); // cursor should not advance + }); + + test('peek() returns current byte without advancing cursor', () => { + const reader = new StreamingReader(); + reader.push(new Uint8Array([10, 20, 30])); + + expect(reader.x).toBe(0); + expect(reader.peek()).toBe(10); + expect(reader.x).toBe(0); // cursor should not advance + + reader.u8(); // advance cursor + expect(reader.x).toBe(1); + expect(reader.peek()).toBe(20); + expect(reader.x).toBe(1); // cursor should not advance + }); + + test('peak() and peek() return the same value', () => { + const reader = new StreamingReader(); + reader.push(new Uint8Array([42, 100, 255])); + + expect(reader.peak()).toBe(reader.peek()); + reader.u8(); // advance cursor + expect(reader.peak()).toBe(reader.peek()); + reader.u8(); // advance cursor + expect(reader.peak()).toBe(reader.peek()); + }); + }); + + describe('StreamingOctetReader', () => { + test('peak() returns current byte without advancing cursor', () => { + const reader = new StreamingOctetReader(); + reader.push(new Uint8Array([100, 200, 150])); + + expect(reader.peak()).toBe(100); + + reader.u8(); // advance cursor internally + expect(reader.peak()).toBe(200); + }); + + test('peek() returns current byte without advancing cursor', () => { + const reader = new StreamingOctetReader(); + reader.push(new Uint8Array([100, 200, 150])); + + expect(reader.peek()).toBe(100); + + reader.u8(); // advance cursor internally + expect(reader.peek()).toBe(200); + }); + + test('peak() and peek() return the same value', () => { + const reader = new StreamingOctetReader(); + reader.push(new Uint8Array([42, 100, 255])); + + expect(reader.peak()).toBe(reader.peek()); + reader.u8(); // advance cursor + expect(reader.peak()).toBe(reader.peek()); + reader.u8(); // advance cursor + expect(reader.peak()).toBe(reader.peek()); + }); + }); +}); \ No newline at end of file diff --git a/src/buffers/types.ts b/src/buffers/types.ts index ae29727..bb100fc 100644 --- a/src/buffers/types.ts +++ b/src/buffers/types.ts @@ -71,6 +71,12 @@ export interface IWriterGrowable { export interface IReaderBase { /** Get current byte value without advancing the cursor. */ + peek(): number; + + /** + * Get current byte value without advancing the cursor. + * @deprecated Use peek() instead. + */ peak(): number; /** Advance the cursor given number of octets. */ From 59343e1583df88ab5228455e965f3e6270fea9eb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:26:37 +0000 Subject: [PATCH 3/4] Move JSDoc description to peek() method and remove whitespace in test Co-authored-by: streamich <9773803+streamich@users.noreply.github.com> --- src/buffers/Reader.ts | 4 +++- src/buffers/__tests__/peak-peek.spec.ts | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/buffers/Reader.ts b/src/buffers/Reader.ts index fa6de91..4b1ea68 100644 --- a/src/buffers/Reader.ts +++ b/src/buffers/Reader.ts @@ -12,12 +12,14 @@ export class Reader implements IReader, IReaderResettable { this.view = new DataView(uint8.buffer, uint8.byteOffset, uint8.length); } + /** + * Get current byte value without advancing the cursor. + */ public peek(): number { return this.view.getUint8(this.x); } /** - * Get current byte value without advancing the cursor. * @deprecated Use peek() instead. */ public peak(): number { diff --git a/src/buffers/__tests__/peak-peek.spec.ts b/src/buffers/__tests__/peak-peek.spec.ts index b9ceae7..326c5d5 100644 --- a/src/buffers/__tests__/peak-peek.spec.ts +++ b/src/buffers/__tests__/peak-peek.spec.ts @@ -7,7 +7,6 @@ describe('peak() and peek() methods', () => { test('peak() returns current byte without advancing cursor', () => { const reader = new Reader(); reader.reset(new Uint8Array([1, 2, 3, 4, 5])); - expect(reader.x).toBe(0); expect(reader.peak()).toBe(1); expect(reader.x).toBe(0); // cursor should not advance From f0fd4ff2455d7441e2cbf0a5115c026e71989555 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:33:45 +0000 Subject: [PATCH 4/4] style: format code with prettier Co-authored-by: streamich <9773803+streamich@users.noreply.github.com> --- src/buffers/Reader.ts | 2 +- src/buffers/StreamingOctetReader.ts | 2 +- src/buffers/StreamingReader.ts | 2 +- src/buffers/__tests__/peak-peek.spec.ts | 36 ++++++++++++------------- src/buffers/types.ts | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/buffers/Reader.ts b/src/buffers/Reader.ts index 4b1ea68..b2ee0b4 100644 --- a/src/buffers/Reader.ts +++ b/src/buffers/Reader.ts @@ -19,7 +19,7 @@ export class Reader implements IReader, IReaderResettable { return this.view.getUint8(this.x); } - /** + /** * @deprecated Use peek() instead. */ public peak(): number { diff --git a/src/buffers/StreamingOctetReader.ts b/src/buffers/StreamingOctetReader.ts index 300c47c..5370e7b 100644 --- a/src/buffers/StreamingOctetReader.ts +++ b/src/buffers/StreamingOctetReader.ts @@ -134,7 +134,7 @@ export class StreamingOctetReader { return this.chunks[0]![this.x]; } - /** + /** * Get current byte value without advancing the cursor. * @deprecated Use peek() instead. */ diff --git a/src/buffers/StreamingReader.ts b/src/buffers/StreamingReader.ts index 03d214e..34caac1 100644 --- a/src/buffers/StreamingReader.ts +++ b/src/buffers/StreamingReader.ts @@ -73,7 +73,7 @@ export class StreamingReader implements IReader, IReaderResettable { return this.view.getUint8(this.x); } - /** + /** * Get current byte value without advancing the cursor. * @deprecated Use peek() instead. */ diff --git a/src/buffers/__tests__/peak-peek.spec.ts b/src/buffers/__tests__/peak-peek.spec.ts index 326c5d5..ac25dd6 100644 --- a/src/buffers/__tests__/peak-peek.spec.ts +++ b/src/buffers/__tests__/peak-peek.spec.ts @@ -10,7 +10,7 @@ describe('peak() and peek() methods', () => { expect(reader.x).toBe(0); expect(reader.peak()).toBe(1); expect(reader.x).toBe(0); // cursor should not advance - + reader.u8(); // advance cursor expect(reader.x).toBe(1); expect(reader.peak()).toBe(2); @@ -20,11 +20,11 @@ describe('peak() and peek() methods', () => { test('peek() returns current byte without advancing cursor', () => { const reader = new Reader(); reader.reset(new Uint8Array([1, 2, 3, 4, 5])); - + expect(reader.x).toBe(0); expect(reader.peek()).toBe(1); expect(reader.x).toBe(0); // cursor should not advance - + reader.u8(); // advance cursor expect(reader.x).toBe(1); expect(reader.peek()).toBe(2); @@ -34,11 +34,11 @@ describe('peak() and peek() methods', () => { test('peak() and peek() return the same value', () => { const reader = new Reader(); reader.reset(new Uint8Array([42, 100, 255])); - + expect(reader.peak()).toBe(reader.peek()); reader.u8(); // advance cursor expect(reader.peak()).toBe(reader.peek()); - reader.u8(); // advance cursor + reader.u8(); // advance cursor expect(reader.peak()).toBe(reader.peek()); }); }); @@ -47,11 +47,11 @@ describe('peak() and peek() methods', () => { test('peak() returns current byte without advancing cursor', () => { const reader = new StreamingReader(); reader.push(new Uint8Array([10, 20, 30])); - + expect(reader.x).toBe(0); expect(reader.peak()).toBe(10); expect(reader.x).toBe(0); // cursor should not advance - + reader.u8(); // advance cursor expect(reader.x).toBe(1); expect(reader.peak()).toBe(20); @@ -61,11 +61,11 @@ describe('peak() and peek() methods', () => { test('peek() returns current byte without advancing cursor', () => { const reader = new StreamingReader(); reader.push(new Uint8Array([10, 20, 30])); - + expect(reader.x).toBe(0); expect(reader.peek()).toBe(10); expect(reader.x).toBe(0); // cursor should not advance - + reader.u8(); // advance cursor expect(reader.x).toBe(1); expect(reader.peek()).toBe(20); @@ -75,11 +75,11 @@ describe('peak() and peek() methods', () => { test('peak() and peek() return the same value', () => { const reader = new StreamingReader(); reader.push(new Uint8Array([42, 100, 255])); - + expect(reader.peak()).toBe(reader.peek()); reader.u8(); // advance cursor expect(reader.peak()).toBe(reader.peek()); - reader.u8(); // advance cursor + reader.u8(); // advance cursor expect(reader.peak()).toBe(reader.peek()); }); }); @@ -88,9 +88,9 @@ describe('peak() and peek() methods', () => { test('peak() returns current byte without advancing cursor', () => { const reader = new StreamingOctetReader(); reader.push(new Uint8Array([100, 200, 150])); - + expect(reader.peak()).toBe(100); - + reader.u8(); // advance cursor internally expect(reader.peak()).toBe(200); }); @@ -98,9 +98,9 @@ describe('peak() and peek() methods', () => { test('peek() returns current byte without advancing cursor', () => { const reader = new StreamingOctetReader(); reader.push(new Uint8Array([100, 200, 150])); - + expect(reader.peek()).toBe(100); - + reader.u8(); // advance cursor internally expect(reader.peek()).toBe(200); }); @@ -108,12 +108,12 @@ describe('peak() and peek() methods', () => { test('peak() and peek() return the same value', () => { const reader = new StreamingOctetReader(); reader.push(new Uint8Array([42, 100, 255])); - + expect(reader.peak()).toBe(reader.peek()); reader.u8(); // advance cursor expect(reader.peak()).toBe(reader.peek()); - reader.u8(); // advance cursor + reader.u8(); // advance cursor expect(reader.peak()).toBe(reader.peek()); }); }); -}); \ No newline at end of file +}); diff --git a/src/buffers/types.ts b/src/buffers/types.ts index bb100fc..2345713 100644 --- a/src/buffers/types.ts +++ b/src/buffers/types.ts @@ -73,7 +73,7 @@ export interface IReaderBase { /** Get current byte value without advancing the cursor. */ peek(): number; - /** + /** * Get current byte value without advancing the cursor. * @deprecated Use peek() instead. */