-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
186 lines (158 loc) · 6.23 KB
/
mod.ts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* ------------------------------------------------------------------------------------
@cross/base64 - MIT License - Hexagon <[email protected]>
------------------------------------------------------------------------------------
License:
Copyright (c) 2024 Hexagon <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
------------------------------------------------------------------------------------ */
/** A string containing standard base64 characters */
const chars: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/** A string containing base64url characters */
const charsUrl: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
/**
* Function type for generating a lookup table
* @param target The target string of characters
* @returns A Uint8Array lookup table
*/
const genLookup: (target: string) => Uint8Array = (target: string) => {
const lookupTemp = new Uint8Array(256);
for (let i = 0; i < target.length; i++) {
lookupTemp[target.charCodeAt(i)] = i;
}
return lookupTemp;
};
/** Lookup table for standard base64 characters */
const lookup: Uint8Array = genLookup(chars);
/** Lookup table for base64url characters */
const lookupUrl: Uint8Array = genLookup(charsUrl);
// Regular Expressions
const base64UrlPattern = /^[-A-Za-z0-9\-_]*$/;
const base64Pattern = /^[-A-Za-z0-9+/]*={0,3}$/;
/**
* Converts a base64 encoded string to an ArrayBuffer
* @param data Base64 encoded string
* @param urlMode If true, expects a base64url encoded string
* @returns The decoded data as an ArrayBuffer.
*/
export const toArrayBuffer = (
data: string,
urlMode: boolean = false,
): ArrayBuffer => {
const len = data.length;
let bufferLength = data.length * 0.75;
let i, p = 0;
let encoded1, encoded2, encoded3, encoded4;
if (data[data.length - 1] === "=") {
bufferLength--;
if (data[data.length - 2] === "=") {
bufferLength--;
}
}
const arraybuffer = new ArrayBuffer(bufferLength);
const bytes = new Uint8Array(arraybuffer);
const target = urlMode ? lookupUrl : lookup;
for (i = 0; i < len; i += 4) {
encoded1 = target[data.charCodeAt(i)];
encoded2 = target[data.charCodeAt(i + 1)];
encoded3 = target[data.charCodeAt(i + 2)];
encoded4 = target[data.charCodeAt(i + 3)];
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
}
return arraybuffer;
};
/**
* Decodes a base64 or base64url encoded ArrayBuffer into a regular string.
*
* @param arrBuf - The ArrayBuffer containing the encoded data.
* @param urlMode - (Optional) Determines decoding mode:
* * `true`: Expects base64url encoding.
* * `false` (Default): Expects standard base64 encoding.
* @returns The decoded string representation of the input data.
*/
export const fromArrayBuffer = (
arrBuf: ArrayBuffer,
urlMode: boolean = false,
): string => {
const bytes = new Uint8Array(arrBuf);
let i,
result = "";
const len = bytes.length,
target = urlMode ? charsUrl : chars;
for (i = 0; i < len; i += 3) {
result += target[bytes[i] >> 2];
result += target[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
result += target[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
result += target[bytes[i + 2] & 63];
}
const remainder = len % 3;
if (remainder === 2) {
result = result.substring(0, result.length - 1) + (urlMode ? "" : "=");
} else if (remainder === 1) {
result = result.substring(0, result.length - 2) + (urlMode ? "" : "==");
}
return result;
};
/**
* Converts a base64 or base64url encoded string into a decoded text string.
*
* @param str - The base64/base64url encoded string to decode.
* @param urlMode - (Optional) Determines decoding mode:
* * `true`: Expects base64url encoding.
* * `false` (Default): Expects standard base64 encoding.
* @returns The decoded text string.
*/
export const toString = (str: string, urlMode: boolean = false): string => {
return new TextDecoder().decode(toArrayBuffer(str, urlMode));
};
/**
* Encodes a regular text string into a base64 or base64url representation.
*
* @param str - The text string to encode.
* @param urlMode - (Optional) Determines encoding mode:
* * `true`: Produces base64url encoded output.
* * `false` (Default): Produces standard base64 encoded output.
* @returns The base64/base64url encoded string.
*/
export const fromString = (str: string, urlMode: boolean = false): string => {
return fromArrayBuffer(new TextEncoder().encode(str), urlMode);
};
/**
* Checks whether a given string is valid base64 or base64url encoded data.
*
* @param encoded - The string to validate.
* @param urlMode - (Optional) Determines validation mode:
* * `true`: Validates against base64url format.
* * `false` (Default): Validates against standard base64 format.
* @returns `true` if the string is valid base64/base64url, `false` otherwise.
*/
export const validate = (
encoded: string,
urlMode: boolean = false,
): boolean => {
// Bail out if not string
if (!(typeof encoded === "string")) {
return false;
}
// Go on validate
try {
return urlMode ? base64UrlPattern.test(encoded) : base64Pattern.test(encoded);
} catch (_e) {
return false;
}
};