8889841cPKx?[Hs8qqindex.jsnuW+Avar 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') } } PKx?[? README.mdnuW+A# 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 MITPKx?[v-- .npmignorenuW+A.DS_Store node_modules npm-debug.log .zuulrc PKx?[1[!$$ .travis.ymlnuW+Alanguage: node_js node_js: - "4.1"PKx?[_--test.jsnuW+Avar 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() }) PKx?[C>>LICENSEnuW+AThe 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.PKx?[ x// .zuul.ymlnuW+Aui: 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..latestPKx?[w; package.jsonnuW+A{ "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" } } PKx?[Hs8qqindex.jsnuW+APKx?[? README.mdnuW+APKx?[v-- .npmignorenuW+APKx?[1[!$$ .travis.ymlnuW+APKx?[_--Ttest.jsnuW+APKx?[C>> LICENSEnuW+APKx?[ x// -.zuul.ymlnuW+APKx?[w; package.jsonnuW+APKIl