@andrew_l/tl-pack 0.0.5 → 0.0.6

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.
Files changed (2) hide show
  1. package/README.md +138 -0
  2. package/package.json +10 -2
package/README.md ADDED
@@ -0,0 +1,138 @@
1
+ # TLPack for JavaScript/NodeJs
2
+
3
+ ![license](https://img.shields.io/npm/l/%40andrew_l%2Ftl-pack)![npm version](https://img.shields.io/npm/v/%40andrew_l%2Ftl-pack)![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40andrew_l%2Ftl-pack)
4
+
5
+ This library is another implementation of binary serialization like **MessagePack** inspired by TL (Type Language) developed by old VK team.
6
+
7
+ You don't needed in schema for data serialization/deserialization unlike official TL implementation at least in this version.
8
+
9
+ ## Benchmark
10
+
11
+ A bit faster and a bit more compact than **@msgpack/msgpack**
12
+
13
+ ## Synopsis
14
+
15
+ ```javascript
16
+ import { BinaryWriter, BinaryReader } from '@andrew_l/tl-pack';
17
+
18
+ const writer = new BinaryWriter();
19
+
20
+ // use to compress this part of data
21
+ // writer.writeObjectGzip
22
+ writer.writeObject({
23
+ null: null,
24
+ uint8: 255,
25
+ uint16: 256,
26
+ uint32: 65536,
27
+ int8: -128,
28
+ int16: -32768,
29
+ int32: -2147483648,
30
+ double: 3.14,
31
+ string: 'Hello world',
32
+ vector: [1, 2, 3, 4, 5, { text: 'hi' }],
33
+ map: { foo: 'bar' },
34
+ date: new Date(),
35
+ });
36
+
37
+ const reader = new BinaryReader(writer.getBuffer());
38
+
39
+ console.log(reader.readObject());
40
+ ```
41
+
42
+ ## Supported Types
43
+
44
+ | Constructor ID | Byte Size |
45
+ | -------------- | ------------------ |
46
+ | Binary | 5 + sizeof(object) |
47
+ | BoolFalse | 1 |
48
+ | BoolTrue | 1 |
49
+ | Null | 1 |
50
+ | Date | 4 |
51
+ | Vector | 5 + n \* sizeof(n) |
52
+ | VectorDynamic | 2 + n \* sizeof(n) |
53
+ | Int8 | 1 |
54
+ | Int16 | 2 |
55
+ | Int32 | 4 |
56
+ | UInt8 | 1 |
57
+ | UInt16 | 2 |
58
+ | UInt32 | 4 |
59
+ | Float | 4 |
60
+ | Double | 8 |
61
+ | Map | 2 + sizeof(object) |
62
+ | String | 5 + sizeof(object) |
63
+ | GZIP | 5 + sizeof(object) |
64
+
65
+ ## Stream Example (NodeJs Only)
66
+
67
+ ```javascript
68
+ import { Readable } from 'node:stream';
69
+ import { TLEncode, TLDecode } from '@andrew_l/tl-pack/stream';
70
+
71
+ const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
72
+
73
+ const dataStream = new Readable({ objectMode: true });
74
+
75
+ dataStream._read = () => {
76
+ const chunk = values.shift();
77
+
78
+ dataStream.push(chunk || null);
79
+ };
80
+
81
+ const encode = new TLEncode();
82
+ const decode = new TLDecode();
83
+
84
+ dataStream.pipe(encode).pipe(decode);
85
+
86
+ decode.on('data', (data) => console.log('stream', data));
87
+ decode.on('error', console.error);
88
+ ```
89
+
90
+ ## Custom Types
91
+
92
+ ```javascript
93
+ import mongoose from 'mongoose';
94
+ import { BinaryWriter, BinaryReader, createExtension } from '@andrew_l/tl-pack';
95
+
96
+ const ObjectId = mongoose.Types.ObjectId;
97
+
98
+ const extensions = [
99
+ // Reserving token - 100 for ObjectId types
100
+ createExtension(100, {
101
+ encode(value) {
102
+ if (value?._bsontype === 'ObjectID') {
103
+ return value.toJSON(); // (string)
104
+ }
105
+ },
106
+ // on client side, you possibly need a decoding as a plain string.
107
+ decode(value) {
108
+ return new ObjectId(value);
109
+ },
110
+ }),
111
+ ];
112
+
113
+ const writer = new BinaryWriter({ extensions });
114
+
115
+ writer.writeObject({
116
+ _id: new Object(),
117
+ firstName: 'Andrew',
118
+ lastName: 'L.',
119
+ });
120
+
121
+ const reader = new BinaryReader(writer.getBuffer(), { extensions });
122
+
123
+ console.log(reader.readObject());
124
+ ```
125
+
126
+ ## Dictionary
127
+
128
+ 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.
129
+
130
+ **Static**
131
+ Dictionary with you initialize a BinaryWriter/BinaryReader.
132
+
133
+ **Dynamic**
134
+ Dictionary which appends while encoding and decoding keys of an object (Map).
135
+
136
+ ## Production
137
+
138
+ No way!
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrew_l/tl-pack",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -10,10 +10,18 @@
10
10
  "keywords": [],
11
11
  "author": "Andrew L.",
12
12
  "license": "ISC",
13
- "types": "./dist",
14
13
  "files": [
15
14
  "dist/**"
16
15
  ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/men232/tl-pack.git"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/men232/tl-pack/issues"
22
+ },
23
+ "main": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
17
25
  "exports": {
18
26
  ".": {
19
27
  "import": "./dist/index.js",