strtok3 7.0.0-alpha.3 → 7.0.0-alpha.7

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,62 +1,69 @@
1
- import { ITokenizer, IFileInfo, IReadChunkOptions } from './types';
2
- import { IGetToken, IToken } from '@tokenizer/token';
3
- /**
4
- * Core tokenizer
5
- */
6
- export declare abstract class AbstractTokenizer implements ITokenizer {
7
- fileInfo: IFileInfo;
8
- protected constructor(fileInfo?: IFileInfo);
9
- /**
10
- * Tokenizer-stream position
11
- */
12
- position: number;
13
- private numBuffer;
14
- /**
15
- * Read buffer from tokenizer
16
- * @param buffer - Target buffer to fill with data read from the tokenizer-stream
17
- * @param options - Additional read options
18
- * @returns Promise with number of bytes read
19
- */
20
- abstract readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
21
- /**
22
- * Peek (read ahead) buffer from tokenizer
23
- * @param uint8Array- Target buffer to fill with data peek from the tokenizer-stream
24
- * @param options - Peek behaviour options
25
- * @returns Promise with number of bytes read
26
- */
27
- abstract peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
28
- /**
29
- * Read a token from the tokenizer-stream
30
- * @param token - The token to read
31
- * @param position - If provided, the desired position in the tokenizer-stream
32
- * @returns Promise with token data
33
- */
34
- readToken<Value>(token: IGetToken<Value>, position?: number): Promise<Value>;
35
- /**
36
- * Peek a token from the tokenizer-stream.
37
- * @param token - Token to peek from the tokenizer-stream.
38
- * @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
39
- * @returns Promise with token data
40
- */
41
- peekToken<Value>(token: IGetToken<Value>, position?: number): Promise<Value>;
42
- /**
43
- * Read a numeric token from the stream
44
- * @param token - Numeric token
45
- * @returns Promise with number
46
- */
47
- readNumber(token: IToken<number>): Promise<number>;
48
- /**
49
- * Read a numeric token from the stream
50
- * @param token - Numeric token
51
- * @returns Promise with number
52
- */
53
- peekNumber(token: IToken<number>): Promise<number>;
54
- /**
55
- * Ignore number of bytes, advances the pointer in under tokenizer-stream.
56
- * @param length - Number of bytes to ignore
57
- * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
58
- */
59
- ignore(length: number): Promise<number>;
60
- close(): Promise<void>;
61
- protected normalizeOptions(uint8Array: Uint8Array, options?: IReadChunkOptions): IReadChunkOptions;
62
- }
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,90 +1,101 @@
1
- import { EndOfStreamError } from 'peek-readable';
2
- /**
3
- * Core tokenizer
4
- */
5
- export class AbstractTokenizer {
6
- constructor(fileInfo) {
7
- /**
8
- * Tokenizer-stream position
9
- */
10
- this.position = 0;
11
- this.numBuffer = new Uint8Array(8);
12
- this.fileInfo = fileInfo ? fileInfo : {};
13
- }
14
- /**
15
- * Read a token from the tokenizer-stream
16
- * @param token - The token to read
17
- * @param position - If provided, the desired position in the tokenizer-stream
18
- * @returns Promise with token data
19
- */
20
- async readToken(token, position = this.position) {
21
- const uint8Array = Buffer.alloc(token.len);
22
- const len = await this.readBuffer(uint8Array, { position });
23
- if (len < token.len)
24
- throw new EndOfStreamError();
25
- return token.get(uint8Array, 0);
26
- }
27
- /**
28
- * Peek a token from the tokenizer-stream.
29
- * @param token - Token to peek from the tokenizer-stream.
30
- * @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
31
- * @returns Promise with token data
32
- */
33
- async peekToken(token, position = this.position) {
34
- const uint8Array = Buffer.alloc(token.len);
35
- const len = await this.peekBuffer(uint8Array, { position });
36
- if (len < token.len)
37
- throw new EndOfStreamError();
38
- return token.get(uint8Array, 0);
39
- }
40
- /**
41
- * Read a numeric token from the stream
42
- * @param token - Numeric token
43
- * @returns Promise with number
44
- */
45
- async readNumber(token) {
46
- const len = await this.readBuffer(this.numBuffer, { length: token.len });
47
- if (len < token.len)
48
- throw new EndOfStreamError();
49
- return token.get(this.numBuffer, 0);
50
- }
51
- /**
52
- * Read a numeric token from the stream
53
- * @param token - Numeric token
54
- * @returns Promise with number
55
- */
56
- async peekNumber(token) {
57
- const len = await this.peekBuffer(this.numBuffer, { length: token.len });
58
- if (len < token.len)
59
- throw new EndOfStreamError();
60
- return token.get(this.numBuffer, 0);
61
- }
62
- /**
63
- * Ignore number of bytes, advances the pointer in under tokenizer-stream.
64
- * @param length - Number of bytes to ignore
65
- * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
66
- */
67
- async ignore(length) {
68
- const bytesLeft = this.fileInfo.size - this.position;
69
- if (length <= bytesLeft) {
70
- this.position += length;
71
- return length;
72
- }
73
- else {
74
- this.position += bytesLeft;
75
- return bytesLeft;
76
- }
77
- }
78
- async close() {
79
- // empty
80
- }
81
- normalizeOptions(uint8Array, options) {
82
- options = {
83
- offset: 0,
84
- length: uint8Array.length - ((options && options.offset) ? options.offset : 0),
85
- position: this.position,
86
- ...options
87
- };
88
- return options;
89
- }
90
- }
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';
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
- options = this.normalizeOptions(uint8Array, options);
39
- const bytes2read = Math.min(this.uint8Array.length - options.position, options.length);
40
- if ((!options.mayBeLess) && bytes2read < options.length) {
41
- throw new EndOfStreamError();
42
- }
43
- else {
44
- uint8Array.set(this.uint8Array.subarray(options.position, options.position + bytes2read), options.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,23 +1,22 @@
1
- /// <reference types="node" />
2
- import { AbstractTokenizer } from './AbstractTokenizer.js';
3
- import { IFileInfo, IReadChunkOptions } from './types';
4
- export declare class FileTokenizer extends AbstractTokenizer {
5
- private fd;
6
- constructor(fd: number, fileInfo: IFileInfo);
7
- /**
8
- * Read buffer from file
9
- * @param buffer
10
- * @param options - Read behaviour options
11
- * @returns Promise number of bytes read
12
- */
13
- readBuffer(buffer: Buffer, options?: IReadChunkOptions): Promise<number>;
14
- /**
15
- * Peek buffer from file
16
- * @param uint8Array - Uint8Array (or Buffer) to write data to
17
- * @param options - Read behaviour options
18
- * @returns Promise number of bytes read
19
- */
20
- peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
21
- close(): Promise<void>;
22
- }
23
- 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>;