@airgap/icp 0.13.15-beta.1 → 0.13.15-beta.3

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 (89) hide show
  1. package/package.json +8 -6
  2. package/v1/block-explorer/ICPBlockExplorer.d.ts +1 -1
  3. package/v1/block-explorer/ICPBlockExplorer.js +16 -49
  4. package/v1/block-explorer/ICPBlockExplorer.js.map +1 -1
  5. package/v1/index.d.ts +2 -2
  6. package/v1/index.js +7 -5
  7. package/v1/index.js.map +1 -1
  8. package/v1/module/ICPModule.d.ts +2 -4
  9. package/v1/module/ICPModule.js +53 -94
  10. package/v1/module/ICPModule.js.map +1 -1
  11. package/v1/module.js +1 -1
  12. package/v1/module.js.map +1 -1
  13. package/v1/protocol/ICPGovernance.d.ts +38 -0
  14. package/v1/protocol/ICPGovernance.js +748 -0
  15. package/v1/protocol/ICPGovernance.js.map +1 -0
  16. package/v1/protocol/ICPImplementation.d.ts +27 -5
  17. package/v1/protocol/ICPImplementation.js +368 -250
  18. package/v1/protocol/ICPImplementation.js.map +1 -1
  19. package/v1/protocol/ICPProtocol.d.ts +33 -8
  20. package/v1/protocol/ICPProtocol.js +508 -322
  21. package/v1/protocol/ICPProtocol.js.map +1 -1
  22. package/v1/protocol/icrc/CkBTCProtocol.js +138 -225
  23. package/v1/protocol/icrc/CkBTCProtocol.js.map +1 -1
  24. package/v1/protocol/icrc/ICRC1Protocol.d.ts +4 -4
  25. package/v1/protocol/icrc/ICRC1Protocol.js +338 -553
  26. package/v1/protocol/icrc/ICRC1Protocol.js.map +1 -1
  27. package/v1/serializer/v3/schemas/converter/transaction-converter.js +17 -4
  28. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
  29. package/v1/serializer/v3/schemas/generated/transaction-sign-request-icp.json +35 -6
  30. package/v1/serializer/v3/serializer-companion.js +86 -144
  31. package/v1/serializer/v3/serializer-companion.js.map +1 -1
  32. package/v1/serializer/v3/validators/transaction-validator.js +22 -60
  33. package/v1/serializer/v3/validators/transaction-validator.js.map +1 -1
  34. package/v1/serializer/v3/validators/validators.js +19 -58
  35. package/v1/serializer/v3/validators/validators.js.map +1 -1
  36. package/v1/types/governance.d.ts +2 -0
  37. package/v1/types/governance.js +268 -210
  38. package/v1/types/governance.js.map +1 -1
  39. package/v1/types/icrc/ledger.js +22 -23
  40. package/v1/types/icrc/ledger.js.map +1 -1
  41. package/v1/types/ledger.d.ts +1 -0
  42. package/v1/types/ledger.js +37 -38
  43. package/v1/types/ledger.js.map +1 -1
  44. package/v1/types/management_idl.js +6 -7
  45. package/v1/types/management_idl.js.map +1 -1
  46. package/v1/types/protocol.d.ts +2 -1
  47. package/v1/types/transaction.d.ts +21 -3
  48. package/v1/types/transaction.js +28 -0
  49. package/v1/types/transaction.js.map +1 -1
  50. package/v1/utils/account.js +42 -70
  51. package/v1/utils/account.js.map +1 -1
  52. package/v1/utils/actor.js +139 -322
  53. package/v1/utils/actor.js.map +1 -1
  54. package/v1/utils/auth.js +44 -134
  55. package/v1/utils/auth.js.map +1 -1
  56. package/v1/utils/bls.js +41 -109
  57. package/v1/utils/bls.js.map +1 -1
  58. package/v1/utils/buffer.js +47 -111
  59. package/v1/utils/buffer.js.map +1 -1
  60. package/v1/utils/cbor.js +50 -85
  61. package/v1/utils/cbor.js.map +1 -1
  62. package/v1/utils/certificate.js +116 -238
  63. package/v1/utils/certificate.js.map +1 -1
  64. package/v1/utils/convert.d.ts +1 -1
  65. package/v1/utils/convert.js +29 -49
  66. package/v1/utils/convert.js.map +1 -1
  67. package/v1/utils/der.js +37 -35
  68. package/v1/utils/der.js.map +1 -1
  69. package/v1/utils/errors.js +10 -30
  70. package/v1/utils/errors.js.map +1 -1
  71. package/v1/utils/hdkey.js +33 -33
  72. package/v1/utils/hdkey.js.map +1 -1
  73. package/v1/utils/http.d.ts +1 -1
  74. package/v1/utils/http.js +202 -426
  75. package/v1/utils/http.js.map +1 -1
  76. package/v1/utils/icrc1.js +24 -41
  77. package/v1/utils/icrc1.js.map +1 -1
  78. package/v1/utils/idl.js +780 -1159
  79. package/v1/utils/idl.js.map +1 -1
  80. package/v1/utils/leb128.js +29 -30
  81. package/v1/utils/leb128.js.map +1 -1
  82. package/v1/utils/polling.js +84 -234
  83. package/v1/utils/polling.js.map +1 -1
  84. package/v1/utils/principal.js +78 -126
  85. package/v1/utils/principal.js.map +1 -1
  86. package/v1/utils/secp256k1.js +68 -134
  87. package/v1/utils/secp256k1.js.map +1 -1
  88. package/v1/utils/transaction.js +7 -7
  89. package/v1/utils/transaction.js.map +1 -1
package/v1/utils/idl.js CHANGED
@@ -1,62 +1,11 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __values = (this && this.__values) || function(o) {
18
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
19
- if (m) return m.call(o);
20
- if (o && typeof o.length === "number") return {
21
- next: function () {
22
- if (o && i >= o.length) o = void 0;
23
- return { value: o && o[i++], done: !o };
24
- }
25
- };
26
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
27
- };
28
- var __read = (this && this.__read) || function (o, n) {
29
- var m = typeof Symbol === "function" && o[Symbol.iterator];
30
- if (!m) return o;
31
- var i = m.call(o), r, ar = [], e;
32
- try {
33
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
34
- }
35
- catch (error) { e = { error: error }; }
36
- finally {
37
- try {
38
- if (r && !r.done && (m = i["return"])) m.call(i);
39
- }
40
- finally { if (e) throw e.error; }
41
- }
42
- return ar;
43
- };
44
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
45
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
46
- if (ar || !(i in from)) {
47
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
48
- ar[i] = from[i];
49
- }
50
- }
51
- return to.concat(ar || Array.prototype.slice.call(from));
52
- };
53
2
  Object.defineProperty(exports, "__esModule", { value: true });
54
3
  exports.Vec = exports.Tuple = exports.Principal = exports.Nat64 = exports.Nat32 = exports.Nat16 = exports.Nat8 = exports.Int64 = exports.Int32 = exports.Int16 = exports.Int8 = exports.Float64 = exports.Float32 = exports.Nat = exports.Int = exports.Text = exports.Null = exports.Bool = exports.Unknown = exports.Reserved = exports.Empty = exports.decode = exports.encode = exports.ServiceClass = exports.FuncClass = exports.PrincipalClass = exports.RecClass = exports.VariantClass = exports.TupleClass = exports.RecordClass = exports.OptClass = exports.VecClass = exports.FixedNatClass = exports.FixedIntClass = exports.FloatClass = exports.NatClass = exports.IntClass = exports.TextClass = exports.ReservedClass = exports.NullClass = exports.BoolClass = exports.UnknownClass = exports.EmptyClass = exports.ConstructType = exports.PrimitiveType = exports.Type = exports.Visitor = exports.idlLabelToId = exports.iexp2 = exports.ilog2 = void 0;
55
4
  exports.Service = exports.Func = exports.Rec = exports.Variant = exports.Record = exports.Opt = void 0;
56
- var buffer_1 = require("./buffer");
57
- var leb128_1 = require("./leb128");
58
- var buffer_2 = require("./buffer");
59
- var principal_1 = require("./principal");
5
+ const buffer_1 = require("./buffer");
6
+ const leb128_1 = require("./leb128");
7
+ const buffer_2 = require("./buffer");
8
+ const principal_1 = require("./principal");
60
9
  /**
61
10
  * Equivalent to `Math.log2(n)` with support for `BigInt` values
62
11
  *
@@ -64,7 +13,7 @@ var principal_1 = require("./principal");
64
13
  * @returns integer
65
14
  */
