@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 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
- // Reserving token for ObjectId type
116
+ // Reserve token for ObjectId type
117
117
  createExtension(100, {
118
118
  encode(value) {
119
- if (value?._bsontype === 'ObjectID') {
120
- return value.toJSON(); // (string)
119
+ if (value instanceof ObjectId) {
120
+ this.writeBytes(value.id);
121
121
  }
122
122
  },
123
- // on client side, you possibly need a decoding as a plain string.
124
- decode(value) {
125
- return new ObjectId(value);
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
  ];
@@ -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 = this.readObject();
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 = this.readObject();
369
- value = ext.decode(value);
367
+ value = ext.decode.call(this);
370
368
  }
371
369
  else {
372
370
  value = this.readCore(constructorId);
@@ -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
- const result = ext.encode(value);
266
- if (result !== undefined) {
267
- const constructorId = coreType(result);
268
- if (constructorId === CORE_TYPES.None) {
269
- throw new TypeError(`Invalid encode extension = ${ext.token} type of ${value}`);
270
- }
271
- if (ext.token !== -1) {
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
  }
@@ -1,5 +1,7 @@
1
- export type EncodeHandler = (value: any) => string | number | undefined;
2
- export type DecodeHandler = (value: string | number) => any;
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrew_l/tl-pack",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Binary serialization library",
5
5
  "type": "module",
6
6
  "scripts": {