@andrew_l/tl-pack 0.2.14 → 0.2.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -15,8 +15,10 @@ Binary serialization library, inspired by the TL (Type Language) format, created
15
15
 
16
16
  - **No Schema Required**: Unlike traditional serialization formats, this version does not require a predefined schema for object serialization.
17
17
  - **Compact & Fast**: Designed to be lightweight and fast, with smaller binary output.
18
+ - **Type-Safe Structures**: Define strongly typed binary structures with validation and versioning.
18
19
  - **Custom Extension Support**: Easily extend serialization to custom types.
19
20
  - **Stream Support**: Supports streaming serialization/deserialization in Node.js.
21
+ - **Version Compatibility**: Built-in version checking for structure evolution.
20
22
 
21
23
  ## 🚀 Example Usage
22
24
 
@@ -64,6 +66,147 @@ console.log(reader.readObject());
64
66
  */
65
67
  ```
66
68
 
69
+ ### Type-Safe Structures with `defineStructure`
70
+
71
+ Define reusable, type-safe binary structures with validation and versioning:
72
+
73
+ ```ts
74
+ import { defineStructure, type Structure } from '@andrew_l/tl-pack';
75
+
76
+ // Define a User structure
77
+ const User = defineStructure({
78
+ name: 'User',
79
+ version: 1,
80
+ checksum: true,
81
+ properties: {
82
+ id: { type: Number, required: true },
83
+ name: { type: String, required: true },
84
+ email: { type: String, required: false },
85
+ isActive: { type: Boolean, required: true },
86
+ createdAt: { type: Date, required: true },
87
+ tags: { type: Array as Structure.PropType<string[]>, required: false },
88
+ metadata: { type: Object, required: false },
89
+ },
90
+ });
91
+
92
+ // Create and serialize a user
93
+ const user = new User({
94
+ id: 123,
95
+ name: 'John Doe',
96
+ email: 'john@example.com',
97
+ isActive: true,
98
+ createdAt: new Date(),
99
+ tags: ['admin', 'verified'],
100
+ metadata: {
101
+ theme: 'dark',
102
+ lang: 'en',
103
+ },
104
+ });
105
+
106
+ // Serialize to binary
107
+ const buffer = user.toBuffer();
108
+ console.log(`Serialized size: ${buffer.length} bytes`);
109
+
110
+ // Deserialize from binary
111
+ const restored = User.fromBuffer(buffer);
112
+ console.log(restored);
113
+ /**
114
+ {
115
+ id: 123,
116
+ name: 'John Doe',
117
+ email: 'john@example.com',
118
+ isActive: true,
119
+ createdAt: 2023-07-03T12:22:26.000Z,
120
+ tags: ['admin', 'verified'],
121
+ metadata: { theme: 'dark', lang: 'en' }
122
+ }
123
+ */
124
+ ```
125
+
126
+ ### Nested Structures
127
+
128
+ Create complex hierarchical data structures:
129
+
130
+ ```javascript
131
+ // Define an Address structure
132
+ const Address = defineStructure({
133
+ name: 'Address',
134
+ version: 1,
135
+ properties: {
136
+ street: { type: String, required: true },
137
+ city: { type: String, required: true },
138
+ zipCode: { type: String, required: true },
139
+ country: { type: String, required: false },
140
+ },
141
+ });
142
+
143
+ // Define a Person with nested Address
144
+ const Person = defineStructure({
145
+ name: 'Person',
146
+ version: 2,
147
+ checksum: true,
148
+ properties: {
149
+ name: { type: String, required: true },
150
+ age: { type: Number, required: true },
151
+ address: { type: Address, required: false },
152
+ contacts: { type: Array, required: false }, // Array of other persons
153
+ },
154
+ });
155
+
156
+ const person = new Person({
157
+ name: 'Alice Smith',
158
+ age: 30,
159
+ address: {
160
+ street: '123 Main St',
161
+ city: 'New York',
162
+ zipCode: '10001',
163
+ country: 'USA',
164
+ },
165
+ contacts: [
166
+ { name: 'Bob', age: 25 },
167
+ { name: 'Carol', age: 35 },
168
+ ],
169
+ });
170
+
171
+ const buffer = person.toBuffer();
172
+ const restored = Person.fromBuffer(buffer);
173
+ ```
174
+
175
+ ### API Response Caching
176
+
177
+ Optimize API responses with binary serialization:
178
+
179
+ ```javascript
180
+ const APIResponse = defineStructure({
181
+ name: 'APIResponse',
182
+ version: 1,
183
+ properties: {
184
+ status: { type: Number, required: true },
185
+ data: { type: Object, required: false },
186
+ headers: { type: Object, required: false },
187
+ timestamp: { type: Date, required: true },
188
+ cacheKey: { type: String, required: true },
189
+ },
190
+ });
191
+
192
+ // Cache API response
193
+ const response = new APIResponse({
194
+ status: 200,
195
+ data: {
196
+ users: [
197
+ /*...*/
198
+ ],
199
+ total: 1250,
200
+ },
201
+ headers: { 'content-type': 'application/json' },
202
+ timestamp: new Date(),
203
+ cacheKey: 'users_page_1',
204
+ });
205
+
206
+ // 60% smaller than JSON in many cases
207
+ const compressed = response.toBuffer();
208
+ ```
209
+
67
210
  ### Stream Example (Node.js Only)
68
211
 
69
212
  ```javascript
