Skip to content

Commit dc8ea66

Browse files
committed
Remove buffer entirely
closes jhiesey#1
1 parent dd47619 commit dc8ea66

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

index.js

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
var Buffer = require('buffer').Buffer
1+
module.exports = buf => {
2+
if (buf instanceof ArrayBuffer) return buf
23

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')
145

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
269
}
10+
11+
// Otherwise we need to get a proper copy
12+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)
2713
}

0 commit comments

Comments
 (0)