-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
136 lines (126 loc) · 4.07 KB
/
index.mjs
File metadata and controls
136 lines (126 loc) · 4.07 KB
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
export const PREFERS_PADDING = 1
export const PREFERS_NO_PADDING = 2
export function make (name, charset, padding, paddingMode) {
if (charset.length !== 64) {
throw new Error(`Charset needs to be 64 characters long! (${charset.length})`)
}
const byCharCode = new Uint8Array(256)
const byNum = new Uint8Array(64)
for (let i = 0; i < 64; i += 1) {
const code = charset.charCodeAt(i)
if (code > 255) {
throw new Error(`Character #${i} in charset [code=${code}, char=${charset.charAt(i)}] is too high! (max=255)`)
}
if (byCharCode[code] !== 0) {
throw new Error(`Character [code=${code}, char=${charset.charAt(i)}] is more than once in the charset!`)
}
byCharCode[code] = i
byNum[i] = code
}
const padCode = padding.charCodeAt(0)
const codec = {
name,
encodingLength (str) {
const strLen = str.length
const len = strLen * 0.75 | 0
if (str.charCodeAt(strLen - 1) === padCode) {
if (str.charCodeAt(strLen - 2) === padCode) {
return len - 2
}
return len - 1
}
return len
},
encode (str, buffer, offset) {
if (buffer === null || buffer === undefined) {
buffer = new Uint8Array(codec.encodingLength(str))
}
if (offset === null || offset === undefined) {
offset = 0
}
let strLen = str.length
if (str.charCodeAt(strLen - 1) === padCode) {
if (str.charCodeAt(strLen - 2) === padCode) {
strLen -= 2
} else {
strLen -= 1
}
}
const padding = strLen % 4
const safeLen = strLen - padding
let off = offset
let i = 0
while (i < safeLen) {
const code =
(byCharCode[str.charCodeAt(i)] << 18) |
(byCharCode[str.charCodeAt(i + 1)] << 12) |
(byCharCode[str.charCodeAt(i + 2)] << 6) |
byCharCode[str.charCodeAt(i + 3)]
buffer[off++] = code >> 16
buffer[off++] = code >> 8
buffer[off++] = code
i += 4
}
if (padding === 3) {
const code =
(byCharCode[str.charCodeAt(i)] << 10) |
(byCharCode[str.charCodeAt(i + 1)] << 4) |
(byCharCode[str.charCodeAt(i + 2)] >> 2)
buffer[off++] = code >> 8
buffer[off++] = code
} else if (padding === 2) {
buffer[off++] = (byCharCode[str.charCodeAt(i)] << 2) |
(byCharCode[str.charCodeAt(i + 1)] >> 4)
}
codec.encode.bytes = off - offset
return buffer
},
decode (buffer, start, end) {
if (start === null || start === undefined) {
start = 0
}
if (end === null || end === undefined) {
end = buffer.length
}
const length = end - start
const pad = length % 3
const safeEnd = start + length - pad
const codes = []
for (let off = start; off < safeEnd; off += 3) {
const num = (buffer[off] << 16) | ((buffer[off + 1] << 8)) | buffer[off + 2]
codes.push(
byNum[num >> 18 & 0x3F],
byNum[num >> 12 & 0x3F],
byNum[num >> 6 & 0x3F],
byNum[num & 0x3F]
)
}
if (pad === 2) {
const num = (buffer[end - 2] << 8) + buffer[end - 1]
codes.push(
byNum[num >> 10],
byNum[(num >> 4) & 0x3F],
byNum[(num << 2) & 0x3F]
)
if (paddingMode === PREFERS_PADDING) {
codes.push(padCode)
}
} else if (pad === 1) {
const num = buffer[end - 1]
codes.push(
byNum[num >> 2],
byNum[(num << 4) & 0x3F]
)
if (paddingMode === PREFERS_PADDING) {
codes.push(padCode, padCode)
}
}
codec.decode.bytes = length
return String.fromCharCode.apply(String, codes)
}
}
return codec
}
export const base64 = make('base64', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', '=', PREFERS_PADDING)
// https://datatracker.ietf.org/doc/html/rfc4648#section-5
export const base64URL = make('base64-url', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', '=', PREFERS_NO_PADDING)