-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.js
68 lines (59 loc) · 1.51 KB
/
tracker.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
var http = require("http"),
https = require("https"),
url = require("url"),
pkg = require("./package.json");
function Tracker(opts) {
var _opts = opts || {};
function _track(opts, callback) {
var client,
err,
data = "";
var entity = {
type: opts.type,
project: opts.project || _opts.project,
db: opts.db || _opts.db,
agent: { name: "sss", version: pkg.version },
record: opts.record,
isLastRecord: opts.isLastRecord
};
if (!entity.isLastRecord)
delete entity.isLastRecord;
// make raw data
var buf = new Buffer(JSON.stringify(entity), "utf8");
// request options
var ropts = url.parse(opts.uri || _opts.uri);
ropts.method = "POST";
ropts.agent = false;
ropts.headers = {
"Content-Type": "application/json",
"Content-Length": buf.length
};
client = ropts.protocol === "https:" ? https : http;
var req = client.request(ropts)
.on("response", function (res) {
res
.on("data", function (chunk) {
data = data + chunk;
})
.on("end", function () {
if (res.statusCode === 200 || res.statusCode === 204) {
callback(null);
return;
}
// problems
err = new Error(res.statusCode + ": " + res.statusMessage);
err.data = data;
callback(err);
});
})
.on("error", function (err) {
callback(err);
});
req.setTimeout(5000);
req.write(buf);
req.end();
}
this._track = _track;
}
Tracker.prototype.track = function (opts, callback) { this._track(opts, callback); };
exports.Tracker = Tracker;