Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ultraviolet
Path: blob/main/src/rewrite/codecs.js
304 views
1
// -------------------------------------------------------------
2
// WARNING: this file is used by both the client and the server.
3
// Do not use any browser or node-specific API!
4
// -------------------------------------------------------------
5
export const none = {
6
encode: (str) => str,
7
decode: (str) => str,
8
};
9
10
export const plain = {
11
encode(str) {
12
if (!str) return str;
13
return encodeURIComponent(str);
14
},
15
decode(str) {
16
if (!str) return str;
17
return decodeURIComponent(str);
18
},
19
};
20
21
export const xor = {
22
encode(str) {
23
if (!str) return str;
24
let result = "";
25
let len = str.length;
26
for (let i = 0; i < len; i++) {
27
const char = str[i];
28
result += i % 2 ? String.fromCharCode(char.charCodeAt(0) ^ 2) : char;
29
}
30
return encodeURIComponent(result);
31
},
32
decode(str) {
33
if (!str) return str;
34
str = decodeURIComponent(str);
35
let result = "";
36
let len = str.length;
37
for (let i = 0; i < len; i++) {
38
const char = str[i];
39
result += i % 2 ? String.fromCharCode(char.charCodeAt(0) ^ 2) : char;
40
}
41
return result;
42
}
43
};
44
45
export const base64 = {
46
encode(str) {
47
if (!str) return str;
48
str = str.toString();
49
50
return btoa(encodeURIComponent(str));
51
},
52
decode(str) {
53
if (!str) return str;
54
str = str.toString();
55
56
return decodeURIComponent(atob(str));
57
},
58
};
59
60