@andrew_l/tl-pack 0.1.81 → 0.2.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 +21 -0
- package/README.md +49 -41
- package/dist/index.cjs +36 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.mts +28 -0
- package/dist/index.d.ts +28 -5
- package/dist/index.mjs +28 -0
- package/dist/index.mjs.map +1 -0
- package/dist/shared/tl-pack.C2PEqnNg.d.cts +200 -0
- package/dist/shared/tl-pack.C2PEqnNg.d.mts +200 -0
- package/dist/shared/tl-pack.C2PEqnNg.d.ts +200 -0
- package/dist/shared/tl-pack.CkwM-fIb.mjs +1152 -0
- package/dist/shared/tl-pack.CkwM-fIb.mjs.map +1 -0
- package/dist/shared/tl-pack.DfL54MjP.cjs +1162 -0
- package/dist/shared/tl-pack.DfL54MjP.cjs.map +1 -0
- package/dist/stream.cjs +72 -0
- package/dist/stream.cjs.map +1 -0
- package/dist/stream.d.cts +20 -0
- package/dist/stream.d.mts +20 -0
- package/dist/stream.d.ts +8 -8
- package/dist/stream.mjs +69 -0
- package/dist/stream.mjs.map +1 -0
- package/package.json +50 -53
- package/dist/BinaryReader.d.ts +0 -109
- package/dist/BinaryReader.js +0 -412
- package/dist/BinaryWriter.d.ts +0 -47
- package/dist/BinaryWriter.js +0 -385
- package/dist/constants.d.ts +0 -26
- package/dist/constants.js +0 -27
- package/dist/dictionary.d.ts +0 -17
- package/dist/dictionary.js +0 -51
- package/dist/extension.d.ts +0 -13
- package/dist/extension.js +0 -13
- package/dist/helpers.d.ts +0 -11
- package/dist/helpers.js +0 -321
- package/dist/index.js +0 -5
- package/dist/stream.js +0 -67
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Andrew L. <andrew.io.dev@gmail.com>
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,24 +1,33 @@
|
|
|
1
|
-
#
|
|
1
|
+
# TL Pack - Binary Serialization Library <!-- omit in toc -->
|
|
2
2
|
|
|
3
|
-

|
|
3
|
+
 <!-- omit in toc -->
|
|
4
|
+
 <!-- omit in toc -->
