@pezkuwi/types-create 16.5.8 → 16.5.10
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/cjs/packageInfo.js +1 -1
- package/package.json +6 -5
- package/packageInfo.js +1 -1
- package/build/LICENSE +0 -201
- package/build/README.md +0 -3
- package/build/bundle.d.ts +0 -3
- package/build/bundle.js +0 -3
- package/build/cjs/bundle.d.ts +0 -3
- package/build/cjs/bundle.js +0 -9
- package/build/cjs/create/class.d.ts +0 -5
- package/build/cjs/create/class.js +0 -165
- package/build/cjs/create/index.d.ts +0 -2
- package/build/cjs/create/index.js +0 -5
- package/build/cjs/create/type.d.ts +0 -3
- package/build/cjs/create/type.js +0 -66
- package/build/cjs/exports.d.ts +0 -2
- package/build/cjs/exports.js +0 -5
- package/build/cjs/index.d.ts +0 -2
- package/build/cjs/index.js +0 -5
- package/build/cjs/package.json +0 -3
- package/build/cjs/packageDetect.d.ts +0 -1
- package/build/cjs/packageDetect.js +0 -6
- package/build/cjs/packageInfo.d.ts +0 -6
- package/build/cjs/packageInfo.js +0 -4
- package/build/cjs/types/augmentRegistry.d.ts +0 -11
- package/build/cjs/types/augmentRegistry.js +0 -3
- package/build/cjs/types/index.d.ts +0 -4
- package/build/cjs/types/index.js +0 -6
- package/build/cjs/types/lookup.d.ts +0 -14
- package/build/cjs/types/lookup.js +0 -2
- package/build/cjs/types/types.d.ts +0 -43
- package/build/cjs/types/types.js +0 -29
- package/build/cjs/util/encodeTypes.d.ts +0 -11
- package/build/cjs/util/encodeTypes.js +0 -128
- package/build/cjs/util/getTypeDef.d.ts +0 -8
- package/build/cjs/util/getTypeDef.js +0 -195
- package/build/cjs/util/index.d.ts +0 -4
- package/build/cjs/util/index.js +0 -7
- package/build/cjs/util/typeSplit.d.ts +0 -1
- package/build/cjs/util/typeSplit.js +0 -60
- package/build/cjs/util/xcm.d.ts +0 -2
- package/build/cjs/util/xcm.js +0 -9
- package/build/create/class.d.ts +0 -5
- package/build/create/class.js +0 -160
- package/build/create/index.d.ts +0 -2
- package/build/create/index.js +0 -2
- package/build/create/type.d.ts +0 -3
- package/build/create/type.js +0 -63
- package/build/exports.d.ts +0 -2
- package/build/exports.js +0 -2
- package/build/index.d.ts +0 -2
- package/build/index.js +0 -2
- package/build/package.json +0 -290
- package/build/packageDetect.d.ts +0 -1
- package/build/packageDetect.js +0 -4
- package/build/packageInfo.d.ts +0 -6
- package/build/packageInfo.js +0 -1
- package/build/types/augmentRegistry.d.ts +0 -11
- package/build/types/augmentRegistry.js +0 -1
- package/build/types/index.d.ts +0 -4
- package/build/types/index.js +0 -3
- package/build/types/lookup.d.ts +0 -14
- package/build/types/lookup.js +0 -1
- package/build/types/types.d.ts +0 -43
- package/build/types/types.js +0 -26
- package/build/util/encodeTypes.d.ts +0 -11
- package/build/util/encodeTypes.js +0 -123
- package/build/util/getTypeDef.d.ts +0 -8
- package/build/util/getTypeDef.js +0 -192
- package/build/util/index.d.ts +0 -4
- package/build/util/index.js +0 -4
- package/build/util/typeSplit.d.ts +0 -1
- package/build/util/typeSplit.js +0 -57
- package/build/util/xcm.d.ts +0 -2
- package/build/util/xcm.js +0 -5
- package/build-deno/README.md +0 -3
- package/build-deno/bundle.ts +0 -5
- package/build-deno/create/class.ts +0 -252
- package/build-deno/create/index.ts +0 -3
- package/build-deno/create/type.ts +0 -85
- package/build-deno/exports.ts +0 -3
- package/build-deno/index.ts +0 -4
- package/build-deno/mod.ts +0 -2
- package/build-deno/packageDetect.ts +0 -8
- package/build-deno/packageInfo.ts +0 -3
- package/build-deno/types/augmentRegistry.ts +0 -15
- package/build-deno/types/index.ts +0 -7
- package/build-deno/types/lookup.ts +0 -16
- package/build-deno/types/types.ts +0 -45
- package/build-deno/util/encodeTypes.ts +0 -201
- package/build-deno/util/getTypeDef.ts +0 -267
- package/build-deno/util/index.ts +0 -5
- package/build-deno/util/typeSplit.ts +0 -53
- package/build-deno/util/xcm.ts +0 -10
- package/build-tsc/bundle.d.ts +0 -3
- package/build-tsc/create/class.d.ts +0 -5
- package/build-tsc/create/index.d.ts +0 -2
- package/build-tsc/create/type.d.ts +0 -3
- package/build-tsc/exports.d.ts +0 -2
- package/build-tsc/index.d.ts +0 -2
- package/build-tsc/packageDetect.d.ts +0 -1
- package/build-tsc/packageInfo.d.ts +0 -6
- package/build-tsc/types/augmentRegistry.d.ts +0 -11
- package/build-tsc/types/index.d.ts +0 -4
- package/build-tsc/types/lookup.d.ts +0 -14
- package/build-tsc/types/types.d.ts +0 -43
- package/build-tsc/util/encodeTypes.d.ts +0 -11
- package/build-tsc/util/getTypeDef.d.ts +0 -8
- package/build-tsc/util/index.d.ts +0 -4
- package/build-tsc/util/typeSplit.d.ts +0 -1
- package/build-tsc/util/xcm.d.ts +0 -2
- package/build-tsc-cjs/bundle.js +0 -9
- package/build-tsc-cjs/create/class.js +0 -165
- package/build-tsc-cjs/create/index.js +0 -5
- package/build-tsc-cjs/create/type.js +0 -66
- package/build-tsc-cjs/exports.js +0 -5
- package/build-tsc-cjs/index.js +0 -5
- package/build-tsc-cjs/packageDetect.js +0 -6
- package/build-tsc-cjs/packageInfo.js +0 -4
- package/build-tsc-cjs/types/augmentRegistry.js +0 -3
- package/build-tsc-cjs/types/index.js +0 -6
- package/build-tsc-cjs/types/lookup.js +0 -2
- package/build-tsc-cjs/types/types.js +0 -29
- package/build-tsc-cjs/util/encodeTypes.js +0 -128
- package/build-tsc-cjs/util/getTypeDef.js +0 -195
- package/build-tsc-cjs/util/index.js +0 -7
- package/build-tsc-cjs/util/typeSplit.js +0 -60
- package/build-tsc-cjs/util/xcm.js +0 -9
- package/build-tsc-esm/bundle.js +0 -3
- package/build-tsc-esm/create/class.js +0 -160
- package/build-tsc-esm/create/index.js +0 -2
- package/build-tsc-esm/create/type.js +0 -63
- package/build-tsc-esm/exports.js +0 -2
- package/build-tsc-esm/index.js +0 -2
- package/build-tsc-esm/packageDetect.js +0 -4
- package/build-tsc-esm/packageInfo.js +0 -1
- package/build-tsc-esm/types/augmentRegistry.js +0 -1
- package/build-tsc-esm/types/index.js +0 -3
- package/build-tsc-esm/types/lookup.js +0 -1
- package/build-tsc-esm/types/types.js +0 -26
- package/build-tsc-esm/util/encodeTypes.js +0 -123
- package/build-tsc-esm/util/getTypeDef.js +0 -192
- package/build-tsc-esm/util/index.js +0 -4
- package/build-tsc-esm/util/typeSplit.js +0 -57
- package/build-tsc-esm/util/xcm.js +0 -5
- package/src/bundle.ts +0 -9
- package/src/create/class.ts +0 -257
- package/src/create/index.ts +0 -5
- package/src/create/type.ts +0 -94
- package/src/exports.ts +0 -6
- package/src/index.ts +0 -6
- package/src/mod.ts +0 -4
- package/src/packageDetect.ts +0 -12
- package/src/packageInfo.ts +0 -6
- package/src/types/augmentRegistry.ts +0 -19
- package/src/types/index.ts +0 -11
- package/src/types/lookup.ts +0 -21
- package/src/types/types.ts +0 -47
- package/src/util/encodeTypes.spec.ts +0 -217
- package/src/util/encodeTypes.ts +0 -205
- package/src/util/getTypeDef.spec.ts +0 -704
- package/src/util/getTypeDef.ts +0 -279
- package/src/util/index.ts +0 -7
- package/src/util/typeSplit.spec.ts +0 -50
- package/src/util/typeSplit.ts +0 -56
- package/src/util/xcm.ts +0 -12
- package/tsconfig.build.json +0 -15
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.spec.json +0 -17
- package/tsconfig.spec.tsbuildinfo +0 -1
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import type { Codec, CodecClass, LookupString, Registry, U8aBitLength, UIntBitLength } from 'https://deno.land/x/pezkuwi/types-codec/types/index.ts';
|
|
3
|
-
import type { TypeDef } from '../types/index.ts';
|
|
4
|
-
|
|
5
|
-
import { BTreeMap, BTreeSet, Bytes, CodecSet, Compact, DoNotConstruct, Enum, HashMap, Int, Null, Option, Range, RangeInclusive, Result, Struct, Tuple, U8aFixed, UInt, Vec, VecFixed, WrapperKeepOpaque, WrapperOpaque } from 'https://deno.land/x/pezkuwi/types-codec/mod.ts';
|
|
6
|
-
import { isNumber, stringify } from 'https://deno.land/x/pezkuwi/util/mod.ts';
|
|
7
|
-
|
|
8
|
-
import { TypeDefInfo } from '../types/index.ts';
|
|
9
|
-
import { getTypeDef } from '../util/getTypeDef.ts';
|
|
10
|
-
|
|
11
|
-
function getTypeDefType ({ lookupName, type }: TypeDef): string {
|
|
12
|
-
return lookupName || type;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function getSubDefArray (value: TypeDef): TypeDef[] {
|
|
16
|
-
if (!Array.isArray(value.sub)) {
|
|
17
|
-
throw new Error(`Expected subtype as TypeDef[] in ${stringify(value)}`);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return value.sub;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function getSubDef (value: TypeDef): TypeDef {
|
|
24
|
-
if (!value.sub || Array.isArray(value.sub)) {
|
|
25
|
-
throw new Error(`Expected subtype as TypeDef in ${stringify(value)}`);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return value.sub;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function getSubType (value: TypeDef): string {
|
|
32
|
-
return getTypeDefType(getSubDef(value));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function getTypeClassMap (value: TypeDef): Record<string, string> {
|
|
36
|
-
const subs = getSubDefArray(value);
|
|
37
|
-
const map: Record<string, string> = {};
|
|
38
|
-
|
|
39
|
-
for (let i = 0, count = subs.length; i < count; i++) {
|
|
40
|
-
const sub = subs[i];
|
|
41
|
-
|
|
42
|
-
if (!sub.name) {
|
|
43
|
-
throw new Error(`No name found in definition ${stringify(sub)}`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
map[sub.name] = getTypeDefType(sub);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return map;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function getTypeClassArray (value: TypeDef): string[] {
|
|
53
|
-
return getSubDefArray(value).map(getTypeDefType);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function createInt (Clazz: typeof Int | typeof UInt, { displayName, length }: TypeDef): CodecClass<Codec> {
|
|
57
|
-
if (!isNumber(length)) {
|
|
58
|
-
throw new Error(`Expected bitLength information for ${displayName || Clazz.constructor.name}<bitLength>`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return Clazz.with(length as UIntBitLength, displayName);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function createHashMap (Clazz: typeof BTreeMap | typeof HashMap, value: TypeDef): CodecClass<Codec> {
|
|
65
|
-
const [keyType, valueType] = getTypeClassArray(value);
|
|
66
|
-
|
|
67
|
-
return Clazz.with(keyType, valueType);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function createWithSub (Clazz: { with: (t: string) => CodecClass<Codec> }, value: TypeDef): CodecClass<Codec> {
|
|
71
|
-
return Clazz.with(getSubType(value));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const infoMapping: Record<TypeDefInfo, (registry: Registry, value: TypeDef) => CodecClass<Codec>> = {
|
|
75
|
-
[TypeDefInfo.BTreeMap]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
76
|
-
createHashMap(BTreeMap, value),
|
|
77
|
-
|
|
78
|
-
[TypeDefInfo.BTreeSet]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
79
|
-
createWithSub(BTreeSet, value),
|
|
80
|
-
|
|
81
|
-
[TypeDefInfo.Compact]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
82
|
-
createWithSub(Compact, value),
|
|
83
|
-
|
|
84
|
-
[TypeDefInfo.DoNotConstruct]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
85
|
-
DoNotConstruct.with(value.displayName || value.type),
|
|
86
|
-
|
|
87
|
-
[TypeDefInfo.Enum]: (_registry: Registry, value: TypeDef): CodecClass<Codec> => {
|
|
88
|
-
const subs = getSubDefArray(value);
|
|
89
|
-
|
|
90
|
-
return Enum.with(
|
|
91
|
-
subs.every(({ type }) => type === 'Null')
|
|
92
|
-
? subs.reduce<Record<string, number>>((out, { index, name }, count) => {
|
|
93
|
-
if (!name) {
|
|
94
|
-
throw new Error('No name found in sub definition');
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
out[name] = index || count;
|
|
98
|
-
|
|
99
|
-
return out;
|
|
100
|
-
}, {})
|
|
101
|
-
: getTypeClassMap(value)
|
|
102
|
-
);
|
|
103
|
-
},
|
|
104
|
-
|
|
105
|
-
[TypeDefInfo.HashMap]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
106
|
-
createHashMap(HashMap, value),
|
|
107
|
-
|
|
108
|
-
[TypeDefInfo.Int]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
109
|
-
createInt(Int, value),
|
|
110
|
-
|
|
111
|
-
// We have circular deps between Linkage & Struct
|
|
112
|
-
[TypeDefInfo.Linkage]: (_registry: Registry, value: TypeDef): CodecClass<Codec> => {
|
|
113
|
-
const type = `Option<${getSubType(value)}>`;
|
|
114
|
-
// eslint-disable-next-line sort-keys
|
|
115
|
-
const Clazz = Struct.with({ previous: type, next: type } as any);
|
|
116
|
-
|
|
117
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
118
|
-
Clazz.prototype.toRawType = function (): string {
|
|
119
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call
|
|
120
|
-
return `Linkage<${this.next.toRawType(true)}>`;
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
return Clazz;
|
|
124
|
-
},
|
|
125
|
-
|
|
126
|
-
[TypeDefInfo.Null]: (_registry: Registry, _value: TypeDef): CodecClass<Codec> =>
|
|
127
|
-
Null,
|
|
128
|
-
|
|
129
|
-
[TypeDefInfo.Option]: (_registry: Registry, value: TypeDef): CodecClass<Codec> => {
|
|
130
|
-
if (!value.sub || Array.isArray(value.sub)) {
|
|
131
|
-
throw new Error('Expected type information for Option');
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// NOTE This is opt-in (unhandled), not by default
|
|
135
|
-
// if (value.sub.type === 'bool') {
|
|
136
|
-
// return OptionBool;
|
|
137
|
-
// }
|
|
138
|
-
|
|
139
|
-
return createWithSub(Option, value);
|
|
140
|
-
},
|
|
141
|
-
|
|
142
|
-
[TypeDefInfo.Plain]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
143
|
-
registry.getOrUnknown(value.type),
|
|
144
|
-
|
|
145
|
-
[TypeDefInfo.Range]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
146
|
-
createWithSub(Range, value),
|
|
147
|
-
|
|
148
|
-
[TypeDefInfo.RangeInclusive]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
149
|
-
createWithSub(RangeInclusive, value),
|
|
150
|
-
|
|
151
|
-
[TypeDefInfo.Result]: (_registry: Registry, value: TypeDef): CodecClass<Codec> => {
|
|
152
|
-
const [Ok, Err] = getTypeClassArray(value);
|
|
153
|
-
|
|
154
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
155
|
-
return Result.with({ Err, Ok });
|
|
156
|
-
},
|
|
157
|
-
|
|
158
|
-
[TypeDefInfo.Set]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
159
|
-
CodecSet.with(
|
|
160
|
-
getSubDefArray(value).reduce<Record<string, number>>((result, { index, name }) => {
|
|
161
|
-
if (!name || !isNumber(index)) {
|
|
162
|
-
throw new Error('No name found in sub definition');
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
result[name] = index;
|
|
166
|
-
|
|
167
|
-
return result;
|
|
168
|
-
}, {}),
|
|
169
|
-
value.length
|
|
170
|
-
),
|
|
171
|
-
|
|
172
|
-
[TypeDefInfo.Si]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
173
|
-
getTypeClass(registry, registry.lookup.getTypeDef(value.type as LookupString)),
|
|
174
|
-
|
|
175
|
-
[TypeDefInfo.Struct]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
176
|
-
Struct.with(getTypeClassMap(value), value.alias),
|
|
177
|
-
|
|
178
|
-
[TypeDefInfo.Tuple]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
179
|
-
Tuple.with(getTypeClassArray(value)),
|
|
180
|
-
|
|
181
|
-
[TypeDefInfo.UInt]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
182
|
-
createInt(UInt, value),
|
|
183
|
-
|
|
184
|
-
[TypeDefInfo.Vec]: (_registry: Registry, { sub }: TypeDef): CodecClass<Codec> => {
|
|
185
|
-
if (!sub || Array.isArray(sub)) {
|
|
186
|
-
throw new Error('Expected type information for vector');
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
return (
|
|
190
|
-
sub.type === 'u8'
|
|
191
|
-
? Bytes
|
|
192
|
-
: Vec.with(getTypeDefType(sub))
|
|
193
|
-
);
|
|
194
|
-
},
|
|
195
|
-
|
|
196
|
-
[TypeDefInfo.VecFixed]: (_registry: Registry, { displayName, length, sub }: TypeDef): CodecClass<Codec> => {
|
|
197
|
-
if (!isNumber(length) || !sub || Array.isArray(sub)) {
|
|
198
|
-
throw new Error('Expected length & type information for fixed vector');
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
return (
|
|
202
|
-
sub.type === 'u8'
|
|
203
|
-
? U8aFixed.with((length * 8) as U8aBitLength, displayName)
|
|
204
|
-
: VecFixed.with(getTypeDefType(sub), length)
|
|
205
|
-
);
|
|
206
|
-
},
|
|
207
|
-
|
|
208
|
-
[TypeDefInfo.WrapperKeepOpaque]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
209
|
-
createWithSub(WrapperKeepOpaque, value),
|
|
210
|
-
|
|
211
|
-
[TypeDefInfo.WrapperOpaque]: (_registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
|
212
|
-
createWithSub(WrapperOpaque, value)
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
export function constructTypeClass<T extends Codec = Codec> (registry: Registry, typeDef: TypeDef): CodecClass<T> {
|
|
216
|
-
try {
|
|
217
|
-
const Type = infoMapping[typeDef.info](registry, typeDef);
|
|
218
|
-
|
|
219
|
-
if (!Type) {
|
|
220
|
-
throw new Error('No class created');
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// don't clobber any existing
|
|
224
|
-
if (!Type.__fallbackType && typeDef.fallbackType) {
|
|
225
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
226
|
-
// @ts-ignore ...this is the only place we we actually assign this...
|
|
227
|
-
Type.__fallbackType = typeDef.fallbackType;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return Type as CodecClass<T>;
|
|
231
|
-
} catch (error) {
|
|
232
|
-
throw new Error(`Unable to construct class from ${stringify(typeDef)}: ${(error as Error).message}`);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
export function getTypeClass<T extends Codec = Codec> (registry: Registry, typeDef: TypeDef): CodecClass<T> {
|
|
237
|
-
return registry.getUnsafe(typeDef.type, false, typeDef) as unknown as CodecClass<T>;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export function createClassUnsafe<T extends Codec = Codec, K extends string = string> (registry: Registry, type: K): CodecClass<T> {
|
|
241
|
-
return (
|
|
242
|
-
// just retrieve via name, no creation via typeDef
|
|
243
|
-
registry.getUnsafe(type) ||
|
|
244
|
-
// we don't have an existing type, create the class via typeDef
|
|
245
|
-
getTypeClass(
|
|
246
|
-
registry,
|
|
247
|
-
registry.isLookupType(type)
|
|
248
|
-
? registry.lookup.getTypeDef(type)
|
|
249
|
-
: getTypeDef(type)
|
|
250
|
-
)
|
|
251
|
-
);
|
|
252
|
-
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import type { Bytes } from 'https://deno.land/x/pezkuwi/types-codec/mod.ts';
|
|
3
|
-
import type { Codec, CodecClass, IU8a, Registry } from 'https://deno.land/x/pezkuwi/types-codec/types/index.ts';
|
|
4
|
-
import type { CreateOptions } from '../types/index.ts';
|
|
5
|
-
|
|
6
|
-
import { Option } from 'https://deno.land/x/pezkuwi/types-codec/mod.ts';
|
|
7
|
-
import { isHex, isU8a, u8aEq, u8aToHex, u8aToU8a } from 'https://deno.land/x/pezkuwi/util/mod.ts';
|
|
8
|
-
|
|
9
|
-
import { createClassUnsafe } from './class.ts';
|
|
10
|
-
|
|
11
|
-
function checkInstance (created: Codec, matcher: Uint8Array): void {
|
|
12
|
-
const u8a = created.toU8a();
|
|
13
|
-
const rawType = created.toRawType();
|
|
14
|
-
const isOk = (
|
|
15
|
-
// full match, all ok
|
|
16
|
-
u8aEq(u8a, matcher) ||
|
|
17
|
-
(
|
|
18
|
-
// on a length-prefixed type, just check the actual length
|
|
19
|
-
['Bytes', 'Text', 'Type'].includes(rawType) &&
|
|
20
|
-
matcher.length === (created as unknown as Bytes).length
|
|
21
|
-
) ||
|
|
22
|
-
(
|
|
23
|
-
// when the created is empty and matcher is also empty, let it slide...
|
|
24
|
-
created.isEmpty &&
|
|
25
|
-
matcher.every((v) => !v)
|
|
26
|
-
)
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
if (!isOk) {
|
|
30
|
-
throw new Error(`${rawType}:: Decoded input doesn't match input, received ${u8aToHex(matcher, 512)} (${matcher.length} bytes), created ${u8aToHex(u8a, 512)} (${u8a.length} bytes)`);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function checkPedantic (created: Codec, [value]: unknown[]): void {
|
|
35
|
-
if (isU8a(value)) {
|
|
36
|
-
checkInstance(created, value);
|
|
37
|
-
} else if (isHex(value)) {
|
|
38
|
-
checkInstance(created, u8aToU8a(value));
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function initType<T extends Codec> (registry: Registry, Type: CodecClass, params: unknown[] = [], { blockHash, isFallback, isOptional, isPedantic }: CreateOptions = {}): T {
|
|
43
|
-
const created = new (
|
|
44
|
-
isOptional
|
|
45
|
-
? Option.with(Type)
|
|
46
|
-
: Type
|
|
47
|
-
)(registry, ...params);
|
|
48
|
-
|
|
49
|
-
isPedantic && checkPedantic(created, params);
|
|
50
|
-
|
|
51
|
-
if (blockHash) {
|
|
52
|
-
created.createdAtHash = createTypeUnsafe<IU8a>(registry, 'BlockHash', [blockHash]);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (isFallback) {
|
|
56
|
-
created.isStorageFallback = true;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return created as T;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function createTypeUnsafe<T extends Codec = Codec, K extends string = string> (registry: Registry, type: K, params: unknown[] = [], options: CreateOptions = {}): T {
|
|
63
|
-
let Clazz: CodecClass | null = null;
|
|
64
|
-
let firstError: Error | null = null;
|
|
65
|
-
|
|
66
|
-
try {
|
|
67
|
-
Clazz = createClassUnsafe(registry, type);
|
|
68
|
-
|
|
69
|
-
return initType(registry, Clazz, params, options);
|
|
70
|
-
} catch (error) {
|
|
71
|
-
firstError = new Error(`createType(${type}):: ${(error as Error).message}`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (Clazz?.__fallbackType) {
|
|
75
|
-
try {
|
|
76
|
-
Clazz = createClassUnsafe(registry, Clazz.__fallbackType as unknown as K);
|
|
77
|
-
|
|
78
|
-
return initType(registry, Clazz, params, options);
|
|
79
|
-
} catch {
|
|
80
|
-
// swallow, we will throw the first error again
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
throw firstError;
|
|
85
|
-
}
|
package/build-deno/exports.ts
DELETED
package/build-deno/index.ts
DELETED
package/build-deno/mod.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { packageInfo as codecInfo } from 'https://deno.land/x/pezkuwi/types-codec/packageInfo.ts';
|
|
4
|
-
import { detectPackage } from 'https://deno.land/x/pezkuwi/util/mod.ts';
|
|
5
|
-
|
|
6
|
-
import { packageInfo } from './packageInfo.ts';
|
|
7
|
-
|
|
8
|
-
detectPackage(packageInfo, null, [codecInfo]);
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import 'https://deno.land/x/pezkuwi/types-codec/types/registry.ts';
|
|
3
|
-
|
|
4
|
-
import type { Codec, CodecClass, ICompact, INumber, LookupString } from 'https://deno.land/x/pezkuwi/types-codec/types/index.ts';
|
|
5
|
-
import type { ILookup, TypeDef } from 'https://deno.land/x/pezkuwi/types-create/types/index.ts';
|
|
6
|
-
|
|
7
|
-
declare module 'https://deno.land/x/pezkuwi/types-codec/types/registry.ts' {
|
|
8
|
-
interface Registry {
|
|
9
|
-
readonly lookup: ILookup;
|
|
10
|
-
|
|
11
|
-
createLookupType (lookupId: ICompact<INumber> | number): LookupString;
|
|
12
|
-
getUnsafe <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<T> | undefined;
|
|
13
|
-
setLookup (lookup: ILookup): void;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import type { Option, Text } from 'https://deno.land/x/pezkuwi/types-codec/mod.ts';
|
|
3
|
-
import type { ICompact, INumber, LookupString } from 'https://deno.land/x/pezkuwi/types-codec/types/index.ts';
|
|
4
|
-
import type { TypeDef } from './types.ts';
|
|
5
|
-
|
|
6
|
-
interface SiTypeBase {
|
|
7
|
-
def: {
|
|
8
|
-
asTuple: ICompact<INumber>[]
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface ILookup {
|
|
13
|
-
getSiType (lookupId: ICompact<INumber> | LookupString | number): SiTypeBase;
|
|
14
|
-
getTypeDef (lookupId: ICompact<INumber> | LookupString | number): TypeDef;
|
|
15
|
-
sanitizeField (name: Option<Text>): [string | null, string | null];
|
|
16
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
export enum TypeDefInfo {
|
|
3
|
-
BTreeMap,
|
|
4
|
-
BTreeSet,
|
|
5
|
-
Compact,
|
|
6
|
-
DoNotConstruct,
|
|
7
|
-
Enum,
|
|
8
|
-
HashMap,
|
|
9
|
-
Int,
|
|
10
|
-
Linkage,
|
|
11
|
-
Null,
|
|
12
|
-
Option,
|
|
13
|
-
Plain,
|
|
14
|
-
Range,
|
|
15
|
-
RangeInclusive,
|
|
16
|
-
Result,
|
|
17
|
-
Set,
|
|
18
|
-
Si,
|
|
19
|
-
Struct,
|
|
20
|
-
Tuple,
|
|
21
|
-
UInt,
|
|
22
|
-
Vec,
|
|
23
|
-
VecFixed,
|
|
24
|
-
WrapperKeepOpaque,
|
|
25
|
-
WrapperOpaque
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface TypeDef {
|
|
29
|
-
alias?: Map<string, string> | undefined;
|
|
30
|
-
displayName?: string | undefined;
|
|
31
|
-
docs?: string[] | undefined;
|
|
32
|
-
fallbackType?: string | undefined;
|
|
33
|
-
info: TypeDefInfo;
|
|
34
|
-
index?: number;
|
|
35
|
-
isFromSi?: boolean;
|
|
36
|
-
length?: number;
|
|
37
|
-
lookupIndex?: number;
|
|
38
|
-
lookupName?: string | undefined;
|
|
39
|
-
lookupNameRoot?: string | undefined;
|
|
40
|
-
name?: string | undefined;
|
|
41
|
-
namespace?: string | undefined;
|
|
42
|
-
sub?: TypeDef | TypeDef[];
|
|
43
|
-
type: string;
|
|
44
|
-
typeName?: string | undefined;
|
|
45
|
-
}
|
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import type { Registry } from 'https://deno.land/x/pezkuwi/types-codec/types/index.ts';
|
|
3
|
-
import type { TypeDef } from 'https://deno.land/x/pezkuwi/types-create/types/index.ts';
|
|
4
|
-
|
|
5
|
-
import { isNumber, isUndefined, objectSpread, stringify } from 'https://deno.land/x/pezkuwi/util/mod.ts';
|
|
6
|
-
|
|
7
|
-
import { TypeDefInfo } from '../types/index.ts';
|
|
8
|
-
|
|
9
|
-
interface ToString { toString: () => string }
|
|
10
|
-
|
|
11
|
-
const stringIdentity = <T extends ToString> (value: T): string => value.toString();
|
|
12
|
-
|
|
13
|
-
const INFO_WRAP = ['BTreeMap', 'BTreeSet', 'Compact', 'HashMap', 'Option', 'Result', 'Vec'];
|
|
14
|
-
|
|
15
|
-
export function paramsNotation <T extends ToString> (outer: string, inner?: T | T[], transform: (_: T) => string = stringIdentity): string {
|
|
16
|
-
return `${outer}${
|
|
17
|
-
inner
|
|
18
|
-
? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>`
|
|
19
|
-
: ''
|
|
20
|
-
}`;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function encodeWithParams (registry: Registry, typeDef: TypeDef, outer: string): string {
|
|
24
|
-
const { info, sub } = typeDef;
|
|
25
|
-
|
|
26
|
-
switch (info) {
|
|
27
|
-
case TypeDefInfo.BTreeMap:
|
|
28
|
-
case TypeDefInfo.BTreeSet:
|
|
29
|
-
case TypeDefInfo.Compact:
|
|
30
|
-
case TypeDefInfo.HashMap:
|
|
31
|
-
case TypeDefInfo.Linkage:
|
|
32
|
-
case TypeDefInfo.Option:
|
|
33
|
-
case TypeDefInfo.Range:
|
|
34
|
-
case TypeDefInfo.RangeInclusive:
|
|
35
|
-
case TypeDefInfo.Result:
|
|
36
|
-
case TypeDefInfo.Vec:
|
|
37
|
-
case TypeDefInfo.WrapperKeepOpaque:
|
|
38
|
-
case TypeDefInfo.WrapperOpaque:
|
|
39
|
-
return paramsNotation(outer, sub, (p) => encodeTypeDef(registry, p));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
throw new Error(`Unable to encode ${stringify(typeDef)} with params`);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function encodeSubTypes (registry: Registry, sub: TypeDef[], asEnum?: boolean, extra?: Record<string, unknown>): string {
|
|
46
|
-
const names = sub.map(({ name }) => name);
|
|
47
|
-
|
|
48
|
-
if (!names.every((n) => !!n)) {
|
|
49
|
-
throw new Error(`Subtypes does not have consistent names, ${names.join(', ')}`);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const inner: Record<string, string> = objectSpread({}, extra);
|
|
53
|
-
|
|
54
|
-
for (let i = 0, count = sub.length; i < count; i++) {
|
|
55
|
-
const def = sub[i];
|
|
56
|
-
|
|
57
|
-
if (!def.name) {
|
|
58
|
-
throw new Error(`No name found in ${stringify(def)}`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
inner[def.name] = encodeTypeDef(registry, def);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return stringify(
|
|
65
|
-
asEnum
|
|
66
|
-
? { _enum: inner }
|
|
67
|
-
: inner
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const encoders: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef) => string> = {
|
|
72
|
-
[TypeDefInfo.BTreeMap]: (registry: Registry, typeDef: TypeDef) =>
|
|
73
|
-
encodeWithParams(registry, typeDef, 'BTreeMap'),
|
|
74
|
-
|
|
75
|
-
[TypeDefInfo.BTreeSet]: (registry: Registry, typeDef: TypeDef) =>
|
|
76
|
-
encodeWithParams(registry, typeDef, 'BTreeSet'),
|
|
77
|
-
|
|
78
|
-
[TypeDefInfo.Compact]: (registry: Registry, typeDef: TypeDef) =>
|
|
79
|
-
encodeWithParams(registry, typeDef, 'Compact'),
|
|
80
|
-
|
|
81
|
-
[TypeDefInfo.DoNotConstruct]: (registry: Registry, { displayName, lookupIndex, lookupName }: TypeDef) =>
|
|
82
|
-
`DoNotConstruct<${lookupName || displayName || (isUndefined(lookupIndex) ? 'Unknown' : registry.createLookupType(lookupIndex))}>`,
|
|
83
|
-
|
|
84
|
-
[TypeDefInfo.Enum]: (registry: Registry, { sub }: TypeDef): string => {
|
|
85
|
-
if (!Array.isArray(sub)) {
|
|
86
|
-
throw new Error('Unable to encode Enum type');
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// c-like enums have all Null entries
|
|
90
|
-
// TODO We need to take the disciminant into account and auto-add empty entries
|
|
91
|
-
return sub.every(({ type }) => type === 'Null')
|
|
92
|
-
? stringify({ _enum: sub.map(({ name }, index) => `${name || `Empty${index}`}`) })
|
|
93
|
-
: encodeSubTypes(registry, sub, true);
|
|
94
|
-
},
|
|
95
|
-
|
|
96
|
-
[TypeDefInfo.HashMap]: (registry: Registry, typeDef: TypeDef) =>
|
|
97
|
-
encodeWithParams(registry, typeDef, 'HashMap'),
|
|
98
|
-
|
|
99
|
-
[TypeDefInfo.Int]: (_registry: Registry, { length = 32 }: TypeDef) =>
|
|
100
|
-
`Int<${length}>`,
|
|
101
|
-
|
|
102
|
-
[TypeDefInfo.Linkage]: (registry: Registry, typeDef: TypeDef) =>
|
|
103
|
-
encodeWithParams(registry, typeDef, 'Linkage'),
|
|
104
|
-
|
|
105
|
-
[TypeDefInfo.Null]: (_registry: Registry, _typeDef: TypeDef) =>
|
|
106
|
-
'Null',
|
|
107
|
-
|
|
108
|
-
[TypeDefInfo.Option]: (registry: Registry, typeDef: TypeDef) =>
|
|
109
|
-
encodeWithParams(registry, typeDef, 'Option'),
|
|
110
|
-
|
|
111
|
-
[TypeDefInfo.Plain]: (_registry: Registry, { displayName, type }: TypeDef) =>
|
|
112
|
-
displayName || type,
|
|
113
|
-
|
|
114
|
-
[TypeDefInfo.Range]: (registry: Registry, typeDef: TypeDef) =>
|
|
115
|
-
encodeWithParams(registry, typeDef, 'Range'),
|
|
116
|
-
|
|
117
|
-
[TypeDefInfo.RangeInclusive]: (registry: Registry, typeDef: TypeDef) =>
|
|
118
|
-
encodeWithParams(registry, typeDef, 'RangeInclusive'),
|
|
119
|
-
|
|
120
|
-
[TypeDefInfo.Result]: (registry: Registry, typeDef: TypeDef) =>
|
|
121
|
-
encodeWithParams(registry, typeDef, 'Result'),
|
|
122
|
-
|
|
123
|
-
[TypeDefInfo.Set]: (_registry: Registry, { length = 8, sub }: TypeDef): string => {
|
|
124
|
-
if (!Array.isArray(sub)) {
|
|
125
|
-
throw new Error('Unable to encode Set type');
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return stringify({
|
|
129
|
-
_set: sub.reduce((all, { index, name }, count) =>
|
|
130
|
-
objectSpread(all, { [`${name || `Unknown${index || count}`}`]: index || count }),
|
|
131
|
-
{ _bitLength: length || 8 })
|
|
132
|
-
});
|
|
133
|
-
},
|
|
134
|
-
|
|
135
|
-
[TypeDefInfo.Si]: (_registry: Registry, { lookupName, type }: TypeDef) =>
|
|
136
|
-
lookupName || type,
|
|
137
|
-
|
|
138
|
-
[TypeDefInfo.Struct]: (registry: Registry, { alias, sub }: TypeDef): string => {
|
|
139
|
-
if (!Array.isArray(sub)) {
|
|
140
|
-
throw new Error('Unable to encode Struct type');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return encodeSubTypes(registry, sub, false, alias
|
|
144
|
-
? {
|
|
145
|
-
_alias: [...alias.entries()].reduce<Record<string, string>>((all, [k, v]) =>
|
|
146
|
-
objectSpread(all, { [k]: v }), {}
|
|
147
|
-
)
|
|
148
|
-
}
|
|
149
|
-
: {}
|
|
150
|
-
);
|
|
151
|
-
},
|
|
152
|
-
|
|
153
|
-
[TypeDefInfo.Tuple]: (registry: Registry, { sub }: TypeDef): string => {
|
|
154
|
-
if (!Array.isArray(sub)) {
|
|
155
|
-
throw new Error('Unable to encode Tuple type');
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return `(${sub.map((type) => encodeTypeDef(registry, type)).join(',')})`;
|
|
159
|
-
},
|
|
160
|
-
|
|
161
|
-
[TypeDefInfo.UInt]: (_registry: Registry, { length = 32 }: TypeDef) =>
|
|
162
|
-
`UInt<${length}>`,
|
|
163
|
-
|
|
164
|
-
[TypeDefInfo.Vec]: (registry: Registry, typeDef: TypeDef) =>
|
|
165
|
-
encodeWithParams(registry, typeDef, 'Vec'),
|
|
166
|
-
|
|
167
|
-
[TypeDefInfo.VecFixed]: (_registry: Registry, { length, sub }: TypeDef): string => {
|
|
168
|
-
if (!isNumber(length) || !sub || Array.isArray(sub)) {
|
|
169
|
-
throw new Error('Unable to encode VecFixed type');
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return `[${sub.type};${length}]`;
|
|
173
|
-
},
|
|
174
|
-
|
|
175
|
-
[TypeDefInfo.WrapperKeepOpaque]: (registry: Registry, typeDef: TypeDef) =>
|
|
176
|
-
encodeWithParams(registry, typeDef, 'WrapperKeepOpaque'),
|
|
177
|
-
|
|
178
|
-
[TypeDefInfo.WrapperOpaque]: (registry: Registry, typeDef: TypeDef) =>
|
|
179
|
-
encodeWithParams(registry, typeDef, 'WrapperOpaque')
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
function encodeType (registry: Registry, typeDef: TypeDef, withLookup = true): string {
|
|
183
|
-
return withLookup && typeDef.lookupName
|
|
184
|
-
? typeDef.lookupName
|
|
185
|
-
: encoders[typeDef.info](registry, typeDef);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export function encodeTypeDef (registry: Registry, typeDef: TypeDef): string {
|
|
189
|
-
// In the case of contracts we do have the unfortunate situation where the displayName would
|
|
190
|
-
// refer to "Option" when it is an option. For these, string it out, only using when actually
|
|
191
|
-
// not a top-level element to be used
|
|
192
|
-
return (typeDef.displayName && !INFO_WRAP.some((i) => typeDef.displayName === i))
|
|
193
|
-
? typeDef.displayName
|
|
194
|
-
: encodeType(registry, typeDef);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export function withTypeString (registry: Registry, typeDef: Omit<TypeDef, 'type'> & { type?: string }): TypeDef {
|
|
198
|
-
return objectSpread({}, typeDef, {
|
|
199
|
-
type: encodeType(registry, typeDef as TypeDef, false)
|
|
200
|
-
});
|
|
201
|
-
}
|