aboutsummaryrefslogtreecommitdiff
path: root/node_modules/to-buffer/index.js
blob: 9bd4978826c98374d9086986747cd83ccf3b4885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
module.exports = toBuffer

var makeBuffer = Buffer.from && Buffer.from !== Uint8Array.from ? Buffer.from : bufferFrom

function bufferFrom (buf, enc) {
  return new Buffer(buf, enc)
}

function toBuffer (buf, enc) {
  if (Buffer.isBuffer(buf)) return buf
  if (typeof buf === 'string') return makeBuffer(buf, enc)
  if (Array.isArray(buf)) return makeBuffer(buf)
  throw new Error('Input should be a buffer or a string')
}