tlsd 2.4.1 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -3
- package/domains/README.txt +0 -0
- package/inchtml.js +40 -0
- package/package.json +2 -1
- package/proxy/config.json +5 -0
- package/proxy/package-lock.json +118 -0
- package/proxy/package.json +15 -0
- package/proxy/proxy.js +20 -0
- package/proxy/siriusmercantile.com/index.js +14 -0
- package/proxy/siriusmercantile.com/package-lock.json +137 -0
- package/proxy/siriusmercantile.com/package.json +7 -0
- package/proxy/zzz/was-nm/debug/CHANGELOG.md +395 -0
- package/proxy/zzz/was-nm/debug/LICENSE +19 -0
- package/proxy/zzz/was-nm/debug/README.md +437 -0
- package/proxy/zzz/was-nm/debug/dist/debug.js +886 -0
- package/proxy/zzz/was-nm/debug/node.js +1 -0
- package/proxy/zzz/was-nm/debug/package.json +90 -0
- package/proxy/zzz/was-nm/debug/src/browser.js +180 -0
- package/proxy/zzz/was-nm/debug/src/common.js +249 -0
- package/proxy/zzz/was-nm/debug/src/index.js +12 -0
- package/proxy/zzz/was-nm/debug/src/node.js +174 -0
- package/proxy/zzz/was-nm/eventemitter3/LICENSE +21 -0
- package/proxy/zzz/was-nm/eventemitter3/README.md +92 -0
- package/proxy/zzz/was-nm/eventemitter3/index.d.ts +67 -0
- package/proxy/zzz/was-nm/eventemitter3/index.js +336 -0
- package/proxy/zzz/was-nm/eventemitter3/package.json +84 -0
- package/proxy/zzz/was-nm/eventemitter3/umd/eventemitter3.js +340 -0
- package/proxy/zzz/was-nm/eventemitter3/umd/eventemitter3.min.js +1 -0
- package/proxy/zzz/was-nm/eventemitter3/umd/eventemitter3.min.js.map +1 -0
- package/proxy/zzz/was-nm/follow-redirects/LICENSE +18 -0
- package/proxy/zzz/was-nm/follow-redirects/README.md +145 -0
- package/proxy/zzz/was-nm/follow-redirects/http.js +1 -0
- package/proxy/zzz/was-nm/follow-redirects/https.js +1 -0
- package/proxy/zzz/was-nm/follow-redirects/index.js +452 -0
- package/proxy/zzz/was-nm/follow-redirects/package.json +87 -0
- package/proxy/zzz/was-nm/http-proxy/.auto-changelog +6 -0
- package/proxy/zzz/was-nm/http-proxy/.gitattributes +1 -0
- package/proxy/zzz/was-nm/http-proxy/CHANGELOG.md +1864 -0
- package/proxy/zzz/was-nm/http-proxy/CODE_OF_CONDUCT.md +74 -0
- package/proxy/zzz/was-nm/http-proxy/LICENSE +23 -0
- package/proxy/zzz/was-nm/http-proxy/README.md +568 -0
- package/proxy/zzz/was-nm/http-proxy/codecov.yml +10 -0
- package/proxy/zzz/was-nm/http-proxy/index.js +13 -0
- package/proxy/zzz/was-nm/http-proxy/lib/http-proxy/common.js +248 -0
- package/proxy/zzz/was-nm/http-proxy/lib/http-proxy/index.js +185 -0
- package/proxy/zzz/was-nm/http-proxy/lib/http-proxy/passes/web-incoming.js +192 -0
- package/proxy/zzz/was-nm/http-proxy/lib/http-proxy/passes/web-outgoing.js +147 -0
- package/proxy/zzz/was-nm/http-proxy/lib/http-proxy/passes/ws-incoming.js +162 -0
- package/proxy/zzz/was-nm/http-proxy/lib/http-proxy.js +66 -0
- package/proxy/zzz/was-nm/http-proxy/package.json +77 -0
- package/proxy/zzz/was-nm/http-proxy/renovate.json +19 -0
- package/proxy/zzz/was-nm/ms/index.js +162 -0
- package/proxy/zzz/was-nm/ms/license.md +21 -0
- package/proxy/zzz/was-nm/ms/package.json +69 -0
- package/proxy/zzz/was-nm/ms/readme.md +60 -0
- package/proxy/zzz/was-nm/requires-port/.travis.yml +19 -0
- package/proxy/zzz/was-nm/requires-port/LICENSE +22 -0
- package/proxy/zzz/was-nm/requires-port/README.md +47 -0
- package/proxy/zzz/was-nm/requires-port/index.js +38 -0
- package/proxy/zzz/was-nm/requires-port/package.json +74 -0
- package/proxy/zzz/was-nm/requires-port/test.js +98 -0
- package/proxy/zzz/was-nm/sleepless/README.md +4 -0
- package/proxy/zzz/was-nm/sleepless/autosite/README.txt +0 -0
- package/proxy/zzz/was-nm/sleepless/autosite/site/foobar.txt +1 -0
- package/proxy/zzz/was-nm/sleepless/autosite/site/pull.php +4 -0
- package/proxy/zzz/was-nm/sleepless/index.js +2 -0
- package/proxy/zzz/was-nm/sleepless/package.json +44 -0
- package/proxy/zzz/was-nm/sleepless/sleepless.js +613 -0
- package/proxy/zzz/was-nm/sleepless/test.html +9 -0
- package/proxy/zzz/was-nm/sleepless/test.js +146 -0
- package/server_static/rpc/rpc.js +2 -5
- package/test.html +4 -0
- package/tlsd +11 -2
- package/tlsd.js +16 -14
- package/foo/rpc/index.js +0 -13
- package/foo/static/index.html +0 -15
|
@@ -0,0 +1,613 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Sleepless Software Inc. All rights reserved.
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to
|
|
6
|
+
deal in the Software without restriction, including without limitation the
|
|
7
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
8
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
|
12
|
+
all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
19
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
20
|
+
IN THE SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
if((typeof process) === 'undefined') {
|
|
24
|
+
isBrowser = true;
|
|
25
|
+
isNode = false;
|
|
26
|
+
global = window;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
isNode = true;
|
|
30
|
+
isBrowser = false;
|
|
31
|
+
}
|
|
32
|
+
global.isNode = isNode;
|
|
33
|
+
global.isBrowser = isBrowser;
|
|
34
|
+
|
|
35
|
+
// for convenience
|
|
36
|
+
global.log = function(m) {
|
|
37
|
+
if(isBrowser && window["console"] === undefined) {
|
|
38
|
+
return; // console doesn't exist in IE unless in debug mode
|
|
39
|
+
}
|
|
40
|
+
if(typeof m === "object") {
|
|
41
|
+
m = o2j(m);
|
|
42
|
+
}
|
|
43
|
+
return console.log(m);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// throw an error if a condition is true
|
|
47
|
+
global.throwIf = function(c, s) { if(c) { throw new Error(s || "FAILED ASSERTION"); } }
|
|
48
|
+
|
|
49
|
+
// a default no-op function for callbacks
|
|
50
|
+
global.nop = function(){}
|
|
51
|
+
|
|
52
|
+
// return json as object or null if error
|
|
53
|
+
global.j2o = function(j) { try { return JSON.parse(j) } catch(e) { return null } }
|
|
54
|
+
|
|
55
|
+
// return object as JSON or null if error
|
|
56
|
+
global.o2j = function(o) { try { return JSON.stringify(o) } catch(e) { return null } }
|
|
57
|
+
|
|
58
|
+
// convert an arguments object to real array
|
|
59
|
+
// XXX DEPRECATE - suspect Firefox doesn't like this
|
|
60
|
+
global.args = function(a) { return Array.prototype.slice.call(a); }
|
|
61
|
+
|
|
62
|
+
// iterate through an object's attributes
|
|
63
|
+
global.eachVal = function(o, cb) { for(var k in o) { cb(o[k], k); } }; // XXX Use Object.keys() or deprecate
|
|
64
|
+
|
|
65
|
+
// convert whatever to float
|
|
66
|
+
global.toFlt = function(v) { return parseFloat((""+v).replace(/[^-.0-9]/g, "")) || 0.0; }
|
|
67
|
+
|
|
68
|
+
// convert whatever to integer
|
|
69
|
+
global.toInt = function(v) {
|
|
70
|
+
var n = toFlt(v);
|
|
71
|
+
return Math[n < 0 ? 'ceil' : 'floor'](n);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// convert from cents to bucks
|
|
75
|
+
global.centsToBucks = function(cents) {
|
|
76
|
+
return toFlt( toInt(cents) / 100 );
|
|
77
|
+
}
|
|
78
|
+
global.c2b = global.centsToBucks;
|
|
79
|
+
|
|
80
|
+
// convert bucks to cents
|
|
81
|
+
global.bucksToCents = function(bucks) {
|
|
82
|
+
// return Math.round( toFlt(bucks) * 100 ); --> can't use this; It breaks on #'s like 0.285 - )-:
|
|
83
|
+
return Math.round( (toFlt(bucks) * 1000) / 10 );
|
|
84
|
+
}
|
|
85
|
+
global.b2c = global.bucksToCents;
|
|
86
|
+
|
|
87
|
+
// format a number into a string with any # of decimal places, & alternative decimal & thousand-separation chars
|
|
88
|
+
global.numFmt = function(n, plcs, dot, sep) {
|
|
89
|
+
n = toFlt(n);
|
|
90
|
+
sep = typeof sep === "string" ? sep : ","; // thousands separator char
|
|
91
|
+
dot = typeof dot === "string" ? dot : "."; // decimal point char
|
|
92
|
+
plcs = toInt(plcs);
|
|
93
|
+
var p = Math.pow(10, plcs);
|
|
94
|
+
var n = Math.round( n * p ) / p;
|
|
95
|
+
var sign = n < 0 ? '-' : '';
|
|
96
|
+
n = Math.abs(+n || 0);
|
|
97
|
+
var intPart = parseInt(n.toFixed(plcs), 10) + '';
|
|
98
|
+
var j = intPart.length > 3 ? intPart.length % 3 : 0;
|
|
99
|
+
return sign +
|
|
100
|
+
(j ? intPart.substr(0, j) + sep : '') +
|
|
101
|
+
intPart.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + sep) +
|
|
102
|
+
(plcs ? dot + Math.abs(n - intPart).toFixed(plcs).slice(-plcs) : '');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// convert something like 1.234 to a string that looks like "123.4%" with optional decimal places
|
|
106
|
+
global.toPct = function(n, plcs, dot, sep) {
|
|
107
|
+
return numFmt(n * 100, plcs, dot, sep);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// convert whatever to a string that looks like "1,234.56"
|
|
111
|
+
global.toMoney = function(n, dot, sep) {
|
|
112
|
+
return numFmt(n, 2, dot, sep);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// return a human readable string that describes 'n' as a number of bytes; "1 KB", "21.5 MB", etc.
|
|
116
|
+
global.byteSize = function(sz) {
|
|
117
|
+
if(typeof sz != "number")
|
|
118
|
+
return sz;
|
|
119
|
+
if(sz < 1024)
|
|
120
|
+
return numFmt(sz, 0) + " B"
|
|
121
|
+
sz = sz / 1024
|
|
122
|
+
if(sz < 1024)
|
|
123
|
+
return numFmt(sz, 1) + " KB"
|
|
124
|
+
sz = sz / 1024
|
|
125
|
+
if(sz < 1024)
|
|
126
|
+
return numFmt(sz, 1) + " MB"
|
|
127
|
+
sz = sz / 1024
|
|
128
|
+
if(sz < 1024)
|
|
129
|
+
return numFmt(sz, 1) + " GB"
|
|
130
|
+
sz = sz / 1024
|
|
131
|
+
return numFmt(sz, 1) + " TB"
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// return "now" as Unix timestamp
|
|
135
|
+
global.time = function() { return toInt(new Date().getTime() / 1000); }
|
|
136
|
+
|
|
137
|
+
// convert "YYYY-MM-YY HH:MM:SS" to Unix timestamp
|
|
138
|
+
global.my2ts = function(m) {
|
|
139
|
+
if(m === "0000-00-00 00:00:00") {
|
|
140
|
+
return 0;
|
|
141
|
+
}
|
|
142
|
+
var a = m.split( /[^\d]+/ );
|
|
143
|
+
if(a.length != 6) {
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
var year = toInt(a[0]);
|
|
147
|
+
var month = toInt(a[1]);
|
|
148
|
+
var day = toInt(a[2]);
|
|
149
|
+
var hour = toInt(a[3]);
|
|
150
|
+
var minute = toInt(a[4]);
|
|
151
|
+
var second = toInt(a[5]);
|
|
152
|
+
var d = new Date(year, month - 1, day, hour, minute, second, 0);
|
|
153
|
+
return toInt(d.getTime() / 1000);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// convert Unix timestamp to "YYYY-MM-DD HH:MM:SS"
|
|
157
|
+
global.ts2my = function(ts) {
|
|
158
|
+
var d = ts2dt(ts);
|
|
159
|
+
if(!d) {
|
|
160
|
+
return "";
|
|
161
|
+
}
|
|
162
|
+
return ""+
|
|
163
|
+
d.getFullYear()+
|
|
164
|
+
"-"+
|
|
165
|
+
("0"+(d.getMonth() + 1)).substr(-2)+
|
|
166
|
+
"-"+
|
|
167
|
+
("0"+d.getDate()).substr(-2)+
|
|
168
|
+
" "+
|
|
169
|
+
("0"+d.getHours()).substr(-2)+
|
|
170
|
+
":"+
|
|
171
|
+
("0"+d.getMinutes()).substr(-2)+
|
|
172
|
+
":"+
|
|
173
|
+
("0"+d.getSeconds()).substr(-2)+
|
|
174
|
+
"";
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// convert Unix timestamp to Date
|
|
178
|
+
global.ts2dt = function(ts) {
|
|
179
|
+
ts = toInt(ts);
|
|
180
|
+
return ts ? new Date(ts * 1000) : null;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// convert Date to Unix timestamp
|
|
184
|
+
global.dt2ts = function(dt) {
|
|
185
|
+
if(! (dt instanceof Date) )
|
|
186
|
+
return 0;
|
|
187
|
+
return toInt(dt.getTime() / 1000);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// Convert "MM/DD/YYYY HH:MM:SS" to Date object or null if "s" can't be parsed
|
|
191
|
+
// If year is 2 digits, it will try guess what you meant
|
|
192
|
+
// Time part (HH:MM:SS) can be omitted and seconds is optional
|
|
193
|
+
// if utc argument is truthy, then return a UTC version
|
|
194
|
+
global.us2dt = function(us, utc) {
|
|
195
|
+
|
|
196
|
+
if(!us) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
var m = (""+us).split( /[^\d]+/ );
|
|
201
|
+
|
|
202
|
+
if(m.length < 3) {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
while(m.length < 7) {
|
|
206
|
+
m.push("0");
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// try to convert 2 digit year to 4 digits (best guess)
|
|
210
|
+
var year = toInt(m[2]);
|
|
211
|
+
var nowyear = new Date().getFullYear();
|
|
212
|
+
if(year <= ((nowyear + 10) - 2000))
|
|
213
|
+
year = 2000 + year;
|
|
214
|
+
if(year < 100)
|
|
215
|
+
year = 1900 + year;
|
|
216
|
+
|
|
217
|
+
var mon = toInt(m[0]) - 1;
|
|
218
|
+
var date = toInt(m[1]);
|
|
219
|
+
|
|
220
|
+
var hour = toInt(m[3]);
|
|
221
|
+
var min = toInt(m[4]);
|
|
222
|
+
var sec = toInt(m[5]);
|
|
223
|
+
var ms = toInt(m[6]);
|
|
224
|
+
|
|
225
|
+
if(utc) {
|
|
226
|
+
return new Date(Date.UTC(year, mon, date, hour, min, sec, ms));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return new Date(year, mon, date, hour, min, sec, ms);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
// Convert "MM/DD/YYYY HH:MM:SS" to Unix timestamp.
|
|
234
|
+
// If utc argument is truthy, then return a UTC version.
|
|
235
|
+
global.us2ts = function(us, utc) {
|
|
236
|
+
return dt2ts(us2dt(us, utc));
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
// Convert Unix timestamp to "MM/DD/YYYY HH:MM:SS".
|
|
240
|
+
global.ts2us = function(ts) {
|
|
241
|
+
var d = ts2dt(ts);
|
|
242
|
+
if(!d) {
|
|
243
|
+
return "";
|
|
244
|
+
}
|
|
245
|
+
return ""+
|
|
246
|
+
("0"+(d.getMonth() + 1)).substr(-2)+
|
|
247
|
+
"/"+
|
|
248
|
+
("0"+d.getDate()).substr(-2)+
|
|
249
|
+
"/"+
|
|
250
|
+
d.getFullYear()+
|
|
251
|
+
" "+
|
|
252
|
+
("0"+d.getHours()).substr(-2)+
|
|
253
|
+
":"+
|
|
254
|
+
("0"+d.getMinutes()).substr(-2)+
|
|
255
|
+
":"+
|
|
256
|
+
("0"+d.getSeconds()).substr(-2)+
|
|
257
|
+
"";
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Convert Unix timestamp to "MM/DD".
|
|
261
|
+
global.ts2us_md = function(ts) {
|
|
262
|
+
return ts2us(ts).substr(0, 5);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Convert Unix timestamp to "MM/DD/YYYY".
|
|
266
|
+
global.ts2us_mdy = function(ts) {
|
|
267
|
+
return ts2us(ts).substr(0, 10);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Convert Unix timestamp to "MM/DD/YY"
|
|
271
|
+
global.ts2us_mdy2 = function(ts) {
|
|
272
|
+
var a = ts2us_mdy(ts).split("/");
|
|
273
|
+
if(a.length != 3) {
|
|
274
|
+
return a;
|
|
275
|
+
}
|
|
276
|
+
a[2] = a[2].substr(2);
|
|
277
|
+
return a.join("/");
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Convert Unix timestamp to "HH:MM"
|
|
281
|
+
global.ts2us_hm = function(ts) {
|
|
282
|
+
return ts2us(ts).substr(11, 5);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Convert Unix timestamp to "MM/DD/YYYY HH:MM"
|
|
286
|
+
global.ts2us_mdyhm = function(ts) {
|
|
287
|
+
return ts2us_mdy(ts) + " " + ts2us_hm(ts);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Convert Unix timestamp to "MM/DD/YY HH:MM"
|
|
291
|
+
global.ts2us_mdy2hm = function(ts) {
|
|
292
|
+
return ts2us_mdy2(ts) + " " + ts2us_hm(ts);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Convert Unix timestamp to something like "01-Jan-2016"
|
|
296
|
+
global.ts2us_dMy = function(ts) {
|
|
297
|
+
var d = ts2dt(ts);
|
|
298
|
+
if(!d) {
|
|
299
|
+
return "";
|
|
300
|
+
}
|
|
301
|
+
var month_names = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" ");
|
|
302
|
+
return ""+
|
|
303
|
+
("0"+d.getDate()).substr(-2)+
|
|
304
|
+
"-"+
|
|
305
|
+
month_names[d.getMonth()]+
|
|
306
|
+
"-"+
|
|
307
|
+
d.getFullYear();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
// Make all lowercase
|
|
313
|
+
String.prototype.lcase = function() { return this.toLowerCase() }
|
|
314
|
+
|
|
315
|
+
// Make all uppercase
|
|
316
|
+
String.prototype.ucase = function() { return this.toUpperCase() }
|
|
317
|
+
|
|
318
|
+
// Capitalize first word
|
|
319
|
+
String.prototype.ucfirst = function() {
|
|
320
|
+
return this.substring(0,1).toUpperCase() + this.substring(1)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Capitalize all words
|
|
324
|
+
String.prototype.ucwords = function( sep ) {
|
|
325
|
+
sep = sep || /[\s]+/;
|
|
326
|
+
var a = this.split( sep );
|
|
327
|
+
for( var i = 0; i < a.length; i++ ) {
|
|
328
|
+
a[ i ] = a[ i ].ucfirst();
|
|
329
|
+
}
|
|
330
|
+
return a.join( " " );
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Returns true if the string begins with the prefix string
|
|
334
|
+
if( String.prototype.startsWith === undefined ) {
|
|
335
|
+
String.prototype.startsWith = function(prefix) {
|
|
336
|
+
return this.substr(0, prefix.length) == prefix;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Returns true if the string ends with the suffix string
|
|
341
|
+
if( String.prototype.endsWith === undefined ) {
|
|
342
|
+
String.prototype.endsWith = function(suffix) {
|
|
343
|
+
return this.substr(-suffix.length) == suffix;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Abbreviate to l chars with ellipses
|
|
348
|
+
String.prototype.abbr = function(l) {
|
|
349
|
+
l = toInt(l) || 5;
|
|
350
|
+
if(this.length <= l) {
|
|
351
|
+
return "" + this; // Cuz ... some times this === a String object, not a literal
|
|
352
|
+
}
|
|
353
|
+
return this.substr(0, l - 4) + " ...";
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Convert a string from something like "prof_fees" to "Prof Fees"
|
|
357
|
+
String.prototype.toLabel = function() {
|
|
358
|
+
var s = this.replace( /[_]+/g, " " );
|
|
359
|
+
s = s.ucwords();
|
|
360
|
+
return s;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Convert a string from something like "Prof. Fees" to "prof_fees"
|
|
364
|
+
String.prototype.toId = function() {
|
|
365
|
+
var s = this.toLowerCase();
|
|
366
|
+
s = s.replace( /[^a-z0-9]+/g, " " );
|
|
367
|
+
s = s.trim();
|
|
368
|
+
s = s.replace( /\s+/g, "_" );
|
|
369
|
+
return s;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Returns true if string contains all of the arguments
|
|
373
|
+
// "I,\nhave a lovely bunch of coconuts".looksLike("i have", "coconuts") == true
|
|
374
|
+
String.prototype.looksLike = function() {
|
|
375
|
+
var a = Array.prototype.slice.call(arguments); // convert arguments to true array
|
|
376
|
+
var s = "_" + this.toLowerCase().toId() + "_"; //.split("_"); //toLowerCase();
|
|
377
|
+
for(var i = 0; i < a.length; i++) {
|
|
378
|
+
var t = "_" + (a[i].toLowerCase().toId()) + "_";
|
|
379
|
+
if(s.indexOf(t) == -1)
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// Returns true if the string is a valid email address
|
|
386
|
+
String.prototype.is_email = function() {
|
|
387
|
+
return /^[A-Za-z0-9_\+-]+(\.[A-Za-z0-9_\+-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.([A-Za-z]{2,})$/.test(this);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// So I can use each() instead of forEach
|
|
391
|
+
Array.prototype.each = Array.prototype.forEach;
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
// Returns something like "3 minutes ago"
|
|
395
|
+
// Pass truthy value for no_suffix to suppress the " ago" at the end
|
|
396
|
+
global.agoStr = function(ts, no_suffix) {
|
|
397
|
+
if(ts == 0)
|
|
398
|
+
return "";
|
|
399
|
+
|
|
400
|
+
var t = time() - ts;
|
|
401
|
+
if(t < 1)
|
|
402
|
+
return "Just now";
|
|
403
|
+
|
|
404
|
+
var v = ""
|
|
405
|
+
var round = Math.round
|
|
406
|
+
|
|
407
|
+
if(t>31536000) v = round(t/31536000,0)+' year';
|
|
408
|
+
else if(t>2419200) v = round(t/2419200,0)+' month';
|
|
409
|
+
else if(t>604800) v = round(t/604800,0)+' week';
|
|
410
|
+
else if(t>86400) v = round(t/86400,0)+' day';
|
|
411
|
+
else if(t>3600) v = round(t/3600,0)+' hour';
|
|
412
|
+
else if(t>60) v = round(t/60,0)+' minute';
|
|
413
|
+
else v = t+' second';
|
|
414
|
+
|
|
415
|
+
if(toInt(v) > 1)
|
|
416
|
+
v += 's';
|
|
417
|
+
|
|
418
|
+
return v + (no_suffix ? "" : " ago");
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if(isNode) {
|
|
422
|
+
// We are in Node.js
|
|
423
|
+
|
|
424
|
+
// XXX Might be nice if this could fetch URL's too.
|
|
425
|
+
global.getFile = function(path, enc, cb) {
|
|
426
|
+
var fs = require("fs");
|
|
427
|
+
if(!cb) {
|
|
428
|
+
return fs.readFileSync(path, enc);
|
|
429
|
+
}
|
|
430
|
+
fs.readFile(path, enc, cb);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
global.sha1 = function(s) {
|
|
434
|
+
var h = require("crypto").createHash("sha1");
|
|
435
|
+
h.update(s);
|
|
436
|
+
return h.digest("hex");
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
global.sha256 = function(s) {
|
|
440
|
+
var h = require("crypto").createHash("sha256");
|
|
441
|
+
h.update(s);
|
|
442
|
+
return h.digest("hex");
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
global.EE = function(Ctr) {
|
|
446
|
+
var EventEmitter = require("events");
|
|
447
|
+
require("util").inherits(Ctr, EventEmitter);
|
|
448
|
+
var o = new Ctr();
|
|
449
|
+
EventEmitter.call(o);
|
|
450
|
+
return o;
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
// We are in a browser.
|
|
457
|
+
|
|
458
|
+
global.LS = {
|
|
459
|
+
get: function(k) {
|
|
460
|
+
try {
|
|
461
|
+
return localStorage.getItem(k);
|
|
462
|
+
} catch(e) { }
|
|
463
|
+
return null;
|
|
464
|
+
},
|
|
465
|
+
set: function(k, v) {
|
|
466
|
+
try {
|
|
467
|
+
return localStorage.setItem(k, v);
|
|
468
|
+
} catch(e) { }
|
|
469
|
+
return null;
|
|
470
|
+
},
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
// navigate to new url
|
|
474
|
+
global.jmp = function(url) { document.location = url; }
|
|
475
|
+
|
|
476
|
+
// reload page
|
|
477
|
+
global.reload = function() { document.location.reload(); }
|
|
478
|
+
|
|
479
|
+
// make an async HTTP GET request for a URL
|
|
480
|
+
global.getFile = function(url, cb) {
|
|
481
|
+
var x = new XMLHttpRequest();
|
|
482
|
+
x.onload = function() { cb(x.responseText, x); };
|
|
483
|
+
x.open("GET", url);
|
|
484
|
+
x.send();
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
// Shows a browser notification if permission granted.
|
|
488
|
+
// Ask permission if user hasn't yet been asked and honor their choice thereafter.
|
|
489
|
+
// title = Title string to show in notice (name of app typically)
|
|
490
|
+
// text = String message to display.
|
|
491
|
+
// icon = String path to graphic
|
|
492
|
+
// onclick = function to call if notification is clicked
|
|
493
|
+
global.showNotification = function(title, text, icon, onclick) {
|
|
494
|
+
if("Notification" in window) {
|
|
495
|
+
let do_notice = function() {
|
|
496
|
+
let n = new Notification(title, {body: text, icon:icon});
|
|
497
|
+
if(onclick) {
|
|
498
|
+
n.addEventListener("click", onclick);
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
if(Notification.permission === "granted") {
|
|
502
|
+
do_notice();
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
if(Notification.permission !== "denied") {
|
|
506
|
+
Notification.requestPermission(function(permission) {
|
|
507
|
+
if(Notification.permission === "granted") {
|
|
508
|
+
do_notice();
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
global.getQueryData = function() {
|
|
517
|
+
var o = {}
|
|
518
|
+
var s = document.location.search
|
|
519
|
+
if(s) {
|
|
520
|
+
var kv = s.substr(1).split("&")
|
|
521
|
+
for(var i = 0; i < kv.length; i++) {
|
|
522
|
+
var aa = kv[i].split("=")
|
|
523
|
+
o[aa[0]] = decodeURIComponent(aa[1])
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return o
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
global.getEl = function(id) {
|
|
530
|
+
return document.getElementById(id);
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
HTMLCollection.prototype.toArray = function() {
|
|
534
|
+
let arr = [];
|
|
535
|
+
for(let i = 0; i < this.length; i++) {
|
|
536
|
+
arr.push( this[ i ]);
|
|
537
|
+
}
|
|
538
|
+
return arr;
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
NodeList.prototype.toArray = HTMLCollection.prototype.toArray;
|
|
542
|
+
|
|
543
|
+
HTMLElement.prototype.addClass = function(c) {
|
|
544
|
+
let cl = this.classList;
|
|
545
|
+
if( ! cl.contains( c ) )
|
|
546
|
+
cl.add( c );
|
|
547
|
+
return this;
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
HTMLElement.prototype.remClass = function(c) {
|
|
551
|
+
let cl = this.classList;
|
|
552
|
+
if( cl.contains( c ) )
|
|
553
|
+
cl.remove( c );
|
|
554
|
+
return this;
|
|
555
|
+
};
|
|
556
|
+
|
|
557
|
+
global.QS = function( qs ) {
|
|
558
|
+
return document.querySelectorAll( qs ).toArray();
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
HTMLElement.prototype.attr = function(a, v) {
|
|
562
|
+
if(v !== undefined) {
|
|
563
|
+
this.setAttribute(a, v);
|
|
564
|
+
return this;
|
|
565
|
+
}
|
|
566
|
+
else {
|
|
567
|
+
return this.getAttribute(a);
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
HTMLElement.prototype.val = function(v) {
|
|
572
|
+
if(v !== undefined) {
|
|
573
|
+
this.value = v;
|
|
574
|
+
return this;
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
return (this.value || "").trim();
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
HTMLElement.prototype.html = function(h) {
|
|
582
|
+
if(h !== undefined) {
|
|
583
|
+
this.innerHTML = h;
|
|
584
|
+
return this;
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
return this.innerHTML;
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
/* --------- deprecate these -------------- */
|
|
592
|
+
// return element with id
|
|
593
|
+
I = function(id) { return document.getElementById(id); }
|
|
594
|
+
|
|
595
|
+
// return value of element with id (generally an input type element)
|
|
596
|
+
// if input is a checkbox, return true/false (checked/unchecked)
|
|
597
|
+
V = function(id) {
|
|
598
|
+
var e = I(id);
|
|
599
|
+
return (e.type === "checkbox") ? (e.checked ? 1 : 0) : e.value.trim();
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// return value of element with id as Unix timestamp
|
|
603
|
+
V.ts = function(id) { return us2ts(V(id)); }
|
|
604
|
+
|
|
605
|
+
// return value of element with id as integer
|
|
606
|
+
V.int = function(id) { return toInt(V(id)); }
|
|
607
|
+
|
|
608
|
+
// return value of element with id as float
|
|
609
|
+
V.flt = function(id) { return toFlt(V(id)); }
|
|
610
|
+
/* --------- deprecate these -------------- */
|
|
611
|
+
|
|
612
|
+
}
|
|
613
|
+
|