Socket
Socket
Sign inDemoInstall

@dfinity/candid

Package Overview
Dependencies
Maintainers
10
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dfinity/candid - npm Package Compare versions

Comparing version 1.0.0-beta.0 to 1.0.0

2

lib/cjs/candid-ui.js

@@ -27,2 +27,4 @@ "use strict";

exports.renderValue = exports.renderInput = exports.Render = exports.vecForm = exports.optForm = exports.variantForm = exports.tupleForm = exports.recordForm = exports.inputBox = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
const IDL = __importStar(require("./idl"));

@@ -29,0 +31,0 @@ const principal_1 = require("@dfinity/principal");

11

lib/cjs/idl.d.ts

@@ -226,3 +226,2 @@ import { Principal as PrincipalId } from '@dfinity/principal';

* Arrays of float or other non-primitive types are encoded/decoded as untyped array in Javascript.
*
* @param {Type} t

@@ -261,3 +260,3 @@ */

* Represents an IDL Record
* @param {Object} [fields] - mapping of function name to Type
* @param {object} [fields] - mapping of function name to Type
*/

@@ -293,3 +292,3 @@ export declare class RecordClass extends ConstructType<Record<string, any>> {

* Represents an IDL Variant
* @param {Object} [fields] - mapping of function name to Type
* @param {object} [fields] - mapping of function name to Type
*/

@@ -376,5 +375,5 @@ export declare class VariantClass extends ConstructType<Record<string, any>> {

* Encode a array of values
* @param argTypes
* @param args
* @returns {Buffer} serialised value
* @param argTypes - array of Types
* @param args - array of values
* @returns {ArrayBuffer} serialised value
*/

@@ -381,0 +380,0 @@ export declare function encode(argTypes: Array<Type<any>>, args: any[]): ArrayBuffer;

@@ -5,3 +5,4 @@ "use strict";

exports.Service = exports.Func = exports.Rec = void 0;
// tslint:disable:max-classes-per-file
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
const principal_1 = require("@dfinity/principal");

@@ -13,2 +14,3 @@ const buffer_1 = require("./utils/buffer");

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) {

@@ -159,2 +161,3 @@ return xs.map((x, i) => f(x, ys[i]));

}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_buildTypeTableImpl(typeTable) {

@@ -192,3 +195,3 @@ // No type table encoding for Primitive types.

covariant(x) {
return false;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -227,3 +230,3 @@ encodeValue() {

covariant(x) {
return false;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -282,3 +285,5 @@ encodeValue() {

covariant(x) {
return typeof x === 'boolean';
if (typeof x === 'boolean')
return true;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -315,3 +320,5 @@ encodeValue(x) {

covariant(x) {
return x === null;
if (x === null)
return true;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -368,3 +375,5 @@ encodeValue() {

covariant(x) {
return typeof x === 'string';
if (typeof x === 'string')
return true;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -404,3 +413,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)}`);
}

@@ -435,3 +446,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)}`);
}

@@ -471,3 +484,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)}`);
}

@@ -522,12 +537,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)}`);
}

@@ -572,12 +591,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)}`);
}

@@ -614,3 +637,2 @@ encodeValue(x) {

* Arrays of float or other non-primitive types are encoded/decoded as untyped array in Javascript.
*
* @param {Type} t

@@ -643,4 +665,14 @@ */

: 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)}`);
}

@@ -734,3 +766,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)}`);
}

@@ -783,3 +822,3 @@ encodeValue(x) {

* Represents an IDL Record
* @param {Object} [fields] - mapping of function name to Type
* @param {object} [fields] - mapping of function name to Type
*/

@@ -806,3 +845,3 @@ class RecordClass extends ConstructType {

covariant(x) {
return (typeof x === 'object' &&
if (typeof x === 'object' &&
this._fields.every(([k, t]) => {

@@ -813,4 +852,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)}`);
}

@@ -846,18 +892,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++;
}

