ensure_buffer.js 1.0 KB

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