@pezkuwi/types-create 16.5.17 → 16.5.19

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.
Files changed (168) hide show
  1. package/build/LICENSE +201 -0
  2. package/build/README.md +3 -0
  3. package/build/bundle.js +3 -0
  4. package/build/cjs/bundle.d.ts +3 -0
  5. package/build/cjs/bundle.js +9 -0
  6. package/build/cjs/create/class.js +165 -0
  7. package/build/cjs/create/index.js +5 -0
  8. package/build/cjs/create/type.js +66 -0
  9. package/build/cjs/exports.js +5 -0
  10. package/build/cjs/index.js +5 -0
  11. package/build/cjs/packageInfo.js +4 -0
  12. package/build/cjs/types/augmentRegistry.js +3 -0
  13. package/build/cjs/types/index.js +6 -0
  14. package/build/cjs/types/lookup.js +2 -0
  15. package/build/cjs/types/types.js +29 -0
  16. package/build/cjs/util/encodeTypes.js +128 -0
  17. package/build/cjs/util/getTypeDef.js +195 -0
  18. package/build/cjs/util/index.js +7 -0
  19. package/build/cjs/util/typeSplit.js +60 -0
  20. package/build/cjs/util/xcm.js +9 -0
  21. package/build/create/class.d.ts +5 -0
  22. package/build/create/class.js +160 -0
  23. package/build/create/index.d.ts +2 -0
  24. package/build/create/index.js +2 -0
  25. package/build/create/type.d.ts +3 -0
  26. package/build/create/type.js +63 -0
  27. package/build/exports.d.ts +2 -0
  28. package/build/exports.js +2 -0
  29. package/build/index.d.ts +2 -0
  30. package/build/index.js +2 -0
  31. package/build/package.json +291 -0
  32. package/build/packageDetect.d.ts +1 -0
  33. package/build/packageDetect.js +4 -0
  34. package/build/packageInfo.d.ts +6 -0
  35. package/build/packageInfo.js +1 -0
  36. package/build/types/augmentRegistry.d.ts +11 -0
  37. package/build/types/augmentRegistry.js +1 -0
  38. package/build/types/index.d.ts +4 -0
  39. package/build/types/index.js +3 -0
  40. package/build/types/lookup.d.ts +14 -0
  41. package/build/types/lookup.js +1 -0
  42. package/build/types/types.d.ts +43 -0
  43. package/build/types/types.js +26 -0
  44. package/build/util/encodeTypes.d.ts +11 -0
  45. package/build/util/encodeTypes.js +123 -0
  46. package/build/util/getTypeDef.d.ts +8 -0
  47. package/build/util/getTypeDef.js +192 -0
  48. package/build/util/index.d.ts +4 -0
  49. package/build/util/index.js +4 -0
  50. package/build/util/typeSplit.d.ts +1 -0
  51. package/build/util/typeSplit.js +57 -0
  52. package/build/util/xcm.d.ts +2 -0
  53. package/build/util/xcm.js +5 -0
  54. package/build-deno/README.md +3 -0
  55. package/build-deno/bundle.ts +5 -0
  56. package/build-deno/create/class.ts +252 -0
  57. package/build-deno/create/index.ts +3 -0
  58. package/build-deno/create/type.ts +85 -0
  59. package/build-deno/exports.ts +3 -0
  60. package/build-deno/index.ts +4 -0
  61. package/build-deno/mod.ts +2 -0
  62. package/build-deno/packageDetect.ts +8 -0
  63. package/build-deno/packageInfo.ts +3 -0
  64. package/build-deno/types/augmentRegistry.ts +15 -0
  65. package/build-deno/types/index.ts +7 -0
  66. package/build-deno/types/lookup.ts +16 -0
  67. package/build-deno/types/types.ts +45 -0
  68. package/build-deno/util/encodeTypes.ts +201 -0
  69. package/build-deno/util/getTypeDef.ts +267 -0
  70. package/build-deno/util/index.ts +5 -0
  71. package/build-deno/util/typeSplit.ts +53 -0
  72. package/build-deno/util/xcm.ts +10 -0
  73. package/build-tsc-cjs/packageDetect.js +6 -0
  74. package/{cjs → build-tsc-cjs}/packageInfo.js +1 -1
  75. package/{packageInfo.js → build-tsc-esm/packageInfo.js} +1 -1
  76. package/package.json +11 -11
  77. package/src/bundle.ts +9 -0
  78. package/src/create/class.ts +257 -0
  79. package/src/create/index.ts +5 -0
  80. package/src/create/type.ts +94 -0
  81. package/src/exports.ts +6 -0
  82. package/src/index.ts +6 -0
  83. package/src/mod.ts +4 -0
  84. package/src/packageDetect.ts +12 -0
  85. package/src/packageInfo.ts +6 -0
  86. package/src/types/augmentRegistry.ts +19 -0
  87. package/src/types/index.ts +11 -0
  88. package/src/types/lookup.ts +21 -0
  89. package/src/types/types.ts +47 -0
  90. package/src/util/encodeTypes.spec.ts +217 -0
  91. package/src/util/encodeTypes.ts +205 -0
  92. package/src/util/getTypeDef.spec.ts +704 -0
  93. package/src/util/getTypeDef.ts +279 -0
  94. package/src/util/index.ts +7 -0
  95. package/src/util/typeSplit.spec.ts +50 -0
  96. package/src/util/typeSplit.ts +56 -0
  97. package/src/util/xcm.ts +12 -0
  98. package/tsconfig.build.json +15 -0
  99. package/tsconfig.build.tsbuildinfo +1 -0
  100. package/tsconfig.spec.json +17 -0
  101. package/tsconfig.spec.tsbuildinfo +1 -0
  102. /package/{cjs → build}/bundle.d.ts +0 -0
  103. /package/{create → build/cjs/create}/class.d.ts +0 -0
  104. /package/{create → build/cjs/create}/index.d.ts +0 -0
  105. /package/{create → build/cjs/create}/type.d.ts +0 -0
  106. /package/{exports.d.ts → build/cjs/exports.d.ts} +0 -0
  107. /package/{index.d.ts → build/cjs/index.d.ts} +0 -0
  108. /package/{cjs → build/cjs}/package.json +0 -0
  109. /package/{packageDetect.d.ts → build/cjs/packageDetect.d.ts} +0 -0
  110. /package/{cjs → build/cjs}/packageDetect.js +0 -0
  111. /package/{packageInfo.d.ts → build/cjs/packageInfo.d.ts} +0 -0
  112. /package/{types → build/cjs/types}/augmentRegistry.d.ts +0 -0
  113. /package/{types → build/cjs/types}/index.d.ts +0 -0
  114. /package/{types → build/cjs/types}/lookup.d.ts +0 -0
  115. /package/{types → build/cjs/types}/types.d.ts +0 -0
  116. /package/{util → build/cjs/util}/encodeTypes.d.ts +0 -0
  117. /package/{util → build/cjs/util}/getTypeDef.d.ts +0 -0
  118. /package/{util → build/cjs/util}/index.d.ts +0 -0
  119. /package/{util → build/cjs/util}/typeSplit.d.ts +0 -0
  120. /package/{util → build/cjs/util}/xcm.d.ts +0 -0
  121. /package/{bundle.d.ts → build-tsc/bundle.d.ts} +0 -0
  122. /package/{cjs → build-tsc}/create/class.d.ts +0 -0
  123. /package/{cjs → build-tsc}/create/index.d.ts +0 -0
  124. /package/{cjs → build-tsc}/create/type.d.ts +0 -0
  125. /package/{cjs → build-tsc}/exports.d.ts +0 -0
  126. /package/{cjs → build-tsc}/index.d.ts +0 -0
  127. /package/{cjs → build-tsc}/packageDetect.d.ts +0 -0
  128. /package/{cjs → build-tsc}/packageInfo.d.ts +0 -0
  129. /package/{cjs → build-tsc}/types/augmentRegistry.d.ts +0 -0
  130. /package/{cjs → build-tsc}/types/index.d.ts +0 -0
  131. /package/{cjs → build-tsc}/types/lookup.d.ts +0 -0
  132. /package/{cjs → build-tsc}/types/types.d.ts +0 -0
  133. /package/{cjs → build-tsc}/util/encodeTypes.d.ts +0 -0
  134. /package/{cjs → build-tsc}/util/getTypeDef.d.ts +0 -0
  135. /package/{cjs → build-tsc}/util/index.d.ts +0 -0
  136. /package/{cjs → build-tsc}/util/typeSplit.d.ts +0 -0
  137. /package/{cjs → build-tsc}/util/xcm.d.ts +0 -0
  138. /package/{cjs → build-tsc-cjs}/bundle.js +0 -0
  139. /package/{cjs → build-tsc-cjs}/create/class.js +0 -0
  140. /package/{cjs → build-tsc-cjs}/create/index.js +0 -0
  141. /package/{cjs → build-tsc-cjs}/create/type.js +0 -0
  142. /package/{cjs → build-tsc-cjs}/exports.js +0 -0
  143. /package/{cjs → build-tsc-cjs}/index.js +0 -0
  144. /package/{cjs → build-tsc-cjs}/types/augmentRegistry.js +0 -0
  145. /package/{cjs → build-tsc-cjs}/types/index.js +0 -0
  146. /package/{cjs → build-tsc-cjs}/types/lookup.js +0 -0
  147. /package/{cjs → build-tsc-cjs}/types/types.js +0 -0
  148. /package/{cjs → build-tsc-cjs}/util/encodeTypes.js +0 -0
  149. /package/{cjs → build-tsc-cjs}/util/getTypeDef.js +0 -0
  150. /package/{cjs → build-tsc-cjs}/util/index.js +0 -0
  151. /package/{cjs → build-tsc-cjs}/util/typeSplit.js +0 -0
  152. /package/{cjs → build-tsc-cjs}/util/xcm.js +0 -0
  153. /package/{bundle.js → build-tsc-esm/bundle.js} +0 -0
  154. /package/{create → build-tsc-esm/create}/class.js +0 -0
  155. /package/{create → build-tsc-esm/create}/index.js +0 -0
  156. /package/{create → build-tsc-esm/create}/type.js +0 -0
  157. /package/{exports.js → build-tsc-esm/exports.js} +0 -0
  158. /package/{index.js → build-tsc-esm/index.js} +0 -0
  159. /package/{packageDetect.js → build-tsc-esm/packageDetect.js} +0 -0
  160. /package/{types → build-tsc-esm/types}/augmentRegistry.js +0 -0
  161. /package/{types → build-tsc-esm/types}/index.js +0 -0
  162. /package/{types → build-tsc-esm/types}/lookup.js +0 -0
  163. /package/{types → build-tsc-esm/types}/types.js +0 -0
  164. /package/{util → build-tsc-esm/util}/encodeTypes.js +0 -0
  165. /package/{util → build-tsc-esm/util}/getTypeDef.js +0 -0
  166. /package/{util → build-tsc-esm/util}/index.js +0 -0
  167. /package/{util → build-tsc-esm/util}/typeSplit.js +0 -0
  168. /package/{util → build-tsc-esm/util}/xcm.js +0 -0
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTypeDef = getTypeDef;
4
+ const types_codec_1 = require("@pezkuwi/types-codec");
5
+ const util_1 = require("@pezkuwi/util");
6
+ const index_js_1 = require("../types/index.js");
7
+ const typeSplit_js_1 = require("./typeSplit.js");
8
+ const KNOWN_INTERNALS = ['_alias', '_fallback'];
9
+ function getTypeString(typeOrObj) {
10
+ return (0, util_1.isString)(typeOrObj)
11
+ ? typeOrObj.toString()
12
+ : (0, util_1.stringify)(typeOrObj);
13
+ }
14
+ function isRustEnum(details) {
15
+ const values = Object.values(details);
16
+ if (values.some((v) => (0, util_1.isNumber)(v))) {
17
+ if (!values.every((v) => (0, util_1.isNumber)(v) && v >= 0 && v <= 255)) {
18
+ throw new Error('Invalid number-indexed enum definition');
19
+ }
20
+ return false;
21
+ }
22
+ return true;
23
+ }
24
+ function _decodeEnum(value, details, count, fallbackType) {
25
+ value.info = index_js_1.TypeDefInfo.Enum;
26
+ value.fallbackType = fallbackType;
27
+ // not as pretty, but remain compatible with oo7 for both struct and Array types
28
+ if (Array.isArray(details)) {
29
+ value.sub = details.map((name, index) => ({
30
+ index,
31
+ info: index_js_1.TypeDefInfo.Plain,
32
+ name,
33
+ type: 'Null'
34
+ }));
35
+ }
36
+ else if (isRustEnum(details)) {
37
+ value.sub = Object.entries(details).map(([name, typeOrObj], index) => (0, util_1.objectSpread)({}, getTypeDef(getTypeString(typeOrObj || 'Null'), { name }, count), { index }));
38
+ }
39
+ else {
40
+ value.sub = Object.entries(details).map(([name, index]) => ({
41
+ index,
42
+ info: index_js_1.TypeDefInfo.Plain,
43
+ name,
44
+ type: 'Null'
45
+ }));
46
+ }
47
+ return value;
48
+ }
49
+ function _decodeSet(value, details, fallbackType) {
50
+ value.info = index_js_1.TypeDefInfo.Set;
51
+ value.fallbackType = fallbackType;
52
+ value.length = details._bitLength;
53
+ value.sub = Object
54
+ .entries(details)
55
+ .filter(([name]) => !name.startsWith('_'))
56
+ .map(([name, index]) => ({
57
+ index,
58
+ info: index_js_1.TypeDefInfo.Plain,
59
+ name,
60
+ type: 'Null'
61
+ }));
62
+ return value;
63
+ }
64
+ function _decodeStruct(value, type, _, count) {
65
+ const parsed = JSON.parse(type);
66
+ const keys = Object.keys(parsed);
67
+ if (parsed._enum) {
68
+ return _decodeEnum(value, parsed._enum, count, parsed._fallback);
69
+ }
70
+ else if (parsed._set) {
71
+ return _decodeSet(value, parsed._set, parsed._fallback);
72
+ }
73
+ value.alias = parsed._alias
74
+ ? new Map(Object.entries(parsed._alias))
75
+ : undefined;
76
+ value.fallbackType = parsed._fallback;
77
+ value.sub = keys
78
+ .filter((name) => !KNOWN_INTERNALS.includes(name))
79
+ .map((name) => getTypeDef(getTypeString(parsed[name]), { name }, count));
80
+ return value;
81
+ }
82
+ function _decodeFixedVec(value, type, _, count) {
83
+ const max = type.length - 1;
84
+ let index = -1;
85
+ let inner = 0;
86
+ for (let i = 1; (i < max) && (index === -1); i++) {
87
+ switch (type[i]) {
88
+ case ';': {
89
+ if (inner === 0) {
90
+ index = i;
91
+ }
92
+ break;
93
+ }
94
+ case '[':
95
+ case '(':
96
+ case '<':
97
+ inner++;
98
+ break;
99
+ case ']':
100
+ case ')':
101
+ case '>':
102
+ inner--;
103
+ break;
104
+ }
105
+ }
106
+ if (index === -1) {
107
+ throw new Error(`${type}: Unable to extract location of ';'`);
108
+ }
109
+ const vecType = type.substring(1, index);
110
+ const [strLength, displayName] = type.substring(index + 1, max).split(';');
111
+ const length = parseInt(strLength.trim(), 10);
112
+ if (length > 2048) {
113
+ throw new Error(`${type}: Only support for [Type; <length>], where length <= 2048`);
114
+ }
115
+ value.displayName = displayName;
116
+ value.length = length;
117
+ value.sub = getTypeDef(vecType, {}, count);
118
+ return value;
119
+ }
120
+ function _decodeTuple(value, _, subType, count) {
121
+ value.sub = subType.length === 0
122
+ ? []
123
+ : (0, typeSplit_js_1.typeSplit)(subType).map((inner) => getTypeDef(inner, {}, count));
124
+ return value;
125
+ }
126
+ function _decodeAnyInt(value, type, _, clazz) {
127
+ const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(',');
128
+ const length = parseInt(strLength.trim(), 10);
129
+ if ((length > 8192) || (length % 8)) {
130
+ throw new Error(`${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
131
+ }
132
+ value.displayName = displayName;
133
+ value.length = length;
134
+ return value;
135
+ }
136
+ function _decodeInt(value, type, subType) {
137
+ return _decodeAnyInt(value, type, subType, 'Int');
138
+ }
139
+ function _decodeUInt(value, type, subType) {
140
+ return _decodeAnyInt(value, type, subType, 'UInt');
141
+ }
142
+ function _decodeDoNotConstruct(value, type, _) {
143
+ const NAME_LENGTH = 'DoNotConstruct'.length;
144
+ value.displayName = type.substring(NAME_LENGTH + 1, type.length - 1);
145
+ return value;
146
+ }
147
+ function hasWrapper(type, [start, end]) {
148
+ return (type.startsWith(start)) && (type.slice(-1 * end.length) === end);
149
+ }
150
+ const nestedExtraction = [
151
+ ['[', ']', index_js_1.TypeDefInfo.VecFixed, _decodeFixedVec],
152
+ ['{', '}', index_js_1.TypeDefInfo.Struct, _decodeStruct],
153
+ ['(', ')', index_js_1.TypeDefInfo.Tuple, _decodeTuple],
154
+ // the inner for these are the same as tuple, multiple values
155
+ ['BTreeMap<', '>', index_js_1.TypeDefInfo.BTreeMap, _decodeTuple],
156
+ ['HashMap<', '>', index_js_1.TypeDefInfo.HashMap, _decodeTuple],
157
+ ['Int<', '>', index_js_1.TypeDefInfo.Int, _decodeInt],
158
+ ['Result<', '>', index_js_1.TypeDefInfo.Result, _decodeTuple],
159
+ ['UInt<', '>', index_js_1.TypeDefInfo.UInt, _decodeUInt],
160
+ ['DoNotConstruct<', '>', index_js_1.TypeDefInfo.DoNotConstruct, _decodeDoNotConstruct]
161
+ ];
162
+ const wrappedExtraction = [
163
+ ['BTreeSet<', '>', index_js_1.TypeDefInfo.BTreeSet],
164
+ ['Compact<', '>', index_js_1.TypeDefInfo.Compact],
165
+ ['Linkage<', '>', index_js_1.TypeDefInfo.Linkage],
166
+ ['Opaque<', '>', index_js_1.TypeDefInfo.WrapperOpaque],
167
+ ['Option<', '>', index_js_1.TypeDefInfo.Option],
168
+ ['Range<', '>', index_js_1.TypeDefInfo.Range],
169
+ ['RangeInclusive<', '>', index_js_1.TypeDefInfo.RangeInclusive],
170
+ ['Vec<', '>', index_js_1.TypeDefInfo.Vec],
171
+ ['WrapperKeepOpaque<', '>', index_js_1.TypeDefInfo.WrapperKeepOpaque],
172
+ ['WrapperOpaque<', '>', index_js_1.TypeDefInfo.WrapperOpaque]
173
+ ];
174
+ function extractSubType(type, [start, end]) {
175
+ return type.substring(start.length, type.length - end.length);
176
+ }
177
+ function getTypeDef(_type, { displayName, name } = {}, count = 0) {
178
+ // create the type via Type, allowing types to be sanitized
179
+ const type = (0, types_codec_1.sanitize)(_type);
180
+ const value = { displayName, info: index_js_1.TypeDefInfo.Plain, name, type };
181
+ if (++count > 64) {
182
+ throw new Error('getTypeDef: Maximum nested limit reached');
183
+ }
184
+ const nested = nestedExtraction.find((nested) => hasWrapper(type, nested));
185
+ if (nested) {
186
+ value.info = nested[2];
187
+ return nested[3](value, type, extractSubType(type, nested), count);
188
+ }
189
+ const wrapped = wrappedExtraction.find((wrapped) => hasWrapper(type, wrapped));
190
+ if (wrapped) {
191
+ value.info = wrapped[2];
192
+ value.sub = getTypeDef(extractSubType(type, wrapped), {}, count);
193
+ }
194
+ return value;
195
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./encodeTypes.js"), exports);
5
+ tslib_1.__exportStar(require("./getTypeDef.js"), exports);
6
+ tslib_1.__exportStar(require("./typeSplit.js"), exports);
7
+ tslib_1.__exportStar(require("./xcm.js"), exports);
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.typeSplit = typeSplit;
4
+ function typeSplit(type) {
5
+ const result = [];
6
+ // these are the depths of the various tokens: <, [, {, (
7
+ let c = 0;
8
+ let f = 0;
9
+ let s = 0;
10
+ let t = 0;
11
+ // current start position
12
+ let start = 0;
13
+ for (let i = 0, count = type.length; i < count; i++) {
14
+ switch (type[i]) {
15
+ // if we are not nested, add the type
16
+ case ',': {
17
+ if (!(c || f || s || t)) {
18
+ result.push(type.substring(start, i).trim());
19
+ start = i + 1;
20
+ }
21
+ break;
22
+ }
23
+ // adjust compact/vec (and friends) depth
24
+ case '<':
25
+ c++;
26
+ break;
27
+ case '>':
28
+ c--;
29
+ break;
30
+ // adjust fixed vec depths
31
+ case '[':
32
+ f++;
33
+ break;
34
+ case ']':
35
+ f--;
36
+ break;
37
+ // adjust struct depth
38
+ case '{':
39
+ s++;
40
+ break;
41
+ case '}':
42
+ s--;
43
+ break;
44
+ // adjust tuple depth
45
+ case '(':
46
+ t++;
47
+ break;
48
+ case ')':
49
+ t--;
50
+ break;
51
+ }
52
+ }
53
+ // ensure we have all the terminators taken care of
54
+ if (c || f || s || t) {
55
+ throw new Error(`Invalid definition (missing terminators) found in ${type}`);
56
+ }
57
+ // the final leg of the journey
58
+ result.push(type.substring(start, type.length).trim());
59
+ return result;
60
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.XCM_MAPPINGS = void 0;
4
+ exports.mapXcmTypes = mapXcmTypes;
5
+ const util_1 = require("@pezkuwi/util");
6
+ exports.XCM_MAPPINGS = ['AssetInstance', 'Fungibility', 'Junction', 'Junctions', 'MultiAsset', 'MultiAssetFilter', 'MultiLocation', 'Response', 'WildFungibility', 'WildMultiAsset', 'Xcm', 'XcmError'];
7
+ function mapXcmTypes(version) {
8
+ return exports.XCM_MAPPINGS.reduce((all, key) => (0, util_1.objectSpread)(all, { [key]: `${key}${version}` }), {});
9
+ }
@@ -0,0 +1,5 @@
1
+ import type { Codec, CodecClass, Registry } from '@pezkuwi/types-codec/types';
2
+ import type { TypeDef } from '../types/index.js';
3
+ export declare function constructTypeClass<T extends Codec = Codec>(registry: Registry, typeDef: TypeDef): CodecClass<T>;
4
+ export declare function getTypeClass<T extends Codec = Codec>(registry: Registry, typeDef: TypeDef): CodecClass<T>;
5
+ export declare function createClassUnsafe<T extends Codec = Codec, K extends string = string>(registry: Registry, type: K): CodecClass<T>;
@@ -0,0 +1,160 @@
1
+ import { BTreeMap, BTreeSet, Bytes, CodecSet, Compact, DoNotConstruct, Enum, HashMap, Int, Null, Option, Range, RangeInclusive, Result, Struct, Tuple, U8aFixed, UInt, Vec, VecFixed, WrapperKeepOpaque, WrapperOpaque } from '@pezkuwi/types-codec';
2
+ import { isNumber, stringify } from '@pezkuwi/util';
3
+ import { TypeDefInfo } from '../types/index.js';
4
+ import { getTypeDef } from '../util/getTypeDef.js';
5
+ function getTypeDefType({ lookupName, type }) {
6
+ return lookupName || type;
7
+ }
8
+ function getSubDefArray(value) {
9
+ if (!Array.isArray(value.sub)) {
10
+ throw new Error(`Expected subtype as TypeDef[] in ${stringify(value)}`);
11
+ }
12
+ return value.sub;
13
+ }
14
+ function getSubDef(value) {
15
+ if (!value.sub || Array.isArray(value.sub)) {
16
+ throw new Error(`Expected subtype as TypeDef in ${stringify(value)}`);
17
+ }
18
+ return value.sub;
19
+ }
20
+ function getSubType(value) {
21
+ return getTypeDefType(getSubDef(value));
22
+ }
23
+ function getTypeClassMap(value) {
24
+ const subs = getSubDefArray(value);
25
+ const map = {};
26
+ for (let i = 0, count = subs.length; i < count; i++) {
27
+ const sub = subs[i];
28
+ if (!sub.name) {
29
+ throw new Error(`No name found in definition ${stringify(sub)}`);
30
+ }
31
+ map[sub.name] = getTypeDefType(sub);
32
+ }
33
+ return map;
34
+ }
35
+ function getTypeClassArray(value) {
36
+ return getSubDefArray(value).map(getTypeDefType);
37
+ }
38
+ function createInt(Clazz, { displayName, length }) {
39
+ if (!isNumber(length)) {
40
+ throw new Error(`Expected bitLength information for ${displayName || Clazz.constructor.name}<bitLength>`);
41
+ }
42
+ return Clazz.with(length, displayName);
43
+ }
44
+ function createHashMap(Clazz, value) {
45
+ const [keyType, valueType] = getTypeClassArray(value);
46
+ return Clazz.with(keyType, valueType);
47
+ }
48
+ function createWithSub(Clazz, value) {
49
+ return Clazz.with(getSubType(value));
50
+ }
51
+ const infoMapping = {
52
+ [TypeDefInfo.BTreeMap]: (_registry, value) => createHashMap(BTreeMap, value),
53
+ [TypeDefInfo.BTreeSet]: (_registry, value) => createWithSub(BTreeSet, value),
54
+ [TypeDefInfo.Compact]: (_registry, value) => createWithSub(Compact, value),
55
+ [TypeDefInfo.DoNotConstruct]: (_registry, value) => DoNotConstruct.with(value.displayName || value.type),
56
+ [TypeDefInfo.Enum]: (_registry, value) => {
57
+ const subs = getSubDefArray(value);
58
+ return Enum.with(subs.every(({ type }) => type === 'Null')
59
+ ? subs.reduce((out, { index, name }, count) => {
60
+ if (!name) {
61
+ throw new Error('No name found in sub definition');
62
+ }
63
+ out[name] = index || count;
64
+ return out;
65
+ }, {})
66
+ : getTypeClassMap(value));
67
+ },
68
+ [TypeDefInfo.HashMap]: (_registry, value) => createHashMap(HashMap, value),
69
+ [TypeDefInfo.Int]: (_registry, value) => createInt(Int, value),
70
+ // We have circular deps between Linkage & Struct
71
+ [TypeDefInfo.Linkage]: (_registry, value) => {
72
+ const type = `Option<${getSubType(value)}>`;
73
+ // eslint-disable-next-line sort-keys
74
+ const Clazz = Struct.with({ previous: type, next: type });
75
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
76
+ Clazz.prototype.toRawType = function () {
77
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call
78
+ return `Linkage<${this.next.toRawType(true)}>`;
79
+ };
80
+ return Clazz;
81
+ },
82
+ [TypeDefInfo.Null]: (_registry, _value) => Null,
83
+ [TypeDefInfo.Option]: (_registry, value) => {
84
+ if (!value.sub || Array.isArray(value.sub)) {
85
+ throw new Error('Expected type information for Option');
86
+ }
87
+ // NOTE This is opt-in (unhandled), not by default
88
+ // if (value.sub.type === 'bool') {
89
+ // return OptionBool;
90
+ // }
91
+ return createWithSub(Option, value);
92
+ },
93
+ [TypeDefInfo.Plain]: (registry, value) => registry.getOrUnknown(value.type),
94
+ [TypeDefInfo.Range]: (_registry, value) => createWithSub(Range, value),
95
+ [TypeDefInfo.RangeInclusive]: (_registry, value) => createWithSub(RangeInclusive, value),
96
+ [TypeDefInfo.Result]: (_registry, value) => {
97
+ const [Ok, Err] = getTypeClassArray(value);
98
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
99
+ return Result.with({ Err, Ok });
100
+ },
101
+ [TypeDefInfo.Set]: (_registry, value) => CodecSet.with(getSubDefArray(value).reduce((result, { index, name }) => {
102
+ if (!name || !isNumber(index)) {
103
+ throw new Error('No name found in sub definition');
104
+ }
105
+ result[name] = index;
106
+ return result;
107
+ }, {}), value.length),
108
+ [TypeDefInfo.Si]: (registry, value) => getTypeClass(registry, registry.lookup.getTypeDef(value.type)),
109
+ [TypeDefInfo.Struct]: (_registry, value) => Struct.with(getTypeClassMap(value), value.alias),
110
+ [TypeDefInfo.Tuple]: (_registry, value) => Tuple.with(getTypeClassArray(value)),
111
+ [TypeDefInfo.UInt]: (_registry, value) => createInt(UInt, value),
112
+ [TypeDefInfo.Vec]: (_registry, { sub }) => {
113
+ if (!sub || Array.isArray(sub)) {
114
+ throw new Error('Expected type information for vector');
115
+ }
116
+ return (sub.type === 'u8'
117
+ ? Bytes
118
+ : Vec.with(getTypeDefType(sub)));
119
+ },
120
+ [TypeDefInfo.VecFixed]: (_registry, { displayName, length, sub }) => {
121
+ if (!isNumber(length) || !sub || Array.isArray(sub)) {
122
+ throw new Error('Expected length & type information for fixed vector');
123
+ }
124
+ return (sub.type === 'u8'
125
+ ? U8aFixed.with((length * 8), displayName)
126
+ : VecFixed.with(getTypeDefType(sub), length));
127
+ },
128
+ [TypeDefInfo.WrapperKeepOpaque]: (_registry, value) => createWithSub(WrapperKeepOpaque, value),
129
+ [TypeDefInfo.WrapperOpaque]: (_registry, value) => createWithSub(WrapperOpaque, value)
130
+ };
131
+ export function constructTypeClass(registry, typeDef) {
132
+ try {
133
+ const Type = infoMapping[typeDef.info](registry, typeDef);
134
+ if (!Type) {
135
+ throw new Error('No class created');
136
+ }
137
+ // don't clobber any existing
138
+ if (!Type.__fallbackType && typeDef.fallbackType) {
139
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
140
+ // @ts-ignore ...this is the only place we we actually assign this...
141
+ Type.__fallbackType = typeDef.fallbackType;
142
+ }
143
+ return Type;
144
+ }
145
+ catch (error) {
146
+ throw new Error(`Unable to construct class from ${stringify(typeDef)}: ${error.message}`);
147
+ }
148
+ }
149
+ export function getTypeClass(registry, typeDef) {
150
+ return registry.getUnsafe(typeDef.type, false, typeDef);
151
+ }
152
+ export function createClassUnsafe(registry, type) {
153
+ return (
154
+ // just retrieve via name, no creation via typeDef
155
+ registry.getUnsafe(type) ||
156
+ // we don't have an existing type, create the class via typeDef
157
+ getTypeClass(registry, registry.isLookupType(type)
158
+ ? registry.lookup.getTypeDef(type)
159
+ : getTypeDef(type)));
160
+ }
@@ -0,0 +1,2 @@
1
+ export * from './class.js';
2
+ export * from './type.js';
@@ -0,0 +1,2 @@
1
+ export * from './class.js';
2
+ export * from './type.js';
@@ -0,0 +1,3 @@
1
+ import type { Codec, Registry } from '@pezkuwi/types-codec/types';
2
+ import type { CreateOptions } from '../types/index.js';
3
+ export declare function createTypeUnsafe<T extends Codec = Codec, K extends string = string>(registry: Registry, type: K, params?: unknown[], options?: CreateOptions): T;
@@ -0,0 +1,63 @@
1
+ import { Option } from '@pezkuwi/types-codec';
2
+ import { isHex, isU8a, u8aEq, u8aToHex, u8aToU8a } from '@pezkuwi/util';
3
+ import { createClassUnsafe } from './class.js';
4
+ function checkInstance(created, matcher) {
5
+ const u8a = created.toU8a();
6
+ const rawType = created.toRawType();
7
+ const isOk = (
8
+ // full match, all ok
9
+ u8aEq(u8a, matcher) ||
10
+ (
11
+ // on a length-prefixed type, just check the actual length
12
+ ['Bytes', 'Text', 'Type'].includes(rawType) &&
13
+ matcher.length === created.length) ||
14
+ (
15
+ // when the created is empty and matcher is also empty, let it slide...
16
+ created.isEmpty &&
17
+ matcher.every((v) => !v)));
18
+ if (!isOk) {
19
+ throw new Error(`${rawType}:: Decoded input doesn't match input, received ${u8aToHex(matcher, 512)} (${matcher.length} bytes), created ${u8aToHex(u8a, 512)} (${u8a.length} bytes)`);
20
+ }
21
+ }
22
+ function checkPedantic(created, [value]) {
23
+ if (isU8a(value)) {
24
+ checkInstance(created, value);
25
+ }
26
+ else if (isHex(value)) {
27
+ checkInstance(created, u8aToU8a(value));
28
+ }
29
+ }
30
+ function initType(registry, Type, params = [], { blockHash, isFallback, isOptional, isPedantic } = {}) {
31
+ const created = new (isOptional
32
+ ? Option.with(Type)
33
+ : Type)(registry, ...params);
34
+ isPedantic && checkPedantic(created, params);
35
+ if (blockHash) {
36
+ created.createdAtHash = createTypeUnsafe(registry, 'BlockHash', [blockHash]);
37
+ }
38
+ if (isFallback) {
39
+ created.isStorageFallback = true;
40
+ }
41
+ return created;
42
+ }
43
+ export function createTypeUnsafe(registry, type, params = [], options = {}) {
44
+ let Clazz = null;
45
+ let firstError = null;
46
+ try {
47
+ Clazz = createClassUnsafe(registry, type);
48
+ return initType(registry, Clazz, params, options);
49
+ }
50
+ catch (error) {
51
+ firstError = new Error(`createType(${type}):: ${error.message}`);
52
+ }
53
+ if (Clazz?.__fallbackType) {
54
+ try {
55
+ Clazz = createClassUnsafe(registry, Clazz.__fallbackType);
56
+ return initType(registry, Clazz, params, options);
57
+ }
58
+ catch {
59
+ // swallow, we will throw the first error again
60
+ }
61
+ }
62
+ throw firstError;
63
+ }
@@ -0,0 +1,2 @@
1
+ export * from './create/index.js';
2
+ export * from './util/index.js';
@@ -0,0 +1,2 @@
1
+ export * from './create/index.js';
2
+ export * from './util/index.js';
@@ -0,0 +1,2 @@
1
+ import './packageDetect.js';
2
+ export * from './bundle.js';
package/build/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import './packageDetect.js';
2
+ export * from './bundle.js';