/
util.js
216 lines (180 loc) · 4.63 KB
/
util.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
/* exported Util, Params, DummyRateLimit */
/*
A JavaScript WebRTC snowflake proxy
Contains helpers for parsing query strings and other utilities.
*/
class Util {
static genSnowflakeID() {
return Math.random().toString(36).substring(2);
}
static hasWebRTC() {
return typeof PeerConnection === 'function';
}
static hasCookies() {
return navigator.cookieEnabled;
}
}
class Parse {
// Parse a cookie data string (usually document.cookie). The return type is an
// object mapping cookies names to values. Returns null on error.
// http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-8747038
static cookie(cookies) {
var i, j, len, name, result, string, strings, value;
result = {};
strings = [];
if (cookies) {
strings = cookies.split(';');
}
for (i = 0, len = strings.length; i < len; i++) {
string = strings[i];
j = string.indexOf('=');
if (-1 === j) {
return null;
}
name = decodeURIComponent(string.substr(0, j).trim());
value = decodeURIComponent(string.substr(j + 1).trim());
if (!(name in result)) {
result[name] = value;
}
}
return result;
}
// Parse an address in the form 'host:port'. Returns an Object with keys 'host'
// (String) and 'port' (int). Returns null on error.
static address(spec) {
var host, m, port;
m = null;
if (!m) {
// IPv6 syntax.
m = spec.match(/^\[([\0-9a-fA-F:.]+)\]:([0-9]+)$/);
}
if (!m) {
// IPv4 syntax.
m = spec.match(/^([0-9.]+):([0-9]+)$/);
}
if (!m) {
// TODO: Domain match
return null;
}
host = m[1];
port = parseInt(m[2], 10);
if (isNaN(port) || port < 0 || port > 65535) {
return null;
}
return {
host: host,
port: port
};
}
// Parse a count of bytes. A suffix of 'k', 'm', or 'g' (or uppercase)
// does what you would think. Returns null on error.
static byteCount(spec) {
let matches = spec.match(/^(\d+(?:\.\d*)?)(\w*)$/);
if (matches === null) {
return null;
}
let count = Number(matches[1]);
if (isNaN(count)) {
return null;
}
const UNITS = new Map([
['', 1],
['k', 1024],
['m', 1024*1024],
['g', 1024*1024*1024],
]);
let unit = matches[2].toLowerCase();
if (!UNITS.has(unit)) {
return null;
}
let multiplier = UNITS.get(unit);
return count * multiplier;
}
// Parse a connection-address out of the "c=" Connection Data field of a
// session description. Return undefined if none is found.
// https://tools.ietf.org/html/rfc4566#section-5.7
static ipFromSDP(sdp) {
var i, len, m, pattern, ref;
ref = [/^c=IN IP4 ([\d.]+)(?:(?:\/\d+)?\/\d+)?(:? |$)/m, /^c=IN IP6 ([0-9A-Fa-f:.]+)(?:\/\d+)?(:? |$)/m];
for (i = 0, len = ref.length; i < len; i++) {
pattern = ref[i];
m = pattern.exec(sdp);
if (m != null) {
return m[1];
}
}
}
}
class Params {
static getBool(query, param, defaultValue) {
if (!query.has(param)) {
return defaultValue;
}
var val;
val = query.get(param);
if ('true' === val || '1' === val || '' === val) {
return true;
}
if ('false' === val || '0' === val) {
return false;
}
return null;
}
// Get an object value and parse it as a byte count. Example byte counts are
// '100' and '1.3m'. Returns |defaultValue| if param is not a key. Return null
// on a parsing error.
static getByteCount(query, param, defaultValue) {
if (!query.has(param)) {
return defaultValue;
}
return Parse.byteCount(query.get(param));
}
}
class BucketRateLimit {
constructor(capacity, time) {
this.capacity = capacity;
this.time = time;
}
age() {
var delta, now;
now = new Date();
delta = (now - this.lastUpdate) / 1000.0;
this.lastUpdate = now;
this.amount -= delta * this.capacity / this.time;
if (this.amount < 0.0) {
return this.amount = 0.0;
}
}
update(n) {
this.age();
this.amount += n;
return this.amount <= this.capacity;
}
// How many seconds in the future will the limit expire?
when() {
this.age();
return (this.amount - this.capacity) / (this.capacity / this.time);
}
isLimited() {
this.age();
return this.amount > this.capacity;
}
}
BucketRateLimit.prototype.amount = 0.0;
BucketRateLimit.prototype.lastUpdate = new Date();
// A rate limiter that never limits.
class DummyRateLimit {
constructor(capacity, time) {
this.capacity = capacity;
this.time = time;
}
update() {
return true;
}
when() {
return 0.0;
}
isLimited() {
return false;
}
}