mirror of
https://gitee.com/dcloud/uni-preset-vue
synced 2026-05-11 00:00:01 +08:00
add node_modules
This commit is contained in:
+150
@@ -0,0 +1,150 @@
|
||||
if (!Array.prototype.forEach) {
|
||||
|
||||
Array.prototype.forEach = function(callback, thisArg) {
|
||||
|
||||
var T, k;
|
||||
|
||||
if (this == null) {
|
||||
throw new TypeError(' this is null or not defined');
|
||||
}
|
||||
|
||||
// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
|
||||
var O = Object(this);
|
||||
|
||||
// 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
|
||||
// 3. Let len be ToUint32(lenValue).
|
||||
var len = O.length >>> 0;
|
||||
|
||||
// 4. If IsCallable(callback) is false, throw a TypeError exception.
|
||||
// See: http://es5.github.com/#x9.11
|
||||
if (typeof callback !== "function") {
|
||||
throw new TypeError(callback + ' is not a function');
|
||||
}
|
||||
|
||||
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
|
||||
if (arguments.length > 1) {
|
||||
T = thisArg;
|
||||
}
|
||||
|
||||
// 6. Let k be 0
|
||||
k = 0;
|
||||
|
||||
// 7. Repeat, while k < len
|
||||
while (k < len) {
|
||||
|
||||
var kValue;
|
||||
|
||||
// a. Let Pk be ToString(k).
|
||||
// This is implicit for LHS operands of the in operator
|
||||
// b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
|
||||
// This step can be combined with c
|
||||
// c. If kPresent is true, then
|
||||
if (k in O) {
|
||||
|
||||
// i. Let kValue be the result of calling the Get internal method of O with argument Pk.
|
||||
kValue = O[k];
|
||||
|
||||
// ii. Call the Call internal method of callback with T as the this value and
|
||||
// argument list containing kValue, k, and O.
|
||||
callback.call(T, kValue, k, O);
|
||||
}
|
||||
// d. Increase k by 1.
|
||||
k++;
|
||||
}
|
||||
// 8. return undefined
|
||||
};
|
||||
}
|
||||
|
||||
if (!Array.isArray) {
|
||||
Array.isArray = function(arg) {
|
||||
return Object.prototype.toString.call(arg) === '[object Array]';
|
||||
};
|
||||
}
|
||||
|
||||
if (!Array.prototype.map) {
|
||||
|
||||
Array.prototype.map = function(callback, thisArg) {
|
||||
|
||||
var T, A, k;
|
||||
|
||||
if (this == null) {
|
||||
throw new TypeError(' this is null or not defined');
|
||||
}
|
||||
|
||||
// 1. Let O be the result of calling ToObject passing the |this|
|
||||
// value as the argument.
|
||||
var O = Object(this);
|
||||
|
||||
// 2. Let lenValue be the result of calling the Get internal
|
||||
// method of O with the argument "length".
|
||||
// 3. Let len be ToUint32(lenValue).
|
||||
var len = O.length >>> 0;
|
||||
|
||||
// 4. If IsCallable(callback) is false, throw a TypeError exception.
|
||||
// See: http://es5.github.com/#x9.11
|
||||
if (typeof callback !== 'function') {
|
||||
throw new TypeError(callback + ' is not a function');
|
||||
}
|
||||
|
||||
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
|
||||
if (arguments.length > 1) {
|
||||
T = thisArg;
|
||||
}
|
||||
|
||||
// 6. Let A be a new array created as if by the expression new Array(len)
|
||||
// where Array is the standard built-in constructor with that name and
|
||||
// len is the value of len.
|
||||
A = new Array(len);
|
||||
|
||||
// 7. Let k be 0
|
||||
k = 0;
|
||||
|
||||
// 8. Repeat, while k < len
|
||||
while (k < len) {
|
||||
|
||||
var kValue, mappedValue;
|
||||
|
||||
// a. Let Pk be ToString(k).
|
||||
// This is implicit for LHS operands of the in operator
|
||||
// b. Let kPresent be the result of calling the HasProperty internal
|
||||
// method of O with argument Pk.
|
||||
// This step can be combined with c
|
||||
// c. If kPresent is true, then
|
||||
if (k in O) {
|
||||
|
||||
// i. Let kValue be the result of calling the Get internal
|
||||
// method of O with argument Pk.
|
||||
kValue = O[k];
|
||||
|
||||
// ii. Let mappedValue be the result of calling the Call internal
|
||||
// method of callback with T as the this value and argument
|
||||
// list containing kValue, k, and O.
|
||||
mappedValue = callback.call(T, kValue, k, O);
|
||||
|
||||
// iii. Call the DefineOwnProperty internal method of A with arguments
|
||||
// Pk, Property Descriptor
|
||||
// { Value: mappedValue,
|
||||
// Writable: true,
|
||||
// Enumerable: true,
|
||||
// Configurable: true },
|
||||
// and false.
|
||||
|
||||
// In browsers that support Object.defineProperty, use the following:
|
||||
// Object.defineProperty(A, k, {
|
||||
// value: mappedValue,
|
||||
// writable: true,
|
||||
// enumerable: true,
|
||||
// configurable: true
|
||||
// });
|
||||
|
||||
// For best browser support, use the following:
|
||||
A[k] = mappedValue;
|
||||
}
|
||||
// d. Increase k by 1.
|
||||
k++;
|
||||
}
|
||||
|
||||
// 9. return A
|
||||
return A;
|
||||
};
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
||||
var B = require('../').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
test('base64: ignore whitespace', function (t) {
|
||||
var text = '\n YW9ldQ== '
|
||||
var buf = new B(text, 'base64')
|
||||
t.equal(buf.toString(), 'aoeu')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('base64: strings without padding', function (t) {
|
||||
t.equal((new B('YW9ldQ', 'base64').toString()), 'aoeu')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('base64: newline in utf8 -- should not be an issue', function (t) {
|
||||
t.equal(
|
||||
new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK', 'base64').toString('utf8'),
|
||||
'---\ntitle: Three dashes marks the spot\ntags:\n'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('base64: newline in base64 -- should get stripped', function (t) {
|
||||
t.equal(
|
||||
new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\nICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt', 'base64').toString('utf8'),
|
||||
'---\ntitle: Three dashes marks the spot\ntags:\n - yaml\n - front-matter\n - dashes\nexpaned-'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('base64: tab characters in base64 - should get stripped', function (t) {
|
||||
t.equal(
|
||||
new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\t\t\t\tICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt', 'base64').toString('utf8'),
|
||||
'---\ntitle: Three dashes marks the spot\ntags:\n - yaml\n - front-matter\n - dashes\nexpaned-'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('base64: invalid non-alphanumeric characters -- should be stripped', function (t) {
|
||||
t.equal(
|
||||
new B('!"#$%&\'()*,.:;<=>?@[\\]^`{|}~', 'base64').toString('utf8'),
|
||||
''
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
||||
var B = require('../').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
test('buf.constructor is Buffer', function (t) {
|
||||
var buf = new B([1, 2])
|
||||
t.strictEqual(buf.constructor, B)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('instanceof Buffer', function (t) {
|
||||
var buf = new B([1, 2])
|
||||
t.ok(buf instanceof B)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('convert to Uint8Array in modern browsers', function (t) {
|
||||
if (B.TYPED_ARRAY_SUPPORT) {
|
||||
var buf = new B([1, 2])
|
||||
var uint8array = new Uint8Array(buf.buffer)
|
||||
t.ok(uint8array instanceof Uint8Array)
|
||||
t.equal(uint8array[0], 1)
|
||||
t.equal(uint8array[1], 2)
|
||||
} else {
|
||||
t.pass('object impl: skipping test')
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('indexes from a string', function (t) {
|
||||
var buf = new B('abc')
|
||||
t.equal(buf[0], 97)
|
||||
t.equal(buf[1], 98)
|
||||
t.equal(buf[2], 99)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('indexes from an array', function (t) {
|
||||
var buf = new B([ 97, 98, 99 ])
|
||||
t.equal(buf[0], 97)
|
||||
t.equal(buf[1], 98)
|
||||
t.equal(buf[2], 99)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('setting index value should modify buffer contents', function (t) {
|
||||
var buf = new B([ 97, 98, 99 ])
|
||||
t.equal(buf[2], 99)
|
||||
t.equal(buf.toString(), 'abc')
|
||||
|
||||
buf[2] += 10
|
||||
t.equal(buf[2], 109)
|
||||
t.equal(buf.toString(), 'abm')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('storing negative number should cast to unsigned', function (t) {
|
||||
var buf = new B(1)
|
||||
|
||||
if (B.TYPED_ARRAY_SUPPORT) {
|
||||
// This does not work with the object implementation -- nothing we can do!
|
||||
buf[0] = -3
|
||||
t.equal(buf[0], 253)
|
||||
}
|
||||
|
||||
buf = new B(1)
|
||||
buf.writeInt8(-3, 0)
|
||||
t.equal(buf[0], 253)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('test that memory is copied from array-like', function (t) {
|
||||
if (B.TYPED_ARRAY_SUPPORT) {
|
||||
var u = new Uint8Array(4)
|
||||
var b = new B(u)
|
||||
b[0] = 1
|
||||
b[1] = 2
|
||||
b[2] = 3
|
||||
b[3] = 4
|
||||
|
||||
t.equal(u[0], 0)
|
||||
t.equal(u[1], 0)
|
||||
t.equal(u[2], 0)
|
||||
t.equal(u[3], 0)
|
||||
} else {
|
||||
t.pass('object impl: skipping test')
|
||||
}
|
||||
|
||||
t.end()
|
||||
})
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
||||
var B = require('../').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
test('buffer.compare', function (t) {
|
||||
var b = new B(1).fill('a')
|
||||
var c = new B(1).fill('c')
|
||||
var d = new B(2).fill('aa')
|
||||
|
||||
t.equal(b.compare(c), -1)
|
||||
t.equal(c.compare(d), 1)
|
||||
t.equal(d.compare(b), 1)
|
||||
t.equal(b.compare(d), -1)
|
||||
|
||||
// static method
|
||||
t.equal(B.compare(b, c), -1)
|
||||
t.equal(B.compare(c, d), 1)
|
||||
t.equal(B.compare(d, b), 1)
|
||||
t.equal(B.compare(b, d), -1)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('buffer.compare argument validation', function (t) {
|
||||
t.throws(function () {
|
||||
var b = new B(1)
|
||||
B.compare(b, 'abc')
|
||||
})
|
||||
|
||||
t.throws(function () {
|
||||
var b = new B(1)
|
||||
B.compare('abc', b)
|
||||
})
|
||||
|
||||
t.throws(function () {
|
||||
var b = new B(1)
|
||||
b.compare('abc')
|
||||
})
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('buffer.equals', function (t) {
|
||||
var b = new B(5).fill('abcdf')
|
||||
var c = new B(5).fill('abcdf')
|
||||
var d = new B(5).fill('abcde')
|
||||
var e = new B(6).fill('abcdef')
|
||||
|
||||
t.ok(b.equals(c))
|
||||
t.ok(!c.equals(d))
|
||||
t.ok(!d.equals(e))
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('buffer.equals argument validation', function (t) {
|
||||
t.throws(function () {
|
||||
var b = new B(1)
|
||||
b.equals('abc')
|
||||
})
|
||||
t.end()
|
||||
})
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
||||
var B = require('../').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
test('new buffer from array', function (t) {
|
||||
t.equal(
|
||||
new B([1, 2, 3]).toString(),
|
||||
'\u0001\u0002\u0003'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from array w/ negatives', function (t) {
|
||||
t.equal(
|
||||
new B([-1, -2, -3]).toString('hex'),
|
||||
'fffefd'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from array with mixed signed input', function (t) {
|
||||
t.equal(
|
||||
new B([-255, 255, -128, 128, 512, -512, 511, -511]).toString('hex'),
|
||||
'01ff80800000ff01'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from string', function (t) {
|
||||
t.equal(
|
||||
new B('hey', 'utf8').toString(),
|
||||
'hey'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from buffer', function (t) {
|
||||
var b1 = new B('asdf')
|
||||
var b2 = new B(b1)
|
||||
t.equal(b1.toString('hex'), b2.toString('hex'))
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from Uint8Array', function (t) {
|
||||
if (typeof Uint8Array !== 'undefined') {
|
||||
var b1 = new Uint8Array([0, 1, 2, 3])
|
||||
var b2 = new B(b1)
|
||||
t.equal(b1.length, b2.length)
|
||||
t.equal(b1[0], 0)
|
||||
t.equal(b1[1], 1)
|
||||
t.equal(b1[2], 2)
|
||||
t.equal(b1[3], 3)
|
||||
t.equal(b1[4], undefined)
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from Uint16Array', function (t) {
|
||||
if (typeof Uint16Array !== 'undefined') {
|
||||
var b1 = new Uint16Array([0, 1, 2, 3])
|
||||
var b2 = new B(b1)
|
||||
t.equal(b1.length, b2.length)
|
||||
t.equal(b1[0], 0)
|
||||
t.equal(b1[1], 1)
|
||||
t.equal(b1[2], 2)
|
||||
t.equal(b1[3], 3)
|
||||
t.equal(b1[4], undefined)
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from Uint32Array', function (t) {
|
||||
if (typeof Uint32Array !== 'undefined') {
|
||||
var b1 = new Uint32Array([0, 1, 2, 3])
|
||||
var b2 = new B(b1)
|
||||
t.equal(b1.length, b2.length)
|
||||
t.equal(b1[0], 0)
|
||||
t.equal(b1[1], 1)
|
||||
t.equal(b1[2], 2)
|
||||
t.equal(b1[3], 3)
|
||||
t.equal(b1[4], undefined)
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from Int16Array', function (t) {
|
||||
if (typeof Int16Array !== 'undefined') {
|
||||
var b1 = new Int16Array([0, 1, 2, 3])
|
||||
var b2 = new B(b1)
|
||||
t.equal(b1.length, b2.length)
|
||||
t.equal(b1[0], 0)
|
||||
t.equal(b1[1], 1)
|
||||
t.equal(b1[2], 2)
|
||||
t.equal(b1[3], 3)
|
||||
t.equal(b1[4], undefined)
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from Int32Array', function (t) {
|
||||
if (typeof Int32Array !== 'undefined') {
|
||||
var b1 = new Int32Array([0, 1, 2, 3])
|
||||
var b2 = new B(b1)
|
||||
t.equal(b1.length, b2.length)
|
||||
t.equal(b1[0], 0)
|
||||
t.equal(b1[1], 1)
|
||||
t.equal(b1[2], 2)
|
||||
t.equal(b1[3], 3)
|
||||
t.equal(b1[4], undefined)
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from Float32Array', function (t) {
|
||||
if (typeof Float32Array !== 'undefined') {
|
||||
var b1 = new Float32Array([0, 1, 2, 3])
|
||||
var b2 = new B(b1)
|
||||
t.equal(b1.length, b2.length)
|
||||
t.equal(b1[0], 0)
|
||||
t.equal(b1[1], 1)
|
||||
t.equal(b1[2], 2)
|
||||
t.equal(b1[3], 3)
|
||||
t.equal(b1[4], undefined)
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from Float64Array', function (t) {
|
||||
if (typeof Float64Array !== 'undefined') {
|
||||
var b1 = new Float64Array([0, 1, 2, 3])
|
||||
var b2 = new B(b1)
|
||||
t.equal(b1.length, b2.length)
|
||||
t.equal(b1[0], 0)
|
||||
t.equal(b1[1], 1)
|
||||
t.equal(b1[2], 2)
|
||||
t.equal(b1[3], 3)
|
||||
t.equal(b1[4], undefined)
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('new buffer from buffer.toJSON() output', function (t) {
|
||||
if (typeof JSON === 'undefined') {
|
||||
// ie6, ie7 lack support
|
||||
t.end()
|
||||
return
|
||||
}
|
||||
var buf = new B('test')
|
||||
var json = JSON.stringify(buf)
|
||||
var obj = JSON.parse(json)
|
||||
var copy = new B(obj)
|
||||
t.ok(buf.equals(copy))
|
||||
t.end()
|
||||
})
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
||||
var B = require('../').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
test('.get (deprecated)', function (t) {
|
||||
var b = new B([7, 42])
|
||||
t.equal(b.get(0), 7)
|
||||
t.equal(b.get(1), 42)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('.set (deprecated)', function (t) {
|
||||
var b = new B(2)
|
||||
b.set(7, 0)
|
||||
b.set(42, 1)
|
||||
t.equal(b[0], 7)
|
||||
t.equal(b[1], 42)
|
||||
t.end()
|
||||
})
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
||||
var B = require('../').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
test('detect utf16 surrogate pairs', function (t) {
|
||||
var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D'
|
||||
var buf = new B(text)
|
||||
t.equal(text, buf.toString())
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('detect utf16 surrogate pairs over U+20000 until U+10FFFF', function (t) {
|
||||
var text = '\uD842\uDFB7' + '\uD93D\uDCAD' + '\uDBFF\uDFFF'
|
||||
var buf = new B(text)
|
||||
t.equal(text, buf.toString())
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('replace orphaned utf16 surrogate lead code point', function (t) {
|
||||
var text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D'
|
||||
var buf = new B(text)
|
||||
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ]))
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('replace orphaned utf16 surrogate trail code point', function (t) {
|
||||
var text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D'
|
||||
var buf = new B(text)
|
||||
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ]))
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('do not write partial utf16 code units', function (t) {
|
||||
var f = new B([0, 0, 0, 0, 0])
|
||||
t.equal(f.length, 5)
|
||||
var size = f.write('あいうえお', 'utf16le')
|
||||
t.equal(size, 4)
|
||||
t.deepEqual(f, new B([0x42, 0x30, 0x44, 0x30, 0x00]))
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('handle partial utf16 code points when encoding to utf8 the way node does', function (t) {
|
||||
var text = '\uD83D\uDE38' + '\uD83D\uDC4D'
|
||||
|
||||
var buf = new B(8)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xf0, 0x9f, 0x91, 0x8d ]))
|
||||
|
||||
buf = new B(7)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00, 0x00 ]))
|
||||
|
||||
buf = new B(6)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00 ]))
|
||||
|
||||
buf = new B(5)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00 ]))
|
||||
|
||||
buf = new B(4)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8 ]))
|
||||
|
||||
buf = new B(3)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0x00, 0x00, 0x00 ]))
|
||||
|
||||
buf = new B(2)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0x00, 0x00 ]))
|
||||
|
||||
buf = new B(1)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0x00 ]))
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('handle invalid utf16 code points when encoding to utf8 the way node does', function (t) {
|
||||
var text = 'a' + '\uDE38\uD83D' + 'b'
|
||||
|
||||
var buf = new B(8)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x62 ]))
|
||||
|
||||
buf = new B(7)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd ]))
|
||||
|
||||
buf = new B(6)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00, 0x00 ]))
|
||||
|
||||
buf = new B(5)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00 ]))
|
||||
|
||||
buf = new B(4)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd ]))
|
||||
|
||||
buf = new B(3)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0x61, 0x00, 0x00 ]))
|
||||
|
||||
buf = new B(2)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0x61, 0x00 ]))
|
||||
|
||||
buf = new B(1)
|
||||
buf.fill(0)
|
||||
buf.write(text)
|
||||
t.deepEqual(buf, new B([ 0x61 ]))
|
||||
|
||||
t.end()
|
||||
})
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
||||
var B = require('../').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
test('buffer.toJSON', function (t) {
|
||||
var data = [1, 2, 3, 4]
|
||||
t.deepEqual(
|
||||
new B(data).toJSON(),
|
||||
{ type: 'Buffer', data: [ 1, 2, 3, 4 ] }
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('buffer.copy', function (t) {
|
||||
// copied from nodejs.org example
|
||||
var buf1 = new B(26)
|
||||
var buf2 = new B(26)
|
||||
|
||||
for (var i = 0; i < 26; i++) {
|
||||
buf1[i] = i + 97 // 97 is ASCII a
|
||||
buf2[i] = 33 // ASCII !
|
||||
}
|
||||
|
||||
buf1.copy(buf2, 8, 16, 20)
|
||||
|
||||
t.equal(
|
||||
buf2.toString('ascii', 0, 25),
|
||||
'!!!!!!!!qrst!!!!!!!!!!!!!'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('test offset returns are correct', function (t) {
|
||||
var b = new B(16)
|
||||
t.equal(4, b.writeUInt32LE(0, 0))
|
||||
t.equal(6, b.writeUInt16LE(0, 4))
|
||||
t.equal(7, b.writeUInt8(0, 6))
|
||||
t.equal(8, b.writeInt8(0, 7))
|
||||
t.equal(16, b.writeDoubleLE(0, 8))
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('concat() a varying number of buffers', function (t) {
|
||||
var zero = []
|
||||
var one = [ new B('asdf') ]
|
||||
var long = []
|
||||
for (var i = 0; i < 10; i++) {
|
||||
long.push(new B('asdf'))
|
||||
}
|
||||
|
||||
var flatZero = B.concat(zero)
|
||||
var flatOne = B.concat(one)
|
||||
var flatLong = B.concat(long)
|
||||
var flatLongLen = B.concat(long, 40)
|
||||
|
||||
t.equal(flatZero.length, 0)
|
||||
t.equal(flatOne.toString(), 'asdf')
|
||||
t.deepEqual(flatOne, one[0])
|
||||
t.equal(flatLong.toString(), (new Array(10 + 1).join('asdf')))
|
||||
t.equal(flatLongLen.toString(), (new Array(10 + 1).join('asdf')))
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('fill', function (t) {
|
||||
var b = new B(10)
|
||||
b.fill(2)
|
||||
t.equal(b.toString('hex'), '02020202020202020202')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('fill (string)', function (t) {
|
||||
var b = new B(10)
|
||||
b.fill('abc')
|
||||
t.equal(b.toString(), 'abcabcabca')
|
||||
b.fill('է')
|
||||
t.equal(b.toString(), 'էէէէէ')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('copy() empty buffer with sourceEnd=0', function (t) {
|
||||
var source = new B([42])
|
||||
var destination = new B([43])
|
||||
source.copy(destination, 0, 0, 0)
|
||||
t.equal(destination.readUInt8(0), 43)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('copy() after slice()', function (t) {
|
||||
var source = new B(200)
|
||||
var dest = new B(200)
|
||||
var expected = new B(200)
|
||||
for (var i = 0; i < 200; i++) {
|
||||
source[i] = i
|
||||
dest[i] = 0
|
||||
}
|
||||
|
||||
source.slice(2).copy(dest)
|
||||
source.copy(expected, 0, 2)
|
||||
t.deepEqual(dest, expected)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('copy() ascending', function (t) {
|
||||
var b = new B('abcdefghij')
|
||||
b.copy(b, 0, 3, 10)
|
||||
t.equal(b.toString(), 'defghijhij')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('copy() descending', function (t) {
|
||||
var b = new B('abcdefghij')
|
||||
b.copy(b, 3, 0, 7)
|
||||
t.equal(b.toString(), 'abcabcdefg')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('buffer.slice sets indexes', function (t) {
|
||||
t.equal((new B('hallo')).slice(0, 5).toString(), 'hallo')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('buffer.slice out of range', function (t) {
|
||||
t.plan(2)
|
||||
t.equal((new B('hallo')).slice(0, 10).toString(), 'hallo')
|
||||
t.equal((new B('hallo')).slice(10, 2).toString(), '')
|
||||
t.end()
|
||||
})
|
||||
+1
@@ -0,0 +1 @@
|
||||
node buffer tests that require ES6 (e.g. "for..of" construct)
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
'use strict';
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
|
||||
var Buffer = require('../../').Buffer;
|
||||
|
||||
var common = {};
|
||||
var assert = require('assert');
|
||||
|
||||
var Buffer = require('../../').Buffer;
|
||||
var LENGTH = 16;
|
||||
|
||||
var ab = new ArrayBuffer(LENGTH);
|
||||
var dv = new DataView(ab);
|
||||
var ui = new Uint8Array(ab);
|
||||
var buf = new Buffer(ab);
|
||||
|
||||
|
||||
assert.ok(Buffer.isBuffer(buf));
|
||||
// For backwards compatibility of old .parent property test that if buf is not
|
||||
// a slice then .parent should be undefined.
|
||||
assert.equal(buf.parent, undefined);
|
||||
assert.equal(buf.buffer, ab);
|
||||
assert.equal(buf.length, ab.byteLength);
|
||||
|
||||
|
||||
buf.fill(0xC);
|
||||
for (var i = 0; i < LENGTH; i++) {
|
||||
assert.equal(ui[i], 0xC);
|
||||
ui[i] = 0xF;
|
||||
assert.equal(buf[i], 0xF);
|
||||
}
|
||||
|
||||
buf.writeUInt32LE(0xF00, 0);
|
||||
buf.writeUInt32BE(0xB47, 4);
|
||||
buf.writeDoubleLE(3.1415, 8);
|
||||
|
||||
assert.equal(dv.getUint32(0, true), 0xF00);
|
||||
assert.equal(dv.getUint32(4), 0xB47);
|
||||
assert.equal(dv.getFloat64(8, true), 3.1415);
|
||||
|
||||
|
||||
// Now test protecting users from doing stupid things
|
||||
|
||||
assert.throws(function() {
|
||||
function AB() { }
|
||||
AB.__proto__ = ArrayBuffer;
|
||||
AB.prototype.__proto__ = ArrayBuffer.prototype;
|
||||
new Buffer(new AB());
|
||||
}, TypeError);
|
||||
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
'use strict';
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
|
||||
var Buffer = require('../../').Buffer;
|
||||
var common = {};
|
||||
var assert = require('assert');
|
||||
|
||||
var buffer = new Buffer([1, 2, 3, 4, 5]);
|
||||
var arr;
|
||||
var b;
|
||||
|
||||
// buffers should be iterable
|
||||
|
||||
arr = [];
|
||||
|
||||
for (b of buffer)
|
||||
arr.push(b);
|
||||
|
||||
assert.deepEqual(arr, [1, 2, 3, 4, 5]);
|
||||
|
||||
|
||||
// buffer iterators should be iterable
|
||||
|
||||
arr = [];
|
||||
|
||||
for (b of buffer[Symbol.iterator]())
|
||||
arr.push(b);
|
||||
|
||||
assert.deepEqual(arr, [1, 2, 3, 4, 5]);
|
||||
|
||||
|
||||
// buffer#values() should return iterator for values
|
||||
|
||||
arr = [];
|
||||
|
||||
for (b of buffer.values())
|
||||
arr.push(b);
|
||||
|
||||
assert.deepEqual(arr, [1, 2, 3, 4, 5]);
|
||||
|
||||
|
||||
// buffer#keys() should return iterator for keys
|
||||
|
||||
arr = [];
|
||||
|
||||
for (b of buffer.keys())
|
||||
arr.push(b);
|
||||
|
||||
assert.deepEqual(arr, [0, 1, 2, 3, 4]);
|
||||
|
||||
|
||||
// buffer#entries() should return iterator for entries
|
||||
|
||||
arr = [];
|
||||
|
||||
for (var b of buffer.entries())
|
||||
arr.push(b);
|
||||
|
||||
assert.deepEqual(arr, [
|
||||
[0, 1],
|
||||
[1, 2],
|
||||
[2, 3],
|
||||
[3, 4],
|
||||
[4, 5]
|
||||
]);
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
node core buffer tests
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
|
||||
var Buffer = require('../../').Buffer;
|
||||
var common = {};
|
||||
var assert = require('assert');
|
||||
|
||||
// ASCII conversion in node.js simply masks off the high bits,
|
||||
// it doesn't do transliteration.
|
||||
assert.equal(Buffer('hérité').toString('ascii'), 'hC)ritC)');
|
||||
|
||||
// 71 characters, 78 bytes. The ’ character is a triple-byte sequence.
|
||||
var input = 'C’est, graphiquement, la réunion d’un accent aigu ' +
|
||||
'et d’un accent grave.';
|
||||
|
||||
var expected = 'Cb\u0000\u0019est, graphiquement, la rC)union ' +
|
||||
'db\u0000\u0019un accent aigu et db\u0000\u0019un ' +
|
||||
'accent grave.';
|
||||
|
||||
var buf = Buffer(input);
|
||||
|
||||
for (var i = 0; i < expected.length; ++i) {
|
||||
assert.equal(buf.slice(i).toString('ascii'), expected.slice(i));
|
||||
|
||||
// Skip remainder of multi-byte sequence.
|
||||
if (input.charCodeAt(i) > 65535) ++i;
|
||||
if (input.charCodeAt(i) > 127) ++i;
|
||||
}
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
'use strict';
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
|
||||
var Buffer = require('../../').Buffer;
|
||||
|
||||
var common = {};
|
||||
var assert = require('assert');
|
||||
var Buffer = require('../../').Buffer;
|
||||
|
||||
// coerce values to string
|
||||
assert.equal(Buffer.byteLength(32, 'raw'), 2);
|
||||
assert.equal(Buffer.byteLength(NaN, 'utf8'), 3);
|
||||
assert.equal(Buffer.byteLength({}, 'raws'), 15);
|
||||
assert.equal(Buffer.byteLength(), 9);
|
||||
|
||||
// special case: zero length string
|
||||
assert.equal(Buffer.byteLength('', 'ascii'), 0);
|
||||
assert.equal(Buffer.byteLength('', 'HeX'), 0);
|
||||
|
||||
// utf8
|
||||
assert.equal(Buffer.byteLength('∑éllö wørl∂!', 'utf-8'), 19);
|
||||
assert.equal(Buffer.byteLength('κλμνξο', 'utf8'), 12);
|
||||
assert.equal(Buffer.byteLength('挵挶挷挸挹', 'utf-8'), 15);
|
||||
assert.equal(Buffer.byteLength('𠝹𠱓𠱸', 'UTF8'), 12);
|
||||
// without an encoding, utf8 should be assumed
|
||||
assert.equal(Buffer.byteLength('hey there'), 9);
|
||||
assert.equal(Buffer.byteLength('𠱸挶νξ#xx :)'), 17);
|
||||
assert.equal(Buffer.byteLength('hello world', ''), 11);
|
||||
// it should also be assumed with unrecognized encoding
|
||||
assert.equal(Buffer.byteLength('hello world', 'abc'), 11);
|
||||
assert.equal(Buffer.byteLength('ßœ∑≈', 'unkn0wn enc0ding'), 10);
|
||||
|
||||
// base64
|
||||
assert.equal(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'base64'), 11);
|
||||
assert.equal(Buffer.byteLength('bm9kZS5qcyByb2NrcyE=', 'base64'), 14);
|
||||
assert.equal(Buffer.byteLength('aGkk', 'base64'), 3);
|
||||
assert.equal(Buffer.byteLength('bHNrZGZsa3NqZmtsc2xrZmFqc2RsZmtqcw==',
|
||||
'base64'), 25);
|
||||
// special padding
|
||||
assert.equal(Buffer.byteLength('aaa=', 'base64'), 2);
|
||||
assert.equal(Buffer.byteLength('aaaa==', 'base64'), 3);
|
||||
|
||||
assert.equal(Buffer.byteLength('Il était tué'), 14);
|
||||
assert.equal(Buffer.byteLength('Il était tué', 'utf8'), 14);
|
||||
assert.equal(Buffer.byteLength('Il était tué', 'ascii'), 12);
|
||||
assert.equal(Buffer.byteLength('Il était tué', 'binary'), 12);
|
||||
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
|
||||
assert.equal(24, Buffer.byteLength('Il était tué', encoding));
|
||||
});
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
|
||||
var Buffer = require('../../').Buffer;
|
||||
var common = {};
|
||||
var assert = require('assert');
|
||||
|
||||
var zero = [];
|
||||
var one = [ new Buffer('asdf') ];
|
||||
var long = [];
|
||||
for (var i = 0; i < 10; i++) long.push(new Buffer('asdf'));
|
||||
|
||||
var flatZero = Buffer.concat(zero);
|
||||
var flatOne = Buffer.concat(one);
|
||||
var flatLong = Buffer.concat(long);
|
||||
var flatLongLen = Buffer.concat(long, 40);
|
||||
|
||||
assert(flatZero.length === 0);
|
||||
assert(flatOne.toString() === 'asdf');
|
||||
// A special case where concat used to return the first item,
|
||||
// if the length is one. This check is to make sure that we don't do that.
|
||||
assert(flatOne !== one[0]);
|
||||
assert(flatLong.toString() === (new Array(10 + 1).join('asdf')));
|
||||
assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf')));
|
||||
|
||||
assert.throws(function() {
|
||||
Buffer.concat([42]);
|
||||
}, TypeError);
|
||||
|
||||
// console.log('ok');
|
||||
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
'use strict';
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
|
||||
var Buffer = require('../../').Buffer;
|
||||
var common = {};
|
||||
var assert = require('assert');
|
||||
|
||||
var Buffer = require('../../').Buffer;
|
||||
|
||||
var b = new Buffer('abcdef');
|
||||
var buf_a = new Buffer('a');
|
||||
var buf_bc = new Buffer('bc');
|
||||
var buf_f = new Buffer('f');
|
||||
var buf_z = new Buffer('z');
|
||||
var buf_empty = new Buffer('');
|
||||
|
||||
assert.equal(b.indexOf('a'), 0);
|
||||
assert.equal(b.indexOf('a', 1), -1);
|
||||
assert.equal(b.indexOf('a', -1), -1);
|
||||
assert.equal(b.indexOf('a', -4), -1);
|
||||
assert.equal(b.indexOf('a', -b.length), 0);
|
||||
assert.equal(b.indexOf('a', NaN), 0);
|
||||
assert.equal(b.indexOf('a', -Infinity), 0);
|
||||
assert.equal(b.indexOf('a', Infinity), -1);
|
||||
assert.equal(b.indexOf('bc'), 1);
|
||||
assert.equal(b.indexOf('bc', 2), -1);
|
||||
assert.equal(b.indexOf('bc', -1), -1);
|
||||
assert.equal(b.indexOf('bc', -3), -1);
|
||||
assert.equal(b.indexOf('bc', -5), 1);
|
||||
assert.equal(b.indexOf('bc', NaN), 1);
|
||||
assert.equal(b.indexOf('bc', -Infinity), 1);
|
||||
assert.equal(b.indexOf('bc', Infinity), -1);
|
||||
assert.equal(b.indexOf('f'), b.length - 1);
|
||||
assert.equal(b.indexOf('z'), -1);
|
||||
assert.equal(b.indexOf(''), -1);
|
||||
assert.equal(b.indexOf('', 1), -1);
|
||||
assert.equal(b.indexOf('', b.length + 1), -1);
|
||||
assert.equal(b.indexOf('', Infinity), -1);
|
||||
assert.equal(b.indexOf(buf_a), 0);
|
||||
assert.equal(b.indexOf(buf_a, 1), -1);
|
||||
assert.equal(b.indexOf(buf_a, -1), -1);
|
||||
assert.equal(b.indexOf(buf_a, -4), -1);
|
||||
assert.equal(b.indexOf(buf_a, -b.length), 0);
|
||||
assert.equal(b.indexOf(buf_a, NaN), 0);
|
||||
assert.equal(b.indexOf(buf_a, -Infinity), 0);
|
||||
assert.equal(b.indexOf(buf_a, Infinity), -1);
|
||||
assert.equal(b.indexOf(buf_bc), 1);
|
||||
assert.equal(b.indexOf(buf_bc, 2), -1);
|
||||
assert.equal(b.indexOf(buf_bc, -1), -1);
|
||||
assert.equal(b.indexOf(buf_bc, -3), -1);
|
||||
assert.equal(b.indexOf(buf_bc, -5), 1);
|
||||
assert.equal(b.indexOf(buf_bc, NaN), 1);
|
||||
assert.equal(b.indexOf(buf_bc, -Infinity), 1);
|
||||
assert.equal(b.indexOf(buf_bc, Infinity), -1);
|
||||
assert.equal(b.indexOf(buf_f), b.length - 1);
|
||||
assert.equal(b.indexOf(buf_z), -1);
|
||||
assert.equal(b.indexOf(buf_empty), -1);
|
||||
assert.equal(b.indexOf(buf_empty, 1), -1);
|
||||
assert.equal(b.indexOf(buf_empty, b.length + 1), -1);
|
||||
assert.equal(b.indexOf(buf_empty, Infinity), -1);
|
||||
assert.equal(b.indexOf(0x61), 0);
|
||||
assert.equal(b.indexOf(0x61, 1), -1);
|
||||
assert.equal(b.indexOf(0x61, -1), -1);
|
||||
assert.equal(b.indexOf(0x61, -4), -1);
|
||||
assert.equal(b.indexOf(0x61, -b.length), 0);
|
||||
assert.equal(b.indexOf(0x61, NaN), 0);
|
||||
assert.equal(b.indexOf(0x61, -Infinity), 0);
|
||||
assert.equal(b.indexOf(0x61, Infinity), -1);
|
||||
assert.equal(b.indexOf(0x0), -1);
|
||||
|
||||
assert.throws(function() {
|
||||
b.indexOf(function() { });
|
||||
});
|
||||
assert.throws(function() {
|
||||
b.indexOf({});
|
||||
});
|
||||
assert.throws(function() {
|
||||
b.indexOf([]);
|
||||
});
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
|
||||
var Buffer = require('../../').Buffer;
|
||||
var common = {};
|
||||
var assert = require('assert');
|
||||
|
||||
var util = require('util');
|
||||
|
||||
var buffer = require('../../');
|
||||
|
||||
buffer.INSPECT_MAX_BYTES = 2;
|
||||
|
||||
var b = new Buffer(4);
|
||||
b.fill('1234');
|
||||
|
||||
var s = new buffer.SlowBuffer(4);
|
||||
s.fill('1234');
|
||||
|
||||
var expected = '<Buffer 31 32 ... >';
|
||||
|
||||
assert.strictEqual(util.inspect(b), expected);
|
||||
assert.strictEqual(util.inspect(s), expected);
|
||||
|
||||
b = new Buffer(2);
|
||||
b.fill('12');
|
||||
|
||||
s = new buffer.SlowBuffer(2);
|
||||
s.fill('12');
|
||||
|
||||
expected = '<Buffer 31 32>';
|
||||
|
||||
assert.strictEqual(util.inspect(b), expected);
|
||||
assert.strictEqual(util.inspect(s), expected);
|
||||
|
||||
buffer.INSPECT_MAX_BYTES = Infinity;
|
||||
|
||||
assert.doesNotThrow(function() {
|
||||
assert.strictEqual(util.inspect(b), expected);
|
||||
assert.strictEqual(util.inspect(s), expected);
|
||||
});
|
||||
|
||||
+1194
File diff suppressed because it is too large
Load Diff
+37
@@ -0,0 +1,37 @@
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
||||
var B = require('../').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
test('modifying buffer created by .slice() modifies original memory', function (t) {
|
||||
if (!B.TYPED_ARRAY_SUPPORT) return t.end()
|
||||
|
||||
var buf1 = new B(26)
|
||||
for (var i = 0; i < 26; i++) {
|
||||
buf1[i] = i + 97 // 97 is ASCII a
|
||||
}
|
||||
|
||||
var buf2 = buf1.slice(0, 3)
|
||||
t.equal(buf2.toString('ascii', 0, buf2.length), 'abc')
|
||||
|
||||
buf2[0] = '!'.charCodeAt(0)
|
||||
t.equal(buf1.toString('ascii', 0, buf2.length), '!bc')
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('modifying parent buffer modifies .slice() buffer\'s memory', function (t) {
|
||||
if (!B.TYPED_ARRAY_SUPPORT) return t.end()
|
||||
|
||||
var buf1 = new B(26)
|
||||
for (var i = 0; i < 26; i++) {
|
||||
buf1[i] = i + 97 // 97 is ASCII a
|
||||
}
|
||||
|
||||
var buf2 = buf1.slice(0, 3)
|
||||
t.equal(buf2.toString('ascii', 0, buf2.length), 'abc')
|
||||
|
||||
buf1[0] = '!'.charCodeAt(0)
|
||||
t.equal(buf2.toString('ascii', 0, buf2.length), '!bc')
|
||||
|
||||
t.end()
|
||||
})
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
||||
var B = require('../').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
test('Buffer.isEncoding', function (t) {
|
||||
t.equal(B.isEncoding('HEX'), true)
|
||||
t.equal(B.isEncoding('hex'), true)
|
||||
t.equal(B.isEncoding('bad'), false)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Buffer.isBuffer', function (t) {
|
||||
t.equal(B.isBuffer(new B('hey', 'utf8')), true)
|
||||
t.equal(B.isBuffer(new B([1, 2, 3], 'utf8')), true)
|
||||
t.equal(B.isBuffer('hey'), false)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Buffer.toArrayBuffer', function (t) {
|
||||
var data = [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
if (typeof Uint8Array !== 'undefined') {
|
||||
var result = new B(data).toArrayBuffer()
|
||||
var expected = new Uint8Array(data).buffer
|
||||
for (var i = 0; i < expected.byteLength; i++) {
|
||||
t.equal(result[i], expected[i])
|
||||
}
|
||||
} else {
|
||||
t.pass('No toArrayBuffer() method provided in old browsers')
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
||||
var B = require('../').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
test('utf8 buffer to base64', function (t) {
|
||||
t.equal(
|
||||
new B('Ձאab', 'utf8').toString('base64'),
|
||||
'1YHXkGFi'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf8 buffer to hex', function (t) {
|
||||
t.equal(
|
||||
new B('Ձאab', 'utf8').toString('hex'),
|
||||
'd581d7906162'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf8 to utf8', function (t) {
|
||||
t.equal(
|
||||
new B('öäüõÖÄÜÕ', 'utf8').toString('utf8'),
|
||||
'öäüõÖÄÜÕ'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf16le to utf16', function (t) {
|
||||
t.equal(
|
||||
new B(new B('abcd', 'utf8').toString('utf16le'), 'utf16le').toString('utf8'),
|
||||
'abcd'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf16le to hex', function (t) {
|
||||
t.equal(
|
||||
new B('abcd', 'utf16le').toString('hex'),
|
||||
'6100620063006400'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('ascii buffer to base64', function (t) {
|
||||
t.equal(
|
||||
new B('123456!@#$%^', 'ascii').toString('base64'),
|
||||
'MTIzNDU2IUAjJCVe'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('ascii buffer to hex', function (t) {
|
||||
t.equal(
|
||||
new B('123456!@#$%^', 'ascii').toString('hex'),
|
||||
'31323334353621402324255e'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('base64 buffer to utf8', function (t) {
|
||||
t.equal(
|
||||
new B('1YHXkGFi', 'base64').toString('utf8'),
|
||||
'Ձאab'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('hex buffer to utf8', function (t) {
|
||||
t.equal(
|
||||
new B('d581d7906162', 'hex').toString('utf8'),
|
||||
'Ձאab'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('base64 buffer to ascii', function (t) {
|
||||
t.equal(
|
||||
new B('MTIzNDU2IUAjJCVe', 'base64').toString('ascii'),
|
||||
'123456!@#$%^'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('hex buffer to ascii', function (t) {
|
||||
t.equal(
|
||||
new B('31323334353621402324255e', 'hex').toString('ascii'),
|
||||
'123456!@#$%^'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('base64 buffer to binary', function (t) {
|
||||
t.equal(
|
||||
new B('MTIzNDU2IUAjJCVe', 'base64').toString('binary'),
|
||||
'123456!@#$%^'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('hex buffer to binary', function (t) {
|
||||
t.equal(
|
||||
new B('31323334353621402324255e', 'hex').toString('binary'),
|
||||
'123456!@#$%^'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf8 to binary', function (t) {
|
||||
/* jshint -W100 */
|
||||
t.equal(
|
||||
new B('öäüõÖÄÜÕ', 'utf8').toString('binary'),
|
||||
'öäüõÃÃÃÃ'
|
||||
)
|
||||
/* jshint +W100 */
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf8 replacement chars (1 byte sequence)', function (t) {
|
||||
t.equal(
|
||||
new B([ 0x80 ]).toString(),
|
||||
'\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0x7F ]).toString(),
|
||||
'\u007F'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf8 replacement chars (2 byte sequences)', function (t) {
|
||||
t.equal(
|
||||
new B([ 0xC7 ]).toString(),
|
||||
'\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xC7, 0xB1 ]).toString(),
|
||||
'\u01F1'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xC0, 0xB1 ]).toString(),
|
||||
'\uFFFD\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xC1, 0xB1 ]).toString(),
|
||||
'\uFFFD\uFFFD'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf8 replacement chars (3 byte sequences)', function (t) {
|
||||
t.equal(
|
||||
new B([ 0xE0 ]).toString(),
|
||||
'\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xE0, 0xAC ]).toString(),
|
||||
'\uFFFD\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xE0, 0xAC, 0xB9 ]).toString(),
|
||||
'\u0B39'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf8 replacement chars (4 byte sequences)', function (t) {
|
||||
t.equal(
|
||||
new B([ 0xF4 ]).toString(),
|
||||
'\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xF4, 0x8F ]).toString(),
|
||||
'\uFFFD\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xF4, 0x8F, 0x80 ]).toString(),
|
||||
'\uFFFD\uFFFD\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xF4, 0x8F, 0x80, 0x84 ]).toString(),
|
||||
'\uDBFC\uDC04'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xFF ]).toString(),
|
||||
'\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xFF, 0x8F, 0x80, 0x84 ]).toString(),
|
||||
'\uFFFD\uFFFD\uFFFD\uFFFD'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf8 replacement chars on 256 random bytes', function (t) {
|
||||
t.equal(
|
||||
new B([ 152, 130, 206, 23, 243, 238, 197, 44, 27, 86, 208, 36, 163, 184, 164, 21, 94, 242, 178, 46, 25, 26, 253, 178, 72, 147, 207, 112, 236, 68, 179, 190, 29, 83, 239, 147, 125, 55, 143, 19, 157, 68, 157, 58, 212, 224, 150, 39, 128, 24, 94, 225, 120, 121, 75, 192, 112, 19, 184, 142, 203, 36, 43, 85, 26, 147, 227, 139, 242, 186, 57, 78, 11, 102, 136, 117, 180, 210, 241, 92, 3, 215, 54, 167, 249, 1, 44, 225, 146, 86, 2, 42, 68, 21, 47, 238, 204, 153, 216, 252, 183, 66, 222, 255, 15, 202, 16, 51, 134, 1, 17, 19, 209, 76, 238, 38, 76, 19, 7, 103, 249, 5, 107, 137, 64, 62, 170, 57, 16, 85, 179, 193, 97, 86, 166, 196, 36, 148, 138, 193, 210, 69, 187, 38, 242, 97, 195, 219, 252, 244, 38, 1, 197, 18, 31, 246, 53, 47, 134, 52, 105, 72, 43, 239, 128, 203, 73, 93, 199, 75, 222, 220, 166, 34, 63, 236, 11, 212, 76, 243, 171, 110, 78, 39, 205, 204, 6, 177, 233, 212, 243, 0, 33, 41, 122, 118, 92, 252, 0, 157, 108, 120, 70, 137, 100, 223, 243, 171, 232, 66, 126, 111, 142, 33, 3, 39, 117, 27, 107, 54, 1, 217, 227, 132, 13, 166, 3, 73, 53, 127, 225, 236, 134, 219, 98, 214, 125, 148, 24, 64, 142, 111, 231, 194, 42, 150, 185, 10, 182, 163, 244, 19, 4, 59, 135, 16 ]).toString(),
|
||||
'\uFFFD\uFFFD\uFFFD\u0017\uFFFD\uFFFD\uFFFD\u002C\u001B\u0056\uFFFD\u0024\uFFFD\uFFFD\uFFFD\u0015\u005E\uFFFD\uFFFD\u002E\u0019\u001A\uFFFD\uFFFD\u0048\uFFFD\uFFFD\u0070\uFFFD\u0044\uFFFD\uFFFD\u001D\u0053\uFFFD\uFFFD\u007D\u0037\uFFFD\u0013\uFFFD\u0044\uFFFD\u003A\uFFFD\uFFFD\uFFFD\u0027\uFFFD\u0018\u005E\uFFFD\u0078\u0079\u004B\uFFFD\u0070\u0013\uFFFD\uFFFD\uFFFD\u0024\u002B\u0055\u001A\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\u0039\u004E\u000B\u0066\uFFFD\u0075\uFFFD\uFFFD\uFFFD\u005C\u0003\uFFFD\u0036\uFFFD\uFFFD\u0001\u002C\uFFFD\uFFFD\u0056\u0002\u002A\u0044\u0015\u002F\uFFFD\u0319\uFFFD\uFFFD\uFFFD\u0042\uFFFD\uFFFD\u000F\uFFFD\u0010\u0033\uFFFD\u0001\u0011\u0013\uFFFD\u004C\uFFFD\u0026\u004C\u0013\u0007\u0067\uFFFD\u0005\u006B\uFFFD\u0040\u003E\uFFFD\u0039\u0010\u0055\uFFFD\uFFFD\u0061\u0056\uFFFD\uFFFD\u0024\uFFFD\uFFFD\uFFFD\uFFFD\u0045\uFFFD\u0026\uFFFD\u0061\uFFFD\uFFFD\uFFFD\uFFFD\u0026\u0001\uFFFD\u0012\u001F\uFFFD\u0035\u002F\uFFFD\u0034\u0069\u0048\u002B\uFFFD\uFFFD\uFFFD\u0049\u005D\uFFFD\u004B\uFFFD\u0726\u0022\u003F\uFFFD\u000B\uFFFD\u004C\uFFFD\uFFFD\u006E\u004E\u0027\uFFFD\uFFFD\u0006\uFFFD\uFFFD\uFFFD\uFFFD\u0000\u0021\u0029\u007A\u0076\u005C\uFFFD\u0000\uFFFD\u006C\u0078\u0046\uFFFD\u0064\uFFFD\uFFFD\uFFFD\uFFFD\u0042\u007E\u006F\uFFFD\u0021\u0003\u0027\u0075\u001B\u006B\u0036\u0001\uFFFD\uFFFD\uFFFD\u000D\uFFFD\u0003\u0049\u0035\u007F\uFFFD\uFFFD\uFFFD\uFFFD\u0062\uFFFD\u007D\uFFFD\u0018\u0040\uFFFD\u006F\uFFFD\uFFFD\u002A\uFFFD\uFFFD\u000A\uFFFD\uFFFD\uFFFD\u0013\u0004\u003B\uFFFD\u0010'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf8 replacement chars for anything in the surrogate pair range', function (t) {
|
||||
t.equal(
|
||||
new B([ 0xED, 0x9F, 0xBF ]).toString(),
|
||||
'\uD7FF'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xED, 0xA0, 0x80 ]).toString(),
|
||||
'\uFFFD\uFFFD\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xED, 0xBE, 0x8B ]).toString(),
|
||||
'\uFFFD\uFFFD\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xED, 0xBF, 0xBF ]).toString(),
|
||||
'\uFFFD\uFFFD\uFFFD'
|
||||
)
|
||||
t.equal(
|
||||
new B([ 0xEE, 0x80, 0x80 ]).toString(),
|
||||
'\uE000'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('utf8 don\'t replace the replacement char', function (t) {
|
||||
t.equal(
|
||||
new B('\uFFFD').toString(),
|
||||
'\uFFFD'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
||||
var B = require('../').Buffer
|
||||
var test = require('tape')
|
||||
var isnan = require('is-nan')
|
||||
|
||||
test('buffer.write string should get parsed as number', function (t) {
|
||||
var b = new B(64)
|
||||
b.writeUInt16LE('1003', 0)
|
||||
t.equal(b.readUInt16LE(0), 1003)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('buffer.writeUInt8 a fractional number will get Math.floored', function (t) {
|
||||
// Some extra work is necessary to make this test pass with the Object implementation
|
||||
|
||||
var b = new B(1)
|
||||
b.writeInt8(5.5, 0)
|
||||
t.equal(b[0], 5)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('writeUint8 with a negative number throws', function (t) {
|
||||
var buf = new B(1)
|
||||
|
||||
t.throws(function () {
|
||||
buf.writeUInt8(-3, 0)
|
||||
})
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) {
|
||||
t.plan(2 * (2 * 2 * 2 + 2))
|
||||
var hex = [
|
||||
'03', '0300', '0003', '03000000', '00000003',
|
||||
'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd'
|
||||
]
|
||||
var reads = [ 3, 3, 3, 3, 3, -3, -3, -3, -3, -3 ]
|
||||
var xs = ['UInt', 'Int']
|
||||
var ys = [8, 16, 32]
|
||||
for (var i = 0; i < xs.length; i++) {
|
||||
var x = xs[i]
|
||||
for (var j = 0; j < ys.length; j++) {
|
||||
var y = ys[j]
|
||||
var endianesses = (y === 8) ? [''] : ['LE', 'BE']
|
||||
for (var k = 0; k < endianesses.length; k++) {
|
||||
var z = endianesses[k]
|
||||
|
||||
var v1 = new B(y / 8)
|
||||
var writefn = 'write' + x + y + z
|
||||
var val = (x === 'Int') ? -3 : 3
|
||||
v1[writefn](val, 0)
|
||||
t.equal(
|
||||
v1.toString('hex'),
|
||||
hex.shift()
|
||||
)
|
||||
var readfn = 'read' + x + y + z
|
||||
t.equal(
|
||||
v1[readfn](0),
|
||||
reads.shift()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) {
|
||||
if (!B.TYPED_ARRAY_SUPPORT) {
|
||||
t.pass('object impl: skipping overflow test')
|
||||
t.end()
|
||||
return
|
||||
}
|
||||
|
||||
t.plan(3 * (2 * 2 * 2 + 2))
|
||||
var hex = [
|
||||
'', '03', '00', '030000', '000000',
|
||||
'', 'fd', 'ff', 'fdffff', 'ffffff'
|
||||
]
|
||||
var reads = [
|
||||
undefined, 3, 0, NaN, 0,
|
||||
undefined, 253, -256, 16777213, -256
|
||||
]
|
||||
var xs = ['UInt', 'Int']
|
||||
var ys = [8, 16, 32]
|
||||
for (var i = 0; i < xs.length; i++) {
|
||||
var x = xs[i]
|
||||
for (var j = 0; j < ys.length; j++) {
|
||||
var y = ys[j]
|
||||
var endianesses = (y === 8) ? [''] : ['LE', 'BE']
|
||||
for (var k = 0; k < endianesses.length; k++) {
|
||||
var z = endianesses[k]
|
||||
|
||||
var v1 = new B(y / 8 - 1)
|
||||
var next = new B(4)
|
||||
next.writeUInt32BE(0, 0)
|
||||
var writefn = 'write' + x + y + z
|
||||
var val = (x === 'Int') ? -3 : 3
|
||||
v1[writefn](val, 0, true)
|
||||
t.equal(
|
||||
v1.toString('hex'),
|
||||
hex.shift()
|
||||
)
|
||||
// check that nothing leaked to next buffer.
|
||||
t.equal(next.readUInt32BE(0), 0)
|
||||
// check that no bytes are read from next buffer.
|
||||
next.writeInt32BE(~0, 0)
|
||||
var readfn = 'read' + x + y + z
|
||||
var r = reads.shift()
|
||||
if (isnan(r)) t.pass('equal')
|
||||
else t.equal(v1[readfn](0, true), r)
|
||||
}
|
||||
}
|
||||
}
|
||||
t.end()
|
||||
})
|
||||
test('large values do not imporoperly roll over (ref #80)', function (t) {
|
||||
var nums = [-25589992, -633756690, -898146932]
|
||||
var out = new B(12)
|
||||
out.fill(0)
|
||||
out.writeInt32BE(nums[0], 0)
|
||||
var newNum = out.readInt32BE(0)
|
||||
t.equal(nums[0], newNum)
|
||||
out.writeInt32BE(nums[1], 4)
|
||||
newNum = out.readInt32BE(4)
|
||||
t.equal(nums[1], newNum)
|
||||
out.writeInt32BE(nums[2], 8)
|
||||
newNum = out.readInt32BE(8)
|
||||
t.equal(nums[2], newNum)
|
||||
t.end()
|
||||
})
|
||||
Reference in New Issue
Block a user