From: Feross Aboukhadijeh Date: Wed, 16 Sep 2015 05:21:05 +0000 (-0700) Subject: test: set TYPED_ARRAY_SUPPORT via global X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=da664f7c6c714015530220e0ee1fddaaa7ca402e;p=buffer.git test: set TYPED_ARRAY_SUPPORT via global This change is to prepare for proper `instanceof Buffer` support. When testing the Object implementation, we’ll want to exclude the code that changes the Buffer’s __proto__ values and it won’t be enough to just set `Buffer.TYPED_ARRAY_SUPPORT = false` like we were doing before. --- diff --git a/bin/download-node-tests.js b/bin/download-node-tests.js index 53c1c70..c1a8846 100755 --- a/bin/download-node-tests.js +++ b/bin/download-node-tests.js @@ -69,8 +69,8 @@ function testfixer (filename) { if (firstline) { // require buffer explicitly - var preamble = 'var Buffer = require(\'../../\').Buffer;\n' + - 'if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false;' + var preamble = 'if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;\n' + + 'var Buffer = require(\'../../\').Buffer;' if (/use strict/.test(line)) line += '\n' + preamble else line + preamble + '\n' + line firstline = false diff --git a/index.js b/index.js index 06f7562..1ff1731 100644 --- a/index.js +++ b/index.js @@ -43,20 +43,22 @@ var rootParent = {} * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they * get the Object implementation, which is slower but behaves correctly. */ -Buffer.TYPED_ARRAY_SUPPORT = (function () { - function Bar () {} - try { - var arr = new Uint8Array(1) - arr.foo = function () { return 42 } - arr.constructor = Bar - return arr.foo() === 42 && // typed array instances can be augmented - arr.constructor === Bar && // constructor can be set - typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray` - arr.subarray(1, 1).byteLength === 0 // ie10 has broken `subarray` - } catch (e) { - return false - } -})() +Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined + ? global.TYPED_ARRAY_SUPPORT + : (function () { + function Bar () {} + try { + var arr = new Uint8Array(1) + arr.foo = function () { return 42 } + arr.constructor = Bar + return arr.foo() === 42 && // typed array instances can be augmented + arr.constructor === Bar && // constructor can be set + typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray` + arr.subarray(1, 1).byteLength === 0 // ie10 has broken `subarray` + } catch (e) { + return false + } + })() function kMaxLength () { return Buffer.TYPED_ARRAY_SUPPORT diff --git a/test/base64.js b/test/base64.js index 12c4c78..e4ecc56 100644 --- a/test/base64.js +++ b/test/base64.js @@ -1,6 +1,6 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false var B = require('../').Buffer var test = require('tape') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false test('base64: ignore whitespace', function (t) { var text = '\n YW9ldQ== ' diff --git a/test/basic.js b/test/basic.js index af6d5ee..de47970 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,6 +1,6 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false var B = require('../').Buffer var test = require('tape') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false test('buf.constructor is Buffer', function (t) { var buf = new B([1, 2]) diff --git a/test/compare.js b/test/compare.js index 502d35d..62b478c 100644 --- a/test/compare.js +++ b/test/compare.js @@ -1,6 +1,6 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false var B = require('../').Buffer var test = require('tape') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false test('buffer.compare', function (t) { var b = new B(1).fill('a') diff --git a/test/constructor.js b/test/constructor.js index 3adc097..1661c25 100644 --- a/test/constructor.js +++ b/test/constructor.js @@ -1,6 +1,6 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false var B = require('../').Buffer var test = require('tape') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false test('new buffer from array', function (t) { t.equal( diff --git a/test/deprecated.js b/test/deprecated.js index 0385265..991d614 100644 --- a/test/deprecated.js +++ b/test/deprecated.js @@ -1,6 +1,6 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false var B = require('../').Buffer var test = require('tape') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false test('.get (deprecated)', function (t) { var b = new B([7, 42]) diff --git a/test/from-string.js b/test/from-string.js index 25d3e57..349d261 100644 --- a/test/from-string.js +++ b/test/from-string.js @@ -1,6 +1,6 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false var B = require('../').Buffer var test = require('tape') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false test('detect utf16 surrogate pairs', function (t) { var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D' diff --git a/test/methods.js b/test/methods.js index 8741252..f45f72d 100644 --- a/test/methods.js +++ b/test/methods.js @@ -1,6 +1,6 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false var B = require('../').Buffer var test = require('tape') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false test('buffer.toJSON', function (t) { var data = [1, 2, 3, 4] diff --git a/test/node-es6/test-buffer-arraybuffer.js b/test/node-es6/test-buffer-arraybuffer.js index b5e014e..f0eb57c 100644 --- a/test/node-es6/test-buffer-arraybuffer.js +++ b/test/node-es6/test-buffer-arraybuffer.js @@ -1,6 +1,6 @@ 'use strict'; +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false; var Buffer = require('../../').Buffer; -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); diff --git a/test/node-es6/test-buffer-iterator.js b/test/node-es6/test-buffer-iterator.js index 1c316d8..3aa3fbf 100644 --- a/test/node-es6/test-buffer-iterator.js +++ b/test/node-es6/test-buffer-iterator.js @@ -1,6 +1,6 @@ 'use strict'; +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false; var Buffer = require('../../').Buffer; -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); diff --git a/test/node/test-buffer-ascii.js b/test/node/test-buffer-ascii.js index 40e6ee5..633a677 100644 --- a/test/node/test-buffer-ascii.js +++ b/test/node/test-buffer-ascii.js @@ -1,6 +1,6 @@ 'use strict'; +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false; var Buffer = require('../../').Buffer; -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); diff --git a/test/node/test-buffer-bytelength.js b/test/node/test-buffer-bytelength.js index 7879eff..d63922e 100644 --- a/test/node/test-buffer-bytelength.js +++ b/test/node/test-buffer-bytelength.js @@ -1,6 +1,6 @@ 'use strict'; +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false; var Buffer = require('../../').Buffer; -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); diff --git a/test/node/test-buffer-concat.js b/test/node/test-buffer-concat.js index d9fb48e..f6b60ae 100644 --- a/test/node/test-buffer-concat.js +++ b/test/node/test-buffer-concat.js @@ -1,6 +1,6 @@ 'use strict'; +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false; var Buffer = require('../../').Buffer; -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); diff --git a/test/node/test-buffer-indexof.js b/test/node/test-buffer-indexof.js index 8fae483..283b9c8 100644 --- a/test/node/test-buffer-indexof.js +++ b/test/node/test-buffer-indexof.js @@ -1,6 +1,6 @@ 'use strict'; +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false; var Buffer = require('../../').Buffer; -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); diff --git a/test/node/test-buffer-inspect.js b/test/node/test-buffer-inspect.js index 98ec0d1..719444f 100644 --- a/test/node/test-buffer-inspect.js +++ b/test/node/test-buffer-inspect.js @@ -1,6 +1,6 @@ 'use strict'; +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false; var Buffer = require('../../').Buffer; -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); diff --git a/test/node/test-buffer.js b/test/node/test-buffer.js index b895336..d6468f6 100644 --- a/test/node/test-buffer.js +++ b/test/node/test-buffer.js @@ -1,6 +1,6 @@ 'use strict'; +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false; var Buffer = require('../../').Buffer; -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); diff --git a/test/slice.js b/test/slice.js index ba256ca..d4adb39 100644 --- a/test/slice.js +++ b/test/slice.js @@ -1,6 +1,6 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false var B = require('../').Buffer var test = require('tape') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false test('modifying buffer created by .slice() modifies original memory', function (t) { if (!B.TYPED_ARRAY_SUPPORT) return t.end() diff --git a/test/static.js b/test/static.js index d9d32f4..68faa00 100644 --- a/test/static.js +++ b/test/static.js @@ -1,6 +1,6 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false var B = require('../').Buffer var test = require('tape') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false test('Buffer.isEncoding', function (t) { t.equal(B.isEncoding('HEX'), true) diff --git a/test/to-string.js b/test/to-string.js index 0992298..2950d4d 100644 --- a/test/to-string.js +++ b/test/to-string.js @@ -1,6 +1,6 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false var B = require('../').Buffer var test = require('tape') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false test('utf8 buffer to base64', function (t) { t.equal( diff --git a/test/write.js b/test/write.js index e59c047..38b199e 100644 --- a/test/write.js +++ b/test/write.js @@ -1,7 +1,7 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false var B = require('../').Buffer var test = require('tape') var isnan = require('is-nan') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false test('buffer.write string should get parsed as number', function (t) { var b = new B(64)