-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
326 lines (257 loc) · 7.12 KB
/
index.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"use strict";
const deepFreeze = require("deep-freeze-node");
const evaluateValue = require("evaluate-value");
const isURL = require("isurl");
const minURL = require("minurl");
const URLRelation = require("url-relation");
const PROTOCOL_RELATIVE = Symbol("protocol_relative");
const ROOT_PATH_RELATIVE = Symbol("root_path_relative");
const PATH_RELATIVE = Symbol("path_relative");
const SHORTEST = Symbol("shortest");
const CAREFUL_PROFILE = { ...minURL.CAREFUL_PROFILE, output:SHORTEST };
const COMMON_PROFILE = { ...minURL.COMMON_PROFILE, output:SHORTEST };
const SEGMENT_PATTERN = /\/|[^\/]+/g;
const relateSegments = (baseSegments, urlSegments) =>
{
let parentIndex = -1;
let related = true;
let slashes = 0;
// Find parents
const relatedSegments = baseSegments.reduce((result, baseSegment, i) =>
{
// Record groups of repeating slashes
if (baseSegment === "/")
{
slashes++;
}
else
{
slashes = 0;
}
if (related)
{
if (urlSegments[i] !== baseSegment)
{
related = false;
}
else
{
// The last related segment
parentIndex = i;
}
}
if (!related)
{
// If a segment, infixed repeating slashes, or a prefixed/leading repeating slash
if (slashes===0 || slashes>1 || i===0)
{
// Up one level
result.push("..", "/");
}
}
return result;
}, []);
// If relation starts at a path beginning with "//"
if (relatedSegments.length===0 && urlSegments[parentIndex+1]==="/")
{
relatedSegments.push(".", "/");
}
// Add children -- starting after last related segment
relatedSegments.push( ...urlSegments.slice(parentIndex+1) );
return relatedSegments;
};
const relateToProtocol = (url, options) => minURL(url, options).slice(url.protocol.length);
const relateToRootPath = (url, options) =>
{
// @todo does this support mailto? unlikely scenario, but complete
const pattern = new RegExp(`^${url.protocol}\/?\/?${url.username}:?${url.password}@?${url.hostname}:?${url.port}`);
const stringifiedURL = minURL(url,
{
clone: false,
plusQueries: false,
removeAuth: false,
removeDefaultPort: false,
removeEmptyHash: false,
removeEmptyQueries: false,
removeEmptyQueryNames: false,
removeEmptyQueryValues: false,
removeEmptySegmentNames: false,
removeHash: false,
removeIndexFilename: false,
removeQueryNames: false,
removeQueryOddities: false,
removeRootTrailingSlash: options.removeRootTrailingSlash,
removeTrailingSlash: options.removeTrailingSlash,
removeWWW: false,
sortQueries: false,
stringify: true
});
// Remove everything before the path
const truncated = stringifiedURL.replace(pattern, "");
if (truncated === "")
{
return "/";
}
else if (truncated.startsWith("//"))
{
// Avoid complication with protocol-relative URLs
return relateToProtocol(url, options);
}
else
{
return truncated;
}
};
const relateURL = (url, base, options) =>
{
if (!isURL.lenient(url) || !isURL.lenient(base))
{
throw new TypeError("Invalid URL");
}
options = { ...COMMON_PROFILE, ...options };
const ignoreAuth = evaluateValue(options.removeAuth, url);
const relation = new URLRelation(url, base,
{
components: ignoreAuth ? [URLRelation.AUTH] : undefined,
defaultPorts: options.defaultPorts,
ignoreDefaultPort: relationOption(options.removeDefaultPort),
ignoreEmptyQueries: relationOption(options.removeEmptyQueries),
ignoreEmptySegmentNames: relationOption(options.removeEmptySegmentNames),
ignoreIndexFilename: relationOption(options.removeIndexFilename),
ignoreQueryNames: relationOption(options.removeQueryNames),
ignoreQueryOrder: relationOption(options.sortQueries),
ignoreWWW: relationOption(options.removeWWW),
indexFilenames: options.indexFilenames,
queryNames: options.queryNames
});
if (relation.upTo(URLRelation.PATH))
{
// Avoid turning a hash-relative URL into ""
options.removeEmptyHash = false;
}
if (!relation.upTo(URLRelation.PROTOCOL))
{
return minURL(url, options);
}
else if (!relation.upTo(URLRelation.HOST) || options.output===PROTOCOL_RELATIVE)
{
return relateToProtocol(url, options);
}
if (options.clone)
{
base = new URL(base);
url = new URL(url);
// Don't let `minURL` clone anything, since it's called multiple times
options.clone = false;
}
// Stringify'ing would require a reparse
const noStringify = { ...options, stringify:false };
url = minURL(url, noStringify);
if (!options.stringify)
{
// This isn't recommended, but still possible
return url;
}
else if (options.output === ROOT_PATH_RELATIVE)
{
return relateToRootPath(url, options);
}
// NOTE :: https://github.com/whatwg/url/issues/221
const urlHash = url.href.endsWith("#") ? "#" : url.hash;
if (relation.upTo(URLRelation.SEARCH))
{
return urlHash;
}
else if (relation.upTo(URLRelation.FILENAME))
{
// @todo this is the second time this will be ran -- first time within `minURL()`
if (!evaluateValue(options.sortQueries, url))
{
base = minURL(base, noStringify);
// Avoid similar queries minifying to the same, but not truncating because
// they were seen as unrelated due to a shallow scan
if (url.search === base.search)
{
return urlHash;
}
}
if (url.search === "")
{
return "." + urlHash;
}
return url.search + urlHash;
}
const baseSegments = splitPathname(base.pathname);
const urlSegments = splitPathname(url.pathname);
let baseFilename = baseSegments[baseSegments.length - 1];
let urlFilename = urlSegments[urlSegments.length - 1];
if (urlFilename===undefined || urlFilename==="/")
{
urlFilename = "";
}
else
{
// Remove filename
urlSegments.pop();
}
if (baseFilename===undefined || baseFilename==="/")
{
baseFilename = "";
}
else
{
// Remove filename
baseSegments.pop();
}
if (relation.upTo(URLRelation.SEGMENTS))
{
if (urlFilename==="" && baseFilename!=="")
{
urlFilename = ".";
}
return urlFilename + url.search + urlHash;
}
const relatedSegments = relateSegments(baseSegments, urlSegments);
if (urlFilename==="" && relatedSegments[relatedSegments.length-2]==="..")
{
relatedSegments.pop();
}
const pathRelative = relatedSegments.join("") + urlFilename + url.search + urlHash;
if (options.output === PATH_RELATIVE)
{
return pathRelative;
}
const rootPathRelative = relateToRootPath(url, options);
// Return shortest -- if same, root-path/protocol-relative takes priority as it's more direct
return rootPathRelative.length <= pathRelative.length ? rootPathRelative : pathRelative;
};
const relationOption = option =>
{
return (url1, url2) => evaluateValue(option,url1) && evaluateValue(option,url2);
};
const splitPathname = pathname =>
{
// Remove leading slash, which will always exist
pathname = pathname.substr(1);
// Split by and include slashes
// Simply splitting produced issues with trailing "//" and joining
let output = pathname.match(SEGMENT_PATTERN);
if (output === null)
{
output = [];
}
return output;
};
Object.assign
(
relateURL,
{
CAREFUL_PROFILE,
COMMON_PROFILE,
PROTOCOL_RELATIVE,
ROOT_PATH_RELATIVE,
PATH_RELATIVE,
SHORTEST
}
);
module.exports = deepFreeze(relateURL);