-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnormalizeUrl.ts
More file actions
47 lines (39 loc) · 1.17 KB
/
normalizeUrl.ts
File metadata and controls
47 lines (39 loc) · 1.17 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
import type { URLImageInfo } from "@imgproxy/imgproxy-js-core";
import type { IRawUrl } from "../types";
import bufferToBase64 from "./bufferToBase64.js";
import getEncryptPair from "./getEncryptPair.js";
import getEncryptedUrl from "./getEncryptedUrl.js";
interface INormalizeUrl {
url: string | IRawUrl;
encryptKey?: string;
encryptIV?: string;
}
const normalizeUrl = ({
url,
encryptKey,
encryptIV,
}: INormalizeUrl): URLImageInfo => {
const changedUrl = {
value: typeof url === "string" ? url : url.value,
type: (typeof url === "string" ? "base64" : url.displayAs) || "base64",
};
//encoded url to base64
if (changedUrl.type === "base64") {
changedUrl.value = bufferToBase64(Buffer.from(changedUrl.value));
}
//encrypting url
if (changedUrl.type === "encrypted") {
if (!encryptKey) {
throw new Error(
"You should provide encryptKey if you want to use encrypted url type"
);
}
const encKey = getEncryptPair(encryptKey, encryptIV);
if (encKey) {
changedUrl.value = getEncryptedUrl(changedUrl.value, encKey);
changedUrl.type = "encrypted";
}
}
return changedUrl;
};
export default normalizeUrl;