forked from saneki-discontinued/node-toxcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
175 lines (160 loc) · 4.29 KB
/
util.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* This file is part of node-toxcore.
*
* node-toxcore is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* node-toxcore is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with node-toxcore. If not, see <http://www.gnu.org/licenses/>.
*
*/
var buffertools = require('buffertools');
var os = require('os');
var ref = require('ref');
var _ = require('underscore');
buffertools.extend();
/**
* Convert a hex string to a Buffer. If not a string, will just
* return what's passed to it.
* @param {String} hex - Hex string
* @return {Buffer} data
*/
var fromHex = function(hex) {
if(_.isString(hex)) {
return (new Buffer(hex)).fromHex();
}
return hex;
};
/**
* Try to parse an address:port string.
* @return {Object} Object with address and port if successful,
* undefined if not.
*/
var parseAddress = function(str) {
var ex = /^[^:]+:(\\d+)$/;
if(ex.test(str)) {
var res = ex.exec(str);
return {
address: res[1],
port: res[2]
};
}
};
/**
* Try to parse a Tox proxy string.
* @return {Object} Proxy object if successful, undefined if not
*/
var parseProxy = function(str) {
var type;
if(str.indexOf('http://') === 0) {
str = str.slice('http://'.length);
type = 'http';
} else if(str.indexOf('socks://') === 0) {
str = str.slice('socks://'.length);
type = 'socks';
} else if(str.indexOf('socks5://') === 0) {
str = str.slice('socks5://'.length);
type = 'socks';
}
var proxy = parseAddress(str);
if(proxy) {
proxy.type = type;
return proxy;
}
};
/**
* Get a "size_t type" (Buffer) from a Number.
* @param {Number} value
* @return {String} size_t
*/
var size_t = function(value) {
return value.toString();
/*
if(isNaN(value)) {
// @todo: Throw error
} else if(value < 0) {
// @todo: Throw error
}
var size = ref.alloc('size_t'),
e = os.endianness();
size.fill(0);
// @todo: Fix for 64-bit integers?
if(size.length === 8) {
if(e === 'BE') {
size.writeUInt32BE(value, 4);
} else {
size.writeUInt32LE(value, 0);
}
} else if(size.length === 4) {
if(e === 'BE') {
size.writeUInt32BE(value, 0);
} else {
size.writeUInt32LE(value, 0);
}
} else if (size.length === 2) {
if(e === 'BE') {
size.writeUInt16BE(value, 0);
} else {
size.writeUInt16LE(value, 0);
}
} else if (size.length === 1) {
size.writeUInt8(value, 0);
} else {
// @todo: Throw
}
return size;
*/
};
/**
* Helper for async functions that pass data through a callback in
* the form of (Error, Buffer). Will translate the Buffer to a hex
* String and pass that instead.
* @param {Function} asyncFunc Asynchronous function to call
* @param {Callback} callback
*/
var hexify = function(asyncFunc, callback) {
asyncFunc(function(err, buffer) {
if(callback) {
callback(err, buffer.toHex().toString());
}
});
};
/**
* Helper for sync functions that return a Buffer. Will translate
* the Buffer to a hex String and return that instead.
* @param {Function} syncFunction Synchronous function to get Buffer from
*/
var hexifySync = function(syncFunction) {
var addr = syncFunction();
return addr.toHex().toString();
};
/**
* Get a Date object from some UInt64. Expects the UInt64 to be represented
* as either a Number or String.
* @param {(Number|String)} timeval
* @return Date object, if timeval is less than 1 or a String this will be (new Date(NaN))
*/
var getDateFromUInt64 = function(timeval) {
// According to tox.h, timeval will be a unix-time timestamp,
// so seconds -> milliseconds
var date = new Date(NaN);
if(_.isNumber(timeval) && timeval >= 1) {
date = new Date(timeval * 1000);
}
return date;
};
module.exports = {
fromHex: fromHex,
getDateFromUInt64: getDateFromUInt64,
hexify: hexify,
hexifySync: hexifySync,
parseProxy: parseProxy,
size_t: size_t
};