msgpackr 1.11.11 → 2.0.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "msgpackr",
3
3
  "author": "Kris Zyp",
4
- "version": "1.11.11",
4
+ "version": "2.0.0",
5
5
  "description": "Ultra-fast MessagePack implementation with extensions for records and structured cloning",
6
6
  "license": "MIT",
7
7
  "types": "./index.d.ts",
package/stream.js CHANGED
@@ -1,57 +1,62 @@
1
- import { Transform } from 'stream'
2
- import { Packr } from './pack.js'
3
- import { Unpackr } from './unpack.js'
4
- var DEFAULT_OPTIONS = {objectMode: true}
1
+ import { Transform } from 'stream';
2
+ import { Packr } from './pack.js';
3
+ import { Unpackr } from './unpack.js';
4
+ var DEFAULT_OPTIONS = {objectMode: true};
5
5
 
6
6
  export class PackrStream extends Transform {
7
7
  constructor(options) {
8
8
  if (!options)
9
- options = {}
10
- options.writableObjectMode = true
11
- super(options)
12
- options.sequential = true
13
- this.packr = options.packr || new Packr(options)
9
+ options = {};
10
+ options.writableObjectMode = true;
11
+ super(options);
12
+ options.sequential = true;
13
+ this.packr = options.packr || new Packr(options);
14
14
  }
15
15
  _transform(value, encoding, callback) {
16
- this.push(this.packr.pack(value))
17
- callback()
16
+ this.push(this.packr.pack(value));
17
+ callback();
18
18
  }
19
19
  }
20
20
 
21
21
  export class UnpackrStream extends Transform {
22
22
  constructor(options) {
23
23
  if (!options)
24
- options = {}
25
- options.objectMode = true
26
- super(options)
27
- options.structures = []
28
- this.unpackr = options.unpackr || new Unpackr(options)
24
+ options = {};
25
+ options.objectMode = true;
26
+ super(options);
27
+ options.structures = [];
28
+ this.maxIncompleteBufferSize = options.maxIncompleteBufferSize !== undefined ? options.maxIncompleteBufferSize : 0x4000000;
29
+ this.unpackr = options.unpackr || new Unpackr(options);
29
30
  }
30
31
  _transform(chunk, encoding, callback) {
31
32
  if (this.incompleteBuffer) {
32
- chunk = Buffer.concat([this.incompleteBuffer, chunk])
33
- this.incompleteBuffer = null
33
+ chunk = Buffer.concat([this.incompleteBuffer, chunk]);
34
+ this.incompleteBuffer = null;
34
35
  }
35
- let values
36
+ let values;
36
37
  try {
37
- values = this.unpackr.unpackMultiple(chunk)
38
+ values = this.unpackr.unpackMultiple(chunk);
38
39
  } catch(error) {
39
40
  if (error.incomplete) {
40
- this.incompleteBuffer = chunk.slice(error.lastPosition)
41
- values = error.values
42
- }
43
- else
44
- throw error
45
- } finally {
46
- for (let value of values || []) {
47
- if (value === null)
48
- value = this.getNullValue()
49
- this.push(value)
41
+ let incompleteBuffer = chunk.slice(error.lastPosition);
42
+ if (incompleteBuffer.length > this.maxIncompleteBufferSize) {
43
+ this.incompleteBuffer = null;
44
+ return callback(new Error('Maximum incomplete buffer size exceeded'));
45
+ }
46
+ this.incompleteBuffer = incompleteBuffer;
47
+ values = error.values;
48
+ } else {
49
+ return callback(error);
50
50
  }
51
51
  }
52
- if (callback) callback()
52
+ for (let value of values || []) {
53
+ if (value === null)
54
+ value = this.getNullValue();
55
+ this.push(value);
56
+ }
57
+ callback();
53
58
  }
54
59
  getNullValue() {
55
- return Symbol.for(null)
60
+ return Symbol.for(null);
56
61
  }
57
62
  }