From: James Halliday Date: Wed, 11 Dec 2013 01:04:16 +0000 (-0800) Subject: using typedarray X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=22bd9bc49f6cec66a5d9b05d3b415cf119d4349f;p=buffer.git using typedarray --- diff --git a/index.js b/index.js index 9077ea5..6a14f49 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,11 @@ +var TA = require('typedarray') +var xDataView = typeof DataView === 'undefined' + ? TA.DataView : DataView +var xArrayBuffer = typeof ArrayBuffer === 'undefined' + ? TA.ArrayBuffer : ArrayBuffer +var xUint8Array = typeof Uint8Array === 'undefined' + ? TA.Uint8Array : Uint8Array + exports.Buffer = Buffer exports.SlowBuffer = Buffer exports.INSPECT_MAX_BYTES = 50 @@ -44,7 +52,7 @@ function Buffer (subject, encoding) { else throw new Error('First argument needs to be a number, array or string.') - var buf = augment(new Uint8Array(length)) + var buf = augment(new xUint8Array(length)) if (Buffer.isBuffer(subject)) { // Speed optimization -- use set if we're copying from a Uint8Array buf.set(subject) @@ -843,7 +851,7 @@ function stringtrim (str) { * @return {boolean} */ function _browserSupport () { - var arr = new Uint8Array(0) + var arr = new xUint8Array(0) arr.foo = function () { return 42 } try { diff --git a/package.json b/package.json index 0484a72..41485f8 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "buffer module compatibility for browserify (backed by ArrayBuffer so its fast!)", "main": "index.js", "dependencies": { - "base64-js": "0.0.4" + "base64-js": "0.0.4", + "typedarray": "~0.0.0" }, "devDependencies": { "tape": "~2.1.0", diff --git a/test/basic.js b/test/basic.js index 5845dcd..f7c6a45 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,5 +1,10 @@ var B = require('../index.js').Buffer var test = require('tape') +var TA = require('typedarray') +var xUint16Array = typeof Uint16Array === 'undefined' + ? TA.Uint16Array : Uint16Array +var xUint8Array = typeof Uint8Array === 'undefined' + ? TA.Uint8Array : Uint8Array test('new buffer from array', function (t) { t.plan(1) @@ -20,7 +25,7 @@ test('new buffer from string', function (t) { }) function arraybufferToString (arraybuffer) { - return String.fromCharCode.apply(null, new Uint16Array(arraybuffer)) + return String.fromCharCode.apply(null, new xUint16Array(arraybuffer)) } test('buffer toArrayBuffer()', function (t) { @@ -28,7 +33,7 @@ test('buffer toArrayBuffer()', function (t) { var data = [1, 2, 3, 4, 5, 6, 7, 8] t.equal( arraybufferToString(new B(data).toArrayBuffer()), - arraybufferToString(new Uint8Array(data).buffer) + arraybufferToString(new xUint8Array(data).buffer) ) t.end() })