strtok3 10.3.1 → 10.3.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/README.md +105 -75
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -57,15 +57,79 @@ If you find this project useful and would like to support its development, consi
|
|
|
57
57
|
### strtok3 methods
|
|
58
58
|
|
|
59
59
|
Use one of the methods to instantiate an [*abstract tokenizer*](#tokenizer-object):
|
|
60
|
+
- [fromBlob](#fromblob-function)
|
|
61
|
+
- [fromBuffer](#frombuffer-function)
|
|
60
62
|
- [fromFile](#fromfile-function)*
|
|
61
63
|
- [fromStream](#fromstream-function)*
|
|
62
64
|
- [fromWebStream](#fromwebstream-function)
|
|
63
|
-
- [fromBuffer](#frombuffer-function)
|
|
64
65
|
|
|
65
|
-
>
|
|
66
|
+
> [!NOTE]
|
|
67
|
+
> `fromFile` and `fromStream` only available when importing this module with Node.js
|
|
66
68
|
|
|
67
69
|
All methods return a [`Tokenizer`](#tokenizer-object), either directly or via a promise.
|
|
68
70
|
|
|
71
|
+
#### `fromBlob()` function
|
|
72
|
+
|
|
73
|
+
Create a tokenizer from a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
function fromBlob(blob: Blob, options?: ITokenizerOptions): BlobTokenizer
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
| Parameter | Optional | Type | Description |
|
|
80
|
+
|-----------|-----------|---------------------------------------------------|----------------------------------------------------------------------------------------|
|
|
81
|
+
| blob | no | [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) | [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) or [File](https://developer.mozilla.org/en-US/docs/Web/API/File) to read from |
|
|
82
|
+
| options | yes | [ITokenizerOptions](#ITokenizerOptions) | Tokenizer options |
|
|
83
|
+
|
|
84
|
+
Returns a [*tokenizer*](#tokenizer-object).
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
import { fromBlob } from 'strtok3';
|
|
88
|
+
import { openAsBlob } from 'node:fs';
|
|
89
|
+
import * as Token from 'token-types';
|
|
90
|
+
|
|
91
|
+
async function parse() {
|
|
92
|
+
const blob = await openAsBlob('somefile.bin');
|
|
93
|
+
|
|
94
|
+
const tokenizer = fromBlob(blob);
|
|
95
|
+
|
|
96
|
+
const myUint8Number = await tokenizer.readToken(Token.UINT8);
|
|
97
|
+
console.log(`My number: ${myUint8Number}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
parse();
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
#### `fromBuffer()` function
|
|
105
|
+
|
|
106
|
+
Create a tokenizer from memory ([Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) or Node.js [Buffer](https://nodejs.org/api/buffer.html)).
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
function fromBuffer(uint8Array: Uint8Array, options?: ITokenizerOptions): BufferTokenizer
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
| Parameter | Optional | Type | Description |
|
|
113
|
+
|------------|----------|--------------------------------------------------|-----------------------------------|
|
|
114
|
+
| uint8Array | no | [Uint8Array](https://nodejs.org/api/buffer.html) | Buffer or Uint8Array to read from |
|
|
115
|
+
| options | yes | [ITokenizerOptions](#ITokenizerOptions) | Tokenizer options |
|
|
116
|
+
|
|
117
|
+
Returns a [*tokenizer*](#tokenizer-object).
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
import { fromBuffer } from 'strtok3';
|
|
121
|
+
import * as Token from 'token-types';
|
|
122
|
+
|
|
123
|
+
const tokenizer = fromBuffer(buffer);
|
|
124
|
+
|
|
125
|
+
async function parse() {
|
|
126
|
+
const myUint8Number = await tokenizer.readToken(Token.UINT8);
|
|
127
|
+
console.log(`My number: ${myUint8Number}`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
parse();
|
|
131
|
+
```
|
|
132
|
+
|
|
69
133
|
#### `fromFile` function
|
|
70
134
|
|
|
71
135
|
Creates a [*tokenizer*](#tokenizer-object) from a local file.
|
|
@@ -82,91 +146,57 @@ function fromFile(sourceFilePath: string): Promise<FileTokenizer>
|
|
|
82
146
|
> - Only available for Node.js engines
|
|
83
147
|
> - `fromFile` automatically embeds [file-information](#file-information)
|
|
84
148
|
|
|
85
|
-
|
|
149
|
+
A Promise resolving to a [*tokenizer*](#tokenizer-object) which can be used to parse a file.
|
|
86
150
|
|
|
87
151
|
```js
|
|
88
|
-
import
|
|
152
|
+
import { fromFile } from 'strtok3';
|
|
89
153
|
import * as Token from 'token-types';
|
|
90
154
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
try {
|
|
155
|
+
async function parse() {
|
|
156
|
+
const tokenizer = await fromFile('somefile.bin');
|
|
157
|
+
try {
|
|
95
158
|
const myNumber = await tokenizer.readToken(Token.UINT8);
|
|
96
159
|
console.log(`My number: ${myNumber}`);
|
|
97
160
|
} finally {
|
|
98
161
|
tokenizer.close(); // Close the file
|
|
99
162
|
}
|
|
100
|
-
}
|
|
101
|
-
```
|
|
163
|
+
}
|
|
102
164
|
|
|
103
|
-
|
|
165
|
+
parse();
|
|
104
166
|
|
|
105
|
-
Creates a [*tokenizer*](#tokenizer-object) from a Node.js [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable).
|
|
106
167
|
|
|
107
|
-
```ts
|
|
108
|
-
function fromStream(stream: Readable, options?: ITokenizerOptions): Promise<ReadStreamTokenizer>
|
|
109
168
|
```
|
|
110
169
|
|
|
111
|
-
|
|
112
|
-
|-----------|-----------|-------------------------|--------------------------|
|
|
113
|
-
| stream | no | [Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable) | Stream to read from |
|
|
114
|
-
| fileInfo | yes | [IFileInfo](#IFileInfo) | Provide file information |
|
|
115
|
-
|
|
116
|
-
Returns a Promise providing a [*tokenizer*](#tokenizer-object).
|
|
117
|
-
|
|
118
|
-
> [!NOTE]
|
|
119
|
-
> - Only available for Node.js engines
|
|
170
|
+
#### `fromWebStream()` function
|
|
120
171
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
Creates [*tokenizer*](#tokenizer-object) from a [WHATWG ReadableStream](https://nodejs.org/api/webstreams.html#web-streams-api).
|
|
172
|
+
Create a tokenizer from a [WHATWG ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
|
|
124
173
|
|
|
125
174
|
```ts
|
|
126
175
|
function fromWebStream(webStream: AnyWebByteStream, options?: ITokenizerOptions): ReadStreamTokenizer
|
|
127
176
|
```
|
|
128
177
|
|
|
129
|
-
| Parameter |
|
|
130
|
-
|
|
131
|
-
|
|
|
132
|
-
|
|
|
178
|
+
| Parameter | Optional | Type | Description |
|
|
179
|
+
|----------------|----------|--------------------------------------------------------------------------|------------------------------------|
|
|
180
|
+
| webStream | no | [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) | WHATWG ReadableStream to read from |
|
|
181
|
+
| options | yes | [ITokenizerOptions](#ITokenizerOptions) | Tokenizer options |
|
|
133
182
|
|
|
134
|
-
Returns a
|
|
183
|
+
Returns a [*tokenizer*](#tokenizer-object).
|
|
135
184
|
|
|
136
185
|
```js
|
|
137
|
-
import
|
|
186
|
+
import { fromWebStream } from 'strtok3';
|
|
138
187
|
import * as Token from 'token-types';
|
|
139
188
|
|
|
140
|
-
|
|
141
|
-
|
|
189
|
+
async function parse() {
|
|
190
|
+
const tokenizer = fromWebStream(readableStream);
|
|
191
|
+
try {
|
|
192
|
+
const myUint8Number = await tokenizer.readToken(Token.UINT8);
|
|
142
193
|
console.log(`My number: ${myUint8Number}`);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
#### `fromBuffer()` function
|
|
148
|
-
|
|
149
|
-
Create a tokenizer from memory ([Uint8Array](https://nodejs.org/api/buffer.html)).
|
|
150
|
-
|
|
151
|
-
```ts
|
|
152
|
-
function fromBuffer(uint8Array: Uint8Array, options?: ITokenizerOptions): BufferTokenizer
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
| Parameter | Optional | Type | Description |
|
|
156
|
-
|------------|----------|--------------------------------------------------|----------------------------------------|
|
|
157
|
-
| uint8Array | no | [Uint8Array](https://nodejs.org/api/buffer.html) | Uint8Array or Buffer to read from |
|
|
158
|
-
| fileInfo | yes | [IFileInfo](#IFileInfo) | Provide file information |
|
|
159
|
-
|
|
160
|
-
Returns a Promise providing a [*tokenizer*](#tokenizer-object).
|
|
161
|
-
|
|
162
|
-
```js
|
|
163
|
-
import * as strtok3 from 'strtok3';
|
|
164
|
-
|
|
165
|
-
const tokenizer = strtok3.fromBuffer(buffer);
|
|
194
|
+
} finally {
|
|
195
|
+
await tokenizer.close();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
166
198
|
|
|
167
|
-
|
|
168
|
-
console.log(`My number: ${myUint8Number}`);
|
|
169
|
-
});
|
|
199
|
+
parse();
|
|
170
200
|
```
|
|
171
201
|
|
|
172
202
|
### `Tokenizer` object
|
|
@@ -202,7 +232,7 @@ readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
|
|
|
202
232
|
| options | [IReadChunkOptions](#ireadchunkoptions) | An integer specifying the number of bytes to read |
|
|
203
233
|
|
|
204
234
|
Return promise with number of bytes read.
|
|
205
|
-
The number of bytes read
|
|
235
|
+
The number of bytes read may be less than requested if the `mayBeLess` flag is set.
|
|
206
236
|
|
|
207
237
|
#### `peekBuffer` function
|
|
208
238
|
|
|
@@ -217,7 +247,7 @@ peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>
|
|
|
217
247
|
| buffer | Buffer | Uint8Array | Target buffer to write the data read (peeked) to. |
|
|
218
248
|
| options | [IReadChunkOptions](#ireadchunkoptions) | An integer specifying the number of bytes to read. | |
|
|
219
249
|
|
|
220
|
-
Return value `Promise<number>` Promise with number of bytes read. The number of bytes read
|
|
250
|
+
Return value `Promise<number>` Promise with number of bytes read. The number of bytes read may be less if the `mayBeLess` flag was set.
|
|
221
251
|
|
|
222
252
|
#### `readToken` function
|
|
223
253
|
|
|
@@ -232,7 +262,7 @@ readToken<Value>(token: IGetToken<Value>, position: number = this.position): Pro
|
|
|
232
262
|
| token | [IGetToken](#IGetToken) | Token to read from the tokenizer-stream. |
|
|
233
263
|
| position? | number | Offset where to begin reading within the file. If position is null, data will be read from the current file position. |
|
|
234
264
|
|
|
235
|
-
Return value `Promise<number>`. Promise with number of bytes read. The number of bytes read maybe if less,
|
|
265
|
+
Return value `Promise<number>`. Promise with number of bytes read. The number of bytes read maybe if less, `mayBeLess` flag was set.
|
|
236
266
|
|
|
237
267
|
#### `peek` function
|
|
238
268
|
|
|
@@ -251,7 +281,7 @@ Return a promise with the token value peeked from the [*tokenizer*](#tokenizer-o
|
|
|
251
281
|
|
|
252
282
|
#### `readNumber` function
|
|
253
283
|
|
|
254
|
-
|
|
284
|
+
Read a numeric [*token*](#token) from the [*tokenizer*](#tokenizer-object).
|
|
255
285
|
|
|
256
286
|
```ts
|
|
257
287
|
readNumber(token: IToken<number>): Promise<number>
|
|
@@ -261,7 +291,7 @@ readNumber(token: IToken<number>): Promise<number>
|
|
|
261
291
|
|------------|---------------------------------|----------------------------------------------------|
|
|
262
292
|
| token | [IGetToken<number>](#IGetToken) | Numeric token to read from the tokenizer-stream. |
|
|
263
293
|
|
|
264
|
-
|
|
294
|
+
A promise resolving to a numeric value read and decoded from the *tokenizer-stream*.
|
|
265
295
|
|
|
266
296
|
#### `ignore` function
|
|
267
297
|
|
|
@@ -271,11 +301,11 @@ Advance the offset pointer with the token number of bytes provided.
|
|
|
271
301
|
ignore(length: number): Promise<number>
|
|
272
302
|
```
|
|
273
303
|
|
|
274
|
-
| Parameter | Type | Description
|
|
275
|
-
|
|
276
|
-
|
|
|
304
|
+
| Parameter | Type | Description |
|
|
305
|
+
|------------|--------|------------------------------------------------------------------|
|
|
306
|
+
| length | number | Number of bytes to ignore. Will advance the `tokenizer.position` |
|
|
277
307
|
|
|
278
|
-
|
|
308
|
+
A promise resolving to the number of bytes ignored from the *tokenizer-stream*.
|
|
279
309
|
|
|
280
310
|
#### `close` function
|
|
281
311
|
Clean up resources, such as closing a file pointer if applicable.
|
|
@@ -299,7 +329,7 @@ Each attribute is optional:
|
|
|
299
329
|
|-----------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
300
330
|
| length | number | Requested number of bytes to read. |
|
|
301
331
|
| position | number | Position where to peek from the file. If position is null, data will be read from the [current file position](#attribute-tokenizerposition). Position may not be less then [tokenizer.position](#attribute-tokenizerposition) |
|
|
302
|
-
| mayBeLess | boolean | If and only if set, will not throw an EOF error if less
|
|
332
|
+
| mayBeLess | boolean | If and only if set, will not throw an EOF error if less than the requested `mayBeLess` could be read. |
|
|
303
333
|
|
|
304
334
|
Example usage:
|
|
305
335
|
```js
|
|
@@ -313,13 +343,13 @@ Provides optional metadata about the file being tokenized.
|
|
|
313
343
|
| Attribute | Type | Description |
|
|
314
344
|
|-----------|---------|---------------------------------------------------------------------------------------------------|
|
|
315
345
|
| size | number | File size in bytes |
|
|
316
|
-
| mimeType |
|
|
317
|
-
| path |
|
|
318
|
-
| url |
|
|
346
|
+
| mimeType | string | [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) of file. |
|
|
347
|
+
| path | string | File path |
|
|
348
|
+
| url | string | File URL |
|
|
319
349
|
|
|
320
350
|
### `Token` object
|
|
321
351
|
|
|
322
|
-
The *token* is basically a description what to read
|
|
352
|
+
The *token* is basically a description of what to read from the [*tokenizer-stream*](#tokenizer-object).
|
|
323
353
|
A basic set of *token types* can be found here: [*token-types*](https://github.com/Borewit/token-types).
|
|
324
354
|
|
|
325
355
|
A token is something which implements the following interface:
|
|
@@ -346,7 +376,7 @@ The `token.get` will be called with the Buffer. `token.get` is responsible for c
|
|
|
346
376
|
To convert a [Web-API readable stream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader) into a [Node.js readable stream]((https://nodejs.org/api/stream.html#stream_readable_streams)), you can use [readable-web-to-node-stream](https://github.com/Borewit/readable-web-to-node-stream) to convert one in another.
|
|
347
377
|
|
|
348
378
|
```js
|
|
349
|
-
import { fromWebStream }
|
|
379
|
+
import { fromWebStream } from 'strtok3';
|
|
350
380
|
import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';
|
|
351
381
|
|
|
352
382
|
(async () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "strtok3",
|
|
3
|
-
"version": "10.3.
|
|
3
|
+
"version": "10.3.2",
|
|
4
4
|
"description": "A promise based streaming tokenizer",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Borewit",
|
|
@@ -55,21 +55,21 @@
|
|
|
55
55
|
"@tokenizer/token": "^0.3.0"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@biomejs/biome": "
|
|
58
|
+
"@biomejs/biome": "2.1.1",
|
|
59
59
|
"@types/chai": "^5.2.2",
|
|
60
60
|
"@types/chai-as-promised": "^8.0.2",
|
|
61
61
|
"@types/debug": "^4.1.12",
|
|
62
62
|
"@types/mocha": "^10.0.10",
|
|
63
|
-
"@types/node": "^
|
|
63
|
+
"@types/node": "^24.0.7",
|
|
64
64
|
"c8": "^10.1.3",
|
|
65
|
-
"chai": "^5.2.
|
|
65
|
+
"chai": "^5.2.1",
|
|
66
66
|
"chai-as-promised": "^8.0.1",
|
|
67
67
|
"del-cli": "^6.0.0",
|
|
68
|
-
"mocha": "^11.
|
|
68
|
+
"mocha": "^11.7.1",
|
|
69
69
|
"node-readable-to-web-readable-stream": "^0.4.2",
|
|
70
70
|
"remark-cli": "^12.0.1",
|
|
71
71
|
"remark-preset-lint-recommended": "^7.0.1",
|
|
72
|
-
"token-types": "^6.0.
|
|
72
|
+
"token-types": "^6.0.3",
|
|
73
73
|
"ts-node": "^10.9.2",
|
|
74
74
|
"typescript": "^5.8.3",
|
|
75
75
|
"uint8array-extras": "^1.4.0"
|
|
@@ -88,5 +88,5 @@
|
|
|
88
88
|
"stream",
|
|
89
89
|
"streaming"
|
|
90
90
|
],
|
|
91
|
-
"packageManager": "yarn@4.9.
|
|
91
|
+
"packageManager": "yarn@4.9.2"
|
|
92
92
|
}
|