to-buffer.js 414 B

12345678910111213
  1. var Buffer = require('safe-buffer').Buffer
  2. module.exports = function (thing, encoding, name) {
  3. if (Buffer.isBuffer(thing)) {
  4. return thing
  5. } else if (typeof thing === 'string') {
  6. return Buffer.from(thing, encoding)
  7. } else if (ArrayBuffer.isView(thing)) {
  8. return Buffer.from(thing.buffer)
  9. } else {
  10. throw new TypeError(name + ' must be a string, a Buffer, a typed array or a DataView')
  11. }
  12. }