forked from shadowsocks/shadowsocks-dotcloud
-
Notifications
You must be signed in to change notification settings - Fork 467
/
utils.js
38 lines (32 loc) · 825 Bytes
/
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
import {Transform} from 'node:stream';
export function inetNtoa(family, buf) {
if (family === 4) return buf[0] + '.' + buf[1] + '.' + buf[2] + '.' + buf[3];
else if (family === 6) {
let addr = [];
for (let i = 0; i < 8; i++) {
addr.push(buf.readUInt16BE(i * 2, i * 2 + 2).toString(16));
}
return addr.join(':');
}
}
export function memoize(func) {
const cache = {};
return function (...args) {
const key = args.join('');
if (cache[key]) return cache[key];
const result = func.apply(this, args);
cache[key] = result;
return result;
};
}
export function createTransform(withFn) {
return new Transform({
transform(chunk, encoding, callback) {
try {
callback(null, withFn(chunk));
} catch (err) {
callback(err);
}
},
});
}