66
15
  function ilog2(n) {
67
- var nBig = BigInt(n);
16
+ const nBig = BigInt(n);
68
17
  if (n <= 0) {
69
18
  throw new RangeError('Input must be positive');
70
19
  }
@@ -79,7 +28,7 @@ exports.ilog2 = ilog2;
79
28
  * @returns bigint
80
29
  */
81
30
  function iexp2(n) {
82
- var nBig = BigInt(n);
31
+ const nBig = BigInt(n);
83
32
  if (n < 0) {
84
33
  throw new RangeError('Input must be non-negative');
85
34
  }
@@ -87,22 +36,11 @@ function iexp2(n) {
87
36
  }
88
37
  exports.iexp2 = iexp2;
89
38
  function idlHash(s) {
90
- var e_1, _a;
91
- var utf8encoder = new TextEncoder();
92
- var array = utf8encoder.encode(s);
93
- var h = 0;
94
- try {
95
- for (var array_1 = __values(array), array_1_1 = array_1.next(); !array_1_1.done; array_1_1 = array_1.next()) {
96
- var c = array_1_1.value;
97
- h = (h * 223 + c) % Math.pow(2, 32);
98
- }
99
- }
100
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
101
- finally {
102
- try {
103
- if (array_1_1 && !array_1_1.done && (_a = array_1.return)) _a.call(array_1);
104
- }
105
- finally { if (e_1) throw e_1.error; }
39
+ const utf8encoder = new TextEncoder();
40
+ const array = utf8encoder.encode(s);
41
+ let h = 0;
42
+ for (const c of array) {
43
+ h = (h * 223 + c) % 2 ** 32;
106
44
  }
107
45
  return h;
108
46
  }
@@ -113,39 +51,39 @@ function idlHash(s) {
113
51
  */
114
52
  function idlLabelToId(label) {
115
53
  if (/^_\d+_$/.test(label) || /^_0x[0-9a-fA-F]+_$/.test(label)) {
116
- var num = +label.slice(1, -1);
117
- if (Number.isSafeInteger(num) && num >= 0 && num < Math.pow(2, 32)) {
54
+ const num = +label.slice(1, -1);
55
+ if (Number.isSafeInteger(num) && num >= 0 && num < 2 ** 32) {
118
56
  return num;
119
57
  }
120
58
  }
121
59
  return idlHash(label);
122
60
  }
123
61
  exports.idlLabelToId = idlLabelToId;
124
- var magicNumber = 'DIDL';
125
- var toReadableString_max = 400; // will not display arguments after 400chars. Makes sure 2mb blobs don't get inside the error
62
+ const magicNumber = 'DIDL';
63
+ const toReadableString_max = 400; // will not display arguments after 400chars. Makes sure 2mb blobs don't get inside the error
126
64
  function zipWith(xs, ys, f) {
127
- return xs.map(function (x, i) { return f(x, ys[i]); });
65
+ return xs.map((x, i) => f(x, ys[i]));
128
66
  }
129
67
  /**
130
68
  * An IDL Type Table, which precedes the data in the stream.
131
69
  */
132
- var TypeTable = /** @class */ (function () {
133
- function TypeTable() {
70
+ class TypeTable {
71
+ constructor() {
134
72
  // List of types. Needs to be an array as the index needs to be stable.
135
73
  this._typs = [];
136
74
  this._idx = new Map();
137
75
  }
138
- TypeTable.prototype.has = function (obj) {
76
+ has(obj) {
139
77
  return this._idx.has(obj.name);
140
- };
141
- TypeTable.prototype.add = function (type, buf) {
142
- var idx = this._typs.length;
78
+ }
79
+ add(type, buf) {
80
+ const idx = this._typs.length;
143
81
  this._idx.set(type.name, idx);
144
82
  this._typs.push(buf);
145
- };
146
- TypeTable.prototype.merge = function (obj, knot) {
147
- var idx = this._idx.get(obj.name);
148
- var knotIdx = this._idx.get(knot);
83
+ }
84
+ merge(obj, knot) {
85
+ const idx = this._idx.get(obj.name);
86
+ const knotIdx = this._idx.get(knot);
149
87
  if (idx === undefined) {
150
88
  throw new Error('Missing type index for ' + obj);
151
89
  }
@@ -156,194 +94,168 @@ var TypeTable = /** @class */ (function () {
156
94
  // Delete the type.
157
95
  this._typs.splice(knotIdx, 1);
158
96
  this._idx.delete(knot);
159
- };
160
- TypeTable.prototype.encode = function () {
161
- var len = (0, leb128_1.lebEncode)(this._typs.length);
162
- var buf = buffer_1.concat.apply(void 0, __spreadArray([], __read(this._typs), false));
97
+ }
98
+ encode() {
99
+ const len = (0, leb128_1.lebEncode)(this._typs.length);
100
+ const buf = (0, buffer_1.concat)(...this._typs);
163
101
  return (0, buffer_1.concat)(len, buf);
164
- };
165
- TypeTable.prototype.indexOf = function (typeName) {
102
+ }
103
+ indexOf(typeName) {
166
104
  if (!this._idx.has(typeName)) {
167
105
  throw new Error('Missing type index for ' + typeName);
168
106
  }
169
107
  return (0, leb128_1.slebEncode)(this._idx.get(typeName) || 0);
170
- };
171
- return TypeTable;
172
- }());
173
- var Visitor = /** @class */ (function () {
174
- function Visitor() {
175
108
  }
176
- Visitor.prototype.visitType = function (t, data) {
109
+ }
110
+ class Visitor {
111
+ visitType(t, data) {
177
112
  throw new Error('Not implemented');
178
- };
179
- Visitor.prototype.visitPrimitive = function (t, data) {
113
+ }
114
+ visitPrimitive(t, data) {
180
115
  return this.visitType(t, data);
181
- };
182
- Visitor.prototype.visitEmpty = function (t, data) {
116
+ }
117
+ visitEmpty(t, data) {
183
118
  return this.visitPrimitive(t, data);
184
- };
185
- Visitor.prototype.visitBool = function (t, data) {
119
+ }
120
+ visitBool(t, data) {
186
121
  return this.visitPrimitive(t, data);
187
- };
188
- Visitor.prototype.visitNull = function (t, data) {
122
+ }
123
+ visitNull(t, data) {
189
124
  return this.visitPrimitive(t, data);
190
- };
191
- Visitor.prototype.visitReserved = function (t, data) {
125
+ }
126
+ visitReserved(t, data) {
192
127
  return this.visitPrimitive(t, data);
193
- };
194
- Visitor.prototype.visitText = function (t, data) {
128
+ }
129
+ visitText(t, data) {
195
130
  return this.visitPrimitive(t, data);
196
- };
197
- Visitor.prototype.visitNumber = function (t, data) {
131
+ }
132
+ visitNumber(t, data) {
198
133
  return this.visitPrimitive(t, data);
199
- };
200
- Visitor.prototype.visitInt = function (t, data) {
134
+ }
135
+ visitInt(t, data) {
201
136
  return this.visitNumber(t, data);
202
- };
203
- Visitor.prototype.visitNat = function (t, data) {
137
+ }
138
+ visitNat(t, data) {
204
139
  return this.visitNumber(t, data);
205
- };
206
- Visitor.prototype.visitFloat = function (t, data) {
140
+ }
141
+ visitFloat(t, data) {
207
142
  return this.visitPrimitive(t, data);
208
- };
209
- Visitor.prototype.visitFixedInt = function (t, data) {
143
+ }
144
+ visitFixedInt(t, data) {
210
145
  return this.visitNumber(t, data);
211
- };
212
- Visitor.prototype.visitFixedNat = function (t, data) {
146
+ }
147
+ visitFixedNat(t, data) {
213
148
  return this.visitNumber(t, data);
214
- };
215
- Visitor.prototype.visitPrincipal = function (t, data) {
149
+ }
150
+ visitPrincipal(t, data) {
216
151
  return this.visitPrimitive(t, data);
217
- };
218
- Visitor.prototype.visitConstruct = function (t, data) {
152
+ }
153
+ visitConstruct(t, data) {
219
154
  return this.visitType(t, data);
220
- };
221
- Visitor.prototype.visitVec = function (t, ty, data) {
155
+ }
156
+ visitVec(t, ty, data) {
222
157
  return this.visitConstruct(t, data);
223
- };
224
- Visitor.prototype.visitOpt = function (t, ty, data) {
158
+ }
159
+ visitOpt(t, ty, data) {
225
160
  return this.visitConstruct(t, data);
226
- };
227
- Visitor.prototype.visitRecord = function (t, fields, data) {
161
+ }
162
+ visitRecord(t, fields, data) {
228
163
  return this.visitConstruct(t, data);
229
- };
230
- Visitor.prototype.visitTuple = function (t, components, data) {
231
- var fields = components.map(function (ty, i) { return ["_".concat(i, "_"), ty]; });
164
+ }
165
+ visitTuple(t, components, data) {
166
+ const fields = components.map((ty, i) => [`_${i}_`, ty]);
232
167
  return this.visitRecord(t, fields, data);
233
- };
234
- Visitor.prototype.visitVariant = function (t, fields, data) {
168
+ }
169
+ visitVariant(t, fields, data) {
235
170
  return this.visitConstruct(t, data);
236
- };
237
- Visitor.prototype.visitRec = function (t, ty, data) {
171
+ }
172
+ visitRec(t, ty, data) {
238
173
  return this.visitConstruct(ty, data);
239
- };
240
- Visitor.prototype.visitFunc = function (t, data) {
174
+ }
175
+ visitFunc(t, data) {
241
176
  return this.visitConstruct(t, data);
242
- };
243
- Visitor.prototype.visitService = function (t, data) {
177
+ }
178
+ visitService(t, data) {
244
179
  return this.visitConstruct(t, data);
245
- };
246
- return Visitor;
247
- }());
180
+ }
181
+ }
248
182
  exports.Visitor = Visitor;
249
183
  /**
250
184
  * Represents an IDL type.
251
185
  */
252
- var Type = /** @class */ (function () {
253
- function Type() {
254
- }
186
+ class Type {
255
187
  /* Display type name */
256
- Type.prototype.display = function () {
188
+ display() {
257
189
  return this.name;
258
- };
259
- Type.prototype.valueToString = function (x) {
190
+ }
191
+ valueToString(x) {
260
192
  return toReadableString(x);
261
- };
193
+ }
262
194
  /* Implement `T` in the IDL spec, only needed for non-primitive types */
263
- Type.prototype.buildTypeTable = function (typeTable) {
195
+ buildTypeTable(typeTable) {
264
196
  if (!typeTable.has(this)) {
265
197
  this._buildTypeTableImpl(typeTable);
266
198
  }
267
- };
268
- return Type;
269
- }());
270
- exports.Type = Type;
271
- var PrimitiveType = /** @class */ (function (_super) {
272
- __extends(PrimitiveType, _super);
273
- function PrimitiveType() {
274
- return _super !== null && _super.apply(this, arguments) || this;
275
199
  }
276
- PrimitiveType.prototype.checkType = function (t) {
200
+ }
201
+ exports.Type = Type;
202
+ class PrimitiveType extends Type {
203
+ checkType(t) {
277
204
  if (this.name !== t.name) {
278
- throw new Error("type mismatch: type on the wire ".concat(t.name, ", expect type ").concat(this.name));
205
+ throw new Error(`type mismatch: type on the wire ${t.name}, expect type ${this.name}`);
279
206
  }
280
207
  return t;
281
- };
282
- PrimitiveType.prototype._buildTypeTableImpl = function (typeTable) {
208
+ }
209
+ _buildTypeTableImpl(typeTable) {
283
210
  // No type table encoding for Primitive types.
284
211
  return;
285
- };
286
- return PrimitiveType;
287
- }(Type));
288
- exports.PrimitiveType = PrimitiveType;
289
- var ConstructType = /** @class */ (function (_super) {
290
- __extends(ConstructType, _super);
291
- function ConstructType() {
292
- return _super !== null && _super.apply(this, arguments) || this;
293
212
  }
294
- ConstructType.prototype.checkType = function (t) {
213
+ }
214
+ exports.PrimitiveType = PrimitiveType;
215
+ class ConstructType extends Type {
216
+ checkType(t) {
295
217
  if (t instanceof RecClass) {
296
- var ty = t.getType();
218
+ const ty = t.getType();
297
219
  if (typeof ty === 'undefined') {
298
220
  throw new Error('type mismatch with uninitialized type');
299
221
  }
300
222
  return ty;
301
223
  }
302
- throw new Error("type mismatch: type on the wire ".concat(t.name, ", expect type ").concat(this.name));
303
- };
304
- ConstructType.prototype.encodeType = function (typeTable) {
224
+ throw new Error(`type mismatch: type on the wire ${t.name}, expect type ${this.name}`);
225
+ }
226
+ encodeType(typeTable) {
305
227
  return typeTable.indexOf(this.name);
306
- };
307
- return ConstructType;
308
- }(Type));
228
+ }
229
+ }
309
230
  exports.ConstructType = ConstructType;
310
231
  /**
311
232
  * Represents an IDL Empty, a type which has no inhabitants.
312
233
  * Since no values exist for this type, it cannot be serialised or deserialised.
313
234
  * Result types like `Result<Text, Empty>` should always succeed.
314
235
  */
315
- var EmptyClass = /** @class */ (function (_super) {
316
- __extends(EmptyClass, _super);
317
- function EmptyClass() {
318
- return _super !== null && _super.apply(this, arguments) || this;
319
- }
320
- EmptyClass.prototype.accept = function (v, d) {
236
+ class EmptyClass extends PrimitiveType {
237
+ accept(v, d) {
321
238
  return v.visitEmpty(this, d);
322
- };
323
- EmptyClass.prototype.covariant = function (x) {
324
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
325
- };
326
- EmptyClass.prototype.encodeValue = function () {
239
+ }
240
+ covariant(x) {
241
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
242
+ }
243
+ encodeValue() {
327
244
  throw new Error('Empty cannot appear as a function argument');
328
- };
329
- EmptyClass.prototype.valueToString = function () {
245
+ }
246
+ valueToString() {
330
247
  throw new Error('Empty cannot appear as a value');
331
- };
332
- EmptyClass.prototype.encodeType = function () {
248
+ }
249
+ encodeType() {
333
250
  return (0, leb128_1.slebEncode)(-17 /* IDLTypeIds.Empty */);
334
- };
335
- EmptyClass.prototype.decodeValue = function () {
251
+ }
252
+ decodeValue() {
336
253
  throw new Error('Empty cannot appear as an output');
337
- };
338
- Object.defineProperty(EmptyClass.prototype, "name", {
339
- get: function () {
340
- return 'empty';
341
- },
342
- enumerable: false,
343
- configurable: true
344
- });
345
- return EmptyClass;
346
- }(PrimitiveType));
254
+ }
255
+ get name() {
256
+ return 'empty';
257
+ }
258
+ }
347
259
  exports.EmptyClass = EmptyClass;
348
260
  /**
349
261
  * Represents an IDL Unknown, a placeholder type for deserialization only.
@@ -352,43 +264,39 @@ exports.EmptyClass = EmptyClass;
352
264
  * A deserialized unknown will offer it's actual type by calling the `type()` function.
353
265
  * Unknown cannot be serialized and attempting to do so will throw an error.
354
266
  */
355
- var UnknownClass = /** @class */ (function (_super) {
356
- __extends(UnknownClass, _super);
357
- function UnknownClass() {
358
- return _super !== null && _super.apply(this, arguments) || this;
359
- }
360
- UnknownClass.prototype.checkType = function (t) {
267
+ class UnknownClass extends Type {
268
+ checkType(t) {
361
269
  throw new Error('Method not implemented for unknown.');
362
- };
363
- UnknownClass.prototype.accept = function (v, d) {
270
+ }
271
+ accept(v, d) {
364
272
  throw v.visitType(this, d);
365
- };
366
- UnknownClass.prototype.covariant = function (x) {
367
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
368
- };
369
- UnknownClass.prototype.encodeValue = function () {
273
+ }
274
+ covariant(x) {
275
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
276
+ }
277
+ encodeValue() {
370
278
  throw new Error('Unknown cannot appear as a function argument');
371
- };
372
- UnknownClass.prototype.valueToString = function () {
279
+ }
280
+ valueToString() {
373
281
  throw new Error('Unknown cannot appear as a value');
374
- };
375
- UnknownClass.prototype.encodeType = function () {
282
+ }
283
+ encodeType() {
376
284
  throw new Error('Unknown cannot be serialized');
377
- };
378
- UnknownClass.prototype.decodeValue = function (b, t) {
379
- var decodedValue = t.decodeValue(b, t);
285
+ }
286
+ decodeValue(b, t) {
287
+ let decodedValue = t.decodeValue(b, t);
380
288
  if (Object(decodedValue) !== decodedValue) {
381
289
  // decodedValue is primitive. Box it, otherwise we cannot add the type() function.
382
290
  // The type() function is important for primitives because otherwise we cannot tell apart the
383
291
  // different number types.
384
292
  decodedValue = Object(decodedValue);
385
293
  }
386
- var typeFunc;
294
+ let typeFunc;
387
295
  if (t instanceof RecClass) {
388
- typeFunc = function () { return t.getType(); };
296
+ typeFunc = () => t.getType();
389
297
  }
390
298
  else {
391
- typeFunc = function () { return t; };
299
+ typeFunc = () => t;
392
300
  }
393
301
  // Do not use 'decodedValue.type = typeFunc' because this would lead to an enumerable property
394
302
  // 'type' which means it would be serialized if the value would be candid encoded again.
@@ -401,43 +309,34 @@ var UnknownClass = /** @class */ (function (_super) {
401
309
  configurable: true
402
310
  });
403
311
  return decodedValue;
404
- };
405
- UnknownClass.prototype._buildTypeTableImpl = function () {
312
+ }
313
+ _buildTypeTableImpl() {
406
314
  throw new Error('Unknown cannot be serialized');
407
- };
408
- Object.defineProperty(UnknownClass.prototype, "name", {
409
- get: function () {
410
- return 'Unknown';
411
- },
412
- enumerable: false,
413
- configurable: true
414
- });
415
- return UnknownClass;
416
- }(Type));
315
+ }
316
+ get name() {
317
+ return 'Unknown';
318
+ }
319
+ }
417
320
  exports.UnknownClass = UnknownClass;
418
321
  /**
419
322
  * Represents an IDL Bool
420
323
  */
421
- var BoolClass = /** @class */ (function (_super) {
422
- __extends(BoolClass, _super);
423
- function BoolClass() {
424
- return _super !== null && _super.apply(this, arguments) || this;
425
- }
426
- BoolClass.prototype.accept = function (v, d) {
324
+ class BoolClass extends PrimitiveType {
325
+ accept(v, d) {
427
326
  return v.visitBool(this, d);
428
- };
429
- BoolClass.prototype.covariant = function (x) {
327
+ }
328
+ covariant(x) {
430
329
  if (typeof x === 'boolean')
431
330
  return true;
432
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
433
- };
434
- BoolClass.prototype.encodeValue = function (x) {
331
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
332
+ }
333
+ encodeValue(x) {
435
334
  return new Uint8Array([x ? 1 : 0]);
436
- };
437
- BoolClass.prototype.encodeType = function () {
335
+ }
336
+ encodeType() {
438
337
  return (0, leb128_1.slebEncode)(-2 /* IDLTypeIds.Bool */);
439
- };
440
- BoolClass.prototype.decodeValue = function (b, t) {
338
+ }
339
+ decodeValue(b, t) {
441
340
  this.checkType(t);
442
341
  switch ((0, leb128_1.safeReadUint8)(b)) {
443
342
  case 0:
@@ -447,239 +346,187 @@ var BoolClass = /** @class */ (function (_super) {
447
346
  default:
448
347
  throw new Error('Boolean value out of range');
449
348
  }
450
- };
451
- Object.defineProperty(BoolClass.prototype, "name", {
452
- get: function () {
453
- return 'bool';
454
- },
455
- enumerable: false,
456
- configurable: true
457
- });
458
- return BoolClass;
459
- }(PrimitiveType));
349
+ }
350
+ get name() {
351
+ return 'bool';
352
+ }
353
+ }
460
354
  exports.BoolClass = BoolClass;
461
355
  /**
462
356
  * Represents an IDL Null
463
357
  */
464
- var NullClass = /** @class */ (function (_super) {
465
- __extends(NullClass, _super);
466
- function NullClass() {
467
- return _super !== null && _super.apply(this, arguments) || this;
468
- }
469
- NullClass.prototype.accept = function (v, d) {
358
+ class NullClass extends PrimitiveType {
359
+ accept(v, d) {
470
360
  return v.visitNull(this, d);
471
- };
472
- NullClass.prototype.covariant = function (x) {
361
+ }
362
+ covariant(x) {
473
363
  if (x === null)
474
364
  return true;
475
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
476
- };
477
- NullClass.prototype.encodeValue = function () {
365
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
366
+ }
367
+ encodeValue() {
478
368
  return new ArrayBuffer(0);
479
- };
480
- NullClass.prototype.encodeType = function () {
369
+ }
370
+ encodeType() {
481
371
  return (0, leb128_1.slebEncode)(-1 /* IDLTypeIds.Null */);
482
- };
483
- NullClass.prototype.decodeValue = function (b, t) {
372
+ }
373
+ decodeValue(b, t) {
484
374
  this.checkType(t);
485
375
  return null;
486
- };
487
- Object.defineProperty(NullClass.prototype, "name", {
488
- get: function () {
489
- return 'null';
490
- },
491
- enumerable: false,
492
- configurable: true
493
- });
494
- return NullClass;
495
- }(PrimitiveType));
376
+ }
377
+ get name() {
378
+ return 'null';
379
+ }
380
+ }
496
381
  exports.NullClass = NullClass;
497
382
  /**
498
383
  * Represents an IDL Reserved
499
384
  */
500
- var ReservedClass = /** @class */ (function (_super) {
501
- __extends(ReservedClass, _super);
502
- function ReservedClass() {
503
- return _super !== null && _super.apply(this, arguments) || this;
504
- }
505
- ReservedClass.prototype.accept = function (v, d) {
385
+ class ReservedClass extends PrimitiveType {
386
+ accept(v, d) {
506
387
  return v.visitReserved(this, d);
507
- };
508
- ReservedClass.prototype.covariant = function (x) {
388
+ }
389
+ covariant(x) {
509
390
  return true;
510
- };
511
- ReservedClass.prototype.encodeValue = function () {
391
+ }
392
+ encodeValue() {
512
393
  return new ArrayBuffer(0);
513
- };
514
- ReservedClass.prototype.encodeType = function () {
394
+ }
395
+ encodeType() {
515
396
  return (0, leb128_1.slebEncode)(-16 /* IDLTypeIds.Reserved */);
516
- };
517
- ReservedClass.prototype.decodeValue = function (b, t) {
397
+ }
398
+ decodeValue(b, t) {
518
399
  if (t.name !== this.name) {
519
400
  t.decodeValue(b, t);
520
401
  }
521
402
  return null;
522
- };
523
- Object.defineProperty(ReservedClass.prototype, "name", {
524
- get: function () {
525
- return 'reserved';
526
- },
527
- enumerable: false,
528
- configurable: true
529
- });
530
- return ReservedClass;
531
- }(PrimitiveType));
403
+ }
404
+ get name() {
405
+ return 'reserved';
406
+ }
407
+ }
532
408
  exports.ReservedClass = ReservedClass;
533
409
  /**
534
410
  * Represents an IDL Text
535
411
  */
536
- var TextClass = /** @class */ (function (_super) {
537
- __extends(TextClass, _super);
538
- function TextClass() {
539
- return _super !== null && _super.apply(this, arguments) || this;
540
- }
541
- TextClass.prototype.accept = function (v, d) {
412
+ class TextClass extends PrimitiveType {
413
+ accept(v, d) {
542
414
  return v.visitText(this, d);
543
- };
544
- TextClass.prototype.covariant = function (x) {
415
+ }
416
+ covariant(x) {
545
417
  if (typeof x === 'string')
546
418
  return true;
547
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
548
- };
549
- TextClass.prototype.encodeValue = function (x) {
550
- var buf = new TextEncoder().encode(x);
551
- var len = (0, leb128_1.lebEncode)(buf.byteLength);
419
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
420
+ }
421
+ encodeValue(x) {
422
+ const buf = new TextEncoder().encode(x);
423
+ const len = (0, leb128_1.lebEncode)(buf.byteLength);
552
424
  return (0, buffer_1.concat)(len, buf);
553
- };
554
- TextClass.prototype.encodeType = function () {
425
+ }
426
+ encodeType() {
555
427
  return (0, leb128_1.slebEncode)(-15 /* IDLTypeIds.Text */);
556
- };
557
- TextClass.prototype.decodeValue = function (b, t) {
428
+ }
429
+ decodeValue(b, t) {
558
430
  this.checkType(t);
559
- var len = (0, leb128_1.lebDecode)(b);
560
- var buf = (0, leb128_1.safeRead)(b, Number(len));
561
- var decoder = new TextDecoder('utf8', { fatal: true });
431
+ const len = (0, leb128_1.lebDecode)(b);
432
+ const buf = (0, leb128_1.safeRead)(b, Number(len));
433
+ const decoder = new TextDecoder('utf8', { fatal: true });
562
434
  return decoder.decode(buf);
563
- };
564
- Object.defineProperty(TextClass.prototype, "name", {
565
- get: function () {
566
- return 'text';
567
- },
568
- enumerable: false,
569
- configurable: true
570
- });
571
- TextClass.prototype.valueToString = function (x) {
435
+ }
436
+ get name() {
437
+ return 'text';
438
+ }
439
+ valueToString(x) {
572
440
  return '"' + x + '"';
573
- };
574
- return TextClass;
575
- }(PrimitiveType));
441
+ }
442
+ }
576
443
  exports.TextClass = TextClass;
577
444
  /**
578
445
  * Represents an IDL Int
579
446
  */
580
- var IntClass = /** @class */ (function (_super) {
581
- __extends(IntClass, _super);
582
- function IntClass() {
583
- return _super !== null && _super.apply(this, arguments) || this;
584
- }
585
- IntClass.prototype.accept = function (v, d) {
447
+ class IntClass extends PrimitiveType {
448
+ accept(v, d) {
586
449
  return v.visitInt(this, d);
587
- };
588
- IntClass.prototype.covariant = function (x) {
450
+ }
451
+ covariant(x) {
589
452
  // We allow encoding of JavaScript plain numbers.
590
453
  // But we will always decode to bigint.
591
454
  if (typeof x === 'bigint' || Number.isInteger(x))
592
455
  return true;
593
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
594
- };
595
- IntClass.prototype.encodeValue = function (x) {
456
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
457
+ }
458
+ encodeValue(x) {
596
459
  return (0, leb128_1.slebEncode)(x);
597
- };
598
- IntClass.prototype.encodeType = function () {
460
+ }
461
+ encodeType() {
599
462
  return (0, leb128_1.slebEncode)(-4 /* IDLTypeIds.Int */);
600
- };
601
- IntClass.prototype.decodeValue = function (b, t) {
463
+ }
464
+ decodeValue(b, t) {
602
465
  this.checkType(t);
603
466
  return (0, leb128_1.slebDecode)(b);
604
- };
605
- Object.defineProperty(IntClass.prototype, "name", {
606
- get: function () {
607
- return 'int';
608
- },
609
- enumerable: false,
610
- configurable: true
611
- });
612
- IntClass.prototype.valueToString = function (x) {
467
+ }
468
+ get name() {
469
+ return 'int';
470
+ }
471
+ valueToString(x) {
613
472
  return x.toString();
614
- };
615
- return IntClass;
616
- }(PrimitiveType));
473
+ }
474
+ }
617
475
  exports.IntClass = IntClass;
618
476
  /**
619
477
  * Represents an IDL Nat
620
478
  */
621
- var NatClass = /** @class */ (function (_super) {
622
- __extends(NatClass, _super);
623
- function NatClass() {
624
- return _super !== null && _super.apply(this, arguments) || this;
625
- }
626
- NatClass.prototype.accept = function (v, d) {
479
+ class NatClass extends PrimitiveType {
480
+ accept(v, d) {
627
481
  return v.visitNat(this, d);
628
- };
629
- NatClass.prototype.covariant = function (x) {
482
+ }
483
+ covariant(x) {
630
484
  // We allow encoding of JavaScript plain numbers.
631
485
  // But we will always decode to bigint.
632
486
  if ((typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0))
633
487
  return true;
634
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
635
- };
636
- NatClass.prototype.encodeValue = function (x) {
488
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
489
+ }
490
+ encodeValue(x) {
637
491
  return (0, leb128_1.lebEncode)(x);
638
- };
639
- NatClass.prototype.encodeType = function () {
492
+ }
493
+ encodeType() {
640
494
  return (0, leb128_1.slebEncode)(-3 /* IDLTypeIds.Nat */);
641
- };
642
- NatClass.prototype.decodeValue = function (b, t) {
495
+ }
496
+ decodeValue(b, t) {
643
497
  this.checkType(t);
644
498
  return (0, leb128_1.lebDecode)(b);
645
- };
646
- Object.defineProperty(NatClass.prototype, "name", {
647
- get: function () {
648
- return 'nat';
649
- },
650
- enumerable: false,
651
- configurable: true
652
- });
653
- NatClass.prototype.valueToString = function (x) {
499
+ }
500
+ get name() {
501
+ return 'nat';
502
+ }
503
+ valueToString(x) {
654
504
  return x.toString();
655
- };
656
- return NatClass;
657
- }(PrimitiveType));
505
+ }
506
+ }
658
507
  exports.NatClass = NatClass;
659
508
  /**
660
509
  * Represents an IDL Float
661
510
  */
662
- var FloatClass = /** @class */ (function (_super) {
663
- __extends(FloatClass, _super);
664
- function FloatClass(_bits) {
665
- var _this = _super.call(this) || this;
666
- _this._bits = _bits;
511
+ class FloatClass extends PrimitiveType {
512
+ constructor(_bits) {
513
+ super();
514
+ this._bits = _bits;
667
515
  if (_bits !== 32 && _bits !== 64) {
668
516
  throw new Error('not a valid float type');
669
517
  }
670
- return _this;
671
518
  }
672
- FloatClass.prototype.accept = function (v, d) {
519
+ accept(v, d) {
673
520
  return v.visitFloat(this, d);
674
- };
675
- FloatClass.prototype.covariant = function (x) {
521
+ }
522
+ covariant(x) {
676
523
  if (typeof x === 'number' || x instanceof Number)
677
524
  return true;
678
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
679
- };
680
- FloatClass.prototype.encodeValue = function (x) {
681
- var buf = new ArrayBuffer(this._bits / 8);
682
- var view = new DataView(buf);
525
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
526
+ }
527
+ encodeValue(x) {
528
+ const buf = new ArrayBuffer(this._bits / 8);
529
+ const view = new DataView(buf);
683
530
  if (this._bits === 32) {
684
531
  view.setFloat32(0, x, true);
685
532
  }
@@ -687,57 +534,50 @@ var FloatClass = /** @class */ (function (_super) {
687
534
  view.setFloat64(0, x, true);
688
535
  }
689
536
  return buf;
690
- };
691
- FloatClass.prototype.encodeType = function () {
692
- var opcode = this._bits === 32 ? -13 /* IDLTypeIds.Float32 */ : -14 /* IDLTypeIds.Float64 */;
537
+ }
538
+ encodeType() {
539
+ const opcode = this._bits === 32 ? -13 /* IDLTypeIds.Float32 */ : -14 /* IDLTypeIds.Float64 */;
693
540
  return (0, leb128_1.slebEncode)(opcode);
694
- };
695
- FloatClass.prototype.decodeValue = function (b, t) {
541
+ }
542
+ decodeValue(b, t) {
696
543
  this.checkType(t);
697
- var bytes = (0, leb128_1.safeRead)(b, this._bits / 8);
698
- var view = new DataView(bytes);
544
+ const bytes = (0, leb128_1.safeRead)(b, this._bits / 8);
545
+ const view = new DataView(bytes);
699
546
  if (this._bits === 32) {
700
547
  return view.getFloat32(0, true);
701
548
  }
702
549
  else {
703
550
  return view.getFloat64(0, true);
704
551
  }
705
- };
706
- Object.defineProperty(FloatClass.prototype, "name", {
707
- get: function () {
708
- return 'float' + this._bits;
709
- },
710
- enumerable: false,
711
- configurable: true
712
- });
713
- FloatClass.prototype.valueToString = function (x) {
552
+ }
553
+ get name() {
554
+ return 'float' + this._bits;
555
+ }
556
+ valueToString(x) {
714
557
  return x.toString();
715
- };
716
- return FloatClass;
717
- }(PrimitiveType));
558
+ }
559
+ }
718
560
  exports.FloatClass = FloatClass;
719
561
  /**
720
562
  * Represents an IDL fixed-width Int(n)
721
563
  */
722
- var FixedIntClass = /** @class */ (function (_super) {
723
- __extends(FixedIntClass, _super);
724
- function FixedIntClass(_bits) {
725
- var _this = _super.call(this) || this;
726
- _this._bits = _bits;
727
- return _this;
728
- }
729
- FixedIntClass.prototype.accept = function (v, d) {
564
+ class FixedIntClass extends PrimitiveType {
565
+ constructor(_bits) {
566
+ super();
567
+ this._bits = _bits;
568
+ }
569
+ accept(v, d) {
730
570
  return v.visitFixedInt(this, d);
731
- };
732
- FixedIntClass.prototype.covariant = function (x) {
733
- var min = iexp2(this._bits - 1) * BigInt(-1);
734
- var max = iexp2(this._bits - 1) - BigInt(1);
735
- var ok = false;
571
+ }
572
+ covariant(x) {
573
+ const min = iexp2(this._bits - 1) * BigInt(-1);
574
+ const max = iexp2(this._bits - 1) - BigInt(1);
575
+ let ok = false;
736
576
  if (typeof x === 'bigint') {
737
577
  ok = x >= min && x <= max;
738
578
  }
739
579
  else if (Number.isInteger(x)) {
740
- var v = BigInt(x);
580
+ const v = BigInt(x);
741
581
  ok = v >= min && v <= max;
742
582
  }
743
583
  else {
@@ -745,59 +585,52 @@ var FixedIntClass = /** @class */ (function (_super) {
745
585
  }
746
586
  if (ok)
747
587
  return true;
748
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
749
- };
750
- FixedIntClass.prototype.encodeValue = function (x) {
588
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
589
+ }
590
+ encodeValue(x) {
751
591
  return (0, leb128_1.writeIntLE)(x, this._bits / 8);
752
- };
753
- FixedIntClass.prototype.encodeType = function () {
754
- var offset = Math.log2(this._bits) - 3;
592
+ }
593
+ encodeType() {
594
+ const offset = Math.log2(this._bits) - 3;
755
595
  return (0, leb128_1.slebEncode)(-9 - offset);
756
- };
757
- FixedIntClass.prototype.decodeValue = function (b, t) {
596
+ }
597
+ decodeValue(b, t) {
758
598
  this.checkType(t);
759
- var num = (0, leb128_1.readIntLE)(b, this._bits / 8);
599
+ const num = (0, leb128_1.readIntLE)(b, this._bits / 8);
760
600
  if (this._bits <= 32) {
761
601
  return Number(num);
762
602
  }
763
603
  else {
764
604
  return num;
765
605
  }
766
- };
767
- Object.defineProperty(FixedIntClass.prototype, "name", {
768
- get: function () {
769
- return "int".concat(this._bits);
770
- },
771
- enumerable: false,
772
- configurable: true
773
- });
774
- FixedIntClass.prototype.valueToString = function (x) {
606
+ }
607
+ get name() {
608
+ return `int${this._bits}`;
609
+ }
610
+ valueToString(x) {
775
611
  return x.toString();
776
- };
777
- return FixedIntClass;
778
- }(PrimitiveType));
612
+ }
613
+ }
779
614
  exports.FixedIntClass = FixedIntClass;
780
615
  /**
781
616
  * Represents an IDL fixed-width Nat(n)
782
617
  */
783
- var FixedNatClass = /** @class */ (function (_super) {
784
- __extends(FixedNatClass, _super);
785
- function FixedNatClass(_bits) {
786
- var _this = _super.call(this) || this;
787
- _this._bits = _bits;
788
- return _this;
789
- }
790
- FixedNatClass.prototype.accept = function (v, d) {
618
+ class FixedNatClass extends PrimitiveType {
619
+ constructor(_bits) {
620
+ super();
621
+ this._bits = _bits;
622
+ }
623
+ accept(v, d) {
791
624
  return v.visitFixedNat(this, d);
792
- };
793
- FixedNatClass.prototype.covariant = function (x) {
794
- var max = iexp2(this._bits);
795
- var ok = false;
625
+ }
626
+ covariant(x) {
627
+ const max = iexp2(this._bits);
628
+ let ok = false;
796
629
  if (typeof x === 'bigint' && x >= BigInt(0)) {
797
630
  ok = x < max;
798
631
  }
799
632
  else if (Number.isInteger(x) && x >= 0) {
800
- var v = BigInt(x);
633
+ const v = BigInt(x);
801
634
  ok = v < max;
802
635
  }
803
636
  else {
@@ -805,37 +638,32 @@ var FixedNatClass = /** @class */ (function (_super) {
805
638
  }
806
639
  if (ok)
807
640
  return true;
808
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
809
- };
810
- FixedNatClass.prototype.encodeValue = function (x) {
641
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
642
+ }
643
+ encodeValue(x) {
811
644
  return (0, leb128_1.writeUIntLE)(x, this._bits / 8);
812
- };
813
- FixedNatClass.prototype.encodeType = function () {
814
- var offset = Math.log2(this._bits) - 3;
645
+ }
646
+ encodeType() {
647
+ const offset = Math.log2(this._bits) - 3;
815
648
  return (0, leb128_1.slebEncode)(-5 - offset);
816
- };
817
- FixedNatClass.prototype.decodeValue = function (b, t) {
649
+ }
650
+ decodeValue(b, t) {
818
651
  this.checkType(t);
819
- var num = (0, leb128_1.readUIntLE)(b, this._bits / 8);
652
+ const num = (0, leb128_1.readUIntLE)(b, this._bits / 8);
820
653
  if (this._bits <= 32) {
821
654
  return Number(num);
822
655
  }
823
656
  else {
824
657
  return num;
825
658
  }
826
- };
827
- Object.defineProperty(FixedNatClass.prototype, "name", {
828
- get: function () {
829
- return "nat".concat(this._bits);
830
- },
831
- enumerable: false,
832
- configurable: true
833
- });
834
- FixedNatClass.prototype.valueToString = function (x) {
659
+ }
660
+ get name() {
661
+ return `nat${this._bits}`;
662
+ }
663
+ valueToString(x) {
835
664
  return x.toString();
836
- };
837
- return FixedNatClass;
838
- }(PrimitiveType));
665
+ }
666
+ }
839
667
  exports.FixedNatClass = FixedNatClass;
840
668
  /**
841
669
  * Represents an IDL Array
@@ -845,82 +673,68 @@ exports.FixedNatClass = FixedNatClass;
845
673
  *
846
674
  * @param {Type} t
847
675
  */
848
- var VecClass = /** @class */ (function (_super) {
849
- __extends(VecClass, _super);
850
- function VecClass(_type) {
851
- var _this = _super.call(this) || this;
852
- _this._type = _type;
676
+ class VecClass extends ConstructType {
677
+ constructor(_type) {
678
+ super();
679
+ this._type = _type;
853
680
  // If true, this vector is really a blob and we can just use memcpy.
854
681
  //
855
682
  // NOTE:
856
683
  // With support of encoding/dencoding of TypedArrays, this optimization is
857
684
  // only used when plain array of bytes are passed as encoding input in order
858
685
  // to be backward compatible.
859
- _this._blobOptimization = false;
686
+ this._blobOptimization = false;
860
687
  if (_type instanceof FixedNatClass && _type._bits === 8) {
861
- _this._blobOptimization = true;
688
+ this._blobOptimization = true;
862
689
  }
863
- return _this;
864
690
  }
865
- VecClass.prototype.accept = function (v, d) {
691
+ accept(v, d) {
866
692
  return v.visitVec(this, this._type, d);
867
- };
868
- VecClass.prototype.covariant = function (x) {
869
- var _this = this;
693
+ }
694
+ covariant(x) {
870
695
  // Special case for ArrayBuffer
871
- var bits = this._type instanceof FixedNatClass ? this._type._bits : this._type instanceof FixedIntClass ? this._type._bits : 0;
696
+ const bits = this._type instanceof FixedNatClass ? this._type._bits : this._type instanceof FixedIntClass ? this._type._bits : 0;
872
697
  if ((ArrayBuffer.isView(x) && bits == x.BYTES_PER_ELEMENT * 8) ||
873
698
  (Array.isArray(x) &&
874
- x.every(function (v, idx) {
699
+ x.every((v, idx) => {
875
700
  try {
876
- return _this._type.covariant(v);
701
+ return this._type.covariant(v);
877
702
  }
878
703
  catch (e) {
879
- throw new Error("Invalid ".concat(_this.display(), " argument: \n\nindex ").concat(idx, " -> ").concat(e.message));
704
+ throw new Error(`Invalid ${this.display()} argument: \n\nindex ${idx} -> ${e.message}`);
880
705
  }
881
706
  })))
882
707
  return true;
883
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
884
- };
885
- VecClass.prototype.encodeValue = function (x) {
886
- var e_2, _a;
887
- var len = (0, leb128_1.lebEncode)(x.length);
708
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
709
+ }
710
+ encodeValue(x) {
711
+ const len = (0, leb128_1.lebEncode)(x.length);
888
712
  if (this._blobOptimization) {
889
713
  return (0, buffer_1.concat)(len, new Uint8Array(x));
890
714
  }
891
715
  if (ArrayBuffer.isView(x)) {
892
716
  return (0, buffer_1.concat)(len, new Uint8Array(x.buffer));
893
717
  }
894
- var buf = new buffer_2.PipeArrayBuffer(new ArrayBuffer(len.byteLength + x.length), 0);
718
+ const buf = new buffer_2.PipeArrayBuffer(new ArrayBuffer(len.byteLength + x.length), 0);
895
719
  buf.write(len);
896
- try {
897
- for (var x_1 = __values(x), x_1_1 = x_1.next(); !x_1_1.done; x_1_1 = x_1.next()) {
898
- var d = x_1_1.value;
899
- var encoded = this._type.encodeValue(d);
900
- buf.write(new Uint8Array(encoded));
901
- }
902
- }
903
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
904
- finally {
905
- try {
906
- if (x_1_1 && !x_1_1.done && (_a = x_1.return)) _a.call(x_1);
907
- }
908
- finally { if (e_2) throw e_2.error; }
720
+ for (const d of x) {
721
+ const encoded = this._type.encodeValue(d);
722
+ buf.write(new Uint8Array(encoded));
909
723
  }
910
724
  return buf.buffer;
911
- };
912
- VecClass.prototype._buildTypeTableImpl = function (typeTable) {
725
+ }
726
+ _buildTypeTableImpl(typeTable) {
913
727
  this._type.buildTypeTable(typeTable);
914
- var opCode = (0, leb128_1.slebEncode)(-19 /* IDLTypeIds.Vector */);
915
- var buffer = this._type.encodeType(typeTable);
728
+ const opCode = (0, leb128_1.slebEncode)(-19 /* IDLTypeIds.Vector */);
729
+ const buffer = this._type.encodeType(typeTable);
916
730
  typeTable.add(this, (0, buffer_1.concat)(opCode, buffer));
917
- };
918
- VecClass.prototype.decodeValue = function (b, t) {
919
- var vec = this.checkType(t);
731
+ }
732
+ decodeValue(b, t) {
733
+ const vec = this.checkType(t);
920
734
  if (!(vec instanceof VecClass)) {
921
735
  throw new Error('Not a vector type');
922
736
  }
923
- var len = Number((0, leb128_1.lebDecode)(b));
737
+ const len = Number((0, leb128_1.lebDecode)(b));
924
738
  if (this._type instanceof FixedNatClass) {
925
739
  if (this._type._bits == 8) {
926
740
  return new Uint8Array(b.read(len));
@@ -949,70 +763,62 @@ var VecClass = /** @class */ (function (_super) {
949
763
  return new BigInt64Array(b.read(len * 8));
950
764
  }
951
765
  }
952
- var rets = [];
953
- for (var i = 0; i < len; i++) {
766
+ const rets = [];
767
+ for (let i = 0; i < len; i++) {
954
768
  rets.push(this._type.decodeValue(b, vec._type));
955
769
  }
956
770
  return rets;
957
- };
958
- Object.defineProperty(VecClass.prototype, "name", {
959
- get: function () {
960
- return "vec ".concat(this._type.name);
961
- },
962
- enumerable: false,
963
- configurable: true
964
- });
965
- VecClass.prototype.display = function () {
966
- return "vec ".concat(this._type.display());
967
- };
968
- VecClass.prototype.valueToString = function (x) {
969
- var _this = this;
970
- var elements = x.map(function (e) { return _this._type.valueToString(e); });
771
+ }
772
+ get name() {
773
+ return `vec ${this._type.name}`;
774
+ }
775
+ display() {
776
+ return `vec ${this._type.display()}`;
777
+ }
778
+ valueToString(x) {
779
+ const elements = x.map((e) => this._type.valueToString(e));
971
780
  return 'vec {' + elements.join('; ') + '}';
972
- };
973
- return VecClass;
974
- }(ConstructType));
781
+ }
782
+ }
975
783
  exports.VecClass = VecClass;
976
784
  /**
977
785
  * Represents an IDL Option
978
786
  * @param {Type} t
979
787
  */
980
- var OptClass = /** @class */ (function (_super) {
981
- __extends(OptClass, _super);
982
- function OptClass(_type) {
983
- var _this = _super.call(this) || this;
984
- _this._type = _type;
985
- return _this;
986
- }
987
- OptClass.prototype.accept = function (v, d) {
788
+ class OptClass extends ConstructType {
789
+ constructor(_type) {
790
+ super();
791
+ this._type = _type;
792
+ }
793
+ accept(v, d) {
988
794
  return v.visitOpt(this, this._type, d);
989
- };
990
- OptClass.prototype.covariant = function (x) {
795
+ }
796
+ covariant(x) {
991
797
  try {
992
798
  if (Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0]))))
993
799
  return true;
994
800
  }
995
801
  catch (e) {
996
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x), " \n\n-> ").concat(e.message));
802
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)} \n\n-> ${e.message}`);
997
803
  }
998
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
999
- };
1000
- OptClass.prototype.encodeValue = function (x) {
804
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
805
+ }
806
+ encodeValue(x) {
1001
807
  if (x.length === 0) {
1002
808
  return new Uint8Array([0]);
1003
809
  }
1004
810
  else {
1005
811
  return (0, buffer_1.concat)(new Uint8Array([1]), this._type.encodeValue(x[0]));
1006
812
  }
1007
- };
1008
- OptClass.prototype._buildTypeTableImpl = function (typeTable) {
813
+ }
814
+ _buildTypeTableImpl(typeTable) {
1009
815
  this._type.buildTypeTable(typeTable);
1010
- var opCode = (0, leb128_1.slebEncode)(-18 /* IDLTypeIds.Opt */);
1011
- var buffer = this._type.encodeType(typeTable);
816
+ const opCode = (0, leb128_1.slebEncode)(-18 /* IDLTypeIds.Opt */);
817
+ const buffer = this._type.encodeType(typeTable);
1012
818
  typeTable.add(this, (0, buffer_1.concat)(opCode, buffer));
1013
- };
1014
- OptClass.prototype.decodeValue = function (b, t) {
1015
- var opt = this.checkType(t);
819
+ }
820
+ decodeValue(b, t) {
821
+ const opt = this.checkType(t);
1016
822
  if (!(opt instanceof OptClass)) {
1017
823
  throw new Error('Not an option type');
1018
824
  }
@@ -1024,117 +830,94 @@ var OptClass = /** @class */ (function (_super) {
1024
830
  default:
1025
831
  throw new Error('Not an option value');
1026
832
  }
1027
- };
1028
- Object.defineProperty(OptClass.prototype, "name", {
1029
- get: function () {
1030
- return "opt ".concat(this._type.name);
1031
- },
1032
- enumerable: false,
1033
- configurable: true
1034
- });
1035
- OptClass.prototype.display = function () {
1036
- return "opt ".concat(this._type.display());
1037
- };
1038
- OptClass.prototype.valueToString = function (x) {
833
+ }
834
+ get name() {
835
+ return `opt ${this._type.name}`;
836
+ }
837
+ display() {
838
+ return `opt ${this._type.display()}`;
839
+ }
840
+ valueToString(x) {
1039
841
  if (x.length === 0) {
1040
842
  return 'null';
1041
843
  }
1042
844
  else {
1043
- return "opt ".concat(this._type.valueToString(x[0]));
845
+ return `opt ${this._type.valueToString(x[0])}`;
1044
846
  }
1045
- };
1046
- return OptClass;
1047
- }(ConstructType));
847
+ }
848
+ }
1048
849
  exports.OptClass = OptClass;
1049
850
  /**
1050
851
  * Represents an IDL Record
1051
852
  * @param {Object} [fields] - mapping of function name to Type
1052
853
  */
1053
- var RecordClass = /** @class */ (function (_super) {
1054
- __extends(RecordClass, _super);
1055
- function RecordClass(fields) {
1056
- if (fields === void 0) { fields = {}; }
1057
- var _this = _super.call(this) || this;
1058
- _this._fields = Object.entries(fields).sort(function (a, b) { return idlLabelToId(a[0]) - idlLabelToId(b[0]); });
1059
- return _this;
1060
- }
1061
- RecordClass.prototype.accept = function (v, d) {
854
+ class RecordClass extends ConstructType {
855
+ constructor(fields = {}) {
856
+ super();
857
+ this._fields = Object.entries(fields).sort((a, b) => idlLabelToId(a[0]) - idlLabelToId(b[0]));
858
+ }
859
+ accept(v, d) {
1062
860
  return v.visitRecord(this, this._fields, d);
1063
- };
1064
- RecordClass.prototype.tryAsTuple = function () {
1065
- var res = [];
1066
- for (var i = 0; i < this._fields.length; i++) {
1067
- var _a = __read(this._fields[i], 2), key = _a[0], type = _a[1];
1068
- if (key !== "_".concat(i, "_")) {
861
+ }
862
+ tryAsTuple() {
863
+ const res = [];
864
+ for (let i = 0; i < this._fields.length; i++) {
865
+ const [key, type] = this._fields[i];
866
+ if (key !== `_${i}_`) {
1069
867
  return null;
1070
868
  }
1071
869
  res.push(type);
1072
870
  }
1073
871
  return res;
1074
- };
1075
- RecordClass.prototype.covariant = function (x) {
1076
- var _this = this;
872
+ }
873
+ covariant(x) {
1077
874
  if (typeof x === 'object' &&
1078
- this._fields.every(function (_a) {
1079
- var _b = __read(_a, 2), k = _b[0], t = _b[1];
875
+ this._fields.every(([k, t]) => {
1080
876
  // eslint-disable-next-line
1081
877
  if (!x.hasOwnProperty(k)) {
1082
- throw new Error("Record is missing key \"".concat(k, "\"."));
878
+ throw new Error(`Record is missing key "${k}".`);
1083
879
  }
1084
880
  try {
1085
881
  return t.covariant(x[k]);
1086
882
  }
1087
883
  catch (e) {
1088
- throw new Error("Invalid ".concat(_this.display(), " argument: \n\nfield ").concat(k, " -> ").concat(e.message));
884
+ throw new Error(`Invalid ${this.display()} argument: \n\nfield ${k} -> ${e.message}`);
1089
885
  }
1090
886
  }))
1091
887
  return true;
1092
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
1093
- };
1094
- RecordClass.prototype.encodeValue = function (x) {
1095
- var values = this._fields.map(function (_a) {
1096
- var _b = __read(_a, 1), key = _b[0];
1097
- return x[key];
1098
- });
1099
- var bufs = zipWith(this._fields, values, function (_a, d) {
1100
- var _b = __read(_a, 2), c = _b[1];
1101
- return c.encodeValue(d);
1102
- });
1103
- return buffer_1.concat.apply(void 0, __spreadArray([], __read(bufs), false));
1104
- };
1105
- RecordClass.prototype._buildTypeTableImpl = function (T) {
1106
- this._fields.forEach(function (_a) {
1107
- var _b = __read(_a, 2), _ = _b[0], value = _b[1];
1108
- return value.buildTypeTable(T);
1109
- });
1110
- var opCode = (0, leb128_1.slebEncode)(-20 /* IDLTypeIds.Record */);
1111
- var len = (0, leb128_1.lebEncode)(this._fields.length);
1112
- var fields = this._fields.map(function (_a) {
1113
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
1114
- return (0, buffer_1.concat)((0, leb128_1.lebEncode)(idlLabelToId(key)), value.encodeType(T));
1115
- });
1116
- T.add(this, (0, buffer_1.concat)(opCode, len, buffer_1.concat.apply(void 0, __spreadArray([], __read(fields), false))));
1117
- };
1118
- RecordClass.prototype.decodeValue = function (b, t) {
1119
- var e_3, _a;
1120
- var record = this.checkType(t);
888
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
889
+ }
890
+ encodeValue(x) {
891
+ const values = this._fields.map(([key]) => x[key]);
892
+ const bufs = zipWith(this._fields, values, ([, c], d) => c.encodeValue(d));
893
+ return (0, buffer_1.concat)(...bufs);
894
+ }
895
+ _buildTypeTableImpl(T) {
896
+ this._fields.forEach(([_, value]) => value.buildTypeTable(T));
897
+ const opCode = (0, leb128_1.slebEncode)(-20 /* IDLTypeIds.Record */);
898
+ const len = (0, leb128_1.lebEncode)(this._fields.length);
899
+ const fields = this._fields.map(([key, value]) => (0, buffer_1.concat)((0, leb128_1.lebEncode)(idlLabelToId(key)), value.encodeType(T)));
900
+ T.add(this, (0, buffer_1.concat)(opCode, len, (0, buffer_1.concat)(...fields)));
901
+ }
902
+ decodeValue(b, t) {
903
+ const record = this.checkType(t);
1121
904
  if (!(record instanceof RecordClass)) {
1122
905
  throw new Error('Not a record type');
1123
906
  }
1124
- var x = {};
1125
- var expectedRecordIdx = 0;
1126
- var actualRecordIdx = 0;
907
+ const x = {};
908
+ let expectedRecordIdx = 0;
909
+ let actualRecordIdx = 0;
1127
910
  while (actualRecordIdx < record._fields.length) {
1128
- var _b = __read(record._fields[actualRecordIdx], 2), hash = _b[0], type = _b[1];
911
+ const [hash, type] = record._fields[actualRecordIdx];
1129
912
  if (expectedRecordIdx >= this._fields.length) {
1130
913
  // skip unexpected left over fields present on the wire
1131
914
  type.decodeValue(b, type);
1132
915
  actualRecordIdx++;
1133
916
  continue;
1134
917
  }
1135
- var _c = __read(this._fields[expectedRecordIdx], 2), expectKey = _c[0], expectType = _c[1];
1136
- var expectedId = idlLabelToId(this._fields[expectedRecordIdx][0]);
1137
- var actualId = idlLabelToId(hash);
918
+ const [expectKey, expectType] = this._fields[expectedRecordIdx];
919
+ const expectedId = idlLabelToId(this._fields[expectedRecordIdx][0]);
920
+ const actualId = idlLabelToId(hash);
1138
921
  if (expectedId === actualId) {
1139
922
  // the current field on the wire matches the expected field
1140
923
  x[expectKey] = expectType.decodeValue(b, type);
@@ -1157,392 +940,291 @@ var RecordClass = /** @class */ (function (_super) {
1157
940
  actualRecordIdx++;
1158
941
  }
1159
942
  }
1160
- try {
1161
- // initialize left over expected optional fields
1162
- for (var _d = __values(this._fields.slice(expectedRecordIdx)), _e = _d.next(); !_e.done; _e = _d.next()) {
1163
- var _f = __read(_e.value, 2), expectKey = _f[0], expectType = _f[1];
1164
- if (expectType instanceof OptClass || expectType instanceof ReservedClass) {
1165
- // TODO this assumes null value in opt is represented as []
1166
- x[expectKey] = [];
1167
- }
1168
- else {
1169
- throw new Error('Cannot find required field ' + expectKey);
1170
- }
943
+ // initialize left over expected optional fields
944
+ for (const [expectKey, expectType] of this._fields.slice(expectedRecordIdx)) {
945
+ if (expectType instanceof OptClass || expectType instanceof ReservedClass) {
946
+ // TODO this assumes null value in opt is represented as []
947
+ x[expectKey] = [];
1171
948
  }
1172
- }
1173
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
1174
- finally {
1175
- try {
1176
- if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
949
+ else {
950
+ throw new Error('Cannot find required field ' + expectKey);
1177
951
  }
1178
- finally { if (e_3) throw e_3.error; }
1179
952
  }
1180
953
  return x;
1181
- };
1182
- Object.defineProperty(RecordClass.prototype, "name", {
1183
- get: function () {
1184
- var fields = this._fields.map(function (_a) {
1185
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
1186
- return key + ':' + value.name;
1187
- });
1188
- return "record {".concat(fields.join('; '), "}");
1189
- },
1190
- enumerable: false,
1191
- configurable: true
1192
- });
1193
- RecordClass.prototype.display = function () {
1194
- var fields = this._fields.map(function (_a) {
1195
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
1196
- return key + ':' + value.display();
1197
- });
1198
- return "record {".concat(fields.join('; '), "}");
1199
- };
1200
- RecordClass.prototype.valueToString = function (x) {
1201
- var values = this._fields.map(function (_a) {
1202
- var _b = __read(_a, 1), key = _b[0];
1203
- return x[key];
1204
- });
1205
- var fields = zipWith(this._fields, values, function (_a, d) {
1206
- var _b = __read(_a, 2), k = _b[0], c = _b[1];
1207
- return k + '=' + c.valueToString(d);
1208
- });
1209
- return "record {".concat(fields.join('; '), "}");
1210
- };
1211
- return RecordClass;
1212
- }(ConstructType));
954
+ }
955
+ get name() {
956
+ const fields = this._fields.map(([key, value]) => key + ':' + value.name);
957
+ return `record {${fields.join('; ')}}`;
958
+ }
959
+ display() {
960
+ const fields = this._fields.map(([key, value]) => key + ':' + value.display());
961
+ return `record {${fields.join('; ')}}`;
962
+ }
963
+ valueToString(x) {
964
+ const values = this._fields.map(([key]) => x[key]);
965
+ const fields = zipWith(this._fields, values, ([k, c], d) => k + '=' + c.valueToString(d));
966
+ return `record {${fields.join('; ')}}`;
967
+ }
968
+ }
1213
969
  exports.RecordClass = RecordClass;
1214
970
  /**
1215
971
  * Represents Tuple, a syntactic sugar for Record.
1216
972
  * @param {Type} components
1217
973
  */
1218
- var TupleClass = /** @class */ (function (_super) {
1219
- __extends(TupleClass, _super);
1220
- function TupleClass(_components) {
1221
- var _this = this;
1222
- var x = {};
1223
- _components.forEach(function (e, i) { return (x['_' + i + '_'] = e); });
1224
- _this = _super.call(this, x) || this;
1225
- _this._components = _components;
1226
- return _this;
1227
- }
1228
- TupleClass.prototype.accept = function (v, d) {
974
+ class TupleClass extends RecordClass {
975
+ constructor(_components) {
976
+ const x = {};
977
+ _components.forEach((e, i) => (x['_' + i + '_'] = e));
978
+ super(x);
979
+ this._components = _components;
980
+ }
981
+ accept(v, d) {
1229
982
  return v.visitTuple(this, this._components, d);
1230
- };
1231
- TupleClass.prototype.covariant = function (x) {
983
+ }
984
+ covariant(x) {
1232
985
  // `>=` because tuples can be covariant when encoded.
1233
- var _this = this;
1234
986
  if (Array.isArray(x) &&
1235
987
  x.length >= this._fields.length &&
1236
- this._components.every(function (t, i) {
988
+ this._components.every((t, i) => {
1237
989
  try {
1238
990
  return t.covariant(x[i]);
1239
991
  }
1240
992
  catch (e) {
1241
- throw new Error("Invalid ".concat(_this.display(), " argument: \n\nindex ").concat(i, " -> ").concat(e.message));
993
+ throw new Error(`Invalid ${this.display()} argument: \n\nindex ${i} -> ${e.message}`);
1242
994
  }
1243
995
  }))
1244
996
  return true;
1245
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
1246
- };
1247
- TupleClass.prototype.encodeValue = function (x) {
1248
- var bufs = zipWith(this._components, x, function (c, d) { return c.encodeValue(d); });
1249
- return buffer_1.concat.apply(void 0, __spreadArray([], __read(bufs), false));
1250
- };
1251
- TupleClass.prototype.decodeValue = function (b, t) {
1252
- var e_4, _a;
1253
- var tuple = this.checkType(t);
997
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
998
+ }
999
+ encodeValue(x) {
1000
+ const bufs = zipWith(this._components, x, (c, d) => c.encodeValue(d));
1001
+ return (0, buffer_1.concat)(...bufs);
1002
+ }
1003
+ decodeValue(b, t) {
1004
+ const tuple = this.checkType(t);
1254
1005
  if (!(tuple instanceof TupleClass)) {
1255
1006
  throw new Error('not a tuple type');
1256
1007
  }
1257
1008
  if (tuple._components.length < this._components.length) {
1258
1009
  throw new Error('tuple mismatch');
1259
1010
  }
1260
- var res = [];
1261
- try {
1262
- for (var _b = __values(tuple._components.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
1263
- var _d = __read(_c.value, 2), i = _d[0], wireType = _d[1];
1264
- if (i >= this._components.length) {
1265
- // skip value
1266
- wireType.decodeValue(b, wireType);
1267
- }
1268
- else {
1269
- res.push(this._components[i].decodeValue(b, wireType));
1270
- }
1011
+ const res = [];
1012
+ for (const [i, wireType] of tuple._components.entries()) {
1013
+ if (i >= this._components.length) {
1014
+ // skip value
1015
+ wireType.decodeValue(b, wireType);
1271
1016
  }
1272
- }
1273
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
1274
- finally {
1275
- try {
1276
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1017
+ else {
1018
+ res.push(this._components[i].decodeValue(b, wireType));
1277
1019
  }
1278
- finally { if (e_4) throw e_4.error; }
1279
1020
  }
1280
1021
  return res;
1281
- };
1282
- TupleClass.prototype.display = function () {
1283
- var fields = this._components.map(function (value) { return value.display(); });
1284
- return "record {".concat(fields.join('; '), "}");
1285
- };
1286
- TupleClass.prototype.valueToString = function (values) {
1287
- var fields = zipWith(this._components, values, function (c, d) { return c.valueToString(d); });
1288
- return "record {".concat(fields.join('; '), "}");
1289
- };
1290
- return TupleClass;
1291
- }(RecordClass));
1022
+ }
1023
+ display() {
1024
+ const fields = this._components.map((value) => value.display());
1025
+ return `record {${fields.join('; ')}}`;
1026
+ }
1027
+ valueToString(values) {
1028
+ const fields = zipWith(this._components, values, (c, d) => c.valueToString(d));
1029
+ return `record {${fields.join('; ')}}`;
1030
+ }
1031
+ }
1292
1032
  exports.TupleClass = TupleClass;
1293
1033
  /**
1294
1034
  * Represents an IDL Variant
1295
1035
  * @param {Object} [fields] - mapping of function name to Type
1296
1036
  */
1297
- var VariantClass = /** @class */ (function (_super) {
1298
- __extends(VariantClass, _super);
1299
- function VariantClass(fields) {
1300
- if (fields === void 0) { fields = {}; }
1301
- var _this = _super.call(this) || this;
1302
- _this._fields = Object.entries(fields).sort(function (a, b) { return idlLabelToId(a[0]) - idlLabelToId(b[0]); });
1303
- return _this;
1304
- }
1305
- VariantClass.prototype.accept = function (v, d) {
1037
+ class VariantClass extends ConstructType {
1038
+ constructor(fields = {}) {
1039
+ super();
1040
+ this._fields = Object.entries(fields).sort((a, b) => idlLabelToId(a[0]) - idlLabelToId(b[0]));
1041
+ }
1042
+ accept(v, d) {
1306
1043
  return v.visitVariant(this, this._fields, d);
1307
- };
1308
- VariantClass.prototype.covariant = function (x) {
1309
- var _this = this;
1044
+ }
1045
+ covariant(x) {
1310
1046
  if (typeof x === 'object' &&
1311
1047
  Object.entries(x).length === 1 &&
1312
- this._fields.every(function (_a) {
1313
- var _b = __read(_a, 2), k = _b[0], v = _b[1];
1048
+ this._fields.every(([k, v]) => {
1314
1049
  try {
1315
1050
  // eslint-disable-next-line
1316
1051
  return !x.hasOwnProperty(k) || v.covariant(x[k]);
1317
1052
  }
1318
1053
  catch (e) {
1319
- throw new Error("Invalid ".concat(_this.display(), " argument: \n\nvariant ").concat(k, " -> ").concat(e.message));
1054
+ throw new Error(`Invalid ${this.display()} argument: \n\nvariant ${k} -> ${e.message}`);
1320
1055
  }
1321
1056
  }))
1322
1057
  return true;
1323
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
1324
- };
1325
- VariantClass.prototype.encodeValue = function (x) {
1326
- for (var i = 0; i < this._fields.length; i++) {
1327
- var _a = __read(this._fields[i], 2), name_1 = _a[0], type = _a[1];
1058
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1059
+ }
1060
+ encodeValue(x) {
1061
+ for (let i = 0; i < this._fields.length; i++) {
1062
+ const [name, type] = this._fields[i];
1328
1063
  // eslint-disable-next-line
1329
- if (x.hasOwnProperty(name_1)) {
1330
- var idx = (0, leb128_1.lebEncode)(i);
1331
- var buf = type.encodeValue(x[name_1]);
1064
+ if (x.hasOwnProperty(name)) {
1065
+ const idx = (0, leb128_1.lebEncode)(i);
1066
+ const buf = type.encodeValue(x[name]);
1332
1067
  return (0, buffer_1.concat)(idx, buf);
1333
1068
  }
1334
1069
  }
1335
1070
  throw Error('Variant has no data: ' + x);
1336
- };
1337
- VariantClass.prototype._buildTypeTableImpl = function (typeTable) {
1338
- this._fields.forEach(function (_a) {
1339
- var _b = __read(_a, 2), type = _b[1];
1071
+ }
1072
+ _buildTypeTableImpl(typeTable) {
1073
+ this._fields.forEach(([, type]) => {
1340
1074
  type.buildTypeTable(typeTable);
1341
1075
  });
1342
- var opCode = (0, leb128_1.slebEncode)(-21 /* IDLTypeIds.Variant */);
1343
- var len = (0, leb128_1.lebEncode)(this._fields.length);
1344
- var fields = this._fields.map(function (_a) {
1345
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
1346
- return (0, buffer_1.concat)((0, leb128_1.lebEncode)(idlLabelToId(key)), value.encodeType(typeTable));
1347
- });
1348
- typeTable.add(this, buffer_1.concat.apply(void 0, __spreadArray([opCode, len], __read(fields), false)));
1349
- };
1350
- VariantClass.prototype.decodeValue = function (b, t) {
1351
- var e_5, _a, _b;
1352
- var variant = this.checkType(t);
1076
+ const opCode = (0, leb128_1.slebEncode)(-21 /* IDLTypeIds.Variant */);
1077
+ const len = (0, leb128_1.lebEncode)(this._fields.length);
1078
+ const fields = this._fields.map(([key, value]) => (0, buffer_1.concat)((0, leb128_1.lebEncode)(idlLabelToId(key)), value.encodeType(typeTable)));
1079
+ typeTable.add(this, (0, buffer_1.concat)(opCode, len, ...fields));
1080
+ }
1081
+ decodeValue(b, t) {
1082
+ const variant = this.checkType(t);
1353
1083
  if (!(variant instanceof VariantClass)) {
1354
1084
  throw new Error('Not a variant type');
1355
1085
  }
1356
- var idx = Number((0, leb128_1.lebDecode)(b));
1086
+ const idx = Number((0, leb128_1.lebDecode)(b));
1357
1087
  if (idx >= variant._fields.length) {
1358
1088
  throw Error('Invalid variant index: ' + idx);
1359
1089
  }
1360
- var _c = __read(variant._fields[idx], 2), wireHash = _c[0], wireType = _c[1];
1361
- try {
1362
- for (var _d = __values(this._fields), _e = _d.next(); !_e.done; _e = _d.next()) {
1363
- var _f = __read(_e.value, 2), key = _f[0], expectType = _f[1];
1364
- if (idlLabelToId(wireHash) === idlLabelToId(key)) {
1365
- var value = expectType.decodeValue(b, wireType);
1366
- return _b = {}, _b[key] = value, _b;
1367
- }
1368
- }
1369
- }
1370
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
1371
- finally {
1372
- try {
1373
- if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1090
+ const [wireHash, wireType] = variant._fields[idx];
1091
+ for (const [key, expectType] of this._fields) {
1092
+ if (idlLabelToId(wireHash) === idlLabelToId(key)) {
1093
+ const value = expectType.decodeValue(b, wireType);
1094
+ return { [key]: value };
1374
1095
  }
1375
- finally { if (e_5) throw e_5.error; }
1376
1096
  }
1377
1097
  throw new Error('Cannot find field hash ' + wireHash);
1378
- };
1379
- Object.defineProperty(VariantClass.prototype, "name", {
1380
- get: function () {
1381
- var fields = this._fields.map(function (_a) {
1382
- var _b = __read(_a, 2), key = _b[0], type = _b[1];
1383
- return key + ':' + type.name;
1384
- });
1385
- return "variant {".concat(fields.join('; '), "}");
1386
- },
1387
- enumerable: false,
1388
- configurable: true
1389
- });
1390
- VariantClass.prototype.display = function () {
1391
- var fields = this._fields.map(function (_a) {
1392
- var _b = __read(_a, 2), key = _b[0], type = _b[1];
1393
- return key + (type.name === 'null' ? '' : ":".concat(type.display()));
1394
- });
1395
- return "variant {".concat(fields.join('; '), "}");
1396
- };
1397
- VariantClass.prototype.valueToString = function (x) {
1398
- var e_6, _a;
1399
- try {
1400
- for (var _b = __values(this._fields), _c = _b.next(); !_c.done; _c = _b.next()) {
1401
- var _d = __read(_c.value, 2), name_2 = _d[0], type = _d[1];
1402
- // eslint-disable-next-line
1403
- if (x.hasOwnProperty(name_2)) {
1404
- var value = type.valueToString(x[name_2]);
1405
- if (value === 'null') {
1406
- return "variant {".concat(name_2, "}");
1407
- }
1408
- else {
1409
- return "variant {".concat(name_2, "=").concat(value, "}");
1410
- }
1098
+ }
1099
+ get name() {
1100
+ const fields = this._fields.map(([key, type]) => key + ':' + type.name);
1101
+ return `variant {${fields.join('; ')}}`;
1102
+ }
1103
+ display() {
1104
+ const fields = this._fields.map(([key, type]) => key + (type.name === 'null' ? '' : `:${type.display()}`));
1105
+ return `variant {${fields.join('; ')}}`;
1106
+ }
1107
+ valueToString(x) {
1108
+ for (const [name, type] of this._fields) {
1109
+ // eslint-disable-next-line
1110
+ if (x.hasOwnProperty(name)) {
1111
+ const value = type.valueToString(x[name]);
1112
+ if (value === 'null') {
1113
+ return `variant {${name}}`;
1114
+ }
1115
+ else {
1116
+ return `variant {${name}=${value}}`;
1411
1117
  }
1412
1118
  }
1413
1119
  }
1414
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
1415
- finally {
1416
- try {
1417
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1418
- }
1419
- finally { if (e_6) throw e_6.error; }
1420
- }
1421
1120
  throw new Error('Variant has no data: ' + x);
1422
- };
1423
- return VariantClass;
1424
- }(ConstructType));
1121
+ }
1122
+ }
1425
1123
  exports.VariantClass = VariantClass;
1426
1124
  /**
1427
1125
  * Represents a reference to an IDL type, used for defining recursive data
1428
1126
  * types.
1429
1127
  */
1430
- var RecClass = /** @class */ (function (_super) {
1431
- __extends(RecClass, _super);
1432
- function RecClass() {
1433
- var _this = _super !== null && _super.apply(this, arguments) || this;
1434
- _this._id = RecClass._counter++;
1435
- _this._type = undefined;
1436
- return _this;
1437
- }
1438
- RecClass.prototype.accept = function (v, d) {
1128
+ class RecClass extends ConstructType {
1129
+ constructor() {
1130
+ super(...arguments);
1131
+ this._id = RecClass._counter++;
1132
+ this._type = undefined;
1133
+ }
1134
+ accept(v, d) {
1439
1135
  if (!this._type) {
1440
1136
  throw Error('Recursive type uninitialized.');
1441
1137
  }
1442
1138
  return v.visitRec(this, this._type, d);
1443
- };
1444
- RecClass.prototype.fill = function (t) {
1139
+ }
1140
+ fill(t) {
1445
1141
  this._type = t;
1446
- };
1447
- RecClass.prototype.getType = function () {
1142
+ }
1143
+ getType() {
1448
1144
  return this._type;
1449
- };
1450
- RecClass.prototype.covariant = function (x) {
1145
+ }
1146
+ covariant(x) {
1451
1147
  if (this._type ? this._type.covariant(x) : false)
1452
1148
  return true;
1453
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
1454
- };
1455
- RecClass.prototype.encodeValue = function (x) {
1149
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1150
+ }
1151
+ encodeValue(x) {
1456
1152
  if (!this._type) {
1457
1153
  throw Error('Recursive type uninitialized.');
1458
1154
  }
1459
1155
  return this._type.encodeValue(x);
1460
- };
1461
- RecClass.prototype._buildTypeTableImpl = function (typeTable) {
1156
+ }
1157
+ _buildTypeTableImpl(typeTable) {
1462
1158
  if (!this._type) {
1463
1159
  throw Error('Recursive type uninitialized.');
1464
1160
  }
1465
1161
  typeTable.add(this, new Uint8Array([]));
1466
1162
  this._type.buildTypeTable(typeTable);
1467
1163
  typeTable.merge(this, this._type.name);
1468
- };
1469
- RecClass.prototype.decodeValue = function (b, t) {
1164
+ }
1165
+ decodeValue(b, t) {
1470
1166
  if (!this._type) {
1471
1167
  throw Error('Recursive type uninitialized.');
1472
1168
  }
1473
1169
  return this._type.decodeValue(b, t);
1474
- };
1475
- Object.defineProperty(RecClass.prototype, "name", {
1476
- get: function () {
1477
- return "rec_".concat(this._id);
1478
- },
1479
- enumerable: false,
1480
- configurable: true
1481
- });
1482
- RecClass.prototype.display = function () {
1170
+ }
1171
+ get name() {
1172
+ return `rec_${this._id}`;
1173
+ }
1174
+ display() {
1483
1175
  if (!this._type) {
1484
1176
  throw Error('Recursive type uninitialized.');
1485
1177
  }
1486
- return "\u03BC".concat(this.name, ".").concat(this._type.name);
1487
- };
1488
- RecClass.prototype.valueToString = function (x) {
1178
+ return `μ${this.name}.${this._type.name}`;
1179
+ }
1180
+ valueToString(x) {
1489
1181
  if (!this._type) {
1490
1182
  throw Error('Recursive type uninitialized.');
1491
1183
  }
1492
1184
  return this._type.valueToString(x);
1493
- };
1494
- RecClass._counter = 0;
1495
- return RecClass;
1496
- }(ConstructType));
1185
+ }
1186
+ }
1497
1187
  exports.RecClass = RecClass;
1188
+ RecClass._counter = 0;
1498
1189
  function decodePrincipalId(b) {
1499
- var x = (0, leb128_1.safeReadUint8)(b);
1190
+ const x = (0, leb128_1.safeReadUint8)(b);
1500
1191
  if (x !== 1) {
1501
1192
  throw new Error('Cannot decode principal');
1502
1193
  }
1503
- var len = Number((0, leb128_1.lebDecode)(b));
1194
+ const len = Number((0, leb128_1.lebDecode)(b));
1504
1195
  return principal_1.Principal.fromUint8Array(new Uint8Array((0, leb128_1.safeRead)(b, len)));
1505
1196
  }
1506
1197
  /**
1507
1198
  * Represents an IDL principal reference
1508
1199
  */
1509
- var PrincipalClass = /** @class */ (function (_super) {
1510
- __extends(PrincipalClass, _super);
1511
- function PrincipalClass() {
1512
- return _super !== null && _super.apply(this, arguments) || this;
1513
- }
1514
- PrincipalClass.prototype.accept = function (v, d) {
1200
+ class PrincipalClass extends PrimitiveType {
1201
+ accept(v, d) {
1515
1202
  return v.visitPrincipal(this, d);
1516
- };
1517
- PrincipalClass.prototype.covariant = function (x) {
1203
+ }
1204
+ covariant(x) {
1518
1205
  if (x && x._isPrincipal)
1519
1206
  return true;
1520
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
1521
- };
1522
- PrincipalClass.prototype.encodeValue = function (x) {
1523
- var buf = x.toUint8Array();
1524
- var len = (0, leb128_1.lebEncode)(buf.byteLength);
1207
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1208
+ }
1209
+ encodeValue(x) {
1210
+ const buf = x.toUint8Array();
1211
+ const len = (0, leb128_1.lebEncode)(buf.byteLength);
1525
1212
  return (0, buffer_1.concat)(new Uint8Array([1]), len, buf);
1526
- };
1527
- PrincipalClass.prototype.encodeType = function () {
1213
+ }
1214
+ encodeType() {
1528
1215
  return (0, leb128_1.slebEncode)(-24 /* IDLTypeIds.Principal */);
1529
- };
1530
- PrincipalClass.prototype.decodeValue = function (b, t) {
1216
+ }
1217
+ decodeValue(b, t) {
1531
1218
  this.checkType(t);
1532
1219
  return decodePrincipalId(b);
1533
- };
1534
- Object.defineProperty(PrincipalClass.prototype, "name", {
1535
- get: function () {
1536
- return 'principal';
1537
- },
1538
- enumerable: false,
1539
- configurable: true
1540
- });
1541
- PrincipalClass.prototype.valueToString = function (x) {
1542
- return "".concat(this.name, " \"").concat(x.toText(), "\"");
1543
- };
1544
- return PrincipalClass;
1545
- }(PrimitiveType));
1220
+ }
1221
+ get name() {
1222
+ return 'principal';
1223
+ }
1224
+ valueToString(x) {
1225
+ return `${this.name} "${x.toText()}"`;
1226
+ }
1227
+ }
1546
1228
  exports.PrincipalClass = PrincipalClass;
1547
1229
  /**
1548
1230
  * Represents an IDL function reference.
@@ -1550,85 +1232,75 @@ exports.PrincipalClass = PrincipalClass;
1550
1232
  * @param retTypes Return types.
1551
1233
  * @param annotations Function annotations.
1552
1234
  */
1553
- var FuncClass = /** @class */ (function (_super) {
1554
- __extends(FuncClass, _super);
1555
- function FuncClass(argTypes, retTypes, annotations) {
1556
- if (annotations === void 0) { annotations = []; }
1557
- var _this = _super.call(this) || this;
1558
- _this.argTypes = argTypes;
1559
- _this.retTypes = retTypes;
1560
- _this.annotations = annotations;
1561
- return _this;
1562
- }
1563
- FuncClass.argsToString = function (types, v) {
1235
+ class FuncClass extends ConstructType {
1236
+ constructor(argTypes, retTypes, annotations = []) {
1237
+ super();
1238
+ this.argTypes = argTypes;
1239
+ this.retTypes = retTypes;
1240
+ this.annotations = annotations;
1241
+ }
1242
+ static argsToString(types, v) {
1564
1243
  if (types.length !== v.length) {
1565
1244
  throw new Error('arity mismatch');
1566
1245
  }
1567
- return '(' + types.map(function (t, i) { return t.valueToString(v[i]); }).join(', ') + ')';
1568
- };
1569
- FuncClass.prototype.accept = function (v, d) {
1246
+ return '(' + types.map((t, i) => t.valueToString(v[i])).join(', ') + ')';
1247
+ }
1248
+ accept(v, d) {
1570
1249
  return v.visitFunc(this, d);
1571
- };
1572
- FuncClass.prototype.covariant = function (x) {
1250
+ }
1251
+ covariant(x) {
1573
1252
  if (Array.isArray(x) && x.length === 2 && x[0] && x[0]._isPrincipal && typeof x[1] === 'string')
1574
1253
  return true;
1575
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
1576
- };
1577
- FuncClass.prototype.encodeValue = function (_a) {
1578
- var _b = __read(_a, 2), principal = _b[0], methodName = _b[1];
1579
- var buf = principal.toUint8Array();
1580
- var len = (0, leb128_1.lebEncode)(buf.byteLength);
1581
- var canister = (0, buffer_1.concat)(new Uint8Array([1]), len, buf);
1582
- var method = new TextEncoder().encode(methodName);
1583
- var methodLen = (0, leb128_1.lebEncode)(method.byteLength);
1254
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1255
+ }
1256
+ encodeValue([principal, methodName]) {
1257
+ const buf = principal.toUint8Array();
1258
+ const len = (0, leb128_1.lebEncode)(buf.byteLength);
1259
+ const canister = (0, buffer_1.concat)(new Uint8Array([1]), len, buf);
1260
+ const method = new TextEncoder().encode(methodName);
1261
+ const methodLen = (0, leb128_1.lebEncode)(method.byteLength);
1584
1262
  return (0, buffer_1.concat)(new Uint8Array([1]), canister, methodLen, method);
1585
- };
1586
- FuncClass.prototype._buildTypeTableImpl = function (T) {
1587
- var _this = this;
1588
- this.argTypes.forEach(function (arg) { return arg.buildTypeTable(T); });
1589
- this.retTypes.forEach(function (arg) { return arg.buildTypeTable(T); });
1590
- var opCode = (0, leb128_1.slebEncode)(-22 /* IDLTypeIds.Func */);
1591
- var argLen = (0, leb128_1.lebEncode)(this.argTypes.length);
1592
- var args = buffer_1.concat.apply(void 0, __spreadArray([], __read(this.argTypes.map(function (arg) { return arg.encodeType(T); })), false));
1593
- var retLen = (0, leb128_1.lebEncode)(this.retTypes.length);
1594
- var rets = buffer_1.concat.apply(void 0, __spreadArray([], __read(this.retTypes.map(function (arg) { return arg.encodeType(T); })), false));
1595
- var annLen = (0, leb128_1.lebEncode)(this.annotations.length);
1596
- var anns = buffer_1.concat.apply(void 0, __spreadArray([], __read(this.annotations.map(function (a) { return _this.encodeAnnotation(a); })), false));
1263
+ }
1264
+ _buildTypeTableImpl(T) {
1265
+ this.argTypes.forEach((arg) => arg.buildTypeTable(T));
1266
+ this.retTypes.forEach((arg) => arg.buildTypeTable(T));
1267
+ const opCode = (0, leb128_1.slebEncode)(-22 /* IDLTypeIds.Func */);
1268
+ const argLen = (0, leb128_1.lebEncode)(this.argTypes.length);
1269
+ const args = (0, buffer_1.concat)(...this.argTypes.map((arg) => arg.encodeType(T)));
1270
+ const retLen = (0, leb128_1.lebEncode)(this.retTypes.length);
1271
+ const rets = (0, buffer_1.concat)(...this.retTypes.map((arg) => arg.encodeType(T)));
1272
+ const annLen = (0, leb128_1.lebEncode)(this.annotations.length);
1273
+ const anns = (0, buffer_1.concat)(...this.annotations.map((a) => this.encodeAnnotation(a)));
1597
1274
  T.add(this, (0, buffer_1.concat)(opCode, argLen, args, retLen, rets, annLen, anns));
1598
- };
1599
- FuncClass.prototype.decodeValue = function (b) {
1600
- var x = (0, leb128_1.safeReadUint8)(b);
1275
+ }
1276
+ decodeValue(b) {
1277
+ const x = (0, leb128_1.safeReadUint8)(b);
1601
1278
  if (x !== 1) {
1602
1279
  throw new Error('Cannot decode function reference');
1603
1280
  }
1604
- var canister = decodePrincipalId(b);
1605
- var mLen = Number((0, leb128_1.lebDecode)(b));
1606
- var buf = (0, leb128_1.safeRead)(b, mLen);
1607
- var decoder = new TextDecoder('utf8', { fatal: true });
1608
- var method = decoder.decode(buf);
1281
+ const canister = decodePrincipalId(b);
1282
+ const mLen = Number((0, leb128_1.lebDecode)(b));
1283
+ const buf = (0, leb128_1.safeRead)(b, mLen);
1284
+ const decoder = new TextDecoder('utf8', { fatal: true });
1285
+ const method = decoder.decode(buf);
1609
1286
  return [canister, method];
1610
- };
1611
- Object.defineProperty(FuncClass.prototype, "name", {
1612
- get: function () {
1613
- var args = this.argTypes.map(function (arg) { return arg.name; }).join(', ');
1614
- var rets = this.retTypes.map(function (arg) { return arg.name; }).join(', ');
1615
- var annon = ' ' + this.annotations.join(' ');
1616
- return "(".concat(args, ") -> (").concat(rets, ")").concat(annon);
1617
- },
1618
- enumerable: false,
1619
- configurable: true
1620
- });
1621
- FuncClass.prototype.valueToString = function (_a) {
1622
- var _b = __read(_a, 2), principal = _b[0], str = _b[1];
1623
- return "func \"".concat(principal.toText(), "\".").concat(str);
1624
- };
1625
- FuncClass.prototype.display = function () {
1626
- var args = this.argTypes.map(function (arg) { return arg.display(); }).join(', ');
1627
- var rets = this.retTypes.map(function (arg) { return arg.display(); }).join(', ');
1628
- var annon = ' ' + this.annotations.join(' ');
1629
- return "(".concat(args, ") \u2192 (").concat(rets, ")").concat(annon);
1630
- };
1631
- FuncClass.prototype.encodeAnnotation = function (ann) {
1287
+ }
1288
+ get name() {
1289
+ const args = this.argTypes.map((arg) => arg.name).join(', ');
1290
+ const rets = this.retTypes.map((arg) => arg.name).join(', ');
1291
+ const annon = ' ' + this.annotations.join(' ');
1292
+ return `(${args}) -> (${rets})${annon}`;
1293
+ }
1294
+ valueToString([principal, str]) {
1295
+ return `func "${principal.toText()}".${str}`;
1296
+ }
1297
+ display() {
1298
+ const args = this.argTypes.map((arg) => arg.display()).join(', ');
1299
+ const rets = this.retTypes.map((arg) => arg.display()).join(', ');
1300
+ const annon = ' ' + this.annotations.join(' ');
1301
+ return `(${args}) → (${rets})${annon}`;
1302
+ }
1303
+ encodeAnnotation(ann) {
1632
1304
  if (ann === 'query') {
1633
1305
  return new Uint8Array([1]);
1634
1306
  }
@@ -1638,64 +1310,49 @@ var FuncClass = /** @class */ (function (_super) {
1638
1310
  else {
1639
1311
  throw new Error('Illegal function annotation');
1640
1312
  }
1641
- };
1642
- return FuncClass;
1643
- }(ConstructType));
1313
+ }
1314
+ }
1644
1315
  exports.FuncClass = FuncClass;
1645
- var ServiceClass = /** @class */ (function (_super) {
1646
- __extends(ServiceClass, _super);
1647
- function ServiceClass(fields) {
1648
- var _this = _super.call(this) || this;
1649
- _this._fields = Object.entries(fields).sort(function (a, b) { return idlLabelToId(a[0]) - idlLabelToId(b[0]); });
1650
- return _this;
1651
- }
1652
- ServiceClass.prototype.accept = function (v, d) {
1316
+ class ServiceClass extends ConstructType {
1317
+ constructor(fields) {
1318
+ super();
1319
+ this._fields = Object.entries(fields).sort((a, b) => idlLabelToId(a[0]) - idlLabelToId(b[0]));
1320
+ }
1321
+ accept(v, d) {
1653
1322
  return v.visitService(this, d);
1654
- };
1655
- ServiceClass.prototype.covariant = function (x) {
1323
+ }
1324
+ covariant(x) {
1656
1325
  if (x && x._isPrincipal)
1657
1326
  return true;
1658
- throw new Error("Invalid ".concat(this.display(), " argument: ").concat(toReadableString(x)));
1659
- };
1660
- ServiceClass.prototype.encodeValue = function (x) {
1661
- var buf = x.toUint8Array();
1662
- var len = (0, leb128_1.lebEncode)(buf.length);
1327
+ throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1328
+ }
1329
+ encodeValue(x) {
1330
+ const buf = x.toUint8Array();
1331
+ const len = (0, leb128_1.lebEncode)(buf.length);
1663
1332
  return (0, buffer_1.concat)(new Uint8Array([1]), len, buf);
1664
- };
1665
- ServiceClass.prototype._buildTypeTableImpl = function (T) {
1666
- this._fields.forEach(function (_a) {
1667
- var _b = __read(_a, 2), _ = _b[0], func = _b[1];
1668
- return func.buildTypeTable(T);
1669
- });
1670
- var opCode = (0, leb128_1.slebEncode)(-23 /* IDLTypeIds.Service */);
1671
- var len = (0, leb128_1.lebEncode)(this._fields.length);
1672
- var meths = this._fields.map(function (_a) {
1673
- var _b = __read(_a, 2), label = _b[0], func = _b[1];
1674
- var labelBuf = new TextEncoder().encode(label);
1675
- var labelLen = (0, leb128_1.lebEncode)(labelBuf.length);
1333
+ }
1334
+ _buildTypeTableImpl(T) {
1335
+ this._fields.forEach(([_, func]) => func.buildTypeTable(T));
1336
+ const opCode = (0, leb128_1.slebEncode)(-23 /* IDLTypeIds.Service */);
1337
+ const len = (0, leb128_1.lebEncode)(this._fields.length);
1338
+ const meths = this._fields.map(([label, func]) => {
1339
+ const labelBuf = new TextEncoder().encode(label);
1340
+ const labelLen = (0, leb128_1.lebEncode)(labelBuf.length);
1676
1341
  return (0, buffer_1.concat)(labelLen, labelBuf, func.encodeType(T));
1677
1342
  });
1678
- T.add(this, buffer_1.concat.apply(void 0, __spreadArray([opCode, len], __read(meths), false)));
1679
- };
1680
- ServiceClass.prototype.decodeValue = function (b) {
1343
+ T.add(this, (0, buffer_1.concat)(opCode, len, ...meths));
1344
+ }
1345
+ decodeValue(b) {
1681
1346
  return decodePrincipalId(b);
1682
- };
1683
- Object.defineProperty(ServiceClass.prototype, "name", {
1684
- get: function () {
1685
- var fields = this._fields.map(function (_a) {
1686
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
1687
- return key + ':' + value.name;
1688
- });
1689
- return "service {".concat(fields.join('; '), "}");
1690
- },
1691
- enumerable: false,
1692
- configurable: true
1693
- });
1694
- ServiceClass.prototype.valueToString = function (x) {
1695
- return "service \"".concat(x.toText(), "\"");
1696
- };
1697
- return ServiceClass;
1698
- }(ConstructType));
1347
+ }
1348
+ get name() {
1349
+ const fields = this._fields.map(([key, value]) => key + ':' + value.name);
1350
+ return `service {${fields.join('; ')}}`;
1351
+ }
1352
+ valueToString(x) {
1353
+ return `service "${x.toText()}"`;
1354
+ }
1355
+ }
1699
1356
  exports.ServiceClass = ServiceClass;
1700
1357
  /**
1701
1358
  *
@@ -1703,7 +1360,7 @@ exports.ServiceClass = ServiceClass;
1703
1360
  * @returns {string}
1704
1361
  */
1705
1362
  function toReadableString(x) {
1706
- var str = JSON.stringify(x, function (_key, value) { return (typeof value === 'bigint' ? "BigInt(".concat(value, ")") : value); });
1363
+ const str = JSON.stringify(x, (_key, value) => (typeof value === 'bigint' ? `BigInt(${value})` : value));
1707
1364
  return str && str.length > toReadableString_max ? str.substring(0, toReadableString_max - 3) + '...' : str;
1708
1365
  }
1709
1366
  /**
@@ -1716,22 +1373,22 @@ function encode(argTypes, args) {
1716
1373
  if (args.length < argTypes.length) {
1717
1374
  throw Error('Wrong number of message arguments');
1718
1375
  }
1719
- var typeTable = new TypeTable();
1720
- argTypes.forEach(function (t) { return t.buildTypeTable(typeTable); });
1721
- var magic = new TextEncoder().encode(magicNumber);
1722
- var table = typeTable.encode();
1723
- var len = (0, leb128_1.lebEncode)(args.length);
1724
- var typs = buffer_1.concat.apply(void 0, __spreadArray([], __read(argTypes.map(function (t) { return t.encodeType(typeTable); })), false));
1725
- var vals = buffer_1.concat.apply(void 0, __spreadArray([], __read(zipWith(argTypes, args, function (t, x) {
1376
+ const typeTable = new TypeTable();
1377
+ argTypes.forEach((t) => t.buildTypeTable(typeTable));
1378
+ const magic = new TextEncoder().encode(magicNumber);
1379
+ const table = typeTable.encode();
1380
+ const len = (0, leb128_1.lebEncode)(args.length);
1381
+ const typs = (0, buffer_1.concat)(...argTypes.map((t) => t.encodeType(typeTable)));
1382
+ const vals = (0, buffer_1.concat)(...zipWith(argTypes, args, (t, x) => {
1726
1383
  try {
1727
1384
  t.covariant(x);
1728
1385
  }
1729
1386
  catch (e) {
1730
- var err = new Error(e.message + '\n\n');
1387
+ const err = new Error(e.message + '\n\n');
1731
1388
  throw err;
1732
1389
  }
1733
1390
  return t.encodeValue(x);
1734
- })), false));
1391
+ }));
1735
1392
  return (0, buffer_1.concat)(magic, table, len, typs, vals);
1736
1393
  }
1737
1394
  exports.encode = encode;
@@ -1742,34 +1399,34 @@ exports.encode = encode;
1742
1399
  * @returns Value deserialised to JS type
1743
1400
  */
1744
1401
  function decode(retTypes, bytes) {
1745
- var b = new buffer_2.PipeArrayBuffer(bytes);
1402
+ const b = new buffer_2.PipeArrayBuffer(bytes);
1746
1403
  if (bytes.byteLength < magicNumber.length) {
1747
1404
  throw new Error('Message length smaller than magic number');
1748
1405
  }
1749
- var magicBuffer = (0, leb128_1.safeRead)(b, magicNumber.length);
1750
- var magic = new TextDecoder().decode(magicBuffer);
1406
+ const magicBuffer = (0, leb128_1.safeRead)(b, magicNumber.length);
1407
+ const magic = new TextDecoder().decode(magicBuffer);
1751
1408
  if (magic !== magicNumber) {
1752
1409
  throw new Error('Wrong magic number: ' + JSON.stringify(magic));
1753
1410
  }
1754
1411
  function readTypeTable(pipe) {
1755
- var typeTable = [];
1756
- var len = Number((0, leb128_1.lebDecode)(pipe));
1757
- for (var i = 0; i < len; i++) {
1758
- var ty = Number((0, leb128_1.slebDecode)(pipe));
1412
+ const typeTable = [];
1413
+ const len = Number((0, leb128_1.lebDecode)(pipe));
1414
+ for (let i = 0; i < len; i++) {
1415
+ const ty = Number((0, leb128_1.slebDecode)(pipe));
1759
1416
  switch (ty) {
1760
1417
  case -18 /* IDLTypeIds.Opt */:
1761
1418
  case -19 /* IDLTypeIds.Vector */: {
1762
- var t = Number((0, leb128_1.slebDecode)(pipe));
1419
+ const t = Number((0, leb128_1.slebDecode)(pipe));
1763
1420
  typeTable.push([ty, t]);
1764
1421
  break;
1765
1422
  }
1766
1423
  case -20 /* IDLTypeIds.Record */:
1767
1424
  case -21 /* IDLTypeIds.Variant */: {
1768
- var fields = [];
1769
- var objectLength = Number((0, leb128_1.lebDecode)(pipe));
1770
- var prevHash = void 0;
1425
+ const fields = [];
1426
+ let objectLength = Number((0, leb128_1.lebDecode)(pipe));
1427
+ let prevHash;
1771
1428
  while (objectLength--) {
1772
- var hash = Number((0, leb128_1.lebDecode)(pipe));
1429
+ const hash = Number((0, leb128_1.lebDecode)(pipe));
1773
1430
  if (hash >= Math.pow(2, 32)) {
1774
1431
  throw new Error('field id out of 32-bit range');
1775
1432
  }
@@ -1777,27 +1434,27 @@ function decode(retTypes, bytes) {
1777
1434
  throw new Error('field id collision or not sorted');
1778
1435
  }
1779
1436
  prevHash = hash;
1780
- var t = Number((0, leb128_1.slebDecode)(pipe));
1437
+ const t = Number((0, leb128_1.slebDecode)(pipe));
1781
1438
  fields.push([hash, t]);
1782
1439
  }
1783
1440
  typeTable.push([ty, fields]);
1784
1441
  break;
1785
1442
  }
1786
1443
  case -22 /* IDLTypeIds.Func */: {
1787
- var args = [];
1788
- var argLength = Number((0, leb128_1.lebDecode)(pipe));
1444
+ const args = [];
1445
+ let argLength = Number((0, leb128_1.lebDecode)(pipe));
1789
1446
  while (argLength--) {
1790
1447
  args.push(Number((0, leb128_1.slebDecode)(pipe)));
1791
1448
  }
1792
- var returnValues = [];
1793
- var returnValuesLength = Number((0, leb128_1.lebDecode)(pipe));
1449
+ const returnValues = [];
1450
+ let returnValuesLength = Number((0, leb128_1.lebDecode)(pipe));
1794
1451
  while (returnValuesLength--) {
1795
1452
  returnValues.push(Number((0, leb128_1.slebDecode)(pipe)));
1796
1453
  }
1797
- var annotations = [];
1798
- var annotationLength = Number((0, leb128_1.lebDecode)(pipe));
1454
+ const annotations = [];
1455
+ let annotationLength = Number((0, leb128_1.lebDecode)(pipe));
1799
1456
  while (annotationLength--) {
1800
- var annotation = Number((0, leb128_1.lebDecode)(pipe));
1457
+ const annotation = Number((0, leb128_1.lebDecode)(pipe));
1801
1458
  switch (annotation) {
1802
1459
  case 1: {
1803
1460
  annotations.push('query');
@@ -1815,12 +1472,12 @@ function decode(retTypes, bytes) {
1815
1472
  break;
1816
1473
  }
1817
1474
  case -23 /* IDLTypeIds.Service */: {
1818
- var servLength = Number((0, leb128_1.lebDecode)(pipe));
1819
- var methods = [];
1475
+ let servLength = Number((0, leb128_1.lebDecode)(pipe));
1476
+ const methods = [];
1820
1477
  while (servLength--) {
1821
- var nameLength = Number((0, leb128_1.lebDecode)(pipe));
1822
- var funcName = new TextDecoder().decode((0, leb128_1.safeRead)(pipe, nameLength));
1823
- var funcType = (0, leb128_1.slebDecode)(pipe);
1478
+ const nameLength = Number((0, leb128_1.lebDecode)(pipe));
1479
+ const funcName = new TextDecoder().decode((0, leb128_1.safeRead)(pipe, nameLength));
1480
+ const funcType = (0, leb128_1.slebDecode)(pipe);
1824
1481
  methods.push([funcName, funcType]);
1825
1482
  }
1826
1483
  typeTable.push([ty, methods]);
@@ -1830,18 +1487,18 @@ function decode(retTypes, bytes) {
1830
1487
  throw new Error('Illegal op_code: ' + ty);
1831
1488
  }
1832
1489
  }
1833
- var rawList = [];
1834
- var length = Number((0, leb128_1.lebDecode)(pipe));
1835
- for (var i = 0; i < length; i++) {
1490
+ const rawList = [];
1491
+ const length = Number((0, leb128_1.lebDecode)(pipe));
1492
+ for (let i = 0; i < length; i++) {
1836
1493
  rawList.push(Number((0, leb128_1.slebDecode)(pipe)));
1837
1494
  }
1838
1495
  return [typeTable, rawList];
1839
1496
  }
1840
- var _a = __read(readTypeTable(b), 2), rawTable = _a[0], rawTypes = _a[1];
1497
+ const [rawTable, rawTypes] = readTypeTable(b);
1841
1498
  if (rawTypes.length < retTypes.length) {
1842
1499
  throw new Error('Wrong number of return values');
1843
1500
  }
1844
- var table = rawTable.map(function (_) { return Rec(); });
1501
+ const table = rawTable.map((_) => Rec());
1845
1502
  function getType(t) {
1846
1503
  if (t < -24) {
1847
1504
  throw new Error('future value not supported');
@@ -1894,86 +1551,55 @@ function decode(retTypes, bytes) {
1894
1551
  return table[t];
1895
1552
  }
1896
1553
  function buildType(entry) {
1897
- var e_7, _a, e_8, _b, e_9, _c;
1898
1554
  switch (entry[0]) {
1899
1555
  case -19 /* IDLTypeIds.Vector */: {
1900
- var ty = getType(entry[1]);
1556
+ const ty = getType(entry[1]);
1901
1557
  return Vec(ty);
1902
1558
  }
1903
1559
  case -18 /* IDLTypeIds.Opt */: {
1904
- var ty = getType(entry[1]);
1560
+ const ty = getType(entry[1]);
1905
1561
  return Opt(ty);
1906
1562
  }
1907
1563
  case -20 /* IDLTypeIds.Record */: {
1908
- var fields = {};
1909
- try {
1910
- for (var _d = __values(entry[1]), _e = _d.next(); !_e.done; _e = _d.next()) {
1911
- var _f = __read(_e.value, 2), hash = _f[0], ty = _f[1];
1912
- var name_3 = "_".concat(hash, "_");
1913
- fields[name_3] = getType(ty);
1914
- }
1564
+ const fields = {};
1565
+ for (const [hash, ty] of entry[1]) {
1566
+ const name = `_${hash}_`;
1567
+ fields[name] = getType(ty);
1915
1568
  }
1916
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
1917
- finally {
1918
- try {
1919
- if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1920
- }
1921
- finally { if (e_7) throw e_7.error; }
1922
- }
1923
- var record = Record(fields);
1924
- var tuple = record.tryAsTuple();
1569
+ const record = Record(fields);
1570
+ const tuple = record.tryAsTuple();
1925
1571
  if (Array.isArray(tuple)) {
1926
- return Tuple.apply(void 0, __spreadArray([], __read(tuple), false));
1572
+ return Tuple(...tuple);
1927
1573
  }
1928
1574
  else {
1929
1575
  return record;
1930
1576
  }
1931
1577
  }
1932
1578
  case -21 /* IDLTypeIds.Variant */: {
1933
- var fields = {};
1934
- try {
1935
- for (var _g = __values(entry[1]), _h = _g.next(); !_h.done; _h = _g.next()) {
1936
- var _j = __read(_h.value, 2), hash = _j[0], ty = _j[1];
1937
- var name_4 = "_".concat(hash, "_");
1938
- fields[name_4] = getType(ty);
1939
- }
1940
- }
1941
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
1942
- finally {
1943
- try {
1944
- if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
1945
- }
1946
- finally { if (e_8) throw e_8.error; }
1579
+ const fields = {};
1580
+ for (const [hash, ty] of entry[1]) {
1581
+ const name = `_${hash}_`;
1582
+ fields[name] = getType(ty);
1947
1583
  }
1948
1584
  return Variant(fields);
1949
1585
  }
1950
1586
  case -22 /* IDLTypeIds.Func */: {
1951
- var _k = __read(entry[1], 3), args = _k[0], returnValues = _k[1], annotations = _k[2];
1952
- return Func(args.map(function (t) { return getType(t); }), returnValues.map(function (t) { return getType(t); }), annotations);
1587
+ const [args, returnValues, annotations] = entry[1];
1588
+ return Func(args.map((t) => getType(t)), returnValues.map((t) => getType(t)), annotations);
1953
1589
  }
1954
1590
  case -23 /* IDLTypeIds.Service */: {
1955
- var rec = {};
1956
- var methods = entry[1];
1957
- try {
1958
- for (var methods_1 = __values(methods), methods_1_1 = methods_1.next(); !methods_1_1.done; methods_1_1 = methods_1.next()) {
1959
- var _l = __read(methods_1_1.value, 2), name_5 = _l[0], typeRef = _l[1];
1960
- var type = getType(typeRef);
1961
- if (type instanceof RecClass) {
1962
- // unpack reference type
1963
- type = type.getType();
1964
- }
1965
- if (!(type instanceof FuncClass)) {
1966
- throw new Error('Illegal service definition: services can only contain functions');
1967
- }
1968
- rec[name_5] = type;
1591
+ const rec = {};
1592
+ const methods = entry[1];
1593
+ for (const [name, typeRef] of methods) {
1594
+ let type = getType(typeRef);
1595
+ if (type instanceof RecClass) {
1596
+ // unpack reference type
1597
+ type = type.getType();
1969
1598
  }
1970
- }
1971
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
1972
- finally {
1973
- try {
1974
- if (methods_1_1 && !methods_1_1.done && (_c = methods_1.return)) _c.call(methods_1);
1599
+ if (!(type instanceof FuncClass)) {
1600
+ throw new Error('Illegal service definition: services can only contain functions');
1975
1601
  }
1976
- finally { if (e_9) throw e_9.error; }
1602
+ rec[name] = type;
1977
1603
  }
1978
1604
  return Service(rec);
1979
1605
  }
@@ -1981,16 +1607,16 @@ function decode(retTypes, bytes) {
1981
1607
  throw new Error('Illegal op_code: ' + entry[0]);
1982
1608
  }
1983
1609
  }
1984
- rawTable.forEach(function (entry, i) {
1985
- var t = buildType(entry);
1610
+ rawTable.forEach((entry, i) => {
1611
+ const t = buildType(entry);
1986
1612
  table[i].fill(t);
1987
1613
  });
1988
- var types = rawTypes.map(function (t) { return getType(t); });
1989
- var output = retTypes.map(function (t, i) {
1614
+ const types = rawTypes.map((t) => getType(t));
1615
+ const output = retTypes.map((t, i) => {
1990
1616
  return t.decodeValue(b, types[i]);
1991
1617
  });
1992
1618
  // skip unused values
1993
- for (var ind = retTypes.length; ind < types.length; ind++) {
1619
+ for (let ind = retTypes.length; ind < types.length; ind++) {
1994
1620
  types[ind].decodeValue(b, types[ind]);
1995
1621
  }
1996
1622
  if (b.byteLength > 0) {
@@ -2027,11 +1653,7 @@ exports.Principal = new PrincipalClass();
2027
1653
  * @param types array of any types
2028
1654
  * @returns TupleClass from those types
2029
1655
  */
2030
- function Tuple() {
2031
- var types = [];
2032
- for (var _i = 0; _i < arguments.length; _i++) {
2033
- types[_i] = arguments[_i];
2034
- }
1656
+ function Tuple(...types) {
2035
1657
  return new TupleClass(types);
2036
1658
  }
2037
1659
  exports.Tuple = Tuple;
@@ -2086,8 +1708,7 @@ exports.Rec = Rec;
2086
1708
  * @param annotations array of strings, [] by default
2087
1709
  * @returns new FuncClass
2088
1710
  */
2089
- function Func(args, ret, annotations) {
2090
- if (annotations === void 0) { annotations = []; }
1711
+ function Func(args, ret, annotations = []) {
2091
1712
  return new FuncClass(args, ret, annotations);
2092
1713
  }
2093
1714
  exports.Func = Func;