forked from kuu/node-hls-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
62 lines (54 loc) · 1.26 KB
/
utils.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
const URL = require('url').URL;
function THROW(err) {
throw err;
}
function ASSERT(msg, ...params) {
for (const [index, param] of params.entries()) {
if (!param) {
THROW(new Error(`${msg} : Failed at [${index}]`));
}
}
}
function PARAMCHECK(...params) {
for (const [index, param] of params.entries()) {
if (param === undefined) {
THROW(new Error(`Param Check : Failed at [${index}]`));
}
}
}
function CONDITIONALPARAMCHECK(...params) {
for (const [index, [cond, param]] of params.entries()) {
if (!cond) {
continue;
}
if (param === undefined) {
THROW(new Error(`Conditional Param Check : Failed at [${index}]`));
}
}
}
function _empty() {}
function tryCatch(body, errorHandler) {
try {
return body();
} catch (err) {
return errorHandler(err);
}
}
function createUrl(url, base) {
return tryCatch(
() => {
return new URL(url);
},
() => {
return new URL(url, base);
}
);
}
module.exports = {
THROW,
ASSERT: process.env.NODE_ENV === 'production' ? _empty : ASSERT,
PARAMCHECK: process.env.NODE_ENV === 'production' ? _empty : PARAMCHECK,
CONDITIONALPARAMCHECK: process.env.NODE_ENV === 'production' ? _empty : CONDITIONALPARAMCHECK,
tryCatch,
createUrl
};