-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
38 lines (32 loc) · 1.02 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
import http from "http";
import https from "https";
const BASE_64_ALPHABET_URL_SAFE = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_';
export const PROTOCOLS = {
http: {
str: 'http',
module: http,
port: 80
},
https: {
str: 'https',
module: https,
port: 443
}
};
export function log(message) {console.log(message);}
export function respond(response, data, code=200, contentType='text/html', headers=undefined) {
response.writeHead(code, headers ? headers : { 'Content-Type': contentType });
response.write(data);
response.end();
}
export function respondCustomHeaders(response, data, headers, code=200) {
respond(response, data, code, '', headers)
}
export function randomString(length, alphabet=BASE_64_ALPHABET_URL_SAFE) {
let outStr = '';
const alphabetSplit = alphabet.split('');
for (let i = 0; i < length; i++) {
outStr += alphabetSplit[Math.floor(Math.random() * alphabetSplit.length)];
}
return outStr;
}