1 var toString = Object.prototype.toString
4 typeof Buffer.alloc === 'function' &&
5 typeof Buffer.allocUnsafe === 'function' &&
6 typeof Buffer.from === 'function'
9 function isArrayBuffer (input) {
10 return toString.call(input).slice(8, -1) === 'ArrayBuffer'
13 function fromArrayBuffer (obj, byteOffset, length) {
16 var maxLength = obj.byteLength - byteOffset
19 throw new RangeError("'offset' is out of bounds")
22 if (length === undefined) {
27 if (length > maxLength) {
28 throw new RangeError("'length' is out of bounds")
33 ? Buffer.from(obj.slice(byteOffset, byteOffset + length))
34 : new Buffer(new Uint8Array(obj.slice(byteOffset, byteOffset + length)))
37 function fromString (string, encoding) {
38 if (typeof encoding !== 'string' || encoding === '') {
42 if (!Buffer.isEncoding(encoding)) {
43 throw new TypeError('"encoding" must be a valid string encoding')
47 ? Buffer.from(string, encoding)
48 : new Buffer(string, encoding)
51 function bufferFrom (value, encodingOrOffset, length) {
52 if (typeof value === 'number') {
53 throw new TypeError('"value" argument must not be a number')
56 if (isArrayBuffer(value)) {
57 return fromArrayBuffer(value, encodingOrOffset, length)
60 if (typeof value === 'string') {
61 return fromString(value, encodingOrOffset)
69 module.exports = bufferFrom