@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/abstract/AbstractArray.d.ts +1 -2
- package/abstract/AbstractArray.js +2 -8
- package/base/Compact.d.ts +7 -2
- package/base/Compact.js +18 -3
- package/base/Enum.d.ts +16 -1
- package/base/Enum.js +27 -13
- package/base/Option.d.ts +6 -1
- package/base/Option.js +20 -3
- package/base/Tuple.d.ts +7 -1
- package/base/Tuple.js +34 -20
- package/base/Vec.d.ts +8 -2
- package/base/Vec.js +39 -22
- package/base/VecFixed.d.ts +7 -1
- package/base/VecFixed.js +19 -17
- package/cjs/abstract/AbstractArray.js +2 -8
- package/cjs/base/Compact.js +18 -3
- package/cjs/base/Enum.js +26 -11
- package/cjs/base/Option.js +20 -2
- package/cjs/base/Tuple.js +34 -20
- package/cjs/base/Vec.js +36 -21
- package/cjs/base/VecFixed.js +18 -16
- package/cjs/extended/BTreeSet.js +5 -3
- package/cjs/extended/Map.js +1 -1
- package/cjs/native/Struct.js +30 -16
- package/cjs/packageInfo.js +1 -1
- package/cjs/utils/decodeU8a.js +8 -8
- package/cjs/utils/mapToTypeMap.js +4 -2
- package/cjs/utils/typesToMap.js +4 -5
- package/extended/BTreeSet.js +5 -3
- package/extended/Map.js +1 -1
- package/native/Struct.d.ts +6 -1
- package/native/Struct.js +29 -16
- package/package.json +1 -1
- package/packageInfo.js +1 -1
- package/types/codec.d.ts +0 -1
- package/utils/decodeU8a.d.ts +2 -4
- package/utils/decodeU8a.js +7 -8
- package/utils/mapToTypeMap.d.ts +1 -1
- package/utils/mapToTypeMap.js +4 -2
- package/utils/typesToMap.d.ts +1 -1
- package/utils/typesToMap.js +3 -5
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
|
-
|
|
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 ===
|
|
14
|
-
const raw = new Array(
|
|
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 <
|
|
17
|
-
const key =
|
|
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[
|
|
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
|
-
|
|
78
|
-
|
|
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
|
|
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
|
|
143
|
+
const [Types, keys] = this.#Types;
|
|
130
144
|
|
|
131
|
-
for (let i = 0; i <
|
|
132
|
-
|
|
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
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-
|
|
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>;
|
package/utils/decodeU8a.d.ts
CHANGED
|
@@ -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,
|
|
11
|
-
|
|
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];
|
package/utils/decodeU8a.js
CHANGED
|
@@ -23,12 +23,11 @@ function getRawType(registry, Type) {
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
|
|
26
|
-
export function decodeU8a(registry, u8a,
|
|
27
|
-
const
|
|
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 <
|
|
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,
|
|
46
|
-
const
|
|
47
|
-
let offset =
|
|
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 [
|
|
58
|
+
return [offset, offset - startAt];
|
|
60
59
|
}
|
package/utils/mapToTypeMap.d.ts
CHANGED
|
@@ -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>):
|
|
5
|
+
export declare function mapToTypeMap(registry: Registry, input: Record<string, string | CodecClass>): [CodecClass[], string[]];
|
package/utils/mapToTypeMap.js
CHANGED
|
@@ -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
|
|
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[
|
|
14
|
+
output[1][i] = entries[i][0];
|
|
15
|
+
output[0][i] = typeToConstructor(registry, entries[i][1]);
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
return output;
|
package/utils/typesToMap.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { CodecClass, Registry } from '../types';
|
|
2
|
-
export declare function typesToMap(registry: Registry, Types:
|
|
2
|
+
export declare function typesToMap(registry: Registry, [Types, keys]: [CodecClass[], string[]]): Record<string, string>;
|
package/utils/typesToMap.js
CHANGED
|
@@ -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 <
|
|
8
|
-
|
|
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;
|