llonebot-dist 6.6.4
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.
- dist/default_config.json +68 -0
- dist/llonebot.js +48348 -0
- dist/llonebot.js.map +1 -0
- dist/node_modules/@borewit/text-codec/LICENSE.txt +9 -0
- dist/node_modules/@borewit/text-codec/README.md +76 -0
- dist/node_modules/@borewit/text-codec/lib/index.d.ts +8 -0
- dist/node_modules/@borewit/text-codec/lib/index.js +161 -0
- dist/node_modules/@borewit/text-codec/package.json +68 -0
- dist/node_modules/@minatojs/sql.js/LICENSE +44 -0
- dist/node_modules/@minatojs/sql.js/README.md +357 -0
- dist/node_modules/@minatojs/sql.js/dist/sql-wasm.d.ts +316 -0
- dist/node_modules/@minatojs/sql.js/dist/sql-wasm.js +225 -0
- dist/node_modules/@minatojs/sql.js/dist/sql-wasm.wasm +0 -0
- dist/node_modules/@minatojs/sql.js/package.json +58 -0
- dist/node_modules/@tokenizer/inflate/LICENSE +15 -0
- dist/node_modules/@tokenizer/inflate/README.md +114 -0
- dist/node_modules/@tokenizer/inflate/lib/GzipHandler.d.ts +6 -0
- dist/node_modules/@tokenizer/inflate/lib/GzipHandler.js +19 -0
- dist/node_modules/@tokenizer/inflate/lib/ZipHandler.d.ts +26 -0
- dist/node_modules/@tokenizer/inflate/lib/ZipHandler.js +233 -0
- dist/node_modules/@tokenizer/inflate/lib/ZipToken.d.ts +94 -0
- dist/node_modules/@tokenizer/inflate/lib/ZipToken.js +117 -0
- dist/node_modules/@tokenizer/inflate/lib/index.d.ts +3 -0
- dist/node_modules/@tokenizer/inflate/lib/index.js +2 -0
- dist/node_modules/@tokenizer/inflate/package.json +76 -0
- dist/node_modules/@tokenizer/token/README.md +19 -0
- dist/node_modules/@tokenizer/token/index.d.ts +30 -0
- dist/node_modules/@tokenizer/token/package.json +33 -0
- dist/node_modules/debug/LICENSE +20 -0
- dist/node_modules/debug/README.md +481 -0
- dist/node_modules/debug/package.json +64 -0
- dist/node_modules/debug/src/browser.js +272 -0
- dist/node_modules/debug/src/common.js +292 -0
- dist/node_modules/debug/src/index.js +10 -0
- dist/node_modules/debug/src/node.js +263 -0
- dist/node_modules/file-type/core.d.ts +253 -0
- dist/node_modules/file-type/core.js +1899 -0
- dist/node_modules/file-type/index.d.ts +98 -0
- dist/node_modules/file-type/index.js +86 -0
- dist/node_modules/file-type/license +9 -0
- dist/node_modules/file-type/package.json +288 -0
- dist/node_modules/file-type/readme.md +674 -0
- dist/node_modules/file-type/supported.js +356 -0
- dist/node_modules/file-type/util.js +60 -0
- dist/node_modules/ieee754/LICENSE +11 -0
- dist/node_modules/ieee754/README.md +51 -0
- dist/node_modules/ieee754/index.d.ts +10 -0
- dist/node_modules/ieee754/index.js +85 -0
- dist/node_modules/ieee754/package.json +52 -0
- dist/node_modules/ms/index.js +162 -0
- dist/node_modules/ms/license.md +21 -0
- dist/node_modules/ms/package.json +38 -0
- dist/node_modules/ms/readme.md +59 -0
- dist/node_modules/silk-wasm/LICENSE +21 -0
- dist/node_modules/silk-wasm/README.md +85 -0
- dist/node_modules/silk-wasm/lib/index.cjs +16 -0
- dist/node_modules/silk-wasm/lib/index.d.ts +70 -0
- dist/node_modules/silk-wasm/lib/index.mjs +16 -0
- dist/node_modules/silk-wasm/lib/silk.wasm +0 -0
- dist/node_modules/silk-wasm/lib/utils.d.ts +4 -0
- dist/node_modules/silk-wasm/package.json +39 -0
- dist/node_modules/strtok3/LICENSE.txt +21 -0
- dist/node_modules/strtok3/README.md +399 -0
- dist/node_modules/strtok3/lib/AbstractTokenizer.d.ts +76 -0
- dist/node_modules/strtok3/lib/AbstractTokenizer.js +108 -0
- dist/node_modules/strtok3/lib/BlobTokenizer.d.ts +29 -0
- dist/node_modules/strtok3/lib/BlobTokenizer.js +53 -0
- dist/node_modules/strtok3/lib/BufferTokenizer.d.ts +29 -0
- dist/node_modules/strtok3/lib/BufferTokenizer.js +52 -0
- dist/node_modules/strtok3/lib/FileTokenizer.d.ts +37 -0
- dist/node_modules/strtok3/lib/FileTokenizer.js +61 -0
- dist/node_modules/strtok3/lib/ReadStreamTokenizer.d.ts +31 -0
- dist/node_modules/strtok3/lib/ReadStreamTokenizer.js +102 -0
- dist/node_modules/strtok3/lib/core.d.ts +40 -0
- dist/node_modules/strtok3/lib/core.js +62 -0
- dist/node_modules/strtok3/lib/index.d.ts +16 -0
- dist/node_modules/strtok3/lib/index.js +22 -0
- dist/node_modules/strtok3/lib/stream/AbstractStreamReader.d.ts +54 -0
- dist/node_modules/strtok3/lib/stream/AbstractStreamReader.js +71 -0
- dist/node_modules/strtok3/lib/stream/Deferred.d.ts +6 -0
- dist/node_modules/strtok3/lib/stream/Deferred.js +10 -0
- dist/node_modules/strtok3/lib/stream/Errors.d.ts +10 -0
- dist/node_modules/strtok3/lib/stream/Errors.js +16 -0
- dist/node_modules/strtok3/lib/stream/StreamReader.d.ts +29 -0
- dist/node_modules/strtok3/lib/stream/StreamReader.js +83 -0
- dist/node_modules/strtok3/lib/stream/WebStreamByobReader.d.ts +14 -0
- dist/node_modules/strtok3/lib/stream/WebStreamByobReader.js +27 -0
- dist/node_modules/strtok3/lib/stream/WebStreamDefaultReader.d.ts +19 -0
- dist/node_modules/strtok3/lib/stream/WebStreamDefaultReader.js +62 -0
- dist/node_modules/strtok3/lib/stream/WebStreamReader.d.ts +14 -0
- dist/node_modules/strtok3/lib/stream/WebStreamReader.js +13 -0
- dist/node_modules/strtok3/lib/stream/WebStreamReaderFactory.d.ts +5 -0
- dist/node_modules/strtok3/lib/stream/WebStreamReaderFactory.js +19 -0
- dist/node_modules/strtok3/lib/stream/index.d.ts +6 -0
- dist/node_modules/strtok3/lib/stream/index.js +5 -0
- dist/node_modules/strtok3/lib/types.d.ts +139 -0
- dist/node_modules/strtok3/lib/types.js +1 -0
- dist/node_modules/strtok3/package.json +94 -0
- dist/node_modules/token-types/LICENSE.txt +9 -0
- dist/node_modules/token-types/README.md +120 -0
- dist/node_modules/token-types/lib/index.d.ts +135 -0
- dist/node_modules/token-types/lib/index.js +401 -0
- dist/node_modules/token-types/package.json +81 -0
- dist/node_modules/uint8array-extras/index.d.ts +312 -0
- dist/node_modules/uint8array-extras/index.js +321 -0
- dist/node_modules/uint8array-extras/license +9 -0
- dist/node_modules/uint8array-extras/package.json +54 -0
- dist/node_modules/uint8array-extras/readme.md +301 -0
- dist/node_modules/ws/LICENSE +20 -0
- dist/node_modules/ws/README.md +548 -0
- dist/node_modules/ws/browser.js +8 -0
- dist/node_modules/ws/index.js +13 -0
- dist/node_modules/ws/lib/buffer-util.js +131 -0
- dist/node_modules/ws/lib/constants.js +18 -0
- dist/node_modules/ws/lib/event-target.js +292 -0
- dist/node_modules/ws/lib/extension.js +203 -0
- dist/node_modules/ws/lib/limiter.js +55 -0
- dist/node_modules/ws/lib/permessage-deflate.js +528 -0
- dist/node_modules/ws/lib/receiver.js +706 -0
- dist/node_modules/ws/lib/sender.js +602 -0
- dist/node_modules/ws/lib/stream.js +161 -0
- dist/node_modules/ws/lib/subprotocol.js +62 -0
- dist/node_modules/ws/lib/validation.js +152 -0
- dist/node_modules/ws/lib/websocket-server.js +550 -0
- dist/node_modules/ws/lib/websocket.js +1388 -0
- dist/node_modules/ws/package.json +69 -0
- dist/node_modules/ws/wrapper.mjs +8 -0
- dist/package.json +1 -0
- dist/webui/assets/index-B9vGhdCO.js +256 -0
- dist/webui/assets/index-DaqFU7JR.css +1 -0
- dist/webui/index.html +13 -0
- dist/webui/logo.jpg +0 -0
- dist//344/275/277/347/224/250/350/257/264/346/230/216.txt +11 -0
- dist//346/233/264/346/226/260/346/227/245/345/277/227.txt +399 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright © 2025 Borewit
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
[](https://github.com/Borewit/strtok3/actions/workflows/ci.yml)
|
|
2
|
+
[](https://github.com/Borewit/strtok3/actions/workflows/codeql.yml)
|
|
3
|
+
[](https://npmjs.org/package/strtok3)
|
|
4
|
+
[](https://npmcharts.com/compare/strtok3,token-types?start=1200&interval=30)
|
|
5
|
+
[](https://deepscan.io/dashboard#view=project&tid=5165&pid=8526&bid=103329)
|
|
6
|
+
[](https://snyk.io/test/github/Borewit/strtok3?targetFile=package.json)
|
|
7
|
+
[](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) | 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 | 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.
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { ITokenizer, IFileInfo, IReadChunkOptions, ITokenizerOptions } from './types.js';
|
|
2
|
+
import type { IGetToken, IToken } from '@tokenizer/token';
|
|
3
|
+
interface INormalizedReadChunkOptions extends IReadChunkOptions {
|
|
4
|
+
length: number;
|
|
5
|
+
position: number;
|
|
6
|
+
mayBeLess?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Core tokenizer
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class AbstractTokenizer implements ITokenizer {
|
|
12
|
+
private onClose?;
|
|
13
|
+
private numBuffer;
|
|
14
|
+
abstract fileInfo: IFileInfo;
|
|
15
|
+
/**
|
|
16
|
+
* Tokenizer-stream position
|
|
17
|
+
*/
|
|
18
|
+
position: number;
|
|
19
|
+
/**
|
|
20
|
+
* Constructor
|
|
21
|
+
* @param options Tokenizer options
|
|
22
|
+
* @protected
|
|
23
|
+
*/
|
|
24
|
+
protected constructor(options?: ITokenizerOptions);
|
|
25
|
+
abstract supportsRandomAccess(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Read buffer from tokenizer
|
|
28
|
+
* @param buffer - Target buffer to fill with data read from the tokenizer-stream
|
|
29
|
+
* @param options - Additional read options
|
|
30
|
+
* @returns Promise with number of bytes read
|
|
31
|
+
*/
|
|
32
|
+
abstract readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
|
|
33
|
+
/**
|
|
34
|
+
* Peek (read ahead) buffer from tokenizer
|
|
35
|
+
* @param uint8Array - Target buffer to fill with data peeked from the tokenizer-stream
|
|
36
|
+
* @param options - Peek behaviour options
|
|
37
|
+
* @returns Promise with number of bytes read
|
|
38
|
+
*/
|
|
39
|
+
abstract peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
|
|
40
|
+
/**
|
|
41
|
+
* Read a token from the tokenizer-stream
|
|
42
|
+
* @param token - The token to read
|
|
43
|
+
* @param position - If provided, the desired position in the tokenizer-stream
|
|
44
|
+
* @returns Promise with token data
|
|
45
|
+
*/
|
|
46
|
+
readToken<Value>(token: IGetToken<Value>, position?: number): Promise<Value>;
|
|
47
|
+
/**
|
|
48
|
+
* Peek a token from the tokenizer-stream.
|
|
49
|
+
* @param token - Token to peek from the tokenizer-stream.
|
|
50
|
+
* @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
|
|
51
|
+
* @returns Promise with token data
|
|
52
|
+
*/
|
|
53
|
+
peekToken<Value>(token: IGetToken<Value>, position?: number): Promise<Value>;
|
|
54
|
+
/**
|
|
55
|
+
* Read a numeric token from the stream
|
|
56
|
+
* @param token - Numeric token
|
|
57
|
+
* @returns Promise with number
|
|
58
|
+
*/
|
|
59
|
+
readNumber(token: IToken<number>): Promise<number>;
|
|
60
|
+
/**
|
|
61
|
+
* Read a numeric token from the stream
|
|
62
|
+
* @param token - Numeric token
|
|
63
|
+
* @returns Promise with number
|
|
64
|
+
*/
|
|
65
|
+
peekNumber(token: IToken<number>): Promise<number>;
|
|
66
|
+
/**
|
|
67
|
+
* Ignore number of bytes, advances the pointer in under tokenizer-stream.
|
|
68
|
+
* @param length - Number of bytes to ignore
|
|
69
|
+
* @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
|
|
70
|
+
*/
|
|
71
|
+
ignore(length: number): Promise<number>;
|
|
72
|
+
close(): Promise<void>;
|
|
73
|
+
protected normalizeOptions(uint8Array: Uint8Array, options?: IReadChunkOptions): INormalizedReadChunkOptions;
|
|
74
|
+
abort(): Promise<void>;
|
|
75
|
+
}
|
|
76
|
+
export {};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { EndOfStreamError } from './stream/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Core tokenizer
|
|
4
|
+
*/
|
|
5
|
+
export class AbstractTokenizer {
|
|
6
|
+
/**
|
|
7
|
+
* Constructor
|
|
8
|
+
* @param options Tokenizer options
|
|
9
|
+
* @protected
|
|
10
|
+
*/
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.numBuffer = new Uint8Array(8);
|
|
13
|
+
/**
|
|
14
|
+
* Tokenizer-stream position
|
|
15
|
+
*/
|
|
16
|
+
this.position = 0;
|
|
17
|
+
this.onClose = options?.onClose;
|
|
18
|
+
if (options?.abortSignal) {
|
|
19
|
+
options.abortSignal.addEventListener('abort', () => {
|
|
20
|
+
this.abort();
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Read a token from the tokenizer-stream
|
|
26
|
+
* @param token - The token to read
|
|
27
|
+
* @param position - If provided, the desired position in the tokenizer-stream
|
|
28
|
+
* @returns Promise with token data
|
|
29
|
+
*/
|
|
30
|
+
async readToken(token, position = this.position) {
|
|
31
|
+
const uint8Array = new Uint8Array(token.len);
|
|
32
|
+
const len = await this.readBuffer(uint8Array, { position });
|
|
33
|
+
if (len < token.len)
|
|
34
|
+
throw new EndOfStreamError();
|
|
35
|
+
return token.get(uint8Array, 0);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Peek a token from the tokenizer-stream.
|
|
39
|
+
* @param token - Token to peek from the tokenizer-stream.
|
|
40
|
+
* @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
|
|
41
|
+
* @returns Promise with token data
|
|
42
|
+
*/
|
|
43
|
+
async peekToken(token, position = this.position) {
|
|
44
|
+
const uint8Array = new Uint8Array(token.len);
|
|
45
|
+
const len = await this.peekBuffer(uint8Array, { position });
|
|
46
|
+
if (len < token.len)
|
|
47
|
+
throw new EndOfStreamError();
|
|
48
|
+
return token.get(uint8Array, 0);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Read a numeric token from the stream
|
|
52
|
+
* @param token - Numeric token
|
|
53
|
+
* @returns Promise with number
|
|
54
|
+
*/
|
|
55
|
+
async readNumber(token) {
|
|
56
|
+
const len = await this.readBuffer(this.numBuffer, { length: token.len });
|
|
57
|
+
if (len < token.len)
|
|
58
|
+
throw new EndOfStreamError();
|
|
59
|
+
return token.get(this.numBuffer, 0);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Read a numeric token from the stream
|
|
63
|
+
* @param token - Numeric token
|
|
64
|
+
* @returns Promise with number
|
|
65
|
+
*/
|
|
66
|
+
async peekNumber(token) {
|
|
67
|
+
const len = await this.peekBuffer(this.numBuffer, { length: token.len });
|
|
68
|
+
if (len < token.len)
|
|
69
|
+
throw new EndOfStreamError();
|
|
70
|
+
return token.get(this.numBuffer, 0);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Ignore number of bytes, advances the pointer in under tokenizer-stream.
|
|
74
|
+
* @param length - Number of bytes to ignore
|
|
75
|
+
* @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
|
|
76
|
+
*/
|
|
77
|
+
async ignore(length) {
|
|
78
|
+
if (this.fileInfo.size !== undefined) {
|
|
79
|
+
const bytesLeft = this.fileInfo.size - this.position;
|
|
80
|
+
if (length > bytesLeft) {
|
|
81
|
+
this.position += bytesLeft;
|
|
82
|
+
return bytesLeft;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
this.position += length;
|
|
86
|
+
return length;
|
|
87
|
+
}
|
|
88
|
+
async close() {
|
|
89
|
+
await this.abort();
|
|
90
|
+
await this.onClose?.();
|
|
91
|
+
}
|
|
92
|
+
normalizeOptions(uint8Array, options) {
|
|
93
|
+
if (!this.supportsRandomAccess() && options && options.position !== undefined && options.position < this.position) {
|
|
94
|
+
throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
...{
|
|
98
|
+
mayBeLess: false,
|
|
99
|
+
offset: 0,
|
|
100
|
+
length: uint8Array.length,
|
|
101
|
+
position: this.position
|
|
102
|
+
}, ...options
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
abort() {
|
|
106
|
+
return Promise.resolve(); // Ignore abort signal
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ITokenizerOptions, IReadChunkOptions, IRandomAccessFileInfo, IRandomAccessTokenizer } from './types.js';
|
|
2
|
+
import { AbstractTokenizer } from './AbstractTokenizer.js';
|
|
3
|
+
export declare class BlobTokenizer extends AbstractTokenizer implements IRandomAccessTokenizer {
|
|
4
|
+
private blob;
|
|
5
|
+
fileInfo: IRandomAccessFileInfo;
|
|
6
|
+
/**
|
|
7
|
+
* Construct BufferTokenizer
|
|
8
|
+
* @param blob - Uint8Array to tokenize
|
|
9
|
+
* @param options Tokenizer options
|
|
10
|
+
*/
|
|
11
|
+
constructor(blob: Blob, options?: ITokenizerOptions);
|
|
12
|
+
/**
|
|
13
|
+
* Read buffer from tokenizer
|
|
14
|
+
* @param uint8Array - Uint8Array to tokenize
|
|
15
|
+
* @param options - Read behaviour options
|
|
16
|
+
* @returns {Promise<number>}
|
|
17
|
+
*/
|
|
18
|
+
readBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
|
|
19
|
+
/**
|
|
20
|
+
* Peek (read ahead) buffer from tokenizer
|
|
21
|
+
* @param buffer
|
|
22
|
+
* @param options - Read behaviour options
|
|
23
|
+
* @returns {Promise<number>}
|
|
24
|
+
*/
|
|
25
|
+
peekBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
|
|
26
|
+
close(): Promise<void>;
|
|
27
|
+
supportsRandomAccess(): boolean;
|
|
28
|
+
setPosition(position: number): void;
|
|
29
|
+
}
|