-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathencoder.ts
More file actions
280 lines (263 loc) · 7.54 KB
/
encoder.ts
File metadata and controls
280 lines (263 loc) · 7.54 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
const defaultBase =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
/**
* Encodes and decodes UUIDs, ObjectIds,
* as well as simple numbers and bigints to and from base64.
*/
export class Encoder {
/**
* The the default set of characters for base64
*/
base: string;
/**
* The lookup table for converting base64 to hex
*/
baseToHex = new Map<string, string>();
/**
* The lookup table for converting hex to base64
*/
hexToBase = new Map<string, string>();
static initialize(
base: string,
baseToHex: Map<string, string>,
hexToBase: Map<string, string>,
): void {
for (let i = 0; i < 4096; i += 1) {
const hex = i.toString(16).padStart(3, "0");
const base64 = base[i >> 6] + base[i & 63];
hexToBase.set(hex, base64);
baseToHex.set(base64, hex);
}
}
/**
* @param base the set of characters to be used as a base for conversions
* @param noLookup whether to skip creation of the lookup table for hex encoding
*/
constructor(base: string = defaultBase, noLookup?: boolean) {
this.base = base;
if (!noLookup) {
(this.constructor as typeof Encoder).initialize(
this.base,
this.baseToHex,
this.hexToBase,
);
}
}
/**
* Encodes a given ObjectId hex string into a base64 string.
*
* @param objectId the ObjectId hex string to encode
* @returns encoded base64 string
*/
fromObjectId(objectId: string): string {
const { hexToBase } = this;
return (
hexToBase.get(objectId.slice(0, 3))! +
hexToBase.get(objectId.slice(3, 6)) +
hexToBase.get(objectId.slice(6, 9)) +
hexToBase.get(objectId.slice(9, 12)) +
hexToBase.get(objectId.slice(12, 15)) +
hexToBase.get(objectId.slice(15, 18)) +
hexToBase.get(objectId.slice(18, 21)) +
hexToBase.get(objectId.slice(21, 24))
);
}
/**
* Encodes a given ObjectId into a base64 string.
*
* @param id the ObjectId
* @returns encoded base64 string
*/
fromBinObjectId(id: Uint8Array): string {
const { base } = this;
let encoded = "";
let i = -1;
while (++i < 12) {
encoded = encoded + base[id[i] >> 2] +
base[((id[i] & 0x03) << 4) | (id[++i] >> 4)] +
base[((id[i] & 0x0f) << 2) | (id[++i] >> 6)] +
base[id[i] & 0x3f];
}
return encoded;
}
/**
* Decodes a given base64 string into an ObjectId hex string.
*
* @param id the base64 string to decode
* @returns decoded ObjectId hex string
*/
toObjectId(id: string): string {
const { baseToHex } = this;
return (
baseToHex.get(id.slice(0, 2))! +
baseToHex.get(id.slice(2, 4)) +
baseToHex.get(id.slice(4, 6)) +
baseToHex.get(id.slice(6, 8)) +
baseToHex.get(id.slice(8, 10)) +
baseToHex.get(id.slice(10, 12)) +
baseToHex.get(id.slice(12, 14)) +
baseToHex.get(id.slice(14, 16))
);
}
/**
* Decodes a given base64 string into an ObjectId.
*
* @param id the base64 string to decode
* @param view the view to store decoded bytes into
* @returns decoded ObjectId
*/
toBinObjectId(id: string, view = new Uint8Array(12)): Uint8Array {
const { base } = this;
let code = 0;
let i = -1;
let j = -1;
while (++i < 12) {
view[i] = (base.indexOf(id[++j]) << 2) |
(code = base.indexOf(id[++j]), code >> 4);
view[++i] = (code << 4) | (code = base.indexOf(id[++j]), code >> 2);
view[++i] = (code << 6) | base.indexOf(id[++j]);
}
return view;
}
/**
* Encodes a given UUID hex string into a base64 string.
*
* @param uuid the UUID hex string to encode
* @returns encoded base64 string
*/
fromUUID(uuid: string): string {
const { hexToBase } = this;
return (
hexToBase.get(uuid.slice(0, 3))! +
hexToBase.get(uuid.slice(3, 6)) +
hexToBase.get(uuid.slice(6, 8) + uuid.slice(9, 10)) +
hexToBase.get(uuid.slice(10, 13)) +
hexToBase.get(uuid.slice(14, 17)) +
hexToBase.get(uuid.slice(17, 18) + uuid.slice(19, 21)) +
hexToBase.get(uuid.slice(21, 23) + uuid.slice(24, 25)) +
hexToBase.get(uuid.slice(25, 28)) +
hexToBase.get(uuid.slice(28, 31)) +
hexToBase.get(uuid.slice(31, 34)) +
hexToBase.get("0" + uuid.slice(34, 36))
);
}
/**
* Encodes a given UUID into a base64 string.
*
* @param id the UUID
* @returns encoded base64 string
*/
fromBinUUID(id: Uint8Array): string {
const { base } = this;
let encoded = "";
let i = -1;
while (++i < 15) {
encoded = encoded + base[id[i] >> 2] +
base[((id[i] & 0x03) << 4) | (id[++i] >> 4)] +
base[((id[i] & 0x0f) << 2) | (id[++i] >> 6)] +
base[id[i] & 0x3f];
}
return encoded + base[id[15] >> 6] +
base[id[15] & 0x3f];
}
/**
* Decodes a given base64 string into a UUID hex string.
*
* @param id the base64 string to decode
* @returns decoded UUID hex string
*/
toUUID(id: string): string {
const { baseToHex } = this;
let slice = "";
return (
baseToHex.get(id.slice(0, 2))! +
baseToHex.get(id.slice(2, 4)) +
(slice = baseToHex.get(id.slice(4, 6))!,
slice.slice(0, 2) + "-" + slice.slice(2)) +
baseToHex.get(id.slice(6, 8)) +
"-" +
baseToHex.get(id.slice(8, 10)) +
(slice = baseToHex.get(id.slice(10, 12))!,
slice.slice(0, 1) + "-" + slice.slice(1)) +
(slice = baseToHex.get(id.slice(12, 14))!,
slice.slice(0, 2) + "-" + slice.slice(2)) +
baseToHex.get(id.slice(14, 16)) +
baseToHex.get(id.slice(16, 18)) +
baseToHex.get(id.slice(18, 20)) +
baseToHex.get(id.slice(20, 22))!.slice(1)
);
}
/**
* Decodes a given base64 string into a UUID.
*
* @param id the base64 string to decode
* @param view the view to store decoded bytes into
* @returns decoded UUID
*/
toBinUUID(id: string, view = new Uint8Array(16)): Uint8Array {
const { base } = this;
let code = 0;
let i = -1;
let j = -1;
while (++i < 15) {
view[i] = (base.indexOf(id[++j]) << 2) |
(code = base.indexOf(id[++j]), code >> 4);
view[++i] = (code << 4) | (code = base.indexOf(id[++j]), code >> 2);
view[++i] = (code << 6) | base.indexOf(id[++j]);
}
view[15] = base.indexOf(id[20]) << 6 | base.indexOf(id[21]);
return view;
}
/**
* Encodes a positive number into a base64 string.
*
* @param integer the number to encode
* @returns encoded base64 string
*/
fromInt(integer: number): string {
let encoded = "";
for (let n = integer; n > 0; n >>= 6) {
encoded = this.base[n & 63] + encoded;
}
return encoded;
}
/**
* Decodes a given base64 string into a number.
*
* @param id the base64 string to decode
* @returns decoded number
*/
toInt(id: string): number {
let n = 0;
for (let i = 0; i < id.length; i++) {
n = (n << 6) + this.base.indexOf(id[i]);
}
return n;
}
/**
* Encodes a positive bigint into a base64 string.
*
* @param integer the bigint to encode
* @returns encoded base64 string
*/
fromBigInt(integer: bigint): string {
let encoded = "";
for (let n = integer; n > 0n; n >>= 6n) {
encoded = this.base[Number(n & 63n)] + encoded;
}
return encoded;
}
/**
* Decodes a given base64 string into a bigint.
*
* @param id the base64 string to decode
* @returns decoded bigint
*/
toBigInt(id: string): bigint {
let n = 0n;
for (let i = 0; i < id.length; i++) {
n = (n << 6n) + BigInt(this.base.indexOf(id[i]));
}
return n;
}
}