strtok3 6.2.0 → 6.2.4

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,61 +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 buffer - 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(buffer: 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 skip (ignore)
57
- * @return actual number of bytes ignored
58
- */
59
- abstract ignore(length: number): Promise<number>;
60
- close(): Promise<void>;
61
- }
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,69 +1,104 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AbstractTokenizer = void 0;
4
- const peek_readable_1 = require("peek-readable");
5
- /**
6
- * Core tokenizer
7
- */
8
- class AbstractTokenizer {
9
- constructor(fileInfo) {
10
- /**
11
- * Tokenizer-stream position
12
- */
13
- this.position = 0;
14
- this.numBuffer = new Uint8Array(8);
15
- this.fileInfo = fileInfo ? fileInfo : {};
16
- }
17
- /**
18
- * Read a token from the tokenizer-stream
19
- * @param token - The token to read
20
- * @param position - If provided, the desired position in the tokenizer-stream
21
- * @returns Promise with token data
22
- */
23
- async readToken(token, position) {
24
- const uint8Array = Buffer.alloc(token.len);
25
- const len = await this.readBuffer(uint8Array, { position });
26
- if (len < token.len)
27
- throw new peek_readable_1.EndOfStreamError();
28
- return token.get(uint8Array, 0);
29
- }
30
- /**
31
- * Peek a token from the tokenizer-stream.
32
- * @param token - Token to peek from the tokenizer-stream.
33
- * @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
34
- * @returns Promise with token data
35
- */
36
- async peekToken(token, position = this.position) {
37
- const uint8Array = Buffer.alloc(token.len);
38
- const len = await this.peekBuffer(uint8Array, { position });
39
- if (len < token.len)
40
- throw new peek_readable_1.EndOfStreamError();
41
- return token.get(uint8Array, 0);
42
- }
43
- /**
44
- * Read a numeric token from the stream
45
- * @param token - Numeric token
46
- * @returns Promise with number
47
- */
48
- async readNumber(token) {
49
- const len = await this.readBuffer(this.numBuffer, { length: token.len });
50
- if (len < token.len)
51
- throw new peek_readable_1.EndOfStreamError();
52
- return token.get(this.numBuffer, 0);
53
- }
54
- /**
55
- * Read a numeric token from the stream
56
- * @param token - Numeric token
57
- * @returns Promise with number
58
- */
59
- async peekNumber(token) {
60
- const len = await this.peekBuffer(this.numBuffer, { length: token.len });
61
- if (len < token.len)
62
- throw new peek_readable_1.EndOfStreamError();
63
- return token.get(this.numBuffer, 0);
64
- }
65
- async close() {
66
- // empty
67
- }
68
- }
69
- exports.AbstractTokenizer = AbstractTokenizer;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractTokenizer = void 0;
4
+ const peek_readable_1 = require("peek-readable");
5
+ /**
6
+ * Core tokenizer
7
+ */
8
+ class AbstractTokenizer {
9
+ constructor(fileInfo) {
10
+ /**
11
+ * Tokenizer-stream position
12
+ */
13
+ this.position = 0;
14
+ this.numBuffer = new Uint8Array(8);
15
+ this.fileInfo = fileInfo ? fileInfo : {};
16
+ }
17
+ /**
18
+ * Read a token from the tokenizer-stream
19
+ * @param token - The token to read
20
+ * @param position - If provided, the desired position in the tokenizer-stream
21
+ * @returns Promise with token data
22
+ */
23
+ async readToken(token, position = this.position) {
24
+ const uint8Array = Buffer.alloc(token.len);
25
+ const len = await this.readBuffer(uint8Array, { position });
26
+ if (len < token.len)
27
+ throw new peek_readable_1.EndOfStreamError();
28
+ return token.get(uint8Array, 0);
29
+ }
30
+ /**
31
+ * Peek a token from the tokenizer-stream.
32
+ * @param token - Token to peek from the tokenizer-stream.
33
+ * @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
34
+ * @returns Promise with token data
35
+ */
36
+ async peekToken(token, position = this.position) {
37
+ const uint8Array = Buffer.alloc(token.len);
38
+ const len = await this.peekBuffer(uint8Array, { position });
39
+ if (len < token.len)
40
+ throw new peek_readable_1.EndOfStreamError();
41
+ return token.get(uint8Array, 0);
42
+ }
43
+ /**
44
+ * Read a numeric token from the stream
45
+ * @param token - Numeric token
46
+ * @returns Promise with number
47
+ */
48
+ async readNumber(token) {
49
+ const len = await this.readBuffer(this.numBuffer, { length: token.len });
50
+ if (len < token.len)
51
+ throw new peek_readable_1.EndOfStreamError();
52
+ return token.get(this.numBuffer, 0);
53
+ }
54
+ /**
55
+ * Read a numeric token from the stream
56
+ * @param token - Numeric token
57
+ * @returns Promise with number
58
+ */
59
+ async peekNumber(token) {
60
+ const len = await this.peekBuffer(this.numBuffer, { length: token.len });
61
+ if (len < token.len)
62
+ throw new peek_readable_1.EndOfStreamError();
63
+ return token.get(this.numBuffer, 0);
64
+ }
65
+ /**
66
+ * Ignore number of bytes, advances the pointer in under tokenizer-stream.
67
+ * @param length - Number of bytes to ignore
68
+ * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
69
+ */
70
+ async ignore(length) {
71
+ if (this.fileInfo.size !== undefined) {
72
+ const bytesLeft = this.fileInfo.size - this.position;
73
+ if (length > bytesLeft) {
74
+ this.position += bytesLeft;
75
+ return bytesLeft;
76
+ }
77
+ }
78
+ this.position += length;
79
+ return length;
80
+ }
81
+ async close() {
82
+ // empty
83
+ }
84
+ normalizeOptions(uint8Array, options) {
85
+ if (options && options.position !== undefined && options.position < this.position) {
86
+ throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
87
+ }
88
+ if (options) {
89
+ return {
90
+ mayBeLess: options.mayBeLess === true,
91
+ offset: options.offset ? options.offset : 0,
92
+ length: options.length ? options.length : (uint8Array.length - (options.offset ? options.offset : 0)),
93
+ position: options.position ? options.position : this.position
94
+ };
95
+ }
96
+ return {
97
+ mayBeLess: false,
98
+ offset: 0,
99
+ length: uint8Array.length,
100
+ position: this.position
101
+ };
102
+ }
103
+ }
104
+ exports.AbstractTokenizer = AbstractTokenizer;
@@ -1,36 +1,26 @@
1
- import { IFileInfo, IReadChunkOptions, ITokenizer } from './types';
2
- import { IGetToken, IToken } from '@tokenizer/token';
3
- export declare class BufferTokenizer implements ITokenizer {
4
- private uint8Array;
5
- fileInfo: IFileInfo;
6
- position: number;
7
- /**
8
- * Construct BufferTokenizer
9
- * @param buffer - Buffer to tokenize
10
- * @param fileInfo - Pass additional file information to the tokenizer
11
- */
12
- constructor(uint8Array: Uint8Array, fileInfo?: IFileInfo);
13
- /**
14
- * Read buffer from tokenizer
15
- * @param buffer
16
- * @param options - Read behaviour options
17
- * @returns {Promise<number>}
18
- */
19
- readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
20
- /**
21
- * Peek (read ahead) buffer from tokenizer
22
- * @param uint8Array
23
- * @param options - Read behaviour options
24
- * @returns {Promise<number>}
25
- */
26
- peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
27
- readToken<T>(token: IGetToken<T>, position?: number): Promise<T>;
28
- peekToken<T>(token: IGetToken<T>, position?: number): Promise<T>;
29
- readNumber(token: IToken<number>): Promise<number>;
30
- peekNumber(token: IToken<number>): Promise<number>;
31
- /**
32
- * @return actual number of bytes ignored
33
- */
34
- ignore(length: number): Promise<number>;
35
- close(): Promise<void>;
36
- }
1
+ import { IFileInfo, IReadChunkOptions } from './types';
2
+ import { AbstractTokenizer } from './AbstractTokenizer';
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,116 +1,55 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BufferTokenizer = void 0;
4
- const peek_readable_1 = require("peek-readable");
5
- class BufferTokenizer {
6
- /**
7
- * Construct BufferTokenizer
8
- * @param buffer - Buffer to tokenize
9
- * @param fileInfo - Pass additional file information to the tokenizer
10
- */
11
- constructor(uint8Array, fileInfo) {
12
- this.uint8Array = uint8Array;
13
- this.position = 0;
14
- this.fileInfo = fileInfo ? fileInfo : {};
15
- this.fileInfo.size = this.fileInfo.size ? this.fileInfo.size : uint8Array.length;
16
- }
17
- /**
18
- * Read buffer from tokenizer
19
- * @param buffer
20
- * @param options - Read behaviour options
21
- * @returns {Promise<number>}
22
- */
23
- async readBuffer(buffer, options) {
24
- if (options && options.position) {
25
- if (options.position < this.position) {
26
- throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
27
- }
28
- this.position = options.position;
29
- }
30
- return this.peekBuffer(buffer, options).then(bytesRead => {
31
- this.position += bytesRead;
32
- return bytesRead;
33
- });
34
- }
35
- /**
36
- * Peek (read ahead) buffer from tokenizer
37
- * @param uint8Array
38
- * @param options - Read behaviour options
39
- * @returns {Promise<number>}
40
- */
41
- async peekBuffer(uint8Array, options) {
42
- let offset = 0;
43
- let length = uint8Array.length;
44
- let position = this.position;
45
- if (options) {
46
- if (options.position) {
47
- if (options.position < this.position) {
48
- throw new Error('`options.position` can be less than `tokenizer.position`');
49
- }
50
- position = options.position;
51
- }
52
- if (Number.isInteger(options.length)) {
53
- length = options.length;
54
- }
55
- else {
56
- length -= options.offset || 0;
57
- }
58
- if (options.offset) {
59
- offset = options.offset;
60
- }
61
- }
62
- if (length === 0) {
63
- return Promise.resolve(0);
64
- }
65
- position = position || this.position;
66
- if (!length) {
67
- length = uint8Array.length;
68
- }
69
- const bytes2read = Math.min(this.uint8Array.length - position, length);
70
- if ((!options || !options.mayBeLess) && bytes2read < length) {
71
- throw new peek_readable_1.EndOfStreamError();
72
- }
73
- else {
74
- // old: this.buffer.copy(buffer, offset, position, position + bytes2read);
75
- // uint8Array.set(this.uint8Array.subarray(position, position + bytes2read), offset);
76
- Buffer.from(this.uint8Array).copy(uint8Array, offset, position, position + bytes2read);
77
- return bytes2read;
78
- }
79
- }
80
- async readToken(token, position) {
81
- this.position = position || this.position;
82
- try {
83
- const tv = this.peekToken(token, this.position);
84
- this.position += token.len;
85
- return tv;
86
- }
87
- catch (err) {
88
- this.position += this.uint8Array.length - position;
89
- throw err;
90
- }
91
- }
92
- async peekToken(token, position = this.position) {
93
- if (this.uint8Array.length - position < token.len) {
94
- throw new peek_readable_1.EndOfStreamError();
95
- }
96
- return token.get(this.uint8Array, position);
97
- }
98
- async readNumber(token) {
99
- return this.readToken(token);
100
- }
101
- async peekNumber(token) {
102
- return this.peekToken(token);
103
- }
104
- /**
105
- * @return actual number of bytes ignored
106
- */
107
- async ignore(length) {
108
- const bytesIgnored = Math.min(this.uint8Array.length - this.position, length);
109
- this.position += bytesIgnored;
110
- return bytesIgnored;
111
- }
112
- async close() {
113
- // empty
114
- }
115
- }
116
- exports.BufferTokenizer = BufferTokenizer;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BufferTokenizer = void 0;
4
+ const peek_readable_1 = require("peek-readable");
5
+ const AbstractTokenizer_1 = require("./AbstractTokenizer");
6
+ class BufferTokenizer extends AbstractTokenizer_1.AbstractTokenizer {
7
+ /**
8
+ * Construct BufferTokenizer
9
+ * @param uint8Array - Uint8Array to tokenize
10
+ * @param fileInfo - Pass additional file information to the tokenizer
11
+ */
12
+ constructor(uint8Array, fileInfo) {
13
+ super(fileInfo);
14
+ this.uint8Array = uint8Array;
15
+ this.fileInfo.size = this.fileInfo.size ? this.fileInfo.size : uint8Array.length;
16
+ }
17
+ /**
18
+ * Read buffer from tokenizer
19
+ * @param uint8Array - Uint8Array to tokenize
20
+ * @param options - Read behaviour options
21
+ * @returns {Promise<number>}
22
+ */
23
+ async readBuffer(uint8Array, options) {
24
+ if (options && options.position) {
25
+ if (options.position < this.position) {
26
+ throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
27
+ }
28
+ this.position = options.position;
29
+ }
30
+ const bytesRead = await this.peekBuffer(uint8Array, options);
31
+ this.position += bytesRead;
32
+ return bytesRead;
33
+ }
34
+ /**
35
+ * Peek (read ahead) buffer from tokenizer
36
+ * @param uint8Array
37
+ * @param options - Read behaviour options
38
+ * @returns {Promise<number>}
39
+ */
40
+ async peekBuffer(uint8Array, options) {
41
+ const normOptions = this.normalizeOptions(uint8Array, options);
42
+ const bytes2read = Math.min(this.uint8Array.length - normOptions.position, normOptions.length);
43
+ if ((!normOptions.mayBeLess) && bytes2read < normOptions.length) {
44
+ throw new peek_readable_1.EndOfStreamError();
45
+ }
46
+ else {
47
+ uint8Array.set(this.uint8Array.subarray(normOptions.position, normOptions.position + bytes2read), normOptions.offset);
48
+ return bytes2read;
49
+ }
50
+ }
51
+ async close() {
52
+ // empty
53
+ }
54
+ }
55
+ exports.BufferTokenizer = BufferTokenizer;
@@ -1,28 +1,22 @@
1
- /// <reference types="node" />
2
- import { AbstractTokenizer } from './AbstractTokenizer';
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 buffer
17
- * @param options - Read behaviour options
18
- * @returns Promise number of bytes read
19
- */
20
- peekBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
21
- /**
22
- * @param length - Number of bytes to ignore
23
- * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
24
- */
25
- ignore(length: number): Promise<number>;
26
- close(): Promise<void>;
27
- }
28
- export declare function fromFile(sourceFilePath: string): Promise<FileTokenizer>;
1
+ import { AbstractTokenizer } from './AbstractTokenizer';
2
+ import { IFileInfo, IReadChunkOptions } from './types';
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>;