|
1 |
| -var Buffer = require('buffer').Buffer |
| 1 | +module.exports = buf => { |
| 2 | + if (buf instanceof ArrayBuffer) return buf |
2 | 3 |
|
3 |
| -module.exports = function (buf) { |
4 |
| - // If the buffer is backed by a Uint8Array, a faster version will work |
5 |
| - if (buf instanceof Uint8Array) { |
6 |
| - // If the buffer isn't a subarray, return the underlying ArrayBuffer |
7 |
| - if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) { |
8 |
| - return buf.buffer |
9 |
| - } else if (typeof buf.buffer.slice === 'function') { |
10 |
| - // Otherwise we need to get a proper copy |
11 |
| - return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength) |
12 |
| - } |
13 |
| - } |
| 4 | + if (!ArrayBuffer.isView(buf)) throw new Error('Argument must be a ArrayBufferView') |
14 | 5 |
|
15 |
| - if (Buffer.isBuffer(buf)) { |
16 |
| - // This is the slow version that will work with any Buffer |
17 |
| - // implementation (even in old browsers) |
18 |
| - var arrayCopy = new Uint8Array(buf.length) |
19 |
| - var len = buf.length |
20 |
| - for (var i = 0; i < len; i++) { |
21 |
| - arrayCopy[i] = buf[i] |
22 |
| - } |
23 |
| - return arrayCopy.buffer |
24 |
| - } else { |
25 |
| - throw new Error('Argument must be a Buffer') |
| 6 | + // If the buffer isn't a subarray, return the underlying ArrayBuffer |
| 7 | + if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) { |
| 8 | + return buf.buffer |
26 | 9 | }
|
| 10 | + |
| 11 | + // Otherwise we need to get a proper copy |
| 12 | + return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength) |
27 | 13 | }
|
0 commit comments