-
Notifications
You must be signed in to change notification settings - Fork 27
/
sixpack.js
executable file
·260 lines (238 loc) · 9.69 KB
/
sixpack.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
(function () {
// Object.assign polyfill from https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill
Object.assign||Object.defineProperty(Object,"assign",{enumerable:!1,configurable:!0,writable:!0,value:function(e){"use strict";if(void 0===e||null===e)throw new TypeError("Cannot convert first argument to object");for(var r=Object(e),t=1;t<arguments.length;t++){var n=arguments[t];if(void 0!==n&&null!==n){n=Object(n);for(var o=Object.keys(Object(n)),a=0,c=o.length;c>a;a++){var i=o[a],b=Object.getOwnPropertyDescriptor(n,i);void 0!==b&&b.enumerable&&(r[i]=n[i])}}}return r}});
var sixpack = {
base_url: "http://localhost:5000",
ip_address: null,
user_agent: null,
timeout: 1000,
persist: true,
cookie_name: "sixpack_client_id",
cookie_domain: null,
ignore_alternates_warning: null
};
// check if on node, else expose on browser's global window object
var on_node = false;
if (typeof window === "undefined") {
on_node = true;
} else {
window["sixpack"] = sixpack;
}
sixpack.generate_client_id = function () {
// from http://stackoverflow.com/questions/105034
var client_id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
if (!on_node && this.persist) {
var cookie_value = this.cookie_name + "=" + client_id + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/";
if (this.cookie_domain) {
cookie_value += '; domain=' + this.cookie_domain;
}
document.cookie = cookie_value;
}
return client_id;
};
sixpack.persisted_client_id = function() {
// http://stackoverflow.com/questions/5639346/shortest-function-for-reading-a-cookie-in-javascript
var result;
return (result = new RegExp('(?:^|; )' + encodeURIComponent(this.cookie_name) + '=([^;]*)').exec(document.cookie)) ? (result[1]) : null;
}
sixpack.Session = function (options) {
Object.assign(this, sixpack, options);
if (!this.client_id) {
if (this.persist && !on_node) {
var persisted_id = this.persisted_client_id();
this.client_id = persisted_id !== null ? persisted_id : this.generate_client_id();
} else {
this.client_id = this.generate_client_id();
}
}
if (!on_node) {
this.user_agent = this.user_agent || (window && window.navigator && window.navigator.userAgent);
}
};
sixpack.Session.prototype = {
participate: function(experiment_name, alternatives, traffic_fraction, force, callback) {
if (typeof traffic_fraction === "function") {
callback = traffic_fraction;
traffic_fraction = null;
force = null;
}
else if (typeof traffic_fraction === "string") {
callback = force;
force = traffic_fraction;
traffic_fraction = null;
}
if (typeof force === "function") {
callback = force;
force = null;
}
if (!callback) {
throw new Error("Callback is not specified");
}
if (!experiment_name || !(/^[a-z0-9][a-z0-9\-_ ]*$/).test(experiment_name)) {
return callback(new Error("Bad experiment_name"));
}
if (alternatives.length < 2 && this.ignore_alternates_warning !== true) {
return callback(new Error("Must specify at least 2 alternatives"));
}
for (var i = 0; i < alternatives.length; i += 1) {
if (!(/^[a-z0-9][a-z0-9\-_ ]*$/).test(alternatives[i])) {
return callback(new Error("Bad alternative name: " + alternatives[i]));
}
}
var params = {client_id: this.client_id,
experiment: experiment_name,
alternatives: alternatives};
if (!on_node && force == null) {
var regex = new RegExp("[\\?&]sixpack-force-" + experiment_name + "=([^&#]*)");
var results = regex.exec(window.location.search);
if(results != null) {
force = decodeURIComponent(results[1].replace(/\+/g, " "));
}
}
if (traffic_fraction !== null && !isNaN(traffic_fraction)) {
params.traffic_fraction = traffic_fraction;
}
if (force != null && _in_array(alternatives, force)) {
return callback(null, {"status": "ok", "alternative": {"name": force}, "experiment": {"version": 0, "name": experiment_name}, "client_id": this.client_id});
}
if (this.ip_address) {
params.ip_address = this.ip_address;
}
if (this.user_agent) {
params.user_agent = this.user_agent;
}
return _request(this.base_url + "/participate", params, this.timeout, function(err, res) {
if (err) {
res = {status: "failed",
error: err,
alternative: {name: alternatives[0]}};
}
return callback(null, res);
});
},
convert: function(experiment_name, kpi, callback) {
if (typeof kpi === 'function') {
callback = kpi;
kpi = null;
}
if (!callback) {
callback = function(err) {
if (err && console && console.error) {
console.error(err);
}
}
}
if (!experiment_name || !(/^[a-z0-9][a-z0-9\-_ ]*$/).test(experiment_name)) {
return callback(new Error("Bad experiment_name"));
}
var params = {client_id: this.client_id,
experiment: experiment_name};
if (this.ip_address) {
params.ip_address = this.ip_address;
}
if (this.user_agent) {
params.user_agent = this.user_agent;
}
if (kpi) {
params.kpi = kpi;
}
return _request(this.base_url + "/convert", params, this.timeout, function(err, res) {
if (err) {
res = {status: "failed",
error: err};
}
return callback(null, res);
});
}
};
var counter = 0;
var _request = function(uri, params, timeout, callback) {
var timed_out = false;
var timeout_handle = setTimeout(function () {
timed_out = true;
return callback(new Error("request timed out"));
}, timeout);
if (!on_node) {
var cb = "callback" + (++counter);
params.callback = "sixpack." + cb
sixpack[cb] = function (res) {
if (!timed_out) {
clearTimeout(timeout_handle);
return callback(null, res);
}
}
}
var url = _request_uri(uri, params);
if (!on_node) {
script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.async = true;
document.body.appendChild(script);
} else {
if (url.indexOf('https') === 0) {
var http = require('https');
} else {
var http = require('http');
}
var req = http.get(url, function(res) {
var body = "";
res.on('data', function(chunk) {
return body += chunk;
});
return res.on('end', function() {
var data;
if (res.statusCode == 500) {
data = {status: "failed", response: body};
} else {
data = JSON.parse(body);
}
if (!timed_out) {
clearTimeout(timeout_handle);
return callback(null, data);
}
});
});
req.on('error', function(err) {
if (!timed_out) {
clearTimeout(timeout_handle);
return callback(err);
}
});
}
};
var _request_uri = function(endpoint, params) {
var query_string = [];
var e = encodeURIComponent;
for (var key in params) {
if (params.hasOwnProperty(key)) {
var vals = params[key];
if (Object.prototype.toString.call(vals) !== '[object Array]') {
vals = [vals];
}
for (var i = 0; i < vals.length; i += 1) {
query_string.push(e(key) + '=' + e(vals[i]));
}
}
}
if (query_string.length) {
endpoint += '?' + query_string.join('&');
}
return endpoint;
};
var _in_array = function(a, v) {
for(var i = 0; i < a.length; i++) {
if(a[i] === v) {
return true;
}
}
return false;
};
// export module for node or environments with module loaders, such as webpack
if (typeof module !== "undefined" && typeof require !== "undefined") {
module.exports = sixpack;
}
})();