browserHashUtils.js
1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var Buffer = require('buffer/').Buffer;
/**
* This is a polyfill for the static method `isView` of `ArrayBuffer`, which is
* e.g. missing in IE 10.
*
* @api private
*/
if (
typeof ArrayBuffer !== 'undefined' &&
typeof ArrayBuffer.isView === 'undefined'
) {
ArrayBuffer.isView = function(arg) {
return viewStrings.indexOf(Object.prototype.toString.call(arg)) > -1;
};
}
/**
* @api private
*/
var viewStrings = [
'[object Int8Array]',
'[object Uint8Array]',
'[object Uint8ClampedArray]',
'[object Int16Array]',
'[object Uint16Array]',
'[object Int32Array]',
'[object Uint32Array]',
'[object Float32Array]',
'[object Float64Array]',
'[object DataView]',
];
/**
* @api private
*/
function isEmptyData(data) {
if (typeof data === 'string') {
return data.length === 0;
}
return data.byteLength === 0;
}
/**
* @api private
*/
function convertToBuffer(data) {
if (typeof data === 'string') {
data = new Buffer(data, 'utf8');
}
if (ArrayBuffer.isView(data)) {
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength / Uint8Array.BYTES_PER_ELEMENT);
}
return new Uint8Array(data);
}
/**
* @api private
*/
module.exports = exports = {
isEmptyData: isEmptyData,
convertToBuffer: convertToBuffer,
};