Skip to content

Commit

Permalink
Make BufferType compliant with Uint8Array input type.
Browse files Browse the repository at this point in the history
Changes the behaviour of BufferType, that will now copy the internal buffer data.
  • Loading branch information
Borewit committed Feb 23, 2022
1 parent 075bc4b commit d9be633
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ export class BufferType implements IGetToken<Uint8Array, Buffer> {
public constructor(public len: number) {
}

public get(buffer: Buffer, off: number): Buffer {
return buffer.slice(off, off + this.len);
public get(uint8Array: Uint8Array, off: number): Buffer {
return Buffer.from(uint8Array.subarray(off, off + this.len));
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/test-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BufferType, StringType, Uint8ArrayType } from '../lib';

describe('BufferType', () => {

it('should should not copy data fom the source array', () => {
it('Should copy data fom the source array', () => {

const source = Buffer.from([0xa1, 0xa2, 0xb1, 0xb2, 0xc1, 0xc2]);

Expand All @@ -16,7 +16,7 @@ describe('BufferType', () => {
bufferResult[0] = 0xd1;
bufferResult[1] = 0xd2;

assert.deepStrictEqual(source, Buffer.from([0xa1, 0xa2, 0xd1, 0xd2, 0xc1, 0xc2]), 'should not copy the data');
assert.notDeepEqual(source, Buffer.from([0xa1, 0xa2, 0xd1, 0xd2, 0xc1, 0xc2]), 'should copy the data');
});
});

Expand Down

0 comments on commit d9be633

Please sign in to comment.