-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
102 lines (94 loc) · 1.6 KB
/
index.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
exports.encode = function (input) {
// throw for all non-sequence-like inputs
if ((input && typeof input.length !== 'number') || (!input && input !== '')) {
throw new Error('invalid encoder input: ' + input)
}
var base64 = new Buffer(input).toString('base64')
var encoded = ''
for (var i = 0, length = base64.length; i < length; i++) {
encoded += ENCODE_TABLE[base64[i]]
}
return encoded
}
exports.decode = function (input) {
// only accept string input
if (typeof input !== 'string') {
throw new Error('invalid decoder input: ' + input)
}
var base64 = ''
for (var i = 0, length = input.length; i < length; i++) {
base64 += DECODE_TABLE[input[i]]
}
return new Buffer(base64, 'base64')
}
var DECODE_TABLE = {
'-': '=',
'0': 'A',
'1': 'B',
'2': 'C',
'3': 'D',
'4': 'E',
'5': 'F',
'6': 'G',
'7': 'H',
'8': 'I',
'9': 'J',
'A': 'K',
'B': 'L',
'C': 'M',
'D': 'N',
'E': 'O',
'F': 'P',
'G': 'Q',
'H': 'R',
'I': 'S',
'J': 'T',
'K': 'U',
'L': 'V',
'M': 'W',
'N': 'X',
'O': 'Y',
'P': 'Z',
'Q': 'a',
'R': 'b',
'S': 'c',
'T': 'd',
'U': 'e',
'V': 'f',
'W': 'g',
'X': 'h',
'Y': 'i',
'Z': 'j',
'_': 'k',
'a': 'l',
'b': 'm',
'c': 'n',
'd': 'o',
'e': 'p',
'f': 'q',
'g': 'r',
'h': 's',
'i': 't',
'j': 'u',
'k': 'v',
'l': 'w',
'm': 'x',
'n': 'y',
'o': 'z',
'p': '0',
'q': '1',
'r': '2',
's': '3',
't': '4',
'u': '5',
'v': '6',
'w': '7',
'x': '8',
'y': '9',
'z': '+',
'~': '/',
}
var ENCODE_TABLE = {}
for (var c in DECODE_TABLE) {
ENCODE_TABLE[DECODE_TABLE[c]] = c
}