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