@andrew_l/tl-pack 0.1.2 → 0.1.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.
- package/README.md +7 -6
- package/dist/BinaryReader.js +2 -4
- package/dist/BinaryWriter.js +12 -11
- package/dist/extension.d.ts +4 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -113,16 +113,17 @@ import { BinaryWriter, BinaryReader, createExtension } from '@andrew_l/tl-pack';
|
|
|
113
113
|
const ObjectId = mongoose.Types.ObjectId;
|
|
114
114
|
|
|
115
115
|
const extensions = [
|
|
116
|
-
//
|
|
116
|
+
// Reserve token for ObjectId type
|
|
117
117
|
createExtension(100, {
|
|
118
118
|
encode(value) {
|
|
119
|
-
if (value
|
|
120
|
-
|
|
119
|
+
if (value instanceof ObjectId) {
|
|
120
|
+
this.writeBytes(value.id);
|
|
121
121
|
}
|
|
122
122
|
},
|
|
123
|
-
//
|
|
124
|
-
decode(
|
|
125
|
-
|
|
123
|
+
// in the browser you possibly need to decode as a plain string
|
|
124
|
+
decode() {
|
|
125
|
+
const bytes = this.readBytes();
|
|
126
|
+
return new ObjectId(bytes);
|
|
126
127
|
},
|
|
127
128
|
}),
|
|
128
129
|
];
|
package/dist/BinaryReader.js
CHANGED
|
@@ -209,8 +209,7 @@ export class BinaryReader {
|
|
|
209
209
|
const ext = this.extensions.get(constructorId);
|
|
210
210
|
let value;
|
|
211
211
|
if (ext) {
|
|
212
|
-
value =
|
|
213
|
-
value = ext.decode(value);
|
|
212
|
+
value = ext.decode.call(this);
|
|
214
213
|
}
|
|
215
214
|
else {
|
|
216
215
|
value = this._lastObject = this.readCore(constructorId);
|
|
@@ -365,8 +364,7 @@ export class BinaryReader {
|
|
|
365
364
|
const ext = this.extensions.get(constructorId);
|
|
366
365
|
let value;
|
|
367
366
|
if (ext) {
|
|
368
|
-
value =
|
|
369
|
-
value = ext.decode(value);
|
|
367
|
+
value = ext.decode.call(this);
|
|
370
368
|
}
|
|
371
369
|
else {
|
|
372
370
|
value = this.readCore(constructorId);
|
package/dist/BinaryWriter.js
CHANGED
|
@@ -221,6 +221,7 @@ export class BinaryWriter {
|
|
|
221
221
|
for (const key in object) {
|
|
222
222
|
if (object[key] === undefined)
|
|
223
223
|
continue;
|
|
224
|
+
this._last = noop;
|
|
224
225
|
this.wireDictionary(key);
|
|
225
226
|
this.writeObject(object[key]);
|
|
226
227
|
}
|
|
@@ -261,20 +262,21 @@ export class BinaryWriter {
|
|
|
261
262
|
this.writeByte(CORE_TYPES.None);
|
|
262
263
|
}
|
|
263
264
|
_writeCustom(value) {
|
|
265
|
+
const start = this.offset;
|
|
266
|
+
this.allocate(1);
|
|
267
|
+
this.offset++;
|
|
264
268
|
for (const ext of this.extensions.values()) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
this.writeByte(ext.token);
|
|
273
|
-
}
|
|
274
|
-
this.writeCore(constructorId, result);
|
|
269
|
+
ext.encode.call(this, value);
|
|
270
|
+
const processed = start < this.offset;
|
|
271
|
+
if (processed) {
|
|
272
|
+
const end = this.offset;
|
|
273
|
+
this.offset = start;
|
|
274
|
+
this.writeByte(ext.token);
|
|
275
|
+
this.offset = end;
|
|
275
276
|
return true;
|
|
276
277
|
}
|
|
277
278
|
}
|
|
279
|
+
this.offset = start;
|
|
278
280
|
return false;
|
|
279
281
|
}
|
|
280
282
|
writeObject(value) {
|
|
@@ -381,7 +383,6 @@ export class BinaryWriter {
|
|
|
381
383
|
}
|
|
382
384
|
this.offset = this._repeat.offset;
|
|
383
385
|
this._repeat.count++;
|
|
384
|
-
// console.log('repeat', { value: this.lastWrite, offset: this.offset });
|
|
385
386
|
this.writeLength(this._repeat.count);
|
|
386
387
|
}
|
|
387
388
|
}
|
package/dist/extension.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { BinaryReader } from './BinaryReader.js';
|
|
2
|
+
import type { BinaryWriter } from './BinaryWriter.js';
|
|
3
|
+
export type EncodeHandler = (this: BinaryWriter, value: any) => void;
|
|
4
|
+
export type DecodeHandler = (this: BinaryReader) => any;
|
|
3
5
|
export interface TLExtension {
|
|
4
6
|
token: number;
|
|
5
7
|
encode: EncodeHandler;
|