typedarray-to-buffer
Advanced tools
Comparing version 3.0.5 to 3.1.0
28
index.js
@@ -9,28 +9,18 @@ /** | ||
*/ | ||
/* eslint-disable no-proto */ | ||
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 | ||
? function (arr) { | ||
arr.__proto__ = Buffer.prototype | ||
return arr | ||
module.exports = function typedarrayToBuffer (arr) { | ||
if (isTypedArray(arr)) { | ||
// To avoid a copy, use the typed array's underlying ArrayBuffer to back new Buffer | ||
var buf = new Buffer(arr.buffer) | ||
if (arr.byteOffset !== 0 || arr.byteLength !== arr.buffer.byteLength) { | ||
// Respect the "view", i.e. byteOffset and byteLength, without doing a copy | ||
buf = buf.slice(arr.byteOffset, arr.byteOffset + arr.byteLength) | ||
} | ||
: 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)) | ||
return buf | ||
} else { | ||
// Unsupported type, just pass it through to the `Buffer` constructor. | ||
// Pass through all other types to the `Buffer` constructor | ||
return new Buffer(arr) | ||
} | ||
} |
{ | ||
"name": "typedarray-to-buffer", | ||
"description": "Convert a typed array to a Buffer without a copy", | ||
"version": "3.0.5", | ||
"version": "3.1.0", | ||
"author": { | ||
@@ -49,6 +49,3 @@ "name": "Feross Aboukhadijeh", | ||
"test-node": "tape test/*.js" | ||
}, | ||
"testling": { | ||
"files": "test/*.js" | ||
} | ||
} |
@@ -7,7 +7,7 @@ # typedarray-to-buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][npm-url] | ||
[travis-image]: https://img.shields.io/travis/feross/typedarray-to-buffer/master.svg?style=flat | ||
[travis-image]: https://img.shields.io/travis/feross/typedarray-to-buffer/master.svg | ||
[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-image]: https://img.shields.io/npm/v/typedarray-to-buffer.svg | ||
[npm-url]: https://npmjs.org/package/typedarray-to-buffer | ||
[downloads-image]: https://img.shields.io/npm/dm/typedarray-to-buffer.svg?style=flat | ||
[downloads-image]: https://img.shields.io/npm/dm/typedarray-to-buffer.svg | ||
[saucelabs-image]: https://saucelabs.com/browser-matrix/typedarray-to-buffer.svg | ||
@@ -19,5 +19,4 @@ [saucelabs-url]: https://saucelabs.com/u/typedarray-to-buffer | ||
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? | ||
Unfortunately, sometimes the browser or someone else's API gives you a typed array like | ||
`Uint8Array` to work with and you need to convert it to a `Buffer`. What do you do? | ||
@@ -36,2 +35,6 @@ Of course: `new Buffer(uint8array)` | ||
If you have an `ArrayBuffer`, you don't need this module, because | ||
`new Buffer(arrayBuffer)` | ||
[is already efficient](https://nodejs.org/api/buffer.html#buffer_new_buffer_arraybuffer). | ||
## install | ||
@@ -38,0 +41,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
82
8613
67