@@ -907,5 +960,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)}`);
}

@@ -948,3 +1010,3 @@ encodeValue(x) {

* Represents an IDL Variant
* @param {Object} [fields] - mapping of function name to Type
* @param {object} [fields] - mapping of function name to Type
*/

@@ -960,8 +1022,15 @@ class VariantClass extends ConstructType {

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)}`);
}

@@ -1055,3 +1124,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)}`);
}

@@ -1112,3 +1183,5 @@ encodeValue(x) {

covariant(x) {
return x && x._isPrincipal;
if (x && x._isPrincipal)
return true;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -1158,3 +1231,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)}`);
}

@@ -1215,2 +1290,5 @@ encodeValue([principal, methodName]) {

}
else if (ann === 'composite_query') {
return new Uint8Array([3]);
}
else {

@@ -1225,3 +1303,11 @@ throw new Error('Illegal function annotation');

super();
this._fields = Object.entries(fields).sort((a, b) => (0, hash_1.idlLabelToId)(a[0]) - (0, hash_1.idlLabelToId)(b[0]));
this._fields = Object.entries(fields).sort((a, b) => {
if (a[0] < b[0]) {
return -1;
}
if (a[0] > b[0]) {
return 1;
}
return 0;
});
}

@@ -1232,3 +1318,5 @@ accept(v, d) {

covariant(x) {
return x && x._isPrincipal;
if (x && x._isPrincipal)
return true;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -1264,14 +1352,17 @@ encodeValue(x) {

/**
*
* @param x
* @returns {string}
* Takes an unknown value and returns a string representation of it.
* @param x - unknown value
* @returns {string} string representation of the value
*/
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;
}
/**
* Encode a array of values
* @param argTypes
* @param args
* @returns {Buffer} serialised value
* @param argTypes - array of Types
* @param args - array of values
* @returns {ArrayBuffer} serialised value
*/

@@ -1289,5 +1380,9 @@ function encode(argTypes, args) {

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);

@@ -1370,2 +1465,6 @@ }));

}
case 3: {
annotations.push('composite_query');
break;
}
default:

@@ -1515,5 +1614,14 @@ throw new Error('unknown annotation');

rawTable.forEach((entry, i) => {
const t = buildType(entry);
table[i].fill(t);
// Process function type first, so that we can construct the correct service type
if (entry[0] === -22 /* IDLTypeIds.Func */) {
const t = buildType(entry);
table[i].fill(t);
}
});
rawTable.forEach((entry, i) => {
if (entry[0] !== -22 /* IDLTypeIds.Func */) {
const t = buildType(entry);
table[i].fill(t);
}
});
const types = rawTypes.map(t => getType(t));

