-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.js
97 lines (78 loc) · 2.35 KB
/
base.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
"use strict";
var generateIdForRequest = require("./lib/generateIdForRequest.js");
/**
* Base Constructor
*
* The Metris Base is the hub that tracks requests to your webserver. Each base is assigned to one http server.
* This is to ensure maximum flexibility in structuring your dashboard while making it easy to trace request
* routes in the case of load balanced services. If you're not interested in that, you can get the aggregate
* by giving them the same name.
*
* @param httpServer {http.Server}
* @param baseName {string} - name to identify this server [1-20 alphanumeric characters long]
* @constructor
*/
function Base(httpServer, baseName) {
if (!(this instanceof Base)) {
return new Base(httpServer);
}
/**
* "Pointer" to an http Server instance. These are the requests this base instance will listen to
* @private
* @type {http.Server}
*/
var _server = null;
/**
* Name of this base. All requests to this base will be grouped under this name.
* @private
* @type {string}
*/
var _name = null;
//Setters & Getters
this.__defineSetter__("server", function (httpServer) {
if (typeof httpServer !== "object") {
throw Error("httpServer must be a http server object.");
}
if (httpServer.constructor.name !== "Server") {
throw Error("httpServer must be a http server object.");
}
_server = httpServer;
});
this.__defineGetter__("server", function () {
if (_server === null) {
throw Error("this base instance does httpServer not set.");
}
return _server;
});
this.__defineSetter__("name", function (serverName) {
if (typeof serverName !== "string") {
throw Error("the name must be a string.");
} else if ((serverName.length > 20) || (serverName.length < 1)) {
throw Error("name must be between 1-20 chars");
}
_name = serverName;
});
this.__defineGetter__("name", function () {
return _name;
});
}
Base.prototype = {
/**
* @private
*/
_generateRandomKeyForIncomingRequest: function () {
return generateIdForRequest(this.name);
},
/**
*
* @param request
* @param response
*/
newIncomingHttpRequest: function newIncomingHttpRequest(request, response) {
request._metris = {
startTime: Date.now(),
requestId: this._generateRandomKeyForIncomingRequest()
};
}
};
module.exports = Base;