2 * Convert a typed array to a Buffer without a copy
4 * Author: Feross Aboukhadijeh <https://feross.org>
7 * `npm install typedarray-to-buffer`
10 var isTypedArray = require('is-typedarray').strict
12 module.exports = function typedarrayToBuffer (arr) {
13 if (isTypedArray(arr)) {
14 // To avoid a copy, use the typed array's underlying ArrayBuffer to back new Buffer
15 var buf = Buffer.from(arr.buffer)
16 if (arr.byteLength !== arr.buffer.byteLength) {
17 // Respect the "view", i.e. byteOffset and byteLength, without doing a copy
18 buf = buf.slice(arr.byteOffset, arr.byteOffset + arr.byteLength)
22 // Pass through all other types to `Buffer.from`
23 return Buffer.from(arr)