@andrew_l/tl-pack 0.1.1 → 0.1.2

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
@@ -76,6 +76,7 @@ console.log(reader.readObject());
76
76
  | Double | 8 |
77
77
  | Map | 2 + sizeof(object) |
78
78
  | String | 5 + sizeof(object) |
79
+ | Repeat | 5 |
79
80
  | GZIP | 5 + sizeof(object) |
80
81
 
81
82
  ## Stream Example (NodeJs Only)
@@ -112,7 +113,7 @@ import { BinaryWriter, BinaryReader, createExtension } from '@andrew_l/tl-pack';
112
113
  const ObjectId = mongoose.Types.ObjectId;
113
114
 
114
115
  const extensions = [
115
- // Reserving token - 100 for ObjectId types
116
+ // Reserving token for ObjectId type
116
117
  createExtension(100, {
117
118
  encode(value) {
118
119
  if (value?._bsontype === 'ObjectID') {
@@ -129,7 +130,7 @@ const extensions = [
129
130
  const writer = new BinaryWriter({ extensions });
130
131
 
131
132
  writer.writeObject({
132
- _id: new Object(),
133
+ _id: new ObjectId('64a2be105e19f67e19a71a1d'),
133
134
  firstName: 'Andrew',
134
135
  lastName: 'L.',
135
136
  });
@@ -9,9 +9,11 @@ export interface BinaryReaderOptions {
9
9
  export declare class BinaryReader {
10
10
  private target;
11
11
  private _last?;
12
+ private _lastObject?;
12
13
  private dictionary?;
13
14
  private dictionaryExtended;
14
15
  private extensions;
16
+ private _repeat?;
15
17
  offset: number;
16
18
  length: number;
17
19
  /**
@@ -5,9 +5,11 @@ import { bytesToUtf8, float32, float64, int32 } from './helpers.js';
5
5
  export class BinaryReader {
6
6
  target;
7
7
  _last;
8
+ _lastObject;
8
9
  dictionary;
9
10
  dictionaryExtended;
10
11
  extensions;
12
+ _repeat;
11
13
  offset;
12
14
  length;
13
15
  /**
@@ -16,7 +18,6 @@ export class BinaryReader {
16
18
  */
17
19
  constructor(data, options) {
18
20
  this.target = data;
19
- this._last = undefined;
20
21
  this.offset = 0;
21
22
  this.length = data.length;
22
23
  this.extensions = new Map();
@@ -149,6 +150,7 @@ export class BinaryReader {
149
150
  this.assertRead(length);
150
151
  const bytes = this.target.subarray(this.offset, this.offset + length);
151
152
  this.offset += bytes.length;
153
+ this._last = bytes;
152
154
  return bytes;
153
155
  }
154
156
  /**
@@ -161,7 +163,9 @@ export class BinaryReader {
161
163
  const bytes = this.target.subarray(this.offset, this.offset + length);
162
164
  this.offset += bytes.length;
163
165
  // return pako.inflateRaw(bytes, { to: 'string' });
164
- return bytesToUtf8(bytes);
166
+ const result = bytesToUtf8(bytes);
167
+ this._last = result;
168
+ return result;
165
169
  }
166
170
  /**
167
171
  * Reads a boolean value.
@@ -192,6 +196,15 @@ export class BinaryReader {
192
196
  * Reads a object.
193
197
  */
194
198
  readObject() {
199
+ if (this._repeat) {
200
+ if (this._repeat.pool > 0) {
201
+ --this._repeat.pool;
202
+ return this._repeat.value;
203
+ }
204
+ else {
205
+ this._repeat = undefined;
206
+ }
207
+ }
195
208
  const constructorId = this.readByte();
196
209
  const ext = this.extensions.get(constructorId);
197
210
  let value;
@@ -200,7 +213,7 @@ export class BinaryReader {
200
213
  value = ext.decode(value);
201
214
  }
202
215
  else {
203
- value = this.readCore(constructorId);
216
+ value = this._lastObject = this.readCore(constructorId);
204
217
  }
205
218
  return value;
206
219
  }
@@ -255,6 +268,11 @@ export class BinaryReader {
255
268
  return this.readDouble();
256
269
  case CORE_TYPES.Map:
257
270
  return this.readMap(false);
271
+ case CORE_TYPES.Repeat: {
272
+ const size = this.readLength();
273
+ this._repeat = { pool: size - 1, value: this._lastObject };
274
+ return this._lastObject;
275
+ }
258
276
  }
259
277
  throw new Error(`Invalid constructor = ${CORE_TYPES[constructorId] || constructorId}, offset = ${this.offset - 1}`);
260
278
  }
@@ -307,6 +325,8 @@ export class BinaryReader {
307
325
  decode(value) {
308
326
  this.target = value;
309
327
  this._last = undefined;
328
+ this._lastObject = undefined;
329
+ this._repeat = undefined;
310
330
  this.offset = 0;
311
331
  this.length = value.length;
312
332
  return this.readObject();
@@ -359,6 +379,7 @@ export class BinaryReader {
359
379
  Error.captureStackTrace(err, this.readDictionary);
360
380
  throw err;
361
381
  }
382
+ this._last = temp;
362
383
  return temp;
363
384
  }
364
385
  /**
@@ -12,6 +12,8 @@ export declare class BinaryWriter {
12
12
  private dictionary?;
13
13
  private dictionaryExtended;
14
14
  private extensions;
15
+ private _last;
16
+ private _repeat?;
15
17
  offset: number;
16
18
  constructor(options?: BinaryWriterOptions);
17
19
  allocate(size: number): void;
@@ -41,4 +43,5 @@ export declare class BinaryWriter {
41
43
  writeObject(value: any): void;
42
44
  writeObjectGzip(value: any): void;
43
45
  private writeCore;
46
+ private writeRepeat;
44
47
  }
@@ -5,6 +5,7 @@ import { coreType, float32, float64, int32 } from './helpers.js';
5
5
  const hasNodeBuffer = typeof Buffer !== 'undefined';
6
6
  const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
7
7
  const textEncoder = new TextEncoder();
8
+ const noop = Symbol();
8
9
  const writeUtf8 = hasNodeBuffer
9
10
  ? function (target, value, offset) {
10
11
  const length = target.utf8Write(value, offset, 0xffffffff);
@@ -27,6 +28,8 @@ export class BinaryWriter {
27
28
  dictionary;
28
29
  dictionaryExtended;
29
30
  extensions;
31
+ _last = noop;
32
+ _repeat;
30
33
  offset;
31
34
  constructor(options) {
32
35
  this.offset = 0;
@@ -245,6 +248,8 @@ export class BinaryWriter {
245
248
  }
246
249
  encode(value) {
247
250
  this.offset = 0;
251
+ this._last = noop;
252
+ this._repeat = undefined;
248
253
  this.target = byteArrayAllocate(256);
249
254
  this.writeObject(value);
250
255
  return this.getBuffer();
@@ -287,7 +292,14 @@ export class BinaryWriter {
287
292
  }
288
293
  throw new TypeError(`Invalid core type of ${value}`);
289
294
  }
290
- this.writeCore(constructorId, value);
295
+ if (this._last === value) {
296
+ this.writeRepeat();
297
+ }
298
+ else {
299
+ this._last = value;
300
+ this._repeat = undefined;
301
+ this.writeCore(constructorId, value);
302
+ }
291
303
  }
292
304
  writeObjectGzip(value) {
293
305
  const writer = new BinaryWriter();
@@ -362,4 +374,14 @@ export class BinaryWriter {
362
374
  }
363
375
  }
364
376
  }
377
+ writeRepeat() {
378
+ if (!this._repeat) {
379
+ this.writeByte(CORE_TYPES.Repeat);
380
+ this._repeat = { count: 0, offset: this.offset };
381
+ }
382
+ this.offset = this._repeat.offset;
383
+ this._repeat.count++;
384
+ // console.log('repeat', { value: this.lastWrite, offset: this.offset });
385
+ this.writeLength(this._repeat.count);
386
+ }
365
387
  }
@@ -19,5 +19,6 @@ export declare enum CORE_TYPES {
19
19
  DictValue = 17,
20
20
  DictIndex = 18,
21
21
  String = 19,
22
+ Repeat = 20,
22
23
  GZIP = 25
23
24
  }
package/dist/constants.js CHANGED
@@ -20,5 +20,6 @@ export var CORE_TYPES;
20
20
  CORE_TYPES[CORE_TYPES["DictValue"] = 17] = "DictValue";
21
21
  CORE_TYPES[CORE_TYPES["DictIndex"] = 18] = "DictIndex";
22
22
  CORE_TYPES[CORE_TYPES["String"] = 19] = "String";
23
+ CORE_TYPES[CORE_TYPES["Repeat"] = 20] = "Repeat";
23
24
  CORE_TYPES[CORE_TYPES["GZIP"] = 25] = "GZIP";
24
25
  })(CORE_TYPES || (CORE_TYPES = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrew_l/tl-pack",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Binary serialization library",
5
5
  "type": "module",
6
6
  "scripts": {