_assert.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.output = exports.exists = exports.hash = exports.bytes = exports.bool = exports.number = exports.isBytes = void 0;
  4. function number(n) {
  5. if (!Number.isSafeInteger(n) || n < 0)
  6. throw new Error(`positive integer expected, not ${n}`);
  7. }
  8. exports.number = number;
  9. function bool(b) {
  10. if (typeof b !== 'boolean')
  11. throw new Error(`boolean expected, not ${b}`);
  12. }
  13. exports.bool = bool;
  14. // copied from utils
  15. function isBytes(a) {
  16. return (a instanceof Uint8Array ||
  17. (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
  18. }
  19. exports.isBytes = isBytes;
  20. function bytes(b, ...lengths) {
  21. if (!isBytes(b))
  22. throw new Error('Uint8Array expected');
  23. if (lengths.length > 0 && !lengths.includes(b.length))
  24. throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
  25. }
  26. exports.bytes = bytes;
  27. function hash(h) {
  28. if (typeof h !== 'function' || typeof h.create !== 'function')
  29. throw new Error('Hash should be wrapped by utils.wrapConstructor');
  30. number(h.outputLen);
  31. number(h.blockLen);
  32. }
  33. exports.hash = hash;
  34. function exists(instance, checkFinished = true) {
  35. if (instance.destroyed)
  36. throw new Error('Hash instance has been destroyed');
  37. if (checkFinished && instance.finished)
  38. throw new Error('Hash#digest() has already been called');
  39. }
  40. exports.exists = exists;
  41. function output(out, instance) {
  42. bytes(out);
  43. const min = instance.outputLen;
  44. if (out.length < min) {
  45. throw new Error(`digestInto() expects output buffer of length at least ${min}`);
  46. }
  47. }
  48. exports.output = output;
  49. const assert = { number, bool, bytes, hash, exists, output };
  50. exports.default = assert;
  51. //# sourceMappingURL=_assert.js.map