strtok3 7.0.0 → 7.1.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/LICENSE +14 -14
- package/README.md +316 -306
- package/lib/AbstractTokenizer.js +2 -3
- package/lib/FsPromise.d.ts +3 -5
- package/lib/FsPromise.js +2 -2
- package/lib/ReadStreamTokenizer.d.ts +2 -3
- package/lib/ReadStreamTokenizer.js +3 -3
- package/lib/core.d.ts +9 -1
- package/lib/core.js +13 -1
- package/lib/index.d.ts +1 -2
- package/lib/index.js +1 -1
- package/lib/types.d.ts +2 -3
- package/package.json +95 -94
package/LICENSE
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
Copyright (c) 2017, Borewit
|
|
2
|
-
All rights reserved.
|
|
3
|
-
|
|
4
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
5
|
-
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
6
|
-
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
7
|
-
persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
8
|
-
|
|
9
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
10
|
-
Software.
|
|
11
|
-
|
|
12
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
13
|
-
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
14
|
-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
1
|
+
Copyright (c) 2017, Borewit
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
5
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
7
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
8
|
+
|
|
9
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
10
|
+
Software.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
13
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
14
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
15
15
|
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,306 +1,316 @@
|
|
|
1
|
-

|
|
2
|
-
[](https://npmjs.org/package/strtok3)
|
|
3
|
-
[](https://npmcharts.com/compare/strtok3,token-types?start=1200&interval=30)
|
|
4
|
-
[](https://deepscan.io/dashboard#view=project&tid=5165&pid=8526&bid=103329)
|
|
5
|
-
[](https://snyk.io/test/github/Borewit/strtok3?targetFile=package.json)
|
|
6
|
-
[
|
|
2
|
+
[](https://npmjs.org/package/strtok3)
|
|
3
|
+
[](https://npmcharts.com/compare/strtok3,token-types?start=1200&interval=30)
|
|
4
|
+
[](https://deepscan.io/dashboard#view=project&tid=5165&pid=8526&bid=103329)
|
|
5
|
+
[](https://snyk.io/test/github/Borewit/strtok3?targetFile=package.json)
|
|
6
|
+
[](https://www.codacy.com/app/Borewit/strtok3?utm_source=github.com&utm_medium=referral&utm_content=Borewit/strtok3&utm_campaign=Badge_Grade)
|
|
7
|
+
# strtok3
|
|
8
|
+
|
|
9
|
+
A promise based streaming [*tokenizer*](#tokenizer) for [Node.js](http://nodejs.org) and browsers.
|
|
10
|
+
This node module is a successor of [strtok2](https://github.com/Borewit/strtok2).
|
|
11
|
+
|
|
12
|
+
The `strtok3` contains a few methods to turn different input into a [*tokenizer*](#tokenizer). Designed to
|
|
13
|
+
* Support a streaming environment
|
|
14
|
+
* Decoding of binary data, strings and numbers in mind
|
|
15
|
+
* Read [predefined](https://github.com/Borewit/token-types) or custom tokens.
|
|
16
|
+
* Optimized [*tokenizers*](#tokenizer) for reading from [file](#method-strtok3fromfile), [stream](#method-strtok3fromstream) or [buffer](#method-strtok3frombuffer).
|
|
17
|
+
|
|
18
|
+
It can read from:
|
|
19
|
+
* A file (taking a file path as an input)
|
|
20
|
+
* A Node.js [stream](https://nodejs.org/api/stream.html).
|
|
21
|
+
* A [Buffer](https://nodejs.org/api/buffer.html) or [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)
|
|
22
|
+
* HTTP chunked transfer provided by [@tokenizer/http](https://github.com/Borewit/tokenizer-http).
|
|
23
|
+
* Chunked [Amazon S3](https://aws.amazon.com/s3) access provided by [@tokenizer/s3](https://github.com/Borewit/tokenizer-s3).
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm install strtok3
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Compatibility
|
|
32
|
+
|
|
33
|
+
Module: version 7 migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
|
|
34
|
+
JavaScript is compliant with [ECMAScript 2019 (ES10)](https://en.wikipedia.org/wiki/ECMAScript#10th_Edition_%E2%80%93_ECMAScript_2019).
|
|
35
|
+
Requires Node.js ≥ 14.16 engine.
|
|
36
|
+
|
|
37
|
+
## API
|
|
38
|
+
|
|
39
|
+
Use one of the methods to instantiate an [*abstract tokenizer*](#tokenizer):
|
|
40
|
+
* [strtok3.fromFile](#method-strtok3fromfile)
|
|
41
|
+
* [strtok3.fromStream](#method-strtok3fromstream)
|
|
42
|
+
* [strtok3.fromWeb](#method-strtok3fromwebstream)
|
|
43
|
+
* [strtok3.fromBuffer](#method-strtok3fromBuffer)
|
|
44
|
+
* [strtok3.fromUint8Array](#method-strtok3fromUint8Array)
|
|
45
|
+
|
|
46
|
+
### strtok3 methods
|
|
47
|
+
|
|
48
|
+
All of the strtok3 methods return a [*tokenizer*](#tokenizer), either directly or via a promise.
|
|
49
|
+
|
|
50
|
+
#### Method `strtok3.fromFile()`
|
|
51
|
+
|
|
52
|
+
| Parameter | Type | Description |
|
|
53
|
+
|-----------|-----------------------|----------------------------|
|
|
54
|
+
| path | Path to file (string) | Path to file to read from |
|
|
55
|
+
|
|
56
|
+
> __Note__: that [file-information](#file-information) is automatically added.
|
|
57
|
+
|
|
58
|
+
Returns, via a promise, a [*tokenizer*](#tokenizer) which can be used to parse a file.
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
import * as strtok3 from 'strtok3';
|
|
62
|
+
import * as Token from 'token-types';
|
|
63
|
+
|
|
64
|
+
(async () => {
|
|
65
|
+
|
|
66
|
+
const tokenizer = await strtok3.fromFile("somefile.bin");
|
|
67
|
+
try {
|
|
68
|
+
const myNumber = await tokenizer.readToken(Token.UINT8);
|
|
69
|
+
console.log(`My number: ${myNumber}`);
|
|
70
|
+
} finally {
|
|
71
|
+
tokenizer.close(); // Close the file
|
|
72
|
+
}
|
|
73
|
+
})();
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
#### Method `strtok3.fromStream()`
|
|
78
|
+
|
|
79
|
+
Create [*tokenizer*](#tokenizer) from a Node.js [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable).
|
|
80
|
+
|
|
81
|
+
| Parameter | Optional | Type | Description |
|
|
82
|
+
|-----------|-----------|-----------------------------------------------------------------------------|--------------------------|
|
|
83
|
+
| stream | no | [Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable) | Stream to read from |
|
|
84
|
+
| fileInfo | yes | [IFileInfo](#IFileInfo) | Provide file information |
|
|
85
|
+
|
|
86
|
+
Returns a Promise providing a [*tokenizer*](#tokenizer).
|
|
87
|
+
|
|
88
|
+
#### Method `strtok3.fromWebStream()`
|
|
89
|
+
|
|
90
|
+
Create [*tokenizer*](#tokenizer) from a [WHATWG ReadableStream](https://nodejs.org/api/webstreams.html#web-streams-api).
|
|
91
|
+
|
|
92
|
+
| Parameter | Optional | Type | Description |
|
|
93
|
+
|----------------|-----------|--------------------------------------------------------------------------|------------------------------------|
|
|
94
|
+
| readableStream | no | [ReadableStream](https://nodejs.org/api/webstreams.html#web-streams-api) | WHATWG ReadableStream to read from |
|
|
95
|
+
| fileInfo | yes | [IFileInfo](#IFileInfo) | Provide file information |
|
|
96
|
+
|
|
97
|
+
Returns a Promise providing a [*tokenizer*](#tokenizer)
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
import strtok3 from 'strtok3';
|
|
101
|
+
import * as Token from 'token-types';
|
|
102
|
+
|
|
103
|
+
strtok3.fromWebStream(readableStream).then(tokenizer => {
|
|
104
|
+
return tokenizer.readToken(Token.UINT8).then(myUint8Number => {
|
|
105
|
+
console.log(`My number: ${myUint8Number}`);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### Method `strtok3.fromBuffer()`
|
|
111
|
+
|
|
112
|
+
| Parameter | Optional | Type | Description |
|
|
113
|
+
|------------|----------|--------------------------------------------------|----------------------------------------|
|
|
114
|
+
| uint8Array | no | [Uint8Array](https://nodejs.org/api/buffer.html) | Uint8Array or Buffer to read from |
|
|
115
|
+
| fileInfo | yes | [IFileInfo](#IFileInfo) | Provide file information |
|
|
116
|
+
|
|
117
|
+
Returns a Promise providing a [*tokenizer*](#tokenizer).
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
import * as strtok3 from 'strtok3';
|
|
121
|
+
|
|
122
|
+
const tokenizer = strtok3.fromBuffer(buffer);
|
|
123
|
+
|
|
124
|
+
tokenizer.readToken(Token.UINT8).then(myUint8Number => {
|
|
125
|
+
console.log(`My number: ${myUint8Number}`);
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Tokenizer
|
|
130
|
+
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).
|
|
131
|
+
It can also be translated in chunked reads, as done in [@tokenizer/http](https://github.com/Borewit/tokenizer-http);
|
|
132
|
+
|
|
133
|
+
What is the difference with Nodejs.js stream?
|
|
134
|
+
* The *tokenizer-stream* supports jumping / seeking in a the *tokenizer-stream* using [`tokenizer.ignore()`](#method-tokenizerignore)
|
|
135
|
+
* In addition to *read* methods, it has *peek* methods, to read a ahead and check what is coming.
|
|
136
|
+
|
|
137
|
+
The [tokenizer.position](#attribute-tokenizerposition) keeps tracks of the read position.
|
|
138
|
+
|
|
139
|
+
### strtok3 attributes
|
|
140
|
+
|
|
141
|
+
#### Attribute `tokenizer.fileInfo`
|
|
142
|
+
Optional attribute describing the file information, see [IFileInfo](#IFileInfo)
|
|
143
|
+
|
|
144
|
+
#### Attribute `tokenizer.position`
|
|
145
|
+
Pointer to the current position in the [*tokenizer*](#tokenizer) stream.
|
|
146
|
+
If a *position* is provided to a *read* or *peek* method, is should be, at least, equal or greater than this value.
|
|
147
|
+
|
|
148
|
+
### Tokenizer methods
|
|
149
|
+
|
|
150
|
+
There are two kind of methods:
|
|
151
|
+
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.
|
|
152
|
+
2. *peek* methods: same as the read, but it will *not* advance the pointer. It allows to read (peek) ahead.
|
|
153
|
+
|
|
154
|
+
#### Method `tokenizer.readBuffer()`
|
|
155
|
+
|
|
156
|
+
Read buffer from stream.
|
|
157
|
+
`readBuffer(buffer, options?)`
|
|
158
|
+
|
|
159
|
+
| Parameter | Type | Description |
|
|
160
|
+
|------------|----------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
161
|
+
| buffer | [Buffer](https://nodejs.org/api/buffer.html) | Uint8Array | Target buffer to write the data read to |
|
|
162
|
+
| options | [IReadChunkOptions](#ireadchunkoptions) | An integer specifying the number of bytes to read |
|
|
163
|
+
|
|
164
|
+
Return value `Promise<number>` Promise with number of bytes read. The number of bytes read maybe if less, *mayBeLess* flag was set.
|
|
165
|
+
|
|
166
|
+
#### Method `tokenizer.peekBuffer()`
|
|
167
|
+
|
|
168
|
+
Peek (read ahead) buffer from [*tokenizer*](#tokenizer)
|
|
169
|
+
`peekBuffer(buffer, options?)`
|
|
170
|
+
|
|
171
|
+
| Parameter | Type | Description |
|
|
172
|
+
|------------|-----------------------------------------|-----------------------------------------------------|
|
|
173
|
+
| buffer | Buffer | Uint8Array | Target buffer to write the data read (peeked) to. |
|
|
174
|
+
| options | [IReadChunkOptions](#ireadchunkoptions) | An integer specifying the number of bytes to read. | |
|
|
175
|
+
|
|
176
|
+
Return value `Promise<number>` Promise with number of bytes read. The number of bytes read maybe if less, *mayBeLess* flag was set.
|
|
177
|
+
|
|
178
|
+
#### Method `tokenizer.readToken()`
|
|
179
|
+
|
|
180
|
+
Read a *token* from the tokenizer-stream.
|
|
181
|
+
`readToken(token, position?)`
|
|
182
|
+
|
|
183
|
+
| Parameter | Type | Description |
|
|
184
|
+
|------------|-------------------------|---------------------------------------------------------------------------------------------------------------------- |
|
|
185
|
+
| token | [IGetToken](#IGetToken) | Token to read from the tokenizer-stream. |
|
|
186
|
+
| position? | number | Offset where to begin reading within the file. If position is null, data will be read from the current file position. |
|
|
187
|
+
|
|
188
|
+
Return value `Promise<number>`. Promise with number of bytes read. The number of bytes read maybe if less, *mayBeLess* flag was set.
|
|
189
|
+
|
|
190
|
+
#### Method `tokenizer.peekToken()`
|
|
191
|
+
|
|
192
|
+
Peek a *token* from the [*tokenizer*](#tokenizer).
|
|
193
|
+
`peekToken(token, position?)`
|
|
194
|
+
|
|
195
|
+
| Parameter | Type | Description |
|
|
196
|
+
|------------|----------------------------|-------------------------------------------------------------------------------------------------------------------------|
|
|
197
|
+
| token | [IGetToken<T>](#IGetToken) | Token to read from the tokenizer-stream. |
|
|
198
|
+
| position? | number | Offset where to begin reading within the file. If position is null, data will be read from the current file position. |
|
|
199
|
+
|
|
200
|
+
Return value `Promise<T>` Promise with token value peeked from the [*tokenizer*](#tokenizer).
|
|
201
|
+
|
|
202
|
+
#### Method `tokenizer.readNumber()`
|
|
203
|
+
|
|
204
|
+
Peek a numeric [*token*](#token) from the [*tokenizer*](#tokenizer).
|
|
205
|
+
`readNumber(token)`
|
|
206
|
+
|
|
207
|
+
| Parameter | Type | Description |
|
|
208
|
+
|------------|---------------------------------|----------------------------------------------------|
|
|
209
|
+
| token | [IGetToken<number>](#IGetToken) | Numeric token to read from the tokenizer-stream. |
|
|
210
|
+
|
|
211
|
+
Return value `Promise<number>` Promise with number peeked from the *tokenizer-stream*.
|
|
212
|
+
|
|
213
|
+
#### Method `tokenizer.ignore()`
|
|
214
|
+
|
|
215
|
+
Advance the offset pointer with the number of bytes provided.
|
|
216
|
+
`ignore(length)`
|
|
217
|
+
|
|
218
|
+
| Parameter | Type | Description |
|
|
219
|
+
|------------|--------|----------------------------------------------------------------------|
|
|
220
|
+
| ignore | number | Numeric of bytes to ignore. Will advance the `tokenizer.position` |
|
|
221
|
+
|
|
222
|
+
Return value `Promise<number>` Promise with number peeked from the *tokenizer-stream*.
|
|
223
|
+
|
|
224
|
+
#### Method `tokenizer.close()`
|
|
225
|
+
Clean up resources, such as closing a file pointer if applicable.
|
|
226
|
+
|
|
227
|
+
### IReadChunkOptions
|
|
228
|
+
|
|
229
|
+
Each attribute is optional:
|
|
230
|
+
|
|
231
|
+
| Attribute | Type | Description |
|
|
232
|
+
|-----------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
233
|
+
| offset | number | The offset in the buffer to start writing at; if not provided, start at 0 |
|
|
234
|
+
| length | number | Requested number of bytes to read. |
|
|
235
|
+
| 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) |
|
|
236
|
+
| mayBeLess | boolean | If and only if set, will not throw an EOF error if less then the requested *mayBeLess* could be read. |
|
|
237
|
+
|
|
238
|
+
Example:
|
|
239
|
+
```js
|
|
240
|
+
tokenizer.peekBuffer(buffer, {mayBeLess: true});
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## IFileInfo
|
|
244
|
+
|
|
245
|
+
File information interface which describes the underlying file, each attribute is optional.
|
|
246
|
+
|
|
247
|
+
| Attribute | Type | Description |
|
|
248
|
+
|-----------|---------|---------------------------------------------------------------------------------------------------|
|
|
249
|
+
| size | number | File size in bytes |
|
|
250
|
+
| mimeType | number | [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) of file. |
|
|
251
|
+
| path | number | File path |
|
|
252
|
+
| url | boolean | File URL |
|
|
253
|
+
|
|
254
|
+
## Token
|
|
255
|
+
|
|
256
|
+
The *token* is basically a description what to read form the [*tokenizer-stream*](#tokenizer).
|
|
257
|
+
A basic set of *token types* can be found here: [*token-types*](https://github.com/Borewit/token-types).
|
|
258
|
+
|
|
259
|
+
A token is something which implements the following interface:
|
|
260
|
+
```ts
|
|
261
|
+
export interface IGetToken<T> {
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Length in bytes of encoded value
|
|
265
|
+
*/
|
|
266
|
+
len: number;
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Decode value from buffer at offset
|
|
270
|
+
* @param buf Buffer to read the decoded value from
|
|
271
|
+
* @param off Decode offset
|
|
272
|
+
*/
|
|
273
|
+
get(buf: Uint8Array, off: number): T;
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
The *tokenizer* reads `token.len` bytes from the *tokenizer-stream* into a Buffer.
|
|
277
|
+
The `token.get` will be called with the Buffer. `token.get` is responsible for conversion from the buffer to the desired output type.
|
|
278
|
+
|
|
279
|
+
## Browser compatibility
|
|
280
|
+
To exclude fs based dependencies, you can use a submodule-import from 'strtok3/lib/core'.
|
|
281
|
+
|
|
282
|
+
| function | 'strtok3' | 'strtok3/lib/core' |
|
|
283
|
+
| ----------------------| --------------------|---------------------|
|
|
284
|
+
| `parseBuffer` | ✓ | ✓ |
|
|
285
|
+
| `parseStream` | ✓ | ✓ |
|
|
286
|
+
| `fromFile` | ✓ | |
|
|
287
|
+
|
|
288
|
+
### Working with Web-API readable stream
|
|
289
|
+
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.
|
|
290
|
+
|
|
291
|
+
Example submodule-import:
|
|
292
|
+
```js
|
|
293
|
+
import * as strtok3core from 'strtok3/core'; // Submodule-import to prevent Node.js specific dependencies
|
|
294
|
+
import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';
|
|
295
|
+
|
|
296
|
+
(async () => {
|
|
297
|
+
|
|
298
|
+
const response = await fetch(url);
|
|
299
|
+
const readableWebStream = response.body; // Web-API readable stream
|
|
300
|
+
const nodeStream = new ReadableWebToNodeStream(readableWebStream); // convert to Node.js readable stream
|
|
301
|
+
|
|
302
|
+
const tokenizer = strtok3core.fromStream(nodeStream); // And we now have tokenizer in a web environment
|
|
303
|
+
})();
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## Licence
|
|
307
|
+
|
|
308
|
+
(The MIT License)
|
|
309
|
+
|
|
310
|
+
Copyright (c) 2020 Borewit
|
|
311
|
+
|
|
312
|
+
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:
|
|
313
|
+
|
|
314
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
315
|
+
|
|
316
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/lib/AbstractTokenizer.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { EndOfStreamError } from 'peek-readable';
|
|
2
|
-
import { Buffer } from 'node:buffer';
|
|
3
2
|
/**
|
|
4
3
|
* Core tokenizer
|
|
5
4
|
*/
|
|
@@ -19,7 +18,7 @@ export class AbstractTokenizer {
|
|
|
19
18
|
* @returns Promise with token data
|
|
20
19
|
*/
|
|
21
20
|
async readToken(token, position = this.position) {
|
|
22
|
-
const uint8Array =
|
|
21
|
+
const uint8Array = new Uint8Array(token.len);
|
|
23
22
|
const len = await this.readBuffer(uint8Array, { position });
|
|
24
23
|
if (len < token.len)
|
|
25
24
|
throw new EndOfStreamError();
|
|
@@ -32,7 +31,7 @@ export class AbstractTokenizer {
|
|
|
32
31
|
* @returns Promise with token data
|
|
33
32
|
*/
|
|
34
33
|
async peekToken(token, position = this.position) {
|
|
35
|
-
const uint8Array =
|
|
34
|
+
const uint8Array = new Uint8Array(token.len);
|
|
36
35
|
const len = await this.peekBuffer(uint8Array, { position });
|
|
37
36
|
if (len < token.len)
|
|
38
37
|
throw new EndOfStreamError();
|
package/lib/FsPromise.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Module convert fs functions to promise based functions
|
|
3
3
|
*/
|
|
4
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
5
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
6
4
|
import fs from 'node:fs';
|
|
7
5
|
export interface IReadResult {
|
|
8
6
|
bytesRead: number;
|
|
@@ -14,6 +12,6 @@ export declare function stat(path: fs.PathLike): Promise<fs.Stats>;
|
|
|
14
12
|
export declare function close(fd: number): Promise<void>;
|
|
15
13
|
export declare function open(path: fs.PathLike, mode: fs.Mode): Promise<number>;
|
|
16
14
|
export declare function read(fd: number, buffer: Uint8Array, offset: number, length: number, position: number): Promise<IReadResult>;
|
|
17
|
-
export declare function writeFile(path: fs.PathLike, data:
|
|
18
|
-
export declare function writeFileSync(path: fs.PathLike, data:
|
|
19
|
-
export declare function readFile(path: fs.PathLike): Promise<
|
|
15
|
+
export declare function writeFile(path: fs.PathLike, data: Uint8Array | string, options?: fs.WriteFileOptions): Promise<void>;
|
|
16
|
+
export declare function writeFileSync(path: fs.PathLike, data: Uint8Array | string): void;
|
|
17
|
+
export declare function readFile(path: fs.PathLike): Promise<Uint8Array>;
|
package/lib/FsPromise.js
CHANGED
|
@@ -44,9 +44,9 @@ export async function read(fd, buffer, offset, length, position) {
|
|
|
44
44
|
});
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
|
-
export async function writeFile(path, data) {
|
|
47
|
+
export async function writeFile(path, data, options = null) {
|
|
48
48
|
return new Promise((resolve, reject) => {
|
|
49
|
-
fs.writeFile(path, data, err => {
|
|
49
|
+
fs.writeFile(path, data, options, err => {
|
|
50
50
|
if (err)
|
|
51
51
|
reject(err);
|
|
52
52
|
else
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
1
|
import { AbstractTokenizer } from './AbstractTokenizer.js';
|
|
3
|
-
import {
|
|
2
|
+
import { IStreamReader } from 'peek-readable';
|
|
4
3
|
import { IFileInfo, IReadChunkOptions } from './types.js';
|
|
5
4
|
export declare class ReadStreamTokenizer extends AbstractTokenizer {
|
|
6
5
|
private streamReader;
|
|
7
|
-
constructor(
|
|
6
|
+
constructor(streamReader: IStreamReader, fileInfo?: IFileInfo);
|
|
8
7
|
/**
|
|
9
8
|
* Get file information, an HTTP-client may implement this doing a HEAD request
|
|
10
9
|
* @return Promise with file information
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { AbstractTokenizer } from './AbstractTokenizer.js';
|
|
2
|
-
import { EndOfStreamError
|
|
2
|
+
import { EndOfStreamError } from 'peek-readable';
|
|
3
3
|
const maxBufferSize = 256000;
|
|
4
4
|
export class ReadStreamTokenizer extends AbstractTokenizer {
|
|
5
|
-
constructor(
|
|
5
|
+
constructor(streamReader, fileInfo) {
|
|
6
6
|
super(fileInfo);
|
|
7
|
-
this.streamReader =
|
|
7
|
+
this.streamReader = streamReader;
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* Get file information, an HTTP-client may implement this doing a HEAD request
|
package/lib/core.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
1
|
import { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
|
|
3
2
|
import { Readable } from 'node:stream';
|
|
4
3
|
import { BufferTokenizer } from './BufferTokenizer.js';
|
|
5
4
|
import { IFileInfo } from './types.js';
|
|
5
|
+
import type { ReadableStream } from 'node:stream/web';
|
|
6
6
|
export { EndOfStreamError } from 'peek-readable';
|
|
7
7
|
export { ITokenizer, IFileInfo } from './types.js';
|
|
8
8
|
export { IToken, IGetToken } from '@tokenizer/token';
|
|
@@ -14,6 +14,14 @@ export { IToken, IGetToken } from '@tokenizer/token';
|
|
|
14
14
|
* @returns ReadStreamTokenizer
|
|
15
15
|
*/
|
|
16
16
|
export declare function fromStream(stream: Readable, fileInfo?: IFileInfo): ReadStreamTokenizer;
|
|
17
|
+
/**
|
|
18
|
+
* Construct ReadStreamTokenizer from given ReadableStream (WebStream API).
|
|
19
|
+
* Will set fileSize, if provided given Stream has set the .path property/
|
|
20
|
+
* @param webStream - Read from Node.js Stream.Readable
|
|
21
|
+
* @param fileInfo - Pass the file information, like size and MIME-type of the corresponding stream.
|
|
22
|
+
* @returns ReadStreamTokenizer
|
|
23
|
+
*/
|
|
24
|
+
export declare function fromWebStream(webStream: ReadableStream<Uint8Array>, fileInfo?: IFileInfo): ReadStreamTokenizer;
|
|
17
25
|
/**
|
|
18
26
|
* Construct ReadStreamTokenizer from given Buffer.
|
|
19
27
|
* @param uint8Array - Uint8Array to tokenize
|
package/lib/core.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
|
|
2
2
|
import { BufferTokenizer } from './BufferTokenizer.js';
|
|
3
|
+
import { StreamReader, WebStreamReader } from "peek-readable";
|
|
3
4
|
export { EndOfStreamError } from 'peek-readable';
|
|
4
5
|
/**
|
|
5
6
|
* Construct ReadStreamTokenizer from given Stream.
|
|
@@ -10,7 +11,18 @@ export { EndOfStreamError } from 'peek-readable';
|
|
|
10
11
|
*/
|
|
11
12
|
export function fromStream(stream, fileInfo) {
|
|
12
13
|
fileInfo = fileInfo ? fileInfo : {};
|
|
13
|
-
return new ReadStreamTokenizer(stream, fileInfo);
|
|
14
|
+
return new ReadStreamTokenizer(new StreamReader(stream), fileInfo);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Construct ReadStreamTokenizer from given ReadableStream (WebStream API).
|
|
18
|
+
* Will set fileSize, if provided given Stream has set the .path property/
|
|
19
|
+
* @param webStream - Read from Node.js Stream.Readable
|
|
20
|
+
* @param fileInfo - Pass the file information, like size and MIME-type of the corresponding stream.
|
|
21
|
+
* @returns ReadStreamTokenizer
|
|
22
|
+
*/
|
|
23
|
+
export function fromWebStream(webStream, fileInfo) {
|
|
24
|
+
fileInfo = fileInfo ? fileInfo : {};
|
|
25
|
+
return new ReadStreamTokenizer(new WebStreamReader(webStream), fileInfo);
|
|
14
26
|
}
|
|
15
27
|
/**
|
|
16
28
|
* Construct ReadStreamTokenizer from given Buffer.
|
package/lib/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
1
|
import { Readable } from 'node:stream';
|
|
3
2
|
import { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
|
|
4
3
|
import * as core from './core.js';
|
|
5
4
|
export { fromFile } from './FileTokenizer.js';
|
|
6
|
-
export { ITokenizer, EndOfStreamError, fromBuffer, IFileInfo } from './core.js';
|
|
5
|
+
export { ITokenizer, EndOfStreamError, fromBuffer, fromWebStream, IFileInfo } from './core.js';
|
|
7
6
|
export { IToken, IGetToken } from '@tokenizer/token';
|
|
8
7
|
/**
|
|
9
8
|
* Construct ReadStreamTokenizer from given Stream.
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as fs from './FsPromise.js';
|
|
2
2
|
import * as core from './core.js';
|
|
3
3
|
export { fromFile } from './FileTokenizer.js';
|
|
4
|
-
export { EndOfStreamError, fromBuffer } from './core.js';
|
|
4
|
+
export { EndOfStreamError, fromBuffer, fromWebStream } from './core.js';
|
|
5
5
|
/**
|
|
6
6
|
* Construct ReadStreamTokenizer from given Stream.
|
|
7
7
|
* Will set fileSize, if provided given Stream has set the .path property.
|
package/lib/types.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
1
|
import { IGetToken } from '@tokenizer/token';
|
|
3
2
|
export interface IFileInfo {
|
|
4
3
|
/**
|
|
@@ -57,14 +56,14 @@ export interface ITokenizer {
|
|
|
57
56
|
* @param options - Read behaviour options
|
|
58
57
|
* @returns Promise with number of bytes read
|
|
59
58
|
*/
|
|
60
|
-
peekBuffer(buffer:
|
|
59
|
+
peekBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
|
|
61
60
|
/**
|
|
62
61
|
* Peek (read ahead) buffer from tokenizer
|
|
63
62
|
* @param buffer - Target buffer to fill with data peeked from the tokenizer-stream
|
|
64
63
|
* @param options - Additional read options
|
|
65
64
|
* @returns Promise with number of bytes read
|
|
66
65
|
*/
|
|
67
|
-
readBuffer(buffer:
|
|
66
|
+
readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
|
|
68
67
|
/**
|
|
69
68
|
* Peek a token from the tokenizer-stream.
|
|
70
69
|
* @param token - Token to peek from the tokenizer-stream.
|
package/package.json
CHANGED
|
@@ -1,94 +1,95 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "strtok3",
|
|
3
|
-
"version": "7.
|
|
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": "npm run compile-src && npm run compile-test",
|
|
18
|
-
"build": "npm run clean && npm run compile",
|
|
19
|
-
"eslint": "eslint lib test --ext .ts --ignore-pattern *.d.ts",
|
|
20
|
-
"lint-md": "remark -u preset-lint-recommended .",
|
|
21
|
-
"lint": "npm run lint-md && npm run eslint",
|
|
22
|
-
"fix": "eslint lib test --ext .ts --ignore-pattern *.d.ts --fix",
|
|
23
|
-
"test": "mocha",
|
|
24
|
-
"test-coverage": "c8 npm run test",
|
|
25
|
-
"send-codacy": "c8 report --reporter=text-lcov | codacy-coverage",
|
|
26
|
-
"start": "npm run compile && npm run lint && npm run cover-test"
|
|
27
|
-
},
|
|
28
|
-
"engines": {
|
|
29
|
-
"node": ">=14.16"
|
|
30
|
-
},
|
|
31
|
-
"repository": {
|
|
32
|
-
"type": "git",
|
|
33
|
-
"url": "https://github.com/Borewit/strtok3.git"
|
|
34
|
-
},
|
|
35
|
-
"license": "MIT",
|
|
36
|
-
"type": "module",
|
|
37
|
-
"exports": {
|
|
38
|
-
".": {
|
|
39
|
-
"node": "./lib/index.js",
|
|
40
|
-
"default": "./lib/core.js"
|
|
41
|
-
},
|
|
42
|
-
"./core": "./lib/core.js"
|
|
43
|
-
},
|
|
44
|
-
"types": "lib/index.d.ts",
|
|
45
|
-
"files": [
|
|
46
|
-
"lib/**/*.js",
|
|
47
|
-
"lib/**/*.d.ts"
|
|
48
|
-
],
|
|
49
|
-
"bugs": {
|
|
50
|
-
"url": "https://github.com/Borewit/strtok3/issues"
|
|
51
|
-
},
|
|
52
|
-
"devDependencies": {
|
|
53
|
-
"@types/chai": "^4.3.
|
|
54
|
-
"@types/debug": "^4.1.
|
|
55
|
-
"@types/mocha": "^
|
|
56
|
-
"@types/node": "^
|
|
57
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
58
|
-
"@typescript-eslint/parser": "^
|
|
59
|
-
"c8": "^
|
|
60
|
-
"chai": "^
|
|
61
|
-
"del-cli": "^5.
|
|
62
|
-
"eslint": "^8.
|
|
63
|
-
"eslint-config-prettier": "^
|
|
64
|
-
"eslint-import-resolver-typescript": "^3.
|
|
65
|
-
"eslint-plugin-import": "^2.
|
|
66
|
-
"eslint-plugin-jsdoc": "^
|
|
67
|
-
"eslint-plugin-node": "^11.1.0",
|
|
68
|
-
"eslint-plugin-unicorn": "^
|
|
69
|
-
"mocha": "^10.
|
|
70
|
-
"remark-cli": "^
|
|
71
|
-
"remark-preset-lint-recommended": "^
|
|
72
|
-
"token-types": "^
|
|
73
|
-
"ts-node": "^10.9.
|
|
74
|
-
"typescript": "^
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
"
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
"
|
|
83
|
-
"
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
"
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
"
|
|
92
|
-
"
|
|
93
|
-
|
|
94
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "strtok3",
|
|
3
|
+
"version": "7.1.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": "npm run compile-src && npm run compile-test",
|
|
18
|
+
"build": "npm run clean && npm run compile",
|
|
19
|
+
"eslint": "eslint lib test --ext .ts --ignore-pattern *.d.ts",
|
|
20
|
+
"lint-md": "remark -u preset-lint-recommended .",
|
|
21
|
+
"lint": "npm run lint-md && npm run eslint",
|
|
22
|
+
"fix": "eslint lib test --ext .ts --ignore-pattern *.d.ts --fix",
|
|
23
|
+
"test": "mocha",
|
|
24
|
+
"test-coverage": "c8 npm run test",
|
|
25
|
+
"send-codacy": "c8 report --reporter=text-lcov | codacy-coverage",
|
|
26
|
+
"start": "npm run compile && npm run lint && npm run cover-test"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=14.16"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/Borewit/strtok3.git"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"type": "module",
|
|
37
|
+
"exports": {
|
|
38
|
+
".": {
|
|
39
|
+
"node": "./lib/index.js",
|
|
40
|
+
"default": "./lib/core.js"
|
|
41
|
+
},
|
|
42
|
+
"./core": "./lib/core.js"
|
|
43
|
+
},
|
|
44
|
+
"types": "lib/index.d.ts",
|
|
45
|
+
"files": [
|
|
46
|
+
"lib/**/*.js",
|
|
47
|
+
"lib/**/*.d.ts"
|
|
48
|
+
],
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/Borewit/strtok3/issues"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/chai": "^4.3.16",
|
|
54
|
+
"@types/debug": "^4.1.12",
|
|
55
|
+
"@types/mocha": "^10.0.7",
|
|
56
|
+
"@types/node": "^20.14.9",
|
|
57
|
+
"@typescript-eslint/eslint-plugin": "^7.15.0",
|
|
58
|
+
"@typescript-eslint/parser": "^7.15.0",
|
|
59
|
+
"c8": "^10.1.2",
|
|
60
|
+
"chai": "^5.1.1",
|
|
61
|
+
"del-cli": "^5.1.0",
|
|
62
|
+
"eslint": "^8.57.0",
|
|
63
|
+
"eslint-config-prettier": "^9.0.0",
|
|
64
|
+
"eslint-import-resolver-typescript": "^3.6.1",
|
|
65
|
+
"eslint-plugin-import": "^2.29.0",
|
|
66
|
+
"eslint-plugin-jsdoc": "^46.8.2",
|
|
67
|
+
"eslint-plugin-node": "^11.1.0",
|
|
68
|
+
"eslint-plugin-unicorn": "^54.0.0",
|
|
69
|
+
"mocha": "^10.6.0",
|
|
70
|
+
"remark-cli": "^12.0.1",
|
|
71
|
+
"remark-preset-lint-recommended": "^7.0.0",
|
|
72
|
+
"token-types": "^6.0.0",
|
|
73
|
+
"ts-node": "^10.9.2",
|
|
74
|
+
"typescript": "^5.5.3",
|
|
75
|
+
"uint8array-extras": "^1.3.0"
|
|
76
|
+
},
|
|
77
|
+
"dependencies": {
|
|
78
|
+
"@tokenizer/token": "^0.3.0",
|
|
79
|
+
"peek-readable": "^5.1.1"
|
|
80
|
+
},
|
|
81
|
+
"keywords": [
|
|
82
|
+
"tokenizer",
|
|
83
|
+
"reader",
|
|
84
|
+
"token",
|
|
85
|
+
"async",
|
|
86
|
+
"promise",
|
|
87
|
+
"parser",
|
|
88
|
+
"decoder",
|
|
89
|
+
"binary",
|
|
90
|
+
"endian",
|
|
91
|
+
"uint",
|
|
92
|
+
"stream",
|
|
93
|
+
"streaming"
|
|
94
|
+
]
|
|
95
|
+
}
|