typedarray-to-buffer
Advanced tools
Comparing version 2.0.0 to 3.0.0
28
index.js
@@ -13,14 +13,20 @@ /** | ||
module.exports = function (arr) { | ||
if (typeof Buffer._augment === 'function' && Buffer.TYPED_ARRAY_SUPPORT) { | ||
// If `Buffer` is the browser `buffer` module, and the browser supports typed arrays, | ||
// then avoid a copy. | ||
if (arr instanceof Uint8Array) { | ||
return Buffer._augment(arr) | ||
} else if (isTypedArray(arr)) { | ||
return Buffer._augment(new Uint8Array(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 = (typeof Buffer._augment === 'function' && 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) | ||
} | ||
// Otherwise, fallback to creating a `Buffer` with a copy. | ||
return new Buffer(arr) | ||
} |
{ | ||
"name": "typedarray-to-buffer", | ||
"description": "Convert a typed array to a Buffer without a copy", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Feross Aboukhadijeh", |
@@ -58,7 +58,13 @@ # typedarray-to-buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][npm-url] | ||
If the browser supports typed arrays, then `toBuffer` will **augment the Uint8Array** 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. | ||
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` | ||
@@ -65,0 +71,0 @@ object, copy the data into it, and return it. There's no simple performance optimization |
var test = require('tape') | ||
var toBuffer = require('../') | ||
test('convert to buffer from uint8array', function (t) { | ||
test('convert to buffer from Uint8Array', function (t) { | ||
if (typeof Uint8Array !== 'undefined') { | ||
@@ -15,3 +15,3 @@ var arr = new Uint8Array([1, 2, 3]) | ||
} else { | ||
t.pass('browser lacks uint8array support, skip test') | ||
t.pass('browser lacks Uint8Array support, skip test') | ||
} | ||
@@ -21,3 +21,3 @@ t.end() | ||
test('convert to buffer from another array type (uint32array)', function (t) { | ||
test('convert to buffer from another arrayview type (Uint32Array)', function (t) { | ||
if (typeof Uint32Array !== 'undefined') { | ||
@@ -27,13 +27,29 @@ var arr = new Uint32Array([1, 2, 3]) | ||
t.deepEqual(arr, new Buffer([1, 2, 3]), 'contents equal') | ||
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.readUInt8(0), 1) | ||
t.equal(arr.readUInt8(1), 2) | ||
t.equal(arr.readUInt8(2), 3) | ||
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.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() | ||
}) |
Sorry, the diff of this file is not supported yet
8777
72
79