123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- const schemeRegex = /^[\w+.-]+:\/\//;
- const urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?/;
- function isAbsoluteUrl(input) {
- return schemeRegex.test(input);
- }
- function isSchemeRelativeUrl(input) {
- return input.startsWith('//');
- }
- function isAbsolutePath(input) {
- return input.startsWith('/');
- }
- function parseAbsoluteUrl(input) {
- const match = urlRegex.exec(input);
- return {
- scheme: match[1],
- user: match[2] || '',
- host: match[3],
- port: match[4] || '',
- path: match[5] || '/',
- relativePath: false,
- };
- }
- function parseUrl(input) {
- if (isSchemeRelativeUrl(input)) {
- const url = parseAbsoluteUrl('http:' + input);
- url.scheme = '';
- return url;
- }
- if (isAbsolutePath(input)) {
- const url = parseAbsoluteUrl('http://foo.com' + input);
- url.scheme = '';
- url.host = '';
- return url;
- }
- if (!isAbsoluteUrl(input)) {
- const url = parseAbsoluteUrl('http://foo.com/' + input);
- url.scheme = '';
- url.host = '';
- url.relativePath = true;
- return url;
- }
- return parseAbsoluteUrl(input);
- }
- function stripPathFilename(path) {
-
-
- if (path.endsWith('/..'))
- return path;
- const index = path.lastIndexOf('/');
- return path.slice(0, index + 1);
- }
- function mergePaths(url, base) {
-
- if (!url.relativePath)
- return;
- normalizePath(base);
-
-
- if (url.path === '/') {
- url.path = base.path;
- }
- else {
-
- url.path = stripPathFilename(base.path) + url.path;
- }
-
- url.relativePath = base.relativePath;
- }
- function normalizePath(url) {
- const { relativePath } = url;
- const pieces = url.path.split('/');
-
-
- let pointer = 1;
-
-
- let positive = 0;
-
-
-
- let addTrailingSlash = false;
- for (let i = 1; i < pieces.length; i++) {
- const piece = pieces[i];
-
- if (!piece) {
- addTrailingSlash = true;
- continue;
- }
-
- addTrailingSlash = false;
-
- if (piece === '.')
- continue;
-
-
- if (piece === '..') {
- if (positive) {
- addTrailingSlash = true;
- positive--;
- pointer--;
- }
- else if (relativePath) {
-
-
- pieces[pointer++] = piece;
- }
- continue;
- }
-
-
- pieces[pointer++] = piece;
- positive++;
- }
- let path = '';
- for (let i = 1; i < pointer; i++) {
- path += '/' + pieces[i];
- }
- if (!path || (addTrailingSlash && !path.endsWith('/..'))) {
- path += '/';
- }
- url.path = path;
- }
- function resolve(input, base) {
- if (!input && !base)
- return '';
- const url = parseUrl(input);
-
- if (base && !url.scheme) {
- const baseUrl = parseUrl(base);
- url.scheme = baseUrl.scheme;
-
- if (!url.host || baseUrl.scheme === 'file:') {
-
- url.user = baseUrl.user;
- url.host = baseUrl.host;
- url.port = baseUrl.port;
- }
- mergePaths(url, baseUrl);
- }
- normalizePath(url);
-
- if (url.relativePath) {
-
- const path = url.path.slice(1);
- if (!path)
- return '.';
-
-
-
- const keepRelative = (base || input).startsWith('.');
- return !keepRelative || path.startsWith('.') ? path : './' + path;
- }
-
- if (!url.scheme && !url.host)
- return url.path;
-
- return `${url.scheme}//${url.user}${url.host}${url.port}${url.path}`;
- }
- export { resolve as default };
|