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/index.js CHANGED
@@ -1,5 +1,5 @@
1
- export { Packr, Encoder, addExtension, pack, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './pack.js'
2
- export { Unpackr, Decoder, C1, unpack, unpackMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './unpack.js'
3
- export { decodeIter, encodeIter } from './iterators.js'
4
- export const useRecords = false
5
- export const mapsAsObjects = true
1
+ export { Packr, Encoder, addExtension, pack, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './pack.js'
2
+ export { Unpackr, Decoder, C1, unpack, unpackMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './unpack.js'
3
+ export { decodeIter, encodeIter } from './iterators.js'
4
+ export const useRecords = false
5
+ export const mapsAsObjects = true
package/iterators.js CHANGED
@@ -1,87 +1,87 @@
1
- import { Packr } from './pack.js'
2
- import { Unpackr } from './unpack.js'
3
-
4
- /**
5
- * Given an Iterable first argument, returns an Iterable where each value is packed as a Buffer
6
- * If the argument is only Async Iterable, the return value will be an Async Iterable.
7
- * @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
8
- * @param {options} [options] - msgpackr pack options
9
- * @returns {IterableIterator|Promise.<AsyncIterableIterator>}
10
- */
11
- export function packIter (objectIterator, options = {}) {
12
- if (!objectIterator || typeof objectIterator !== 'object') {
13
- throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
14
- } else if (typeof objectIterator[Symbol.iterator] === 'function') {
15
- return packIterSync(objectIterator, options)
16
- } else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
17
- return packIterAsync(objectIterator, options)
18
- } else {
19
- throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
20
- }
21
- }
22
-
23
- function * packIterSync (objectIterator, options) {
24
- const packr = new Packr(options)
25
- for (const value of objectIterator) {
26
- yield packr.pack(value)
27
- }
28
- }
29
-
30
- async function * packIterAsync (objectIterator, options) {
31
- const packr = new Packr(options)
32
- for await (const value of objectIterator) {
33
- yield packr.pack(value)
34
- }
35
- }
36
-
37
- /**
38
- * Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
39
- * Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
40
- * @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
41
- * @param {object} [options] - unpackr options
42
- * @returns {IterableIterator|Promise.<AsyncIterableIterator}
43
- */
44
- export function unpackIter (bufferIterator, options = {}) {
45
- if (!bufferIterator || typeof bufferIterator !== 'object') {
46
- throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
47
- }
48
-
49
- const unpackr = new Unpackr(options)
50
- let incomplete
51
- const parser = (chunk) => {
52
- let yields
53
- // if there's incomplete data from previous chunk, concatinate and try again
54
- if (incomplete) {
55
- chunk = Buffer.concat([incomplete, chunk])
56
- incomplete = undefined
57
- }
58
-
59
- try {
60
- yields = unpackr.unpackMultiple(chunk)
61
- } catch (err) {
62
- if (err.incomplete) {
63
- incomplete = chunk.slice(err.lastPosition)
64
- yields = err.values
65
- } else {
66
- throw err
67
- }
68
- }
69
- return yields
70
- }
71
-
72
- if (typeof bufferIterator[Symbol.iterator] === 'function') {
73
- return (function * iter () {
74
- for (const value of bufferIterator) {
75
- yield * parser(value)
76
- }
77
- })()
78
- } else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
79
- return (async function * iter () {
80
- for await (const value of bufferIterator) {
81
- yield * parser(value)
82
- }
83
- })()
84
- }
85
- }
86
- export const decodeIter = unpackIter
1
+ import { Packr } from './pack.js'
2
+ import { Unpackr } from './unpack.js'
3
+
4
+ /**
5
+ * Given an Iterable first argument, returns an Iterable where each value is packed as a Buffer
6
+ * If the argument is only Async Iterable, the return value will be an Async Iterable.
7
+ * @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
8
+ * @param {options} [options] - msgpackr pack options
9
+ * @returns {IterableIterator|Promise.<AsyncIterableIterator>}
10
+ */
11
+ export function packIter (objectIterator, options = {}) {
12
+ if (!objectIterator || typeof objectIterator !== 'object') {
13
+ throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
14
+ } else if (typeof objectIterator[Symbol.iterator] === 'function') {
15
+ return packIterSync(objectIterator, options)
16
+ } else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
17
+ return packIterAsync(objectIterator, options)
18
+ } else {
19
+ throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
20
+ }
21
+ }
22
+
23
+ function * packIterSync (objectIterator, options) {
24
+ const packr = new Packr(options)
25
+ for (const value of objectIterator) {
26
+ yield packr.pack(value)
27
+ }
28
+ }
29
+
30
+ async function * packIterAsync (objectIterator, options) {
31
+ const packr = new Packr(options)
32
+ for await (const value of objectIterator) {
33
+ yield packr.pack(value)
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
39
+ * Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
40
+ * @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
41
+ * @param {object} [options] - unpackr options
42
+ * @returns {IterableIterator|Promise.<AsyncIterableIterator}
43
+ */
44
+ export function unpackIter (bufferIterator, options = {}) {
45
+ if (!bufferIterator || typeof bufferIterator !== 'object') {
46
+ throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
47
+ }
48
+
49
+ const unpackr = new Unpackr(options)
50
+ let incomplete
51
+ const parser = (chunk) => {
52
+ let yields
53
+ // if there's incomplete data from previous chunk, concatinate and try again
54
+ if (incomplete) {
55
+ chunk = Buffer.concat([incomplete, chunk])
56
+ incomplete = undefined
57
+ }
58
+
59
+ try {
60
+ yields = unpackr.unpackMultiple(chunk)
61
+ } catch (err) {
62
+ if (err.incomplete) {
63
+ incomplete = chunk.slice(err.lastPosition)
64
+ yields = err.values
65
+ } else {
66
+ throw err
67
+ }
68
+ }
69
+ return yields
70
+ }
71
+
72
+ if (typeof bufferIterator[Symbol.iterator] === 'function') {
73
+ return (function * iter () {
74
+ for (const value of bufferIterator) {
75
+ yield * parser(value)
76
+ }
77
+ })()
78
+ } else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
79
+ return (async function * iter () {
80
+ for await (const value of bufferIterator) {
81
+ yield * parser(value)
82
+ }
83
+ })()
84
+ }
85
+ }
86
+ export const decodeIter = unpackIter
87
87
  export const encodeIter = packIter
package/pack.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { Unpackr } from './unpack'
2
- export { addExtension, FLOAT32_OPTIONS } from './unpack'
3
- export class Packr extends Unpackr {
4
- pack(value: any): Buffer
5
- encode(value: any): Buffer
6
- }
7
- export class Encoder extends Packr {}
8
- export function pack(value: any): Buffer
9
- export function encode(value: any): Buffer
1
+ import { Unpackr } from './unpack'
2
+ export { addExtension, FLOAT32_OPTIONS } from './unpack'
3
+ export class Packr extends Unpackr {
4
+ pack(value: any): Buffer
5
+ encode(value: any): Buffer
6
+ }
7
+ export class Encoder extends Packr {}
8
+ export function pack(value: any): Buffer
9
+ export function encode(value: any): Buffer
package/package.json CHANGED
@@ -1,83 +1,83 @@
1
- {
2
- "name": "msgpackr",
3
- "author": "Kris Zyp",
4
- "version": "1.6.2",
5
- "description": "Ultra-fast MessagePack implementation with extensions for records and structured cloning",
6
- "license": "MIT",
7
- "types": "./index.d.ts",
8
- "main": "./dist/node.cjs",
9
- "module": "./index.js",
10
- "keywords": [
11
- "MessagePack",
12
- "msgpack",
13
- "performance",
14
- "structured",
15
- "clone"
16
- ],
17
- "repository": {
18
- "type": "git",
19
- "url": "http://github.com/kriszyp/msgpackr"
20
- },
21
- "scripts": {
22
- "benchmark": "node ./tests/benchmark.cjs",
23
- "build": "rollup -c",
24
- "dry-run": "npm publish --dry-run",
25
- "prepare": "npm run build",
26
- "test": "mocha tests/test**.*js -u tdd --experimental-json-modules"
27
- },
28
- "type": "module",
29
- "exports": {
30
- ".": {
31
- "node": {
32
- "require": "./dist/node.cjs",
33
- "import": "./node-index.js"
34
- },
35
- "bun": {
36
- "require": "./dist/node.cjs",
37
- "import": "./node-index.js"
38
- },
39
- "default": "./index.js"
40
- },
41
- "./pack": {
42
- "node": {
43
- "import": "./index.js",
44
- "require": "./dist/node.cjs"
45
- },
46
- "bun": {
47
- "import": "./index.js",
48
- "require": "./dist/node.cjs"
49
- },
50
- "default": "./pack.js"
51
- },
52
- "./unpack": {
53
- "node": {
54
- "import": "./index.js",
55
- "require": "./dist/node.cjs"
56
- },
57
- "bun": {
58
- "import": "./index.js",
59
- "require": "./dist/node.cjs"
60
- },
61
- "default": "./unpack.js"
62
- }
63
- },
64
- "files": [
65
- "/dist",
66
- "*.md",
67
- "/*.js",
68
- "/*.ts"
69
- ],
70
- "optionalDependencies": {
71
- "msgpackr-extract": "^2.0.2"
72
- },
73
- "devDependencies": {
74
- "@rollup/plugin-json": "^4.1.0",
75
- "@types/node": "latest",
76
- "async": "^3",
77
- "chai": "^4.3.4",
78
- "esm": "^3.2.25",
79
- "mocha": "^8.1.3",
80
- "rollup": "^1.20.3",
81
- "rollup-plugin-babel-minify": "^9.0.0"
82
- }
83
- }
1
+ {
2
+ "name": "msgpackr",
3
+ "author": "Kris Zyp",
4
+ "version": "1.6.3",
5
+ "description": "Ultra-fast MessagePack implementation with extensions for records and structured cloning",
6
+ "license": "MIT",
7
+ "types": "./index.d.ts",
8
+ "main": "./dist/node.cjs",
9
+ "module": "./index.js",
10
+ "keywords": [
11
+ "MessagePack",
12
+ "msgpack",
13
+ "performance",
14
+ "structured",
15
+ "clone"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "http://github.com/kriszyp/msgpackr"
20
+ },
21
+ "scripts": {
22
+ "benchmark": "node ./tests/benchmark.cjs",
23
+ "build": "rollup -c",
24
+ "dry-run": "npm publish --dry-run",
25
+ "prepare": "npm run build",
26
+ "test": "mocha tests/test**.*js -u tdd --experimental-json-modules"
27
+ },
28
+ "type": "module",
29
+ "exports": {
30
+ ".": {
31
+ "node": {
32
+ "require": "./dist/node.cjs",
33
+ "import": "./node-index.js"
34
+ },
35
+ "bun": {
36
+ "require": "./dist/node.cjs",
37
+ "import": "./node-index.js"
38
+ },
39
+ "default": "./index.js"
40
+ },
41
+ "./pack": {
42
+ "node": {
43
+ "import": "./index.js",
44
+ "require": "./dist/node.cjs"
45
+ },
46
+ "bun": {
47
+ "import": "./index.js",
48
+ "require": "./dist/node.cjs"
49
+ },
50
+ "default": "./pack.js"
51
+ },
52
+ "./unpack": {
53
+ "node": {
54
+ "import": "./index.js",
55
+ "require": "./dist/node.cjs"
56
+ },
57
+ "bun": {
58
+ "import": "./index.js",
59
+ "require": "./dist/node.cjs"
60
+ },
61
+ "default": "./unpack.js"
62
+ }
63
+ },
64
+ "files": [
65
+ "/dist",
66
+ "*.md",
67
+ "/*.js",
68
+ "/*.ts"
69
+ ],
70
+ "optionalDependencies": {
71
+ "msgpackr-extract": "^2.0.2"
72
+ },
73
+ "devDependencies": {
74
+ "@rollup/plugin-json": "^4.1.0",
75
+ "@types/node": "latest",
76
+ "async": "^3",
77
+ "chai": "^4.3.4",
78
+ "esm": "^3.2.25",
79
+ "mocha": "^8.1.3",
80
+ "rollup": "^1.20.3",
81
+ "rollup-plugin-babel-minify": "^9.0.0"
82
+ }
83
+ }
package/rollup.config.js CHANGED
@@ -1,45 +1,45 @@
1
- import minify from "rollup-plugin-babel-minify";
2
- import json from "@rollup/plugin-json";
3
-
4
- export default [
5
- {
6
- input: "node-index.js",
7
- output: [
8
- {
9
- file: "dist/node.cjs",
10
- format: "cjs"
11
- }
12
- ]
13
- },
14
- {
15
- input: "index.js",
16
- output: {
17
- file: "dist/index.js",
18
- format: "umd",
19
- name: "msgpackr"
20
- }
21
- },
22
- {
23
- input: "index.js",
24
- plugins: [minify({
25
- })],
26
- output: {
27
- file: "dist/index.min.js",
28
- format: "umd",
29
- name: "msgpackr"
30
- }
31
- },
32
- {
33
- input: "tests/test.js",
34
- plugins: [json()],
35
- external: ['chai', '../index.js'],
36
- output: {
37
- file: "dist/test.js",
38
- format: "iife",
39
- globals: {
40
- chai: 'chai',
41
- './index.js': 'msgpackr',
42
- },
43
- }
44
- }
45
- ];
1
+ import minify from "rollup-plugin-babel-minify";
2
+ import json from "@rollup/plugin-json";
3
+
4
+ export default [
5
+ {
6
+ input: "node-index.js",
7
+ output: [
8
+ {
9
+ file: "dist/node.cjs",
10
+ format: "cjs"
11
+ }
12
+ ]
13
+ },
14
+ {
15
+ input: "index.js",
16
+ output: {
17
+ file: "dist/index.js",
18
+ format: "umd",
19
+ name: "msgpackr"
20
+ }
21
+ },
22
+ {
23
+ input: "index.js",
24
+ plugins: [minify({
25
+ })],
26
+ output: {
27
+ file: "dist/index.min.js",
28
+ format: "umd",
29
+ name: "msgpackr"
30
+ }
31
+ },
32
+ {
33
+ input: "tests/test.js",
34
+ plugins: [json()],
35
+ external: ['chai', '../index.js'],
36
+ output: {
37
+ file: "dist/test.js",
38
+ format: "iife",
39
+ globals: {
40
+ chai: 'chai',
41
+ './index.js': 'msgpackr',
42
+ },
43
+ }
44
+ }
45
+ ];
package/stream.js CHANGED
@@ -1,57 +1,57 @@
1
- import { Transform } from 'stream'
2
- import { Packr } from './pack.js'
3
- import { Unpackr } from './unpack.js'
4
- var DEFAULT_OPTIONS = {objectMode: true}
5
-
6
- export class PackrStream extends Transform {
7
- constructor(options) {
8
- if (!options)
9
- options = {}
10
- options.writableObjectMode = true
11
- super(options)
12
- options.sequential = true
13
- this.packr = options.packr || new Packr(options)
14
- }
15
- _transform(value, encoding, callback) {
16
- this.push(this.packr.pack(value))
17
- callback()
18
- }
19
- }
20
-
21
- export class UnpackrStream extends Transform {
22
- constructor(options) {
23
- if (!options)
24
- options = {}
25
- options.objectMode = true
26
- super(options)
27
- options.structures = []
28
- this.unpackr = options.unpackr || new Unpackr(options)
29
- }
30
- _transform(chunk, encoding, callback) {
31
- if (this.incompleteBuffer) {
32
- chunk = Buffer.concat([this.incompleteBuffer, chunk])
33
- this.incompleteBuffer = null
34
- }
35
- let values
36
- try {
37
- values = this.unpackr.unpackMultiple(chunk)
38
- } catch(error) {
39
- 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)
50
- }
51
- }
52
- if (callback) callback()
53
- }
54
- getNullValue() {
55
- return Symbol.for(null)
56
- }
57
- }
1
+ import { Transform } from 'stream'
2
+ import { Packr } from './pack.js'
3
+ import { Unpackr } from './unpack.js'
4
+ var DEFAULT_OPTIONS = {objectMode: true}
5
+
6
+ export class PackrStream extends Transform {
7
+ constructor(options) {
8
+ if (!options)
9
+ options = {}
10
+ options.writableObjectMode = true
11
+ super(options)
12
+ options.sequential = true
13
+ this.packr = options.packr || new Packr(options)
14
+ }
15
+ _transform(value, encoding, callback) {
16
+ this.push(this.packr.pack(value))
17
+ callback()
18
+ }
19
+ }
20
+
21
+ export class UnpackrStream extends Transform {
22
+ constructor(options) {
23
+ if (!options)
24
+ options = {}
25
+ options.objectMode = true
26
+ super(options)
27
+ options.structures = []
28
+ this.unpackr = options.unpackr || new Unpackr(options)
29
+ }
30
+ _transform(chunk, encoding, callback) {
31
+ if (this.incompleteBuffer) {
32
+ chunk = Buffer.concat([this.incompleteBuffer, chunk])
33
+ this.incompleteBuffer = null
34
+ }
35
+ let values
36
+ try {
37
+ values = this.unpackr.unpackMultiple(chunk)
38
+ } catch(error) {
39
+ 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)
50
+ }
51
+ }
52
+ if (callback) callback()
53
+ }
54
+ getNullValue() {
55
+ return Symbol.for(null)
56
+ }
57
+ }
package/unpack.js CHANGED
@@ -231,7 +231,10 @@ export function read() {
231
231
  if (currentUnpackr.mapsAsObjects) {
232
232
  let object = {}
233
233
  for (let i = 0; i < token; i++) {
234
- object[readKey()] = read()
234
+ let key = readKey()
235
+ if (key === '__proto__')
236
+ key = '__proto_'
237
+ object[key] = read()
235
238
  }
236
239
  return object
237
240
  } else {
@@ -457,7 +460,8 @@ function createStructureReader(structure, firstId) {
457
460
  function readObject() {
458
461
  // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
459
462
  if (readObject.count++ > inlineObjectReadThreshold) {
460
- let readObject = structure.read = (new Function('r', 'return function(){return {' + structure.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}}'))(read)
463
+ let readObject = structure.read = (new Function('r', 'return function(){return {' + structure.map(key => key === '__proto__' ? '__proto_:r()' :
464
+ validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}}'))(read)
461
465
  if (structure.highByte === 0)
462
466
  structure.read = createSecondByteReader(firstId, structure.read)
463
467
  return readObject() // second byte is already read, if there is one so immediately read object
@@ -465,6 +469,8 @@ function createStructureReader(structure, firstId) {
465
469
  let object = {}
466
470
  for (let i = 0, l = structure.length; i < l; i++) {
467
471
  let key = structure[i]
472
+ if (key === '__proto__')
473
+ key = '__proto_'
468
474
  object[key] = read()
469
475
  }
470
476
  return object
@@ -611,7 +617,10 @@ function readMap(length) {
611
617
  if (currentUnpackr.mapsAsObjects) {
612
618
  let object = {}
613
619
  for (let i = 0; i < length; i++) {
614
- object[readKey()] = read()
620
+ let key = readKey()
621
+ if (key === '__proto__')
622
+ key = '__proto_';
623
+ object[key] = read()
615
624
  }
616
625
  return object
617
626
  } else {