@polkadot/types-codec 8.5.2-4 → 8.5.2-7

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.
@@ -11,7 +11,9 @@ var _utils = require("../utils");
11
11
 
12
12
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
13
13
  // SPDX-License-Identifier: Apache-2.0
14
-
14
+ function noopSetDefinition(d) {
15
+ return d;
16
+ }
15
17
  /**
16
18
  * @name Compact
17
19
  * @description
@@ -20,23 +22,36 @@ var _utils = require("../utils");
20
22
  * used by other types to add length-prefixed encoding, or in the case of wrapped types, taking
21
23
  * a number and making the compact representation thereof
22
24
  */
25
+
26
+
23
27
  class Compact {
24
28
  #Type;
25
29
  #raw;
26
30
 
27
31
  constructor(registry, Type) {
28
32
  let value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
33
+ let {
34
+ definition,
35
+ setDefinition = noopSetDefinition
36
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
29
37
  this.registry = registry;
30
- this.#Type = (0, _utils.typeToConstructor)(registry, Type);
38
+ this.#Type = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
31
39
  const [raw, decodedLength] = Compact.decodeCompact(registry, this.#Type, value);
32
40
  this.initialU8aLength = decodedLength;
33
41
  this.#raw = raw;
34
42
  }
35
43
 
36
44
  static with(Type) {
45
+ let definition; // eslint-disable-next-line no-return-assign
46
+
47
+ const setDefinition = d => definition = d;
48
+
37
49
  return class extends Compact {
38
50
  constructor(registry, value) {
39
- super(registry, Type, value);
51
+ super(registry, Type, value, {
52
+ definition,
53
+ setDefinition
54
+ });
40
55
  }
41
56
 
42
57
  };
package/cjs/base/Enum.js CHANGED
@@ -13,6 +13,10 @@ var _Null = require("./Null");
13
13
 
14
14
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
15
15
  // SPDX-License-Identifier: Apache-2.0
16
+ function noopSetDefinition(d) {
17
+ return d;
18
+ }
19
+
16
20
  function isRustEnum(def) {
17
21
  const defValues = Object.values(def);
18
22
 
@@ -40,12 +44,11 @@ function extractDef(registry, _def) {
40
44
  isBasic = true;
41
45
  isIndexed = false;
42
46
  } else if (isRustEnum(_def)) {
43
- const entries = Object.entries((0, _utils.mapToTypeMap)(registry, _def));
47
+ const [Types, keys] = (0, _utils.mapToTypeMap)(registry, _def);
44
48
 
45
- for (let i = 0; i < entries.length; i++) {
46
- const [key, Type] = entries[i];
47
- def[key] = {
48
- Type,
49
+ for (let i = 0; i < keys.length; i++) {
50
+ def[keys[i]] = {
51
+ Type: Types[i],
49
52
  index: i
50
53
  };
51
54
  }
@@ -147,11 +150,15 @@ class Enum {
147
150
  #raw;
148
151
 
149
152
  constructor(registry, Types, value, index) {
153
+ let {
154
+ definition,
155
+ setDefinition = noopSetDefinition
156
+ } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
150
157
  const {
151
158
  def,
152
159
  isBasic,
153
160
  isIndexed
154
- } = extractDef(registry, Types); // shortcut isU8a as used in SCALE decoding
161
+ } = definition || setDefinition(extractDef(registry, Types)); // shortcut isU8a as used in SCALE decoding
155
162
 
156
163
  const decoded = (0, _util.isU8a)(value) && value.length && !(0, _util.isNumber)(index) ? createFromValue(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
157
164
  this.registry = registry;
@@ -183,9 +190,16 @@ class Enum {
183
190
  isKeys[i] = `is${name}`;
184
191
  }
185
192
 
193
+ let definition; // eslint-disable-next-line no-return-assign
194
+
195
+ const setDefinition = d => definition = d;
196
+
186
197
  return class extends Enum {
187
198
  constructor(registry, value, index) {
188
- super(registry, Types, value, index);
199
+ super(registry, Types, value, index, {
200
+ definition,
201
+ setDefinition
202
+ });
189
203
  (0, _util.objectProperties)(this, isKeys, (_, i) => this.type === keys[i]);
190
204
  (0, _util.objectProperties)(this, asKeys, (k, i) => {
191
205
  (0, _util.assert)(this[isKeys[i]], () => `Cannot convert '${this.type}' via ${k}`);
@@ -386,14 +400,15 @@ class Enum {
386
400
  }, {}) : this.defKeys;
387
401
  }
388
402
 
389
- const typeMap = Object.entries(this.#def).reduce((out, _ref3) => {
403
+ const entries = Object.entries(this.#def);
404
+ return (0, _utils.typesToMap)(this.registry, entries.reduce((out, _ref3, i) => {
390
405
  let [key, {
391
406
  Type
392
407
  }] = _ref3;
393
- out[key] = Type;
408
+ out[0][i] = Type;
409
+ out[1][i] = key;
394
410
  return out;
395
- }, {});
396
- return (0, _utils.typesToMap)(this.registry, typeMap);
411
+ }, [new Array(entries.length), new Array(entries.length)]));
397
412
  }
398
413
  /**
399
414
  * @description Returns the base runtime type name for this instance
@@ -13,6 +13,10 @@ var _Null = require("./Null");
13
13
 
14
14
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
15
15
  // SPDX-License-Identifier: Apache-2.0
16
+ function noopSetDefinition(d) {
17
+ return d;
18
+ }
19
+
16
20
  class None extends _Null.Null {
17
21
  /**
18
22
  * @description Returns the base runtime type name for this instance
@@ -60,7 +64,11 @@ class Option {
60
64
  #raw;
61
65
 
62
66
  constructor(registry, typeName, value) {
63
- const Type = (0, _utils.typeToConstructor)(registry, typeName);
67
+ let {
68
+ definition,
69
+ setDefinition = noopSetDefinition
70
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
71
+ const Type = definition || setDefinition((0, _utils.typeToConstructor)(registry, typeName));
64
72
  const decoded = (0, _util.isU8a)(value) && value.length && !(0, _util.isCodec)(value) ? value[0] === 0 ? new None(registry) : new Type(registry, value.subarray(1)) : decodeOption(registry, Type, value);
65
73
  this.registry = registry;
66
74
  this.#Type = Type;
@@ -72,9 +80,19 @@ class Option {
72
80
  }
73
81
 
74
82
  static with(Type) {
83
+ let definition;
84
+
85
+ const setDefinition = d => {
86
+ definition = d;
87
+ return d;
88
+ };
89
+
75
90
  return class extends Option {
76
91
  constructor(registry, value) {
77
- super(registry, Type, value);
92
+ super(registry, Type, value, {
93
+ definition,
94
+ setDefinition
95
+ });
78
96
  }
79
97
 
80
98
  };
package/cjs/base/Tuple.js CHANGED
@@ -13,27 +13,29 @@ var _utils = require("../utils");
13
13
 
14
14
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
15
15
  // SPDX-License-Identifier: Apache-2.0
16
-
16
+ function noopSetDefinition(d) {
17
+ return d;
18
+ }
17
19
  /** @internal */
18
- function decodeTuple(registry, Classes, value) {
20
+
21
+
22
+ function decodeTuple(registry, result, Classes, value) {
19
23
  if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
20
- return (0, _utils.decodeU8a)(registry, (0, _util.u8aToU8a)(value), Classes);
24
+ return (0, _utils.decodeU8a)(registry, result, (0, _util.u8aToU8a)(value), Classes);
21
25
  }
22
26
 
23
- const Types = Array.isArray(Classes) ? Classes : Object.values(Classes);
24
- return [Types.map((Type, index) => {
25
- try {
26
- const entry = value === null || value === void 0 ? void 0 : value[index];
27
-
28
- if (entry instanceof Type) {
29
- return entry;
30
- }
27
+ const Types = Classes[0];
31
28
 
32
- return new Type(registry, entry);
29
+ for (let i = 0; i < Types.length; i++) {
30
+ try {
31
+ const entry = value === null || value === void 0 ? void 0 : value[i];
32
+ result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
33
33
  } catch (error) {
34
- throw new Error(`Tuple: failed on ${index}:: ${error.message}`);
34
+ throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
35
35
  }
36
- }), 0];
36
+ }
37
+
38
+ return [result, 0];
37
39
  }
38
40
  /**
39
41
  * @name Tuple
@@ -47,16 +49,28 @@ class Tuple extends _AbstractArray.AbstractArray {
47
49
  #Types;
48
50
 
49
51
  constructor(registry, Types, value) {
50
- const Classes = Array.isArray(Types) ? Types.map(t => (0, _utils.typeToConstructor)(registry, t)) : (0, _util.isFunction)(Types) || (0, _util.isString)(Types) ? [(0, _utils.typeToConstructor)(registry, Types)] : (0, _utils.mapToTypeMap)(registry, Types);
51
- const [values, decodedLength] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8a)(registry, value, Classes) : decodeTuple(registry, Classes, value);
52
- super(registry, values, decodedLength);
52
+ let {
53
+ definition,
54
+ setDefinition = noopSetDefinition
55
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
56
+ const Classes = definition || setDefinition(Array.isArray(Types) ? [Types.map(t => (0, _utils.typeToConstructor)(registry, t)), []] : (0, _util.isFunction)(Types) || (0, _util.isString)(Types) ? [[(0, _utils.typeToConstructor)(registry, Types)], []] : (0, _utils.mapToTypeMap)(registry, Types));
57
+ super(registry, Classes[0].length);
58
+ const [, decodedLength] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8a)(registry, this, value, Classes, false) : decodeTuple(registry, this, Classes, value);
59
+ this.initialU8aLength = decodedLength;
53
60
  this.#Types = Classes;
54
61
  }
55
62
 
56
63
  static with(Types) {
64
+ let definition; // eslint-disable-next-line no-return-assign
65
+
66
+ const setDefinition = d => definition = d;
67
+
57
68
  return class extends Tuple {
58
69
  constructor(registry, value) {
59
- super(registry, Types, value);
70
+ super(registry, Types, value, {
71
+ definition,
72
+ setDefinition
73
+ });
60
74
  }
61
75
 
62
76
  };
@@ -81,7 +95,7 @@ class Tuple extends _AbstractArray.AbstractArray {
81
95
 
82
96
 
83
97
  get Types() {
84
- return Array.isArray(this.#Types) ? this.#Types.map(T => new T(this.registry).toRawType()) : Object.keys(this.#Types);
98
+ return this.#Types[1].length ? this.#Types[1] : this.#Types[0].map(T => new T(this.registry).toRawType());
85
99
  }
86
100
  /**
87
101
  * @description Returns a breakdown of the hex encoding for this Codec
@@ -99,7 +113,7 @@ class Tuple extends _AbstractArray.AbstractArray {
99
113
 
100
114
 
101
115
  toRawType() {
102
- const types = (Array.isArray(this.#Types) ? this.#Types : Object.values(this.#Types)).map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
116
+ const types = this.#Types[0].map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
103
117
  return `(${types.join(',')})`;
104
118
  }
105
119
  /**
package/cjs/base/Vec.js CHANGED
@@ -17,13 +17,26 @@ var _utils = require("../utils");
17
17
  const MAX_LENGTH = 64 * 1024;
18
18
  const l = (0, _util.logger)('Vec');
19
19
 
20
- function decodeVec(registry, Type, value) {
21
- let length = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1;
20
+ function noopSetDefinition(d) {
21
+ return d;
22
+ }
23
+
24
+ function decodeVecLength(value) {
25
+ if (Array.isArray(value)) {
26
+ return [value, value.length, 0];
27
+ }
28
+
29
+ const u8a = (0, _util.u8aToU8a)(value);
30
+ const [startAt, length] = (0, _util.compactFromU8a)(u8a);
31
+ (0, _util.assert)(length.lten(MAX_LENGTH), () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
32
+ return [u8a, length.toNumber(), startAt];
33
+ }
22
34
 
35
+ function decodeVec(registry, result, value, startAt, Type) {
23
36
  if (Array.isArray(value)) {
24
- const result = new Array(value.length);
37
+ const count = result.length;
25
38
 
26
- for (let i = 0; i < value.length; i++) {
39
+ for (let i = 0; i < count; i++) {
27
40
  const entry = value[i];
28
41
 
29
42
  try {
@@ -34,20 +47,10 @@ function decodeVec(registry, Type, value) {
34
47
  }
35
48
  }
36
49
 
37
- return [result, 0, 0];
50
+ return [0, 0];
38
51
  }
39
52
 
40
- const u8a = (0, _util.u8aToU8a)(value);
41
- let offset = 0;
42
-
43
- if (length === -1) {
44
- const [_offset, _length] = (0, _util.compactFromU8a)(u8a);
45
- (0, _util.assert)(_length.lten(MAX_LENGTH), () => `Vec length ${_length.toString()} exceeds ${MAX_LENGTH}`);
46
- length = _length.toNumber();
47
- offset = _offset;
48
- }
49
-
50
- return (0, _utils.decodeU8aVec)(registry, u8a, offset, Type, length);
53
+ return (0, _utils.decodeU8aVec)(registry, result, (0, _util.u8aToU8a)(value), startAt, Type);
51
54
  }
52
55
  /**
53
56
  * @name Vec
@@ -63,16 +66,28 @@ class Vec extends _AbstractArray.AbstractArray {
63
66
 
64
67
  constructor(registry, Type) {
65
68
  let value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
66
- const Clazz = (0, _utils.typeToConstructor)(registry, Type);
67
- const [values, decodedLength] = decodeVec(registry, Clazz, value);
68
- super(registry, values, decodedLength);
69
- this.#Type = Clazz;
69
+ let {
70
+ definition,
71
+ setDefinition = noopSetDefinition
72
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
73
+ const [decodeFrom, length, startAt] = decodeVecLength(value);
74
+ super(registry, length);
75
+ this.#Type = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
76
+ const [decodedLength] = (0, _util.isU8a)(decodeFrom) ? (0, _utils.decodeU8aVec)(registry, this, decodeFrom, startAt, this.#Type) : decodeVec(registry, this, decodeFrom, startAt, this.#Type);
77
+ this.initialU8aLength = decodedLength;
70
78
  }
71
79
 
72
80
  static with(Type) {
81
+ let definition; // eslint-disable-next-line no-return-assign
82
+
83
+ const setDefinition = d => definition = d;
84
+
73
85
  return class extends Vec {
74
86
  constructor(registry, value) {
75
- super(registry, Type, value);
87
+ super(registry, Type, value, {
88
+ definition,
89
+ setDefinition
90
+ });
76
91
  }
77
92
 
78
93
  };
@@ -15,17 +15,8 @@ var _Vec = require("./Vec");
15
15
 
16
16
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
17
17
  // SPDX-License-Identifier: Apache-2.0
18
-
19
- /** @internal */
20
- function decodeVecFixed(registry, value, Type, length) {
21
- const [values, decodedLength, decodedLengthNoOffset] = (0, _Vec.decodeVec)(registry, Type, value, length);
22
-
23
- while (values.length < length) {
24
- values.push(new Type(registry));
25
- }
26
-
27
- (0, _util.assert)(values.length === length, () => `Expected a length of exactly ${length} entries`);
28
- return [values, decodedLength, decodedLengthNoOffset];
18
+ function noopSetDefinition(d) {
19
+ return d;
29
20
  }
30
21
  /**
31
22
  * @name VecFixed
@@ -39,16 +30,27 @@ class VecFixed extends _AbstractArray.AbstractArray {
39
30
 
40
31
  constructor(registry, Type, length) {
41
32
  let value = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
42
- const Clazz = (0, _utils.typeToConstructor)(registry, Type);
43
- const [values,, decodedLengthNoOffset] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8aVec)(registry, value, 0, Clazz, length) : decodeVecFixed(registry, value, Clazz, length);
44
- super(registry, values, decodedLengthNoOffset);
45
- this.#Type = Clazz;
33
+ let {
34
+ definition,
35
+ setDefinition = noopSetDefinition
36
+ } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
37
+ super(registry, length);
38
+ this.#Type = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
39
+ const [, decodedLengthNoOffset] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8aVec)(registry, this, value, 0, this.#Type) : (0, _Vec.decodeVec)(registry, this, value, 0, this.#Type);
40
+ this.initialU8aLength = decodedLengthNoOffset;
46
41
  }
47
42
 
48
43
  static with(Type, length) {
44
+ let definition; // eslint-disable-next-line no-return-assign
45
+
46
+ const setDefinition = d => definition = d;
47
+
49
48
  return class extends VecFixed {
50
49
  constructor(registry, value) {
51
- super(registry, Type, length, value);
50
+ super(registry, Type, length, value, {
51
+ definition,
52
+ setDefinition
53
+ });
52
54
  }
53
55
 
54
56
  };
@@ -17,10 +17,12 @@ const l = (0, _util.logger)('BTreeSet');
17
17
  function decodeSetFromU8a(registry, ValClass, u8a) {
18
18
  const output = new Set();
19
19
  const [offset, length] = (0, _util.compactFromU8a)(u8a);
20
- const [values, decodedLength] = (0, _utils.decodeU8aVec)(registry, u8a, offset, ValClass, length.toNumber());
20
+ const count = length.toNumber();
21
+ const result = new Array(count);
22
+ const [decodedLength] = (0, _utils.decodeU8aVec)(registry, result, u8a, offset, ValClass);
21
23
 
22
- for (let i = 0; i < values.length; i++) {
23
- output.add(values[i]);
24
+ for (let i = 0; i < count; i++) {
25
+ output.add(result[i]);
24
26
  }
25
27
 
26
28
  return [ValClass, output, decodedLength];
@@ -30,7 +30,7 @@ function decodeMapFromU8a(registry, KeyClass, ValClass, u8a) {
30
30
  types.push(KeyClass, ValClass);
31
31
  }
32
32
 
33
- const [values, decodedLength] = (0, _utils.decodeU8a)(registry, u8a.subarray(offset), types);
33
+ const [values, decodedLength] = (0, _utils.decodeU8a)(registry, new Array(types.length), u8a.subarray(offset), [types, []]);
34
34
 
35
35
  for (let i = 0; i < values.length; i += 2) {
36
36
  output.set(values[i], values[i + 1]);
@@ -11,21 +11,25 @@ var _utils = require("../utils");
11
11
 
12
12
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
13
13
  // SPDX-License-Identifier: Apache-2.0
14
-
14
+ function noopSetDefinition(d) {
15
+ return d;
16
+ }
15
17
  /** @internal */
16
- function decodeStructFromObject(registry, Types, value, jsonMap) {
18
+
19
+
20
+ function decodeStructFromObject(registry, _ref, value, jsonMap) {
21
+ let [Types, keys] = _ref;
17
22
  let jsonObj;
18
- const inputKeys = Object.keys(Types);
19
23
  const typeofArray = Array.isArray(value);
20
24
  const typeofMap = value instanceof Map;
21
25
  (0, _util.assert)(typeofArray || typeofMap || (0, _util.isObject)(value), () => `Struct: Cannot decode value ${(0, _util.stringify)(value)} (typeof ${typeof value}), expected an input object, map or array`);
22
- (0, _util.assert)(!typeofArray || value.length === inputKeys.length, () => `Struct: Unable to map ${(0, _util.stringify)(value)} array to object with known keys ${inputKeys.join(', ')}`);
23
- const raw = new Array(inputKeys.length);
26
+ (0, _util.assert)(!typeofArray || value.length === keys.length, () => `Struct: Unable to map ${(0, _util.stringify)(value)} array to object with known keys ${keys.join(', ')}`);
27
+ const raw = new Array(keys.length);
24
28
 
25
- for (let i = 0; i < inputKeys.length; i++) {
26
- const key = inputKeys[i];
29
+ for (let i = 0; i < keys.length; i++) {
30
+ const key = keys[i];
27
31
  const jsonKey = jsonMap.get(key) || key;
28
- const Type = Types[key];
32
+ const Type = Types[i];
29
33
  let assign;
30
34
 
31
35
  try {
@@ -84,8 +88,12 @@ class Struct extends Map {
84
88
 
85
89
  constructor(registry, Types, value) {
86
90
  let jsonMap = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new Map();
87
- const typeMap = (0, _utils.mapToTypeMap)(registry, Types);
88
- const [decoded, decodedLength] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8a)(registry, value, typeMap, true) : (0, _util.isHex)(value) ? (0, _utils.decodeU8a)(registry, (0, _util.u8aToU8a)(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
91
+ let {
92
+ definition,
93
+ setDefinition = noopSetDefinition
94
+ } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
95
+ const typeMap = definition || setDefinition((0, _utils.mapToTypeMap)(registry, Types));
96
+ const [decoded, decodedLength] = (0, _util.isU8a)(value) || (0, _util.isHex)(value) ? (0, _utils.decodeU8a)(registry, new Array(typeMap[0].length), (0, _util.u8aToU8a)(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
89
97
  super(decoded);
90
98
  this.#registry = registry;
91
99
  this.initialU8aLength = decodedLength;
@@ -95,9 +103,16 @@ class Struct extends Map {
95
103
 
96
104
  static with(Types, jsonMap) {
97
105
  const keys = Object.keys(Types);
106
+ let definition; // eslint-disable-next-line no-return-assign
107
+
108
+ const setDefinition = d => definition = d;
109
+
98
110
  return class extends Struct {
99
111
  constructor(registry, value) {
100
- super(registry, Types, value, jsonMap);
112
+ super(registry, Types, value, jsonMap, {
113
+ definition,
114
+ setDefinition
115
+ });
101
116
  (0, _util.objectProperties)(this, keys, k => this.get(k));
102
117
  }
103
118
 
@@ -109,7 +124,7 @@ class Struct extends Map {
109
124
 
110
125
 
111
126
  get defKeys() {
112
- return Object.keys(this.#Types);
127
+ return this.#Types[1];
113
128
  }
114
129
 
115
130
  getT(key) {
@@ -136,11 +151,10 @@ class Struct extends Map {
136
151
 
137
152
  get Type() {
138
153
  const result = {};
139
- const defs = Object.entries(this.#Types);
154
+ const [Types, keys] = this.#Types;
140
155
 
141
- for (let i = 0; i < defs.length; i++) {
142
- const [key, Type] = defs[i];
143
- result[key] = new Type(this.registry).toRawType();
156
+ for (let i = 0; i < keys.length; i++) {
157
+ result[keys[i]] = new Types[i](this.registry).toRawType();
144
158
  }
145
159
 
146
160
  return result;
@@ -11,6 +11,6 @@ const packageInfo = {
11
11
  name: '@polkadot/types-codec',
12
12
  path: typeof __dirname === 'string' ? __dirname : 'auto',
13
13
  type: 'cjs',
14
- version: '8.5.2-4'
14
+ version: '8.5.2-7'
15
15
  };
16
16
  exports.packageInfo = packageInfo;
@@ -31,12 +31,12 @@ function getRawType(registry, Type) {
31
31
  */
32
32
 
33
33
 
34
- function decodeU8a(registry, u8a, types, withZip) {
35
- const [Types, keys] = Array.isArray(types) ? [types, []] : [Object.values(types), Object.keys(types)];
36
- const result = new Array(Types.length);
34
+ function decodeU8a(registry, result, u8a, _ref, withZip) {
35
+ let [Types, keys] = _ref;
36
+ const count = result.length;
37
37
  let offset = 0;
38
38
 
39
- for (let i = 0; i < Types.length; i++) {
39
+ for (let i = 0; i < count; i++) {
40
40
  try {
41
41
  const value = new Types[i](registry, u8a.subarray(offset));
42
42
  offset += value.initialU8aLength || value.encodedLength;
@@ -51,9 +51,9 @@ function decodeU8a(registry, u8a, types, withZip) {
51
51
  // since we have to do less checks (and these are intensive anyway), much faster
52
52
 
53
53
 
54
- function decodeU8aVec(registry, u8a, start, Type, count) {
55
- const result = new Array(count);
56
- let offset = start;
54
+ function decodeU8aVec(registry, result, u8a, startAt, Type) {
55
+ const count = result.length;
56
+ let offset = startAt;
57
57
 
58
58
  for (let i = 0; i < count; i++) {
59
59
  try {
@@ -65,5 +65,5 @@ function decodeU8aVec(registry, u8a, start, Type, count) {
65
65
  }
66
66
  }
67
67
 
68
- return [result, offset, offset - start];
68
+ return [offset, offset - startAt];
69
69
  }
@@ -15,10 +15,12 @@ var _typeToConstructor = require("./typeToConstructor");
15
15
  */
16
16
  function mapToTypeMap(registry, input) {
17
17
  const entries = Object.entries(input);
18
- const output = {};
18
+ const count = entries.length;
19
+ const output = [new Array(count), new Array(count)];
19
20
 
20
21
  for (let i = 0; i < entries.length; i++) {
21
- output[entries[i][0]] = (0, _typeToConstructor.typeToConstructor)(registry, entries[i][1]);
22
+ output[1][i] = entries[i][0];
23
+ output[0][i] = (0, _typeToConstructor.typeToConstructor)(registry, entries[i][1]);
22
24
  }
23
25
 
24
26
  return output;
@@ -7,13 +7,12 @@ exports.typesToMap = typesToMap;
7
7
 
8
8
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
9
9
  // SPDX-License-Identifier: Apache-2.0
10
- function typesToMap(registry, Types) {
11
- const entries = Object.entries(Types);
10
+ function typesToMap(registry, _ref) {
11
+ let [Types, keys] = _ref;
12
12
  const result = {};
13
13
 
14
- for (let i = 0; i < entries.length; i++) {
15
- const [key, Type] = entries[i];
16
- result[key] = registry.getClassName(Type) || new Type(registry).toRawType();
14
+ for (let i = 0; i < keys.length; i++) {
15
+ result[keys[i]] = registry.getClassName(Types[i]) || new Types[i](registry).toRawType();
17
16
  }
18
17
 
19
18
  return result;
@@ -8,10 +8,12 @@ const l = logger('BTreeSet');
8
8
  function decodeSetFromU8a(registry, ValClass, u8a) {
9
9
  const output = new Set();
10
10
  const [offset, length] = compactFromU8a(u8a);
11
- const [values, decodedLength] = decodeU8aVec(registry, u8a, offset, ValClass, length.toNumber());
11
+ const count = length.toNumber();
12
+ const result = new Array(count);
13
+ const [decodedLength] = decodeU8aVec(registry, result, u8a, offset, ValClass);
12
14
 
13
- for (let i = 0; i < values.length; i++) {
14
- output.add(values[i]);
15
+ for (let i = 0; i < count; i++) {
16
+ output.add(result[i]);
15
17
  }
16
18
 
17
19
  return [ValClass, output, decodedLength];
package/extended/Map.js CHANGED
@@ -18,7 +18,7 @@ function decodeMapFromU8a(registry, KeyClass, ValClass, u8a) {
18
18
  types.push(KeyClass, ValClass);
19
19
  }
20
20
 
21
- const [values, decodedLength] = decodeU8a(registry, u8a.subarray(offset), types);
21
+ const [values, decodedLength] = decodeU8a(registry, new Array(types.length), u8a.subarray(offset), [types, []]);
22
22
 
23
23
  for (let i = 0; i < values.length; i += 2) {
24
24
  output.set(values[i], values[i + 1]);
@@ -1,6 +1,11 @@
1
1
  import type { HexString } from '@polkadot/util/types';
2
2
  import type { AnyJson, BareOpts, Codec, CodecClass, Inspect, IStruct, IU8a, Registry } from '../types';
3
3
  declare type TypesDef<T = Codec> = Record<string, string | CodecClass<T>>;
4
+ declare type Definition = [CodecClass[], string[]];
5
+ interface Options {
6
+ definition?: Definition;
7
+ setDefinition?: (d: Definition) => Definition;
8
+ }
4
9
  /**
5
10
  * @name Struct
6
11
  * @description
@@ -23,7 +28,7 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
23
28
  #private;
24
29
  createdAtHash?: IU8a;
25
30
  readonly initialU8aLength?: number;
26
- constructor(registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string>);
31
+ constructor(registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string>, { definition, setDefinition }?: Options);
27
32
  static with<S extends TypesDef>(Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>>;
28
33
  /**
29
34
  * @description The available keys for this struct