2019-06-20 19:37:28 +07:00
|
|
|
import isURLValidator from 'validator/lib/isURL';
|
|
|
|
import isEmailValidator from 'validator/lib/isEmail';
|
|
|
|
import '../../types';
|
|
|
|
|
2019-07-13 13:53:23 +07:00
|
|
|
export function isURL(url: string): boolean {
|
2019-06-20 19:37:28 +07:00
|
|
|
return isURLValidator(url || '', {
|
|
|
|
protocols: ['http', 'https', 'git+https'],
|
|
|
|
require_protocol: true,
|
2019-07-11 05:51:25 +07:00
|
|
|
require_tld: false,
|
2019-06-20 19:37:28 +07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-11 03:20:05 +07:00
|
|
|
export function isEmail(email: string): boolean {
|
2019-06-20 19:37:28 +07:00
|
|
|
return isEmailValidator(email || '');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getRegistryURL(): string {
|
2019-11-25 02:17:28 +07:00
|
|
|
// Don't add slash if it's not a sub directory
|
|
|
|
return `${location.origin}${location.pathname === '/' ? '' : location.pathname}`;
|
2019-06-20 19:37:28 +07:00
|
|
|
}
|
2019-07-28 19:12:18 +07:00
|
|
|
|
|
|
|
export function extractFileName(url: string): string {
|
|
|
|
return url.substring(url.lastIndexOf('/') + 1);
|
|
|
|
}
|
|
|
|
|
2019-10-03 17:47:30 +07:00
|
|
|
function blobToFile(blob: Blob, fileName: string): File {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const b: any = blob;
|
|
|
|
b.lastModified = Date.now();
|
|
|
|
b.name = fileName;
|
|
|
|
return b as File;
|
|
|
|
}
|
|
|
|
|
2019-07-28 19:12:18 +07:00
|
|
|
export function downloadFile(fileStream: Blob, fileName: string): void {
|
2019-10-03 17:47:30 +07:00
|
|
|
let file: File;
|
|
|
|
// File constructor is not supported by Edge
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/File#Browser_compatibility
|
2019-10-18 17:36:17 +07:00
|
|
|
// @ts-ignore. Please see: https://github.com/microsoft/TypeScript/issues/33792
|
2019-10-03 17:47:30 +07:00
|
|
|
if (navigator.msSaveBlob) {
|
|
|
|
// Detect if Edge
|
|
|
|
file = blobToFile(new Blob([fileStream], { type: 'application/octet-stream' }), fileName);
|
|
|
|
} else {
|
|
|
|
file = new File([fileStream], fileName, { type: 'application/octet-stream', lastModified: Date.now() });
|
|
|
|
}
|
|
|
|
|
2019-07-28 19:12:18 +07:00
|
|
|
const objectURL = URL.createObjectURL(file);
|
|
|
|
const fileLink = document.createElement('a');
|
|
|
|
fileLink.href = objectURL;
|
|
|
|
fileLink.download = fileName;
|
2019-10-03 17:47:30 +07:00
|
|
|
|
|
|
|
// Without appending to an HTML Element, download dialog does not show up on Firefox
|
|
|
|
// https://github.com/verdaccio/ui/issues/119
|
|
|
|
document.documentElement.appendChild(fileLink);
|
2019-07-28 19:12:18 +07:00
|
|
|
fileLink.click();
|
|
|
|
// firefox requires remove the object url
|
|
|
|
setTimeout(() => {
|
|
|
|
URL.revokeObjectURL(objectURL);
|
2019-10-03 17:47:30 +07:00
|
|
|
document.documentElement.removeChild(fileLink);
|
2019-07-28 19:12:18 +07:00
|
|
|
}, 150);
|
|
|
|
}
|