ensure_buffer.js 979 B

123456789101112131415161718192021222324
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ensureBuffer = void 0;
  4. var buffer_1 = require("buffer");
  5. var utils_1 = require("./parser/utils");
  6. /**
  7. * Makes sure that, if a Uint8Array is passed in, it is wrapped in a Buffer.
  8. *
  9. * @param potentialBuffer - The potential buffer
  10. * @returns Buffer the input if potentialBuffer is a buffer, or a buffer that
  11. * wraps a passed in Uint8Array
  12. * @throws TypeError If anything other than a Buffer or Uint8Array is passed in
  13. */
  14. function ensureBuffer(potentialBuffer) {
  15. if (ArrayBuffer.isView(potentialBuffer)) {
  16. return buffer_1.Buffer.from(potentialBuffer.buffer, potentialBuffer.byteOffset, potentialBuffer.byteLength);
  17. }
  18. if (utils_1.isAnyArrayBuffer(potentialBuffer)) {
  19. return buffer_1.Buffer.from(potentialBuffer);
  20. }
  21. throw new TypeError('Must use either Buffer or TypedArray');
  22. }
  23. exports.ensureBuffer = ensureBuffer;
  24. //# sourceMappingURL=ensure_buffer.js.map