Skip to content

Commit

Permalink
Fix random read at position 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Nov 30, 2024
1 parent 375e817 commit 3dc1bb2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
19 changes: 7 additions & 12 deletions lib/AbstractTokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,17 @@ export abstract class AbstractTokenizer implements ITokenizer {

protected normalizeOptions(uint8Array: Uint8Array, options?: IReadChunkOptions): INormalizedReadChunkOptions {

if (options && options.position !== undefined && options.position < this.position) {
if (!this.supportsRandomAccess() && options && options.position !== undefined && options.position < this.position) {
throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
}

if (options) {
return {
mayBeLess: options.mayBeLess === true,
length: options.length ? options.length : uint8Array.length,
position: options.position ? options.position : this.position
};
}

return {
mayBeLess: false,
length: uint8Array.length,
position: this.position
...{
mayBeLess: false,
offset: 0,
length: uint8Array.length,
position: this.position
}, ...options
};
}

Expand Down
10 changes: 10 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,16 @@ describe('Random-read-acccess', async () => {
}
});

it('Be able to random read from position 0', async () => {
const tokenizer = await fromFile(getResourcePath('id3v1.mp3'));
// Advance tokenizer.position
await tokenizer.ignore(20);
const mpegSync = new Uint8Array(2);
await tokenizer.readBuffer(mpegSync,{position: 0});
assert.strictEqual(mpegSync[0], 255, 'First sync byte');
assert.strictEqual(mpegSync[1], 251, 'Second sync byte');
});

});


Expand Down

0 comments on commit 3dc1bb2

Please sign in to comment.