Websocket
[VSoRC/.git] / node_modules / websocket / node_modules / typedarray-to-buffer / index.js
1 /**
2  * Convert a typed array to a Buffer without a copy
3  *
4  * Author:   Feross Aboukhadijeh <https://feross.org>
5  * License:  MIT
6  *
7  * `npm install typedarray-to-buffer`
8  */
9
10 var isTypedArray = require('is-typedarray').strict
11
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)
19     }
20     return buf
21   } else {
22     // Pass through all other types to `Buffer.from`
23     return Buffer.from(arr)
24   }
25 }