@andrew_l/tl-pack 0.1.3 → 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);
@@ -262,20 +262,21 @@ export class BinaryWriter {
262
262
  this.writeByte(CORE_TYPES.None);
263
263
  }
264
264
  _writeCustom(value) {
265
+ const start = this.offset;
266
+ this.allocate(1);
267
+ this.offset++;
265
268
  for (const ext of this.extensions.values()) {
266
- const result = ext.encode(value);
267
- if (result !== undefined) {
268
- const constructorId = coreType(result);
269
- if (constructorId === CORE_TYPES.None) {
270
- throw new TypeError(`Invalid encode extension = ${ext.token} type of ${value}`);
271
- }
272
- if (ext.token !== -1) {
273
- this.writeByte(ext.token);
274
- }
275
- 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;
276
276
  return true;
277
277
  }
278
278
  }
279
+ this.offset = start;
279
280
  return false;
280
281
  }
281
282
  writeObject(value) {
@@ -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.3",
3
+ "version": "0.1.4",
4
4
  "description": "Binary serialization library",
5
5
  "type": "module",
6
6
  "scripts": {