1 /* eslint-disable node/no-deprecated-api */
3 var toString = Object.prototype.toString
6 typeof Buffer !== 'undefined' &&
7 typeof Buffer.alloc === 'function' &&
8 typeof Buffer.allocUnsafe === 'function' &&
9 typeof Buffer.from === 'function'
12 function isArrayBuffer (input) {
13 return toString.call(input).slice(8, -1) === 'ArrayBuffer'
16 function fromArrayBuffer (obj, byteOffset, length) {
19 var maxLength = obj.byteLength - byteOffset
22 throw new RangeError("'offset' is out of bounds")
25 if (length === undefined) {
30 if (length > maxLength) {
31 throw new RangeError("'length' is out of bounds")
36 ? Buffer.from(obj.slice(byteOffset, byteOffset + length))
37 : new Buffer(new Uint8Array(obj.slice(byteOffset, byteOffset + length)))
40 function fromString (string, encoding) {
41 if (typeof encoding !== 'string' || encoding === '') {
45 if (!Buffer.isEncoding(encoding)) {
46 throw new TypeError('"encoding" must be a valid string encoding')
50 ? Buffer.from(string, encoding)
51 : new Buffer(string, encoding)
54 function bufferFrom (value, encodingOrOffset, length) {
55 if (typeof value === 'number') {
56 throw new TypeError('"value" argument must not be a number')
59 if (isArrayBuffer(value)) {
60 return fromArrayBuffer(value, encodingOrOffset, length)
63 if (typeof value === 'string') {
64 return fromString(value, encodingOrOffset)
72 module.exports = bufferFrom