msgpackr 1.7.2 → 1.8.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/index.d.ts CHANGED
@@ -20,7 +20,9 @@ export interface Options {
20
20
  encodeUndefinedAsNil?: boolean
21
21
  maxSharedStructures?: number
22
22
  maxOwnStructures?: number
23
+ /** @deprecated use int64AsType: 'number' */
23
24
  int64AsNumber?: boolean
25
+ int64AsType?: 'bigint' | 'number' | 'string'
24
26
  shouldShareStructure?: (keys: string[]) => boolean
25
27
  getStructures?(): {}[]
26
28
  saveStructures?(structures: {}[]): boolean | void
package/pack.js CHANGED
@@ -66,14 +66,14 @@ export class Packr extends Unpackr {
66
66
  this.pack = this.encode = function(value, encodeOptions) {
67
67
  if (!target) {
68
68
  target = new ByteArrayAllocate(8192)
69
- targetView = target.dataView = new DataView(target.buffer, 0, 8192)
69
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192))
70
70
  position = 0
71
71
  }
72
72
  safeEnd = target.length - 10
73
73
  if (safeEnd - position < 0x800) {
74
74
  // don't start too close to the end,
75
75
  target = new ByteArrayAllocate(target.length)
76
- targetView = target.dataView = new DataView(target.buffer, 0, target.length)
76
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length))
77
77
  safeEnd = target.length - 10
78
78
  position = 0
79
79
  } else
@@ -388,7 +388,7 @@ export class Packr extends Unpackr {
388
388
  let constructor = value.constructor
389
389
  if (constructor === Object) {
390
390
  writeObject(value, true)
391
- } else if (constructor === Array) {
391
+ } else if (constructor === Array || Array.isArray(value)) {
392
392
  length = value.length
393
393
  if (length < 0x10) {
394
394
  target[position++] = 0x90 | length
@@ -627,7 +627,7 @@ export class Packr extends Unpackr {
627
627
  } else // faster handling for smaller buffers
628
628
  newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12
629
629
  let newBuffer = new ByteArrayAllocate(newSize)
630
- targetView = newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize)
630
+ targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize))
631
631
  end = Math.min(end, target.length)
632
632
  if (target.copy)
633
633
  target.copy(newBuffer, 0, start, end)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "msgpackr",
3
3
  "author": "Kris Zyp",
4
- "version": "1.7.2",
4
+ "version": "1.8.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",
@@ -72,17 +72,18 @@
72
72
  "/*.ts"
73
73
  ],
74
74
  "optionalDependencies": {
75
- "msgpackr-extract": "^2.1.2"
75
+ "msgpackr-extract": "^2.2.0"
76
76
  },
77
77
  "devDependencies": {
78
- "@rollup/plugin-json": "^4.1.0",
78
+ "@rollup/plugin-json": "^5.0.1",
79
+ "@rollup/plugin-replace": "^5.0.1",
79
80
  "@types/node": "latest",
80
81
  "async": "^3",
81
82
  "chai": "^4.3.4",
82
83
  "cpy-cli": "^4.1.0",
83
84
  "esm": "^3.2.25",
84
- "mocha": "^8.1.3",
85
- "rollup": "^1.20.3",
86
- "rollup-plugin-babel-minify": "^9.0.0"
85
+ "mocha": "^10.1.0",
86
+ "rollup": "^3.2.5",
87
+ "@rollup/plugin-terser": "^0.1.0"
87
88
  }
88
89
  }
package/rollup.config.js CHANGED
@@ -1,5 +1,6 @@
1
- import minify from "rollup-plugin-babel-minify";
2
- import json from "@rollup/plugin-json";
1
+ import terser from '@rollup/plugin-terser';
2
+ import json from "@rollup/plugin-json";
3
+ import replace from "@rollup/plugin-replace";
3
4
 
