strtok3 8.1.0 → 9.0.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
@@ -7,21 +7,22 @@
7
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
8
  # strtok3
9
9
 
10
- A promise based streaming [*tokenizer*](#tokenizer) for [Node.js](http://nodejs.org) and browsers.
11
- This node module is a successor of [strtok2](https://github.com/Borewit/strtok2).
12
-
13
- The `strtok3` contains a few methods to turn different input into a [*tokenizer*](#tokenizer). Designed to
14
- * Support a streaming environment
15
- * Decoding of binary data, strings and numbers in mind
16
- * Read [predefined](https://github.com/Borewit/token-types) or custom tokens.
17
- * Optimized [*tokenizers*](#tokenizer) for reading from [file](#method-strtok3fromfile), [stream](#method-strtok3fromstream) or [buffer](#method-strtok3frombuffer).
18
-
19
- It can read from:
20
- * A file (taking a file path as an input)
21
- * A Node.js [stream](https://nodejs.org/api/stream.html).
22
- * A [Buffer](https://nodejs.org/api/buffer.html) or [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)
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).
23
24
  * HTTP chunked transfer provided by [@tokenizer/http](https://github.com/Borewit/tokenizer-http).
24
- * Chunked [Amazon S3](https://aws.amazon.com/s3) access provided by [@tokenizer/s3](https://github.com/Borewit/tokenizer-s3).
25
+ * [Amazon S3](https://aws.amazon.com/s3) chunks with [@tokenizer/s3](https://github.com/Borewit/tokenizer-s3).
25
26
 
26
27
  ## Installation
27
28
 
@@ -31,32 +32,53 @@ npm install strtok3
31
32
 
32
33
  ### Compatibility
33
34
 
34
- Module: version 7 migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
35
- JavaScript is compliant with [ECMAScript 2020 (11th Edition)](https://en.wikipedia.org/wiki/ECMAScript_version_history#11th_Edition_%E2%80%93_ECMAScript_2020).
36
- Requires [Node.js ≥ 16](https://nodejs.org/en/about/previous-releases) engine, but can also be used with a module bundler in a browser.
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
37
 
38
- ## API
38
+ > [!NOTE]
39
+ > This module requires a [Node.js ≥ 16](https://nodejs.org/en/about/previous-releases) engine.
40
+ > It can also be used in a browser environment when bundled with a module bundler.
39
41
 
40
- Use one of the methods to instantiate an [*abstract tokenizer*](#tokenizer):
41
- * [strtok3.fromFile](#method-strtok3fromfile)
42
- * [strtok3.fromStream](#method-strtok3fromstream)
43
- * [strtok3.fromWeb](#method-strtok3fromwebstream)
44
- * [strtok3.fromBuffer](#method-strtok3fromBuffer)
45
- * [strtok3.fromUint8Array](#method-strtok3fromUint8Array)
42
+ ## Support the Project
43
+ If you find this project useful and would like to support its development, consider sponsoring or contributing:
44
+
45
+ - [Become a sponsor to Borewit](https://github.com/sponsors/Borewit)
46
+
47
+ - Buy me a coffee:
48
+
49
+ <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>
50
+
51
+ ## API Documentation
46
52
 
47
53
  ### strtok3 methods
48
54
 
49
- All of the strtok3 methods return a [*tokenizer*](#tokenizer), either directly or via a promise.
55
+ Use one of the methods to instantiate an [*abstract tokenizer*](#tokenizer-object):
56
+ - [fromFile](#fromfile-function)[^1]
57
+ - [fromStream](#fromstream-function)[^1]
58
+ - [fromWebStream](#fromwebstream-function)
59
+ - [fromBuffer](#frombuffer-function)
50
60
 
51
- #### Method `strtok3.fromFile()`
61
+ [^1]: Only available when importing this module with Node.js
52
62
 
53
- | Parameter | Type | Description |
54
- |-----------|-----------------------|----------------------------|
55
- | path | Path to file (string) | Path to file to read from |
63
+ All methods return a [`Tokenizer`](#tokenizer-object), either directly or via a promise.
56
64
 
57
- > __Note__: that [file-information](#file-information) is automatically added.
65
+ #### `fromFile` function
66
+
67
+ Creates a [*tokenizer*](#tokenizer-object) from a local file.
68
+
69
+ ```ts
70
+ function fromFile(sourceFilePath: string): Promise<FileTokenizer>
71
+ ```
58
72
 
59
- Returns, via a promise, a [*tokenizer*](#tokenizer) which can be used to parse a file.
73
+ | Parameter | Type | Description |
74
+ |----------------|----------|----------------------------|
75
+ | sourceFilePath | `string` | Path to file to read from |
76
+
77
+ > [!NOTE]
78
+ > - Only available for Node.js engines
79
+ > - `fromFile` automatically embeds [file-information](#file-information)
80
+
81
+ Returns, via a promise, a [*tokenizer*](#tokenizer-object) which can be used to parse a file.
60
82
 
61
83
  ```js
62
84
  import * as strtok3 from 'strtok3';
@@ -72,30 +94,40 @@ import * as Token from 'token-types';
72
94
  tokenizer.close(); // Close the file
73
95
  }
74
96
  })();
97
+ ```
75
98
 
99
+ #### `fromStream` function
100
+
101
+ Creates a [*tokenizer*](#tokenizer-object) from a Node.js [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable).
102
+
103
+ ```ts
104
+ function fromStream(stream: Readable, options?: ITokenizerOptions): Promise<ReadStreamTokenizer>
76
105
  ```
77
106
 
78
- #### Method `strtok3.fromStream()`
107
+ | Parameter | Optional | Type | Description |
108
+ |-----------|-----------|-------------------------|--------------------------|
109
+ | stream | no | [Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable) | Stream to read from |
110
+ | fileInfo | yes | [IFileInfo](#IFileInfo) | Provide file information |
79
111
 
80
- Create [*tokenizer*](#tokenizer) from a Node.js [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable).
112
+ Returns a Promise providing a [*tokenizer*](#tokenizer-object).
81
113
 
82
- | Parameter | Optional | Type | Description |
83
- |-----------|-----------|-----------------------------------------------------------------------------|--------------------------|
84
- | stream | no | [Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable) | Stream to read from |
85
- | fileInfo | yes | [IFileInfo](#IFileInfo) | Provide file information |
114
+ > [!NOTE]
115
+ > - Only available for Node.js engines
86
116
 
87
- Returns a Promise providing a [*tokenizer*](#tokenizer).
117
+ #### `fromWebStream` function
88
118
 
89
- #### Method `strtok3.fromWebStream()`
119
+ Creates [*tokenizer*](#tokenizer-object) from a [WHATWG ReadableStream](https://nodejs.org/api/webstreams.html#web-streams-api).
90
120
 
91
- Create [*tokenizer*](#tokenizer) from a [WHATWG ReadableStream](https://nodejs.org/api/webstreams.html#web-streams-api).
121
+ ```ts
122
+ function fromWebStream(webStream: AnyWebByteStream, options?: ITokenizerOptions): ReadStreamTokenizer
123
+ ```
92
124
 
93
125
  | Parameter | Optional | Type | Description |
94
126
  |----------------|-----------|--------------------------------------------------------------------------|------------------------------------|
95
127
  | readableStream | no | [ReadableStream](https://nodejs.org/api/webstreams.html#web-streams-api) | WHATWG ReadableStream to read from |
96
128
  | fileInfo | yes | [IFileInfo](#IFileInfo) | Provide file information |
97
129
 
98
- Returns a Promise providing a [*tokenizer*](#tokenizer)
130
+ Returns a Promise providing a [*tokenizer*](#tokenizer-object)
99
131
 
100
132
  ```js
101
133
  import strtok3 from 'strtok3';
@@ -108,14 +140,20 @@ strtok3.fromWebStream(readableStream).then(tokenizer => {
108
140
  });
109
141
  ```
110
142
 
111
- #### Method `strtok3.fromBuffer()`
143
+ #### `fromBuffer()` function
144
+
145
+ Create a tokenizer from memory ([Uint8Array](https://nodejs.org/api/buffer.html)).
146
+
147
+ ```ts
148
+ function fromBuffer(uint8Array: Uint8Array, options?: ITokenizerOptions): BufferTokenizer
149
+ ```
112
150
 
113
151
  | Parameter | Optional | Type | Description |
114
152
  |------------|----------|--------------------------------------------------|----------------------------------------|
115
153
  | uint8Array | no | [Uint8Array](https://nodejs.org/api/buffer.html) | Uint8Array or Buffer to read from |
116
154
  | fileInfo | yes | [IFileInfo](#IFileInfo) | Provide file information |
117
155
 
118
- Returns a Promise providing a [*tokenizer*](#tokenizer).
156
+ Returns a Promise providing a [*tokenizer*](#tokenizer-object).
119
157
 
120
158
  ```js
121
159
  import * as strtok3 from 'strtok3';
@@ -127,47 +165,48 @@ tokenizer.readToken(Token.UINT8).then(myUint8Number => {
127
165
  });
128
166
  ```
129
167
 
130
- ## Tokenizer
131
- The tokenizer allows us to *read* or *peek* from the *tokenizer-stream*. The *tokenizer-stream* 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).
168
+ ### `Tokenizer` object
169
+ 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.
132
170
  It can also be translated in chunked reads, as done in [@tokenizer/http](https://github.com/Borewit/tokenizer-http);
133
171
 
134
- What is the difference with Nodejs.js stream?
135
- * The *tokenizer-stream* supports jumping / seeking in a the *tokenizer-stream* using [`tokenizer.ignore()`](#method-tokenizerignore)
136
- * In addition to *read* methods, it has *peek* methods, to read a ahead and check what is coming.
172
+ #### Key Features:
137
173
 
138
- The [tokenizer.position](#attribute-tokenizerposition) keeps tracks of the read position.
174
+ - Supports seeking within the stream using `tokenizer.ignore()`.
175
+ - Offers `peek` methods to preview data without advancing the read pointer.
176
+ - Maintains the read position via tokenizer.position.
139
177
 
140
- ### strtok3 attributes
178
+ #### Tokenizer functions
141
179
 
142
- #### Attribute `tokenizer.fileInfo`
143
- Optional attribute describing the file information, see [IFileInfo](#IFileInfo)
180
+ _Read_ methods advance the stream pointer, while _peek_ methods do not.
144
181
 
145
- #### Attribute `tokenizer.position`
146
- Pointer to the current position in the [*tokenizer*](#tokenizer) stream.
147
- If a *position* is provided to a *read* or *peek* method, is should be, at least, equal or greater than this value.
148
-
149
- ### Tokenizer methods
150
-
151
- There are two kind of methods:
152
- 1. *read* methods: used to read a *token* of [Buffer](https://nodejs.org/api/buffer.html) from the [*tokenizer*](#tokenizer). The position of the *tokenizer-stream* will advance with the size of the token.
182
+ There are two kind of functions:
183
+ 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.
153
184
  2. *peek* methods: same as the read, but it will *not* advance the pointer. It allows to read (peek) ahead.
154
185
 
155
- #### Method `tokenizer.readBuffer()`
186
+ #### `readBuffer` function
156
187
 
157
- Read buffer from stream.
188
+ Read data from the _tokenizer_ into provided "buffer" (`Uint8Array`).
158
189
  `readBuffer(buffer, options?)`
159
190
 
191
+ ```ts
192
+ readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
193
+ ```
194
+
160
195
  | Parameter | Type | Description |
161
196
  |------------|----------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
162
197
  | buffer | [Buffer](https://nodejs.org/api/buffer.html) &#124; Uint8Array | Target buffer to write the data read to |
163
198
  | options | [IReadChunkOptions](#ireadchunkoptions) | An integer specifying the number of bytes to read |
164
199
 
165
- Return value `Promise<number>` Promise with number of bytes read. The number of bytes read maybe if less, *mayBeLess* flag was set.
200
+ Return promise with number of bytes read.
201
+ The number of bytes read maybe if less, *mayBeLess* flag was set.
166
202
 
167
- #### Method `tokenizer.peekBuffer()`
203
+ #### `peekBuffer` function
168
204
 
169
- Peek (read ahead) buffer from [*tokenizer*](#tokenizer)
170
- `peekBuffer(buffer, options?)`
205
+ Peek (read ahead), from [*tokenizer*](#tokenizer-object), into the buffer without advancing the stream pointer.
206
+
207
+ ```ts
208
+ peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
209
+ ```
171
210
 
172
211
  | Parameter | Type | Description |
173
212
  |------------|-----------------------------------------|-----------------------------------------------------|
@@ -176,10 +215,13 @@ Peek (read ahead) buffer from [*tokenizer*](#tokenizer)
176
215
 
177
216
  Return value `Promise<number>` Promise with number of bytes read. The number of bytes read maybe if less, *mayBeLess* flag was set.
178
217
 
179
- #### Method `tokenizer.readToken()`
218
+ #### `readToken` function
180
219
 
181
220
  Read a *token* from the tokenizer-stream.
182
- `readToken(token, position?)`
221
+
222
+ ```ts
223
+ readToken<Value>(token: IGetToken<Value>, position: number = this.position): Promise<Value>
224
+ ```
183
225
 
184
226
  | Parameter | Type | Description |
185
227
  |------------|-------------------------|---------------------------------------------------------------------------------------------------------------------- |
@@ -188,44 +230,64 @@ Read a *token* from the tokenizer-stream.
188
230
 
189
231
  Return value `Promise<number>`. Promise with number of bytes read. The number of bytes read maybe if less, *mayBeLess* flag was set.
190
232
 
191
- #### Method `tokenizer.peekToken()`
233
+ #### `peek` function
234
+
235
+ Peek a *token* from the [*tokenizer*](#tokenizer-object).
192
236
 
193
- Peek a *token* from the [*tokenizer*](#tokenizer).
194
- `peekToken(token, position?)`
237
+ ```ts
238
+ peekToken<Value>(token: IGetToken<Value>, position: number = this.position): Promise<Value>
239
+ ```
195
240
 
196
241
  | Parameter | Type | Description |
197
242
  |------------|----------------------------|-------------------------------------------------------------------------------------------------------------------------|
198
243
  | token | [IGetToken<T>](#IGetToken) | Token to read from the tokenizer-stream. |
199
244
  | position? | number | Offset where to begin reading within the file. If position is null, data will be read from the current file position. |
200
245
 
201
- Return value `Promise<T>` Promise with token value peeked from the [*tokenizer*](#tokenizer).
246
+ Return a promise with the token value peeked from the [*tokenizer*](#tokenizer-object).
247
+
248
+ #### `readNumber` function
202
249
 
203
- #### Method `tokenizer.readNumber()`
250
+ Peek a numeric [*token*](#token) from the [*tokenizer*](#tokenizer-object).
204
251
 
205
- Peek a numeric [*token*](#token) from the [*tokenizer*](#tokenizer).
206
- `readNumber(token)`
252
+ ```ts
253
+ readNumber(token: IToken<number>): Promise<number>
254
+ ```
207
255
 
208
256
  | Parameter | Type | Description |
209
257
  |------------|---------------------------------|----------------------------------------------------|
210
258
  | token | [IGetToken<number>](#IGetToken) | Numeric token to read from the tokenizer-stream. |
211
259
 
212
- Return value `Promise<number>` Promise with number peeked from the *tokenizer-stream*.
260
+ Returns a promise with the decoded numeric value from the *tokenizer-stream*.
261
+
262
+ #### `ignore` function
213
263
 
214
- #### Method `tokenizer.ignore()`
264
+ Advance the offset pointer with the token number of bytes provided.
215
265
 
216
- Advance the offset pointer with the number of bytes provided.
217
- `ignore(length)`
266
+ ```ts
267
+ ignore(length: number): Promise<number>
268
+ ```
218
269
 
219
270
  | Parameter | Type | Description |
220
271
  |------------|--------|----------------------------------------------------------------------|
221
272
  | ignore | number | Numeric of bytes to ignore. Will advance the `tokenizer.position` |
222
273
 
223
- Return value `Promise<number>` Promise with number peeked from the *tokenizer-stream*.
274
+ Returns a promise with the decoded numeric value from the *tokenizer-stream*.
224
275
 
225
- #### Method `tokenizer.close()`
276
+ #### `close` function
226
277
  Clean up resources, such as closing a file pointer if applicable.
227
278
 
228
- ### IReadChunkOptions
279
+ #### `Tokenizer` attributes
280
+
281
+ - `fileInfo`
282
+
283
+ Optional attribute describing the file information, see [IFileInfo](#IFileInfo)
284
+
285
+ - `position`
286
+
287
+ Pointer to the current position in the [*tokenizer*](#tokenizer-object) stream.
288
+ If a *position* is provided to a _read_ or _peek_ method, is should be, at least, equal or greater than this value.
289
+
290
+ ### `IReadChunkOptions` interface
229
291
 
230
292
  Each attribute is optional:
231
293
 
@@ -236,14 +298,14 @@ Each attribute is optional:
236
298
  | 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) |
237
299
  | mayBeLess | boolean | If and only if set, will not throw an EOF error if less then the requested *mayBeLess* could be read. |
238
300
 
239
- Example:
301
+ Example usage:
240
302
  ```js
241
303
  tokenizer.peekBuffer(buffer, {mayBeLess: true});
242
304
  ```
243
305
 
244
- ## IFileInfo
306
+ ### `IFileInfo` interface
245
307
 
246
- File information interface which describes the underlying file, each attribute is optional.
308
+ Provides optional metadata about the file being tokenized.
247
309
 
248
310
  | Attribute | Type | Description |
249
311
  |-----------|---------|---------------------------------------------------------------------------------------------------|
@@ -252,9 +314,9 @@ File information interface which describes the underlying file, each attribute i
252
314
  | path | number | File path |
253
315
  | url | boolean | File URL |
254
316
 
255
- ## Token
317
+ ### `Token` object
256
318
 
257
- The *token* is basically a description what to read form the [*tokenizer-stream*](#tokenizer).
319
+ The *token* is basically a description what to read form the [*tokenizer-stream*](#tokenizer-object).
258
320
  A basic set of *token types* can be found here: [*token-types*](https://github.com/Borewit/token-types).
259
321
 
260
322
  A token is something which implements the following interface:
@@ -277,38 +339,40 @@ export interface IGetToken<T> {
277
339
  The *tokenizer* reads `token.len` bytes from the *tokenizer-stream* into a Buffer.
278
340
  The `token.get` will be called with the Buffer. `token.get` is responsible for conversion from the buffer to the desired output type.
279
341
 
280
- ## Browser compatibility
281
- To exclude fs based dependencies, you can use a submodule-import from 'strtok3/lib/core'.
282
-
283
- | function | 'strtok3' | 'strtok3/lib/core' |
284
- | ----------------------| --------------------|---------------------|
285
- | `parseBuffer` | ✓ | ✓ |
286
- | `parseStream` | ✓ | ✓ |
287
- | `fromFile` | ✓ | |
288
-
289
342
  ### Working with Web-API readable stream
290
343
  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.
291
344
 
292
- Example submodule-import:
293
345
  ```js
294
- import * as strtok3core from 'strtok3/core'; // Submodule-import to prevent Node.js specific dependencies
346
+ import { fromWebStream } strtok3 from 'strtok3';
295
347
  import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';
296
348
 
297
349
  (async () => {
298
350
 
299
351
  const response = await fetch(url);
300
352
  const readableWebStream = response.body; // Web-API readable stream
301
- const nodeStream = new ReadableWebToNodeStream(readableWebStream); // convert to Node.js readable stream
353
+ const webStream = new ReadableWebToNodeStream(readableWebStream); // convert to Node.js readable stream
302
354
 
303
- const tokenizer = strtok3core.fromStream(nodeStream); // And we now have tokenizer in a web environment
355
+ const tokenizer = fromWebStream(webStream); // And we now have tokenizer in a web environment
304
356
  })();
305
357
  ```
306
358
 
359
+ ## Dependencies
360
+
361
+ The diagram below illustrates the primary dependencies of `strtok3`:
362
+ ```mermaid
363
+ graph TD;
364
+ S(strtok3)-->P(peek-readable)
365
+ S(strtok3)-->TO("@tokenizer/token")
366
+ ```
367
+ -
368
+ - [peek-readable](https://github.com/Borewit/peek-readable): Manages reading operations with peeking capabilities, allowing data to be previewed without advancing the read pointer.
369
+ - [@tokenizer/token](https://github.com/Borewit/tokenizer-token): Provides token definitions and utilities used by `strtok3` for interpreting binary data.
370
+
307
371
  ## Licence
308
372
 
309
373
  (The MIT License)
310
374
 
311
- Copyright (c) 2020 Borewit
375
+ Copyright (c) 2024 Borewit
312
376
 
313
377
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
314
378
 
@@ -71,5 +71,6 @@ export declare abstract class AbstractTokenizer implements ITokenizer {
71
71
  ignore(length: number): Promise<number>;
72
72
  close(): Promise<void>;
73
73
  protected normalizeOptions(uint8Array: Uint8Array, options?: IReadChunkOptions): INormalizedReadChunkOptions;
74
+ abort(): Promise<void>;
74
75
  }
75
76
  export {};
@@ -16,6 +16,11 @@ export class AbstractTokenizer {
16
16
  this.numBuffer = new Uint8Array(8);
17
17
  this.fileInfo = options?.fileInfo ?? {};
18
18
  this.onClose = options?.onClose;
19
+ if (options?.abortSignal) {
20
+ options.abortSignal.addEventListener('abort', () => {
21
+ this.abort();
22
+ });
23
+ }
19
24
  }
20
25
  /**
21
26
  * Read a token from the tokenizer-stream
@@ -103,4 +108,7 @@ export class AbstractTokenizer {
103
108
  position: this.position
104
109
  };
105
110
  }
111
+ abort() {
112
+ return Promise.resolve(); // Ignore abort signal
113
+ }
106
114
  }
@@ -24,4 +24,5 @@ export declare class ReadStreamTokenizer extends AbstractTokenizer {
24
24
  */
25
25
  peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
26
26
  ignore(length: number): Promise<number>;
27
+ abort(): Promise<void>;
27
28
  }
@@ -89,4 +89,7 @@ export class ReadStreamTokenizer extends AbstractTokenizer {
89
89
  }
90
90
  return totBytesRead;
91
91
  }
92
+ abort() {
93
+ return this.streamReader.abort();
94
+ }
92
95
  }
package/lib/types.d.ts CHANGED
@@ -99,6 +99,10 @@ export interface ITokenizer {
99
99
  * It does not close the stream for StreamReader, but is does close the file-descriptor.
100
100
  */
101
101
  close(): Promise<void>;
102
+ /**
103
+ * Abort pending asynchronous operations
104
+ */
105
+ abort(): Promise<void>;
102
106
  }
103
107
  export type OnClose = () => Promise<void>;
104
108
  export interface ITokenizerOptions {
@@ -110,4 +114,9 @@ export interface ITokenizerOptions {
110
114
  * On tokenizer close handler
111
115
  */
112
116
  onClose?: OnClose;
117
+ /**
118
+ * Pass `AbortSignal` which can stop active async operations
119
+ * Ref: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
120
+ */
121
+ abortSignal?: AbortSignal;
113
122
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strtok3",
3
- "version": "8.1.0",
3
+ "version": "9.0.0",
4
4
  "description": "A promise based streaming tokenizer",
5
5
  "author": {
6
6
  "name": "Borewit",
@@ -53,11 +53,13 @@
53
53
  "devDependencies": {
54
54
  "@biomejs/biome": "^1.8.3",
55
55
  "@types/chai": "^4.3.17",
56
+ "@types/chai-as-promised": "^8.0.0",
56
57
  "@types/debug": "^4.1.12",
57
58
  "@types/mocha": "^10.0.7",
58
59
  "@types/node": "^22.2.0",
59
60
  "c8": "^10.1.2",
60
61
  "chai": "^5.1.1",
62
+ "chai-as-promised": "^8.0.0",
61
63
  "del-cli": "^5.1.0",
62
64
  "mocha": "^10.7.3",
63
65
  "remark-cli": "^12.0.1",
@@ -69,7 +71,7 @@
69
71
  },
70
72
  "dependencies": {
71
73
  "@tokenizer/token": "^0.3.0",
72
- "peek-readable": "^5.1.4"
74
+ "peek-readable": "^5.2.0"
73
75
  },
74
76
  "keywords": [
75
77
  "tokenizer",