@polkadot/types-codec 8.5.2-6 → 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.
@@ -10,8 +10,7 @@ import type { AnyJson, Codec, Inspect, IU8a, IVec, Registry } from '../types';
10
10
  export declare abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
11
11
  readonly registry: Registry;
12
12
  createdAtHash?: IU8a;
13
- readonly initialU8aLength?: number;
14
- protected constructor(registry: Registry, values: T[], initialU8aLength?: number);
13
+ protected constructor(registry: Registry, length: number);
15
14
  /**
16
15
  * @description The length of the value when encoded as a Uint8Array
17
16
  */
@@ -11,15 +11,9 @@ import { compareArray } from "../utils/compareArray.js";
11
11
  */
12
12
 
13
13
  export class AbstractArray extends Array {
14
- constructor(registry, values, initialU8aLength) {
15
- super(values.length); // explicitly set the values here - this removes the need for any extra allocations
16
-
17
- for (let i = 0; i < values.length; i++) {
18
- this[i] = values[i];
19
- }
20
-
14
+ constructor(registry, length) {
15
+ super(length);
21
16
  this.registry = registry;
22
- this.initialU8aLength = initialU8aLength;
23
17
  }
24
18
  /**
25
19
  * @description The length of the value when encoded as a Uint8Array
package/base/Enum.js CHANGED
@@ -35,12 +35,11 @@ function extractDef(registry, _def) {
35
35
  isBasic = true;
36
36
  isIndexed = false;
37
37
  } else if (isRustEnum(_def)) {
38
- const entries = Object.entries(mapToTypeMap(registry, _def));
38
+ const [Types, keys] = mapToTypeMap(registry, _def);
39
39
 
40
- for (let i = 0; i < entries.length; i++) {
41
- const [key, Type] = entries[i];
42
- def[key] = {
43
- Type,
40
+ for (let i = 0; i < keys.length; i++) {
41
+ def[keys[i]] = {
42
+ Type: Types[i],
44
43
  index: i
45
44
  };
46
45
  }
@@ -383,13 +382,14 @@ export class Enum {
383
382
  }, {}) : this.defKeys;
384
383
  }
385
384
 
386
- const typeMap = Object.entries(this.#def).reduce((out, [key, {
385
+ const entries = Object.entries(this.#def);
386
+ return typesToMap(this.registry, entries.reduce((out, [key, {
387
387
  Type
388
- }]) => {
389
- out[key] = Type;
388
+ }], i) => {
389
+ out[0][i] = Type;
390
+ out[1][i] = key;
390
391
  return out;
391
- }, {});
392
- return typesToMap(this.registry, typeMap);
392
+ }, [new Array(entries.length), new Array(entries.length)]));
393
393
  }
394
394
  /**
395
395
  * @description Returns the base runtime type name for this instance
package/base/Tuple.d.ts CHANGED
@@ -1,15 +1,13 @@
1
1
  import type { AnyTupleValue, Codec, CodecClass, Inspect, ITuple, Registry } from '../types';
2
2
  import { AbstractArray } from '../abstract/AbstractArray';
3
- declare type TupleCodecClass = CodecClass[] | {
4
- [index: string]: CodecClass;
5
- };
6
3
  declare type TupleType = (CodecClass | string);
7
4
  declare type TupleTypes = TupleType[] | {
8
5
  [index: string]: CodecClass | string;
9
6
  };
7
+ declare type Definition = [CodecClass[], string[]];
10
8
  interface Options {
11
- definition?: TupleCodecClass;
12
- setDefinition?: (d: TupleCodecClass) => TupleCodecClass;
9
+ definition?: Definition;
10
+ setDefinition?: (d: Definition) => Definition;
13
11
  }
14
12
  /**
15
13
  * @name Tuple
@@ -19,6 +17,7 @@ interface Options {
19
17
  */
20
18
  export declare class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
21
19
  #private;
20
+ readonly initialU8aLength?: number;
22
21
  constructor(registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue, { definition, setDefinition }?: Options);
23
22
  static with(Types: TupleTypes | TupleType): CodecClass<Tuple>;
24
23
  /**
package/base/Tuple.js CHANGED
@@ -10,25 +10,23 @@ function noopSetDefinition(d) {
10
10
  /** @internal */
11
11
 
12
12
 
13
- function decodeTuple(registry, Classes, value) {
13
+ function decodeTuple(registry, result, Classes, value) {
14
14
  if (isU8a(value) || isHex(value)) {
15
- return decodeU8a(registry, u8aToU8a(value), Classes);
15
+ return decodeU8a(registry, result, u8aToU8a(value), Classes);
16
16
  }
17
17
 
18
- const Types = Array.isArray(Classes) ? Classes : Object.values(Classes);
19
- return [Types.map((Type, index) => {
20
- try {
21
- const entry = value === null || value === void 0 ? void 0 : value[index];
22
-
23
- if (entry instanceof Type) {
24
- return entry;
25
- }
18
+ const Types = Classes[0];
26
19
 
27
- return new Type(registry, entry);
20
+ for (let i = 0; i < Types.length; i++) {
21
+ try {
22
+ const entry = value === null || value === void 0 ? void 0 : value[i];
23
+ result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
28
24
  } catch (error) {
29
- throw new Error(`Tuple: failed on ${index}:: ${error.message}`);
25
+ throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
30
26
  }
31
- }), 0];
27
+ }
28
+
29
+ return [result, 0];
32
30
  }
33
31
  /**
34
32
  * @name Tuple
@@ -45,9 +43,10 @@ export class Tuple extends AbstractArray {
45
43
  definition,
46
44
  setDefinition = noopSetDefinition
47
45
  } = {}) {
48
- const Classes = definition || setDefinition(Array.isArray(Types) ? Types.map(t => typeToConstructor(registry, t)) : isFunction(Types) || isString(Types) ? [typeToConstructor(registry, Types)] : mapToTypeMap(registry, Types));
49
- const [values, decodedLength] = isU8a(value) ? decodeU8a(registry, value, Classes) : decodeTuple(registry, Classes, value);
50
- super(registry, values, decodedLength);
46
+ const Classes = definition || setDefinition(Array.isArray(Types) ? [Types.map(t => typeToConstructor(registry, t)), []] : isFunction(Types) || isString(Types) ? [[typeToConstructor(registry, Types)], []] : mapToTypeMap(registry, Types));
47
+ super(registry, Classes[0].length);
48
+ const [, decodedLength] = isU8a(value) ? decodeU8a(registry, this, value, Classes, false) : decodeTuple(registry, this, Classes, value);
49
+ this.initialU8aLength = decodedLength;
51
50
  this.#Types = Classes;
52
51
  }
53
52
 
@@ -86,7 +85,7 @@ export class Tuple extends AbstractArray {
86
85
 
87
86
 
88
87
  get Types() {
89
- return Array.isArray(this.#Types) ? this.#Types.map(T => new T(this.registry).toRawType()) : Object.keys(this.#Types);
88
+ return this.#Types[1].length ? this.#Types[1] : this.#Types[0].map(T => new T(this.registry).toRawType());
90
89
  }
91
90
  /**
92
91
  * @description Returns a breakdown of the hex encoding for this Codec
@@ -104,7 +103,7 @@ export class Tuple extends AbstractArray {
104
103
 
105
104
 
106
105
  toRawType() {
107
- const types = (Array.isArray(this.#Types) ? this.#Types : Object.values(this.#Types)).map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
106
+ const types = this.#Types[0].map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
108
107
  return `(${types.join(',')})`;
109
108
  }
110
109
  /**
package/base/Vec.d.ts CHANGED
@@ -5,7 +5,7 @@ interface Options<T> {
5
5
  definition?: CodecClass<T>;
6
6
  setDefinition?: (d: CodecClass<T>) => CodecClass<T>;
7
7
  }
8
- export declare function decodeVec<T extends Codec>(registry: Registry, Type: CodecClass<T>, value: Uint8Array | HexString | unknown[], length?: number): [T[], number, number];
8
+ export declare function decodeVec<T extends Codec>(registry: Registry, result: T[], value: Uint8Array | HexString | unknown[], startAt: number, Type: CodecClass<T>): [number, number];
9
9
  /**
10
10
  * @name Vec
11
11
  * @description
@@ -15,6 +15,7 @@ export declare function decodeVec<T extends Codec>(registry: Registry, Type: Cod
15
15
  */
16
16
  export declare class Vec<T extends Codec> extends AbstractArray<T> {
17
17
  #private;
18
+ readonly initialU8aLength?: number;
18
19
  constructor(registry: Registry, Type: CodecClass<T> | string, value?: Uint8Array | HexString | unknown[], { definition, setDefinition }?: Options<T>);
19
20
  static with<O extends Codec>(Type: CodecClass<O> | string): CodecClass<Vec<O>>;
20
21
  /**
package/base/Vec.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { assert, compactFromU8a, logger, u8aToU8a } from '@polkadot/util';
3
+ import { assert, compactFromU8a, isU8a, logger, u8aToU8a } from '@polkadot/util';
4
4
  import { AbstractArray } from "../abstract/AbstractArray.js";
5
5
  import { decodeU8aVec, typeToConstructor } from "../utils/index.js";
6
6
  const MAX_LENGTH = 64 * 1024;
@@ -10,11 +10,22 @@ function noopSetDefinition(d) {
10
10
  return d;
11
11
  }
12
12
 
13
- export function decodeVec(registry, Type, value, length = -1) {
13
+ function decodeVecLength(value) {
14
14
  if (Array.isArray(value)) {
15
- const result = new Array(value.length);
15
+ return [value, value.length, 0];
16
+ }
17
+
18
+ const u8a = u8aToU8a(value);
19
+ const [startAt, length] = compactFromU8a(u8a);
20
+ assert(length.lten(MAX_LENGTH), () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
21
+ return [u8a, length.toNumber(), startAt];
22
+ }
23
+
24
+ export function decodeVec(registry, result, value, startAt, Type) {
25
+ if (Array.isArray(value)) {
26
+ const count = result.length;
16
27
 
17
- for (let i = 0; i < value.length; i++) {
28
+ for (let i = 0; i < count; i++) {
18
29
  const entry = value[i];
19
30
 
20
31
  try {
@@ -25,20 +36,10 @@ export function decodeVec(registry, Type, value, length = -1) {
25
36
  }
26
37
  }
27
38
 
28
- return [result, 0, 0];
29
- }
30
-
31
- const u8a = u8aToU8a(value);
32
- let offset = 0;
33
-
34
- if (length === -1) {
35
- const [_offset, _length] = compactFromU8a(u8a);
36
- assert(_length.lten(MAX_LENGTH), () => `Vec length ${_length.toString()} exceeds ${MAX_LENGTH}`);
37
- length = _length.toNumber();
38
- offset = _offset;
39
+ return [0, 0];
39
40
  }
40
41
 
41
- return decodeU8aVec(registry, u8a, offset, Type, length);
42
+ return decodeU8aVec(registry, result, u8aToU8a(value), startAt, Type);
42
43
  }
43
44
  /**
44
45
  * @name Vec
@@ -55,10 +56,11 @@ export class Vec extends AbstractArray {
55
56
  definition,
56
57
  setDefinition = noopSetDefinition
57
58
  } = {}) {
58
- const Clazz = definition || setDefinition(typeToConstructor(registry, Type));
59
- const [values, decodedLength] = decodeVec(registry, Clazz, value);
60
- super(registry, values, decodedLength);
61
- this.#Type = Clazz;
59
+ const [decodeFrom, length, startAt] = decodeVecLength(value);
60
+ super(registry, length);
61
+ this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
62
+ const [decodedLength] = isU8a(decodeFrom) ? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type) : decodeVec(registry, this, decodeFrom, startAt, this.#Type);
63
+ this.initialU8aLength = decodedLength;
62
64
  }
63
65
 
64
66
  static with(Type) {
@@ -12,6 +12,7 @@ interface Options<T> {
12
12
  */
13
13
  export declare class VecFixed<T extends Codec> extends AbstractArray<T> {
14
14
  #private;
15
+ readonly initialU8aLength?: number;
15
16
  constructor(registry: Registry, Type: CodecClass<T> | string, length: number, value?: Uint8Array | HexString | unknown[], { definition, setDefinition }?: Options<T>);
16
17
  static with<O extends Codec>(Type: CodecClass<O> | string, length: number): CodecClass<VecFixed<O>>;
17
18
  /**
package/base/VecFixed.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { assert, isU8a, u8aConcat } from '@polkadot/util';
3
+ import { isU8a, u8aConcat } from '@polkadot/util';
4
4
  import { AbstractArray } from "../abstract/AbstractArray.js";
5
5
  import { decodeU8aVec, typeToConstructor } from "../utils/index.js";
6
6
  import { decodeVec } from "./Vec.js";
@@ -8,19 +8,6 @@ import { decodeVec } from "./Vec.js";
8
8
  function noopSetDefinition(d) {
9
9
  return d;
10
10
  }
11
- /** @internal */
12
-
13
-
14
- function decodeVecFixed(registry, value, Type, length) {
15
- const [values, decodedLength, decodedLengthNoOffset] = decodeVec(registry, Type, value, length);
16
-
17
- while (values.length < length) {
18
- values.push(new Type(registry));
19
- }
20
-
21
- assert(values.length === length, () => `Expected a length of exactly ${length} entries`);
22
- return [values, decodedLength, decodedLengthNoOffset];
23
- }
24
11
  /**
25
12
  * @name VecFixed
26
13
  * @description
@@ -35,10 +22,10 @@ export class VecFixed extends AbstractArray {
35
22
  definition,
36
23
  setDefinition = noopSetDefinition
37
24
  } = {}) {
38
- const Clazz = definition || setDefinition(typeToConstructor(registry, Type));
39
- const [values,, decodedLengthNoOffset] = isU8a(value) ? decodeU8aVec(registry, value, 0, Clazz, length) : decodeVecFixed(registry, value, Clazz, length);
40
- super(registry, values, decodedLengthNoOffset);
41
- this.#Type = Clazz;
25
+ super(registry, length);
26
+ this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
27
+ const [, decodedLengthNoOffset] = isU8a(value) ? decodeU8aVec(registry, this, value, 0, this.#Type) : decodeVec(registry, this, value, 0, this.#Type);
28
+ this.initialU8aLength = decodedLengthNoOffset;
42
29
  }
43
30
 
44
31
  static with(Type, length) {
@@ -20,15 +20,9 @@ var _compareArray = require("../utils/compareArray");
20
20
  * @noInheritDoc
21
21
  */
22
22
  class AbstractArray extends Array {
23
- constructor(registry, values, initialU8aLength) {
24
- super(values.length); // explicitly set the values here - this removes the need for any extra allocations
25
-
26
- for (let i = 0; i < values.length; i++) {
27
- this[i] = values[i];
28
- }
29
-
23
+ constructor(registry, length) {
24
+ super(length);
30
25
  this.registry = registry;
31
- this.initialU8aLength = initialU8aLength;
32
26
  }
33
27
  /**
34
28
  * @description The length of the value when encoded as a Uint8Array
package/cjs/base/Enum.js CHANGED
@@ -44,12 +44,11 @@ function extractDef(registry, _def) {
44
44
  isBasic = true;
45
45
  isIndexed = false;
46
46
  } else if (isRustEnum(_def)) {
47
- const entries = Object.entries((0, _utils.mapToTypeMap)(registry, _def));
47
+ const [Types, keys] = (0, _utils.mapToTypeMap)(registry, _def);
48
48
 
49
- for (let i = 0; i < entries.length; i++) {
50
- const [key, Type] = entries[i];
51
- def[key] = {
52
- Type,
49
+ for (let i = 0; i < keys.length; i++) {
50
+ def[keys[i]] = {
51
+ Type: Types[i],
53
52
  index: i
54
53
  };
55
54
  }
@@ -401,14 +400,15 @@ class Enum {
401
400
  }, {}) : this.defKeys;
402
401
  }
403
402
 
404
- 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) => {
405
405
  let [key, {
406
406
  Type
407
407
  }] = _ref3;
408
- out[key] = Type;
408
+ out[0][i] = Type;
409
+ out[1][i] = key;
409
410
  return out;
410
- }, {});
411
- return (0, _utils.typesToMap)(this.registry, typeMap);
411
+ }, [new Array(entries.length), new Array(entries.length)]));
412
412
  }
413
413
  /**
414
414
  * @description Returns the base runtime type name for this instance
package/cjs/base/Tuple.js CHANGED
@@ -19,25 +19,23 @@ function noopSetDefinition(d) {
19
19
  /** @internal */
20
20
 
21
21
 
22
- function decodeTuple(registry, Classes, value) {
22
+ function decodeTuple(registry, result, Classes, value) {
23
23
  if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
24
- return (0, _utils.decodeU8a)(registry, (0, _util.u8aToU8a)(value), Classes);
24
+ return (0, _utils.decodeU8a)(registry, result, (0, _util.u8aToU8a)(value), Classes);
25
25
  }
26
26
 
27
- const Types = Array.isArray(Classes) ? Classes : Object.values(Classes);
28
- return [Types.map((Type, index) => {
29
- try {
30
- const entry = value === null || value === void 0 ? void 0 : value[index];
31
-
32
- if (entry instanceof Type) {
33
- return entry;
34
- }
27
+ const Types = Classes[0];
35
28
 
36
- 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);
37
33
  } catch (error) {
38
- throw new Error(`Tuple: failed on ${index}:: ${error.message}`);
34
+ throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
39
35
  }
40
- }), 0];
36
+ }
37
+
38
+ return [result, 0];
41
39
  }
42
40
  /**
43
41
  * @name Tuple
@@ -55,9 +53,10 @@ class Tuple extends _AbstractArray.AbstractArray {
55
53
  definition,
56
54
  setDefinition = noopSetDefinition
57
55
  } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
58
- 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));
59
- const [values, decodedLength] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8a)(registry, value, Classes) : decodeTuple(registry, Classes, value);
60
- super(registry, values, decodedLength);
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;
61
60
  this.#Types = Classes;
62
61
  }
63
62
 
@@ -96,7 +95,7 @@ class Tuple extends _AbstractArray.AbstractArray {
96
95
 
97
96
 
98
97
  get Types() {
99
- 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());
100
99
  }
101
100
  /**
102
101
  * @description Returns a breakdown of the hex encoding for this Codec
@@ -114,7 +113,7 @@ class Tuple extends _AbstractArray.AbstractArray {
114
113
 
115
114
 
116
115
  toRawType() {
117
- 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());
118
117
  return `(${types.join(',')})`;
119
118
  }
120
119
  /**
package/cjs/base/Vec.js CHANGED
@@ -21,13 +21,22 @@ function noopSetDefinition(d) {
21
21
  return d;
22
22
  }
23
23
 
24
- function decodeVec(registry, Type, value) {
25
- let length = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1;
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
+ }
26
34
 
35
+ function decodeVec(registry, result, value, startAt, Type) {
27
36
  if (Array.isArray(value)) {
28
- const result = new Array(value.length);
37
+ const count = result.length;
29
38
 
30
- for (let i = 0; i < value.length; i++) {
39
+ for (let i = 0; i < count; i++) {
31
40
  const entry = value[i];
32
41
 
33
42
  try {
@@ -38,20 +47,10 @@ function decodeVec(registry, Type, value) {
38
47
  }
39
48
  }
40
49
 
41
- return [result, 0, 0];
42
- }
43
-
44
- const u8a = (0, _util.u8aToU8a)(value);
45
- let offset = 0;
46
-
47
- if (length === -1) {
48
- const [_offset, _length] = (0, _util.compactFromU8a)(u8a);
49
- (0, _util.assert)(_length.lten(MAX_LENGTH), () => `Vec length ${_length.toString()} exceeds ${MAX_LENGTH}`);
50
- length = _length.toNumber();
51
- offset = _offset;
50
+ return [0, 0];
52
51
  }
53
52
 
54
- return (0, _utils.decodeU8aVec)(registry, u8a, offset, Type, length);
53
+ return (0, _utils.decodeU8aVec)(registry, result, (0, _util.u8aToU8a)(value), startAt, Type);
55
54
  }
56
55
  /**
57
56
  * @name Vec
@@ -71,10 +70,11 @@ class Vec extends _AbstractArray.AbstractArray {
71
70
  definition,
72
71
  setDefinition = noopSetDefinition
73
72
  } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
74
- const Clazz = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
75
- const [values, decodedLength] = decodeVec(registry, Clazz, value);
76
- super(registry, values, decodedLength);
77
- this.#Type = Clazz;
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;
78
78
  }
79
79
 
80
80
  static with(Type) {
@@ -18,19 +18,6 @@ var _Vec = require("./Vec");
18
18
  function noopSetDefinition(d) {
19
19
  return d;
20
20
  }
21
- /** @internal */
22
-
23
-
24
- function decodeVecFixed(registry, value, Type, length) {
25
- const [values, decodedLength, decodedLengthNoOffset] = (0, _Vec.decodeVec)(registry, Type, value, length);
26
-
27
- while (values.length < length) {
28
- values.push(new Type(registry));
29
- }
30
-
31
- (0, _util.assert)(values.length === length, () => `Expected a length of exactly ${length} entries`);
32
- return [values, decodedLength, decodedLengthNoOffset];
33
- }
34
21
  /**
35
22
  * @name VecFixed
36
23
  * @description
@@ -47,10 +34,10 @@ class VecFixed extends _AbstractArray.AbstractArray {
47
34
  definition,
48
35
  setDefinition = noopSetDefinition
49
36
  } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
50
- const Clazz = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
51
- const [values,, decodedLengthNoOffset] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8aVec)(registry, value, 0, Clazz, length) : decodeVecFixed(registry, value, Clazz, length);
52
- super(registry, values, decodedLengthNoOffset);
53
- this.#Type = Clazz;
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;
54
41
  }
55
42
 
56
43
  static with(Type, length) {
@@ -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]);
@@ -17,19 +17,19 @@ function noopSetDefinition(d) {
17
17
  /** @internal */
18
18
 
19
19
 
20
- function decodeStructFromObject(registry, Types, value, jsonMap) {
20
+ function decodeStructFromObject(registry, _ref, value, jsonMap) {
21
+ let [Types, keys] = _ref;
21
22
  let jsonObj;
22
- const inputKeys = Object.keys(Types);
23
23
  const typeofArray = Array.isArray(value);
24
24
  const typeofMap = value instanceof Map;
25
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`);
26
- (0, _util.assert)(!typeofArray || value.length === inputKeys.length, () => `Struct: Unable to map ${(0, _util.stringify)(value)} array to object with known keys ${inputKeys.join(', ')}`);
27
- 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);
28
28
 
29
- for (let i = 0; i < inputKeys.length; i++) {
30
- const key = inputKeys[i];
29
+ for (let i = 0; i < keys.length; i++) {
30
+ const key = keys[i];
31
31
  const jsonKey = jsonMap.get(key) || key;
32
- const Type = Types[key];
32
+ const Type = Types[i];
33
33
  let assign;
34
34
 
35
35
  try {
@@ -93,7 +93,7 @@ class Struct extends Map {
93
93
  setDefinition = noopSetDefinition
94
94
  } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
95
95
  const typeMap = definition || setDefinition((0, _utils.mapToTypeMap)(registry, Types));
96
- 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);
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);
97
97
  super(decoded);
98
98
  this.#registry = registry;
99
99
  this.initialU8aLength = decodedLength;
@@ -124,7 +124,7 @@ class Struct extends Map {
124
124
 
125
125
 
126
126
  get defKeys() {
127
- return Object.keys(this.#Types);
127
+ return this.#Types[1];
128
128
  }
129
129
 
130
130
  getT(key) {
@@ -151,11 +151,10 @@ class Struct extends Map {
151
151
 
152
152
  get Type() {
153
153
  const result = {};
154
- const defs = Object.entries(this.#Types);
154
+ const [Types, keys] = this.#Types;
155
155
 
156
- for (let i = 0; i < defs.length; i++) {
157
- const [key, Type] = defs[i];
158
- 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();
159
158
  }
160
159
 
161
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-6'
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,9 +1,10 @@
1
1
  import type { HexString } from '@polkadot/util/types';
2
- import type { AnyJson, BareOpts, Codec, CodecClass, CodecClassDef, Inspect, IStruct, IU8a, Registry } from '../types';
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[]];
4
5
  interface Options {
5
- definition?: CodecClassDef;
6
- setDefinition?: (d: CodecClassDef) => CodecClassDef;
6
+ definition?: Definition;
7
+ setDefinition?: (d: Definition) => Definition;
7
8
  }
8
9
  /**
9
10
  * @name Struct
package/native/Struct.js CHANGED
@@ -9,19 +9,18 @@ function noopSetDefinition(d) {
9
9
  /** @internal */
10
10
 
11
11
 
12
- function decodeStructFromObject(registry, Types, value, jsonMap) {
12
+ function decodeStructFromObject(registry, [Types, keys], value, jsonMap) {
13
13
  let jsonObj;
14
- const inputKeys = Object.keys(Types);
15
14
  const typeofArray = Array.isArray(value);
16
15
  const typeofMap = value instanceof Map;
17
16
  assert(typeofArray || typeofMap || isObject(value), () => `Struct: Cannot decode value ${stringify(value)} (typeof ${typeof value}), expected an input object, map or array`);
18
- assert(!typeofArray || value.length === inputKeys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${inputKeys.join(', ')}`);
19
- const raw = new Array(inputKeys.length);
17
+ assert(!typeofArray || value.length === keys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${keys.join(', ')}`);
18
+ const raw = new Array(keys.length);
20
19
 
21
- for (let i = 0; i < inputKeys.length; i++) {
22
- const key = inputKeys[i];
20
+ for (let i = 0; i < keys.length; i++) {
21
+ const key = keys[i];
23
22
  const jsonKey = jsonMap.get(key) || key;
24
- const Type = Types[key];
23
+ const Type = Types[i];
25
24
  let assign;
26
25
 
27
26
  try {
@@ -83,7 +82,7 @@ export class Struct extends Map {
83
82
  setDefinition = noopSetDefinition
84
83
  } = {}) {
85
84
  const typeMap = definition || setDefinition(mapToTypeMap(registry, Types));
86
- const [decoded, decodedLength] = isU8a(value) ? decodeU8a(registry, value, typeMap, true) : isHex(value) ? decodeU8a(registry, u8aToU8a(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
85
+ const [decoded, decodedLength] = isU8a(value) || isHex(value) ? decodeU8a(registry, new Array(typeMap[0].length), u8aToU8a(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
87
86
  super(decoded);
88
87
  this.#registry = registry;
89
88
  this.initialU8aLength = decodedLength;
@@ -114,7 +113,7 @@ export class Struct extends Map {
114
113
 
115
114
 
116
115
  get defKeys() {
117
- return Object.keys(this.#Types);
116
+ return this.#Types[1];
118
117
  }
119
118
 
120
119
  getT(key) {
@@ -141,11 +140,10 @@ export class Struct extends Map {
141
140
 
142
141
  get Type() {
143
142
  const result = {};
144
- const defs = Object.entries(this.#Types);
143
+ const [Types, keys] = this.#Types;
145
144
 
146
- for (let i = 0; i < defs.length; i++) {
147
- const [key, Type] = defs[i];
148
- result[key] = new Type(this.registry).toRawType();
145
+ for (let i = 0; i < keys.length; i++) {
146
+ result[keys[i]] = new Types[i](this.registry).toRawType();
149
147
  }
150
148
 
151
149
  return result;
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "8.5.2-6",
23
+ "version": "8.5.2-7",
24
24
  "main": "./cjs/index.js",
25
25
  "module": "./index.js",
26
26
  "types": "./index.d.ts",
package/packageInfo.js CHANGED
@@ -5,5 +5,5 @@ export const packageInfo = {
5
5
  name: '@polkadot/types-codec',
6
6
  path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
7
7
  type: 'esm',
8
- version: '8.5.2-6'
8
+ version: '8.5.2-7'
9
9
  };
package/types/codec.d.ts CHANGED
@@ -81,6 +81,5 @@ export interface CodecClass<T = Codec> {
81
81
  readonly __fallbackType?: string;
82
82
  new (registry: Registry, ...args: any[]): T;
83
83
  }
84
- export declare type CodecClassDef<T = Codec> = Record<string, CodecClass<T>>;
85
84
  export declare type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a';
86
85
  export declare type ArgsDef = Record<string, CodecClass | string>;
@@ -7,7 +7,5 @@ import type { Codec, CodecClass, Registry } from '../types';
7
7
  * @param result - The result array (will be returned with values pushed)
8
8
  * @param types - The array of CodecClass to decode the U8a against.
9
9
  */
10
- export declare function decodeU8a<T extends Codec = Codec, E = T>(registry: Registry, u8a: Uint8Array, types: CodecClass[] | {
11
- [index: string]: CodecClass;
12
- }, withZip?: boolean): [E[], number];
13
- export declare function decodeU8aVec<T extends Codec = Codec>(registry: Registry, u8a: Uint8Array, start: number, Type: CodecClass<T>, count: number): [T[], number, number];
10
+ export declare function decodeU8a<T extends Codec = Codec, E = T>(registry: Registry, result: unknown[], u8a: Uint8Array, [Types, keys]: [CodecClass[], string[]], withZip?: boolean): [E[], number];
11
+ export declare function decodeU8aVec<T extends Codec = Codec>(registry: Registry, result: unknown[], u8a: Uint8Array, startAt: number, Type: CodecClass<T>): [number, number];
@@ -23,12 +23,11 @@ function getRawType(registry, Type) {
23
23
  */
24
24
 
25
25
 
26
- export function decodeU8a(registry, u8a, types, withZip) {
27
- const [Types, keys] = Array.isArray(types) ? [types, []] : [Object.values(types), Object.keys(types)];
28
- const result = new Array(Types.length);
26
+ export function decodeU8a(registry, result, u8a, [Types, keys], withZip) {
27
+ const count = result.length;
29
28
  let offset = 0;
30
29
 
31
- for (let i = 0; i < Types.length; i++) {
30
+ for (let i = 0; i < count; i++) {
32
31
  try {
33
32
  const value = new Types[i](registry, u8a.subarray(offset));
34
33
  offset += value.initialU8aLength || value.encodedLength;
@@ -42,9 +41,9 @@ export function decodeU8a(registry, u8a, types, withZip) {
42
41
  } // Split from decodeU8a since this is specialized to 1 instance ... yes duplication, but
43
42
  // since we have to do less checks (and these are intensive anyway), much faster
44
43
 
45
- export function decodeU8aVec(registry, u8a, start, Type, count) {
46
- const result = new Array(count);
47
- let offset = start;
44
+ export function decodeU8aVec(registry, result, u8a, startAt, Type) {
45
+ const count = result.length;
46
+ let offset = startAt;
48
47
 
49
48
  for (let i = 0; i < count; i++) {
50
49
  try {
@@ -56,5 +55,5 @@ export function decodeU8aVec(registry, u8a, start, Type, count) {
56
55
  }
57
56
  }
58
57
 
59
- return [result, offset, offset - start];
58
+ return [offset, offset - startAt];
60
59
  }
@@ -2,4 +2,4 @@ import type { CodecClass, Registry } from '../types';
2
2
  /**
3
3
  * @description takes an input map of the form `{ [string]: string | CodecClass }` and returns a map of `{ [string]: CodecClass }`
4
4
  */
5
- export declare function mapToTypeMap(registry: Registry, input: Record<string, string | CodecClass>): Record<string, CodecClass>;
5
+ export declare function mapToTypeMap(registry: Registry, input: Record<string, string | CodecClass>): [CodecClass[], string[]];
@@ -7,10 +7,12 @@ import { typeToConstructor } from "./typeToConstructor.js";
7
7
 
8
8
  export function mapToTypeMap(registry, input) {
9
9
  const entries = Object.entries(input);
10
- const output = {};
10
+ const count = entries.length;
11
+ const output = [new Array(count), new Array(count)];
11
12
 
12
13
  for (let i = 0; i < entries.length; i++) {
13
- output[entries[i][0]] = typeToConstructor(registry, entries[i][1]);
14
+ output[1][i] = entries[i][0];
15
+ output[0][i] = typeToConstructor(registry, entries[i][1]);
14
16
  }
15
17
 
16
18
  return output;
@@ -1,2 +1,2 @@
1
1
  import type { CodecClass, Registry } from '../types';
2
- export declare function typesToMap(registry: Registry, Types: Record<string, CodecClass>): Record<string, string>;
2
+ export declare function typesToMap(registry: Registry, [Types, keys]: [CodecClass[], string[]]): Record<string, string>;
@@ -1,12 +1,10 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- export function typesToMap(registry, Types) {
4
- const entries = Object.entries(Types);
3
+ export function typesToMap(registry, [Types, keys]) {
5
4
  const result = {};
6
5
 
7
- for (let i = 0; i < entries.length; i++) {
8
- const [key, Type] = entries[i];
9
- result[key] = registry.getClassName(Type) || new Type(registry).toRawType();
6
+ for (let i = 0; i < keys.length; i++) {
7
+ result[keys[i]] = registry.getClassName(Types[i]) || new Types[i](registry).toRawType();
10
8
  }
11
9
 
12
10
  return result;