43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
|
function number(n) {
|
||
|
if (!Number.isSafeInteger(n) || n < 0)
|
||
|
throw new Error(`positive integer expected, not ${n}`);
|
||
|
}
|
||
|
function bool(b) {
|
||
|
if (typeof b !== 'boolean')
|
||
|
throw new Error(`boolean expected, not ${b}`);
|
||
|
}
|
||
|
// copied from utils
|
||
|
export function isBytes(a) {
|
||
|
return (a instanceof Uint8Array ||
|
||
|
(a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
|
||
|
}
|
||
|
function bytes(b, ...lengths) {
|
||
|
if (!isBytes(b))
|
||
|
throw new Error('Uint8Array expected');
|
||
|
if (lengths.length > 0 && !lengths.includes(b.length))
|
||
|
throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
|
||
|
}
|
||
|
function hash(h) {
|
||
|
if (typeof h !== 'function' || typeof h.create !== 'function')
|
||
|
throw new Error('Hash should be wrapped by utils.wrapConstructor');
|
||
|
number(h.outputLen);
|
||
|
number(h.blockLen);
|
||
|
}
|
||
|
function exists(instance, checkFinished = true) {
|
||
|
if (instance.destroyed)
|
||
|
throw new Error('Hash instance has been destroyed');
|
||
|
if (checkFinished && instance.finished)
|
||
|
throw new Error('Hash#digest() has already been called');
|
||
|
}
|
||
|
function output(out, instance) {
|
||
|
bytes(out);
|
||
|
const min = instance.outputLen;
|
||
|
if (out.length < min) {
|
||
|
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
||
|
}
|
||
|
}
|
||
|
export { number, bool, bytes, hash, exists, output };
|
||
|
const assert = { number, bool, bytes, hash, exists, output };
|
||
|
export default assert;
|
||
|
//# sourceMappingURL=_assert.js.map
|