@icp-sdk/core 5.0.0 → 5.2.0-beta.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 +28 -2
- package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/index.js +182 -90
- 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 +45 -19
- package/lib/cjs/agent/errors.d.ts.map +1 -1
- package/lib/cjs/agent/errors.js +58 -18
- package/lib/cjs/agent/errors.js.map +1 -1
- package/lib/cjs/agent/index.d.ts +1 -0
- package/lib/cjs/agent/index.d.ts.map +1 -1
- package/lib/cjs/agent/index.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/createExpirableStore.d.ts +23 -0
- package/lib/cjs/agent/utils/createExpirableStore.d.ts.map +1 -0
- package/lib/cjs/agent/utils/createExpirableStore.js +18 -0
- package/lib/cjs/agent/utils/createExpirableStore.js.map +1 -0
- package/lib/cjs/agent/utils/expirableStore.d.ts +39 -0
- package/lib/cjs/agent/utils/expirableStore.d.ts.map +1 -0
- package/lib/cjs/agent/utils/expirableStore.js +14 -0
- package/lib/cjs/agent/utils/expirableStore.js.map +1 -0
- package/lib/cjs/agent/utils/inMemoryExpirableStore.d.ts +14 -0
- package/lib/cjs/agent/utils/inMemoryExpirableStore.d.ts.map +1 -0
- package/lib/cjs/agent/utils/inMemoryExpirableStore.js +46 -0
- package/lib/cjs/agent/utils/inMemoryExpirableStore.js.map +1 -0
- package/lib/cjs/agent/utils/indexedDBExpirableStore.d.ts +28 -0
- package/lib/cjs/agent/utils/indexedDBExpirableStore.d.ts.map +1 -0
- package/lib/cjs/agent/utils/indexedDBExpirableStore.js +139 -0
- package/lib/cjs/agent/utils/indexedDBExpirableStore.js.map +1 -0
- 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 +28 -2
- package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/index.js +185 -93
- 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 +45 -19
- package/lib/esm/agent/errors.d.ts.map +1 -1
- package/lib/esm/agent/errors.js +54 -16
- package/lib/esm/agent/errors.js.map +1 -1
- package/lib/esm/agent/index.d.ts +1 -0
- package/lib/esm/agent/index.d.ts.map +1 -1
- package/lib/esm/agent/index.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/createExpirableStore.d.ts +23 -0
- package/lib/esm/agent/utils/createExpirableStore.d.ts.map +1 -0
- package/lib/esm/agent/utils/createExpirableStore.js +15 -0
- package/lib/esm/agent/utils/createExpirableStore.js.map +1 -0
- package/lib/esm/agent/utils/expirableStore.d.ts +39 -0
- package/lib/esm/agent/utils/expirableStore.d.ts.map +1 -0
- package/lib/esm/agent/utils/expirableStore.js +11 -0
- package/lib/esm/agent/utils/expirableStore.js.map +1 -0
- package/lib/esm/agent/utils/inMemoryExpirableStore.d.ts +14 -0
- package/lib/esm/agent/utils/inMemoryExpirableStore.d.ts.map +1 -0
- package/lib/esm/agent/utils/inMemoryExpirableStore.js +42 -0
- package/lib/esm/agent/utils/inMemoryExpirableStore.js.map +1 -0
- package/lib/esm/agent/utils/indexedDBExpirableStore.d.ts +28 -0
- package/lib/esm/agent/utils/indexedDBExpirableStore.d.ts.map +1 -0
- package/lib/esm/agent/utils/indexedDBExpirableStore.js +135 -0
- package/lib/esm/agent/utils/indexedDBExpirableStore.js.map +1 -0
- 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 +14 -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 +262 -104
- 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 +85 -34
- package/src/agent/fetch_candid.ts +1 -1
- package/src/agent/index.ts +1 -0
- 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/createExpirableStore.ts +31 -0
- package/src/agent/utils/expirableStore.ts +47 -0
- package/src/agent/utils/inMemoryExpirableStore.ts +49 -0
- package/src/agent/utils/indexedDBExpirableStore.ts +191 -0
- 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/lib/cjs/agent/utils/expirableMap.d.ts +0 -80
- package/lib/cjs/agent/utils/expirableMap.d.ts.map +0 -1
- package/lib/cjs/agent/utils/expirableMap.js +0 -148
- package/lib/cjs/agent/utils/expirableMap.js.map +0 -1
- package/lib/esm/agent/utils/expirableMap.d.ts +0 -80
- package/lib/esm/agent/utils/expirableMap.d.ts.map +0 -1
- package/lib/esm/agent/utils/expirableMap.js +0 -144
- package/lib/esm/agent/utils/expirableMap.js.map +0 -1
- package/src/agent/utils/expirableMap.ts +0 -163
package/lib/esm/candid/idl.js
CHANGED
|
@@ -10,6 +10,17 @@ import { iexp2 } from "./utils/bigint-math.js";
|
|
|
10
10
|
* between JavaScript values and IDL used by canisters on the Internet Computer,
|
|
11
11
|
* as documented at https://github.com/dfinity/candid/blob/119703ba342d2fef6ab4972d2541b9fe36ae8e36/spec/Candid.md
|
|
12
12
|
*/
|
|
13
|
+
/**
|
|
14
|
+
* Error thrown when candid decoding fails due to a type mismatch
|
|
15
|
+
* between the expected and received types.
|
|
16
|
+
*/
|
|
17
|
+
export class CandidDecodeError extends Error {
|
|
18
|
+
constructor(message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = 'CandidDecodeError';
|
|
21
|
+
Object.setPrototypeOf(this, CandidDecodeError.prototype);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
13
24
|
var IDLTypeIds;
|
|
14
25
|
(function (IDLTypeIds) {
|
|
15
26
|
IDLTypeIds[IDLTypeIds["Null"] = -1] = "Null";
|
|
@@ -58,10 +69,10 @@ class TypeTable {
|
|
|
58
69
|
const idx = this._idx.get(obj.name);
|
|
59
70
|
const knotIdx = this._idx.get(knot);
|
|
60
71
|
if (idx === undefined) {
|
|
61
|
-
throw new Error(
|
|
72
|
+
throw new Error(`Missing type index for ${obj}`);
|
|
62
73
|
}
|
|
63
74
|
if (knotIdx === undefined) {
|
|
64
|
-
throw new Error(
|
|
75
|
+
throw new Error(`Missing type index for ${knot}`);
|
|
65
76
|
}
|
|
66
77
|
// Point the recursive placeholder (obj) to the concrete type bytes
|
|
67
78
|
this._typs[idx] = this._typs[knotIdx];
|
|
@@ -98,7 +109,7 @@ class TypeTable {
|
|
|
98
109
|
}
|
|
99
110
|
indexOf(typeName) {
|
|
100
111
|
if (!this._idx.has(typeName)) {
|
|
101
|
-
throw new Error(
|
|
112
|
+
throw new Error(`Missing type index for ${typeName}`);
|
|
102
113
|
}
|
|
103
114
|
return slebEncode(this._idx.get(typeName) || 0);
|
|
104
115
|
}
|
|
@@ -226,7 +237,6 @@ export class PrimitiveType extends Type {
|
|
|
226
237
|
}
|
|
227
238
|
_buildTypeTableImpl(_typeTable) {
|
|
228
239
|
// No type table encoding for Primitive types.
|
|
229
|
-
return;
|
|
230
240
|
}
|
|
231
241
|
}
|
|
232
242
|
export class ConstructType extends Type {
|
|
@@ -234,7 +244,7 @@ export class ConstructType extends Type {
|
|
|
234
244
|
if (t instanceof RecClass) {
|
|
235
245
|
const ty = t.getType();
|
|
236
246
|
if (typeof ty === 'undefined') {
|
|
237
|
-
throw new
|
|
247
|
+
throw new CandidDecodeError('Type mismatch: cannot decode from uninitialized recursive type');
|
|
238
248
|
}
|
|
239
249
|
return ty;
|
|
240
250
|
}
|
|
@@ -358,8 +368,9 @@ export class BoolClass extends PrimitiveType {
|
|
|
358
368
|
return v.visitBool(this, d);
|
|
359
369
|
}
|
|
360
370
|
covariant(x) {
|
|
361
|
-
if (typeof x === 'boolean')
|
|
371
|
+
if (typeof x === 'boolean') {
|
|
362
372
|
return true;
|
|
373
|
+
}
|
|
363
374
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
364
375
|
}
|
|
365
376
|
encodeValue(x) {
|
|
@@ -397,8 +408,9 @@ export class NullClass extends PrimitiveType {
|
|
|
397
408
|
return v.visitNull(this, d);
|
|
398
409
|
}
|
|
399
410
|
covariant(x) {
|
|
400
|
-
if (x === null)
|
|
411
|
+
if (x === null) {
|
|
401
412
|
return true;
|
|
413
|
+
}
|
|
402
414
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
403
415
|
}
|
|
404
416
|
encodeValue() {
|
|
@@ -461,8 +473,9 @@ export class TextClass extends PrimitiveType {
|
|
|
461
473
|
return v.visitText(this, d);
|
|
462
474
|
}
|
|
463
475
|
covariant(x) {
|
|
464
|
-
if (typeof x === 'string')
|
|
476
|
+
if (typeof x === 'string') {
|
|
465
477
|
return true;
|
|
478
|
+
}
|
|
466
479
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
467
480
|
}
|
|
468
481
|
encodeValue(x) {
|
|
@@ -484,7 +497,7 @@ export class TextClass extends PrimitiveType {
|
|
|
484
497
|
return 'text';
|
|
485
498
|
}
|
|
486
499
|
valueToString(x) {
|
|
487
|
-
return
|
|
500
|
+
return `"${x}"`;
|
|
488
501
|
}
|
|
489
502
|
}
|
|
490
503
|
/**
|
|
@@ -503,8 +516,9 @@ export class IntClass extends PrimitiveType {
|
|
|
503
516
|
covariant(x) {
|
|
504
517
|
// We allow encoding of JavaScript plain numbers.
|
|
505
518
|
// But we will always decode to bigint.
|
|
506
|
-
if (typeof x === 'bigint' || Number.isInteger(x))
|
|
519
|
+
if (typeof x === 'bigint' || Number.isInteger(x)) {
|
|
507
520
|
return true;
|
|
521
|
+
}
|
|
508
522
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
509
523
|
}
|
|
510
524
|
encodeValue(x) {
|
|
@@ -540,8 +554,9 @@ export class NatClass extends PrimitiveType {
|
|
|
540
554
|
covariant(x) {
|
|
541
555
|
// We allow encoding of JavaScript plain numbers.
|
|
542
556
|
// But we will always decode to bigint.
|
|
543
|
-
if ((typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0))
|
|
557
|
+
if ((typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0)) {
|
|
544
558
|
return true;
|
|
559
|
+
}
|
|
545
560
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
546
561
|
}
|
|
547
562
|
encodeValue(x) {
|
|
@@ -582,8 +597,9 @@ export class FloatClass extends PrimitiveType {
|
|
|
582
597
|
return v.visitFloat(this, d);
|
|
583
598
|
}
|
|
584
599
|
covariant(x) {
|
|
585
|
-
if (typeof x === 'number' || x instanceof Number)
|
|
600
|
+
if (typeof x === 'number' || x instanceof Number) {
|
|
586
601
|
return true;
|
|
602
|
+
}
|
|
587
603
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
588
604
|
}
|
|
589
605
|
encodeValue(x) {
|
|
@@ -608,12 +624,10 @@ export class FloatClass extends PrimitiveType {
|
|
|
608
624
|
if (this._bits === 32) {
|
|
609
625
|
return view.getFloat32(0, true);
|
|
610
626
|
}
|
|
611
|
-
|
|
612
|
-
return view.getFloat64(0, true);
|
|
613
|
-
}
|
|
627
|
+
return view.getFloat64(0, true);
|
|
614
628
|
}
|
|
615
629
|
get name() {
|
|
616
|
-
return
|
|
630
|
+
return `float${this._bits}`;
|
|
617
631
|
}
|
|
618
632
|
valueToString(x) {
|
|
619
633
|
return x.toString();
|
|
@@ -639,19 +653,17 @@ export class FixedIntClass extends PrimitiveType {
|
|
|
639
653
|
covariant(x) {
|
|
640
654
|
const min = iexp2(this._bits - 1) * BigInt(-1);
|
|
641
655
|
const max = iexp2(this._bits - 1) - BigInt(1);
|
|
642
|
-
let ok = false;
|
|
643
656
|
if (typeof x === 'bigint') {
|
|
644
|
-
|
|
657
|
+
if (x >= min && x <= max) {
|
|
658
|
+
return true;
|
|
659
|
+
}
|
|
645
660
|
}
|
|
646
661
|
else if (Number.isInteger(x)) {
|
|
647
662
|
const v = BigInt(x);
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
ok = false;
|
|
663
|
+
if (v >= min && v <= max) {
|
|
664
|
+
return true;
|
|
665
|
+
}
|
|
652
666
|
}
|
|
653
|
-
if (ok)
|
|
654
|
-
return true;
|
|
655
667
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
656
668
|
}
|
|
657
669
|
encodeValue(x) {
|
|
@@ -667,9 +679,7 @@ export class FixedIntClass extends PrimitiveType {
|
|
|
667
679
|
if (this._bits <= 32) {
|
|
668
680
|
return Number(num);
|
|
669
681
|
}
|
|
670
|
-
|
|
671
|
-
return num;
|
|
672
|
-
}
|
|
682
|
+
return num;
|
|
673
683
|
}
|
|
674
684
|
get name() {
|
|
675
685
|
return `int${this._bits}`;
|
|
@@ -697,19 +707,17 @@ export class FixedNatClass extends PrimitiveType {
|
|
|
697
707
|
}
|
|
698
708
|
covariant(x) {
|
|
699
709
|
const max = iexp2(this._bits);
|
|
700
|
-
let ok = false;
|
|
701
710
|
if (typeof x === 'bigint' && x >= BigInt(0)) {
|
|
702
|
-
|
|
711
|
+
if (x < max) {
|
|
712
|
+
return true;
|
|
713
|
+
}
|
|
703
714
|
}
|
|
704
715
|
else if (Number.isInteger(x) && x >= 0) {
|
|
705
716
|
const v = BigInt(x);
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
ok = false;
|
|
717
|
+
if (v < max) {
|
|
718
|
+
return true;
|
|
719
|
+
}
|
|
710
720
|
}
|
|
711
|
-
if (ok)
|
|
712
|
-
return true;
|
|
713
721
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
714
722
|
}
|
|
715
723
|
encodeValue(x) {
|
|
@@ -725,9 +733,7 @@ export class FixedNatClass extends PrimitiveType {
|
|
|
725
733
|
if (this._bits <= 32) {
|
|
726
734
|
return Number(num);
|
|
727
735
|
}
|
|
728
|
-
|
|
729
|
-
return num;
|
|
730
|
-
}
|
|
736
|
+
return num;
|
|
731
737
|
}
|
|
732
738
|
get name() {
|
|
733
739
|
return `nat${this._bits}`;
|
|
@@ -781,10 +787,11 @@ export class VecClass extends ConstructType {
|
|
|
781
787
|
return this._type.covariant(v);
|
|
782
788
|
}
|
|
783
789
|
catch (e) {
|
|
784
|
-
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${idx} -> ${e.message}
|
|
790
|
+
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${idx} -> ${e.message}`, { cause: e });
|
|
785
791
|
}
|
|
786
|
-
})))
|
|
792
|
+
}))) {
|
|
787
793
|
return true;
|
|
794
|
+
}
|
|
788
795
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
789
796
|
}
|
|
790
797
|
encodeValue(x) {
|
|
@@ -806,7 +813,7 @@ export class VecClass extends ConstructType {
|
|
|
806
813
|
}
|
|
807
814
|
return concat(len, new Uint8Array(buffer.buffer));
|
|
808
815
|
}
|
|
809
|
-
|
|
816
|
+
if (x instanceof Int32Array || x instanceof Uint32Array) {
|
|
810
817
|
const buffer = new DataView(new ArrayBuffer(x.length * 4));
|
|
811
818
|
for (let i = 0; i < x.length; i++) {
|
|
812
819
|
if (x instanceof Int32Array) {
|
|
@@ -818,7 +825,7 @@ export class VecClass extends ConstructType {
|
|
|
818
825
|
}
|
|
819
826
|
return concat(len, new Uint8Array(buffer.buffer));
|
|
820
827
|
}
|
|
821
|
-
|
|
828
|
+
if (x instanceof BigInt64Array || x instanceof BigUint64Array) {
|
|
822
829
|
const buffer = new DataView(new ArrayBuffer(x.length * 8));
|
|
823
830
|
for (let i = 0; i < x.length; i++) {
|
|
824
831
|
if (x instanceof BigInt64Array) {
|
|
@@ -830,10 +837,8 @@ export class VecClass extends ConstructType {
|
|
|
830
837
|
}
|
|
831
838
|
return concat(len, new Uint8Array(buffer.buffer));
|
|
832
839
|
}
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
return concat(len, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
836
|
-
}
|
|
840
|
+
// For Uint8Array, Int8Array, etc. that don't have endianness concerns
|
|
841
|
+
return concat(len, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
837
842
|
}
|
|
838
843
|
const buf = new Pipe(new Uint8Array(len.byteLength + x.length), 0);
|
|
839
844
|
buf.write(len);
|
|
@@ -852,7 +857,7 @@ export class VecClass extends ConstructType {
|
|
|
852
857
|
decodeValue(b, t) {
|
|
853
858
|
const vec = this.checkType(t);
|
|
854
859
|
if (!(vec instanceof VecClass)) {
|
|
855
|
-
throw new
|
|
860
|
+
throw new CandidDecodeError(`Expected type '${this.display()}', but received non-vector type '${vec.display()}'`);
|
|
856
861
|
}
|
|
857
862
|
const len = Number(lebDecode(b));
|
|
858
863
|
if (this._type instanceof FixedNatClass) {
|
|
@@ -923,7 +928,7 @@ export class VecClass extends ConstructType {
|
|
|
923
928
|
}
|
|
924
929
|
valueToString(x) {
|
|
925
930
|
const elements = x.map(e => this._type.valueToString(e));
|
|
926
|
-
return
|
|
931
|
+
return `vec {${elements.join('; ')}}`;
|
|
927
932
|
}
|
|
928
933
|
}
|
|
929
934
|
/**
|
|
@@ -946,11 +951,12 @@ export class OptClass extends ConstructType {
|
|
|
946
951
|
}
|
|
947
952
|
covariant(x) {
|
|
948
953
|
try {
|
|
949
|
-
if (Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0]))))
|
|
954
|
+
if (Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0])))) {
|
|
950
955
|
return true;
|
|
956
|
+
}
|
|
951
957
|
}
|
|
952
958
|
catch (e) {
|
|
953
|
-
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)} \n\n-> ${e.message}
|
|
959
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)} \n\n-> ${e.message}`, { cause: e });
|
|
954
960
|
}
|
|
955
961
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
956
962
|
}
|
|
@@ -958,9 +964,7 @@ export class OptClass extends ConstructType {
|
|
|
958
964
|
if (x.length === 0) {
|
|
959
965
|
return new Uint8Array([0]);
|
|
960
966
|
}
|
|
961
|
-
|
|
962
|
-
return concat(new Uint8Array([1]), this._type.encodeValue(x[0]));
|
|
963
|
-
}
|
|
967
|
+
return concat(new Uint8Array([1]), this._type.encodeValue(x[0]));
|
|
964
968
|
}
|
|
965
969
|
_buildTypeTableImpl(typeTable) {
|
|
966
970
|
this._type.buildTypeTable(typeTable);
|
|
@@ -980,10 +984,11 @@ export class OptClass extends ConstructType {
|
|
|
980
984
|
if (t instanceof RecClass) {
|
|
981
985
|
const ty = t.getType();
|
|
982
986
|
if (typeof ty === 'undefined') {
|
|
983
|
-
throw new
|
|
987
|
+
throw new CandidDecodeError('Type mismatch: cannot decode from uninitialized recursive type');
|
|
984
988
|
}
|
|
985
|
-
else
|
|
989
|
+
else {
|
|
986
990
|
wireType = ty;
|
|
991
|
+
}
|
|
987
992
|
}
|
|
988
993
|
if (wireType instanceof OptClass) {
|
|
989
994
|
switch (safeReadUint8(b)) {
|
|
@@ -1007,7 +1012,7 @@ export class OptClass extends ConstructType {
|
|
|
1007
1012
|
}
|
|
1008
1013
|
}
|
|
1009
1014
|
default:
|
|
1010
|
-
throw new
|
|
1015
|
+
throw new CandidDecodeError(`Invalid option value: expected 0 (None) or 1 (Some) for type '${this.display()}'`);
|
|
1011
1016
|
}
|
|
1012
1017
|
}
|
|
1013
1018
|
else if (
|
|
@@ -1048,9 +1053,7 @@ export class OptClass extends ConstructType {
|
|
|
1048
1053
|
if (x.length === 0) {
|
|
1049
1054
|
return 'null';
|
|
1050
1055
|
}
|
|
1051
|
-
|
|
1052
|
-
return `opt ${this._type.valueToString(x[0])}`;
|
|
1053
|
-
}
|
|
1056
|
+
return `opt ${this._type.valueToString(x[0])}`;
|
|
1054
1057
|
}
|
|
1055
1058
|
}
|
|
1056
1059
|
/**
|
|
@@ -1094,10 +1097,13 @@ export class RecordClass extends ConstructType {
|
|
|
1094
1097
|
return t.covariant(x[k]);
|
|
1095
1098
|
}
|
|
1096
1099
|
catch (e) {
|
|
1097
|
-
throw new Error(`Invalid ${this.display()} argument: \n\nfield ${k} -> ${e.message}
|
|
1100
|
+
throw new Error(`Invalid ${this.display()} argument: \n\nfield ${k} -> ${e.message}`, {
|
|
1101
|
+
cause: e,
|
|
1102
|
+
});
|
|
1098
1103
|
}
|
|
1099
|
-
}))
|
|
1104
|
+
})) {
|
|
1100
1105
|
return true;
|
|
1106
|
+
}
|
|
1101
1107
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1102
1108
|
}
|
|
1103
1109
|
encodeValue(x) {
|
|
@@ -1115,7 +1121,7 @@ export class RecordClass extends ConstructType {
|
|
|
1115
1121
|
decodeValue(b, t) {
|
|
1116
1122
|
const record = this.checkType(t);
|
|
1117
1123
|
if (!(record instanceof RecordClass)) {
|
|
1118
|
-
throw new
|
|
1124
|
+
throw new CandidDecodeError(`Expected type '${this.display()}', but received non-record type '${record.display()}'`);
|
|
1119
1125
|
}
|
|
1120
1126
|
const x = {};
|
|
1121
1127
|
let expectedRecordIdx = 0;
|
|
@@ -1144,7 +1150,9 @@ export class RecordClass extends ConstructType {
|
|
|
1144
1150
|
expectedRecordIdx++;
|
|
1145
1151
|
}
|
|
1146
1152
|
else {
|
|
1147
|
-
|
|
1153
|
+
const expectedFields = this._fields.map(([name]) => name).join(', ');
|
|
1154
|
+
const receivedFields = record._fields.map(([name]) => name).join(', ');
|
|
1155
|
+
throw new CandidDecodeError(`Cannot find required field '${expectKey}' in record, expected fields: [${expectedFields}], received fields: [${receivedFields}]`);
|
|
1148
1156
|
}
|
|
1149
1157
|
}
|
|
1150
1158
|
else {
|
|
@@ -1160,7 +1168,9 @@ export class RecordClass extends ConstructType {
|
|
|
1160
1168
|
x[expectKey] = [];
|
|
1161
1169
|
}
|
|
1162
1170
|
else {
|
|
1163
|
-
|
|
1171
|
+
const expectedFields = this._fields.map(([name]) => name).join(', ');
|
|
1172
|
+
const receivedFields = record._fields.map(([name]) => name).join(', ');
|
|
1173
|
+
throw new CandidDecodeError(`Cannot find required field '${expectKey}' in record, expected fields: [${expectedFields}], received fields: [${receivedFields}]`);
|
|
1164
1174
|
}
|
|
1165
1175
|
}
|
|
1166
1176
|
return x;
|
|
@@ -1173,16 +1183,16 @@ export class RecordClass extends ConstructType {
|
|
|
1173
1183
|
return fields;
|
|
1174
1184
|
}
|
|
1175
1185
|
get name() {
|
|
1176
|
-
const fields = this._fields.map(([key, value]) => key
|
|
1186
|
+
const fields = this._fields.map(([key, value]) => `${key}:${value.name}`);
|
|
1177
1187
|
return `record {${fields.join('; ')}}`;
|
|
1178
1188
|
}
|
|
1179
1189
|
display() {
|
|
1180
|
-
const fields = this._fields.map(([key, value]) => key
|
|
1190
|
+
const fields = this._fields.map(([key, value]) => `${key}:${value.display()}`);
|
|
1181
1191
|
return `record {${fields.join('; ')}}`;
|
|
1182
1192
|
}
|
|
1183
1193
|
valueToString(x) {
|
|
1184
1194
|
const values = this._fields.map(([key]) => x[key]);
|
|
1185
|
-
const fields = zipWith(this._fields, values, ([k, c], d) => k
|
|
1195
|
+
const fields = zipWith(this._fields, values, ([k, c], d) => `${k}=${c.valueToString(d)}`);
|
|
1186
1196
|
return `record {${fields.join('; ')}}`;
|
|
1187
1197
|
}
|
|
1188
1198
|
}
|
|
@@ -1199,7 +1209,7 @@ export class TupleClass extends RecordClass {
|
|
|
1199
1209
|
}
|
|
1200
1210
|
constructor(_components) {
|
|
1201
1211
|
const x = {};
|
|
1202
|
-
_components.forEach((e, i) => (x[
|
|
1212
|
+
_components.forEach((e, i) => (x[`_${i}_`] = e));
|
|
1203
1213
|
super(x);
|
|
1204
1214
|
this._components = _components;
|
|
1205
1215
|
}
|
|
@@ -1215,10 +1225,13 @@ export class TupleClass extends RecordClass {
|
|
|
1215
1225
|
return t.covariant(x[i]);
|
|
1216
1226
|
}
|
|
1217
1227
|
catch (e) {
|
|
1218
|
-
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${i} -> ${e.message}
|
|
1228
|
+
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${i} -> ${e.message}`, {
|
|
1229
|
+
cause: e,
|
|
1230
|
+
});
|
|
1219
1231
|
}
|
|
1220
|
-
}))
|
|
1232
|
+
})) {
|
|
1221
1233
|
return true;
|
|
1234
|
+
}
|
|
1222
1235
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1223
1236
|
}
|
|
1224
1237
|
encodeValue(x) {
|
|
@@ -1228,10 +1241,10 @@ export class TupleClass extends RecordClass {
|
|
|
1228
1241
|
decodeValue(b, t) {
|
|
1229
1242
|
const tuple = this.checkType(t);
|
|
1230
1243
|
if (!(tuple instanceof TupleClass)) {
|
|
1231
|
-
throw new
|
|
1244
|
+
throw new CandidDecodeError(`Expected type '${this.display()}', but received non-tuple type '${tuple.display()}'`);
|
|
1232
1245
|
}
|
|
1233
1246
|
if (tuple._components.length < this._components.length) {
|
|
1234
|
-
throw new
|
|
1247
|
+
throw new CandidDecodeError(`Tuple mismatch: expected ${this._components.length} components, but received ${tuple._components.length}`);
|
|
1235
1248
|
}
|
|
1236
1249
|
const res = [];
|
|
1237
1250
|
for (const [i, wireType] of tuple._components.entries()) {
|
|
@@ -1281,10 +1294,13 @@ export class VariantClass extends ConstructType {
|
|
|
1281
1294
|
return !x.hasOwnProperty(k) || v.covariant(x[k]);
|
|
1282
1295
|
}
|
|
1283
1296
|
catch (e) {
|
|
1284
|
-
throw new Error(`Invalid ${this.display()} argument: \n\nvariant ${k} -> ${e.message}
|
|
1297
|
+
throw new Error(`Invalid ${this.display()} argument: \n\nvariant ${k} -> ${e.message}`, {
|
|
1298
|
+
cause: e,
|
|
1299
|
+
});
|
|
1285
1300
|
}
|
|
1286
|
-
}))
|
|
1301
|
+
})) {
|
|
1287
1302
|
return true;
|
|
1303
|
+
}
|
|
1288
1304
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1289
1305
|
}
|
|
1290
1306
|
encodeValue(x) {
|
|
@@ -1297,7 +1313,7 @@ export class VariantClass extends ConstructType {
|
|
|
1297
1313
|
return concat(idx, buf);
|
|
1298
1314
|
}
|
|
1299
1315
|
}
|
|
1300
|
-
throw Error(
|
|
1316
|
+
throw Error(`Variant has no data: ${x}`);
|
|
1301
1317
|
}
|
|
1302
1318
|
_buildTypeTableImpl(typeTable) {
|
|
1303
1319
|
this._fields.forEach(([, type]) => {
|
|
@@ -1311,11 +1327,11 @@ export class VariantClass extends ConstructType {
|
|
|
1311
1327
|
decodeValue(b, t) {
|
|
1312
1328
|
const variant = this.checkType(t);
|
|
1313
1329
|
if (!(variant instanceof VariantClass)) {
|
|
1314
|
-
throw new
|
|
1330
|
+
throw new CandidDecodeError(`Expected type '${this.display()}', but received non-variant type '${variant.display()}'`);
|
|
1315
1331
|
}
|
|
1316
1332
|
const idx = Number(lebDecode(b));
|
|
1317
1333
|
if (idx >= variant._fields.length) {
|
|
1318
|
-
throw Error(
|
|
1334
|
+
throw Error(`Invalid variant index: ${idx}`);
|
|
1319
1335
|
}
|
|
1320
1336
|
const [wireHash, wireType] = variant._fields[idx];
|
|
1321
1337
|
for (const [key, expectType] of this._fields) {
|
|
@@ -1324,10 +1340,11 @@ export class VariantClass extends ConstructType {
|
|
|
1324
1340
|
return { [key]: value };
|
|
1325
1341
|
}
|
|
1326
1342
|
}
|
|
1327
|
-
|
|
1343
|
+
const expectedFields = this._fields.map(([name]) => name).join(', ');
|
|
1344
|
+
throw new CandidDecodeError(`Cannot find field hash ${wireHash} in variant, expected fields: [${expectedFields}]`);
|
|
1328
1345
|
}
|
|
1329
1346
|
get name() {
|
|
1330
|
-
const fields = this._fields.map(([key, type]) => key
|
|
1347
|
+
const fields = this._fields.map(([key, type]) => `${key}:${type.name}`);
|
|
1331
1348
|
return `variant {${fields.join('; ')}}`;
|
|
1332
1349
|
}
|
|
1333
1350
|
display() {
|
|
@@ -1342,12 +1359,10 @@ export class VariantClass extends ConstructType {
|
|
|
1342
1359
|
if (value === 'null') {
|
|
1343
1360
|
return `variant {${name}}`;
|
|
1344
1361
|
}
|
|
1345
|
-
|
|
1346
|
-
return `variant {${name}=${value}}`;
|
|
1347
|
-
}
|
|
1362
|
+
return `variant {${name}=${value}}`;
|
|
1348
1363
|
}
|
|
1349
1364
|
}
|
|
1350
|
-
throw new Error(
|
|
1365
|
+
throw new Error(`Variant has no data: ${x}`);
|
|
1351
1366
|
}
|
|
1352
1367
|
get alternativesAsObject() {
|
|
1353
1368
|
const alternatives = {};
|
|
@@ -1386,8 +1401,9 @@ export class RecClass extends ConstructType {
|
|
|
1386
1401
|
return this._type;
|
|
1387
1402
|
}
|
|
1388
1403
|
covariant(x) {
|
|
1389
|
-
if (this._type ? this._type.covariant(x) : false)
|
|
1404
|
+
if (this._type ? this._type.covariant(x) : false) {
|
|
1390
1405
|
return true;
|
|
1406
|
+
}
|
|
1391
1407
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1392
1408
|
}
|
|
1393
1409
|
encodeValue(x) {
|
|
@@ -1448,8 +1464,9 @@ export class PrincipalClass extends PrimitiveType {
|
|
|
1448
1464
|
return v.visitPrincipal(this, d);
|
|
1449
1465
|
}
|
|
1450
1466
|
covariant(x) {
|
|
1451
|
-
if (x && x._isPrincipal)
|
|
1467
|
+
if (x && x._isPrincipal) {
|
|
1452
1468
|
return true;
|
|
1469
|
+
}
|
|
1453
1470
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1454
1471
|
}
|
|
1455
1472
|
encodeValue(x) {
|
|
@@ -1488,7 +1505,7 @@ export class FuncClass extends ConstructType {
|
|
|
1488
1505
|
if (types.length !== v.length) {
|
|
1489
1506
|
throw new Error('arity mismatch');
|
|
1490
1507
|
}
|
|
1491
|
-
return
|
|
1508
|
+
return `(${types.map((t, i) => t.valueToString(v[i])).join(', ')})`;
|
|
1492
1509
|
}
|
|
1493
1510
|
constructor(argTypes, retTypes, annotations = []) {
|
|
1494
1511
|
super();
|
|
@@ -1500,8 +1517,13 @@ export class FuncClass extends ConstructType {
|
|
|
1500
1517
|
return v.visitFunc(this, d);
|
|
1501
1518
|
}
|
|
1502
1519
|
covariant(x) {
|
|
1503
|
-
if (Array.isArray(x) &&
|
|
1520
|
+
if (Array.isArray(x) &&
|
|
1521
|
+
x.length === 2 &&
|
|
1522
|
+
x[0] &&
|
|
1523
|
+
x[0]._isPrincipal &&
|
|
1524
|
+
typeof x[1] === 'string') {
|
|
1504
1525
|
return true;
|
|
1526
|
+
}
|
|
1505
1527
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1506
1528
|
}
|
|
1507
1529
|
encodeValue([principal, methodName]) {
|
|
@@ -1543,7 +1565,7 @@ export class FuncClass extends ConstructType {
|
|
|
1543
1565
|
get name() {
|
|
1544
1566
|
const args = this.argTypes.map(arg => arg.name).join(', ');
|
|
1545
1567
|
const rets = this.retTypes.map(arg => arg.name).join(', ');
|
|
1546
|
-
const annon =
|
|
1568
|
+
const annon = ` ${this.annotations.join(' ')}`;
|
|
1547
1569
|
return `(${args}) -> (${rets})${annon}`;
|
|
1548
1570
|
}
|
|
1549
1571
|
valueToString([principal, str]) {
|
|
@@ -1552,22 +1574,20 @@ export class FuncClass extends ConstructType {
|
|
|
1552
1574
|
display() {
|
|
1553
1575
|
const args = this.argTypes.map(arg => arg.display()).join(', ');
|
|
1554
1576
|
const rets = this.retTypes.map(arg => arg.display()).join(', ');
|
|
1555
|
-
const annon =
|
|
1577
|
+
const annon = ` ${this.annotations.join(' ')}`;
|
|
1556
1578
|
return `(${args}) → (${rets})${annon}`;
|
|
1557
1579
|
}
|
|
1558
1580
|
encodeAnnotation(ann) {
|
|
1559
1581
|
if (ann === 'query') {
|
|
1560
1582
|
return new Uint8Array([1]);
|
|
1561
1583
|
}
|
|
1562
|
-
|
|
1584
|
+
if (ann === 'oneway') {
|
|
1563
1585
|
return new Uint8Array([2]);
|
|
1564
1586
|
}
|
|
1565
|
-
|
|
1587
|
+
if (ann === 'composite_query') {
|
|
1566
1588
|
return new Uint8Array([3]);
|
|
1567
1589
|
}
|
|
1568
|
-
|
|
1569
|
-
throw new Error('Illegal function annotation');
|
|
1570
|
-
}
|
|
1590
|
+
throw new Error('Illegal function annotation');
|
|
1571
1591
|
}
|
|
1572
1592
|
}
|
|
1573
1593
|
export class ServiceClass extends ConstructType {
|
|
@@ -1593,8 +1613,9 @@ export class ServiceClass extends ConstructType {
|
|
|
1593
1613
|
return v.visitService(this, d);
|
|
1594
1614
|
}
|
|
1595
1615
|
covariant(x) {
|
|
1596
|
-
if (x && x._isPrincipal)
|
|
1616
|
+
if (x && x._isPrincipal) {
|
|
1597
1617
|
return true;
|
|
1618
|
+
}
|
|
1598
1619
|
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1599
1620
|
}
|
|
1600
1621
|
encodeValue(x) {
|
|
@@ -1621,7 +1642,7 @@ export class ServiceClass extends ConstructType {
|
|
|
1621
1642
|
return decodePrincipalId(b);
|
|
1622
1643
|
}
|
|
1623
1644
|
get name() {
|
|
1624
|
-
const fields = this._fields.map(([key, value]) => key
|
|
1645
|
+
const fields = this._fields.map(([key, value]) => `${key}:${value.name}`);
|
|
1625
1646
|
return `service {${fields.join('; ')}}`;
|
|
1626
1647
|
}
|
|
1627
1648
|
valueToString(x) {
|
|
@@ -1643,7 +1664,7 @@ export class ServiceClass extends ConstructType {
|
|
|
1643
1664
|
function toReadableString(x) {
|
|
1644
1665
|
const str = JSON.stringify(x, (_key, value) => typeof value === 'bigint' ? `BigInt(${value})` : value);
|
|
1645
1666
|
return str && str.length > toReadableString_max
|
|
1646
|
-
? str.substring(0, toReadableString_max - 3)
|
|
1667
|
+
? `${str.substring(0, toReadableString_max - 3)}...`
|
|
1647
1668
|
: str;
|
|
1648
1669
|
}
|
|
1649
1670
|
/**
|
|
@@ -1666,7 +1687,7 @@ export function encode(argTypes, args) {
|
|
|
1666
1687
|
t.covariant(x);
|
|
1667
1688
|
}
|
|
1668
1689
|
catch (e) {
|
|
1669
|
-
const err = new Error(e.message
|
|
1690
|
+
const err = new Error(`${e.message}\n\n`);
|
|
1670
1691
|
throw err;
|
|
1671
1692
|
}
|
|
1672
1693
|
return t.encodeValue(x);
|
|
@@ -1687,7 +1708,7 @@ export function decode(retTypes, bytes) {
|
|
|
1687
1708
|
const magicBuffer = safeRead(b, magicNumber.length);
|
|
1688
1709
|
const magic = new TextDecoder().decode(magicBuffer);
|
|
1689
1710
|
if (magic !== magicNumber) {
|
|
1690
|
-
throw new Error(
|
|
1711
|
+
throw new Error(`Wrong magic number: ${JSON.stringify(magic)}`);
|
|
1691
1712
|
}
|
|
1692
1713
|
function readTypeTable(pipe) {
|
|
1693
1714
|
const typeTable = [];
|
|
@@ -1769,7 +1790,7 @@ export function decode(retTypes, bytes) {
|
|
|
1769
1790
|
break;
|
|
1770
1791
|
}
|
|
1771
1792
|
default:
|
|
1772
|
-
throw new Error(
|
|
1793
|
+
throw new Error(`Illegal op_code: ${ty}`);
|
|
1773
1794
|
}
|
|
1774
1795
|
}
|
|
1775
1796
|
const rawList = [];
|
|
@@ -1827,7 +1848,7 @@ export function decode(retTypes, bytes) {
|
|
|
1827
1848
|
case -24:
|
|
1828
1849
|
return Principal;
|
|
1829
1850
|
default:
|
|
1830
|
-
throw new Error(
|
|
1851
|
+
throw new Error(`Illegal op_code: ${t}`);
|
|
1831
1852
|
}
|
|
1832
1853
|
}
|
|
1833
1854
|
if (t >= rawTable.length) {
|
|
@@ -1856,9 +1877,7 @@ export function decode(retTypes, bytes) {
|
|
|
1856
1877
|
if (Array.isArray(tuple)) {
|
|
1857
1878
|
return Tuple(...tuple);
|
|
1858
1879
|
}
|
|
1859
|
-
|
|
1860
|
-
return record;
|
|
1861
|
-
}
|
|
1880
|
+
return record;
|
|
1862
1881
|
}
|
|
1863
1882
|
case IDLTypeIds.Variant: {
|
|
1864
1883
|
const fields = {};
|
|
@@ -1889,7 +1908,7 @@ export function decode(retTypes, bytes) {
|
|
|
1889
1908
|
return Service(rec);
|
|
1890
1909
|
}
|
|
1891
1910
|
default:
|
|
1892
|
-
throw new Error(
|
|
1911
|
+
throw new Error(`Illegal op_code: ${entry[0]}`);
|
|
1893
1912
|
}
|
|
1894
1913
|
}
|
|
1895
1914
|
rawTable.forEach((entry, i) => {
|
|
@@ -2073,8 +2092,9 @@ function eqFunctionAnnotations(t1, t2) {
|
|
|
2073
2092
|
return false;
|
|
2074
2093
|
}
|
|
2075
2094
|
for (const a of t1Annotations) {
|
|
2076
|
-
if (!t2Annotations.has(a))
|
|
2095
|
+
if (!t2Annotations.has(a)) {
|
|
2077
2096
|
return false;
|
|
2097
|
+
}
|
|
2078
2098
|
}
|
|
2079
2099
|
return true;
|
|
2080
2100
|
}
|
|
@@ -2098,60 +2118,74 @@ export function subtype(t1, t2) {
|
|
|
2098
2118
|
return isSubtype;
|
|
2099
2119
|
}
|
|
2100
2120
|
function subtype_(relations, t1, t2) {
|
|
2101
|
-
if (t1.name === t2.name)
|
|
2121
|
+
if (t1.name === t2.name) {
|
|
2102
2122
|
return true;
|
|
2123
|
+
}
|
|
2103
2124
|
const known = relations.known(t1, t2);
|
|
2104
|
-
if (known !== undefined)
|
|
2125
|
+
if (known !== undefined) {
|
|
2105
2126
|
return known;
|
|
2127
|
+
}
|
|
2106
2128
|
relations.add(t1, t2);
|
|
2107
|
-
if (t2 instanceof ReservedClass)
|
|
2129
|
+
if (t2 instanceof ReservedClass) {
|
|
2108
2130
|
return true;
|
|
2109
|
-
|
|
2131
|
+
}
|
|
2132
|
+
if (t1 instanceof EmptyClass) {
|
|
2110
2133
|
return true;
|
|
2111
|
-
|
|
2134
|
+
}
|
|
2135
|
+
if (t1 instanceof NatClass && t2 instanceof IntClass) {
|
|
2112
2136
|
return true;
|
|
2113
|
-
|
|
2137
|
+
}
|
|
2138
|
+
if (t1 instanceof VecClass && t2 instanceof VecClass) {
|
|
2114
2139
|
return subtype_(relations, t1._type, t2._type);
|
|
2115
|
-
|
|
2140
|
+
}
|
|
2141
|
+
if (t2 instanceof OptClass) {
|
|
2116
2142
|
return true;
|
|
2143
|
+
}
|
|
2117
2144
|
if (t1 instanceof RecordClass && t2 instanceof RecordClass) {
|
|
2118
2145
|
const t1Object = t1.fieldsAsObject;
|
|
2119
2146
|
for (const [label, ty2] of t2._fields) {
|
|
2120
2147
|
const ty1 = t1Object[idlLabelToId(label)];
|
|
2121
2148
|
if (!ty1) {
|
|
2122
|
-
if (!canBeOmmitted(ty2))
|
|
2149
|
+
if (!canBeOmmitted(ty2)) {
|
|
2123
2150
|
return false;
|
|
2151
|
+
}
|
|
2124
2152
|
}
|
|
2125
2153
|
else {
|
|
2126
|
-
if (!subtype_(relations, ty1, ty2))
|
|
2154
|
+
if (!subtype_(relations, ty1, ty2)) {
|
|
2127
2155
|
return false;
|
|
2156
|
+
}
|
|
2128
2157
|
}
|
|
2129
2158
|
}
|
|
2130
2159
|
return true;
|
|
2131
2160
|
}
|
|
2132
2161
|
if (t1 instanceof FuncClass && t2 instanceof FuncClass) {
|
|
2133
|
-
if (!eqFunctionAnnotations(t1, t2))
|
|
2162
|
+
if (!eqFunctionAnnotations(t1, t2)) {
|
|
2134
2163
|
return false;
|
|
2164
|
+
}
|
|
2135
2165
|
for (let i = 0; i < t1.argTypes.length; i++) {
|
|
2136
2166
|
const argTy1 = t1.argTypes[i];
|
|
2137
2167
|
if (i < t2.argTypes.length) {
|
|
2138
|
-
if (!subtype_(relations, t2.argTypes[i], argTy1))
|
|
2168
|
+
if (!subtype_(relations, t2.argTypes[i], argTy1)) {
|
|
2139
2169
|
return false;
|
|
2170
|
+
}
|
|
2140
2171
|
}
|
|
2141
2172
|
else {
|
|
2142
|
-
if (!canBeOmmitted(argTy1))
|
|
2173
|
+
if (!canBeOmmitted(argTy1)) {
|
|
2143
2174
|
return false;
|
|
2175
|
+
}
|
|
2144
2176
|
}
|
|
2145
2177
|
}
|
|
2146
2178
|
for (let i = 0; i < t2.retTypes.length; i++) {
|
|
2147
2179
|
const retTy2 = t2.retTypes[i];
|
|
2148
2180
|
if (i < t1.retTypes.length) {
|
|
2149
|
-
if (!subtype_(relations, t1.retTypes[i], retTy2))
|
|
2181
|
+
if (!subtype_(relations, t1.retTypes[i], retTy2)) {
|
|
2150
2182
|
return false;
|
|
2183
|
+
}
|
|
2151
2184
|
}
|
|
2152
2185
|
else {
|
|
2153
|
-
if (!canBeOmmitted(retTy2))
|
|
2186
|
+
if (!canBeOmmitted(retTy2)) {
|
|
2154
2187
|
return false;
|
|
2188
|
+
}
|
|
2155
2189
|
}
|
|
2156
2190
|
}
|
|
2157
2191
|
return true;
|
|
@@ -2160,10 +2194,12 @@ function subtype_(relations, t1, t2) {
|
|
|
2160
2194
|
const t2Object = t2.alternativesAsObject;
|
|
2161
2195
|
for (const [label, ty1] of t1._fields) {
|
|
2162
2196
|
const ty2 = t2Object[idlLabelToId(label)];
|
|
2163
|
-
if (!ty2)
|
|
2197
|
+
if (!ty2) {
|
|
2164
2198
|
return false;
|
|
2165
|
-
|
|
2199
|
+
}
|
|
2200
|
+
if (!subtype_(relations, ty1, ty2)) {
|
|
2166
2201
|
return false;
|
|
2202
|
+
}
|
|
2167
2203
|
}
|
|
2168
2204
|
return true;
|
|
2169
2205
|
}
|
|
@@ -2171,10 +2207,12 @@ function subtype_(relations, t1, t2) {
|
|
|
2171
2207
|
const t1Object = t1.fieldsAsObject();
|
|
2172
2208
|
for (const [name, ty2] of t2._fields) {
|
|
2173
2209
|
const ty1 = t1Object[name];
|
|
2174
|
-
if (!ty1)
|
|
2210
|
+
if (!ty1) {
|
|
2175
2211
|
return false;
|
|
2176
|
-
|
|
2212
|
+
}
|
|
2213
|
+
if (!subtype_(relations, ty1, ty2)) {
|
|
2177
2214
|
return false;
|
|
2215
|
+
}
|
|
2178
2216
|
}
|
|
2179
2217
|
return true;
|
|
2180
2218
|
}
|