pax_global_header00006660000000000000000000000064125314665700014523gustar00rootroot0000000000000052 comment=cdf662c4de7d59a90754a2797803486203e47451 typedarray-to-buffer-3.0.3/000077500000000000000000000000001253146657000156015ustar00rootroot00000000000000typedarray-to-buffer-3.0.3/.travis.yml000066400000000000000000000006451253146657000177170ustar00rootroot00000000000000language: node_js node_js: - '0.10' env: global: - secure: i51rE9rZGHbcZWlL58j3H1qtL23OIV2r0X4TcQKNI3pw2mubdHFJmfPNNO19ItfReu8wwQMxOehKamwaNvqMiKWyHfn/QcThFQysqzgGZ6AgnUbYx9od6XFNDeWd1sVBf7QBAL07y7KWlYGWCwFwWjabSVySzQhEBdisPcskfkI= - secure: BKq6/5z9LK3KDkTjs7BGeBZ1KsWgz+MsAXZ4P64NSeVGFaBdXU45+ww1mwxXFt5l22/mhyOQZfebQl+kGVqRSZ+DEgQeCymkNZ6CD8c6w6cLuOJXiXwuu/cDM2DD0tfGeu2YZC7yEikP7BqEFwH3D324rRzSGLF2RSAAwkOI7bE= typedarray-to-buffer-3.0.3/.zuul.yml000066400000000000000000000004661253146657000174070ustar00rootroot00000000000000ui: tape browsers: - name: chrome version: latest - name: firefox version: latest - name: safari version: latest - name: ie version: latest - name: opera version: latest - name: iphone version: latest - name: ipad version: latest - name: android version: latest typedarray-to-buffer-3.0.3/LICENSE000066400000000000000000000020711253146657000166060ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Feross Aboukhadijeh 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. typedarray-to-buffer-3.0.3/README.md000066400000000000000000000057371253146657000170740ustar00rootroot00000000000000# typedarray-to-buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][npm-url] #### Convert a typed array to a [Buffer](https://github.com/feross/buffer) without a copy. [![saucelabs][saucelabs-image]][saucelabs-url] [travis-image]: https://img.shields.io/travis/feross/typedarray-to-buffer.svg?style=flat [travis-url]: https://travis-ci.org/feross/typedarray-to-buffer [npm-image]: https://img.shields.io/npm/v/typedarray-to-buffer.svg?style=flat [npm-url]: https://npmjs.org/package/typedarray-to-buffer [downloads-image]: https://img.shields.io/npm/dm/typedarray-to-buffer.svg?style=flat [saucelabs-image]: https://saucelabs.com/browser-matrix/typedarray-to-buffer.svg [saucelabs-url]: https://saucelabs.com/u/typedarray-to-buffer Say you're using the ['buffer'](https://github.com/feross/buffer) module on npm, or [browserify](http://browserify.org/) and you're working with lots of binary data. Unfortunately, sometimes the browser or someone else's API gives you an `ArrayBuffer` or a typed array like `Uint8Array` to work with and you need to convert it to a `Buffer`. What do you do? Of course: `new Buffer(uint8array)` But, alas, every time you do `new Buffer(uint8array)` **the entire array gets copied**. The `Buffer` constructor does a copy; this is defined by the [node docs](http://nodejs.org/api/buffer.html) and the 'buffer' module matches the node API exactly. So, how can we avoid this expensive copy in [performance critical applications](https://github.com/feross/buffer/issues/22)? ***Simply use this module, of course!*** ## install ```bash npm install typedarray-to-buffer ``` ## usage To convert a typed array to a `Buffer` **without a copy**, do this: ```js var toBuffer = require('typedarray-to-buffer') var arr = new Uint8Array([1, 2, 3]) arr = toBuffer(arr) // arr is a buffer now! arr.toString() // '\u0001\u0002\u0003' arr.readUInt16BE(0) // 258 ``` ## how it works If the browser supports typed arrays, then `toBuffer` will **augment the typed array** you pass in with the `Buffer` methods and return it. See [how does Buffer work?](https://github.com/feross/buffer#how-does-it-work) for more about how augmentation works. This module uses the typed array's underlying `ArrayBuffer` to back the new `Buffer`. This respects the "view" on the `ArrayBuffer`, i.e. `byteOffset` and `byteLength`. In other words, if you do `toBuffer(new Uint32Array([1, 2, 3]))`, then the new `Buffer` will contain `[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]`, **not** `[1, 2, 3]`. And it still doesn't require a copy. If the browser doesn't support typed arrays, then `toBuffer` will create a new `Buffer` object, copy the data into it, and return it. There's no simple performance optimization we can do for old browsers. Oh well. If this module is used in node, then it will just call `new Buffer`. This is just for the convenience of modules that work in both node and the browser. ## license MIT. Copyright (C) [Feross Aboukhadijeh](http://feross.org). typedarray-to-buffer-3.0.3/index.js000066400000000000000000000021201253146657000172410ustar00rootroot00000000000000/** * Convert a typed array to a Buffer without a copy * * Author: Feross Aboukhadijeh * License: MIT * * `npm install typedarray-to-buffer` */ var isTypedArray = require('is-typedarray').strict module.exports = function (arr) { // If `Buffer` is the browser `buffer` module, and the browser supports typed arrays, // then avoid a copy. Otherwise, create a `Buffer` with a copy. var constructor = Buffer.TYPED_ARRAY_SUPPORT ? Buffer._augment : function (arr) { return new Buffer(arr) } if (arr instanceof Uint8Array) { return constructor(arr) } else if (arr instanceof ArrayBuffer) { return constructor(new Uint8Array(arr)) } else if (isTypedArray(arr)) { // Use the typed array's underlying ArrayBuffer to back new Buffer. This respects // the "view" on the ArrayBuffer, i.e. byteOffset and byteLength. No copy. return constructor(new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength)) } else { // Unsupported type, just pass it through to the `Buffer` constructor. return new Buffer(arr) } } typedarray-to-buffer-3.0.3/package.json000066400000000000000000000022531253146657000200710ustar00rootroot00000000000000{ "name": "typedarray-to-buffer", "description": "Convert a typed array to a Buffer without a copy", "version": "3.0.3", "author": { "name": "Feross Aboukhadijeh", "email": "feross@feross.org", "url": "http://feross.org/" }, "bugs": { "url": "https://github.com/feross/typedarray-to-buffer/issues" }, "dependencies": { "is-typedarray": "^1.0.0" }, "devDependencies": { "standard": "^3.8.0", "tape": "^4.0.0", "zuul": "^3.0.0" }, "homepage": "http://feross.org", "keywords": [ "buffer", "typed array", "convert", "no copy", "uint8array", "uint16array", "uint32array", "int16array", "int32array", "float32array", "float64array", "browser", "arraybuffer", "dataview" ], "license": "MIT", "main": "index.js", "repository": { "type": "git", "url": "git://github.com/feross/typedarray-to-buffer.git" }, "scripts": { "test": "standard && npm run test-node && npm run test-browser", "test-browser": "zuul -- test/*.js", "test-browser-local": "zuul --local -- test/*.js", "test-node": "tape test/*.js" }, "testling": { "files": "test/*.js" } } typedarray-to-buffer-3.0.3/test/000077500000000000000000000000001253146657000165605ustar00rootroot00000000000000typedarray-to-buffer-3.0.3/test/basic.js000066400000000000000000000030701253146657000201770ustar00rootroot00000000000000var test = require('tape') var toBuffer = require('../') test('convert to buffer from Uint8Array', function (t) { if (typeof Uint8Array !== 'undefined') { var arr = new Uint8Array([1, 2, 3]) arr = toBuffer(arr) t.deepEqual(arr, new Buffer([1, 2, 3]), 'contents equal') t.ok(Buffer.isBuffer(arr), 'is buffer') t.equal(arr.readUInt8(0), 1) t.equal(arr.readUInt8(1), 2) t.equal(arr.readUInt8(2), 3) } else { t.pass('browser lacks Uint8Array support, skip test') } t.end() }) test('convert to buffer from another arrayview type (Uint32Array)', function (t) { if (typeof Uint32Array !== 'undefined') { var arr = new Uint32Array([1, 2, 3]) arr = toBuffer(arr) t.deepEqual(arr, new Buffer([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]), 'contents equal') t.ok(Buffer.isBuffer(arr), 'is buffer') t.equal(arr.readUInt32LE(0), 1) t.equal(arr.readUInt32LE(4), 2) t.equal(arr.readUInt32LE(8), 3) t.equal(arr instanceof Uint8Array, !!Buffer.TYPED_ARRAY_SUPPORT) } else { t.pass('browser lacks Uint32Array support, skip test') } t.end() }) test('convert to buffer from ArrayBuffer', function (t) { if (typeof Uint32Array !== 'undefined') { var arr = new Uint32Array([1, 2, 3]).subarray(1, 2) arr = toBuffer(arr) t.deepEqual(arr, new Buffer([2, 0, 0, 0]), 'contents equal') t.ok(Buffer.isBuffer(arr), 'is buffer') t.equal(arr.readUInt32LE(0), 2) t.equal(arr instanceof Uint8Array, !!Buffer.TYPED_ARRAY_SUPPORT) } else { t.pass('browser lacks ArrayBuffer support, skip test') } t.end() })