@@ -1520,0 +1628,0 @@ const output = retTypes.map((t, i) => {

/**
* Equivalent to `Math.log2(n)` with support for `BigInt` values
*
* @param n bigint or integer

@@ -11,3 +10,2 @@ * @returns integer

* (necessary for browser preprocessors which replace the `**` operator with `Math.pow`)
*
* @param n bigint or integer

@@ -14,0 +12,0 @@ * @returns bigint

@@ -6,3 +6,2 @@ "use strict";

* Equivalent to `Math.log2(n)` with support for `BigInt` values
*
* @param n bigint or integer

@@ -22,3 +21,2 @@ * @returns integer

* (necessary for browser preprocessors which replace the `**` operator with `Math.pow`)
*
* @param n bigint or integer

@@ -25,0 +23,0 @@ * @returns bigint

@@ -59,1 +59,13 @@ /**

}
/**
* Returns a true ArrayBuffer from a Uint8Array, as Uint8Array.buffer is unsafe.
* @param {Uint8Array} arr Uint8Array to convert
* @returns ArrayBuffer
*/
export declare function uint8ToBuf(arr: Uint8Array): ArrayBuffer;
/**
* Returns a true ArrayBuffer from an ArrayBufferLike object.
* @param bufLike a buffer-like object
* @returns ArrayBuffer
*/
export declare function bufFromBufLike(bufLike: ArrayBuffer | Uint8Array | DataView | ArrayBufferView | ArrayBufferLike): ArrayBuffer;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PipeArrayBuffer = exports.fromHexString = exports.toHexString = exports.concat = void 0;
exports.bufFromBufLike = exports.uint8ToBuf = exports.PipeArrayBuffer = exports.fromHexString = exports.toHexString = exports.concat = void 0;
/**

@@ -45,3 +45,3 @@ * Concatenate multiple array buffers.

constructor(buffer, length = (buffer === null || buffer === void 0 ? void 0 : buffer.byteLength) || 0) {
this._buffer = buffer || new ArrayBuffer(0);
this._buffer = bufFromBufLike(buffer || new ArrayBuffer(0));
this._view = new Uint8Array(this._buffer, 0, length);

@@ -51,3 +51,3 @@ }

// Return a copy of the buffer.
return this._view.slice();
return bufFromBufLike(this._view.slice());
}

@@ -100,3 +100,2 @@ get byteLength() {

// Add a little bit of exponential growth.
// tslint:disable-next-line:no-bitwise
const b = new ArrayBuffer(((this._buffer.byteLength + amount) * 1.2) | 0);

@@ -110,2 +109,29 @@ const v = new Uint8Array(b, 0, this._view.byteLength + amount);

exports.PipeArrayBuffer = PipeArrayBuffer;
/**
* Returns a true ArrayBuffer from a Uint8Array, as Uint8Array.buffer is unsafe.
* @param {Uint8Array} arr Uint8Array to convert
* @returns ArrayBuffer
*/
function uint8ToBuf(arr) {
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength).buffer;
}
exports.uint8ToBuf = uint8ToBuf;
/**
* Returns a true ArrayBuffer from an ArrayBufferLike object.
* @param bufLike a buffer-like object
* @returns ArrayBuffer
*/
function bufFromBufLike(bufLike) {
if (bufLike instanceof Uint8Array) {
return uint8ToBuf(bufLike);
}
if (bufLike instanceof ArrayBuffer) {
return bufLike;
}
if ('buffer' in bufLike) {
return bufLike.buffer;
}
return new Uint8Array(bufLike);
}
exports.bufFromBufLike = bufFromBufLike;
//# sourceMappingURL=buffer.js.map

@@ -7,3 +7,4 @@ "use strict";

* https://caml.inria.fr/pub/papers/garrigue-polymorphic_variants-ml98.pdf
* @param s
* @param s - string to hash
* @returns number representing hashed string
*/

@@ -10,0 +11,0 @@ function idlHash(s) {

@@ -10,3 +10,3 @@ import { PipeArrayBuffer as Pipe } from './buffer';

/**
* @param pipe
* @param pipe - PipeArrayBuffer simulating buffer-pipe api
*/

@@ -47,4 +47,5 @@ export declare function safeReadUint8(pipe: Pipe): number;

*
* @param value
* @param byteLength
* @param value - bigint or number
* @param byteLength - number
* @returns ArrayBuffer
*/

@@ -51,0 +52,0 @@ export declare function writeIntLE(value: bigint | number, byteLength: number): ArrayBuffer;

@@ -5,3 +5,2 @@ "use strict";

/* eslint-disable no-constant-condition */
// tslint:disable:no-bitwise
// Note: this file uses buffer-pipe, which on Node only, uses the Node Buffer

@@ -32,3 +31,3 @@ // implementation, which isn't compatible with the NPM buffer package

/**
* @param pipe
* @param pipe - PipeArrayBuffer simulating buffer-pipe api
*/

@@ -170,4 +169,5 @@ function safeReadUint8(pipe) {

*
* @param value
* @param byteLength
* @param value - bigint or number
* @param byteLength - number
* @returns ArrayBuffer
*/

@@ -174,0 +174,0 @@ function writeIntLE(value, byteLength) {

@@ -0,1 +1,3 @@

/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import * as IDL from './idl';

@@ -2,0 +4,0 @@ import { Principal } from '@dfinity/principal';

@@ -226,3 +226,2 @@ import { Principal as PrincipalId } from '@dfinity/principal';

* Arrays of float or other non-primitive types are encoded/decoded as untyped array in Javascript.
*
* @param {Type} t

@@ -261,3 +260,3 @@ */

* Represents an IDL Record
* @param {Object} [fields] - mapping of function name to Type
* @param {object} [fields] - mapping of function name to Type
*/

@@ -293,3 +292,3 @@ export declare class RecordClass extends ConstructType<Record<string, any>> {

* Represents an IDL Variant
* @param {Object} [fields] - mapping of function name to Type
* @param {object} [fields] - mapping of function name to Type
*/

@@ -376,5 +375,5 @@ export declare class VariantClass extends ConstructType<Record<string, any>> {

* Encode a array of values
* @param argTypes
* @param args
* @returns {Buffer} serialised value
* @param argTypes - array of Types
* @param args - array of values
* @returns {ArrayBuffer} serialised value
*/

@@ -381,0 +380,0 @@ export declare function encode(argTypes: Array<Type<any>>, args: any[]): ArrayBuffer;

@@ -1,2 +0,3 @@

// tslint:disable:max-classes-per-file
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Principal as PrincipalId } from '@dfinity/principal';

@@ -8,2 +9,3 @@ import { concat, PipeArrayBuffer as Pipe } from './utils/buffer';

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) {

@@ -152,2 +154,3 @@ return xs.map((x, i) => f(x, ys[i]));

}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_buildTypeTableImpl(typeTable) {

@@ -183,3 +186,3 @@ // No type table encoding for Primitive types.

covariant(x) {
return false;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -217,3 +220,3 @@ encodeValue() {

covariant(x) {
return false;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -271,3 +274,5 @@ encodeValue() {

covariant(x) {
return typeof x === 'boolean';
if (typeof x === 'boolean')
return true;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -303,3 +308,5 @@ encodeValue(x) {

covariant(x) {
return x === null;
if (x === null)
return true;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -354,3 +361,5 @@ encodeValue() {

covariant(x) {
return typeof x === 'string';
if (typeof x === 'string')
return true;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -389,3 +398,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)}`);
}

@@ -419,3 +430,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)}`);
}

@@ -454,3 +467,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)}`);
}

@@ -504,12 +519,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)}`);
}

@@ -553,12 +572,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)}`);
}

@@ -594,3 +617,2 @@ encodeValue(x) {

* Arrays of float or other non-primitive types are encoded/decoded as untyped array in Javascript.
*
* @param {Type} t

@@ -623,4 +645,14 @@ */

: 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)}`);
}

@@ -713,3 +745,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)}`);
}

@@ -761,3 +800,3 @@ encodeValue(x) {

* Represents an IDL Record
* @param {Object} [fields] - mapping of function name to Type
* @param {object} [fields] - mapping of function name to Type
*/

@@ -784,3 +823,3 @@ export class RecordClass extends ConstructType {

covariant(x) {
return (typeof x === 'object' &&
if (typeof x === 'object' &&
this._fields.every(([k, t]) => {

@@ -791,4 +830,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)}`);
}