4
5
  export default [
5
6
  {
@@ -20,11 +21,24 @@ export default [
20
21
  name: "msgpackr",
21
22
  sourcemap: true
22
23
  }
23
- },
24
+ },
25
+ {
26
+ input: "index.js",
27
+ plugins: [
28
+ replace({ Function: 'BlockedFunction '})
29
+ ],
30
+ output: {
31
+ file: "dist/index-no-eval.js",
32
+ format: "umd",
33
+ name: "msgpackr",
34
+ sourcemap: true
35
+ },
36
+ },
24
37
  {
25
38
  input: "index.js",
26
- plugins: [minify({
27
- })],
39
+ plugins: [
40
+ terser({})
41
+ ],
28
42
  output: {
29
43
  file: "dist/index.min.js",
30
44
  format: "umd",
@@ -32,6 +46,19 @@ export default [
32
46
  sourcemap: true
33
47
  }
34
48
  },
49
+ {
50
+ input: "index.js",
51
+ plugins: [
52
+ replace({ Function: 'BlockedFunction '}),
53
+ terser({})
54
+ ],
55
+ output: {
56
+ file: "dist/index-no-eval.min.js",
57
+ format: "umd",
58
+ name: "msgpackr",
59
+ sourcemap: true
60
+ }
61
+ },
35
62
  {
36
63
  input: "tests/test.js",
37
64
  plugins: [json()],
package/struct.js CHANGED
@@ -161,9 +161,9 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
161
161
  case 'string':
162
162
  let strLength = value.length;
163
163
  refOffset = refPosition - refsStartPosition;
164
- if ((strLength << 2) + position > safeEnd) {
164
+ if ((strLength << 2) + refPosition > safeEnd) {
165
165
  let lastStart = start;
166
- target = makeRoom(refPosition);
166
+ target = makeRoom((strLength << 2) + refPosition);
167
167
  targetView = target.dataView;
168
168
  position -= lastStart;
169
169
  refsStartPosition -= lastStart;
@@ -776,7 +776,7 @@ function prepareStructures(structures, packr) {
776
776
  let typed = existing.get('typed') || [];
777
777
  if (typed.length !== lastTypedStructuresLength)
778
778
  compatible = false;
779
- } else if (existing instanceof Array) {
779
+ } else if (existing instanceof Array || Array.isArray(existing)) {
780
780
  if (existing.length !== (packr.lastNamedStructuresLength || 0))
781
781
  compatible = false;
782
782
  }
package/unpack.js CHANGED
@@ -28,6 +28,8 @@ C1.name = 'MessagePack 0xC1'
28
28
  var sequentialMode = false
29
29
  var inlineObjectReadThreshold = 2
30
30
  var readStruct, onLoadedStructures, onSaveState
31
+ var BlockedFunction // we use search and replace to change the next call to BlockedFunction to avoid CSP issues for
32
+ // no-eval build
31
33
  try {
32
34
  new Function('')
33
35
  } catch(error) {
@@ -54,6 +56,9 @@ export class Unpackr {
54
56
  (options.structures = []).uninitialized = true // this is what we use to denote an uninitialized structures
55
57
  options.structures.sharedLength = 0
56
58
  }
59
+ if (options.int64AsNumber) {
60
+ options.int64AsType = 'number'
61
+ }
57
62
  }
58
63
  Object.assign(this, options)
59
64
  }
@@ -188,8 +193,10 @@ export function checkedRead(options) {
188
193
  position = srcEnd
189
194
  } else
190
195
  result = read()
191
- if (bundledStrings) // bundled strings to skip past
196
+ if (bundledStrings) { // bundled strings to skip past
192
197
  position = bundledStrings.postBundlePosition
198
+ bundledStrings = null
199
+ }
193
200
 
194
201
  if (position == srcEnd) {
195
202
  // finished reading this source, cleanup references
@@ -354,9 +361,11 @@ export function read() {
354
361
  position += 4
355
362
  return value
356
363
  case 0xcf:
357
- if (currentUnpackr.int64AsNumber) {
364
+ if (currentUnpackr.int64AsType === 'number') {
358
365
  value = dataView.getUint32(position) * 0x100000000
359
366
  value += dataView.getUint32(position + 4)
367
+ } else if (currentUnpackr.int64AsType === 'string') {
368
+ value = dataView.getBigUint64(position).toString()
360
369
  } else
361
370
  value = dataView.getBigUint64(position)
362
371
  position += 8
@@ -374,9 +383,11 @@ export function read() {
374
383
  position += 4
375
384
  return value
376
385
  case 0xd3:
377
- if (currentUnpackr.int64AsNumber) {
386
+ if (currentUnpackr.int64AsType === 'number') {
378
387
  value = dataView.getInt32(position) * 0x100000000
379
388
  value += dataView.getUint32(position + 4)
389
+ } else if (currentUnpackr.int64AsType === 'string') {
390
+ value = dataView.getBigInt64(position).toString()
380
391
  } else
381
392
  value = dataView.getBigInt64(position)
382
393
  position += 8
@@ -869,7 +880,7 @@ function readExt(length) {
869
880
  })
870
881
  }
871
882
  else
872
- throw new Error('Unknown extension type ' + type)``
883
+ throw new Error('Unknown extension type ' + type)
873
884
  }
874
885
 
875
886
  var keyCache = new Array(4096)