msgpackr 1.6.2 → 1.6.3

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/dist/node.cjs CHANGED
@@ -233,7 +233,10 @@ function read() {
233
233
  if (currentUnpackr.mapsAsObjects) {
234
234
  let object = {};
235
235
  for (let i = 0; i < token; i++) {
236
- object[readKey()] = read();
236
+ let key = readKey();
237
+ if (key === '__proto__')
238
+ key = '__proto_';
239
+ object[key] = read();
237
240
  }
238
241
  return object
239
242
  } else {
@@ -459,7 +462,8 @@ function createStructureReader(structure, firstId) {
459
462
  function readObject() {
460
463
  // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
461
464
  if (readObject.count++ > inlineObjectReadThreshold) {
462
- let readObject = structure.read = (new Function('r', 'return function(){return {' + structure.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}}'))(read);
465
+ let readObject = structure.read = (new Function('r', 'return function(){return {' + structure.map(key => key === '__proto__' ? '__proto_:r()' :
466
+ validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}}'))(read);
463
467
  if (structure.highByte === 0)
464
468
  structure.read = createSecondByteReader(firstId, structure.read);
465
469
  return readObject() // second byte is already read, if there is one so immediately read object
@@ -467,6 +471,8 @@ function createStructureReader(structure, firstId) {
467
471
  let object = {};
468
472
  for (let i = 0, l = structure.length; i < l; i++) {
469
473
  let key = structure[i];
474
+ if (key === '__proto__')
475
+ key = '__proto_';
470
476
  object[key] = read();
471
477
  }
472
478
  return object
@@ -613,7 +619,10 @@ function readMap(length) {
613
619
  if (currentUnpackr.mapsAsObjects) {
614
620
  let object = {};
615
621
  for (let i = 0; i < length; i++) {
616
- object[readKey()] = read();
622
+ let key = readKey();
623
+ if (key === '__proto__')
624
+ key = '__proto_';
625
+ object[key] = read();
617
626
  }
618
627
  return object
619
628
  } else {
@@ -1986,142 +1995,142 @@ const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
1986
1995
  const REUSE_BUFFER_MODE = 512;
1987
1996
  const RESET_BUFFER_MODE = 1024;
1988
1997
 
1989
- class PackrStream extends stream.Transform {
1990
- constructor(options) {
1991
- if (!options)
1992
- options = {};
1993
- options.writableObjectMode = true;
1994
- super(options);
1995
- options.sequential = true;
1996
- this.packr = options.packr || new Packr(options);
1997
- }
1998
- _transform(value, encoding, callback) {
1999
- this.push(this.packr.pack(value));
2000
- callback();
2001
- }
2002
- }
2003
-
2004
- class UnpackrStream extends stream.Transform {
2005
- constructor(options) {
2006
- if (!options)
2007
- options = {};
2008
- options.objectMode = true;
2009
- super(options);
2010
- options.structures = [];
2011
- this.unpackr = options.unpackr || new Unpackr(options);
2012
- }
2013
- _transform(chunk, encoding, callback) {
2014
- if (this.incompleteBuffer) {
2015
- chunk = Buffer.concat([this.incompleteBuffer, chunk]);
2016
- this.incompleteBuffer = null;
2017
- }
2018
- let values;
2019
- try {
2020
- values = this.unpackr.unpackMultiple(chunk);
2021
- } catch(error) {
2022
- if (error.incomplete) {
2023
- this.incompleteBuffer = chunk.slice(error.lastPosition);
2024
- values = error.values;
2025
- }
2026
- else
2027
- throw error
2028
- } finally {
2029
- for (let value of values || []) {
2030
- if (value === null)
2031
- value = this.getNullValue();
2032
- this.push(value);
2033
- }
2034
- }
2035
- if (callback) callback();
2036
- }
2037
- getNullValue() {
2038
- return Symbol.for(null)
2039
- }
1998
+ class PackrStream extends stream.Transform {
1999
+ constructor(options) {
2000
+ if (!options)
2001
+ options = {};
2002
+ options.writableObjectMode = true;
2003
+ super(options);
2004
+ options.sequential = true;
2005
+ this.packr = options.packr || new Packr(options);
2006
+ }
2007
+ _transform(value, encoding, callback) {
2008
+ this.push(this.packr.pack(value));
2009
+ callback();
2010
+ }
2040
2011
  }
2041
2012
 
2042
- /**
2043
- * Given an Iterable first argument, returns an Iterable where each value is packed as a Buffer
2044
- * If the argument is only Async Iterable, the return value will be an Async Iterable.
2045
- * @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
2046
- * @param {options} [options] - msgpackr pack options
2047
- * @returns {IterableIterator|Promise.<AsyncIterableIterator>}
2048
- */
2049
- function packIter (objectIterator, options = {}) {
2050
- if (!objectIterator || typeof objectIterator !== 'object') {
2051
- throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
2052
- } else if (typeof objectIterator[Symbol.iterator] === 'function') {
2053
- return packIterSync(objectIterator, options)
2054
- } else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
2055
- return packIterAsync(objectIterator, options)
2056
- } else {
2057
- throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
2058
- }
2059
- }
2060
-
2061
- function * packIterSync (objectIterator, options) {
2062
- const packr = new Packr(options);
2063
- for (const value of objectIterator) {
2064
- yield packr.pack(value);
2065
- }
2066
- }
2067
-
2068
- async function * packIterAsync (objectIterator, options) {
2069
- const packr = new Packr(options);
2070
- for await (const value of objectIterator) {
2071
- yield packr.pack(value);
2072
- }
2073
- }
2074
-
2075
- /**
2076
- * Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
2077
- * Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
2078
- * @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
2079
- * @param {object} [options] - unpackr options
2080
- * @returns {IterableIterator|Promise.<AsyncIterableIterator}
2081
- */
2082
- function unpackIter (bufferIterator, options = {}) {
2083
- if (!bufferIterator || typeof bufferIterator !== 'object') {
2084
- throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
2085
- }
2086
-
2087
- const unpackr = new Unpackr(options);
2088
- let incomplete;
2089
- const parser = (chunk) => {
2090
- let yields;
2091
- // if there's incomplete data from previous chunk, concatinate and try again
2092
- if (incomplete) {
2093
- chunk = Buffer.concat([incomplete, chunk]);
2094
- incomplete = undefined;
2095
- }
2096
-
2097
- try {
2098
- yields = unpackr.unpackMultiple(chunk);
2099
- } catch (err) {
2100
- if (err.incomplete) {
2101
- incomplete = chunk.slice(err.lastPosition);
2102
- yields = err.values;
2103
- } else {
2104
- throw err
2105
- }
2106
- }
2107
- return yields
2108
- };
2109
-
2110
- if (typeof bufferIterator[Symbol.iterator] === 'function') {
2111
- return (function * iter () {
2112
- for (const value of bufferIterator) {
2113
- yield * parser(value);
2114
- }
2115
- })()
2116
- } else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
2117
- return (async function * iter () {
2118
- for await (const value of bufferIterator) {
2119
- yield * parser(value);
2120
- }
2121
- })()
2122
- }
2123
- }
2124
- const decodeIter = unpackIter;
2013
+ class UnpackrStream extends stream.Transform {
2014
+ constructor(options) {
2015
+ if (!options)
2016
+ options = {};
2017
+ options.objectMode = true;
2018
+ super(options);
2019
+ options.structures = [];
2020
+ this.unpackr = options.unpackr || new Unpackr(options);
2021
+ }
2022
+ _transform(chunk, encoding, callback) {
2023
+ if (this.incompleteBuffer) {
2024
+ chunk = Buffer.concat([this.incompleteBuffer, chunk]);
2025
+ this.incompleteBuffer = null;
2026
+ }
2027
+ let values;
2028
+ try {
2029
+ values = this.unpackr.unpackMultiple(chunk);
2030
+ } catch(error) {
2031
+ if (error.incomplete) {
2032
+ this.incompleteBuffer = chunk.slice(error.lastPosition);
2033
+ values = error.values;
2034
+ }
2035
+ else
2036
+ throw error
2037
+ } finally {
2038
+ for (let value of values || []) {
2039
+ if (value === null)
2040
+ value = this.getNullValue();
2041
+ this.push(value);
2042
+ }
2043
+ }
2044
+ if (callback) callback();
2045
+ }
2046
+ getNullValue() {
2047
+ return Symbol.for(null)
2048
+ }
2049
+ }
2050
+
2051
+ /**
2052
+ * Given an Iterable first argument, returns an Iterable where each value is packed as a Buffer
2053
+ * If the argument is only Async Iterable, the return value will be an Async Iterable.
2054
+ * @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
2055
+ * @param {options} [options] - msgpackr pack options
2056
+ * @returns {IterableIterator|Promise.<AsyncIterableIterator>}
2057
+ */
2058
+ function packIter (objectIterator, options = {}) {
2059
+ if (!objectIterator || typeof objectIterator !== 'object') {
2060
+ throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
2061
+ } else if (typeof objectIterator[Symbol.iterator] === 'function') {
2062
+ return packIterSync(objectIterator, options)
2063
+ } else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
2064
+ return packIterAsync(objectIterator, options)
2065
+ } else {
2066
+ throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
2067
+ }
2068
+ }
2069
+
2070
+ function * packIterSync (objectIterator, options) {
2071
+ const packr = new Packr(options);
2072
+ for (const value of objectIterator) {
2073
+ yield packr.pack(value);
2074
+ }
2075
+ }
2076
+
2077
+ async function * packIterAsync (objectIterator, options) {
2078
+ const packr = new Packr(options);
2079
+ for await (const value of objectIterator) {
2080
+ yield packr.pack(value);
2081
+ }
2082
+ }
2083
+
2084
+ /**
2085
+ * Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
2086
+ * Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
2087
+ * @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
2088
+ * @param {object} [options] - unpackr options
2089
+ * @returns {IterableIterator|Promise.<AsyncIterableIterator}
2090
+ */
2091
+ function unpackIter (bufferIterator, options = {}) {
2092
+ if (!bufferIterator || typeof bufferIterator !== 'object') {
2093
+ throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
2094
+ }
2095
+
2096
+ const unpackr = new Unpackr(options);
2097
+ let incomplete;
2098
+ const parser = (chunk) => {
2099
+ let yields;
2100
+ // if there's incomplete data from previous chunk, concatinate and try again
2101
+ if (incomplete) {
2102
+ chunk = Buffer.concat([incomplete, chunk]);
2103
+ incomplete = undefined;
2104
+ }
2105
+
2106
+ try {
2107
+ yields = unpackr.unpackMultiple(chunk);
2108
+ } catch (err) {
2109
+ if (err.incomplete) {
2110
+ incomplete = chunk.slice(err.lastPosition);
2111
+ yields = err.values;
2112
+ } else {
2113
+ throw err
2114
+ }
2115
+ }
2116
+ return yields
2117
+ };
2118
+
2119
+ if (typeof bufferIterator[Symbol.iterator] === 'function') {
2120
+ return (function * iter () {
2121
+ for (const value of bufferIterator) {
2122
+ yield * parser(value);
2123
+ }
2124
+ })()
2125
+ } else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
2126
+ return (async function * iter () {
2127
+ for await (const value of bufferIterator) {
2128
+ yield * parser(value);
2129
+ }
2130
+ })()
2131
+ }
2132
+ }
2133
+ const decodeIter = unpackIter;
2125
2134
  const encodeIter = packIter;
2126
2135
 
2127
2136
  const useRecords = false;