strtok3 8.0.1 → 8.0.2
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.
- package/LICENSE +14 -14
- package/README.md +317 -317
- package/package.json +89 -100
- package/lib/AbstractTokenizer.d.ts +0 -75
- package/lib/AbstractTokenizer.js +0 -108
- package/lib/BufferTokenizer.d.ts +0 -26
- package/lib/BufferTokenizer.js +0 -51
- package/lib/FileTokenizer.d.ts +0 -23
- package/lib/FileTokenizer.js +0 -48
- package/lib/ReadStreamTokenizer.d.ts +0 -27
- package/lib/ReadStreamTokenizer.js +0 -92
- package/lib/core.d.ts +0 -32
- package/lib/core.js +0 -34
- package/lib/index.d.ts +0 -15
- package/lib/index.js +0 -22
- package/lib/types.d.ts +0 -113
- package/lib/types.js +0 -1
package/package.json
CHANGED
|
@@ -1,100 +1,89 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "strtok3",
|
|
3
|
-
"version": "8.0.
|
|
4
|
-
"description": "A promise based streaming tokenizer",
|
|
5
|
-
"author": {
|
|
6
|
-
"name": "Borewit",
|
|
7
|
-
"url": "https://github.com/Borewit"
|
|
8
|
-
},
|
|
9
|
-
"funding": {
|
|
10
|
-
"type": "github",
|
|
11
|
-
"url": "https://github.com/sponsors/Borewit"
|
|
12
|
-
},
|
|
13
|
-
"scripts": {
|
|
14
|
-
"clean": "del-cli 'lib/**/*.js' 'lib/**/*.js.map' 'lib/**/*.d.ts' 'test/**/*.js' 'test/**/*.js.map'",
|
|
15
|
-
"compile-src": "tsc -p lib",
|
|
16
|
-
"compile-test": "tsc -p test",
|
|
17
|
-
"compile": "
|
|
18
|
-
"build": "
|
|
19
|
-
"eslint": "eslint lib test",
|
|
20
|
-
"lint-md": "remark -u preset-lint-recommended .",
|
|
21
|
-
"lint": "
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"test
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
"lib/**/*.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
"@
|
|
55
|
-
"@
|
|
56
|
-
"@types/
|
|
57
|
-
"@types/
|
|
58
|
-
"@types/
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
"
|
|
83
|
-
"
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
"
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
"promise",
|
|
91
|
-
"parser",
|
|
92
|
-
"decoder",
|
|
93
|
-
"binary",
|
|
94
|
-
"endian",
|
|
95
|
-
"uint",
|
|
96
|
-
"stream",
|
|
97
|
-
"streaming"
|
|
98
|
-
],
|
|
99
|
-
"packageManager": "yarn@4.3.1"
|
|
100
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "strtok3",
|
|
3
|
+
"version": "8.0.2",
|
|
4
|
+
"description": "A promise based streaming tokenizer",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Borewit",
|
|
7
|
+
"url": "https://github.com/Borewit"
|
|
8
|
+
},
|
|
9
|
+
"funding": {
|
|
10
|
+
"type": "github",
|
|
11
|
+
"url": "https://github.com/sponsors/Borewit"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"clean": "del-cli 'lib/**/*.js' 'lib/**/*.js.map' 'lib/**/*.d.ts' 'test/**/*.js' 'test/**/*.js.map'",
|
|
15
|
+
"compile-src": "tsc -p lib",
|
|
16
|
+
"compile-test": "tsc -p test",
|
|
17
|
+
"compile": "yarn run compile-src && yarn run compile-test",
|
|
18
|
+
"build": "yarn run clean && yarn run compile",
|
|
19
|
+
"eslint": "eslint lib test",
|
|
20
|
+
"lint-md": "remark -u preset-lint-recommended .",
|
|
21
|
+
"lint-ts": "biome check",
|
|
22
|
+
"lint": "yarn run lint-md && yarn run lint-ts",
|
|
23
|
+
"fix": "yarn run biome lint --write",
|
|
24
|
+
"test": "mocha",
|
|
25
|
+
"test-coverage": "c8 yarn run test",
|
|
26
|
+
"send-codacy": "c8 report --reporter=text-lcov | codacy-coverage",
|
|
27
|
+
"start": "yarn run compile && yarn run lint && yarn run cover-test"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=16"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/Borewit/strtok3.git"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"type": "module",
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"node": "./lib/index.js",
|
|
41
|
+
"default": "./lib/core.js"
|
|
42
|
+
},
|
|
43
|
+
"./core": "./lib/core.js"
|
|
44
|
+
},
|
|
45
|
+
"types": "lib/index.d.ts",
|
|
46
|
+
"files": [
|
|
47
|
+
"lib/**/*.js",
|
|
48
|
+
"lib/**/*.d.ts"
|
|
49
|
+
],
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/Borewit/strtok3/issues"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@biomejs/biome": "^1.8.3",
|
|
55
|
+
"@types/chai": "^4.3.17",
|
|
56
|
+
"@types/debug": "^4.1.12",
|
|
57
|
+
"@types/mocha": "^10.0.7",
|
|
58
|
+
"@types/node": "^22.1.0",
|
|
59
|
+
"c8": "^10.1.2",
|
|
60
|
+
"chai": "^5.1.1",
|
|
61
|
+
"del-cli": "^5.1.0",
|
|
62
|
+
"mocha": "^10.7.0",
|
|
63
|
+
"remark-cli": "^12.0.1",
|
|
64
|
+
"remark-preset-lint-recommended": "^7.0.0",
|
|
65
|
+
"token-types": "^6.0.0",
|
|
66
|
+
"ts-node": "^10.9.2",
|
|
67
|
+
"typescript": "^5.5.4",
|
|
68
|
+
"uint8array-extras": "^1.4.0"
|
|
69
|
+
},
|
|
70
|
+
"dependencies": {
|
|
71
|
+
"@tokenizer/token": "^0.3.0",
|
|
72
|
+
"peek-readable": "^5.1.3"
|
|
73
|
+
},
|
|
74
|
+
"keywords": [
|
|
75
|
+
"tokenizer",
|
|
76
|
+
"reader",
|
|
77
|
+
"token",
|
|
78
|
+
"async",
|
|
79
|
+
"promise",
|
|
80
|
+
"parser",
|
|
81
|
+
"decoder",
|
|
82
|
+
"binary",
|
|
83
|
+
"endian",
|
|
84
|
+
"uint",
|
|
85
|
+
"stream",
|
|
86
|
+
"streaming"
|
|
87
|
+
],
|
|
88
|
+
"packageManager": "yarn@4.3.1"
|
|
89
|
+
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import type { ITokenizer, IFileInfo, IReadChunkOptions, ITokenizerOptions } from './types.js';
|
|
2
|
-
import type { 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
|
-
private onClose?;
|
|
15
|
-
/**
|
|
16
|
-
* Constructor
|
|
17
|
-
* @param options Tokenizer options
|
|
18
|
-
* @protected
|
|
19
|
-
*/
|
|
20
|
-
protected constructor(options?: ITokenizerOptions);
|
|
21
|
-
/**
|
|
22
|
-
* Tokenizer-stream position
|
|
23
|
-
*/
|
|
24
|
-
position: number;
|
|
25
|
-
private numBuffer;
|
|
26
|
-
/**
|
|
27
|
-
* Read buffer from tokenizer
|
|
28
|
-
* @param buffer - Target buffer to fill with data read from the tokenizer-stream
|
|
29
|
-
* @param options - Additional read options
|
|
30
|
-
* @returns Promise with number of bytes read
|
|
31
|
-
*/
|
|
32
|
-
abstract readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
|
|
33
|
-
/**
|
|
34
|
-
* Peek (read ahead) buffer from tokenizer
|
|
35
|
-
* @param uint8Array - Target buffer to fill with data peeked from the tokenizer-stream
|
|
36
|
-
* @param options - Peek behaviour options
|
|
37
|
-
* @returns Promise with number of bytes read
|
|
38
|
-
*/
|
|
39
|
-
abstract peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
|
|
40
|
-
/**
|
|
41
|
-
* Read a token from the tokenizer-stream
|
|
42
|
-
* @param token - The token to read
|
|
43
|
-
* @param position - If provided, the desired position in the tokenizer-stream
|
|
44
|
-
* @returns Promise with token data
|
|
45
|
-
*/
|
|
46
|
-
readToken<Value>(token: IGetToken<Value>, position?: number): Promise<Value>;
|
|
47
|
-
/**
|
|
48
|
-
* Peek a token from the tokenizer-stream.
|
|
49
|
-
* @param token - Token to peek from the tokenizer-stream.
|
|
50
|
-
* @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
|
|
51
|
-
* @returns Promise with token data
|
|
52
|
-
*/
|
|
53
|
-
peekToken<Value>(token: IGetToken<Value>, position?: number): Promise<Value>;
|
|
54
|
-
/**
|
|
55
|
-
* Read a numeric token from the stream
|
|
56
|
-
* @param token - Numeric token
|
|
57
|
-
* @returns Promise with number
|
|
58
|
-
*/
|
|
59
|
-
readNumber(token: IToken<number>): Promise<number>;
|
|
60
|
-
/**
|
|
61
|
-
* Read a numeric token from the stream
|
|
62
|
-
* @param token - Numeric token
|
|
63
|
-
* @returns Promise with number
|
|
64
|
-
*/
|
|
65
|
-
peekNumber(token: IToken<number>): Promise<number>;
|
|
66
|
-
/**
|
|
67
|
-
* Ignore number of bytes, advances the pointer in under tokenizer-stream.
|
|
68
|
-
* @param length - Number of bytes to ignore
|
|
69
|
-
* @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
|
|
70
|
-
*/
|
|
71
|
-
ignore(length: number): Promise<number>;
|
|
72
|
-
close(): Promise<void>;
|
|
73
|
-
protected normalizeOptions(uint8Array: Uint8Array, options?: IReadChunkOptions): INormalizedReadChunkOptions;
|
|
74
|
-
}
|
|
75
|
-
export {};
|
package/lib/AbstractTokenizer.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { EndOfStreamError } from 'peek-readable';
|
|
2
|
-
/**
|
|
3
|
-
* Core tokenizer
|
|
4
|
-
*/
|
|
5
|
-
export class AbstractTokenizer {
|
|
6
|
-
/**
|
|
7
|
-
* Constructor
|
|
8
|
-
* @param options Tokenizer options
|
|
9
|
-
* @protected
|
|
10
|
-
*/
|
|
11
|
-
constructor(options) {
|
|
12
|
-
var _a;
|
|
13
|
-
/**
|
|
14
|
-
* Tokenizer-stream position
|
|
15
|
-
*/
|
|
16
|
-
this.position = 0;
|
|
17
|
-
this.numBuffer = new Uint8Array(8);
|
|
18
|
-
this.fileInfo = (_a = options === null || options === void 0 ? void 0 : options.fileInfo) !== null && _a !== void 0 ? _a : {};
|
|
19
|
-
this.onClose = options === null || options === void 0 ? void 0 : options.onClose;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Read a token from the tokenizer-stream
|
|
23
|
-
* @param token - The token to read
|
|
24
|
-
* @param position - If provided, the desired position in the tokenizer-stream
|
|
25
|
-
* @returns Promise with token data
|
|
26
|
-
*/
|
|
27
|
-
async readToken(token, position = this.position) {
|
|
28
|
-
const uint8Array = new Uint8Array(token.len);
|
|
29
|
-
const len = await this.readBuffer(uint8Array, { position });
|
|
30
|
-
if (len < token.len)
|
|
31
|
-
throw new EndOfStreamError();
|
|
32
|
-
return token.get(uint8Array, 0);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Peek a token from the tokenizer-stream.
|
|
36
|
-
* @param token - Token to peek from the tokenizer-stream.
|
|
37
|
-
* @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
|
|
38
|
-
* @returns Promise with token data
|
|
39
|
-
*/
|
|
40
|
-
async peekToken(token, position = this.position) {
|
|
41
|
-
const uint8Array = new Uint8Array(token.len);
|
|
42
|
-
const len = await this.peekBuffer(uint8Array, { position });
|
|
43
|
-
if (len < token.len)
|
|
44
|
-
throw new EndOfStreamError();
|
|
45
|
-
return token.get(uint8Array, 0);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Read a numeric token from the stream
|
|
49
|
-
* @param token - Numeric token
|
|
50
|
-
* @returns Promise with number
|
|
51
|
-
*/
|
|
52
|
-
async readNumber(token) {
|
|
53
|
-
const len = await this.readBuffer(this.numBuffer, { length: token.len });
|
|
54
|
-
if (len < token.len)
|
|
55
|
-
throw new EndOfStreamError();
|
|
56
|
-
return token.get(this.numBuffer, 0);
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Read a numeric token from the stream
|
|
60
|
-
* @param token - Numeric token
|
|
61
|
-
* @returns Promise with number
|
|
62
|
-
*/
|
|
63
|
-
async peekNumber(token) {
|
|
64
|
-
const len = await this.peekBuffer(this.numBuffer, { length: token.len });
|
|
65
|
-
if (len < token.len)
|
|
66
|
-
throw new EndOfStreamError();
|
|
67
|
-
return token.get(this.numBuffer, 0);
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Ignore number of bytes, advances the pointer in under tokenizer-stream.
|
|
71
|
-
* @param length - Number of bytes to ignore
|
|
72
|
-
* @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
|
|
73
|
-
*/
|
|
74
|
-
async ignore(length) {
|
|
75
|
-
if (this.fileInfo.size !== undefined) {
|
|
76
|
-
const bytesLeft = this.fileInfo.size - this.position;
|
|
77
|
-
if (length > bytesLeft) {
|
|
78
|
-
this.position += bytesLeft;
|
|
79
|
-
return bytesLeft;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
this.position += length;
|
|
83
|
-
return length;
|
|
84
|
-
}
|
|
85
|
-
async close() {
|
|
86
|
-
var _a;
|
|
87
|
-
await ((_a = this.onClose) === null || _a === void 0 ? void 0 : _a.call(this));
|
|
88
|
-
}
|
|
89
|
-
normalizeOptions(uint8Array, options) {
|
|
90
|
-
if (options && options.position !== undefined && options.position < this.position) {
|
|
91
|
-
throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
|
|
92
|
-
}
|
|
93
|
-
if (options) {
|
|
94
|
-
return {
|
|
95
|
-
mayBeLess: options.mayBeLess === true,
|
|
96
|
-
offset: options.offset ? options.offset : 0,
|
|
97
|
-
length: options.length ? options.length : (uint8Array.length - (options.offset ? options.offset : 0)),
|
|
98
|
-
position: options.position ? options.position : this.position
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
return {
|
|
102
|
-
mayBeLess: false,
|
|
103
|
-
offset: 0,
|
|
104
|
-
length: uint8Array.length,
|
|
105
|
-
position: this.position
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
}
|
package/lib/BufferTokenizer.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { ITokenizerOptions, 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 options Tokenizer options
|
|
9
|
-
*/
|
|
10
|
-
constructor(uint8Array: Uint8Array, options?: ITokenizerOptions);
|
|
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
|
-
}
|
package/lib/BufferTokenizer.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
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 options Tokenizer options
|
|
8
|
-
*/
|
|
9
|
-
constructor(uint8Array, options) {
|
|
10
|
-
super(options);
|
|
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
|
-
close() {
|
|
49
|
-
return super.close();
|
|
50
|
-
}
|
|
51
|
-
}
|
package/lib/FileTokenizer.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { AbstractTokenizer } from './AbstractTokenizer.js';
|
|
2
|
-
import type { IReadChunkOptions, ITokenizerOptions } from './types.js';
|
|
3
|
-
import { FileHandle } from 'node:fs/promises';
|
|
4
|
-
export declare class FileTokenizer extends AbstractTokenizer {
|
|
5
|
-
private fileHandle;
|
|
6
|
-
constructor(fileHandle: FileHandle, options: ITokenizerOptions);
|
|
7
|
-
/**
|
|
8
|
-
* Read buffer from file
|
|
9
|
-
* @param uint8Array - Uint8Array to write result to
|
|
10
|
-
* @param options - Read behaviour options
|
|
11
|
-
* @returns Promise number of bytes read
|
|
12
|
-
*/
|
|
13
|
-
readBuffer(uint8Array: Uint8Array, 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>;
|
package/lib/FileTokenizer.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { AbstractTokenizer } from './AbstractTokenizer.js';
|
|
2
|
-
import { EndOfStreamError } from 'peek-readable';
|
|
3
|
-
import { open as fsOpen } from 'node:fs/promises';
|
|
4
|
-
export class FileTokenizer extends AbstractTokenizer {
|
|
5
|
-
constructor(fileHandle, options) {
|
|
6
|
-
super(options);
|
|
7
|
-
this.fileHandle = fileHandle;
|
|
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 this.fileHandle.read(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 this.fileHandle.read(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
|
-
await this.fileHandle.close();
|
|
41
|
-
return super.close();
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
export async function fromFile(sourceFilePath) {
|
|
45
|
-
const fileHandle = await fsOpen(sourceFilePath, 'r');
|
|
46
|
-
const stat = await fileHandle.stat();
|
|
47
|
-
return new FileTokenizer(fileHandle, { fileInfo: { path: sourceFilePath, size: stat.size } });
|
|
48
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { AbstractTokenizer } from './AbstractTokenizer.js';
|
|
2
|
-
import { type IStreamReader } from 'peek-readable';
|
|
3
|
-
import type { IReadChunkOptions, ITokenizerOptions } from './types.js';
|
|
4
|
-
export declare class ReadStreamTokenizer extends AbstractTokenizer {
|
|
5
|
-
private streamReader;
|
|
6
|
-
/**
|
|
7
|
-
* Constructor
|
|
8
|
-
* @param streamReader stream-reader to read from
|
|
9
|
-
* @param options Tokenizer options
|
|
10
|
-
*/
|
|
11
|
-
constructor(streamReader: IStreamReader, options?: ITokenizerOptions);
|
|
12
|
-
/**
|
|
13
|
-
* Read buffer from tokenizer
|
|
14
|
-
* @param uint8Array - Target Uint8Array to fill with data read from the tokenizer-stream
|
|
15
|
-
* @param options - Read behaviour options
|
|
16
|
-
* @returns Promise with number of bytes read
|
|
17
|
-
*/
|
|
18
|
-
readBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
|
|
19
|
-
/**
|
|
20
|
-
* Peek (read ahead) buffer from tokenizer
|
|
21
|
-
* @param uint8Array - Uint8Array (or Buffer) to write data to
|
|
22
|
-
* @param options - Read behaviour options
|
|
23
|
-
* @returns Promise with number of bytes peeked
|
|
24
|
-
*/
|
|
25
|
-
peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
|
|
26
|
-
ignore(length: number): Promise<number>;
|
|
27
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { AbstractTokenizer } from './AbstractTokenizer.js';
|
|
2
|
-
import { EndOfStreamError } from 'peek-readable';
|
|
3
|
-
const maxBufferSize = 256000;
|
|
4
|
-
export class ReadStreamTokenizer extends AbstractTokenizer {
|
|
5
|
-
/**
|
|
6
|
-
* Constructor
|
|
7
|
-
* @param streamReader stream-reader to read from
|
|
8
|
-
* @param options Tokenizer options
|
|
9
|
-
*/
|
|
10
|
-
constructor(streamReader, options) {
|
|
11
|
-
super(options);
|
|
12
|
-
this.streamReader = streamReader;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Read buffer from tokenizer
|
|
16
|
-
* @param uint8Array - Target Uint8Array to fill with data read from the tokenizer-stream
|
|
17
|
-
* @param options - Read behaviour options
|
|
18
|
-
* @returns Promise with number of bytes read
|
|
19
|
-
*/
|
|
20
|
-
async readBuffer(uint8Array, options) {
|
|
21
|
-
const normOptions = this.normalizeOptions(uint8Array, options);
|
|
22
|
-
const skipBytes = normOptions.position - this.position;
|
|
23
|
-
if (skipBytes > 0) {
|
|
24
|
-
await this.ignore(skipBytes);
|
|
25
|
-
return this.readBuffer(uint8Array, options);
|
|
26
|
-
}
|
|
27
|
-
else if (skipBytes < 0) {
|
|
28
|
-
throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
|
|
29
|
-
}
|
|
30
|
-
if (normOptions.length === 0) {
|
|
31
|
-
return 0;
|
|
32
|
-
}
|
|
33
|
-
const bytesRead = await this.streamReader.read(uint8Array, normOptions.offset, normOptions.length);
|
|
34
|
-
this.position += bytesRead;
|
|
35
|
-
if ((!options || !options.mayBeLess) && bytesRead < normOptions.length) {
|
|
36
|
-
throw new EndOfStreamError();
|
|
37
|
-
}
|
|
38
|
-
return bytesRead;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Peek (read ahead) buffer from tokenizer
|
|
42
|
-
* @param uint8Array - Uint8Array (or Buffer) to write data to
|
|
43
|
-
* @param options - Read behaviour options
|
|
44
|
-
* @returns Promise with number of bytes peeked
|
|
45
|
-
*/
|
|
46
|
-
async peekBuffer(uint8Array, options) {
|
|
47
|
-
const normOptions = this.normalizeOptions(uint8Array, options);
|
|
48
|
-
let bytesRead = 0;
|
|
49
|
-
if (normOptions.position) {
|
|
50
|
-
const skipBytes = normOptions.position - this.position;
|
|
51
|
-
if (skipBytes > 0) {
|
|
52
|
-
const skipBuffer = new Uint8Array(normOptions.length + skipBytes);
|
|
53
|
-
bytesRead = await this.peekBuffer(skipBuffer, { mayBeLess: normOptions.mayBeLess });
|
|
54
|
-
uint8Array.set(skipBuffer.subarray(skipBytes), normOptions.offset);
|
|
55
|
-
return bytesRead - skipBytes;
|
|
56
|
-
}
|
|
57
|
-
else if (skipBytes < 0) {
|
|
58
|
-
throw new Error('Cannot peek from a negative offset in a stream');
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
if (normOptions.length > 0) {
|
|
62
|
-
try {
|
|
63
|
-
bytesRead = await this.streamReader.peek(uint8Array, normOptions.offset, normOptions.length);
|
|
64
|
-
}
|
|
65
|
-
catch (err) {
|
|
66
|
-
if (options && options.mayBeLess && err instanceof EndOfStreamError) {
|
|
67
|
-
return 0;
|
|
68
|
-
}
|
|
69
|
-
throw err;
|
|
70
|
-
}
|
|
71
|
-
if ((!normOptions.mayBeLess) && bytesRead < normOptions.length) {
|
|
72
|
-
throw new EndOfStreamError();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return bytesRead;
|
|
76
|
-
}
|
|
77
|
-
async ignore(length) {
|
|
78
|
-
// debug(`ignore ${this.position}...${this.position + length - 1}`);
|
|
79
|
-
const bufSize = Math.min(maxBufferSize, length);
|
|
80
|
-
const buf = new Uint8Array(bufSize);
|
|
81
|
-
let totBytesRead = 0;
|
|
82
|
-
while (totBytesRead < length) {
|
|
83
|
-
const remaining = length - totBytesRead;
|
|
84
|
-
const bytesRead = await this.readBuffer(buf, { length: Math.min(bufSize, remaining) });
|
|
85
|
-
if (bytesRead < 0) {
|
|
86
|
-
return bytesRead;
|
|
87
|
-
}
|
|
88
|
-
totBytesRead += bytesRead;
|
|
89
|
-
}
|
|
90
|
-
return totBytesRead;
|
|
91
|
-
}
|
|
92
|
-
}
|