@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.
package/native/Struct.js CHANGED
@@ -3,20 +3,24 @@
3
3
  import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
4
4
  import { compareMap, decodeU8a, mapToTypeMap, typesToMap } from "../utils/index.js";
5
5
 
6
+ function noopSetDefinition(d) {
7
+ return d;
8
+ }
6
9
  /** @internal */
7
- function decodeStructFromObject(registry, Types, value, jsonMap) {
10
+
11
+
12
+ function decodeStructFromObject(registry, [Types, keys], value, jsonMap) {
8
13
  let jsonObj;
9
- const inputKeys = Object.keys(Types);
10
14
  const typeofArray = Array.isArray(value);
11
15
  const typeofMap = value instanceof Map;
12
16
  assert(typeofArray || typeofMap || isObject(value), () => `Struct: Cannot decode value ${stringify(value)} (typeof ${typeof value}), expected an input object, map or array`);
13
- assert(!typeofArray || value.length === inputKeys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${inputKeys.join(', ')}`);
14
- 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);
15
19
 
16
- for (let i = 0; i < inputKeys.length; i++) {
17
- const key = inputKeys[i];
20
+ for (let i = 0; i < keys.length; i++) {
21
+ const key = keys[i];
18
22
  const jsonKey = jsonMap.get(key) || key;
19
- const Type = Types[key];
23
+ const Type = Types[i];
20
24
  let assign;
21
25
 
22
26
  try {
@@ -73,9 +77,12 @@ export class Struct extends Map {
73
77
  #jsonMap;
74
78
  #Types;
75
79
 
76
- constructor(registry, Types, value, jsonMap = new Map()) {
77
- const typeMap = mapToTypeMap(registry, Types);
78
- 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);
80
+ constructor(registry, Types, value, jsonMap = new Map(), {
81
+ definition,
82
+ setDefinition = noopSetDefinition
83
+ } = {}) {
84
+ const typeMap = definition || setDefinition(mapToTypeMap(registry, Types));
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);
79
86
  super(decoded);
80
87
  this.#registry = registry;
81
88
  this.initialU8aLength = decodedLength;
@@ -85,9 +92,16 @@ export class Struct extends Map {
85
92
 
86
93
  static with(Types, jsonMap) {
87
94
  const keys = Object.keys(Types);
95
+ let definition; // eslint-disable-next-line no-return-assign
96
+
97
+ const setDefinition = d => definition = d;
98
+
88
99
  return class extends Struct {
89
100
  constructor(registry, value) {
90
- super(registry, Types, value, jsonMap);
101
+ super(registry, Types, value, jsonMap, {
102
+ definition,
103
+ setDefinition
104
+ });
91
105
  objectProperties(this, keys, k => this.get(k));
92
106
  }
93
107
 
@@ -99,7 +113,7 @@ export class Struct extends Map {
99
113
 
100
114
 
101
115
  get defKeys() {
102
- return Object.keys(this.#Types);
116
+ return this.#Types[1];
103
117
  }
104
118
 
105
119
  getT(key) {
@@ -126,11 +140,10 @@ export class Struct extends Map {
126
140
 
127
141
  get Type() {
128
142
  const result = {};
129
- const defs = Object.entries(this.#Types);
143
+ const [Types, keys] = this.#Types;
130
144
 
131
- for (let i = 0; i < defs.length; i++) {
132
- const [key, Type] = defs[i];
133
- 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();
134
147
  }
135
148
 
136
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-4",
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-4'
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;