@@ -824,18 +870,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++;
}

@@ -884,5 +937,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)}`);
}

@@ -924,3 +986,3 @@ encodeValue(x) {

* Represents an IDL Variant
* @param {Object} [fields] - mapping of function name to Type
* @param {object} [fields] - mapping of function name to Type
*/

@@ -936,8 +998,15 @@ export class VariantClass extends ConstructType {

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)}`);
}

@@ -1030,3 +1099,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)}`);
}

@@ -1086,3 +1157,5 @@ encodeValue(x) {

covariant(x) {
return x && x._isPrincipal;
if (x && x._isPrincipal)
return true;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -1131,3 +1204,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)}`);
}

@@ -1188,2 +1263,5 @@ encodeValue([principal, methodName]) {

}
else if (ann === 'composite_query') {
return new Uint8Array([3]);
}
else {

@@ -1197,3 +1275,11 @@ throw new Error('Illegal function annotation');

super();
this._fields = Object.entries(fields).sort((a, b) => idlLabelToId(a[0]) - idlLabelToId(b[0]));
this._fields = Object.entries(fields).sort((a, b) => {
if (a[0] < b[0]) {
return -1;
}
if (a[0] > b[0]) {
return 1;
}
return 0;
});
}

@@ -1204,3 +1290,5 @@ accept(v, d) {

covariant(x) {
return x && x._isPrincipal;
if (x && x._isPrincipal)
return true;
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
}

@@ -1235,14 +1323,17 @@ encodeValue(x) {

/**
*
* @param x
* @returns {string}
* Takes an unknown value and returns a string representation of it.
* @param x - unknown value
* @returns {string} string representation of the value
*/
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;
}
/**
* Encode a array of values
* @param argTypes
* @param args
* @returns {Buffer} serialised value
* @param argTypes - array of Types
* @param args - array of values
* @returns {ArrayBuffer} serialised value
*/

@@ -1260,5 +1351,9 @@ export function encode(argTypes, args) {

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);

@@ -1340,2 +1435,6 @@ }));

}
case 3: {
annotations.push('composite_query');
break;
}
default:

