blob: 9bd4978826c98374d9086986747cd83ccf3b4885 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001module.exports = toBuffer
2
3var makeBuffer = Buffer.from && Buffer.from !== Uint8Array.from ? Buffer.from : bufferFrom
4
5function bufferFrom (buf, enc) {
6 return new Buffer(buf, enc)
7}
8
9function toBuffer (buf, enc) {
10 if (Buffer.isBuffer(buf)) return buf
11 if (typeof buf === 'string') return makeBuffer(buf, enc)
12 if (Array.isArray(buf)) return makeBuffer(buf)
13 throw new Error('Input should be a buffer or a string')
14}