@@ -89,6 +232,58 @@ decode.on('data', data => console.log('stream', data));
89
232
  decode.on('error', console.error);
90
233
  ```
91
234
 
235
+ ## Structure Features
236
+
237
+ ### Version Management
238
+
239
+ Structures support versioning for backward compatibility:
240
+
241
+ ```javascript
242
+ // Version 1
243
+ const UserV1 = defineStructure({
244
+ name: 'User',
245
+ version: 1,
246
+ properties: {
247
+ name: { type: String, required: true },
248
+ email: { type: String, required: true },
249
+ },
250
+ });
251
+
252
+ // Version 2 - Added optional fields
253
+ const UserV2 = defineStructure({
254
+ name: 'User',
255
+ version: 2,
256
+ properties: {
257
+ name: { type: String, required: true },
258
+ email: { type: String, required: true },
259
+ createdAt: { type: Date, required: false },
260
+ isVerified: { type: Boolean, required: false },
261
+ },
262
+ });
263
+
264
+ // Reading V1 data with V2 structure will fail with version mismatch
265
+ // This ensures data integrity across application updates
266
+ ```
267
+
268
+ ### Checksum Validation
269
+
270
+ Enable checksum validation for data integrity:
271
+
272
+ ```javascript
273
+ const SecureData = defineStructure({
274
+ name: 'SecureData',
275
+ version: 1,
276
+ checksum: true, // Enables automatic checksum validation
277
+ properties: {
278
+ sensitiveInfo: { type: String, required: true },
279
+ timestamp: { type: Date, required: true },
280
+ },
281
+ });
282
+
283
+ // Checksum is automatically calculated during serialization
284
+ // and validated during deserialization
285
+ ```
286
+
92
287
  ## Supported Types
93
288
 
94
289
  | Constructor ID | Byte Size |
@@ -112,6 +307,7 @@ decode.on('error', console.error);
112
307
  | String | 5 + sizeof(object) |
113
308
  | Repeat | 5 |
114
309
  | GZIP | 5 + sizeof(object) |
310
+ | Structure | 4 + sizeof(object) |
115
311
 
116
312
  ## Custom Types
117
313
 
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const BinaryWriter = require('./shared/tl-pack.DfL54MjP.cjs');
3
+ const BinaryReader = require('./shared/tl-pack.CaWRi9zF.cjs');
4
4
  const toolkit = require('@andrew_l/toolkit');
5
5
  require('pako');
6
6
 
@@ -19,17 +19,19 @@ function createExtension(token, { encode, decode }) {
19
19
  }
20
20
 
21
21
  function tlEncode(value, opts) {
22
- return new BinaryWriter.BinaryWriter(opts).writeObject(value).getBuffer();
22
+ return new BinaryReader.BinaryWriter(opts).writeObject(value).getBuffer();
23
23
  }
24
24
  function tlDecode(buffer, opts) {
25
- return new BinaryWriter.BinaryReader(buffer, opts).readObject();
25
+ return new BinaryReader.BinaryReader(buffer, opts).readObject();
26
26
  }
27
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;
28
+ exports.BinaryReader = BinaryReader.BinaryReader;
29
+ exports.BinaryWriter = BinaryReader.BinaryWriter;
30
+ exports.CORE_TYPES = BinaryReader.CORE_TYPES;
31
+ exports.MAX_BUFFER_SIZE = BinaryReader.MAX_BUFFER_SIZE;
32
+ exports.Structure = BinaryReader.Structure;
33
+ exports.createDictionary = BinaryReader.createDictionary;
34
+ exports.defineStructure = BinaryReader.defineStructure;
33
35
  exports.createExtension = createExtension;
34
36
  exports.tlDecode = tlDecode;
35
37
  exports.tlEncode = tlEncode;
@@ -1 +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 === -1 || (token >= 0 && token <= 255),\n 'Token must be a 8 bit number. (0 - 255)',\n );\n\n assert.ok(token === -1 || 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,KAAU,KAAA,CAAA,CAAA,IAAO,KAAS,IAAA,CAAA,IAAK,KAAS,IAAA,GAAA;AAAA,IACxC,yCAAA;AAAA,GACF,CAAA;AAEA,EAAAA,cAAA,CAAO,EAAG,CAAA,KAAA,KAAU,CAAM,CAAA,IAAA,KAAA,IAAS,IAAI,+BAA+B,CAAA,CAAA;AAEtE,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;;;;;;;;;;;"}
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<T = any> = (this: BinaryWriter, value: T) => void;\n\nexport type DecodeHandler<T = any> = (this: BinaryReader) => T;\n\nexport interface TLExtension<T = any> {\n token: number;\n encode: EncodeHandler<T>;\n decode: DecodeHandler<T>;\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 === -1 || (token >= 0 && token <= 255),\n 'Token must be a 8 bit number. (0 - 255)',\n );\n\n assert.ok(token === -1 || 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\nexport * from './Structure.js';\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,KAAU,KAAA,CAAA,CAAA,IAAO,KAAS,IAAA,CAAA,IAAK,KAAS,IAAA,GAAA;AAAA,IACxC,yCAAA;AAAA,GACF,CAAA;AAEA,EAAAA,cAAA,CAAO,EAAG,CAAA,KAAA,KAAU,CAAM,CAAA,IAAA,KAAA,IAAS,IAAI,+BAA+B,CAAA,CAAA;AAEtE,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 CHANGED
@@ -1,5 +1,6 @@
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';
1
+ import { B as BinaryWriterOptions, a as BinaryReaderOptions } from './shared/tl-pack.BEGh3J6q.cjs';
2
+ export { b as BinaryReader, c as BinaryWriter, C as CORE_TYPES, D as DecodeHandler, f as DefineStructureOptions, E as EncodeHandler, M as MAX_BUFFER_SIZE, S as Structure, T as TLExtension, d as createDictionary, e as createExtension, g as defineStructure } from './shared/tl-pack.BEGh3J6q.cjs';
3
+ import '@andrew_l/toolkit';
3
4
 
4
5
  /**
5
6
  * Encode any value into `Uint8Array`
package/dist/index.d.mts CHANGED
@@ -1,5 +1,6 @@
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';
1
+ import { B as BinaryWriterOptions, a as BinaryReaderOptions } from './shared/tl-pack.BEGh3J6q.mjs';
2
+ export { b as BinaryReader, c as BinaryWriter, C as CORE_TYPES, D as DecodeHandler, f as DefineStructureOptions, E as EncodeHandler, M as MAX_BUFFER_SIZE, S as Structure, T as TLExtension, d as createDictionary, e as createExtension, g as defineStructure } from './shared/tl-pack.BEGh3J6q.mjs';
3
+ import '@andrew_l/toolkit';
3
4
 
4
5
  /**
5
6
  * Encode any value into `Uint8Array`
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
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';
1
+ import { B as BinaryWriterOptions, a as BinaryReaderOptions } from './shared/tl-pack.BEGh3J6q.js';
2
+ export { b as BinaryReader, c as BinaryWriter, C as CORE_TYPES, D as DecodeHandler, f as DefineStructureOptions, E as EncodeHandler, M as MAX_BUFFER_SIZE, S as Structure, T as TLExtension, d as createDictionary, e as createExtension, g as defineStructure } from './shared/tl-pack.BEGh3J6q.js';
3
+ import '@andrew_l/toolkit';
3
4
 
4
5
  /**
5
6
  * Encode any value into `Uint8Array`
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
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';
1
+ import { B as BinaryWriter, a as BinaryReader } from './shared/tl-pack.CxheH0R_.mjs';
2
+ export { C as CORE_TYPES, M as MAX_BUFFER_SIZE, S as Structure, c as createDictionary, d as defineStructure } from './shared/tl-pack.CxheH0R_.mjs';
3
3
  import { assert } from '@andrew_l/toolkit';
4
4
  import 'pako';
5
5
 
@@ -1 +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 === -1 || (token >= 0 && token <= 255),\n 'Token must be a 8 bit number. (0 - 255)',\n );\n\n assert.ok(token === -1 || 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,KAAU,KAAA,CAAA,CAAA,IAAO,KAAS,IAAA,CAAA,IAAK,KAAS,IAAA,GAAA;AAAA,IACxC,yCAAA;AAAA,GACF,CAAA;AAEA,EAAA,MAAA,CAAO,EAAG,CAAA,KAAA,KAAU,CAAM,CAAA,IAAA,KAAA,IAAS,IAAI,+BAA+B,CAAA,CAAA;AAEtE,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;;;;"}
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<T = any> = (this: BinaryWriter, value: T) => void;\n\nexport type DecodeHandler<T = any> = (this: BinaryReader) => T;\n\nexport interface TLExtension<T = any> {\n token: number;\n encode: EncodeHandler<T>;\n decode: DecodeHandler<T>;\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 === -1 || (token >= 0 && token <= 255),\n 'Token must be a 8 bit number. (0 - 255)',\n );\n\n assert.ok(token === -1 || 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\nexport * from './Structure.js';\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,KAAU,KAAA,CAAA,CAAA,IAAO,KAAS,IAAA,CAAA,IAAK,KAAS,IAAA,GAAA;AAAA,IACxC,yCAAA;AAAA,GACF,CAAA;AAEA,EAAA,MAAA,CAAO,EAAG,CAAA,KAAA,KAAU,CAAM,CAAA,IAAA,KAAA,IAAS,IAAI,+BAA+B,CAAA,CAAA;AAEtE,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;;;;"}