forked from basharmadi/node-whatsapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
69 lines (53 loc) · 1.15 KB
/
common.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
var url = require('url');
var http = require('http');
var https = require('https');
function tstamp() {
return Math.floor(Date.now() / 1000);
}
function objSize(obj) {
var size = 0;
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
++size;
}
}
return size;
}
function toArray(iterable) {
var arr = [];
for(var i = 0, len = iterable.length; i < len; i++) {
arr.push(iterable[i]);
}
return arr;
}
function extend(dest) {
var args = toArray(arguments),
target = args.shift();
for(var i = 0, len = args.length, source; i < len; i++) {
source = args[i];
for(var key in source) {
if(source.hasOwnProperty(key)) {
target[key] = source[key];
}
}
}
return target;
}
function fetch(target, callback) {
var protocol = url.parse(target).protocol === 'https:' ? https : http;
protocol.get(target, function(res) {
var buffers = [];
res.on('data', function(buf) {
buffers.push(buf);
});
res.on('end', function() {
callback(false, Buffer.concat(buffers));
});
}).on('error', function(e) {
callback(e);
});
}
exports.tstamp = tstamp;
exports.objSize = objSize;
exports.extend = extend;
exports.fetch = fetch;