Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 3x 3x 3x 3x 3x 37x 37x 1x 36x 36x 36x 3x 37x 3x 42x 17x 3x 23x 6x 17x 17x 17x 3x 17x 3x 5x 3x 14x 3x 7x 3x 3x 3x 17x 17x 4x 17x 17x 17x 3x 17x 3x 19x 19x 2x 17x 17x 3x 14x 14x 14x 14x 14x 14x 14x 6x 14x 3x 6x 1x 5x 4x 5x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x | /* eslint-disable no-useless-escape */
import { homedir } from "os";
import { URL } from "url";
import * as URI from "urijs";
import * as fileUri from "file-url";
const RELATIVE_PROTOCOL = "relative:///";
/**
* Remove extra / in the url path.
* @param url
*/
function cleanUrlPath(url: URL): URL {
const pathname = url.pathname
.replace(/\\/g, "/") // Replace \ => /
.replace(/\/\//g, "/"); // Replace // => /;
// Means the url is a windows absolute path without the file:// protocol(drive letter gets parsed as the protocol.)
if (url.protocol.length === 2) {
return new URL(`${url.protocol}${pathname}`);
}
const newUrl = new URL(url.toString());
newUrl.pathname = pathname;
return newUrl;
}
/**
* Simplify the given uri by resolving relative paths.
*
* @param uri
* @returns fully qualified url with protocol. Defaults to null:// if not provided
*/
export function simplifyUri(uri: string): string {
return cleanUrlPath(new URL(uri, RELATIVE_PROTOCOL)).toString();
}
export function isUri(uri: string): boolean {
return /^([a-z0-9+.-]+):(?:\/\/(?:((?:[a-z0-9-._~!$&'()*+,;=:]|%[0-9A-F]{2})*)@)?((?:[a-z0-9-._~!$&'()*+,;=]|%[0-9A-F]{2})*)(?::(\d*))?(\/(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9A-F]{2})*)?|(\/?(?:[a-z0-9-._~!$&'()*+,;=:@]|%[0-9A-F]{2})+(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9A-F]{2})*)?)(?:\?((?:[a-z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*))?(?:#((?:[a-z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*))?$/i.test(
uri,
);
}
/**
* remake of path.isAbsolute... because it's platform dependent:
* Windows: C:\\... -> true /... -> true
* Linux: C:\\... -> false /... -> true
*/
function isAbsolute(path: string): boolean {
return !!path.match(/^([a-zA-Z]:)?(\/|\\)/);
}
/**
* determines what an absolute URI is for our purposes, consider:
* - we had Ruby try to use "Azure::ARM::SQL" as a file name, so that should not be considered absolute
* - we want simple, easily predictable semantics
*/
function isUriAbsolute(url: string): boolean {
return /^[a-z]+:\/\//.test(url);
}
/**
* Create a 'file:///' URI from given absolute path.
* Examples:
* - "C:\swagger\storage.yaml" -> "file:///C:/swagger/storage.yaml"
* - "/input/swagger.yaml" -> "file:///input/swagger.yaml"
*/
export function createFileOrFolderUri(absolutePath: string): string {
if (!isAbsolute(absolutePath)) {
throw new Error(`Can only create file URIs from absolute paths. Got '${absolutePath}'`);
}
let result = fileUri(absolutePath, { resolve: false });
// handle UNCs
Iif (absolutePath.startsWith("//") || absolutePath.startsWith("\\\\")) {
result = result.replace(/^file:\/\/\/\//, "file://");
}
return result;
}
export function ensureIfFileUri(uri: string): string {
return uri.replace(/\/$/g, "");
}
export function ensureIsFolderUri(uri: string): string {
return ensureIfFileUri(uri) + "/";
}
export function createFileUri(absolutePath: string): string {
return ensureIfFileUri(createFileOrFolderUri(absolutePath));
}
export function createFolderUri(absolutePath: string): string {
return ensureIsFolderUri(createFileOrFolderUri(absolutePath));
}
export function getFilename(uri: string): string {
return uri.split("/").reverse()[0].split("\\").reverse()[0];
}
export function getFilenameWithoutExtension(uri: string): string {
const lastPart = getFilename(uri);
const ext = lastPart.indexOf(".") === -1 ? "" : lastPart.split(".").reverse()[0];
return lastPart.substr(0, lastPart.length - ext.length - 1);
}
export function toRawDataUrl(uri: string): string {
uri = simplifyUri(uri);
// special URI handlers (the 'if's shouldn't be necessary but provide some additional isolation in case there is anything wrong with one of the regexes)
// - GitHub repo
if (uri.startsWith("https://github.com")) {
uri = uri.replace(
/^https?:\/\/(github.com)(\/[^\/]+\/[^\/]+\/)(blob|tree)\/(.*)$/gi,
"https://raw.githubusercontent.com$2$4",
);
}
// - GitHub gist
Iif (uri.startsWith("gist://")) {
uri = uri.replace(/^gist:\/\/([^\/]+\/[^\/]+)$/gi, "https://gist.githubusercontent.com/$1/raw/");
}
Iif (uri.startsWith("https://gist.github.com")) {
uri = uri.replace(/^https?:\/\/gist.github.com\/([^\/]+\/[^\/]+)$/gi, "https://gist.githubusercontent.com/$1/raw/");
}
if (uri.startsWith(RELATIVE_PROTOCOL)) {
uri = uri.substr(RELATIVE_PROTOCOL.length);
}
return uri;
}
/**
* The singularity of all resolving.
* With URI as our one data type of truth, this method maps an absolute or relative path or URI to a URI using given base URI.
* @param baseUri Absolute base URI
* @param pathOrUri Relative/absolute path/URI
* @returns Absolute URI
*/
export function resolveUri(baseUri: string, pathOrUri: string): string {
Iif (pathOrUri.startsWith("~/")) {
pathOrUri = pathOrUri.replace(/^~/, homedir());
}
if (isAbsolute(pathOrUri)) {
return createFileOrFolderUri(pathOrUri);
}
// known here: `pathOrUri` is eiher URI (relative or absolute) or relative path - which we can normalize to a relative URI
pathOrUri = pathOrUri.replace(/\\/g, "/");
// known here: `pathOrUri` is a URI (relative or absolute)
if (isUriAbsolute(pathOrUri)) {
return pathOrUri;
}
// known here: `pathOrUri` is a relative URI
Iif (!baseUri) {
throw new Error("'pathOrUri' was detected to be relative so 'baseUri' is required");
}
try {
const base = new URI(baseUri);
const relative = new URI(pathOrUri);
Iif (baseUri.startsWith("untitled:///") && pathOrUri.startsWith("untitled:")) {
return pathOrUri;
}
const result = relative.absoluteTo(base);
// GitHub simple token forwarding, for when you pass a URI to a private repo file with `?token=` query parameter.
// this may be easier for quick testing than getting and passing an OAuth token.
if (
base.protocol() === "https" &&
base.hostname() === "raw.githubusercontent.com" &&
result.protocol() === "https" &&
result.hostname() === "raw.githubusercontent.com"
) {
result.query(base.query());
}
return simplifyUri(result.toString());
} catch (e) {
throw new Error(`Failed resolving '${pathOrUri}' against '${baseUri}'.`);
}
}
export function parentFolderUri(uri: string): string | null {
// root?
if (uri.endsWith("//")) {
return null;
}
// folder? => cut away last "/"
if (uri.endsWith("/")) {
uri = uri.slice(0, uri.length - 1);
}
// cut away last component
const compLen = uri.split("/").reverse()[0].length;
return uri.slice(0, uri.length - compLen);
}
export function makeRelativeUri(baseUri: string, absoluteUri: string): string {
return new URI(absoluteUri).relativeTo(baseUri).toString();
}
//------------------------------------------
// Legacy names, will be removed in next major version
//------------------------------------------
/**
* @deprecated use isUri instead.
*/
export const IsUri = isUri;
/**
* @deprecated use createFileOrFolderUri instead.
*/
export const CreateFileOrFolderUri = createFileOrFolderUri;
/**
* @deprecated use ensureIfFileUri instead.
*/
export const EnsureIfFileUri = ensureIfFileUri;
/**
* @deprecated use ensureIsFolderUri instead.
*/
export const EnsureIsFolderUri = ensureIsFolderUri;
/**
* @deprecated use createFileUri instead.
*/
export const CreateFileUri = createFileUri;
/**
* @deprecated use createFolderUri instead.
*/
export const CreateFolderUri = createFolderUri;
/**
* @deprecated use toRawDataUrl instead.
*/
export const ToRawDataUrl = toRawDataUrl;
/**
* @deprecated use getFilename instead.
*/
export const GetFilename = getFilename;
/**
* @deprecated use getFilenameWithoutExtension instead.
*/
export const GetFilenameWithoutExtension = getFilenameWithoutExtension;
/**
* @deprecated use resolveUri instead.
*/
export const ResolveUri = resolveUri;
/**
* @deprecated use parentFolderUri instead.
*/
export const ParentFolderUri = parentFolderUri;
/**
* @deprecated use makeRelativeUri instead.
*/
export const MakeRelativeUri = makeRelativeUri;
|