-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathutils.js
158 lines (139 loc) · 4.2 KB
/
utils.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict';
exports.assertArrayEqual = function (array1, array2) {
if (array1.length !== array2.length) return false;
for (var i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) return false;
}
return true;
};
// String to arraybuffer
exports.stringToArrayBuffer = function (string) {
var dataBuffer = new Uint8Array(new ArrayBuffer(string.length));
// Return the strings
for (var i = 0; i < string.length; i++) {
dataBuffer[i] = string.charCodeAt(i);
}
// Return the data buffer
return dataBuffer;
};
// String to arraybuffer
exports.stringToArray = function (string) {
var dataBuffer = new Array(string.length);
// Return the strings
for (var i = 0; i < string.length; i++) {
dataBuffer[i] = string.charCodeAt(i);
}
// Return the data buffer
return dataBuffer;
};
exports.Utf8 = {
// public method for url encoding
encode: function (string) {
string = string.replace(/\r\n/g, '\n');
var utftext = '';
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if (c > 127 && c < 2048) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// public method for url decoding
decode: function (utftext) {
var string = '';
var i = 0;
var c = 0,
c2 = 0,
c3 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if (c > 191 && c < 224) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
};
exports.assertBuffersEqual = function (done, buffer1, buffer2) {
if (buffer1.length !== buffer2.length) {
done('Buffers do not have the same length', buffer1, buffer2);
}
for (var i = 0; i < buffer1.length; i++) {
expect(buffer1[i]).to.equal(buffer2[i]);
}
};
/**
* A helper to turn hex string sequences into BSON.
* Omit the first 8 hex digits for the document it will be calculated
* As well as the BSON document's null terminator '00'
*
* @example
* ```js
* const bytes = bufferFromHexArray([
* '10', // int32 type
* '6100', // 'a' key with key null terminator
* '01000000' // little endian int32
* ])
* BSON.serialize(bytes) // { a: 1 }
* ```
*
* @param {string[]} array - sequences of hex digits broken up to be human readable
* @returns
*/
const bufferFromHexArray = array => {
const string = array.concat(['00']).join('');
const size = string.length / 2 + 4;
const byteLength = [size & 0xff, (size >> 8) & 0xff, (size >> 16) & 0xff, (size >> 24) & 0xff]
.map(n => {
const hexCode = n.toString(16);
return hexCode.length === 2 ? hexCode : '0' + hexCode;
})
.join('');
return Buffer.from(byteLength + string, 'hex');
};
exports.bufferFromHexArray = bufferFromHexArray;
/**
* A helper to calculate the byte size of a string (including null)
*
* ```js
* const x = stringToUTF8HexBytes('ab') // { x: '03000000616200' }
*
* @param string - representing what you want to encode into BSON
* @returns BSON string with byte size encoded
*/
const stringToUTF8HexBytes = str => {
var b = Buffer.from(str, 'utf8');
var len = b.byteLength;
var out = Buffer.alloc(len + 4 + 1);
out.writeInt32LE(len + 1, 0);
out.set(b, 4);
out[len + 1] = 0x00;
return out.toString('hex');
};
exports.stringToUTF8HexBytes = stringToUTF8HexBytes;
exports.isBrowser = function () {
// eslint-disable-next-line no-undef
return typeof window === 'object' && typeof window['navigator'] === 'object';
};
exports.isNode6 = function () {
// eslint-disable-next-line no-undef
return process.version.split('.')[0] === 'v6';
};