strtok3 10.3.4 → 10.4.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.
package/README.md CHANGED
@@ -1,399 +1,561 @@
1
- [![Node.js CI](https://github.com/Borewit/strtok3/actions/workflows/ci.yml/badge.svg)](https://github.com/Borewit/strtok3/actions/workflows/ci.yml)
2
- [![CodeQL](https://github.com/Borewit/strtok3/actions/workflows/codeql.yml/badge.svg?branch=master)](https://github.com/Borewit/strtok3/actions/workflows/codeql.yml)
3
- [![NPM version](https://badge.fury.io/js/strtok3.svg)](https://npmjs.org/package/strtok3)
4
- [![npm downloads](http://img.shields.io/npm/dm/strtok3.svg)](https://npmcharts.com/compare/strtok3,token-types?start=1200&interval=30)
5
- [![DeepScan grade](https://deepscan.io/api/teams/5165/projects/8526/branches/103329/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=5165&pid=8526&bid=103329)
6
- [![Known Vulnerabilities](https://snyk.io/test/github/Borewit/strtok3/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Borewit/strtok3?targetFile=package.json)
7
- [![Codacy Badge](https://api.codacy.com/project/badge/Grade/59dd6795e61949fb97066ca52e6097ef)](https://www.codacy.com/app/Borewit/strtok3?utm_source=github.com&utm_medium=referral&utm_content=Borewit/strtok3&utm_campaign=Badge_Grade)
8
- # strtok3
9
-
10
- A promise based streaming [*tokenizer*](#tokenizer-object) for [Node.js](http://nodejs.org) and browsers.
11
-
12
- The `strtok3` module provides several methods for creating a [*tokenizer*](#tokenizer-object) from various input sources.
13
- Designed for:
14
- * Seamless support in streaming environments.
15
- * Efficiently decode binary data, strings, and numbers.
16
- * Reading [predefined](https://github.com/Borewit/token-types) or custom tokens.
17
- * Offering [*tokenizers*](#tokenizer-object) for reading from [files](#method-strtok3fromfile), [streams](#fromstream-function) or [Uint8Arrays](#frombuffer-function).
18
-
19
- ### Features
20
- `strtok3` can read from:
21
- * Files, using a file path as input.
22
- * Node.js [streams](https://nodejs.org/api/stream.html).
23
- * [Buffer](https://nodejs.org/api/buffer.html) or [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).
24
- * HTTP chunked transfer provided by [@tokenizer/http](https://github.com/Borewit/tokenizer-http).
25
- * [Amazon S3](https://aws.amazon.com/s3) chunks with [@tokenizer/s3](https://github.com/Borewit/tokenizer-s3).
26
-
27
- ## Installation
28
-
29
- ```sh
30
- npm install strtok3
31
- ```
32
-
33
- ### Compatibility
34
-
35
- Starting with version 7, the module has migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
36
- The distributed JavaScript codebase is compliant with the [ECMAScript 2020 (11th Edition)](https://en.wikipedia.org/wiki/ECMAScript_version_history#11th_Edition_%E2%80%93_ECMAScript_2020) standard.
37
-
38
- Requires a modern browser, Node.js (V8) ≥ 18 engine or Bun (JavaScriptCore) ≥ 1.2.
39
-
40
- For TypeScript CommonJs backward compatibility, you can use [load-esm](https://github.com/Borewit/load-esm).
41
-
42
- > [!NOTE]
43
- > This module requires a [Node.js ≥ 16](https://nodejs.org/en/about/previous-releases) engine.
44
- > It can also be used in a browser environment when bundled with a module bundler.
45
-
46
- ## Support the Project
47
- If you find this project useful and would like to support its development, consider sponsoring or contributing:
48
-
49
- - [Become a sponsor to Borewit](https://github.com/sponsors/Borewit)
50
-
51
- - Buy me a coffee:
52
-
53
- <a href="https://www.buymeacoffee.com/borewit" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy me A coffee" height="41" width="174"></a>
54
-
55
- ## API Documentation
56
-
57
- ### strtok3 methods
58
-
59
- Use one of the methods to instantiate an [*abstract tokenizer*](#tokenizer-object):
60
- - [fromBlob](#fromblob-function)
61
- - [fromBuffer](#frombuffer-function)
62
- - [fromFile](#fromfile-function)*
63
- - [fromStream](#fromstream-function)*
64
- - [fromWebStream](#fromwebstream-function)
65
-
66
- > [!NOTE]
67
- > `fromFile` and `fromStream` only available when importing this module with Node.js
68
-
69
- All methods return a [`Tokenizer`](#tokenizer-object), either directly or via a promise.
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
-
133
- #### `fromFile` function
134
-
135
- Creates a [*tokenizer*](#tokenizer-object) from a local file.
136
-
137
- ```ts
138
- function fromFile(sourceFilePath: string): Promise<FileTokenizer>
139
- ```
140
-
141
- | Parameter | Type | Description |
142
- |----------------|----------|----------------------------|
143
- | sourceFilePath | `string` | Path to file to read from |
144
-
145
- > [!NOTE]
146
- > - Only available for Node.js engines
147
- > - `fromFile` automatically embeds [file-information](#file-information)
148
-
149
- A Promise resolving to a [*tokenizer*](#tokenizer-object) which can be used to parse a file.
150
-
151
- ```js
152
- import { fromFile } from 'strtok3';
153
- import * as Token from 'token-types';
154
-
155
- async function parse() {
156
- const tokenizer = await fromFile('somefile.bin');
157
- try {
158
- const myNumber = await tokenizer.readToken(Token.UINT8);
159
- console.log(`My number: ${myNumber}`);
160
- } finally {
161
- tokenizer.close(); // Close the file
162
- }
163
- }
164
-
165
- parse();
166
-
167
-
168
- ```
169
-
170
- #### `fromWebStream()` function
171
-
172
- Create a tokenizer from a [WHATWG ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
173
-
174
- ```ts
175
- function fromWebStream(webStream: AnyWebByteStream, options?: ITokenizerOptions): ReadStreamTokenizer
176
- ```
177
-
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 |
182
-
183
- Returns a [*tokenizer*](#tokenizer-object).
184
-
185
- ```js
186
- import { fromWebStream } from 'strtok3';
187
- import * as Token from 'token-types';
188
-
189
- async function parse() {
190
- const tokenizer = fromWebStream(readableStream);
191
- try {
192
- const myUint8Number = await tokenizer.readToken(Token.UINT8);
193
- console.log(`My number: ${myUint8Number}`);
194
- } finally {
195
- await tokenizer.close();
196
- }
197
- }
198
-
199
- parse();
200
- ```
201
-
202
- ### `Tokenizer` object
203
- The *tokenizer* is an abstraction of a [stream](https://nodejs.org/api/stream.html), file or [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array), allowing _reading_ or _peeking_ from the stream.
204
- It can also be translated in chunked reads, as done in [@tokenizer/http](https://github.com/Borewit/tokenizer-http);
205
-
206
- #### Key Features:
207
-
208
- - Supports seeking within the stream using `tokenizer.ignore()`.
209
- - Offers `peek` methods to preview data without advancing the read pointer.
210
- - Maintains the read position via tokenizer.position.
211
-
212
- #### Tokenizer functions
213
-
214
- _Read_ methods advance the stream pointer, while _peek_ methods do not.
215
-
216
- There are two kind of functions:
217
- 1. *read* methods: used to read a *token* of [Buffer](https://nodejs.org/api/buffer.html) from the [*tokenizer*](#tokenizer-object). The position of the *tokenizer-stream* will advance with the size of the token.
218
- 2. *peek* methods: same as the read, but it will *not* advance the pointer. It allows to read (peek) ahead.
219
-
220
- #### `readBuffer` function
221
-
222
- Read data from the _tokenizer_ into provided "buffer" (`Uint8Array`).
223
- `readBuffer(buffer, options?)`
224
-
225
- ```ts
226
- readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
227
- ```
228
-
229
- | Parameter | Type | Description |
230
- |------------|----------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
231
- | buffer | [Buffer](https://nodejs.org/api/buffer.html) &#124; Uint8Array | Target buffer to write the data read to |
232
- | options | [IReadChunkOptions](#ireadchunkoptions) | An integer specifying the number of bytes to read |
233
-
234
- Return promise with number of bytes read.
235
- The number of bytes read may be less than requested if the `mayBeLess` flag is set.
236
-
237
- #### `peekBuffer` function
238
-
239
- Peek (read ahead), from [*tokenizer*](#tokenizer-object), into the buffer without advancing the stream pointer.
240
-
241
- ```ts
242
- peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
243
- ```
244
-
245
- | Parameter | Type | Description |
246
- |------------|-----------------------------------------|-----------------------------------------------------|
247
- | buffer | Buffer &#124; Uint8Array | Target buffer to write the data read (peeked) to. |
248
- | options | [IReadChunkOptions](#ireadchunkoptions) | An integer specifying the number of bytes to read. | |
249
-
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.
251
-
252
- #### `readToken` function
253
-
254
- Read a *token* from the tokenizer-stream.
255
-
256
- ```ts
257
- readToken<Value>(token: IGetToken<Value>, position: number = this.position): Promise<Value>
258
- ```
259
-
260
- | Parameter | Type | Description |
261
- |------------|-------------------------|---------------------------------------------------------------------------------------------------------------------- |
262
- | token | [IGetToken](#IGetToken) | Token to read from the tokenizer-stream. |
263
- | position? | number | Offset where to begin reading within the file. If position is null, data will be read from the current file position. |
264
-
265
- Return value `Promise<number>`. Promise with number of bytes read. The number of bytes read maybe if less, `mayBeLess` flag was set.
266
-
267
- #### `peek` function
268
-
269
- Peek a *token* from the [*tokenizer*](#tokenizer-object).
270
-
271
- ```ts
272
- peekToken<Value>(token: IGetToken<Value>, position: number = this.position): Promise<Value>
273
- ```
274
-
275
- | Parameter | Type | Description |
276
- |------------|----------------------------|-------------------------------------------------------------------------------------------------------------------------|
277
- | token | [IGetToken<T>](#IGetToken) | Token to read from the tokenizer-stream. |
278
- | position? | number | Offset where to begin reading within the file. If position is null, data will be read from the current file position. |
279
-
280
- Return a promise with the token value peeked from the [*tokenizer*](#tokenizer-object).
281
-
282
- #### `readNumber` function
283
-
284
- Read a numeric [*token*](#token) from the [*tokenizer*](#tokenizer-object).
285
-
286
- ```ts
287
- readNumber(token: IToken<number>): Promise<number>
288
- ```
289
-
290
- | Parameter | Type | Description |
291
- |------------|---------------------------------|----------------------------------------------------|
292
- | token | [IGetToken<number>](#IGetToken) | Numeric token to read from the tokenizer-stream. |
293
-
294
- A promise resolving to a numeric value read and decoded from the *tokenizer-stream*.
295
-
296
- #### `ignore` function
297
-
298
- Advance the offset pointer with the token number of bytes provided.
299
-
300
- ```ts
301
- ignore(length: number): Promise<number>
302
- ```
303
-
304
- | Parameter | Type | Description |
305
- |------------|--------|------------------------------------------------------------------|
306
- | length | number | Number of bytes to ignore. Will advance the `tokenizer.position` |
307
-
308
- A promise resolving to the number of bytes ignored from the *tokenizer-stream*.
309
-
310
- #### `close` function
311
- Clean up resources, such as closing a file pointer if applicable.
312
-
313
- #### `Tokenizer` attributes
314
-
315
- - `fileInfo`
316
-
317
- Optional attribute describing the file information, see [IFileInfo](#IFileInfo)
318
-
319
- - `position`
320
-
321
- Pointer to the current position in the [*tokenizer*](#tokenizer-object) stream.
322
- If a *position* is provided to a _read_ or _peek_ method, is should be, at least, equal or greater than this value.
323
-
324
- ### `IReadChunkOptions` interface
325
-
326
- Each attribute is optional:
327
-
328
- | Attribute | Type | Description |
329
- |-----------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
330
- | length | number | Requested number of bytes to read. |
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) |
332
- | mayBeLess | boolean | If and only if set, will not throw an EOF error if less than the requested `mayBeLess` could be read. |
333
-
334
- Example usage:
335
- ```js
336
- tokenizer.peekBuffer(buffer, {mayBeLess: true});
337
- ```
338
-
339
- ### `IFileInfo` interface
340
-
341
- Provides optional metadata about the file being tokenized.
342
-
343
- | Attribute | Type | Description |
344
- |-----------|---------|---------------------------------------------------------------------------------------------------|
345
- | size | number | File size in bytes |
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 |
349
-
350
- ### `Token` object
351
-
352
- The *token* is basically a description of what to read from the [*tokenizer-stream*](#tokenizer-object).
353
- A basic set of *token types* can be found here: [*token-types*](https://github.com/Borewit/token-types).
354
-
355
- A token is something which implements the following interface:
356
- ```ts
357
- export interface IGetToken<T> {
358
-
359
- /**
360
- * Length in bytes of encoded value
361
- */
362
- len: number;
363
-
364
- /**
365
- * Decode value from buffer at offset
366
- * @param buf Buffer to read the decoded value from
367
- * @param off Decode offset
368
- */
369
- get(buf: Uint8Array, off: number): T;
370
- }
371
- ```
372
- The *tokenizer* reads `token.len` bytes from the *tokenizer-stream* into a Buffer.
373
- The `token.get` will be called with the Buffer. `token.get` is responsible for conversion from the buffer to the desired output type.
374
-
375
- ### Working with Web-API readable stream
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.
377
-
378
- ```js
379
- import { fromWebStream } from 'strtok3';
380
- import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';
381
-
382
- (async () => {
383
-
384
- const response = await fetch(url);
385
- const readableWebStream = response.body; // Web-API readable stream
386
- const webStream = new ReadableWebToNodeStream(readableWebStream); // convert to Node.js readable stream
387
-
388
- const tokenizer = fromWebStream(webStream); // And we now have tokenizer in a web environment
389
- })();
390
- ```
391
-
392
- ## Dependencies
393
-
394
- Dependencies:
395
- - [@tokenizer/token](https://github.com/Borewit/tokenizer-token): Provides token definitions and utilities used by `strtok3` for interpreting binary data.
396
-
397
- ## Licence
398
-
399
- This project is licensed under the [MIT License](LICENSE.txt). Feel free to use, modify, and distribute as needed.
1
+ [![Node.js CI](https://github.com/Borewit/strtok3/actions/workflows/ci.yml/badge.svg)](https://github.com/Borewit/strtok3/actions/workflows/ci.yml)
2
+ [![CodeQL](https://github.com/Borewit/strtok3/actions/workflows/codeql.yml/badge.svg?branch=master)](https://github.com/Borewit/strtok3/actions/workflows/codeql.yml)
3
+ [![NPM version](https://badge.fury.io/js/strtok3.svg)](https://npmjs.org/package/strtok3)
4
+ [![npm downloads](http://img.shields.io/npm/dm/strtok3.svg)](https://npmcharts.com/compare/strtok3,token-types?start=1200&interval=30)
5
+ [![DeepScan grade](https://deepscan.io/api/teams/5165/projects/8526/branches/103329/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=5165&pid=8526&bid=103329)
6
+ [![Known Vulnerabilities](https://snyk.io/test/github/Borewit/strtok3/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Borewit/strtok3?targetFile=package.json)
7
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/59dd6795e61949fb97066ca52e6097ef)](https://www.codacy.com/app/Borewit/strtok3?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=Borewit/strtok3&amp;utm_campaign=Badge_Grade)
8
+ # strtok3
9
+
10
+ A promise-based streaming [*tokenizer*](#tokenizer-object) for [Node.js](https://nodejs.org) and browsers.
11
+
12
+ The `strtok3` module provides several methods for creating a [*tokenizer*](#tokenizer-object) from various input sources.
13
+ Designed for:
14
+ * Seamless support in streaming environments.
15
+ * Efficiently decode binary data, strings, and numbers.
16
+ * Reading [predefined](https://github.com/Borewit/token-types) or custom tokens.
17
+ * Offering [*tokenizers*](#tokenizer-object) for reading from [files](#fromfile-function), [streams](#fromstream-function) or [Uint8Arrays](#frombuffer-function).
18
+
19
+ ### Features
20
+ `strtok3` can read from:
21
+ * Files, using a file path as input.
22
+ * Node.js [streams](https://nodejs.org/api/stream.html).
23
+ * WHATWG [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) objects containing `Uint8Array` chunks.
24
+ * [Buffer](https://nodejs.org/api/buffer.html) or [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).
25
+ * [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) or [File](https://developer.mozilla.org/en-US/docs/Web/API/File) objects.
26
+ * HTTP chunked transfer provided by [@tokenizer/http](https://github.com/Borewit/tokenizer-http).
27
+ * [Amazon S3](https://aws.amazon.com/s3) chunks with [@tokenizer/s3](https://github.com/Borewit/tokenizer-s3).
28
+
29
+ ## Installation
30
+
31
+ ```sh
32
+ npm install strtok3
33
+ ```
34
+
35
+ ### Compatibility
36
+
37
+ Starting with version 7, the module has migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
38
+ The distributed JavaScript codebase is compliant with the [ECMAScript 2020 (11th Edition)](https://en.wikipedia.org/wiki/ECMAScript_version_history#11th_Edition_%E2%80%93_ECMAScript_2020) standard.
39
+
40
+ Requires a modern browser, Node.js (V8) ≥ 18 engine or Bun (JavaScriptCore) ≥ 1.2.
41
+ It can also be used in a browser environment when bundled with a module bundler.
42
+
43
+ For TypeScript CommonJS backward compatibility, you can use [load-esm](https://github.com/Borewit/load-esm).
44
+
45
+ Use Node.js 24 LTS to build the project and install development dependencies.
46
+ This build requirement is separate from the Node.js ≥ 18 runtime requirement in `package.json`.
47
+ CI builds once with Node.js 24, then runs the compiled JavaScript unit tests across the Node.js compatibility matrix and Bun 1.2.x, 1.3.x, and 1.4.x.
48
+ Coverage is collected in a separate Node.js 24 job.
49
+
50
+ For local development, running the TypeScript tests with `tsx` requires Node.js 18.19+ (18.x) or Node.js ≥ 20.6.0.
51
+
52
+ ## Support the Project
53
+ If you find this project useful and would like to support its development, consider sponsoring or contributing:
54
+
55
+ - [Become a sponsor to Borewit](https://github.com/sponsors/Borewit)
56
+
57
+ - Buy me a coffee:
58
+
59
+ <a href="https://www.buymeacoffee.com/borewit" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy me A coffee" height="41" width="174"></a>
60
+
61
+ ## API Documentation
62
+
63
+ ### strtok3 methods
64
+
65
+ Use one of the methods to instantiate an [*abstract tokenizer*](#tokenizer-object):
66
+ - [fromBlob](#fromblob-function)
67
+ - [fromBuffer](#frombuffer-function)
68
+ - [fromFile](#fromfile-function)*
69
+ - [fromStream](#fromstream-function)*
70
+ - [fromWebStream](#fromwebstream-function)
71
+
72
+ > [!NOTE]
73
+ > `fromFile` requires Node.js file-system APIs, and `fromStream` requires a Node.js readable stream.
74
+ > In browsers, use `fromBlob`, `fromBuffer`, or `fromWebStream`.
75
+
76
+ All methods return a [`Tokenizer`](#tokenizer-object), either directly or via a promise.
77
+
78
+ #### `fromBlob()` function
79
+
80
+ Create a tokenizer from a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
81
+
82
+ ```ts
83
+ function fromBlob(blob: Blob, options?: ITokenizerOptions): BlobTokenizer
84
+ ```
85
+
86
+ | Parameter | Optional | Type | Description |
87
+ |-----------|-----------|---------------------------------------------------|----------------------------------------------------------------------------------------|
88
+ | 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 |
89
+ | options | yes | [ITokenizerOptions](#itokenizeroptions-interface) | Tokenizer options |
90
+
91
+ Returns a [*tokenizer*](#tokenizer-object).
92
+
93
+ ```js
94
+ import { fromBlob } from 'strtok3';
95
+ import * as Token from 'token-types';
96
+
97
+ async function parse() {
98
+ const blob = new Blob([new Uint8Array([42])]);
99
+
100
+ const tokenizer = fromBlob(blob);
101
+
102
+ const myUint8Number = await tokenizer.readToken(Token.UINT8);
103
+ console.log(`My number: ${myUint8Number}`);
104
+ }
105
+
106
+ parse();
107
+
108
+ ```
109
+
110
+ #### `fromBuffer()` function
111
+
112
+ 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)).
113
+
114
+ ```ts
115
+ function fromBuffer(uint8Array: Uint8Array, options?: ITokenizerOptions): BufferTokenizer
116
+ ```
117
+
118
+ | Parameter | Optional | Type | Description |
119
+ |------------|----------|--------------------------------------------------|-----------------------------------|
120
+ | uint8Array | no | [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) | Buffer or Uint8Array to read from |
121
+ | options | yes | [ITokenizerOptions](#itokenizeroptions-interface) | Tokenizer options |
122
+
123
+ Returns a [*tokenizer*](#tokenizer-object).
124
+
125
+ ```js
126
+ import { fromBuffer } from 'strtok3';
127
+ import * as Token from 'token-types';
128
+
129
+ const tokenizer = fromBuffer(new Uint8Array([42]));
130
+
131
+ async function parse() {
132
+ const myUint8Number = await tokenizer.readToken(Token.UINT8);
133
+ console.log(`My number: ${myUint8Number}`);
134
+ }
135
+
136
+ parse();
137
+ ```
138
+
139
+ #### `fromFile` function
140
+
141
+ Creates a [*tokenizer*](#tokenizer-object) from a local file.
142
+
143
+ ```ts
144
+ function fromFile(sourceFilePath: string, options?: ITokenizerOptions): Promise<FileTokenizer>
145
+ ```
146
+
147
+ | Parameter | Type | Description |
148
+ |----------------|----------|----------------------------|
149
+ | sourceFilePath | `string` | Path to file to read from |
150
+ | options | [ITokenizerOptions](#itokenizeroptions-interface) | Optional tokenizer options |
151
+
152
+ > [!NOTE]
153
+ > - Only available for Node.js engines
154
+ > - `fromFile` automatically embeds [file information](#ifileinfo-interface)
155
+
156
+ A Promise resolving to a [*tokenizer*](#tokenizer-object) which can be used to parse a file.
157
+
158
+ ```js
159
+ import { fromFile } from 'strtok3';
160
+ import * as Token from 'token-types';
161
+
162
+ async function parse() {
163
+ const tokenizer = await fromFile('somefile.bin');
164
+ try {
165
+ const myNumber = await tokenizer.readToken(Token.UINT8);
166
+ console.log(`My number: ${myNumber}`);
167
+ } finally {
168
+ await tokenizer.close(); // Close the file
169
+ }
170
+ }
171
+
172
+ parse();
173
+
174
+
175
+ ```
176
+
177
+ #### `fromStream()` function
178
+
179
+ Create a tokenizer from a Node.js [readable stream](https://nodejs.org/api/stream.html#readable-streams) containing binary data.
180
+
181
+ ```ts
182
+ function fromStream(stream: Readable, options?: ITokenizerOptions): Promise<ReadStreamTokenizer>
183
+ ```
184
+
185
+ | Parameter | Optional | Type | Description |
186
+ |-----------|----------|------|-------------|
187
+ | stream | no | Readable | Node.js readable stream to read from |
188
+ | options | yes | [ITokenizerOptions](#itokenizeroptions-interface) | Tokenizer options |
189
+
190
+ When importing from `strtok3` in Node.js, returns a promise resolving to a [*tokenizer*](#tokenizer-object).
191
+ For streams created by `createReadStream`, the file path and size are added to `tokenizer.fileInfo`.
192
+ The `strtok3/core` entry point also exports `fromStream`, but returns the tokenizer synchronously without looking up file information.
193
+
194
+ ```js
195
+ import { createReadStream } from 'node:fs';
196
+ import { fromStream } from 'strtok3';
197
+ import * as Token from 'token-types';
198
+
199
+ async function parse() {
200
+ const stream = createReadStream('somefile.bin');
201
+ try {
202
+ const tokenizer = await fromStream(stream);
203
+ try {
204
+ const myNumber = await tokenizer.readToken(Token.UINT8);
205
+ console.log(`My number: ${myNumber}`);
206
+ } finally {
207
+ await tokenizer.close();
208
+ }
209
+ } finally {
210
+ stream.destroy();
211
+ }
212
+ }
213
+
214
+ parse();
215
+ ```
216
+
217
+ #### `fromWebStream()` function
218
+
219
+ Create a tokenizer from a [WHATWG ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) containing `Uint8Array` chunks.
220
+
221
+ Uses a BYOB reader when supported by the stream, otherwise a default reader. Source cleanup differs between these readers; see [`close()`](#close-function) and [`abort()`](#abort-function).
222
+
223
+ ```ts
224
+ function fromWebStream(webStream: AnyWebByteStream, options?: ITokenizerOptions): ReadStreamTokenizer
225
+ ```
226
+
227
+ | Parameter | Optional | Type | Description |
228
+ |----------------|----------|--------------------------------------------------------------------------|------------------------------------|
229
+ | webStream | no | [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) | WHATWG ReadableStream to read from |
230
+ | options | yes | [ITokenizerOptions](#itokenizeroptions-interface) | Tokenizer options |
231
+
232
+ Returns a [*tokenizer*](#tokenizer-object).
233
+
234
+ ```js
235
+ import { fromWebStream } from 'strtok3';
236
+ import * as Token from 'token-types';
237
+
238
+ async function parse() {
239
+ const tokenizer = fromWebStream(readableStream);
240
+ try {
241
+ const myUint8Number = await tokenizer.readToken(Token.UINT8);
242
+ console.log(`My number: ${myUint8Number}`);
243
+ } finally {
244
+ await tokenizer.close();
245
+ }
246
+ }
247
+
248
+ parse();
249
+ ```
250
+
251
+ ### `Tokenizer` object
252
+ The *tokenizer* is an abstraction of a [stream](https://nodejs.org/api/stream.html), file or [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array), allowing _reading_ or _peeking_ from the stream.
253
+ It can also be translated in chunked reads, as done in [@tokenizer/http](https://github.com/Borewit/tokenizer-http);
254
+
255
+ #### Key Features:
256
+
257
+ - Supports seeking within the stream using `tokenizer.ignore()`.
258
+ - Offers `peek` methods to preview data without advancing the read pointer.
259
+ - Maintains the read position via `tokenizer.position`.
260
+ - Supports random access for buffer, blob, and file tokenizers.
261
+
262
+ #### Tokenizer functions
263
+
264
+ _Read_ methods advance the stream pointer, while _peek_ methods do not.
265
+
266
+ There are two kinds of functions:
267
+ 1. *read* methods: used to read a *token* or bytes into a buffer from the [*tokenizer*](#tokenizer-object). The position of the *tokenizer-stream* will advance with the number of bytes read.
268
+ 2. *peek* methods: same as the read, but it will *not* advance the pointer. It allows to read (peek) ahead.
269
+
270
+ #### `readBuffer` function
271
+
272
+ Read data from the _tokenizer_ into provided "buffer" (`Uint8Array`).
273
+ `readBuffer(buffer, options?)`
274
+
275
+ ```ts
276
+ readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
277
+ ```
278
+
279
+ | Parameter | Type | Description |
280
+ |------------|----------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
281
+ | buffer | [Buffer](https://nodejs.org/api/buffer.html) &#124; Uint8Array | Target buffer to write the data read to |
282
+ | options | [IReadChunkOptions](#ireadchunkoptions-interface) | Optional settings for the length, position, and handling of partial reads |
283
+
284
+ Return promise with number of bytes read.
285
+ The number of bytes read may be less than requested if the `mayBeLess` flag is set.
286
+
287
+ #### `peekBuffer` function
288
+
289
+ Peek (read ahead), from [*tokenizer*](#tokenizer-object), into the buffer without advancing the stream pointer.
290
+
291
+ ```ts
292
+ peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
293
+ ```
294
+
295
+ | Parameter | Type | Description |
296
+ |------------|-----------------------------------------|-----------------------------------------------------|
297
+ | buffer | Buffer &#124; Uint8Array | Target buffer to write the data read (peeked) to. |
298
+ | options | [IReadChunkOptions](#ireadchunkoptions-interface) | Optional settings for the length, position, and handling of partial reads |
299
+
300
+ Return value `Promise<number>` Promise with number of bytes read. The number of bytes read may be less if the `mayBeLess` flag was set.
301
+
302
+ #### `readToken` function
303
+
304
+ Read a *token* from the tokenizer-stream.
305
+
306
+ ```ts
307
+ readToken<Value>(token: IGetToken<Value>, position: number = this.position): Promise<Value>
308
+ ```
309
+
310
+ | Parameter | Type | Description |
311
+ |------------|-------------------------|---------------------------------------------------------------------------------------------------------------------- |
312
+ | token | [IGetToken](#token-object) | Token to read from the tokenizer-stream. |
313
+ | position? | number | Offset where to begin reading. Defaults to `tokenizer.position` when omitted. |
314
+
315
+ Returns a `Promise<Value>` resolving to the decoded token value. Rejects with `EndOfStreamError` if fewer than `token.len` bytes are available.
316
+
317
+ #### `peekToken` function
318
+
319
+ Peek a *token* from the [*tokenizer*](#tokenizer-object).
320
+
321
+ ```ts
322
+ peekToken<Value>(token: IGetToken<Value>, position: number | null = this.position): Promise<Value>
323
+ ```
324
+
325
+ | Parameter | Type | Description |
326
+ |------------|----------------------------|-------------------------------------------------------------------------------------------------------------------------|
327
+ | token | [IGetToken](#token-object) | Token to peek from the tokenizer-stream. |
328
+ | position? | number &#124; null | Offset where to begin peeking. Defaults to `tokenizer.position` when omitted or `null`. |
329
+
330
+ Return a promise with the token value peeked from the [*tokenizer*](#tokenizer-object).
331
+
332
+ #### `readNumber` function
333
+
334
+ Read a numeric [*token*](#token-object) from the [*tokenizer*](#tokenizer-object).
335
+
336
+ ```ts
337
+ readNumber(token: IToken<number>): Promise<number>
338
+ ```
339
+
340
+ | Parameter | Type | Description |
341
+ |------------|---------------------------------|----------------------------------------------------|
342
+ | token | [IToken<number>](#token-object) | Numeric token to read from the tokenizer-stream. |
343
+
344
+ A promise resolving to a numeric value read and decoded from the *tokenizer-stream*.
345
+
346
+ #### `peekNumber` function
347
+
348
+ Peek a numeric [*token*](#token-object) without advancing `tokenizer.position`.
349
+
350
+ ```ts
351
+ peekNumber(token: IToken<number>): Promise<number>
352
+ ```
353
+
354
+ Returns a promise resolving to the decoded numeric value.
355
+ Like `readNumber`, rejects with `EndOfStreamError` if the token cannot be read in full.
356
+
357
+ #### `ignore` function
358
+
359
+ Advance the offset pointer with the token number of bytes provided.
360
+
361
+ ```ts
362
+ ignore(length: number): Promise<number>
363
+ ```
364
+
365
+ | Parameter | Type | Description |
366
+ |------------|--------|------------------------------------------------------------------|
367
+ | length | number | Non-negative number of bytes to ignore. Advances `tokenizer.position`. |
368
+
369
+ Returns a promise resolving to the number of bytes ignored.
370
+ Buffer, blob, and file tokenizers stop at the known file size. Stream tokenizers reject with `EndOfStreamError` if the requested bytes are unavailable.
371
+
372
+ #### `close` function
373
+ Clean up resources, such as closing a file pointer if applicable.
374
+
375
+ ```ts
376
+ close(): Promise<void>
377
+ ```
378
+
379
+ Await this method when finished with a tokenizer. For Node.js streams, it does not destroy the underlying stream; the caller remains responsible for its lifecycle.
380
+
381
+ For Web streams, `close()` releases the reader lock. A BYOB reader leaves the source uncancelled, allowing another reader to continue reading it. A default reader cancels the source before releasing the lock, discarding remaining data. Bytes already read into tokenizer buffers, including peeked bytes, are not returned to the source.
382
+
383
+ #### `abort` function
384
+
385
+ Abort pending asynchronous stream operations.
386
+
387
+ ```ts
388
+ abort(): Promise<void>
389
+ ```
390
+
391
+ This is a no-op for buffer, blob, and file tokenizers.
392
+
393
+ For Web streams, `abort()` releases a BYOB reader's lock without cancelling the source. A default reader cancels the source but keeps its lock until `close()` is called. An `AbortSignal` invokes the same abort behavior.
394
+
395
+ #### `supportsRandomAccess` function
396
+
397
+ ```ts
398
+ supportsRandomAccess(): boolean
399
+ ```
400
+
401
+ Returns `true` for buffer, blob, and file tokenizers, and `false` for stream tokenizers.
402
+ When random access is supported, read and peek methods can use positions before `tokenizer.position`.
403
+
404
+ #### `setPosition` function
405
+
406
+ Available on buffer, blob, and file tokenizers (`IRandomAccessTokenizer`).
407
+
408
+ ```ts
409
+ setPosition(position: number): void
410
+ ```
411
+
412
+ Sets the current byte offset without reading data.
413
+
414
+ #### `Tokenizer` attributes
415
+
416
+ - `fileInfo`
417
+
418
+ Object containing optional file information, see [IFileInfo](#ifileinfo-interface).
419
+
420
+ - `position`
421
+
422
+ Pointer to the current position in the [*tokenizer*](#tokenizer-object) stream.
423
+ For stream tokenizers, a *position* provided to a _read_ or _peek_ method must be at least this value.
424
+ Buffer, blob, and file tokenizers also support earlier positions.
425
+
426
+ ### `ITokenizerOptions` interface
427
+
428
+ Set these options once when creating a tokenizer. The configured limit applies to subsequent
429
+ reads and peeks for that tokenizer; no per-read limit is needed. Each attribute is optional:
430
+
431
+ | Attribute | Type | Description |
432
+ |-----------|------|-------------|
433
+ | maxBufferSize | number | Maximum bytes in a dynamically sized tokenizer buffer or stream lookahead span. Disabled by default (`Infinity`). Must be a positive safe integer or `Infinity`. |
434
+ | fileInfo | [IFileInfo](#ifileinfo-interface) | Additional metadata about the input |
435
+ | onClose | `() => Promise<void>` | Asynchronous callback invoked when a buffer, blob, or file tokenizer is closed |
436
+ | abortSignal | [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | Signal used to abort asynchronous stream operations |
437
+
438
+ Creating a tokenizer with an already-aborted `abortSignal` throws or rejects with `AbortError`
439
+ before acquiring file handles or stream readers. Signal listeners are removed when the signal
440
+ fires or the tokenizer is closed, so closed tokenizers can be collected even when a signal is reused.
441
+
442
+ When `maxBufferSize` is configured, `readToken()` and `peekToken()` reject oversized buffers with `BufferSizeError`
443
+ before allocating or consuming input. The error exposes `requestedSize` and `maxBufferSize`.
444
+ Token lengths and positions must be non-negative safe integers. When the input size is known,
445
+ a token extending beyond the input rejects with `EndOfStreamError` before allocation.
446
+
447
+ For streams, the lookahead limit includes bytes between the current position and the requested
448
+ peek position. `mayBeLess: true` does not bypass this limit. With a finite cap, reads into
449
+ caller-provided buffers, Blob reads and `ignore()` use bounded chunks where internal buffers
450
+ are needed, so their total requested length may exceed `maxBufferSize`.
451
+
452
+ This limit does not cap total process memory, caller-owned input/output buffers, or allocations
453
+ inside a token's `get()` method. Stream sources may also supply chunks larger than this limit.
454
+ The cap is opt-in to preserve existing buffer sizes. Applications parsing untrusted input should
455
+ set a finite limit, for example 64 MiB. Omit the option or set it to `Infinity` to disable the cap.
456
+
457
+ ```js
458
+ import { BufferSizeError, fromFile } from 'strtok3';
459
+
460
+ const tokenizer = await fromFile('somefile.bin', { maxBufferSize: 64 * 1024 * 1024 });
461
+ try {
462
+ // Read or peek tokens here.
463
+ } catch (error) {
464
+ if (error instanceof BufferSizeError) {
465
+ console.error(`Requested ${error.requestedSize} bytes; limit is ${error.maxBufferSize}`);
466
+ } else {
467
+ throw error;
468
+ }
469
+ } finally {
470
+ await tokenizer.close();
471
+ }
472
+ ```
473
+
474
+ ### `IReadChunkOptions` interface
475
+
476
+ Each attribute is optional:
477
+
478
+ | Attribute | Type | Description |
479
+ |-----------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
480
+ | length | number | Requested number of bytes to read or peek. Defaults to the target buffer length. |
481
+ | position | number | Byte offset to read or peek from. Defaults to [tokenizer.position](#tokenizer-attributes) when omitted. Earlier positions require random access. |
482
+ | mayBeLess | boolean | If `true`, allows fewer than the requested number of bytes to be read or peeked instead of throwing `EndOfStreamError`. Defaults to `false`. |
483
+
484
+ Example usage:
485
+ ```js
486
+ const bytesPeeked = await tokenizer.peekBuffer(buffer, {mayBeLess: true});
487
+ ```
488
+
489
+ ### `IFileInfo` interface
490
+
491
+ Provides optional metadata about the file being tokenized.
492
+
493
+ | Attribute | Type | Description |
494
+ |-----------|---------|---------------------------------------------------------------------------------------------------|
495
+ | size | number | File size in bytes |
496
+ | mimeType | string | [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) of file. |
497
+ | path | string | File path |
498
+ | url | string | File URL |
499
+
500
+ ### `Token` object
501
+
502
+ The *token* is basically a description of what to read from the [*tokenizer-stream*](#tokenizer-object).
503
+ A basic set of *token types* can be found here: [*token-types*](https://github.com/Borewit/token-types).
504
+
505
+ A token is something which implements the following interface:
506
+ ```ts
507
+ export interface IGetToken<T> {
508
+
509
+ /**
510
+ * Length in bytes of encoded value
511
+ */
512
+ len: number;
513
+
514
+ /**
515
+ * Decode value from buffer at offset
516
+ * @param buf Buffer to read the decoded value from
517
+ * @param off Decode offset
518
+ */
519
+ get(buf: Uint8Array, off: number): T;
520
+ }
521
+ ```
522
+ The *tokenizer* reads `token.len` bytes from the *tokenizer-stream* into a Buffer.
523
+ The `token.get` will be called with the Buffer. `token.get` is responsible for conversion from the buffer to the desired output type.
524
+
525
+ ### Working with a fetch response
526
+
527
+ Pass the response's [WHATWG readable stream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) directly to `fromWebStream`.
528
+
529
+ ```js
530
+ import { fromWebStream } from 'strtok3';
531
+ import * as Token from 'token-types';
532
+
533
+ (async () => {
534
+
535
+ const response = await fetch(url);
536
+ if (!response.ok || !response.body) {
537
+ throw new Error(`Cannot read response: ${response.status}`);
538
+ }
539
+
540
+ const tokenizer = fromWebStream(response.body);
541
+ try {
542
+ const myNumber = await tokenizer.readToken(Token.UINT8);
543
+ console.log(`My number: ${myNumber}`);
544
+ } finally {
545
+ await tokenizer.close();
546
+ }
547
+ })();
548
+ ```
549
+
550
+ ## Dependencies
551
+
552
+ Dependencies:
553
+ - [@tokenizer/token](https://github.com/Borewit/tokenizer-token): Provides token definitions and utilities used by `strtok3` for interpreting binary data.
554
+
555
+ ## Contributing
556
+
557
+ See [the contribution guide](CONTRIBUTING.md) for code style and development commands.
558
+
559
+ ## Licence
560
+
561
+ This project is licensed under the [MIT License](LICENSE.txt). Feel free to use, modify, and distribute as needed.