Skip to content

Modernize #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ when using [the Buffer implementation provided by Browserify](https://www.npmjs.
## Usage

``` js
var toArrayBuffer = require('to-arraybuffer')
import toArrayBuffer from 'to-arraybuffer'

var buffer = new Buffer(100)
const buffer = Buffer.alloc(100)
// Fill the buffer with some data

var ab = toArrayBuffer(buffer)
const ab = toArrayBuffer(buffer)
// `ab` now contains the same data as `buffer`
```

## License

MIT
MIT
33 changes: 9 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
var Buffer = require('buffer').Buffer
/** @param {ArrayBufferView} buf */
module.exports = buf => {
if (buf instanceof ArrayBuffer) return buf

module.exports = function (buf) {
// If the buffer is backed by a Uint8Array, a faster version will work
if (buf instanceof Uint8Array) {
// If the buffer isn't a subarray, return the underlying ArrayBuffer
if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
return buf.buffer
} else if (typeof buf.buffer.slice === 'function') {
// Otherwise we need to get a proper copy
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)
}
}
if (!ArrayBuffer.isView(buf)) throw new Error('Argument must be a ArrayBufferView')

if (Buffer.isBuffer(buf)) {
// This is the slow version that will work with any Buffer
// implementation (even in old browsers)
var arrayCopy = new Uint8Array(buf.length)
var len = buf.length
for (var i = 0; i < len; i++) {
arrayCopy[i] = buf[i]
}
return arrayCopy.buffer
} else {
throw new Error('Argument must be a Buffer')
}
// If the buffer isn't a subarray, return the underlying ArrayBuffer
// Otherwise we need to get a proper copy
return buf.byteLength === buf.buffer.byteLength
? buf.buffer
: buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"test-browser": "airtap --loopback airtap.local -- test.js",
"test-browser-local": "airtap --no-instrument --local 8080 -- test.js"
},
"files": [
"index.js"
],
"repository": {
"type": "git",
"url": "git://github.com/jhiesey/to-arraybuffer.git"
Expand All @@ -28,7 +31,7 @@
},
"homepage": "https://github.com/jhiesey/to-arraybuffer#readme",
"devDependencies": {
"airtap": "^0.0.5",
"tape": "^4.9.0"
"airtap": "^3.0.0",
"tape": "^5.2.2"
}
}
84 changes: 42 additions & 42 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
var Buffer = require('buffer').Buffer
var test = require('tape')
const { Buffer } = require('buffer')
const test = require('tape')

var toArrayBuffer = require('.')
const toArrayBuffer = require('./index.js')

function elementsEqual (ab, buffer) {
var view = new Uint8Array(ab)
for (var i = 0; i < view.length; i++) {
if (view[i] !== buffer[i]) {
return false
}
}
return true
const view = new Uint8Array(ab)
for (let i = 0; i < view.length; i++) {
if (view[i] !== buffer[i]) {
return false
}
}
return true
}

test('Basic behavior', function (t) {
var buf = new Buffer(10)
for (var i = 0; i < 10; i++) {
buf[i] = i
}
const buf = Buffer.alloc(10)
for (let i = 0; i < 10; i++) {
buf[i] = i
}

var ab = toArrayBuffer(buf)
const ab = toArrayBuffer(buf)

t.equals(ab.byteLength, 10, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.end()
t.equals(ab.byteLength, 10, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.end()
})

test('Behavior when input is a subarray 1', function (t) {
var origBuf = new Buffer(10)
for (var i = 0; i < 10; i++) {
origBuf[i] = i
}
var buf = origBuf.slice(1)

var ab = toArrayBuffer(buf)

t.equals(ab.byteLength, 9, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.notOk(ab === buf.buffer, 'the underlying ArrayBuffer is not returned when incorrect')
t.end()
const origBuf = Buffer.alloc(10)
for (let i = 0; i < 10; i++) {
origBuf[i] = i
}
const buf = origBuf.slice(1)

const ab = toArrayBuffer(buf)

t.equals(ab.byteLength, 9, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.notOk(ab === buf.buffer, 'the underlying ArrayBuffer is not returned when incorrect')
t.end()
})

test('Behavior when input is a subarray 2', function (t) {
var origBuf = new Buffer(10)
for (var i = 0; i < 10; i++) {
origBuf[i] = i
}
var buf = origBuf.slice(0, 9)

var ab = toArrayBuffer(buf)

t.equals(ab.byteLength, 9, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.notOk(ab === buf.buffer, 'the underlying ArrayBuffer is not returned when incorrect')
t.end()
const origBuf = Buffer.alloc(10)
for (let i = 0; i < 10; i++) {
origBuf[i] = i
}
const buf = origBuf.slice(0, 9)

const ab = toArrayBuffer(buf)

t.equals(ab.byteLength, 9, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.notOk(ab === buf.buffer, 'the underlying ArrayBuffer is not returned when incorrect')
t.end()
})