strtok3 7.0.0-alpha.7 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,69 +1,69 @@
1
- import { ITokenizer, IFileInfo, IReadChunkOptions } from './types';
2
- import { IGetToken, IToken } from '@tokenizer/token';
3
- interface INormalizedReadChunkOptions extends IReadChunkOptions {
4
- offset: number;
5
- length: number;
6
- position: number;
7
- mayBeLess?: boolean;
8
- }
9
- /**
10
- * Core tokenizer
11
- */
12
- export declare abstract class AbstractTokenizer implements ITokenizer {
13
- fileInfo: IFileInfo;
14
- protected constructor(fileInfo?: IFileInfo);
15
- /**
16
- * Tokenizer-stream position
17
- */
18
- position: number;
19
- private numBuffer;
20
- /**
21
- * Read buffer from tokenizer
22
- * @param buffer - Target buffer to fill with data read from the tokenizer-stream
23
- * @param options - Additional read options
24
- * @returns Promise with number of bytes read
25
- */
26
- abstract readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
27
- /**
28
- * Peek (read ahead) buffer from tokenizer
29
- * @param uint8Array- Target buffer to fill with data peek from the tokenizer-stream
30
- * @param options - Peek behaviour options
31
- * @returns Promise with number of bytes read
32
- */
33
- abstract peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
34
- /**
35
- * Read a token from the tokenizer-stream
36
- * @param token - The token to read
37
- * @param position - If provided, the desired position in the tokenizer-stream
38
- * @returns Promise with token data
39
- */
40
- readToken<Value>(token: IGetToken<Value>, position?: number): Promise<Value>;
41
- /**
42
- * Peek a token from the tokenizer-stream.
43
- * @param token - Token to peek from the tokenizer-stream.
44
- * @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
45
- * @returns Promise with token data
46
- */
47
- peekToken<Value>(token: IGetToken<Value>, position?: number): Promise<Value>;
48
- /**
49
- * Read a numeric token from the stream
50
- * @param token - Numeric token
51
- * @returns Promise with number
52
- */
53
- readNumber(token: IToken<number>): Promise<number>;
54
- /**
55
- * Read a numeric token from the stream
56
- * @param token - Numeric token
57
- * @returns Promise with number
58
- */
59
- peekNumber(token: IToken<number>): Promise<number>;
60
- /**
61
- * Ignore number of bytes, advances the pointer in under tokenizer-stream.
62
- * @param length - Number of bytes to ignore
63
- * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
64
- */
65
- ignore(length: number): Promise<number>;
66
- close(): Promise<void>;
67
- protected normalizeOptions(uint8Array: Uint8Array, options?: IReadChunkOptions): INormalizedReadChunkOptions;
68
- }
69
- export {};
1
+ import { ITokenizer, IFileInfo, IReadChunkOptions } from './types.js';
2
+ import { IGetToken, IToken } from '@tokenizer/token';
3
+ interface INormalizedReadChunkOptions extends IReadChunkOptions {
4
+ offset: number;
5
+ length: number;
6
+ position: number;
7
+ mayBeLess?: boolean;
8
+ }
9
+ /**
10
+ * Core tokenizer
11
+ */
12
+ export declare abstract class AbstractTokenizer implements ITokenizer {
13
+ fileInfo: IFileInfo;
14
+ protected constructor(fileInfo?: IFileInfo);
15
+ /**
16
+ * Tokenizer-stream position
17
+ */
18
+ position: number;
19
+ private numBuffer;
20
+ /**
21
+ * Read buffer from tokenizer
22
+ * @param buffer - Target buffer to fill with data read from the tokenizer-stream
23
+ * @param options - Additional read options
24
+ * @returns Promise with number of bytes read
25
+ */
26
+ abstract readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
27
+ /**
28
+ * Peek (read ahead) buffer from tokenizer
29
+ * @param uint8Array- Target buffer to fill with data peek from the tokenizer-stream
30
+ * @param options - Peek behaviour options
31
+ * @returns Promise with number of bytes read
32
+ */
33
+ abstract peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
34
+ /**
35
+ * Read a token from the tokenizer-stream
36
+ * @param token - The token to read
37
+ * @param position - If provided, the desired position in the tokenizer-stream
38
+ * @returns Promise with token data
39
+ */
40
+ readToken<Value>(token: IGetToken<Value>, position?: number): Promise<Value>;
41
+ /**
42
+ * Peek a token from the tokenizer-stream.
43
+ * @param token - Token to peek from the tokenizer-stream.
44
+ * @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
45
+ * @returns Promise with token data
46
+ */
47
+ peekToken<Value>(token: IGetToken<Value>, position?: number): Promise<Value>;
48
+ /**
49
+ * Read a numeric token from the stream
50
+ * @param token - Numeric token
51
+ * @returns Promise with number
52
+ */
53
+ readNumber(token: IToken<number>): Promise<number>;
54
+ /**
55
+ * Read a numeric token from the stream
56
+ * @param token - Numeric token
57
+ * @returns Promise with number
58
+ */
59
+ peekNumber(token: IToken<number>): Promise<number>;
60
+ /**
61
+ * Ignore number of bytes, advances the pointer in under tokenizer-stream.
62
+ * @param length - Number of bytes to ignore
63
+ * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
64
+ */
65
+ ignore(length: number): Promise<number>;
66
+ close(): Promise<void>;
67
+ protected normalizeOptions(uint8Array: Uint8Array, options?: IReadChunkOptions): INormalizedReadChunkOptions;
68
+ }
69
+ export {};
@@ -1,101 +1,101 @@
1
- import { EndOfStreamError } from 'peek-readable';
2
- import { Buffer } from 'node:buffer';
3
- /**
4
- * Core tokenizer
5
- */
6
- export class AbstractTokenizer {
7
- constructor(fileInfo) {
8
- /**
9
- * Tokenizer-stream position
10
- */
11
- this.position = 0;
12
- this.numBuffer = new Uint8Array(8);
13
- this.fileInfo = fileInfo ? fileInfo : {};
14
- }
15
- /**
16
- * Read a token from the tokenizer-stream
17
- * @param token - The token to read
18
- * @param position - If provided, the desired position in the tokenizer-stream
19
- * @returns Promise with token data
20
- */
21
- async readToken(token, position = this.position) {
22
- const uint8Array = Buffer.alloc(token.len);
23
- const len = await this.readBuffer(uint8Array, { position });
24
- if (len < token.len)
25
- throw new EndOfStreamError();
26
- return token.get(uint8Array, 0);
27
- }
28
- /**
29
- * Peek a token from the tokenizer-stream.
30
- * @param token - Token to peek from the tokenizer-stream.
31
- * @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
32
- * @returns Promise with token data
33
- */
34
- async peekToken(token, position = this.position) {
35
- const uint8Array = Buffer.alloc(token.len);
36
- const len = await this.peekBuffer(uint8Array, { position });
37
- if (len < token.len)
38
- throw new EndOfStreamError();
39
- return token.get(uint8Array, 0);
40
- }
41
- /**
42
- * Read a numeric token from the stream
43
- * @param token - Numeric token
44
- * @returns Promise with number
45
- */
46
- async readNumber(token) {
47
- const len = await this.readBuffer(this.numBuffer, { length: token.len });
48
- if (len < token.len)
49
- throw new EndOfStreamError();
50
- return token.get(this.numBuffer, 0);
51
- }
52
- /**
53
- * Read a numeric token from the stream
54
- * @param token - Numeric token
55
- * @returns Promise with number
56
- */
57
- async peekNumber(token) {
58
- const len = await this.peekBuffer(this.numBuffer, { length: token.len });
59
- if (len < token.len)
60
- throw new EndOfStreamError();
61
- return token.get(this.numBuffer, 0);
62
- }
63
- /**
64
- * Ignore number of bytes, advances the pointer in under tokenizer-stream.
65
- * @param length - Number of bytes to ignore
66
- * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
67
- */
68
- async ignore(length) {
69
- if (this.fileInfo.size !== undefined) {
70
- const bytesLeft = this.fileInfo.size - this.position;
71
- if (length > bytesLeft) {
72
- this.position += bytesLeft;
73
- return bytesLeft;
74
- }
75
- }
76
- this.position += length;
77
- return length;
78
- }
79
- async close() {
80
- // empty
81
- }
82
- normalizeOptions(uint8Array, options) {
83
- if (options && options.position !== undefined && options.position < this.position) {
84
- throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
85
- }
86
- if (options) {
87
- return {
88
- mayBeLess: options.mayBeLess === true,
89
- offset: options.offset ? options.offset : 0,
90
- length: options.length ? options.length : (uint8Array.length - (options.offset ? options.offset : 0)),
91
- position: options.position ? options.position : this.position
92
- };
93
- }
94
- return {
95
- mayBeLess: false,
96
- offset: 0,
97
- length: uint8Array.length,
98
- position: this.position
99
- };
100
- }
101
- }
1
+ import { EndOfStreamError } from 'peek-readable';
2
+ import { Buffer } from 'node:buffer';
3
+ /**
4
+ * Core tokenizer
5
+ */
6
+ export class AbstractTokenizer {
7
+ constructor(fileInfo) {
8
+ /**
9
+ * Tokenizer-stream position
10
+ */
11
+ this.position = 0;
12
+ this.numBuffer = new Uint8Array(8);
13
+ this.fileInfo = fileInfo ? fileInfo : {};
14
+ }
15
+ /**
16
+ * Read a token from the tokenizer-stream
17
+ * @param token - The token to read
18
+ * @param position - If provided, the desired position in the tokenizer-stream
19
+ * @returns Promise with token data
20
+ */
21
+ async readToken(token, position = this.position) {
22
+ const uint8Array = Buffer.alloc(token.len);
23
+ const len = await this.readBuffer(uint8Array, { position });
24
+ if (len < token.len)
25
+ throw new EndOfStreamError();
26
+ return token.get(uint8Array, 0);
27
+ }
28
+ /**
29
+ * Peek a token from the tokenizer-stream.
30
+ * @param token - Token to peek from the tokenizer-stream.
31
+ * @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
32
+ * @returns Promise with token data
33
+ */
34
+ async peekToken(token, position = this.position) {
35
+ const uint8Array = Buffer.alloc(token.len);
36
+ const len = await this.peekBuffer(uint8Array, { position });
37
+ if (len < token.len)
38
+ throw new EndOfStreamError();
39
+ return token.get(uint8Array, 0);
40
+ }
41
+ /**
42
+ * Read a numeric token from the stream
43
+ * @param token - Numeric token
44
+ * @returns Promise with number
45
+ */
46
+ async readNumber(token) {
47
+ const len = await this.readBuffer(this.numBuffer, { length: token.len });
48
+ if (len < token.len)
49
+ throw new EndOfStreamError();
50
+ return token.get(this.numBuffer, 0);
51
+ }
52
+ /**
53
+ * Read a numeric token from the stream
54
+ * @param token - Numeric token
55
+ * @returns Promise with number
56
+ */
57
+ async peekNumber(token) {
58
+ const len = await this.peekBuffer(this.numBuffer, { length: token.len });
59
+ if (len < token.len)
60
+ throw new EndOfStreamError();
61
+ return token.get(this.numBuffer, 0);
62
+ }
63
+ /**
64
+ * Ignore number of bytes, advances the pointer in under tokenizer-stream.
65
+ * @param length - Number of bytes to ignore
66
+ * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
67
+ */
68
+ async ignore(length) {
69
+ if (this.fileInfo.size !== undefined) {
70
+ const bytesLeft = this.fileInfo.size - this.position;
71
+ if (length > bytesLeft) {
72
+ this.position += bytesLeft;
73
+ return bytesLeft;
74
+ }
75
+ }
76
+ this.position += length;
77
+ return length;
78
+ }
79
+ async close() {
80
+ // empty
81
+ }
82
+ normalizeOptions(uint8Array, options) {
83
+ if (options && options.position !== undefined && options.position < this.position) {
84
+ throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
85
+ }
86
+ if (options) {
87
+ return {
88
+ mayBeLess: options.mayBeLess === true,
89
+ offset: options.offset ? options.offset : 0,
90
+ length: options.length ? options.length : (uint8Array.length - (options.offset ? options.offset : 0)),
91
+ position: options.position ? options.position : this.position
92
+ };
93
+ }
94
+ return {
95
+ mayBeLess: false,
96
+ offset: 0,
97
+ length: uint8Array.length,
98
+ position: this.position
99
+ };
100
+ }
101
+ }
@@ -1,26 +1,26 @@
1
- import { IFileInfo, IReadChunkOptions } from './types';
2
- import { AbstractTokenizer } from './AbstractTokenizer.js';
3
- export declare class BufferTokenizer extends AbstractTokenizer {
4
- private uint8Array;
5
- /**
6
- * Construct BufferTokenizer
7
- * @param uint8Array - Uint8Array to tokenize
8
- * @param fileInfo - Pass additional file information to the tokenizer
9
- */
10
- constructor(uint8Array: Uint8Array, fileInfo?: IFileInfo);
11
- /**
12
- * Read buffer from tokenizer
13
- * @param uint8Array - Uint8Array to tokenize
14
- * @param options - Read behaviour options
15
- * @returns {Promise<number>}
16
- */
17
- readBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
18
- /**
19
- * Peek (read ahead) buffer from tokenizer
20
- * @param uint8Array
21
- * @param options - Read behaviour options
22
- * @returns {Promise<number>}
23
- */
24
- peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
25
- close(): Promise<void>;
26
- }
1
+ import { IFileInfo, IReadChunkOptions } from './types.js';
2
+ import { AbstractTokenizer } from './AbstractTokenizer.js';
3
+ export declare class BufferTokenizer extends AbstractTokenizer {
4
+ private uint8Array;
5
+ /**
6
+ * Construct BufferTokenizer
7
+ * @param uint8Array - Uint8Array to tokenize
8
+ * @param fileInfo - Pass additional file information to the tokenizer
9
+ */
10
+ constructor(uint8Array: Uint8Array, fileInfo?: IFileInfo);
11
+ /**
12
+ * Read buffer from tokenizer
13
+ * @param uint8Array - Uint8Array to tokenize
14
+ * @param options - Read behaviour options
15
+ * @returns {Promise<number>}
16
+ */
17
+ readBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
18
+ /**
19
+ * Peek (read ahead) buffer from tokenizer
20
+ * @param uint8Array
21
+ * @param options - Read behaviour options
22
+ * @returns {Promise<number>}
23
+ */
24
+ peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
25
+ close(): Promise<void>;
26
+ }
@@ -1,51 +1,51 @@
1
- import { EndOfStreamError } from 'peek-readable';
2
- import { AbstractTokenizer } from './AbstractTokenizer.js';
3
- export class BufferTokenizer extends AbstractTokenizer {
4
- /**
5
- * Construct BufferTokenizer
6
- * @param uint8Array - Uint8Array to tokenize
7
- * @param fileInfo - Pass additional file information to the tokenizer
8
- */
9
- constructor(uint8Array, fileInfo) {
10
- super(fileInfo);
11
- this.uint8Array = uint8Array;
12
- this.fileInfo.size = this.fileInfo.size ? this.fileInfo.size : uint8Array.length;
13
- }
14
- /**
15
- * Read buffer from tokenizer
16
- * @param uint8Array - Uint8Array to tokenize
17
- * @param options - Read behaviour options
18
- * @returns {Promise<number>}
19
- */
20
- async readBuffer(uint8Array, options) {
21
- if (options && options.position) {
22
- if (options.position < this.position) {
23
- throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
24
- }
25
- this.position = options.position;
26
- }
27
- const bytesRead = await this.peekBuffer(uint8Array, options);
28
- this.position += bytesRead;
29
- return bytesRead;
30
- }
31
- /**
32
- * Peek (read ahead) buffer from tokenizer
33
- * @param uint8Array
34
- * @param options - Read behaviour options
35
- * @returns {Promise<number>}
36
- */
37
- async peekBuffer(uint8Array, options) {
38
- const normOptions = this.normalizeOptions(uint8Array, options);
39
- const bytes2read = Math.min(this.uint8Array.length - normOptions.position, normOptions.length);
40
- if ((!normOptions.mayBeLess) && bytes2read < normOptions.length) {
41
- throw new EndOfStreamError();
42
- }
43
- else {
44
- uint8Array.set(this.uint8Array.subarray(normOptions.position, normOptions.position + bytes2read), normOptions.offset);
45
- return bytes2read;
46
- }
47
- }
48
- async close() {
49
- // empty
50
- }
51
- }
1
+ import { EndOfStreamError } from 'peek-readable';
2
+ import { AbstractTokenizer } from './AbstractTokenizer.js';
3
+ export class BufferTokenizer extends AbstractTokenizer {
4
+ /**
5
+ * Construct BufferTokenizer
6
+ * @param uint8Array - Uint8Array to tokenize
7
+ * @param fileInfo - Pass additional file information to the tokenizer
8
+ */
9
+ constructor(uint8Array, fileInfo) {
10
+ super(fileInfo);
11
+ this.uint8Array = uint8Array;
12
+ this.fileInfo.size = this.fileInfo.size ? this.fileInfo.size : uint8Array.length;
13
+ }
14
+ /**
15
+ * Read buffer from tokenizer
16
+ * @param uint8Array - Uint8Array to tokenize
17
+ * @param options - Read behaviour options
18
+ * @returns {Promise<number>}
19
+ */
20
+ async readBuffer(uint8Array, options) {
21
+ if (options && options.position) {
22
+ if (options.position < this.position) {
23
+ throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
24
+ }
25
+ this.position = options.position;
26
+ }
27
+ const bytesRead = await this.peekBuffer(uint8Array, options);
28
+ this.position += bytesRead;
29
+ return bytesRead;
30
+ }
31
+ /**
32
+ * Peek (read ahead) buffer from tokenizer
33
+ * @param uint8Array
34
+ * @param options - Read behaviour options
35
+ * @returns {Promise<number>}
36
+ */
37
+ async peekBuffer(uint8Array, options) {
38
+ const normOptions = this.normalizeOptions(uint8Array, options);
39
+ const bytes2read = Math.min(this.uint8Array.length - normOptions.position, normOptions.length);
40
+ if ((!normOptions.mayBeLess) && bytes2read < normOptions.length) {
41
+ throw new EndOfStreamError();
42
+ }
43
+ else {
44
+ uint8Array.set(this.uint8Array.subarray(normOptions.position, normOptions.position + bytes2read), normOptions.offset);
45
+ return bytes2read;
46
+ }
47
+ }
48
+ async close() {
49
+ // empty
50
+ }
51
+ }
@@ -1,22 +1,22 @@
1
- import { AbstractTokenizer } from './AbstractTokenizer.js';
2
- import { IFileInfo, IReadChunkOptions } from './types.js';
3
- export declare class FileTokenizer extends AbstractTokenizer {
4
- private fd;
5
- constructor(fd: number, fileInfo: IFileInfo);
6
- /**
7
- * Read buffer from file
8
- * @param uint8Array - Uint8Array to write result to
9
- * @param options - Read behaviour options
10
- * @returns Promise number of bytes read
11
- */
12
- readBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
13
- /**
14
- * Peek buffer from file
15
- * @param uint8Array - Uint8Array (or Buffer) to write data to
16
- * @param options - Read behaviour options
17
- * @returns Promise number of bytes read
18
- */
19
- peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
20
- close(): Promise<void>;
21
- }
22
- export declare function fromFile(sourceFilePath: string): Promise<FileTokenizer>;
1
+ import { AbstractTokenizer } from './AbstractTokenizer.js';
2
+ import { IFileInfo, IReadChunkOptions } from './types.js';
3
+ export declare class FileTokenizer extends AbstractTokenizer {
4
+ private fd;
5
+ constructor(fd: number, fileInfo: IFileInfo);
6
+ /**
7
+ * Read buffer from file
8
+ * @param uint8Array - Uint8Array to write result to
9
+ * @param options - Read behaviour options
10
+ * @returns Promise number of bytes read
11
+ */
12
+ readBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
13
+ /**
14
+ * Peek buffer from file
15
+ * @param uint8Array - Uint8Array (or Buffer) to write data to
16
+ * @param options - Read behaviour options
17
+ * @returns Promise number of bytes read
18
+ */
19
+ peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
20
+ close(): Promise<void>;
21
+ }
22
+ export declare function fromFile(sourceFilePath: string): Promise<FileTokenizer>;
@@ -1,50 +1,50 @@
1
- import { AbstractTokenizer } from './AbstractTokenizer.js';
2
- import { EndOfStreamError } from 'peek-readable';
3
- import * as fs from './FsPromise.js';
4
- export class FileTokenizer extends AbstractTokenizer {
5
- constructor(fd, fileInfo) {
6
- super(fileInfo);
7
- this.fd = fd;
8
- }
9
- /**
10
- * Read buffer from file
11
- * @param uint8Array - Uint8Array to write result to
12
- * @param options - Read behaviour options
13
- * @returns Promise number of bytes read
14
- */
15
- async readBuffer(uint8Array, options) {
16
- const normOptions = this.normalizeOptions(uint8Array, options);
17
- this.position = normOptions.position;
18
- const res = await fs.read(this.fd, uint8Array, normOptions.offset, normOptions.length, normOptions.position);
19
- this.position += res.bytesRead;
20
- if (res.bytesRead < normOptions.length && (!options || !options.mayBeLess)) {
21
- throw new EndOfStreamError();
22
- }
23
- return res.bytesRead;
24
- }
25
- /**
26
- * Peek buffer from file
27
- * @param uint8Array - Uint8Array (or Buffer) to write data to
28
- * @param options - Read behaviour options
29
- * @returns Promise number of bytes read
30
- */
31
- async peekBuffer(uint8Array, options) {
32
- const normOptions = this.normalizeOptions(uint8Array, options);
33
- const res = await fs.read(this.fd, uint8Array, normOptions.offset, normOptions.length, normOptions.position);
34
- if ((!normOptions.mayBeLess) && res.bytesRead < normOptions.length) {
35
- throw new EndOfStreamError();
36
- }
37
- return res.bytesRead;
38
- }
39
- async close() {
40
- return fs.close(this.fd);
41
- }
42
- }
43
- export async function fromFile(sourceFilePath) {
44
- const stat = await fs.stat(sourceFilePath);
45
- if (!stat.isFile) {
46
- throw new Error(`File not a file: ${sourceFilePath}`);
47
- }
48
- const fd = await fs.open(sourceFilePath, 'r');
49
- return new FileTokenizer(fd, { path: sourceFilePath, size: stat.size });
50
- }
1
+ import { AbstractTokenizer } from './AbstractTokenizer.js';
2
+ import { EndOfStreamError } from 'peek-readable';
3
+ import * as fs from './FsPromise.js';
4
+ export class FileTokenizer extends AbstractTokenizer {
5
+ constructor(fd, fileInfo) {
6
+ super(fileInfo);
7
+ this.fd = fd;
8
+ }
9
+ /**
10
+ * Read buffer from file
11
+ * @param uint8Array - Uint8Array to write result to
12
+ * @param options - Read behaviour options
13
+ * @returns Promise number of bytes read
14
+ */
15
+ async readBuffer(uint8Array, options) {
16
+ const normOptions = this.normalizeOptions(uint8Array, options);
17
+ this.position = normOptions.position;
18
+ const res = await fs.read(this.fd, uint8Array, normOptions.offset, normOptions.length, normOptions.position);
19
+ this.position += res.bytesRead;
20
+ if (res.bytesRead < normOptions.length && (!options || !options.mayBeLess)) {
21
+ throw new EndOfStreamError();
22
+ }
23
+ return res.bytesRead;
24
+ }
25
+ /**
26
+ * Peek buffer from file
27
+ * @param uint8Array - Uint8Array (or Buffer) to write data to
28
+ * @param options - Read behaviour options
29
+ * @returns Promise number of bytes read
30
+ */
31
+ async peekBuffer(uint8Array, options) {
32
+ const normOptions = this.normalizeOptions(uint8Array, options);
33
+ const res = await fs.read(this.fd, uint8Array, normOptions.offset, normOptions.length, normOptions.position);
34
+ if ((!normOptions.mayBeLess) && res.bytesRead < normOptions.length) {
35
+ throw new EndOfStreamError();
36
+ }
37
+ return res.bytesRead;
38
+ }
39
+ async close() {
40
+ return fs.close(this.fd);
41
+ }
42
+ }
43
+ export async function fromFile(sourceFilePath) {
44
+ const stat = await fs.stat(sourceFilePath);
45
+ if (!stat.isFile) {
46
+ throw new Error(`File not a file: ${sourceFilePath}`);
47
+ }
48
+ const fd = await fs.open(sourceFilePath, 'r');
49
+ return new FileTokenizer(fd, { path: sourceFilePath, size: stat.size });
50
+ }