@dfinity/candid
Advanced tools
Comparing version 0.13.3 to 0.14.0
@@ -12,2 +12,3 @@ "use strict"; | ||
const magicNumber = 'DIDL'; | ||
const toReadableString_max = 400; // will not display arguments after 400chars. Makes sure 2mb blobs don't get inside the error | ||
function zipWith(xs, ys, f) { | ||
@@ -190,3 +191,3 @@ return xs.map((x, i) => f(x, ys[i])); | ||
covariant(x) { | ||
return false; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -225,3 +226,3 @@ encodeValue() { | ||
covariant(x) { | ||
return false; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -280,3 +281,5 @@ encodeValue() { | ||
covariant(x) { | ||
return typeof x === 'boolean'; | ||
if (typeof x === 'boolean') | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -313,3 +316,5 @@ encodeValue(x) { | ||
covariant(x) { | ||
return x === null; | ||
if (x === null) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -366,3 +371,5 @@ encodeValue() { | ||
covariant(x) { | ||
return typeof x === 'string'; | ||
if (typeof x === 'string') | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -402,3 +409,5 @@ encodeValue(x) { | ||
// But we will always decode to bigint. | ||
return typeof x === 'bigint' || Number.isInteger(x); | ||
if (typeof x === 'bigint' || Number.isInteger(x)) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -433,3 +442,5 @@ encodeValue(x) { | ||
// But we will always decode to bigint. | ||
return (typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0); | ||
if ((typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0)) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -469,3 +480,5 @@ encodeValue(x) { | ||
covariant(x) { | ||
return typeof x === 'number' || x instanceof Number; | ||
if (typeof x === 'number' || x instanceof Number) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -520,12 +533,16 @@ encodeValue(x) { | ||
const max = (0, bigint_math_1.iexp2)(this._bits - 1) - BigInt(1); | ||
let ok = false; | ||
if (typeof x === 'bigint') { | ||
return x >= min && x <= max; | ||
ok = x >= min && x <= max; | ||
} | ||
else if (Number.isInteger(x)) { | ||
const v = BigInt(x); | ||
return v >= min && v <= max; | ||
ok = v >= min && v <= max; | ||
} | ||
else { | ||
return false; | ||
ok = false; | ||
} | ||
if (ok) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -570,12 +587,16 @@ encodeValue(x) { | ||
const max = (0, bigint_math_1.iexp2)(this._bits); | ||
let ok = false; | ||
if (typeof x === 'bigint' && x >= BigInt(0)) { | ||
return x < max; | ||
ok = x < max; | ||
} | ||
else if (Number.isInteger(x) && x >= 0) { | ||
const v = BigInt(x); | ||
return v < max; | ||
ok = v < max; | ||
} | ||
else { | ||
return false; | ||
ok = false; | ||
} | ||
if (ok) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -640,4 +661,14 @@ encodeValue(x) { | ||
: 0; | ||
return ((ArrayBuffer.isView(x) && bits == x.BYTES_PER_ELEMENT * 8) || | ||
(Array.isArray(x) && x.every(v => this._type.covariant(v)))); | ||
if ((ArrayBuffer.isView(x) && bits == x.BYTES_PER_ELEMENT * 8) || | ||
(Array.isArray(x) && | ||
x.every((v, idx) => { | ||
try { | ||
return this._type.covariant(v); | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${idx} -> ${e.message}`); | ||
} | ||
}))) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -731,3 +762,10 @@ encodeValue(x) { | ||
covariant(x) { | ||
return Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0]))); | ||
try { | ||
if (Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0])))) | ||
return true; | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)} \n\n-> ${e.message}`); | ||
} | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -802,3 +840,3 @@ encodeValue(x) { | ||
covariant(x) { | ||
return (typeof x === 'object' && | ||
if (typeof x === 'object' && | ||
this._fields.every(([k, t]) => { | ||
@@ -809,4 +847,11 @@ // eslint-disable-next-line | ||
} | ||
return t.covariant(x[k]); | ||
})); | ||
try { | ||
return t.covariant(x[k]); | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid ${this.display()} argument: \n\nfield ${k} -> ${e.message}`); | ||
} | ||
})) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -842,18 +887,25 @@ encodeValue(x) { | ||
const [expectKey, expectType] = this._fields[expectedRecordIdx]; | ||
if ((0, hash_1.idlLabelToId)(this._fields[expectedRecordIdx][0]) !== (0, hash_1.idlLabelToId)(hash)) { | ||
// the current field on the wire does not match the expected field | ||
// skip expected optional fields that are not present on the wire | ||
const expectedId = (0, hash_1.idlLabelToId)(this._fields[expectedRecordIdx][0]); | ||
const actualId = (0, hash_1.idlLabelToId)(hash); | ||
if (expectedId === actualId) { | ||
// the current field on the wire matches the expected field | ||
x[expectKey] = expectType.decodeValue(b, type); | ||
expectedRecordIdx++; | ||
actualRecordIdx++; | ||
} | ||
else if (actualId > expectedId) { | ||
// The expected field does not exist on the wire | ||
if (expectType instanceof OptClass || expectType instanceof ReservedClass) { | ||
x[expectKey] = []; | ||
expectedRecordIdx++; | ||
continue; | ||
} | ||
// skip unexpected interspersed fields present on the wire | ||
else { | ||
throw new Error('Cannot find required field ' + expectKey); | ||
} | ||
} | ||
else { | ||
// The field on the wire does not exist in the output type, so we can skip it | ||
type.decodeValue(b, type); | ||
actualRecordIdx++; | ||
continue; | ||
} | ||
x[expectKey] = expectType.decodeValue(b, type); | ||
expectedRecordIdx++; | ||
actualRecordIdx++; | ||
} | ||
@@ -903,5 +955,14 @@ // initialize left over expected optional fields | ||
// `>=` because tuples can be covariant when encoded. | ||
return (Array.isArray(x) && | ||
if (Array.isArray(x) && | ||
x.length >= this._fields.length && | ||
this._components.every((t, i) => t.covariant(x[i]))); | ||
this._components.every((t, i) => { | ||
try { | ||
return t.covariant(x[i]); | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${i} -> ${e.message}`); | ||
} | ||
})) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -955,8 +1016,15 @@ encodeValue(x) { | ||
covariant(x) { | ||
return (typeof x === 'object' && | ||
if (typeof x === 'object' && | ||
Object.entries(x).length === 1 && | ||
this._fields.every(([k, v]) => { | ||
// eslint-disable-next-line | ||
return !x.hasOwnProperty(k) || v.covariant(x[k]); | ||
})); | ||
try { | ||
// eslint-disable-next-line | ||
return !x.hasOwnProperty(k) || v.covariant(x[k]); | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid ${this.display()} argument: \n\nvariant ${k} -> ${e.message}`); | ||
} | ||
})) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -1050,3 +1118,5 @@ encodeValue(x) { | ||
covariant(x) { | ||
return this._type ? this._type.covariant(x) : false; | ||
if (this._type ? this._type.covariant(x) : false) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -1107,3 +1177,5 @@ encodeValue(x) { | ||
covariant(x) { | ||
return x && x._isPrincipal; | ||
if (x && x._isPrincipal) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -1153,3 +1225,5 @@ encodeValue(x) { | ||
covariant(x) { | ||
return (Array.isArray(x) && x.length === 2 && x[0] && x[0]._isPrincipal && typeof x[1] === 'string'); | ||
if (Array.isArray(x) && x.length === 2 && x[0] && x[0]._isPrincipal && typeof x[1] === 'string') | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -1225,3 +1299,5 @@ encodeValue([principal, methodName]) { | ||
covariant(x) { | ||
return x && x._isPrincipal; | ||
if (x && x._isPrincipal) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -1262,3 +1338,6 @@ encodeValue(x) { | ||
function toReadableString(x) { | ||
return JSON.stringify(x, (_key, value) => typeof value === 'bigint' ? `BigInt(${value})` : value); | ||
const str = JSON.stringify(x, (_key, value) => typeof value === 'bigint' ? `BigInt(${value})` : value); | ||
return str && str.length > toReadableString_max | ||
? str.substring(0, toReadableString_max - 3) + '...' | ||
: str; | ||
} | ||
@@ -1282,5 +1361,9 @@ /** | ||
const vals = (0, buffer_1.concat)(...zipWith(argTypes, args, (t, x) => { | ||
if (!t.covariant(x)) { | ||
throw new Error(`Invalid ${t.display()} argument: ${toReadableString(x)}`); | ||
try { | ||
t.covariant(x); | ||
} | ||
catch (e) { | ||
const err = new Error(e.message + '\n\n'); | ||
throw err; | ||
} | ||
return t.encodeValue(x); | ||
@@ -1287,0 +1370,0 @@ })); |
@@ -8,2 +8,3 @@ // tslint:disable:max-classes-per-file | ||
const magicNumber = 'DIDL'; | ||
const toReadableString_max = 400; // will not display arguments after 400chars. Makes sure 2mb blobs don't get inside the error | ||
function zipWith(xs, ys, f) { | ||
@@ -182,3 +183,3 @@ return xs.map((x, i) => f(x, ys[i])); | ||
covariant(x) { | ||
return false; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -216,3 +217,3 @@ encodeValue() { | ||
covariant(x) { | ||
return false; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -270,3 +271,5 @@ encodeValue() { | ||
covariant(x) { | ||
return typeof x === 'boolean'; | ||
if (typeof x === 'boolean') | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -302,3 +305,5 @@ encodeValue(x) { | ||
covariant(x) { | ||
return x === null; | ||
if (x === null) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -353,3 +358,5 @@ encodeValue() { | ||
covariant(x) { | ||
return typeof x === 'string'; | ||
if (typeof x === 'string') | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -388,3 +395,5 @@ encodeValue(x) { | ||
// But we will always decode to bigint. | ||
return typeof x === 'bigint' || Number.isInteger(x); | ||
if (typeof x === 'bigint' || Number.isInteger(x)) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -418,3 +427,5 @@ encodeValue(x) { | ||
// But we will always decode to bigint. | ||
return (typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0); | ||
if ((typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0)) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -453,3 +464,5 @@ encodeValue(x) { | ||
covariant(x) { | ||
return typeof x === 'number' || x instanceof Number; | ||
if (typeof x === 'number' || x instanceof Number) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -503,12 +516,16 @@ encodeValue(x) { | ||
const max = iexp2(this._bits - 1) - BigInt(1); | ||
let ok = false; | ||
if (typeof x === 'bigint') { | ||
return x >= min && x <= max; | ||
ok = x >= min && x <= max; | ||
} | ||
else if (Number.isInteger(x)) { | ||
const v = BigInt(x); | ||
return v >= min && v <= max; | ||
ok = v >= min && v <= max; | ||
} | ||
else { | ||
return false; | ||
ok = false; | ||
} | ||
if (ok) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -552,12 +569,16 @@ encodeValue(x) { | ||
const max = iexp2(this._bits); | ||
let ok = false; | ||
if (typeof x === 'bigint' && x >= BigInt(0)) { | ||
return x < max; | ||
ok = x < max; | ||
} | ||
else if (Number.isInteger(x) && x >= 0) { | ||
const v = BigInt(x); | ||
return v < max; | ||
ok = v < max; | ||
} | ||
else { | ||
return false; | ||
ok = false; | ||
} | ||
if (ok) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -621,4 +642,14 @@ encodeValue(x) { | ||
: 0; | ||
return ((ArrayBuffer.isView(x) && bits == x.BYTES_PER_ELEMENT * 8) || | ||
(Array.isArray(x) && x.every(v => this._type.covariant(v)))); | ||
if ((ArrayBuffer.isView(x) && bits == x.BYTES_PER_ELEMENT * 8) || | ||
(Array.isArray(x) && | ||
x.every((v, idx) => { | ||
try { | ||
return this._type.covariant(v); | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${idx} -> ${e.message}`); | ||
} | ||
}))) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -711,3 +742,10 @@ encodeValue(x) { | ||
covariant(x) { | ||
return Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0]))); | ||
try { | ||
if (Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0])))) | ||
return true; | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)} \n\n-> ${e.message}`); | ||
} | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -781,3 +819,3 @@ encodeValue(x) { | ||
covariant(x) { | ||
return (typeof x === 'object' && | ||
if (typeof x === 'object' && | ||
this._fields.every(([k, t]) => { | ||
@@ -788,4 +826,11 @@ // eslint-disable-next-line | ||
} | ||
return t.covariant(x[k]); | ||
})); | ||
try { | ||
return t.covariant(x[k]); | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid ${this.display()} argument: \n\nfield ${k} -> ${e.message}`); | ||
} | ||
})) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -821,18 +866,25 @@ encodeValue(x) { | ||
const [expectKey, expectType] = this._fields[expectedRecordIdx]; | ||
if (idlLabelToId(this._fields[expectedRecordIdx][0]) !== idlLabelToId(hash)) { | ||
// the current field on the wire does not match the expected field | ||
// skip expected optional fields that are not present on the wire | ||
const expectedId = idlLabelToId(this._fields[expectedRecordIdx][0]); | ||
const actualId = idlLabelToId(hash); | ||
if (expectedId === actualId) { | ||
// the current field on the wire matches the expected field | ||
x[expectKey] = expectType.decodeValue(b, type); | ||
expectedRecordIdx++; | ||
actualRecordIdx++; | ||
} | ||
else if (actualId > expectedId) { | ||
// The expected field does not exist on the wire | ||
if (expectType instanceof OptClass || expectType instanceof ReservedClass) { | ||
x[expectKey] = []; | ||
expectedRecordIdx++; | ||
continue; | ||
} | ||
// skip unexpected interspersed fields present on the wire | ||
else { | ||
throw new Error('Cannot find required field ' + expectKey); | ||
} | ||
} | ||
else { | ||
// The field on the wire does not exist in the output type, so we can skip it | ||
type.decodeValue(b, type); | ||
actualRecordIdx++; | ||
continue; | ||
} | ||
x[expectKey] = expectType.decodeValue(b, type); | ||
expectedRecordIdx++; | ||
actualRecordIdx++; | ||
} | ||
@@ -881,5 +933,14 @@ // initialize left over expected optional fields | ||
// `>=` because tuples can be covariant when encoded. | ||
return (Array.isArray(x) && | ||
if (Array.isArray(x) && | ||
x.length >= this._fields.length && | ||
this._components.every((t, i) => t.covariant(x[i]))); | ||
this._components.every((t, i) => { | ||
try { | ||
return t.covariant(x[i]); | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${i} -> ${e.message}`); | ||
} | ||
})) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -932,8 +993,15 @@ encodeValue(x) { | ||
covariant(x) { | ||
return (typeof x === 'object' && | ||
if (typeof x === 'object' && | ||
Object.entries(x).length === 1 && | ||
this._fields.every(([k, v]) => { | ||
// eslint-disable-next-line | ||
return !x.hasOwnProperty(k) || v.covariant(x[k]); | ||
})); | ||
try { | ||
// eslint-disable-next-line | ||
return !x.hasOwnProperty(k) || v.covariant(x[k]); | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid ${this.display()} argument: \n\nvariant ${k} -> ${e.message}`); | ||
} | ||
})) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -1026,3 +1094,5 @@ encodeValue(x) { | ||
covariant(x) { | ||
return this._type ? this._type.covariant(x) : false; | ||
if (this._type ? this._type.covariant(x) : false) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -1082,3 +1152,5 @@ encodeValue(x) { | ||
covariant(x) { | ||
return x && x._isPrincipal; | ||
if (x && x._isPrincipal) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -1127,3 +1199,5 @@ encodeValue(x) { | ||
covariant(x) { | ||
return (Array.isArray(x) && x.length === 2 && x[0] && x[0]._isPrincipal && typeof x[1] === 'string'); | ||
if (Array.isArray(x) && x.length === 2 && x[0] && x[0]._isPrincipal && typeof x[1] === 'string') | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -1198,3 +1272,5 @@ encodeValue([principal, methodName]) { | ||
covariant(x) { | ||
return x && x._isPrincipal; | ||
if (x && x._isPrincipal) | ||
return true; | ||
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`); | ||
} | ||
@@ -1234,3 +1310,6 @@ encodeValue(x) { | ||
function toReadableString(x) { | ||
return JSON.stringify(x, (_key, value) => typeof value === 'bigint' ? `BigInt(${value})` : value); | ||
const str = JSON.stringify(x, (_key, value) => typeof value === 'bigint' ? `BigInt(${value})` : value); | ||
return str && str.length > toReadableString_max | ||
? str.substring(0, toReadableString_max - 3) + '...' | ||
: str; | ||
} | ||
@@ -1254,5 +1333,9 @@ /** | ||
const vals = concat(...zipWith(argTypes, args, (t, x) => { | ||
if (!t.covariant(x)) { | ||
throw new Error(`Invalid ${t.display()} argument: ${toReadableString(x)}`); | ||
try { | ||
t.covariant(x); | ||
} | ||
catch (e) { | ||
const err = new Error(e.message + '\n\n'); | ||
throw err; | ||
} | ||
return t.encodeValue(x); | ||
@@ -1259,0 +1342,0 @@ })); |
{ | ||
"name": "@dfinity/candid", | ||
"version": "0.13.3", | ||
"version": "0.14.0", | ||
"author": "DFINITY Stiftung <sdk@dfinity.org>", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
620390
6702