|
|
5
|
+
 <!-- omit in toc -->
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
Binary serialization library, inspired by the TL (Type Language) format, created by the VK team. Unlike official TL, this version does not require a schema for serialization/deserialization. It provides a compact and fast alternative to other binary serialization formats like MessagePack.
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
⚡ **Benchmark**: Slightly faster and more compact than **@msgpack/msgpack**
|
|
10
|
+
⚠️ **Note**: Benchmark claims may vary.
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
<!-- install placeholder -->
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
## ✨ Features
|
|
12
15
|
|
|
13
|
-
|
|
16
|
+
- **No Schema Required**: Unlike traditional serialization formats, this version does not require a predefined schema for object serialization.
|
|
17
|
+
- **Compact & Fast**: Designed to be lightweight and fast, with smaller binary output.
|
|
18
|
+
- **Custom Extension Support**: Easily extend serialization to custom types.
|
|
19
|
+
- **Stream Support**: Supports streaming serialization/deserialization in Node.js.
|
|
20
|
+
|
|
21
|
+
## 🚀 Example Usage
|
|
22
|
+
|
|
23
|
+
### Basic Example
|
|
14
24
|
|
|
15
25
|
```javascript
|
|
16
26
|
import { BinaryWriter, BinaryReader } from '@andrew_l/tl-pack';
|
|
17
27
|
|
|
18
28
|
const writer = new BinaryWriter();
|
|
19
29
|
|
|
20
|
-
//
|
|
21
|
-
// writer.writeObjectGzip
|
|
30
|
+
// Serialize an object with various data types
|
|
22
31
|
writer.writeObject({
|
|
23
32
|
null: null,
|
|
24
33
|
uint8: 255,
|
|
@@ -55,6 +64,31 @@ console.log(reader.readObject());
|
|
|
55
64
|
*/
|
|
56
65
|
```
|
|
57
66
|
|
|
67
|
+
### Stream Example (Node.js Only)
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
import { Readable } from 'node:stream';
|
|
71
|
+
import { TLEncode, TLDecode } from '@andrew_l/tl-pack/stream';
|
|
72
|
+
|
|
73
|
+
const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
74
|
+
|
|
75
|
+
const dataStream = new Readable({ objectMode: true });
|
|
76
|
+
|
|
77
|
+
dataStream._read = () => {
|
|
78
|
+
const chunk = values.shift();
|
|
79
|
+
|
|
80
|
+
dataStream.push(chunk || null);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const encode = new TLEncode();
|
|
84
|
+
const decode = new TLDecode();
|
|
85
|
+
|
|
86
|
+
dataStream.pipe(encode).pipe(decode);
|
|
87
|
+
|
|
88
|
+
decode.on('data', data => console.log('stream', data));
|
|
89
|
+
decode.on('error', console.error);
|
|
90
|
+
```
|
|
91
|
+
|
|
58
92
|
## Supported Types
|
|
59
93
|
|
|
60
94
|
| Constructor ID | Byte Size |
|
|
@@ -79,33 +113,10 @@ console.log(reader.readObject());
|
|
|
79
113
|
| Repeat | 5 |
|
|
80
114
|
| GZIP | 5 + sizeof(object) |
|
|
81
115
|
|
|
82
|
-
## Stream Example (NodeJs Only)
|
|
83
|
-
|
|
84
|
-
```javascript
|
|
85
|
-
import { Readable } from 'node:stream';
|
|
86
|
-
import { TLEncode, TLDecode } from '@andrew_l/tl-pack/stream';
|
|
87
|
-
|
|
88
|
-
const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
89
|
-
|
|
90
|
-
const dataStream = new Readable({ objectMode: true });
|
|
91
|
-
|
|
92
|
-
dataStream._read = () => {
|
|
93
|
-
const chunk = values.shift();
|
|
94
|
-
|
|
95
|
-
dataStream.push(chunk || null);
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const encode = new TLEncode();
|
|
99
|
-
const decode = new TLDecode();
|
|
100
|
-
|
|
101
|
-
dataStream.pipe(encode).pipe(decode);
|
|
102
|
-
|
|
103
|
-
decode.on('data', (data) => console.log('stream', data));
|
|
104
|
-
decode.on('error', console.error);
|
|
105
|
-
```
|
|
106
|
-
|
|
107
116
|
## Custom Types
|
|
108
117
|
|
|
118
|
+
You can extend tl-pack to handle custom types. For example, handling ObjectId from Mongoose:
|
|
119
|
+
|
|
109
120
|
```javascript
|
|
110
121
|
import mongoose from 'mongoose';
|
|
111
122
|
import { BinaryWriter, BinaryReader, createExtension } from '@andrew_l/tl-pack';
|
|
@@ -170,15 +181,12 @@ function hex(arrayBuffer: Uint8Array) {
|
|
|
170
181
|
}
|
|
171
182
|
```
|
|
172
183
|
|
|
173
|
-
## Dictionary
|
|
174
|
-
|
|
175
|
-
A dictionary is used to replace strings with numeric indexes, which saves the resulting buffer size. In stream mode, the dictionary is grown while the stream is alive.
|
|
184
|
+
## Dictionary Support
|
|
176
185
|
|
|
177
|
-
|
|
178
|
-
Dictionary with you initialize a BinaryWriter/BinaryReader.
|
|
186
|
+
The dictionary helps optimize serialization by replacing strings with numeric indexes, saving buffer space.
|
|
179
187
|
|
|
180
|
-
**
|
|
181
|
-
Dictionary
|
|
188
|
+
- **Static Dictionary:** Pre-defined dictionary initialized when creating the BinaryWriter/BinaryReader.
|
|
189
|
+
- **Dynamic Dictionary:** Grows dynamically during encoding and decoding, especially useful for objects (Maps).
|
|
182
190
|
|
|
183
191
|
## Production
|
|
184
192
|
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BinaryWriter = require('./shared/tl-pack.DfL54MjP.cjs');
|
|
4
|
+
const toolkit = require('@andrew_l/toolkit');
|
|
5
|
+
require('pako');
|
|
6
|
+
|
|
7
|
+
function createExtension(token, { encode, decode }) {
|
|
8
|
+
toolkit.assert.ok(Math.trunc(token) === token, " Token must be integer value.");
|
|
9
|
+
toolkit.assert.ok(
|
|
10
|
+
token >= 0 && token <= 255,
|
|
11
|
+
"Token must be a 8 bit number. (0 - 255)"
|
|
12
|
+
);
|
|
13
|
+
toolkit.assert.ok(token >= 35, "Tokens from 0 to 34 reserved.");
|
|
14
|
+
return {
|
|
15
|
+
token,
|
|
16
|
+
encode,
|
|
17
|
+
decode
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function tlEncode(value, opts) {
|
|
22
|
+
return new BinaryWriter.BinaryWriter(opts).writeObject(value).getBuffer();
|
|
23
|
+
}
|
|
24
|
+
function tlDecode(buffer, opts) {
|
|
25
|
+
return new BinaryWriter.BinaryReader(buffer, opts).readObject();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
exports.BinaryReader = BinaryWriter.BinaryReader;
|
|
29
|
+
exports.BinaryWriter = BinaryWriter.BinaryWriter;
|
|
30
|
+
exports.CORE_TYPES = BinaryWriter.CORE_TYPES;
|
|
31
|
+
exports.MAX_BUFFER_SIZE = BinaryWriter.MAX_BUFFER_SIZE;
|
|
32
|
+
exports.createDictionary = BinaryWriter.createDictionary;
|
|
33
|
+
exports.createExtension = createExtension;
|
|
34
|
+
exports.tlDecode = tlDecode;
|
|
35
|
+
exports.tlEncode = tlEncode;
|
|
36
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/extension.ts","../src/index.ts"],"sourcesContent":["import { assert } from '@andrew_l/toolkit';\nimport type { BinaryReader } from './BinaryReader';\nimport type { BinaryWriter } from './BinaryWriter';\n\nexport type EncodeHandler = (this: BinaryWriter, value: any) => void;\n\nexport type DecodeHandler = (this: BinaryReader) => any;\n\nexport interface TLExtension {\n token: number;\n encode: EncodeHandler;\n decode: DecodeHandler;\n}\n\nexport function createExtension(\n token: number,\n { encode, decode }: { encode: EncodeHandler; decode: DecodeHandler },\n): TLExtension {\n assert.ok(Math.trunc(token) === token, ' Token must be integer value.');\n\n assert.ok(\n token >= 0 && token <= 255,\n 'Token must be a 8 bit number. (0 - 255)',\n );\n\n assert.ok(token >= 35, 'Tokens from 0 to 34 reserved.');\n\n return {\n token,\n encode,\n decode,\n };\n}\n","import { BinaryReader, type BinaryReaderOptions } from './BinaryReader';\nimport { BinaryWriter, type BinaryWriterOptions } from './BinaryWriter';\n\nexport { BinaryReader, type BinaryReaderOptions } from './BinaryReader';\nexport { BinaryWriter, type BinaryWriterOptions } from './BinaryWriter';\nexport * from './constants';\nexport { createDictionary } from './dictionary';\nexport {\n createExtension,\n type DecodeHandler,\n type EncodeHandler,\n type TLExtension,\n} from './extension';\n\n/**\n * Encode any value into `Uint8Array`\n *\n * @example\n * const buffer = tlEncode(new Date(0));\n *\n * console.log(buffer); // Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0])\n *\n * @group Main\n */\nexport function tlEncode(\n value: unknown,\n opts?: BinaryWriterOptions,\n): Uint8Array {\n return new BinaryWriter(opts).writeObject(value).getBuffer();\n}\n\n/**\n * Decode value from `Uint8Array`\n *\n * @example\n * const buffer = new Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0]);\n * const value = tlDecode(buffer);\n *\n * console.log(value); // Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)\n *\n * @group Main\n */\nexport function tlDecode<T = any>(\n buffer: Uint8Array,\n opts?: BinaryReaderOptions,\n): T {\n return new BinaryReader(buffer, opts).readObject();\n}\n"],"names":["assert","BinaryWriter","BinaryReader"],"mappings":";;;;;;AAcO,SAAS,eACd,CAAA,KAAA,EACA,EAAE,MAAA,EAAQ,QACG,EAAA;AACb,EAAAA,cAAA,CAAO,GAAG,IAAK,CAAA,KAAA,CAAM,KAAK,CAAA,KAAM,OAAO,+BAA+B,CAAA,CAAA;AAEtE,EAAOA,cAAA,CAAA,EAAA;AAAA,IACL,KAAA,IAAS,KAAK,KAAS,IAAA,GAAA;AAAA,IACvB,yCAAA;AAAA,GACF,CAAA;AAEA,EAAOA,cAAA,CAAA,EAAA,CAAG,KAAS,IAAA,EAAA,EAAI,+BAA+B,CAAA,CAAA;AAEtD,EAAO,OAAA;AAAA,IACL,KAAA;AAAA,IACA,MAAA;AAAA,IACA,MAAA;AAAA,GACF,CAAA;AACF;;ACRgB,SAAA,QAAA,CACd,OACA,IACY,EAAA;AACZ,EAAA,OAAO,IAAIC,yBAAa,CAAA,IAAI,EAAE,WAAY,CAAA,KAAK,EAAE,SAAU,EAAA,CAAA;AAC7D,CAAA;AAagB,SAAA,QAAA,CACd,QACA,IACG,EAAA;AACH,EAAA,OAAO,IAAIC,yBAAA,CAAa,MAAQ,EAAA,IAAI,EAAE,UAAW,EAAA,CAAA;AACnD;;;;;;;;;;;"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { B as BinaryWriterOptions, a as BinaryReaderOptions } from './shared/tl-pack.C2PEqnNg.cjs';
|
|
2
|
+
export { b as BinaryReader, c as BinaryWriter, C as CORE_TYPES, D as DecodeHandler, E as EncodeHandler, M as MAX_BUFFER_SIZE, T as TLExtension, d as createDictionary, e as createExtension } from './shared/tl-pack.C2PEqnNg.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Encode any value into `Uint8Array`
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const buffer = tlEncode(new Date(0));
|
|
9
|
+
*
|
|
10
|
+
* console.log(buffer); // Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0])
|
|
11
|
+
*
|
|
12
|
+
* @group Main
|
|
13
|
+
*/
|
|
14
|
+
declare function tlEncode(value: unknown, opts?: BinaryWriterOptions): Uint8Array;
|
|
15
|
+
/**
|
|
16
|
+
* Decode value from `Uint8Array`
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const buffer = new Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
20
|
+
* const value = tlDecode(buffer);
|
|
21
|
+
*
|
|
22
|
+
* console.log(value); // Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)
|
|
23
|
+
*
|
|
24
|
+
* @group Main
|
|
25
|
+
*/
|
|
26
|
+
declare function tlDecode<T = any>(buffer: Uint8Array, opts?: BinaryReaderOptions): T;
|
|
27
|
+
|
|
28
|
+
export { BinaryReaderOptions, BinaryWriterOptions, tlDecode, tlEncode };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { B as BinaryWriterOptions, a as BinaryReaderOptions } from './shared/tl-pack.C2PEqnNg.mjs';
|
|
2
|
+
export { b as BinaryReader, c as BinaryWriter, C as CORE_TYPES, D as DecodeHandler, E as EncodeHandler, M as MAX_BUFFER_SIZE, T as TLExtension, d as createDictionary, e as createExtension } from './shared/tl-pack.C2PEqnNg.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Encode any value into `Uint8Array`
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const buffer = tlEncode(new Date(0));
|
|
9
|
+
*
|
|
10
|
+
* console.log(buffer); // Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0])
|
|
11
|
+
*
|
|
12
|
+
* @group Main
|
|
13
|
+
*/
|
|
14
|
+
declare function tlEncode(value: unknown, opts?: BinaryWriterOptions): Uint8Array;
|
|
15
|
+
/**
|
|
16
|
+
* Decode value from `Uint8Array`
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const buffer = new Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
20
|
+
* const value = tlDecode(buffer);
|
|
21
|
+
*
|
|
22
|
+
* console.log(value); // Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)
|
|
23
|
+
*
|
|
24
|
+
* @group Main
|
|
25
|
+
*/
|
|
26
|
+
declare function tlDecode<T = any>(buffer: Uint8Array, opts?: BinaryReaderOptions): T;
|
|
27
|
+
|
|
28
|
+
export { BinaryReaderOptions, BinaryWriterOptions, tlDecode, tlEncode };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { B as BinaryWriterOptions, a as BinaryReaderOptions } from './shared/tl-pack.C2PEqnNg.js';
|
|
2
|
+
export { b as BinaryReader, c as BinaryWriter, C as CORE_TYPES, D as DecodeHandler, E as EncodeHandler, M as MAX_BUFFER_SIZE, T as TLExtension, d as createDictionary, e as createExtension } from './shared/tl-pack.C2PEqnNg.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Encode any value into `Uint8Array`
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const buffer = tlEncode(new Date(0));
|
|
9
|
+
*
|
|
10
|
+
* console.log(buffer); // Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0])
|
|
11
|
+
*
|
|
12
|
+
* @group Main
|
|
13
|
+
*/
|
|
14
|
+
declare function tlEncode(value: unknown, opts?: BinaryWriterOptions): Uint8Array;
|
|
15
|
+
/**
|
|
16
|
+
* Decode value from `Uint8Array`
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const buffer = new Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
20
|
+
* const value = tlDecode(buffer);
|
|
21
|
+
*
|
|
22
|
+
* console.log(value); // Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)
|
|
23
|
+
*
|
|
24
|
+
* @group Main
|
|
25
|
+
*/
|
|
26
|
+
declare function tlDecode<T = any>(buffer: Uint8Array, opts?: BinaryReaderOptions): T;
|
|
27
|
+
|
|
28
|
+
export { BinaryReaderOptions, BinaryWriterOptions, tlDecode, tlEncode };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { B as BinaryWriter, a as BinaryReader } from './shared/tl-pack.CkwM-fIb.mjs';
|
|
2
|
+
export { C as CORE_TYPES, M as MAX_BUFFER_SIZE, c as createDictionary } from './shared/tl-pack.CkwM-fIb.mjs';
|
|
3
|
+
import { assert } from '@andrew_l/toolkit';
|
|
4
|
+
import 'pako';
|
|
5
|
+
|
|
6
|
+
function createExtension(token, { encode, decode }) {
|
|
7
|
+
assert.ok(Math.trunc(token) === token, " Token must be integer value.");
|
|
8
|
+
assert.ok(
|
|
9
|
+
token >= 0 && token <= 255,
|
|
10
|
+
"Token must be a 8 bit number. (0 - 255)"
|
|
11
|
+
);
|
|
12
|
+
assert.ok(token >= 35, "Tokens from 0 to 34 reserved.");
|
|
13
|
+
return {
|
|
14
|
+
token,
|
|
15
|
+
encode,
|
|
16
|
+
decode
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function tlEncode(value, opts) {
|
|
21
|
+
return new BinaryWriter(opts).writeObject(value).getBuffer();
|
|
22
|
+
}
|
|
23
|
+
function tlDecode(buffer, opts) {
|
|
24
|
+
return new BinaryReader(buffer, opts).readObject();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { BinaryReader, BinaryWriter, createExtension, tlDecode, tlEncode };
|
|
28
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/extension.ts","../src/index.ts"],"sourcesContent":["import { assert } from '@andrew_l/toolkit';\nimport type { BinaryReader } from './BinaryReader';\nimport type { BinaryWriter } from './BinaryWriter';\n\nexport type EncodeHandler = (this: BinaryWriter, value: any) => void;\n\nexport type DecodeHandler = (this: BinaryReader) => any;\n\nexport interface TLExtension {\n token: number;\n encode: EncodeHandler;\n decode: DecodeHandler;\n}\n\nexport function createExtension(\n token: number,\n { encode, decode }: { encode: EncodeHandler; decode: DecodeHandler },\n): TLExtension {\n assert.ok(Math.trunc(token) === token, ' Token must be integer value.');\n\n assert.ok(\n token >= 0 && token <= 255,\n 'Token must be a 8 bit number. (0 - 255)',\n );\n\n assert.ok(token >= 35, 'Tokens from 0 to 34 reserved.');\n\n return {\n token,\n encode,\n decode,\n };\n}\n","import { BinaryReader, type BinaryReaderOptions } from './BinaryReader';\nimport { BinaryWriter, type BinaryWriterOptions } from './BinaryWriter';\n\nexport { BinaryReader, type BinaryReaderOptions } from './BinaryReader';\nexport { BinaryWriter, type BinaryWriterOptions } from './BinaryWriter';\nexport * from './constants';\nexport { createDictionary } from './dictionary';\nexport {\n createExtension,\n type DecodeHandler,\n type EncodeHandler,\n type TLExtension,\n} from './extension';\n\n/**\n * Encode any value into `Uint8Array`\n *\n * @example\n * const buffer = tlEncode(new Date(0));\n *\n * console.log(buffer); // Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0])\n *\n * @group Main\n */\nexport function tlEncode(\n value: unknown,\n opts?: BinaryWriterOptions,\n): Uint8Array {\n return new BinaryWriter(opts).writeObject(value).getBuffer();\n}\n\n/**\n * Decode value from `Uint8Array`\n *\n * @example\n * const buffer = new Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0]);\n * const value = tlDecode(buffer);\n *\n * console.log(value); // Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)\n *\n * @group Main\n */\nexport function tlDecode<T = any>(\n buffer: Uint8Array,\n opts?: BinaryReaderOptions,\n): T {\n return new BinaryReader(buffer, opts).readObject();\n}\n"],"names":[],"mappings":";;;;;AAcO,SAAS,eACd,CAAA,KAAA,EACA,EAAE,MAAA,EAAQ,QACG,EAAA;AACb,EAAA,MAAA,CAAO,GAAG,IAAK,CAAA,KAAA,CAAM,KAAK,CAAA,KAAM,OAAO,+BAA+B,CAAA,CAAA;AAEtE,EAAO,MAAA,CAAA,EAAA;AAAA,IACL,KAAA,IAAS,KAAK,KAAS,IAAA,GAAA;AAAA,IACvB,yCAAA;AAAA,GACF,CAAA;AAEA,EAAO,MAAA,CAAA,EAAA,CAAG,KAAS,IAAA,EAAA,EAAI,+BAA+B,CAAA,CAAA;AAEtD,EAAO,OAAA;AAAA,IACL,KAAA;AAAA,IACA,MAAA;AAAA,IACA,MAAA;AAAA,GACF,CAAA;AACF;;ACRgB,SAAA,QAAA,CACd,OACA,IACY,EAAA;AACZ,EAAA,OAAO,IAAI,YAAa,CAAA,IAAI,EAAE,WAAY,CAAA,KAAK,EAAE,SAAU,EAAA,CAAA;AAC7D,CAAA;AAagB,SAAA,QAAA,CACd,QACA,IACG,EAAA;AACH,EAAA,OAAO,IAAI,YAAA,CAAa,MAAQ,EAAA,IAAI,EAAE,UAAW,EAAA,CAAA;AACnD;;;;"}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
declare enum CORE_TYPES {
|
|
2
|
+
None = 0,
|
|
3
|
+
Binary = 1,
|
|
4
|
+
BoolFalse = 2,
|
|
5
|
+
BoolTrue = 3,
|
|
6
|
+
Null = 4,
|
|
7
|
+
Date = 5,
|
|
8
|
+
Vector = 6,
|
|
9
|
+
VectorDynamic = 7,
|
|
10
|
+
Int32 = 8,
|
|
11
|
+
Int16 = 9,
|
|
12
|
+
Int8 = 10,
|
|
13
|
+
UInt32 = 11,
|
|
14
|
+
UInt16 = 12,
|
|
15
|
+
UInt8 = 13,
|
|
16
|
+
Float = 14,
|
|
17
|
+
Double = 15,
|
|
18
|
+
Map = 16,
|
|
19
|
+
DictValue = 17,
|
|
20
|
+
DictIndex = 18,
|
|
21
|
+
String = 19,
|
|
22
|
+
Repeat = 20,
|
|
23
|
+
Checksum = 21,
|
|
24
|
+
GZIP = 25
|
|
25
|
+
}
|
|
26
|
+
declare const MAX_BUFFER_SIZE = 2144337920;
|
|
27
|
+
|
|
28
|
+
declare function createDictionary(values?: string[]): Dictionary;
|
|
29
|
+
declare class Dictionary {
|
|
30
|
+
private _count;
|
|
31
|
+
private _wordToIndex;
|
|
32
|
+
private _words;
|
|
33
|
+
private _offset;
|
|
34
|
+
constructor(values?: string[], offset?: number);
|
|
35
|
+
get size(): number;
|
|
36
|
+
/**
|
|
37
|
+
* Returns inserted index or nothing
|
|
38
|
+
*/
|
|
39
|
+
maybeInsert(word: string): number | null;
|
|
40
|
+
getValue(index: number): string | null;
|
|
41
|
+
getIndex(value: string): number | null;
|
|
42
|
+
hasValue(value: string): boolean;
|
|
43
|
+
hasIndex(index: number): boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface BinaryWriterOptions {
|
|
47
|
+
gzip?: boolean;
|
|
48
|
+
dictionary?: string[] | Dictionary;
|
|
49
|
+
extensions?: TLExtension[];
|
|
50
|
+
}
|
|
51
|
+
declare class BinaryWriter {
|
|
52
|
+
private withGzip;
|
|
53
|
+
private target;
|
|
54
|
+
private dictionary?;
|
|
55
|
+
private dictionaryExtended;
|
|
56
|
+
private extensions;
|
|
57
|
+
private _last;
|
|
58
|
+
private _checksumOffset;
|
|
59
|
+
private _repeat?;
|
|
60
|
+
offset: number;
|
|
61
|
+
constructor(options?: BinaryWriterOptions);
|
|
62
|
+
allocate(size: number): this;
|
|
63
|
+
private makeRoom;
|
|
64
|
+
get safeEnd(): number;
|
|
65
|
+
getBuffer(): Uint8Array;
|
|
66
|
+
writeByte(value: number): this;
|
|
67
|
+
writeBool(value: boolean): this;
|
|
68
|
+
writeNull(): this;
|
|
69
|
+
writeInt32(value: number, signed?: boolean): this;
|
|
70
|
+
writeInt16(value: number, signed?: boolean): this;
|
|
71
|
+
writeInt8(value: number, signed?: boolean): this;
|
|
72
|
+
writeFloat(value: number): this;
|
|
73
|
+
writeDouble(value: number): this;
|
|
74
|
+
writeDate(value: number | Date): this;
|
|
75
|
+
writeString(value: string): this;
|
|
76
|
+
writeChecksum(withConstructor?: boolean): this;
|
|
77
|
+
writeBytes(value: Uint8Array): this;
|
|
78
|
+
writeLength(value: number): this;
|
|
79
|
+
writeVector(value: Array<any>): this;
|
|
80
|
+
writeMap(object: Record<string, any>): this;
|
|
81
|
+
wireDictionary(value: string): this;
|
|
82
|
+
writeGzip(value: Uint8Array | ArrayBuffer): this;
|
|
83
|
+
encode(value: any): Uint8Array;
|
|
84
|
+
startDynamicVector(): this;
|
|
85
|
+
endDynamicVector(): this;
|
|
86
|
+
private _writeCustom;
|
|
87
|
+
writeObject(value: any): this;
|
|
88
|
+
writeObjectGzip(value: any): this;
|
|
89
|
+
private writeCore;
|
|
90
|
+
private writeRepeat;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
type EncodeHandler = (this: BinaryWriter, value: any) => void;
|
|
94
|
+
type DecodeHandler = (this: BinaryReader) => any;
|
|
95
|
+
interface TLExtension {
|
|
96
|
+
token: number;
|
|
97
|
+
encode: EncodeHandler;
|
|
98
|
+
decode: DecodeHandler;
|
|
99
|
+
}
|
|
100
|
+
declare function createExtension(token: number, { encode, decode }: {
|
|
101
|
+
encode: EncodeHandler;
|
|
102
|
+
decode: DecodeHandler;
|
|
103
|
+
}): TLExtension;
|
|
104
|
+
|
|
105
|
+
interface BinaryReaderOptions {
|
|
106
|
+
dictionary?: string[] | Dictionary;
|
|
107
|
+
extensions?: TLExtension[];
|
|
108
|
+
}
|
|
109
|
+
declare class BinaryReader {
|
|
110
|
+
private target;
|
|
111
|
+
private _last?;
|
|
112
|
+
private _lastObject?;
|
|
113
|
+
private dictionary?;
|
|
114
|
+
private dictionaryExtended;
|
|
115
|
+
private extensions;
|
|
116
|
+
private _repeat?;
|
|
117
|
+
private _checksumOffset;
|
|
118
|
+
offset: number;
|
|
119
|
+
length: number;
|
|
120
|
+
/**
|
|
121
|
+
* Small utility class to read binary data.
|
|
122
|
+
*/
|
|
123
|
+
constructor(data: Uint8Array, options?: BinaryReaderOptions);
|
|
124
|
+
readByte(): number;
|
|
125
|
+
readInt32(signed?: boolean): number;
|
|
126
|
+
readInt16(signed?: boolean): number;
|
|
127
|
+
readInt8(signed?: boolean): number;
|
|
128
|
+
/**
|
|
129
|
+
* Reads a real floating point (4 bytes) value.
|
|
130
|
+
* @returns {number}
|
|
131
|
+
*/
|
|
132
|
+
readFloat(): number;
|
|
133
|
+
/**
|
|
134
|
+
* Reads a real floating point (8 bytes) value.
|
|
135
|
+
* @returns {BigInteger}
|
|
136
|
+
*/
|
|
137
|
+
readDouble(): number;
|
|
138
|
+
/**
|
|
139
|
+
* Read the given amount of bytes, or -1 to read all remaining.
|
|
140
|
+
* @param length {number}
|
|
141
|
+
*/
|
|
142
|
+
assertRead(length: number): void;
|
|
143
|
+
assertConstructor(constructorId: CORE_TYPES): void;
|
|
144
|
+
/**
|
|
145
|
+
* Gets the byte array representing the current buffer as a whole.
|
|
146
|
+
*/
|
|
147
|
+
getBuffer(): Uint8Array;
|
|
148
|
+
readNull(): null;
|
|
149
|
+
readLength(): number;
|
|
150
|
+
readAll(): any[];
|
|
151
|
+
readBytes(): Uint8Array;
|
|
152
|
+
/**
|
|
153
|
+
* Reads encoded string.
|
|
154
|
+
*/
|
|
155
|
+
readString(): string;
|
|
156
|
+
/**
|
|
157
|
+
* Reads a boolean value.
|
|
158
|
+
*/
|
|
159
|
+
readBool(): boolean;
|
|
160
|
+
/**
|
|
161
|
+
* Reads and converts Unix time
|
|
162
|
+
* into a Javascript {Date} object.
|
|
163
|
+
*/
|
|
164
|
+
readDate(): Date;
|
|
165
|
+
/**
|
|
166
|
+
* Reads a object.
|
|
167
|
+
*/
|
|
168
|
+
readObject(): any;
|
|
169
|
+
readObjectGzip(): any;
|
|
170
|
+
readGzip(): any;
|
|
171
|
+
private readCore;
|
|
172
|
+
getDictionaryValue(index: number): string | null;
|
|
173
|
+
readDictionary(): null | string;
|
|
174
|
+
readMap(checkConstructor?: boolean): Record<string, any>;
|
|
175
|
+
decode<T = any>(value: Uint8Array): T;
|
|
176
|
+
/**
|
|
177
|
+
* Reads a vector (a list) of objects.
|
|
178
|
+
*/
|
|
179
|
+
readVector<T = any>(checkConstructor?: boolean): T[];
|
|
180
|
+
/**
|
|
181
|
+
* Reads a vector (a list) of objects.
|
|
182
|
+
*/
|
|
183
|
+
readVectorDynamic<T>(checkConstructor?: boolean): T[];
|
|
184
|
+
readChecksum(checkConstructor?: boolean): void;
|
|
185
|
+
/**
|
|
186
|
+
* Tells the current position on the stream.
|
|
187
|
+
*/
|
|
188
|
+
tellPosition(): number;
|
|
189
|
+
/**
|
|
190
|
+
* Sets the current position on the stream.
|
|
191
|
+
*/
|
|
192
|
+
setPosition(position: number): void;
|
|
193
|
+
/**
|
|
194
|
+
* Seeks the stream position given an offset from the current position.
|
|
195
|
+
* The offset may be negative.
|
|
196
|
+
*/
|
|
197
|
+
seek(offset: number): void;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export { type BinaryWriterOptions as B, CORE_TYPES as C, type DecodeHandler as D, type EncodeHandler as E, MAX_BUFFER_SIZE as M, type TLExtension as T, type BinaryReaderOptions as a, BinaryReader as b, BinaryWriter as c, createDictionary as d, createExtension as e };
|