-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
55 lines (45 loc) · 1.34 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
'use strict'
/* istanbul ignore next */
function setLogger (logger) {
module.exports.logger = logger
}
var BASE = 16 * 16
var LOG_BASE = Math.log(BASE)
var POW_3_BASE = Math.pow(BASE, 3)
var POW_2_BASE = Math.pow(BASE, 2)
var POW_1_BASE = Math.pow(BASE, 1)
var NullCharBuffer = new Buffer([0x00])
function getBuffer (str, options) {
if (Buffer.isBuffer(str)) return str
str = options && options.addNullInTheEnd ? str + '\0' : str
var buff = new Buffer(str, 'utf8')
return buff
}
function getBufferForTheLength (number, len) {
if (!len) len = Math.ceil(Math.log(number) / LOG_BASE)
// -Infinity
if (len < 0) len = 0
var buff = new Buffer(len)
while (len--) {
buff[len] = number % BASE
number = Math.floor(number / BASE)
}
if (number > BASE) throw new Error('Body to large!')
return buff
}
function getContentLengthFromBuffer (contentLengthBuffer) {
return contentLengthBuffer[0] * POW_3_BASE + contentLengthBuffer[1] * POW_2_BASE + contentLengthBuffer[2] * POW_1_BASE + contentLengthBuffer[3]
}
var nullFunction = function () {}
module.exports = {
logger: {
debug: nullFunction,
info: nullFunction,
error: nullFunction
},
setLogger: setLogger,
NullCharBuffer: NullCharBuffer,
getBuffer: getBuffer,
getBufferForTheLength: getBufferForTheLength,
getContentLengthFromBuffer: getContentLengthFromBuffer
}