pax_global_header00006660000000000000000000000064126453461000014513gustar00rootroot0000000000000052 comment=6502d9850e70ba7935a7df4ad86b358fc216f9f0 to-arraybuffer-1.0.1/000077500000000000000000000000001264534610000144425ustar00rootroot00000000000000to-arraybuffer-1.0.1/.gitignore000066400000000000000000000000551264534610000164320ustar00rootroot00000000000000.DS_Store node_modules npm-debug.log .zuulrc to-arraybuffer-1.0.1/.travis.yml000066400000000000000000000000441264534610000165510ustar00rootroot00000000000000language: node_js node_js: - "4.1"to-arraybuffer-1.0.1/.zuul.yml000066400000000000000000000004571264534610000162500ustar00rootroot00000000000000ui: tape browsers: - name: chrome version: 39..latest - name: firefox version: 34..latest - name: safari version: 5..latest - name: ie version: 10..latest - name: opera version: 11..latest - name: iphone version: 5.1..latest - name: android version: 4.0..latestto-arraybuffer-1.0.1/LICENSE000066400000000000000000000020761264534610000154540ustar00rootroot00000000000000The MIT License Copyright (c) 2016 John Hiesey Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.to-arraybuffer-1.0.1/README.md000066400000000000000000000016541264534610000157270ustar00rootroot00000000000000# to-arraybuffer [![Build Status](https://travis-ci.org/jhiesey/to-arraybuffer.svg?branch=master)](https://travis-ci.org/jhiesey/to-arraybuffer) [![Sauce Test Status](https://saucelabs.com/browser-matrix/to-arraybuffer.svg)](https://saucelabs.com/u/to-arraybuffer) Convert from a Buffer to an ArrayBuffer as fast as possible. Note that in some cases the returned ArrayBuffer is backed by the same memory as the original Buffer (but in other cases it is a copy), so **modifying the ArrayBuffer is not recommended**. This module is designed to work both in node.js and in all browsers with ArrayBuffer support when using [the Buffer implementation provided by Browserify](https://www.npmjs.com/package/buffer). ## Usage ``` js var toArrayBuffer = require('to-arraybuffer') var buffer = new Buffer(100) // Fill the buffer with some data var ab = toArrayBuffer(buffer) // `ab` now contains the same data as `buffer` ``` ## License MITto-arraybuffer-1.0.1/index.js000066400000000000000000000015611264534610000161120ustar00rootroot00000000000000var Buffer = require('buffer').Buffer 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 (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') } } to-arraybuffer-1.0.1/package.json000066400000000000000000000016331264534610000167330ustar00rootroot00000000000000{ "name": "to-arraybuffer", "version": "1.0.1", "description": "Get an ArrayBuffer from a Buffer as fast as possible", "main": "index.js", "scripts": { "test": "npm run test-node && ([ -n \"${TRAVIS_PULL_REQUEST}\" -a \"${TRAVIS_PULL_REQUEST}\" != 'false' ] || npm run test-browser)", "test-node": "tape test.js", "test-browser": "zuul --no-coverage -- test.js", "test-browser-local": "zuul --local 8080 --no-coverage -- test.js" }, "repository": { "type": "git", "url": "git://github.com/jhiesey/to-arraybuffer.git" }, "keywords": [ "buffer", "to", "arraybuffer", "fast", "read", "only" ], "author": "John Hiesey", "license": "MIT", "bugs": { "url": "https://github.com/jhiesey/to-arraybuffer/issues" }, "homepage": "https://github.com/jhiesey/to-arraybuffer#readme", "devDependencies": { "tape": "^4.4.0", "zuul": "^3.9.0" } } to-arraybuffer-1.0.1/test.js000066400000000000000000000024551264534610000157650ustar00rootroot00000000000000var Buffer = require('buffer').Buffer var test = require('tape') var toArrayBuffer = require('.') 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 } test('Basic behavior', function (t) { var buf = new Buffer(10) for (var i = 0; i < 10; i++) { buf[i] = i } var ab = toArrayBuffer(buf) 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() }) 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() })