utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const buffer_1 = require("buffer");
  4. /**
  5. * Error strings
  6. */
  7. const ERRORS = {
  8. INVALID_ENCODING: 'Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.',
  9. INVALID_SMARTBUFFER_SIZE: 'Invalid size provided. Size must be a valid integer greater than zero.',
  10. INVALID_SMARTBUFFER_BUFFER: 'Invalid Buffer provided in SmartBufferOptions.',
  11. INVALID_SMARTBUFFER_OBJECT: 'Invalid SmartBufferOptions object supplied to SmartBuffer constructor or factory methods.',
  12. INVALID_OFFSET: 'An invalid offset value was provided.',
  13. INVALID_OFFSET_NON_NUMBER: 'An invalid offset value was provided. A numeric value is required.',
  14. INVALID_LENGTH: 'An invalid length value was provided.',
  15. INVALID_LENGTH_NON_NUMBER: 'An invalid length value was provived. A numeric value is required.',
  16. INVALID_TARGET_OFFSET: 'Target offset is beyond the bounds of the internal SmartBuffer data.',
  17. INVALID_TARGET_LENGTH: 'Specified length value moves cursor beyong the bounds of the internal SmartBuffer data.',
  18. INVALID_READ_BEYOND_BOUNDS: 'Attempted to read beyond the bounds of the managed data.',
  19. INVALID_WRITE_BEYOND_BOUNDS: 'Attempted to write beyond the bounds of the managed data.'
  20. };
  21. exports.ERRORS = ERRORS;
  22. /**
  23. * Checks if a given encoding is a valid Buffer encoding. (Throws an exception if check fails)
  24. *
  25. * @param { String } encoding The encoding string to check.
  26. */
  27. function checkEncoding(encoding) {
  28. if (!buffer_1.Buffer.isEncoding(encoding)) {
  29. throw new Error(ERRORS.INVALID_ENCODING);
  30. }
  31. }
  32. exports.checkEncoding = checkEncoding;
  33. /**
  34. * Checks if a given number is a finite integer. (Throws an exception if check fails)
  35. *
  36. * @param { Number } value The number value to check.
  37. */
  38. function isFiniteInteger(value) {
  39. return typeof value === 'number' && isFinite(value) && isInteger(value);
  40. }
  41. exports.isFiniteInteger = isFiniteInteger;
  42. /**
  43. * Checks if an offset/length value is valid. (Throws an exception if check fails)
  44. *
  45. * @param value The value to check.
  46. * @param offset True if checking an offset, false if checking a length.
  47. */
  48. function checkOffsetOrLengthValue(value, offset) {
  49. if (typeof value === 'number') {
  50. // Check for non finite/non integers
  51. if (!isFiniteInteger(value) || value < 0) {
  52. throw new Error(offset ? ERRORS.INVALID_OFFSET : ERRORS.INVALID_LENGTH);
  53. }
  54. }
  55. else {
  56. throw new Error(offset ? ERRORS.INVALID_OFFSET_NON_NUMBER : ERRORS.INVALID_LENGTH_NON_NUMBER);
  57. }
  58. }
  59. /**
  60. * Checks if a length value is valid. (Throws an exception if check fails)
  61. *
  62. * @param { Number } length The value to check.
  63. */
  64. function checkLengthValue(length) {
  65. checkOffsetOrLengthValue(length, false);
  66. }
  67. exports.checkLengthValue = checkLengthValue;
  68. /**
  69. * Checks if a offset value is valid. (Throws an exception if check fails)
  70. *
  71. * @param { Number } offset The value to check.
  72. */
  73. function checkOffsetValue(offset) {
  74. checkOffsetOrLengthValue(offset, true);
  75. }
  76. exports.checkOffsetValue = checkOffsetValue;
  77. /**
  78. * Checks if a target offset value is out of bounds. (Throws an exception if check fails)
  79. *
  80. * @param { Number } offset The offset value to check.
  81. * @param { SmartBuffer } buff The SmartBuffer instance to check against.
  82. */
  83. function checkTargetOffset(offset, buff) {
  84. if (offset < 0 || offset > buff.length) {
  85. throw new Error(ERRORS.INVALID_TARGET_OFFSET);
  86. }
  87. }
  88. exports.checkTargetOffset = checkTargetOffset;
  89. /**
  90. * Determines whether a given number is a integer.
  91. * @param value The number to check.
  92. */
  93. function isInteger(value) {
  94. return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
  95. }
  96. /**
  97. * Throws if Node.js version is too low to support bigint
  98. */
  99. function bigIntAndBufferInt64Check(bufferMethod) {
  100. if (typeof BigInt === 'undefined') {
  101. throw new Error('Platform does not support JS BigInt type.');
  102. }
  103. if (typeof buffer_1.Buffer.prototype[bufferMethod] === 'undefined') {
  104. throw new Error(`Platform does not support Buffer.prototype.${bufferMethod}.`);
  105. }
  106. }
  107. exports.bigIntAndBufferInt64Check = bigIntAndBufferInt64Check;
  108. //# sourceMappingURL=utils.js.map