to-buffer.js 611 B

1234567891011121314151617181920
  1. 'use strict';
  2. var Buffer = require('safe-buffer').Buffer;
  3. var toBuffer = require('to-buffer');
  4. var useUint8Array = typeof Uint8Array !== 'undefined';
  5. var useArrayBuffer = useUint8Array && typeof ArrayBuffer !== 'undefined';
  6. var isView = useArrayBuffer && ArrayBuffer.isView;
  7. module.exports = function (thing, encoding, name) {
  8. if (
  9. typeof thing === 'string'
  10. || Buffer.isBuffer(thing)
  11. || (useUint8Array && thing instanceof Uint8Array)
  12. || (isView && isView(thing))
  13. ) {
  14. return toBuffer(thing, encoding);
  15. }
  16. throw new TypeError(name + ' must be a string, a Buffer, a Uint8Array, or a DataView');
  17. };