@icp-sdk/core 5.0.0 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/agent/actor.d.ts +15 -1
- package/lib/cjs/agent/actor.d.ts.map +1 -1
- package/lib/cjs/agent/actor.js +37 -91
- package/lib/cjs/agent/actor.js.map +1 -1
- package/lib/cjs/agent/agent/api.d.ts +29 -6
- package/lib/cjs/agent/agent/api.d.ts.map +1 -1
- package/lib/cjs/agent/agent/api.js.map +1 -1
- package/lib/cjs/agent/agent/http/expiry.d.ts +36 -0
- package/lib/cjs/agent/agent/http/expiry.d.ts.map +1 -0
- package/lib/cjs/agent/agent/http/expiry.js +92 -0
- package/lib/cjs/agent/agent/http/expiry.js.map +1 -0
- package/lib/cjs/agent/agent/http/index.d.ts +11 -2
- package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/index.js +142 -73
- package/lib/cjs/agent/agent/http/index.js.map +1 -1
- package/lib/cjs/agent/agent/http/transforms.d.ts +1 -35
- package/lib/cjs/agent/agent/http/transforms.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/transforms.js +6 -90
- package/lib/cjs/agent/agent/http/transforms.js.map +1 -1
- package/lib/cjs/agent/agent/http/types.d.ts +1 -1
- package/lib/cjs/agent/agent/http/types.d.ts.map +1 -1
- package/lib/cjs/agent/auth.d.ts +1 -1
- package/lib/cjs/agent/auth.d.ts.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.d.ts +5 -4
- package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.js +6 -10
- package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
- package/lib/cjs/agent/cbor.js +2 -2
- package/lib/cjs/agent/cbor.js.map +1 -1
- package/lib/cjs/agent/certificate.d.ts +4 -4
- package/lib/cjs/agent/certificate.d.ts.map +1 -1
- package/lib/cjs/agent/certificate.js +6 -10
- package/lib/cjs/agent/certificate.js.map +1 -1
- package/lib/cjs/agent/constants.d.ts +4 -4
- package/lib/cjs/agent/constants.d.ts.map +1 -1
- package/lib/cjs/agent/der.d.ts.map +1 -1
- package/lib/cjs/agent/der.js +26 -21
- package/lib/cjs/agent/der.js.map +1 -1
- package/lib/cjs/agent/errors.d.ts +27 -18
- package/lib/cjs/agent/errors.d.ts.map +1 -1
- package/lib/cjs/agent/errors.js +18 -15
- package/lib/cjs/agent/errors.js.map +1 -1
- package/lib/cjs/agent/observable.d.ts +1 -1
- package/lib/cjs/agent/observable.d.ts.map +1 -1
- package/lib/cjs/agent/polling/backoff.d.ts +7 -7
- package/lib/cjs/agent/polling/backoff.d.ts.map +1 -1
- package/lib/cjs/agent/polling/backoff.js +3 -5
- package/lib/cjs/agent/polling/backoff.js.map +1 -1
- package/lib/cjs/agent/polling/index.d.ts +11 -10
- package/lib/cjs/agent/polling/index.d.ts.map +1 -1
- package/lib/cjs/agent/polling/index.js +8 -6
- package/lib/cjs/agent/polling/index.js.map +1 -1
- package/lib/cjs/agent/polling/strategy.d.ts +2 -5
- package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
- package/lib/cjs/agent/polling/strategy.js.map +1 -1
- package/lib/cjs/agent/polling/types.d.ts +18 -0
- package/lib/cjs/agent/polling/types.d.ts.map +1 -0
- package/lib/cjs/agent/polling/types.js +3 -0
- package/lib/cjs/agent/polling/types.js.map +1 -0
- package/lib/cjs/agent/public_key.d.ts +1 -1
- package/lib/cjs/agent/public_key.d.ts.map +1 -1
- package/lib/cjs/agent/request_id.d.ts.map +1 -1
- package/lib/cjs/agent/request_id.js +7 -7
- package/lib/cjs/agent/request_id.js.map +1 -1
- package/lib/cjs/agent/subnetStatus/index.d.ts +5 -4
- package/lib/cjs/agent/subnetStatus/index.d.ts.map +1 -1
- package/lib/cjs/agent/subnetStatus/index.js +6 -10
- package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
- package/lib/cjs/agent/utils/buffer.d.ts.map +1 -1
- package/lib/cjs/agent/utils/buffer.js +4 -2
- package/lib/cjs/agent/utils/buffer.js.map +1 -1
- package/lib/cjs/agent/utils/certificateReject.d.ts +11 -0
- package/lib/cjs/agent/utils/certificateReject.d.ts.map +1 -0
- package/lib/cjs/agent/utils/certificateReject.js +19 -0
- package/lib/cjs/agent/utils/certificateReject.js.map +1 -0
- package/lib/cjs/agent/utils/expirableMap.d.ts +4 -4
- package/lib/cjs/agent/utils/expirableMap.d.ts.map +1 -1
- package/lib/cjs/agent/utils/expirableMap.js +2 -2
- package/lib/cjs/agent/utils/readState.d.ts +3 -3
- package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
- package/lib/cjs/candid/candid-core.d.ts +1 -1
- package/lib/cjs/candid/candid-core.d.ts.map +1 -1
- package/lib/cjs/candid/candid-core.js +7 -9
- package/lib/cjs/candid/candid-core.js.map +1 -1
- package/lib/cjs/candid/candid-ui.d.ts.map +1 -1
- package/lib/cjs/candid/candid-ui.js +8 -24
- package/lib/cjs/candid/candid-ui.js.map +1 -1
- package/lib/cjs/candid/idl.d.ts +14 -1
- package/lib/cjs/candid/idl.d.ts.map +1 -1
- package/lib/cjs/candid/idl.js +164 -125
- package/lib/cjs/candid/idl.js.map +1 -1
- package/lib/cjs/candid/utils/leb128.d.ts.map +1 -1
- package/lib/cjs/candid/utils/leb128.js +1 -3
- package/lib/cjs/candid/utils/leb128.js.map +1 -1
- package/lib/cjs/identity/identity/delegation.d.ts +4 -3
- package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
- package/lib/cjs/identity/identity/delegation.js +3 -3
- package/lib/cjs/identity/identity/delegation.js.map +1 -1
- package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
- package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
- package/lib/cjs/identity/identity/ecdsa.js +2 -4
- package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
- package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
- package/lib/cjs/identity/identity/ed25519.js +9 -10
- package/lib/cjs/identity/identity/ed25519.js.map +1 -1
- package/lib/cjs/identity/identity/partial.d.ts +1 -1
- package/lib/cjs/identity/identity/partial.d.ts.map +1 -1
- package/lib/cjs/identity/identity/webauthn.js +4 -5
- package/lib/cjs/identity/identity/webauthn.js.map +1 -1
- package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/der.js +27 -22
- package/lib/cjs/identity/secp256k1/der.js.map +1 -1
- package/lib/cjs/identity/secp256k1/pem.js +1 -1
- package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
- package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/secp256k1.js +6 -6
- package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
- package/lib/cjs/principal/principal.d.ts +2 -2
- package/lib/cjs/principal/principal.d.ts.map +1 -1
- package/lib/cjs/principal/principal.js +10 -6
- package/lib/cjs/principal/principal.js.map +1 -1
- package/lib/esm/agent/actor.d.ts +15 -1
- package/lib/esm/agent/actor.d.ts.map +1 -1
- package/lib/esm/agent/actor.js +40 -94
- package/lib/esm/agent/actor.js.map +1 -1
- package/lib/esm/agent/agent/api.d.ts +29 -6
- package/lib/esm/agent/agent/api.d.ts.map +1 -1
- package/lib/esm/agent/agent/api.js.map +1 -1
- package/lib/esm/agent/agent/http/expiry.d.ts +36 -0
- package/lib/esm/agent/agent/http/expiry.d.ts.map +1 -0
- package/lib/esm/agent/agent/http/expiry.js +88 -0
- package/lib/esm/agent/agent/http/expiry.js.map +1 -0
- package/lib/esm/agent/agent/http/index.d.ts +11 -2
- package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/index.js +145 -76
- package/lib/esm/agent/agent/http/index.js.map +1 -1
- package/lib/esm/agent/agent/http/transforms.d.ts +1 -35
- package/lib/esm/agent/agent/http/transforms.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/transforms.js +2 -87
- package/lib/esm/agent/agent/http/transforms.js.map +1 -1
- package/lib/esm/agent/agent/http/types.d.ts +1 -1
- package/lib/esm/agent/agent/http/types.d.ts.map +1 -1
- package/lib/esm/agent/auth.d.ts +1 -1
- package/lib/esm/agent/auth.d.ts.map +1 -1
- package/lib/esm/agent/canister-env/index.d.ts +2 -2
- package/lib/esm/agent/canister-env/index.d.ts.map +1 -1
- package/lib/esm/agent/canisterStatus/index.d.ts +5 -4
- package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/esm/agent/canisterStatus/index.js +6 -10
- package/lib/esm/agent/canisterStatus/index.js.map +1 -1
- package/lib/esm/agent/cbor.js +1 -1
- package/lib/esm/agent/cbor.js.map +1 -1
- package/lib/esm/agent/certificate.d.ts +4 -4
- package/lib/esm/agent/certificate.d.ts.map +1 -1
- package/lib/esm/agent/certificate.js +6 -10
- package/lib/esm/agent/certificate.js.map +1 -1
- package/lib/esm/agent/constants.d.ts +4 -4
- package/lib/esm/agent/constants.d.ts.map +1 -1
- package/lib/esm/agent/der.d.ts.map +1 -1
- package/lib/esm/agent/der.js +26 -21
- package/lib/esm/agent/der.js.map +1 -1
- package/lib/esm/agent/errors.d.ts +27 -18
- package/lib/esm/agent/errors.d.ts.map +1 -1
- package/lib/esm/agent/errors.js +16 -13
- package/lib/esm/agent/errors.js.map +1 -1
- package/lib/esm/agent/observable.d.ts +1 -1
- package/lib/esm/agent/observable.d.ts.map +1 -1
- package/lib/esm/agent/polling/backoff.d.ts +7 -7
- package/lib/esm/agent/polling/backoff.d.ts.map +1 -1
- package/lib/esm/agent/polling/backoff.js +3 -5
- package/lib/esm/agent/polling/backoff.js.map +1 -1
- package/lib/esm/agent/polling/index.d.ts +11 -10
- package/lib/esm/agent/polling/index.d.ts.map +1 -1
- package/lib/esm/agent/polling/index.js +9 -7
- package/lib/esm/agent/polling/index.js.map +1 -1
- package/lib/esm/agent/polling/strategy.d.ts +2 -5
- package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
- package/lib/esm/agent/polling/strategy.js.map +1 -1
- package/lib/esm/agent/polling/types.d.ts +18 -0
- package/lib/esm/agent/polling/types.d.ts.map +1 -0
- package/lib/esm/agent/polling/types.js +2 -0
- package/lib/esm/agent/polling/types.js.map +1 -0
- package/lib/esm/agent/public_key.d.ts +1 -1
- package/lib/esm/agent/public_key.d.ts.map +1 -1
- package/lib/esm/agent/request_id.d.ts.map +1 -1
- package/lib/esm/agent/request_id.js +7 -7
- package/lib/esm/agent/request_id.js.map +1 -1
- package/lib/esm/agent/subnetStatus/index.d.ts +5 -4
- package/lib/esm/agent/subnetStatus/index.d.ts.map +1 -1
- package/lib/esm/agent/subnetStatus/index.js +6 -10
- package/lib/esm/agent/subnetStatus/index.js.map +1 -1
- package/lib/esm/agent/utils/buffer.d.ts.map +1 -1
- package/lib/esm/agent/utils/buffer.js +4 -2
- package/lib/esm/agent/utils/buffer.js.map +1 -1
- package/lib/esm/agent/utils/certificateReject.d.ts +11 -0
- package/lib/esm/agent/utils/certificateReject.d.ts.map +1 -0
- package/lib/esm/agent/utils/certificateReject.js +16 -0
- package/lib/esm/agent/utils/certificateReject.js.map +1 -0
- package/lib/esm/agent/utils/expirableMap.d.ts +4 -4
- package/lib/esm/agent/utils/expirableMap.d.ts.map +1 -1
- package/lib/esm/agent/utils/expirableMap.js +2 -2
- package/lib/esm/agent/utils/readState.d.ts +3 -3
- package/lib/esm/agent/utils/readState.d.ts.map +1 -1
- package/lib/esm/candid/candid-core.d.ts +1 -1
- package/lib/esm/candid/candid-core.d.ts.map +1 -1
- package/lib/esm/candid/candid-core.js +7 -9
- package/lib/esm/candid/candid-core.js.map +1 -1
- package/lib/esm/candid/candid-ui.d.ts.map +1 -1
- package/lib/esm/candid/candid-ui.js +7 -23
- package/lib/esm/candid/candid-ui.js.map +1 -1
- package/lib/esm/candid/idl.d.ts +14 -1
- package/lib/esm/candid/idl.d.ts.map +1 -1
- package/lib/esm/candid/idl.js +162 -124
- package/lib/esm/candid/idl.js.map +1 -1
- package/lib/esm/candid/utils/leb128.d.ts.map +1 -1
- package/lib/esm/candid/utils/leb128.js +1 -3
- package/lib/esm/candid/utils/leb128.js.map +1 -1
- package/lib/esm/identity/identity/delegation.d.ts +4 -3
- package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
- package/lib/esm/identity/identity/delegation.js +3 -3
- package/lib/esm/identity/identity/delegation.js.map +1 -1
- package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
- package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
- package/lib/esm/identity/identity/ecdsa.js +2 -4
- package/lib/esm/identity/identity/ecdsa.js.map +1 -1
- package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
- package/lib/esm/identity/identity/ed25519.js +9 -10
- package/lib/esm/identity/identity/ed25519.js.map +1 -1
- package/lib/esm/identity/identity/partial.d.ts +1 -1
- package/lib/esm/identity/identity/partial.d.ts.map +1 -1
- package/lib/esm/identity/identity/webauthn.js +4 -5
- package/lib/esm/identity/identity/webauthn.js.map +1 -1
- package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/der.js +27 -22
- package/lib/esm/identity/secp256k1/der.js.map +1 -1
- package/lib/esm/identity/secp256k1/pem.js +1 -1
- package/lib/esm/identity/secp256k1/pem.js.map +1 -1
- package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/secp256k1.js +6 -6
- package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
- package/lib/esm/principal/principal.d.ts +2 -2
- package/lib/esm/principal/principal.d.ts.map +1 -1
- package/lib/esm/principal/principal.js +10 -6
- package/lib/esm/principal/principal.js.map +1 -1
- package/package.json +13 -13
- package/src/agent/actor.ts +74 -126
- package/src/agent/agent/api.ts +36 -6
- package/src/agent/agent/http/expiry.ts +107 -0
- package/src/agent/agent/http/index.ts +217 -90
- package/src/agent/agent/http/transforms.ts +2 -106
- package/src/agent/agent/http/types.ts +1 -1
- package/src/agent/auth.ts +1 -1
- package/src/agent/canister-env/index.ts +2 -2
- package/src/agent/canisterStatus/index.ts +10 -12
- package/src/agent/cbor.ts +1 -1
- package/src/agent/certificate.ts +11 -13
- package/src/agent/der.ts +40 -19
- package/src/agent/errors.ts +41 -31
- package/src/agent/fetch_candid.ts +1 -1
- package/src/agent/observable.ts +1 -1
- package/src/agent/polling/backoff.ts +9 -10
- package/src/agent/polling/index.ts +14 -26
- package/src/agent/polling/strategy.ts +5 -9
- package/src/agent/polling/types.ts +28 -0
- package/src/agent/public_key.ts +1 -1
- package/src/agent/request_id.ts +15 -8
- package/src/agent/subnetStatus/index.ts +10 -12
- package/src/agent/utils/buffer.ts +6 -2
- package/src/agent/utils/certificateReject.ts +25 -0
- package/src/agent/utils/expirableMap.ts +4 -4
- package/src/agent/utils/random.ts +2 -2
- package/src/agent/utils/readState.ts +3 -3
- package/src/candid/candid-core.ts +9 -11
- package/src/candid/candid-ui.ts +8 -16
- package/src/candid/idl.ts +210 -112
- package/src/candid/utils/leb128.ts +1 -2
- package/src/identity/identity/delegation.ts +5 -5
- package/src/identity/identity/ecdsa.ts +12 -8
- package/src/identity/identity/ed25519.ts +16 -9
- package/src/identity/identity/partial.ts +1 -1
- package/src/identity/identity/webauthn.ts +21 -21
- package/src/identity/secp256k1/der.ts +41 -20
- package/src/identity/secp256k1/pem.ts +1 -1
- package/src/identity/secp256k1/secp256k1.ts +12 -6
- package/src/principal/principal.ts +18 -8
- package/LICENSE +0 -201
package/src/candid/idl.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
3
|
import { Principal as PrincipalId } from '#principal';
|
|
4
|
-
import {
|
|
4
|
+
import type { JsonValue } from './types.ts';
|
|
5
5
|
import { concat, PipeArrayBuffer as Pipe, uint8ToDataView } from './utils/buffer.ts';
|
|
6
6
|
import { idlLabelToId } from './utils/hash.ts';
|
|
7
7
|
import {
|
|
@@ -24,6 +24,19 @@ import { iexp2 } from './utils/bigint-math.ts';
|
|
|
24
24
|
* as documented at https://github.com/dfinity/candid/blob/119703ba342d2fef6ab4972d2541b9fe36ae8e36/spec/Candid.md
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Error thrown when candid decoding fails due to a type mismatch
|
|
29
|
+
* between the expected and received types.
|
|
30
|
+
*/
|
|
31
|
+
export class CandidDecodeError extends Error {
|
|
32
|
+
public override name = 'CandidDecodeError';
|
|
33
|
+
|
|
34
|
+
constructor(message: string) {
|
|
35
|
+
super(message);
|
|
36
|
+
Object.setPrototypeOf(this, CandidDecodeError.prototype);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
27
40
|
enum IDLTypeIds {
|
|
28
41
|
Null = -1,
|
|
29
42
|
Bool = -2,
|
|
@@ -75,10 +88,10 @@ class TypeTable {
|
|
|
75
88
|
const idx = this._idx.get(obj.name);
|
|
76
89
|
const knotIdx = this._idx.get(knot);
|
|
77
90
|
if (idx === undefined) {
|
|
78
|
-
throw new Error(
|
|
91
|
+
throw new Error(`Missing type index for ${obj}`);
|
|
79
92
|
}
|
|
80
93
|
if (knotIdx === undefined) {
|
|
81
|
-
throw new Error(
|
|
94
|
+
throw new Error(`Missing type index for ${knot}`);
|
|
82
95
|
}
|
|
83
96
|
// Point the recursive placeholder (obj) to the concrete type bytes
|
|
84
97
|
this._typs[idx] = this._typs[knotIdx];
|
|
@@ -122,7 +135,7 @@ class TypeTable {
|
|
|
122
135
|
|
|
123
136
|
public indexOf(typeName: string): Uint8Array {
|
|
124
137
|
if (!this._idx.has(typeName)) {
|
|
125
|
-
throw new Error(
|
|
138
|
+
throw new Error(`Missing type index for ${typeName}`);
|
|
126
139
|
}
|
|
127
140
|
return slebEncode(this._idx.get(typeName) || 0);
|
|
128
141
|
}
|
|
@@ -286,7 +299,6 @@ export abstract class PrimitiveType<T = any> extends Type<T> {
|
|
|
286
299
|
|
|
287
300
|
public _buildTypeTableImpl(_typeTable: TypeTable): void {
|
|
288
301
|
// No type table encoding for Primitive types.
|
|
289
|
-
return;
|
|
290
302
|
}
|
|
291
303
|
}
|
|
292
304
|
|
|
@@ -295,7 +307,7 @@ export abstract class ConstructType<T = any> extends Type<T> {
|
|
|
295
307
|
if (t instanceof RecClass) {
|
|
296
308
|
const ty = t.getType();
|
|
297
309
|
if (typeof ty === 'undefined') {
|
|
298
|
-
throw new
|
|
310
|
+
throw new CandidDecodeError('Type mismatch: cannot decode from uninitialized recursive type');
|
|
299
311
|
}
|
|
300
312
|
return ty;
|
|
301
313
|
}
|
|
@@ -444,7 +456,9 @@ export class BoolClass extends PrimitiveType<boolean> {
|
|
|
444
456
|
}
|
|
445
457
|
|
|
446
458
|
public covariant(x: any): x is boolean {
|
|
447
|
-
if (typeof x === 'boolean')
|
|
459
|
+
if (typeof x === 'boolean') {
|
|
460
|
+
return true;
|
|
461
|
+
}
|
|
448
462
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
449
463
|
}
|
|
450
464
|
|
|
@@ -490,7 +504,9 @@ export class NullClass extends PrimitiveType<null> {
|
|
|
490
504
|
}
|
|
491
505
|
|
|
492
506
|
public covariant(x: any): x is null {
|
|
493
|
-
if (x === null)
|
|
507
|
+
if (x === null) {
|
|
508
|
+
return true;
|
|
509
|
+
}
|
|
494
510
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
495
511
|
}
|
|
496
512
|
|
|
@@ -569,7 +585,9 @@ export class TextClass extends PrimitiveType<string> {
|
|
|
569
585
|
}
|
|
570
586
|
|
|
571
587
|
public covariant(x: any): x is string {
|
|
572
|
-
if (typeof x === 'string')
|
|
588
|
+
if (typeof x === 'string') {
|
|
589
|
+
return true;
|
|
590
|
+
}
|
|
573
591
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
574
592
|
}
|
|
575
593
|
|
|
@@ -596,7 +614,7 @@ export class TextClass extends PrimitiveType<string> {
|
|
|
596
614
|
}
|
|
597
615
|
|
|
598
616
|
public valueToString(x: string) {
|
|
599
|
-
return
|
|
617
|
+
return `"${x}"`;
|
|
600
618
|
}
|
|
601
619
|
}
|
|
602
620
|
|
|
@@ -619,7 +637,9 @@ export class IntClass extends PrimitiveType<bigint> {
|
|
|
619
637
|
public covariant(x: any): x is bigint {
|
|
620
638
|
// We allow encoding of JavaScript plain numbers.
|
|
621
639
|
// But we will always decode to bigint.
|
|
622
|
-
if (typeof x === 'bigint' || Number.isInteger(x))
|
|
640
|
+
if (typeof x === 'bigint' || Number.isInteger(x)) {
|
|
641
|
+
return true;
|
|
642
|
+
}
|
|
623
643
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
624
644
|
}
|
|
625
645
|
|
|
@@ -664,7 +684,9 @@ export class NatClass extends PrimitiveType<bigint> {
|
|
|
664
684
|
public covariant(x: any): x is bigint {
|
|
665
685
|
// We allow encoding of JavaScript plain numbers.
|
|
666
686
|
// But we will always decode to bigint.
|
|
667
|
-
if ((typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0))
|
|
687
|
+
if ((typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0)) {
|
|
688
|
+
return true;
|
|
689
|
+
}
|
|
668
690
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
669
691
|
}
|
|
670
692
|
|
|
@@ -713,7 +735,9 @@ export class FloatClass extends PrimitiveType<number> {
|
|
|
713
735
|
}
|
|
714
736
|
|
|
715
737
|
public covariant(x: any): x is number {
|
|
716
|
-
if (typeof x === 'number' || x instanceof Number)
|
|
738
|
+
if (typeof x === 'number' || x instanceof Number) {
|
|
739
|
+
return true;
|
|
740
|
+
}
|
|
717
741
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
718
742
|
}
|
|
719
743
|
|
|
@@ -739,13 +763,12 @@ export class FloatClass extends PrimitiveType<number> {
|
|
|
739
763
|
const view = uint8ToDataView(bytes);
|
|
740
764
|
if (this._bits === 32) {
|
|
741
765
|
return view.getFloat32(0, true);
|
|
742
|
-
} else {
|
|
743
|
-
return view.getFloat64(0, true);
|
|
744
766
|
}
|
|
767
|
+
return view.getFloat64(0, true);
|
|
745
768
|
}
|
|
746
769
|
|
|
747
770
|
get name() {
|
|
748
|
-
return
|
|
771
|
+
return `float${this._bits}`;
|
|
749
772
|
}
|
|
750
773
|
|
|
751
774
|
public valueToString(x: number) {
|
|
@@ -776,17 +799,16 @@ export class FixedIntClass extends PrimitiveType<bigint | number> {
|
|
|
776
799
|
public covariant(x: any): x is bigint {
|
|
777
800
|
const min = iexp2(this._bits - 1) * BigInt(-1);
|
|
778
801
|
const max = iexp2(this._bits - 1) - BigInt(1);
|
|
779
|
-
let ok = false;
|
|
780
802
|
if (typeof x === 'bigint') {
|
|
781
|
-
|
|
803
|
+
if (x >= min && x <= max) {
|
|
804
|
+
return true;
|
|
805
|
+
}
|
|
782
806
|
} else if (Number.isInteger(x)) {
|
|
783
807
|
const v = BigInt(x);
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
808
|
+
if (v >= min && v <= max) {
|
|
809
|
+
return true;
|
|
810
|
+
}
|
|
787
811
|
}
|
|
788
|
-
|
|
789
|
-
if (ok) return true;
|
|
790
812
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
791
813
|
}
|
|
792
814
|
|
|
@@ -804,9 +826,8 @@ export class FixedIntClass extends PrimitiveType<bigint | number> {
|
|
|
804
826
|
const num = readIntLE(b, this._bits / 8);
|
|
805
827
|
if (this._bits <= 32) {
|
|
806
828
|
return Number(num);
|
|
807
|
-
} else {
|
|
808
|
-
return num;
|
|
809
829
|
}
|
|
830
|
+
return num;
|
|
810
831
|
}
|
|
811
832
|
|
|
812
833
|
get name() {
|
|
@@ -840,16 +861,16 @@ export class FixedNatClass extends PrimitiveType<bigint | number> {
|
|
|
840
861
|
|
|
841
862
|
public covariant(x: any): x is bigint {
|
|
842
863
|
const max = iexp2(this._bits);
|
|
843
|
-
let ok = false;
|
|
844
864
|
if (typeof x === 'bigint' && x >= BigInt(0)) {
|
|
845
|
-
|
|
865
|
+
if (x < max) {
|
|
866
|
+
return true;
|
|
867
|
+
}
|
|
846
868
|
} else if (Number.isInteger(x) && x >= 0) {
|
|
847
869
|
const v = BigInt(x);
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
870
|
+
if (v < max) {
|
|
871
|
+
return true;
|
|
872
|
+
}
|
|
851
873
|
}
|
|
852
|
-
if (ok) return true;
|
|
853
874
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
854
875
|
}
|
|
855
876
|
|
|
@@ -867,9 +888,8 @@ export class FixedNatClass extends PrimitiveType<bigint | number> {
|
|
|
867
888
|
const num = readUIntLE(b, this._bits / 8);
|
|
868
889
|
if (this._bits <= 32) {
|
|
869
890
|
return Number(num);
|
|
870
|
-
} else {
|
|
871
|
-
return num;
|
|
872
891
|
}
|
|
892
|
+
return num;
|
|
873
893
|
}
|
|
874
894
|
|
|
875
895
|
get name() {
|
|
@@ -932,11 +952,15 @@ export class VecClass<T> extends ConstructType<T[]> {
|
|
|
932
952
|
try {
|
|
933
953
|
return this._type.covariant(v);
|
|
934
954
|
} catch (e: any) {
|
|
935
|
-
throw new Error(
|
|
955
|
+
throw new Error(
|
|
956
|
+
`Invalid ${this.display()} argument: \n\nindex ${idx} -> ${e.message}`,
|
|
957
|
+
{ cause: e },
|
|
958
|
+
);
|
|
936
959
|
}
|
|
937
960
|
}))
|
|
938
|
-
)
|
|
961
|
+
) {
|
|
939
962
|
return true;
|
|
963
|
+
}
|
|
940
964
|
|
|
941
965
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
942
966
|
}
|
|
@@ -959,7 +983,8 @@ export class VecClass<T> extends ConstructType<T[]> {
|
|
|
959
983
|
}
|
|
960
984
|
}
|
|
961
985
|
return concat(len, new Uint8Array(buffer.buffer));
|
|
962
|
-
}
|
|
986
|
+
}
|
|
987
|
+
if (x instanceof Int32Array || x instanceof Uint32Array) {
|
|
963
988
|
const buffer = new DataView(new ArrayBuffer(x.length * 4));
|
|
964
989
|
for (let i = 0; i < x.length; i++) {
|
|
965
990
|
if (x instanceof Int32Array) {
|
|
@@ -969,7 +994,8 @@ export class VecClass<T> extends ConstructType<T[]> {
|
|
|
969
994
|
}
|
|
970
995
|
}
|
|
971
996
|
return concat(len, new Uint8Array(buffer.buffer));
|
|
972
|
-
}
|
|
997
|
+
}
|
|
998
|
+
if (x instanceof BigInt64Array || x instanceof BigUint64Array) {
|
|
973
999
|
const buffer = new DataView(new ArrayBuffer(x.length * 8));
|
|
974
1000
|
for (let i = 0; i < x.length; i++) {
|
|
975
1001
|
if (x instanceof BigInt64Array) {
|
|
@@ -979,10 +1005,9 @@ export class VecClass<T> extends ConstructType<T[]> {
|
|
|
979
1005
|
}
|
|
980
1006
|
}
|
|
981
1007
|
return concat(len, new Uint8Array(buffer.buffer));
|
|
982
|
-
} else {
|
|
983
|
-
// For Uint8Array, Int8Array, etc. that don't have endianness concerns
|
|
984
|
-
return concat(len, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
985
1008
|
}
|
|
1009
|
+
// For Uint8Array, Int8Array, etc. that don't have endianness concerns
|
|
1010
|
+
return concat(len, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
986
1011
|
}
|
|
987
1012
|
const buf = new Pipe(new Uint8Array(len.byteLength + x.length), 0);
|
|
988
1013
|
buf.write(len);
|
|
@@ -1004,7 +1029,7 @@ export class VecClass<T> extends ConstructType<T[]> {
|
|
|
1004
1029
|
public decodeValue(b: Pipe, t: Type): T[] {
|
|
1005
1030
|
const vec = this.checkType(t);
|
|
1006
1031
|
if (!(vec instanceof VecClass)) {
|
|
1007
|
-
throw new
|
|
1032
|
+
throw new CandidDecodeError(`Expected type '${this.display()}', but received non-vector type '${vec.display()}'`);
|
|
1008
1033
|
}
|
|
1009
1034
|
const len = Number(lebDecode(b));
|
|
1010
1035
|
|
|
@@ -1084,7 +1109,7 @@ export class VecClass<T> extends ConstructType<T[]> {
|
|
|
1084
1109
|
|
|
1085
1110
|
public valueToString(x: T[]) {
|
|
1086
1111
|
const elements = x.map(e => this._type.valueToString(e));
|
|
1087
|
-
return
|
|
1112
|
+
return `vec {${elements.join('; ')}}`;
|
|
1088
1113
|
}
|
|
1089
1114
|
}
|
|
1090
1115
|
|
|
@@ -1111,11 +1136,13 @@ export class OptClass<T> extends ConstructType<[T] | []> {
|
|
|
1111
1136
|
|
|
1112
1137
|
public covariant(x: any): x is [T] | [] {
|
|
1113
1138
|
try {
|
|
1114
|
-
if (Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0]))))
|
|
1139
|
+
if (Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0])))) {
|
|
1115
1140
|
return true;
|
|
1141
|
+
}
|
|
1116
1142
|
} catch (e: any) {
|
|
1117
1143
|
throw new Error(
|
|
1118
1144
|
`Invalid ${this.display()} argument: ${toReadableString(x)} \n\n-> ${e.message}`,
|
|
1145
|
+
{ cause: e },
|
|
1119
1146
|
);
|
|
1120
1147
|
}
|
|
1121
1148
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
@@ -1124,9 +1151,8 @@ export class OptClass<T> extends ConstructType<[T] | []> {
|
|
|
1124
1151
|
public encodeValue(x: [T] | []): Uint8Array {
|
|
1125
1152
|
if (x.length === 0) {
|
|
1126
1153
|
return new Uint8Array([0]);
|
|
1127
|
-
} else {
|
|
1128
|
-
return concat(new Uint8Array([1]), this._type.encodeValue(x[0]));
|
|
1129
1154
|
}
|
|
1155
|
+
return concat(new Uint8Array([1]), this._type.encodeValue(x[0]));
|
|
1130
1156
|
}
|
|
1131
1157
|
|
|
1132
1158
|
public _buildTypeTableImpl(typeTable: TypeTable) {
|
|
@@ -1151,8 +1177,10 @@ export class OptClass<T> extends ConstructType<[T] | []> {
|
|
|
1151
1177
|
if (t instanceof RecClass) {
|
|
1152
1178
|
const ty = t.getType();
|
|
1153
1179
|
if (typeof ty === 'undefined') {
|
|
1154
|
-
throw new
|
|
1155
|
-
} else
|
|
1180
|
+
throw new CandidDecodeError('Type mismatch: cannot decode from uninitialized recursive type');
|
|
1181
|
+
} else {
|
|
1182
|
+
wireType = ty;
|
|
1183
|
+
}
|
|
1156
1184
|
}
|
|
1157
1185
|
|
|
1158
1186
|
if (wireType instanceof OptClass) {
|
|
@@ -1176,7 +1204,7 @@ export class OptClass<T> extends ConstructType<[T] | []> {
|
|
|
1176
1204
|
}
|
|
1177
1205
|
}
|
|
1178
1206
|
default:
|
|
1179
|
-
throw new
|
|
1207
|
+
throw new CandidDecodeError(`Invalid option value: expected 0 (None) or 1 (Some) for type '${this.display()}'`);
|
|
1180
1208
|
}
|
|
1181
1209
|
} else if (
|
|
1182
1210
|
// this check corresponds to `not (null <: <t>)` in the spec
|
|
@@ -1217,9 +1245,8 @@ export class OptClass<T> extends ConstructType<[T] | []> {
|
|
|
1217
1245
|
public valueToString(x: [T] | []) {
|
|
1218
1246
|
if (x.length === 0) {
|
|
1219
1247
|
return 'null';
|
|
1220
|
-
} else {
|
|
1221
|
-
return `opt ${this._type.valueToString(x[0])}`;
|
|
1222
1248
|
}
|
|
1249
|
+
return `opt ${this._type.valueToString(x[0])}`;
|
|
1223
1250
|
}
|
|
1224
1251
|
}
|
|
1225
1252
|
|
|
@@ -1273,11 +1300,14 @@ export class RecordClass extends ConstructType<Record<string, any>> {
|
|
|
1273
1300
|
try {
|
|
1274
1301
|
return t.covariant(x[k]);
|
|
1275
1302
|
} catch (e: any) {
|
|
1276
|
-
throw new Error(`Invalid ${this.display()} argument: \n\nfield ${k} -> ${e.message}
|
|
1303
|
+
throw new Error(`Invalid ${this.display()} argument: \n\nfield ${k} -> ${e.message}`, {
|
|
1304
|
+
cause: e,
|
|
1305
|
+
});
|
|
1277
1306
|
}
|
|
1278
1307
|
})
|
|
1279
|
-
)
|
|
1308
|
+
) {
|
|
1280
1309
|
return true;
|
|
1310
|
+
}
|
|
1281
1311
|
|
|
1282
1312
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1283
1313
|
}
|
|
@@ -1302,7 +1332,7 @@ export class RecordClass extends ConstructType<Record<string, any>> {
|
|
|
1302
1332
|
public decodeValue(b: Pipe, t: Type) {
|
|
1303
1333
|
const record = this.checkType(t);
|
|
1304
1334
|
if (!(record instanceof RecordClass)) {
|
|
1305
|
-
throw new
|
|
1335
|
+
throw new CandidDecodeError(`Expected type '${this.display()}', but received non-record type '${record.display()}'`);
|
|
1306
1336
|
}
|
|
1307
1337
|
const x: Record<string, any> = {};
|
|
1308
1338
|
|
|
@@ -1332,7 +1362,11 @@ export class RecordClass extends ConstructType<Record<string, any>> {
|
|
|
1332
1362
|
x[expectKey] = [];
|
|
1333
1363
|
expectedRecordIdx++;
|
|
1334
1364
|
} else {
|
|
1335
|
-
|
|
1365
|
+
const expectedFields = this._fields.map(([name]) => name).join(', ');
|
|
1366
|
+
const receivedFields = record._fields.map(([name]) => name).join(', ');
|
|
1367
|
+
throw new CandidDecodeError(
|
|
1368
|
+
`Cannot find required field '${expectKey}' in record, expected fields: [${expectedFields}], received fields: [${receivedFields}]`,
|
|
1369
|
+
);
|
|
1336
1370
|
}
|
|
1337
1371
|
} else {
|
|
1338
1372
|
// The field on the wire does not exist in the output type, so we can skip it
|
|
@@ -1347,7 +1381,11 @@ export class RecordClass extends ConstructType<Record<string, any>> {
|
|
|
1347
1381
|
// TODO this assumes null value in opt is represented as []
|
|
1348
1382
|
x[expectKey] = [];
|
|
1349
1383
|
} else {
|
|
1350
|
-
|
|
1384
|
+
const expectedFields = this._fields.map(([name]) => name).join(', ');
|
|
1385
|
+
const receivedFields = record._fields.map(([name]) => name).join(', ');
|
|
1386
|
+
throw new CandidDecodeError(
|
|
1387
|
+
`Cannot find required field '${expectKey}' in record, expected fields: [${expectedFields}], received fields: [${receivedFields}]`,
|
|
1388
|
+
);
|
|
1351
1389
|
}
|
|
1352
1390
|
}
|
|
1353
1391
|
return x;
|
|
@@ -1362,18 +1400,18 @@ export class RecordClass extends ConstructType<Record<string, any>> {
|
|
|
1362
1400
|
}
|
|
1363
1401
|
|
|
1364
1402
|
get name() {
|
|
1365
|
-
const fields = this._fields.map(([key, value]) => key
|
|
1403
|
+
const fields = this._fields.map(([key, value]) => `${key}:${value.name}`);
|
|
1366
1404
|
return `record {${fields.join('; ')}}`;
|
|
1367
1405
|
}
|
|
1368
1406
|
|
|
1369
1407
|
public display() {
|
|
1370
|
-
const fields = this._fields.map(([key, value]) => key
|
|
1408
|
+
const fields = this._fields.map(([key, value]) => `${key}:${value.display()}`);
|
|
1371
1409
|
return `record {${fields.join('; ')}}`;
|
|
1372
1410
|
}
|
|
1373
1411
|
|
|
1374
1412
|
public valueToString(x: Record<string, any>) {
|
|
1375
1413
|
const values = this._fields.map(([key]) => x[key]);
|
|
1376
|
-
const fields = zipWith(this._fields, values, ([k, c], d) => k
|
|
1414
|
+
const fields = zipWith(this._fields, values, ([k, c], d) => `${k}=${c.valueToString(d)}`);
|
|
1377
1415
|
return `record {${fields.join('; ')}}`;
|
|
1378
1416
|
}
|
|
1379
1417
|
}
|
|
@@ -1395,7 +1433,7 @@ export class TupleClass<T extends any[]> extends RecordClass {
|
|
|
1395
1433
|
|
|
1396
1434
|
constructor(_components: Type[]) {
|
|
1397
1435
|
const x: Record<string, any> = {};
|
|
1398
|
-
_components.forEach((e, i) => (x[
|
|
1436
|
+
_components.forEach((e, i) => (x[`_${i}_`] = e));
|
|
1399
1437
|
super(x);
|
|
1400
1438
|
this._components = _components;
|
|
1401
1439
|
}
|
|
@@ -1414,11 +1452,14 @@ export class TupleClass<T extends any[]> extends RecordClass {
|
|
|
1414
1452
|
try {
|
|
1415
1453
|
return t.covariant(x[i]);
|
|
1416
1454
|
} catch (e: any) {
|
|
1417
|
-
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${i} -> ${e.message}
|
|
1455
|
+
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${i} -> ${e.message}`, {
|
|
1456
|
+
cause: e,
|
|
1457
|
+
});
|
|
1418
1458
|
}
|
|
1419
1459
|
})
|
|
1420
|
-
)
|
|
1460
|
+
) {
|
|
1421
1461
|
return true;
|
|
1462
|
+
}
|
|
1422
1463
|
|
|
1423
1464
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1424
1465
|
}
|
|
@@ -1431,10 +1472,12 @@ export class TupleClass<T extends any[]> extends RecordClass {
|
|
|
1431
1472
|
public decodeValue(b: Pipe, t: Type): T {
|
|
1432
1473
|
const tuple = this.checkType(t);
|
|
1433
1474
|
if (!(tuple instanceof TupleClass)) {
|
|
1434
|
-
throw new
|
|
1475
|
+
throw new CandidDecodeError(`Expected type '${this.display()}', but received non-tuple type '${tuple.display()}'`);
|
|
1435
1476
|
}
|
|
1436
1477
|
if (tuple._components.length < this._components.length) {
|
|
1437
|
-
throw new
|
|
1478
|
+
throw new CandidDecodeError(
|
|
1479
|
+
`Tuple mismatch: expected ${this._components.length} components, but received ${tuple._components.length}`,
|
|
1480
|
+
);
|
|
1438
1481
|
}
|
|
1439
1482
|
const res = [];
|
|
1440
1483
|
for (const [i, wireType] of tuple._components.entries()) {
|
|
@@ -1492,11 +1535,14 @@ export class VariantClass extends ConstructType<Record<string, any>> {
|
|
|
1492
1535
|
// eslint-disable-next-line
|
|
1493
1536
|
return !x.hasOwnProperty(k) || v.covariant(x[k]);
|
|
1494
1537
|
} catch (e: any) {
|
|
1495
|
-
throw new Error(`Invalid ${this.display()} argument: \n\nvariant ${k} -> ${e.message}
|
|
1538
|
+
throw new Error(`Invalid ${this.display()} argument: \n\nvariant ${k} -> ${e.message}`, {
|
|
1539
|
+
cause: e,
|
|
1540
|
+
});
|
|
1496
1541
|
}
|
|
1497
1542
|
})
|
|
1498
|
-
)
|
|
1543
|
+
) {
|
|
1499
1544
|
return true;
|
|
1545
|
+
}
|
|
1500
1546
|
|
|
1501
1547
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1502
1548
|
}
|
|
@@ -1512,7 +1558,7 @@ export class VariantClass extends ConstructType<Record<string, any>> {
|
|
|
1512
1558
|
return concat(idx, buf);
|
|
1513
1559
|
}
|
|
1514
1560
|
}
|
|
1515
|
-
throw Error(
|
|
1561
|
+
throw Error(`Variant has no data: ${x}`);
|
|
1516
1562
|
}
|
|
1517
1563
|
|
|
1518
1564
|
public _buildTypeTableImpl(typeTable: TypeTable) {
|
|
@@ -1530,11 +1576,11 @@ export class VariantClass extends ConstructType<Record<string, any>> {
|
|
|
1530
1576
|
public decodeValue(b: Pipe, t: Type) {
|
|
1531
1577
|
const variant = this.checkType(t);
|
|
1532
1578
|
if (!(variant instanceof VariantClass)) {
|
|
1533
|
-
throw new
|
|
1579
|
+
throw new CandidDecodeError(`Expected type '${this.display()}', but received non-variant type '${variant.display()}'`);
|
|
1534
1580
|
}
|
|
1535
1581
|
const idx = Number(lebDecode(b));
|
|
1536
1582
|
if (idx >= variant._fields.length) {
|
|
1537
|
-
throw Error(
|
|
1583
|
+
throw Error(`Invalid variant index: ${idx}`);
|
|
1538
1584
|
}
|
|
1539
1585
|
const [wireHash, wireType] = variant._fields[idx];
|
|
1540
1586
|
for (const [key, expectType] of this._fields) {
|
|
@@ -1543,11 +1589,14 @@ export class VariantClass extends ConstructType<Record<string, any>> {
|
|
|
1543
1589
|
return { [key]: value };
|
|
1544
1590
|
}
|
|
1545
1591
|
}
|
|
1546
|
-
|
|
1592
|
+
const expectedFields = this._fields.map(([name]) => name).join(', ');
|
|
1593
|
+
throw new CandidDecodeError(
|
|
1594
|
+
`Cannot find field hash ${wireHash} in variant, expected fields: [${expectedFields}]`,
|
|
1595
|
+
);
|
|
1547
1596
|
}
|
|
1548
1597
|
|
|
1549
1598
|
get name() {
|
|
1550
|
-
const fields = this._fields.map(([key, type]) => key
|
|
1599
|
+
const fields = this._fields.map(([key, type]) => `${key}:${type.name}`);
|
|
1551
1600
|
return `variant {${fields.join('; ')}}`;
|
|
1552
1601
|
}
|
|
1553
1602
|
|
|
@@ -1565,12 +1614,11 @@ export class VariantClass extends ConstructType<Record<string, any>> {
|
|
|
1565
1614
|
const value = type.valueToString(x[name]);
|
|
1566
1615
|
if (value === 'null') {
|
|
1567
1616
|
return `variant {${name}}`;
|
|
1568
|
-
} else {
|
|
1569
|
-
return `variant {${name}=${value}}`;
|
|
1570
1617
|
}
|
|
1618
|
+
return `variant {${name}=${value}}`;
|
|
1571
1619
|
}
|
|
1572
1620
|
}
|
|
1573
|
-
throw new Error(
|
|
1621
|
+
throw new Error(`Variant has no data: ${x}`);
|
|
1574
1622
|
}
|
|
1575
1623
|
|
|
1576
1624
|
get alternativesAsObject(): Record<number, Type> {
|
|
@@ -1615,7 +1663,9 @@ export class RecClass<T = any> extends ConstructType<T> {
|
|
|
1615
1663
|
}
|
|
1616
1664
|
|
|
1617
1665
|
public covariant(x: any): x is T {
|
|
1618
|
-
if (this._type ? this._type.covariant(x) : false)
|
|
1666
|
+
if (this._type ? this._type.covariant(x) : false) {
|
|
1667
|
+
return true;
|
|
1668
|
+
}
|
|
1619
1669
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1620
1670
|
}
|
|
1621
1671
|
|
|
@@ -1688,7 +1738,9 @@ export class PrincipalClass extends PrimitiveType<PrincipalId> {
|
|
|
1688
1738
|
}
|
|
1689
1739
|
|
|
1690
1740
|
public covariant(x: any): x is PrincipalId {
|
|
1691
|
-
if (x && x._isPrincipal)
|
|
1741
|
+
if (x && x._isPrincipal) {
|
|
1742
|
+
return true;
|
|
1743
|
+
}
|
|
1692
1744
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1693
1745
|
}
|
|
1694
1746
|
|
|
@@ -1746,7 +1798,7 @@ export class FuncClass<
|
|
|
1746
1798
|
if (types.length !== v.length) {
|
|
1747
1799
|
throw new Error('arity mismatch');
|
|
1748
1800
|
}
|
|
1749
|
-
return
|
|
1801
|
+
return `(${types.map((t, i) => t.valueToString(v[i])).join(', ')})`;
|
|
1750
1802
|
}
|
|
1751
1803
|
|
|
1752
1804
|
constructor(
|
|
@@ -1761,8 +1813,15 @@ export class FuncClass<
|
|
|
1761
1813
|
return v.visitFunc(this, d);
|
|
1762
1814
|
}
|
|
1763
1815
|
public covariant(x: any): x is [PrincipalId, string] {
|
|
1764
|
-
if (
|
|
1816
|
+
if (
|
|
1817
|
+
Array.isArray(x) &&
|
|
1818
|
+
x.length === 2 &&
|
|
1819
|
+
x[0] &&
|
|
1820
|
+
x[0]._isPrincipal &&
|
|
1821
|
+
typeof x[1] === 'string'
|
|
1822
|
+
) {
|
|
1765
1823
|
return true;
|
|
1824
|
+
}
|
|
1766
1825
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1767
1826
|
}
|
|
1768
1827
|
|
|
@@ -1815,7 +1874,7 @@ export class FuncClass<
|
|
|
1815
1874
|
get name() {
|
|
1816
1875
|
const args = this.argTypes.map(arg => arg.name).join(', ');
|
|
1817
1876
|
const rets = this.retTypes.map(arg => arg.name).join(', ');
|
|
1818
|
-
const annon =
|
|
1877
|
+
const annon = ` ${this.annotations.join(' ')}`;
|
|
1819
1878
|
return `(${args}) -> (${rets})${annon}`;
|
|
1820
1879
|
}
|
|
1821
1880
|
|
|
@@ -1826,20 +1885,21 @@ export class FuncClass<
|
|
|
1826
1885
|
public display(): string {
|
|
1827
1886
|
const args = this.argTypes.map(arg => arg.display()).join(', ');
|
|
1828
1887
|
const rets = this.retTypes.map(arg => arg.display()).join(', ');
|
|
1829
|
-
const annon =
|
|
1888
|
+
const annon = ` ${this.annotations.join(' ')}`;
|
|
1830
1889
|
return `(${args}) → (${rets})${annon}`;
|
|
1831
1890
|
}
|
|
1832
1891
|
|
|
1833
1892
|
private encodeAnnotation(ann: string): Uint8Array {
|
|
1834
1893
|
if (ann === 'query') {
|
|
1835
1894
|
return new Uint8Array([1]);
|
|
1836
|
-
}
|
|
1895
|
+
}
|
|
1896
|
+
if (ann === 'oneway') {
|
|
1837
1897
|
return new Uint8Array([2]);
|
|
1838
|
-
}
|
|
1898
|
+
}
|
|
1899
|
+
if (ann === 'composite_query') {
|
|
1839
1900
|
return new Uint8Array([3]);
|
|
1840
|
-
} else {
|
|
1841
|
-
throw new Error('Illegal function annotation');
|
|
1842
1901
|
}
|
|
1902
|
+
throw new Error('Illegal function annotation');
|
|
1843
1903
|
}
|
|
1844
1904
|
}
|
|
1845
1905
|
|
|
@@ -1877,7 +1937,9 @@ export class ServiceClass<
|
|
|
1877
1937
|
return v.visitService(this, d);
|
|
1878
1938
|
}
|
|
1879
1939
|
public covariant(x: any): x is PrincipalId {
|
|
1880
|
-
if (x && x._isPrincipal)
|
|
1940
|
+
if (x && x._isPrincipal) {
|
|
1941
|
+
return true;
|
|
1942
|
+
}
|
|
1881
1943
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1882
1944
|
}
|
|
1883
1945
|
|
|
@@ -1910,7 +1972,7 @@ export class ServiceClass<
|
|
|
1910
1972
|
return decodePrincipalId(b);
|
|
1911
1973
|
}
|
|
1912
1974
|
get name() {
|
|
1913
|
-
const fields = this._fields.map(([key, value]) => key
|
|
1975
|
+
const fields = this._fields.map(([key, value]) => `${key}:${value.name}`);
|
|
1914
1976
|
return `service {${fields.join('; ')}}`;
|
|
1915
1977
|
}
|
|
1916
1978
|
|
|
@@ -1938,7 +2000,7 @@ function toReadableString(x: unknown): string {
|
|
|
1938
2000
|
);
|
|
1939
2001
|
|
|
1940
2002
|
return str && str.length > toReadableString_max
|
|
1941
|
-
? str.substring(0, toReadableString_max - 3)
|
|
2003
|
+
? `${str.substring(0, toReadableString_max - 3)}...`
|
|
1942
2004
|
: str;
|
|
1943
2005
|
}
|
|
1944
2006
|
|
|
@@ -1964,7 +2026,7 @@ export function encode(argTypes: Array<Type<any>>, args: any[]): Uint8Array {
|
|
|
1964
2026
|
try {
|
|
1965
2027
|
t.covariant(x);
|
|
1966
2028
|
} catch (e: any) {
|
|
1967
|
-
const err = new Error(e.message
|
|
2029
|
+
const err = new Error(`${e.message}\n\n`);
|
|
1968
2030
|
throw err;
|
|
1969
2031
|
}
|
|
1970
2032
|
|
|
@@ -1990,7 +2052,7 @@ export function decode(retTypes: Type[], bytes: Uint8Array): JsonValue[] {
|
|
|
1990
2052
|
const magicBuffer = safeRead(b, magicNumber.length);
|
|
1991
2053
|
const magic = new TextDecoder().decode(magicBuffer);
|
|
1992
2054
|
if (magic !== magicNumber) {
|
|
1993
|
-
throw new Error(
|
|
2055
|
+
throw new Error(`Wrong magic number: ${JSON.stringify(magic)}`);
|
|
1994
2056
|
}
|
|
1995
2057
|
|
|
1996
2058
|
function readTypeTable(pipe: Pipe): [Array<[IDLTypeIds, any]>, number[]] {
|
|
@@ -2074,7 +2136,7 @@ export function decode(retTypes: Type[], bytes: Uint8Array): JsonValue[] {
|
|
|
2074
2136
|
break;
|
|
2075
2137
|
}
|
|
2076
2138
|
default:
|
|
2077
|
-
throw new Error(
|
|
2139
|
+
throw new Error(`Illegal op_code: ${ty}`);
|
|
2078
2140
|
}
|
|
2079
2141
|
}
|
|
2080
2142
|
|
|
@@ -2134,7 +2196,7 @@ export function decode(retTypes: Type[], bytes: Uint8Array): JsonValue[] {
|
|
|
2134
2196
|
case -24:
|
|
2135
2197
|
return Principal;
|
|
2136
2198
|
default:
|
|
2137
|
-
throw new Error(
|
|
2199
|
+
throw new Error(`Illegal op_code: ${t}`);
|
|
2138
2200
|
}
|
|
2139
2201
|
}
|
|
2140
2202
|
if (t >= rawTable.length) {
|
|
@@ -2162,9 +2224,8 @@ export function decode(retTypes: Type[], bytes: Uint8Array): JsonValue[] {
|
|
|
2162
2224
|
const tuple = record.tryAsTuple();
|
|
2163
2225
|
if (Array.isArray(tuple)) {
|
|
2164
2226
|
return Tuple(...tuple);
|
|
2165
|
-
} else {
|
|
2166
|
-
return record;
|
|
2167
2227
|
}
|
|
2228
|
+
return record;
|
|
2168
2229
|
}
|
|
2169
2230
|
case IDLTypeIds.Variant: {
|
|
2170
2231
|
const fields: Record<string, Type> = {};
|
|
@@ -2200,7 +2261,7 @@ export function decode(retTypes: Type[], bytes: Uint8Array): JsonValue[] {
|
|
|
2200
2261
|
return Service(rec);
|
|
2201
2262
|
}
|
|
2202
2263
|
default:
|
|
2203
|
-
throw new Error(
|
|
2264
|
+
throw new Error(`Illegal op_code: ${entry[0]}`);
|
|
2204
2265
|
}
|
|
2205
2266
|
}
|
|
2206
2267
|
|
|
@@ -2455,7 +2516,9 @@ function eqFunctionAnnotations(t1: FuncClass, t2: FuncClass): boolean {
|
|
|
2455
2516
|
return false;
|
|
2456
2517
|
}
|
|
2457
2518
|
for (const a of t1Annotations) {
|
|
2458
|
-
if (!t2Annotations.has(a))
|
|
2519
|
+
if (!t2Annotations.has(a)) {
|
|
2520
|
+
return false;
|
|
2521
|
+
}
|
|
2459
2522
|
}
|
|
2460
2523
|
return true;
|
|
2461
2524
|
}
|
|
@@ -2481,46 +2544,73 @@ export function subtype(t1: Type, t2: Type): boolean {
|
|
|
2481
2544
|
}
|
|
2482
2545
|
|
|
2483
2546
|
function subtype_(relations: Relations, t1: Type, t2: Type): boolean {
|
|
2484
|
-
if (t1.name === t2.name)
|
|
2547
|
+
if (t1.name === t2.name) {
|
|
2548
|
+
return true;
|
|
2549
|
+
}
|
|
2485
2550
|
const known = relations.known(t1, t2);
|
|
2486
|
-
if (known !== undefined)
|
|
2551
|
+
if (known !== undefined) {
|
|
2552
|
+
return known;
|
|
2553
|
+
}
|
|
2487
2554
|
relations.add(t1, t2);
|
|
2488
2555
|
|
|
2489
|
-
if (t2 instanceof ReservedClass)
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
if (t1 instanceof
|
|
2556
|
+
if (t2 instanceof ReservedClass) {
|
|
2557
|
+
return true;
|
|
2558
|
+
}
|
|
2559
|
+
if (t1 instanceof EmptyClass) {
|
|
2560
|
+
return true;
|
|
2561
|
+
}
|
|
2562
|
+
if (t1 instanceof NatClass && t2 instanceof IntClass) {
|
|
2563
|
+
return true;
|
|
2564
|
+
}
|
|
2565
|
+
if (t1 instanceof VecClass && t2 instanceof VecClass) {
|
|
2493
2566
|
return subtype_(relations, t1._type, t2._type);
|
|
2494
|
-
|
|
2567
|
+
}
|
|
2568
|
+
if (t2 instanceof OptClass) {
|
|
2569
|
+
return true;
|
|
2570
|
+
}
|
|
2495
2571
|
if (t1 instanceof RecordClass && t2 instanceof RecordClass) {
|
|
2496
2572
|
const t1Object = t1.fieldsAsObject;
|
|
2497
2573
|
for (const [label, ty2] of t2._fields) {
|
|
2498
2574
|
const ty1 = t1Object[idlLabelToId(label)];
|
|
2499
2575
|
if (!ty1) {
|
|
2500
|
-
if (!canBeOmmitted(ty2))
|
|
2576
|
+
if (!canBeOmmitted(ty2)) {
|
|
2577
|
+
return false;
|
|
2578
|
+
}
|
|
2501
2579
|
} else {
|
|
2502
|
-
if (!subtype_(relations, ty1, ty2))
|
|
2580
|
+
if (!subtype_(relations, ty1, ty2)) {
|
|
2581
|
+
return false;
|
|
2582
|
+
}
|
|
2503
2583
|
}
|
|
2504
2584
|
}
|
|
2505
2585
|
return true;
|
|
2506
2586
|
}
|
|
2507
2587
|
|
|
2508
2588
|
if (t1 instanceof FuncClass && t2 instanceof FuncClass) {
|
|
2509
|
-
if (!eqFunctionAnnotations(t1, t2))
|
|
2589
|
+
if (!eqFunctionAnnotations(t1, t2)) {
|
|
2590
|
+
return false;
|
|
2591
|
+
}
|
|
2510
2592
|
for (let i = 0; i < t1.argTypes.length; i++) {
|
|
2511
2593
|
const argTy1 = t1.argTypes[i];
|
|
2512
2594
|
if (i < t2.argTypes.length) {
|
|
2513
|
-
if (!subtype_(relations, t2.argTypes[i], argTy1))
|
|
2595
|
+
if (!subtype_(relations, t2.argTypes[i], argTy1)) {
|
|
2596
|
+
return false;
|
|
2597
|
+
}
|
|
2514
2598
|
} else {
|
|
2515
|
-
if (!canBeOmmitted(argTy1))
|
|
2599
|
+
if (!canBeOmmitted(argTy1)) {
|
|
2600
|
+
return false;
|
|
2601
|
+
}
|
|
2516
2602
|
}
|
|
2517
2603
|
}
|
|
2518
2604
|
for (let i = 0; i < t2.retTypes.length; i++) {
|
|
2519
2605
|
const retTy2 = t2.retTypes[i];
|
|
2520
2606
|
if (i < t1.retTypes.length) {
|
|
2521
|
-
if (!subtype_(relations, t1.retTypes[i], retTy2))
|
|
2607
|
+
if (!subtype_(relations, t1.retTypes[i], retTy2)) {
|
|
2608
|
+
return false;
|
|
2609
|
+
}
|
|
2522
2610
|
} else {
|
|
2523
|
-
if (!canBeOmmitted(retTy2))
|
|
2611
|
+
if (!canBeOmmitted(retTy2)) {
|
|
2612
|
+
return false;
|
|
2613
|
+
}
|
|
2524
2614
|
}
|
|
2525
2615
|
}
|
|
2526
2616
|
return true;
|
|
@@ -2530,8 +2620,12 @@ function subtype_(relations: Relations, t1: Type, t2: Type): boolean {
|
|
|
2530
2620
|
const t2Object = t2.alternativesAsObject;
|
|
2531
2621
|
for (const [label, ty1] of t1._fields) {
|
|
2532
2622
|
const ty2 = t2Object[idlLabelToId(label)];
|
|
2533
|
-
if (!ty2)
|
|
2534
|
-
|
|
2623
|
+
if (!ty2) {
|
|
2624
|
+
return false;
|
|
2625
|
+
}
|
|
2626
|
+
if (!subtype_(relations, ty1, ty2)) {
|
|
2627
|
+
return false;
|
|
2628
|
+
}
|
|
2535
2629
|
}
|
|
2536
2630
|
return true;
|
|
2537
2631
|
}
|
|
@@ -2540,8 +2634,12 @@ function subtype_(relations: Relations, t1: Type, t2: Type): boolean {
|
|
|
2540
2634
|
const t1Object = t1.fieldsAsObject();
|
|
2541
2635
|
for (const [name, ty2] of t2._fields) {
|
|
2542
2636
|
const ty1 = t1Object[name];
|
|
2543
|
-
if (!ty1)
|
|
2544
|
-
|
|
2637
|
+
if (!ty1) {
|
|
2638
|
+
return false;
|
|
2639
|
+
}
|
|
2640
|
+
if (!subtype_(relations, ty1, ty2)) {
|
|
2641
|
+
return false;
|
|
2642
|
+
}
|
|
2545
2643
|
}
|
|
2546
2644
|
return true;
|
|
2547
2645
|
}
|