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/LICENSE +21 -21
- package/README.md +335 -335
- package/SECURITY.md +11 -11
- package/benchmark.md +67 -67
- package/dist/index.js +96 -87
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +17 -17
- package/dist/index.min.js.map +1 -0
- package/dist/node.cjs +146 -137
- package/dist/node.cjs.map +1 -0
- package/dist/test.js +11 -0
- package/dist/test.js.map +1 -0
- package/index.js +5 -5
- package/iterators.js +86 -86
- package/pack.d.ts +9 -9
- package/package.json +83 -83
- package/rollup.config.js +45 -45
- package/stream.js +57 -57
- package/unpack.js +12 -3
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
|
-
|
|
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 =>
|
|
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
|
-
|
|
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
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
}
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
}
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
}
|
|
2124
|
-
|
|
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;
|