@@ -1485,5 +1584,14 @@ throw new Error('unknown annotation');

rawTable.forEach((entry, i) => {
const t = buildType(entry);
table[i].fill(t);
// Process function type first, so that we can construct the correct service type
if (entry[0] === -22 /* IDLTypeIds.Func */) {
const t = buildType(entry);
table[i].fill(t);
}
});
rawTable.forEach((entry, i) => {
if (entry[0] !== -22 /* IDLTypeIds.Func */) {
const t = buildType(entry);
table[i].fill(t);
}
});
const types = rawTypes.map(t => getType(t));

@@ -1490,0 +1598,0 @@ const output = retTypes.map((t, i) => {

/**
* Equivalent to `Math.log2(n)` with support for `BigInt` values
*
* @param n bigint or integer

@@ -11,3 +10,2 @@ * @returns integer

* (necessary for browser preprocessors which replace the `**` operator with `Math.pow`)
*
* @param n bigint or integer

@@ -14,0 +12,0 @@ * @returns bigint

/**
* Equivalent to `Math.log2(n)` with support for `BigInt` values
*
* @param n bigint or integer

@@ -17,3 +16,2 @@ * @returns integer

* (necessary for browser preprocessors which replace the `**` operator with `Math.pow`)
*
* @param n bigint or integer

@@ -20,0 +18,0 @@ * @returns bigint

@@ -59,1 +59,13 @@ /**

}
/**
* Returns a true ArrayBuffer from a Uint8Array, as Uint8Array.buffer is unsafe.
* @param {Uint8Array} arr Uint8Array to convert
* @returns ArrayBuffer
*/
export declare function uint8ToBuf(arr: Uint8Array): ArrayBuffer;
/**
* Returns a true ArrayBuffer from an ArrayBufferLike object.
* @param bufLike a buffer-like object
* @returns ArrayBuffer
*/
export declare function bufFromBufLike(bufLike: ArrayBuffer | Uint8Array | DataView | ArrayBufferView | ArrayBufferLike): ArrayBuffer;

@@ -39,3 +39,3 @@ /**

constructor(buffer, length = (buffer === null || buffer === void 0 ? void 0 : buffer.byteLength) || 0) {
this._buffer = buffer || new ArrayBuffer(0);
this._buffer = bufFromBufLike(buffer || new ArrayBuffer(0));
this._view = new Uint8Array(this._buffer, 0, length);

@@ -45,3 +45,3 @@ }

// Return a copy of the buffer.
return this._view.slice();
return bufFromBufLike(this._view.slice());
}

@@ -94,3 +94,2 @@ get byteLength() {

// Add a little bit of exponential growth.
// tslint:disable-next-line:no-bitwise
const b = new ArrayBuffer(((this._buffer.byteLength + amount) * 1.2) | 0);

@@ -103,2 +102,27 @@ const v = new Uint8Array(b, 0, this._view.byteLength + amount);

}
/**
* Returns a true ArrayBuffer from a Uint8Array, as Uint8Array.buffer is unsafe.
* @param {Uint8Array} arr Uint8Array to convert
* @returns ArrayBuffer
*/
export function uint8ToBuf(arr) {
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength).buffer;
}
/**
* Returns a true ArrayBuffer from an ArrayBufferLike object.
* @param bufLike a buffer-like object
* @returns ArrayBuffer
*/
export function bufFromBufLike(bufLike) {
if (bufLike instanceof Uint8Array) {
return uint8ToBuf(bufLike);
}
if (bufLike instanceof ArrayBuffer) {
return bufLike;
}
if ('buffer' in bufLike) {
return bufLike.buffer;
}
return new Uint8Array(bufLike);
}
//# sourceMappingURL=buffer.js.map
/**
* Hashes a string to a number. Algorithm can be found here:
* https://caml.inria.fr/pub/papers/garrigue-polymorphic_variants-ml98.pdf
* @param s
* @param s - string to hash
* @returns number representing hashed string
*/

@@ -6,0 +7,0 @@ function idlHash(s) {

@@ -10,3 +10,3 @@ import { PipeArrayBuffer as Pipe } from './buffer';

/**
* @param pipe
* @param pipe - PipeArrayBuffer simulating buffer-pipe api
*/

@@ -47,4 +47,5 @@ export declare function safeReadUint8(pipe: Pipe): number;

*
* @param value
* @param byteLength
* @param value - bigint or number
* @param byteLength - number
* @returns ArrayBuffer
*/

@@ -51,0 +52,0 @@ export declare function writeIntLE(value: bigint | number, byteLength: number): ArrayBuffer;

/* eslint-disable no-constant-condition */
// tslint:disable:no-bitwise
// Note: this file uses buffer-pipe, which on Node only, uses the Node Buffer

@@ -27,3 +26,3 @@ // implementation, which isn't compatible with the NPM buffer package

/**
* @param pipe
* @param pipe - PipeArrayBuffer simulating buffer-pipe api
*/

@@ -159,4 +158,5 @@ export function safeReadUint8(pipe) {

*
* @param value
* @param byteLength
* @param value - bigint or number
* @param byteLength - number
* @returns ArrayBuffer
*/

@@ -163,0 +163,0 @@ export function writeIntLE(value, byteLength) {

{
"name": "@dfinity/candid",
"version": "1.0.0-beta.0",
"version": "1.0.0",
"author": "DFINITY Stiftung <sdk@dfinity.org>",
"license": "Apache-2.0",
"description": "JavaScript and TypeScript library to work with candid interfaces",
"homepage": "https://smartcontracts.org",
"homepage": "https://internetcomputer.org",
"repository": {

@@ -36,5 +36,7 @@ "type": "git",

"module": "./lib/esm/index.js",
"unpkg": "./lib/esm/index",
"scripts": {
"build": "tsc -b && tsc -p tsconfig-cjs.json",
"bundle": "npm run build",
"bundle": "esbuild --bundle src/index.ts --outfile=dist/index.js",
"size-limit": "size-limit",
"ci": "npm run prettier && npm run lint && npm run test",

@@ -47,23 +49,29 @@ "lint": "eslint 'src' --ext '.js,.jsx,.ts,.tsx'",

"test": "jest",
"test:coverage": "jest --verbose --collectCoverage"
"test:coverage": "jest --collectCoverage"
},
"peerDependencies": {
"@dfinity/principal": "^1.0.0"
},
"devDependencies": {
"@types/jest": "^28.1.4",
"@types/jest": "^29.5.5",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"esbuild": "^0.15.16",
"eslint": "^8.19.0",
"eslint-plugin-jsdoc": "^39.3.3",
"jest": "^28.1.2",
"jest": "^29.7.0",
"jest-diff": "^27.3.1",
"prettier": "^2.0.5",
"size-limit": "^8.2.6",
"text-encoding": "^0.7.0",
"ts-jest": "^28.0.5",
"tslint": "^5.20.0",
"ts-node": "^10.8.2",
"typedoc": "^0.22.11",
"typescript": "^4.7.4",
"whatwg-fetch": "^3.0.0"
},
"dependencies": {
"ts-node": "^10.8.2"
}
"size-limit": [
{
"path": "./dist/index.js",
"limit": "100 kB",
"webpack": false
}
]
}

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

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

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc