/
helpers.js
255 lines (207 loc) · 6.35 KB
/
helpers.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
'use strict';
// Define some pseudo module globals
var isPro = require('../libs/debug').isPro;
var isDev = require('../libs/debug').isDev;
var isDbg = require('../libs/debug').isDbg;
//
var url = require("url");
var _ = require('underscore');
var months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
];
var second = 1000;
var minute = second * 60;
var hour = minute * 60;
var day = hour * 24;
var week = day * 7;
// Get a formatted date that can be used everywhere a date is displayed
exports.formatDate = function (aDate) {
var difference = new Date().getTime() - aDate.getTime();
var ret = '';
var days = 0;
function pluralize(aNumber, aUnit) {
return aNumber + ' ' + aUnit + (aNumber > 1 ? 's' : '') + ' ago';
}
if (difference > week) {
ret = aDate.getDate() + ' '
+ months[aDate.getMonth()] + ' '
+ aDate.getFullYear();
} else if (difference > day) {
days = Math.round(difference / day);
if (days <= 1) {
ret = 'Yesterday';
} else {
ret = days + ' days ago';
}
} else if (difference > hour) {
ret = pluralize(Math.round(difference / hour), 'hour');
} else if (difference > minute) {
ret = pluralize(Math.round(difference / minute), 'minute');
} else {
ret = pluralize(Math.ceil(difference / second), 'second');
}
return ret;
};
// Create an object with no properties
exports.nil = function (aObj) {
var nilObj = Object.create(null);
if (!aObj) return nilObj;
exports.forIn(aObj, function (aVal, aKey) {
nilObj[aKey] = aVal;
});
return nilObj;
};
// Safely iterate on an object not create using nil()
exports.forIn = function (aObj, aForProp) {
var key = null;
for (key in aObj) {
if (!Object.prototype.hasOwnProperty.call(aObj, key)) { continue; }
aForProp(aObj[key], key, aObj);
}
};
// Clean filenames but leave them readable
// Based on Greasemonkey modules/remoteScript.js
// NOTE: Keep in sync with client side JavaScript
exports.cleanFilename = function (aFilename, aDefaultName) {
// Blacklist problem characters (slashes, colons, etc.).
var cleanName = (aFilename || '').replace(/[\\\/:*?\'\"<>|#;@=&]/g, '')
// Make whitespace readable.
.replace(/(\s|%20)+/g, '_');
return cleanName || aDefaultName;
};
// Smarter encoder
exports.encode = function (aStr) {
try {
// Check for bad decoding
decodeURIComponent(aStr);
return aStr;
} catch (aE) {
return encodeURIComponent(aStr);
}
};
exports.decode = function (aStr) {
try {
// Check for bad decoding
return decodeURIComponent(aStr);
} catch (aE) {
return aStr;
}
};
exports.limitRange = function (aMin, aX, aMax, aDefault) {
var x = Math.max(Math.min(aX, aMax), aMin);
// ES5 strict similar check to ES6 Number.isNaN()
return (x !== x ? aDefault : x);
};
exports.limitMin = function (aMin, aX) {
return Math.max(aX, aMin);
};
var setUrlQueryValue = function (aBaseUrl, aQueryVarKey, aQueryVarValue) {
var parseQueryString = true;
var u = url.parse(aBaseUrl, parseQueryString);
u.query[aQueryVarKey] = aQueryVarValue;
delete u.search; // https://stackoverflow.com/a/7517673/947742
return url.format(u);
};
exports.setUrlQueryValue = setUrlQueryValue;
exports.updateUrlQueryString = function (aBaseUrl, aDict) {
var url = aBaseUrl;
_.each(aDict, function (aValue, aKey) {
url = setUrlQueryValue(url, aKey, aValue);
});
return url;
};
exports.isFQUrl = function (aString, aOptions) {
var URL = url.parse(aString); // TODO: Convert to non-legacy
var reTrusty = null;
var protocol = URL.protocol;
var username = URL.username; // NOTE: BUG: in current *node*
var password = URL.password; // NOTE: BUG: in current *node*
var hostname = URL.hostname;
var port = URL.port;
var pathname = URL.pathname;
var search = URL.search;
var hash = URL.hash;
var source = encodeURIComponent(aString);
var target = null;
if (!aOptions) {
aOptions = {};
}
reTrusty = aOptions.isSecure ? new RegExp('^https:$') : new RegExp('^https?:$');
if (protocol && reTrusty.test(protocol)) {
if (hostname) {
target = encodeURIComponent(protocol)
+ encodeURIComponent('//')
+ encodeURIComponent(username ? username : '')
+ encodeURIComponent(password ? ':' + password : '')
+ (username || password ? encodeURIComponent('@') : '')
+ encodeURIComponent(hostname)
+ encodeURIComponent(port ? ':' + port : '')
+ encodeURIComponent(pathname)
+ encodeURIComponent(search ? search : '')
+ encodeURIComponent(hash ? hash : '');
return target === source;
}
} else if (aOptions.canMailto && /^mailto:\S+@\S+/.test(aString)) {
return true;
} else if (aOptions.canDataImg && /^data:image\//.test(aString)) {
return true;
}
return false;
};
// Helper function to ensure value is type Integer `number` or `null`
// Please be very careful if this is edited
exports.ensureIntegerOrNull = function (aEnvVar) {
if (typeof aEnvVar !== 'number') {
aEnvVar = parseInt(aEnvVar);
if (aEnvVar !== aEnvVar) { // NOTE: ES6 `Number.isNaN`
aEnvVar = null;
}
} else {
aEnvVar = parseInt(aEnvVar);
}
return aEnvVar;
};
//
exports.port = process.env.PORT || 8080;
exports.securePort = process.env.SECURE_PORT || 8081;
exports.baseOrigin = 'https://openuserjs.org/';
// Absolute pattern and is combined for pro and dev
exports.patternHasSameOrigin =
'(?:https?://openuserjs\.org(?::' + exports.securePort + ')?|http://(?:oujs\.org' +
(isDev ? '|localhost:' + exports.port : '' ) + '))';
// Possible pattern and is split for pro vs. dev
// NOTE: This re is quite sensitive to changes esp. with `|`
// This routine is fuzzy due to the lack of https on short domain
exports.patternMaybeSameOrigin =
(isPro
? '(?:https?:)?(?://openuserjs\.org(?::' + exports.securePort + ')?|//oujs\.org)?'
: '(?:http:)?(?://localhost:' + exports.port + ')?')
exports.isSameOrigin = function (aUrl) {
var url = null;
var sameOrigin = false;
var rIsSameOrigin = new RegExp('^' + exports.patternHasSameOrigin + '$', 'i');
if (aUrl) {
try {
url = new URL(aUrl, exports.baseOrigin);
if (rIsSameOrigin.test(url.origin)) {
sameOrigin = true;
}
} catch (aE) {
}
}
return {
result: sameOrigin,
URL: url
};
}