-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcdn-utils.js
198 lines (180 loc) · 5.15 KB
/
cdn-utils.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
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
import { PACKAGE_NAME, PACKAGE_VERSION } from '../env.js';
/**
* Trim leading `-/`, `/` and trailing `/` from CDN operation
*
* @param {String | unknown} [operation]
* @returns {String}
*/
export const normalizeCdnOperation = (operation) => {
if (typeof operation !== 'string' || !operation) {
return '';
}
let str = operation.trim();
if (str.startsWith('-/')) {
str = str.slice(2);
} else if (str.startsWith('/')) {
str = str.slice(1);
}
if (str.endsWith('/')) {
str = str.slice(0, str.length - 1);
}
return str;
};
/**
* Join multiple CDN operations into one string without trailing or leading delimeters
*
* @param {...(String | unknown)} [operations]
* @returns {String}
*/
export const joinCdnOperations = (...operations) => {
return operations
.filter((op) => typeof op === 'string' && op)
.map((op) => normalizeCdnOperation(/** @type {String} */ (op)))
.join('/-/');
};
/**
* Create string with leading `-/` from passed CDN operations. Do the same as `joinCdnOperations` but adds leading `-/`
* and trailing `/`
*
* @param {...(String | unknown)} [cdnOperations] -
* @returns {String}
*/
export const createCdnUrlModifiers = (...cdnOperations) => {
let joined = joinCdnOperations(...cdnOperations);
return joined ? `-/${joined}/` : '';
};
/**
* Extract filename or file URL
*
* @param {String} cdnUrl
* @returns {String}
*/
export function extractFilename(cdnUrl) {
let url = new URL(cdnUrl);
let noOrigin = url.pathname + url.search + url.hash;
let urlFilenameIdx = noOrigin.lastIndexOf('http');
let plainFilenameIdx = noOrigin.lastIndexOf('/');
let filename = '';
if (urlFilenameIdx >= 0) {
filename = noOrigin.slice(urlFilenameIdx);
} else if (plainFilenameIdx >= 0) {
filename = noOrigin.slice(plainFilenameIdx + 1);
}
return filename;
}
/**
* Extract UUID from CDN URL
*
* @param {string} cdnUrl
* @returns {string}
*/
export function extractUuid(cdnUrl) {
let url = new URL(cdnUrl);
let { pathname } = url;
const slashIndex = pathname.indexOf('/');
const secondSlashIndex = pathname.indexOf('/', slashIndex + 1);
return pathname.substring(slashIndex + 1, secondSlashIndex);
}
/**
* Extract UUID from CDN URL
*
* @param {string} cdnUrl
* @returns {string[]}
*/
export function extractOperations(cdnUrl) {
let withoutFilename = trimFilename(cdnUrl);
let url = new URL(withoutFilename);
let operationsMarker = url.pathname.indexOf('/-/');
if (operationsMarker === -1) {
return [];
}
let operationsStr = url.pathname.substring(operationsMarker);
return operationsStr
.split('/-/')
.filter(Boolean)
.map((operation) => normalizeCdnOperation(operation));
}
/**
* Trim filename or file URL
*
* @param {String} cdnUrl
* @returns {String}
*/
export function trimFilename(cdnUrl) {
let url = new URL(cdnUrl);
let filename = extractFilename(cdnUrl);
let filenamePathPart = isFileUrl(filename) ? splitFileUrl(filename).pathname : filename;
url.pathname = url.pathname.replace(filenamePathPart, '');
url.search = '';
url.hash = '';
return url.toString();
}
/**
* Detect if filename is actually file URL
*
* @param {String} filename
* @returns {Boolean}
*/
export function isFileUrl(filename) {
return filename.startsWith('http');
}
/**
* Split file URL into the path and search parts
*
* @param {String} fileUrl
* @returns {{ pathname: String; search: String; hash: String }}
*/
export function splitFileUrl(fileUrl) {
let url = new URL(fileUrl);
return {
pathname: url.origin + url.pathname || '',
search: url.search || '',
hash: url.hash || '',
};
}
/**
* Create a final CDN URL with CDN modifiers and filename
*
* @param {String} baseCdnUrl - Base URL to CDN or Proxy, CDN modifiers and filename accepted
* @param {String} [cdnModifiers] - CDN modifiers to apply, will be appended to `baseCdnUrl` ones
* @param {String} [filename] - Filename for CDN or file URL for Proxy, will override one from `baseCdnUrl`
* @returns {String}
*/
// TODO eadidenko replace arg to pass the object parameter
export const createCdnUrl = (baseCdnUrl, cdnModifiers, filename) => {
let url = new URL(trimFilename(baseCdnUrl));
filename = filename || extractFilename(baseCdnUrl);
// TODO: fix double slash pathname bug (--cfg-cdn-cname: 'https://ucarecdn.com/' - trailing slash case)
if (url.pathname.startsWith('//')) {
url.pathname = url.pathname.replace('//', '/');
}
if (isFileUrl(filename)) {
let splitted = splitFileUrl(filename);
url.pathname = url.pathname + (cdnModifiers || '') + (splitted.pathname || '');
url.search = splitted.search;
url.hash = splitted.hash;
} else {
url.pathname = url.pathname + (cdnModifiers || '') + (filename || '');
}
return url.toString();
};
/**
* Create URL for an original file on CDN
*
* @param {String} cdnUrl - URL to get base domain from, any pathname will be stripped
* @param {String} uuid
* @returns {String}
*/
export const createOriginalUrl = (cdnUrl, uuid) => {
let url = new URL(cdnUrl);
url.pathname = uuid + '/';
return url.toString();
};
export const isValidURL = (url) => {
try {
new URL(url);
return true;
} catch (error) {
return false;
}
};