1234567891011121314151617181920212223 |
- 'use strict';
- const Buffer = require('buffer').Buffer;
- /**
- * Makes sure that, if a Uint8Array is passed in, it is wrapped in a Buffer.
- *
- * @param {Buffer|Uint8Array} potentialBuffer The potential buffer
- * @returns {Buffer} the input if potentialBuffer is a buffer, or a buffer that
- * wraps a passed in Uint8Array
- * @throws {TypeError} If anything other than a Buffer or Uint8Array is passed in
- */
- module.exports = function ensureBuffer(potentialBuffer) {
- if (potentialBuffer instanceof Buffer) {
- return potentialBuffer;
- }
- if (potentialBuffer instanceof Uint8Array) {
- return Buffer.from(potentialBuffer.buffer);
- }
- throw new TypeError('Must use either Buffer or Uint8Array');
- };
|