UNPKG

@jbrowse/core

Version:

JBrowse 2 core libraries used by plugins

104 lines (103 loc) 4.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RemoteFileWithRangeCache = void 0; exports.resolveUriLocation = resolveUriLocation; exports.openLocation = openLocation; exports.getFetcher = getFetcher; const detect_node_1 = __importDefault(require("detect-node")); const generic_filehandle2_1 = require("generic-filehandle2"); const RemoteFileWithRangeCache_1 = require("./RemoteFileWithRangeCache"); const __1 = require("../"); const tracks_1 = require("../tracks"); const types_1 = require("../types"); function isLocalPathLocation(location) { return 'localPath' in location; } function isBlobLocation(location) { return 'blobId' in location; } function resolveUriLocation(location) { return location.baseUri ? { ...location, uri: new URL(location.uri, location.baseUri).href } : location; } function openLocation(location, pluginManager) { if (isLocalPathLocation(location)) { if (!location.localPath) { throw new Error('No local path provided'); } if (detect_node_1.default || __1.isElectron) { return new generic_filehandle2_1.LocalFile(location.localPath); } else { throw new Error("can't use local files in the browser"); } } if (isBlobLocation(location)) { const blob = (0, tracks_1.getBlob)(location.blobId); if (!blob) { throw new Error(`file ("${location.name}") was opened locally from a previous session. To restore it, go to track settings and reopen the file`); } return new generic_filehandle2_1.BlobFile(blob); } if ((0, types_1.isUriLocation)(location)) { if (!location.uri) { throw new Error('No URI provided'); } const absoluteLocation = resolveUriLocation(location); if (pluginManager) { const internetAccount = getInternetAccount(location, pluginManager); if (internetAccount) { return internetAccount.openLocation(absoluteLocation); } } return new RemoteFileWithRangeCache_1.RemoteFileWithRangeCache(absoluteLocation.uri, { fetch: checkAuthNeededFetch, }); } throw new Error('invalid fileLocation'); } function getFetcher(location, pluginManager) { if (!(0, types_1.isUriLocation)(location)) { throw new Error(`Not a valid UriLocation: ${JSON.stringify(location)}`); } if (pluginManager) { const internetAccount = getInternetAccount(location, pluginManager); if (internetAccount) { return internetAccount.getFetcher(location); } } return checkAuthNeededFetch; } function getInternetAccount(location, pluginManager) { const { rootModel } = pluginManager; if (rootModel && (0, types_1.isRootModelWithInternetAccounts)(rootModel)) { return rootModel.findAppropriateInternetAccount(location); } if (location.internetAccountPreAuthorization) { if (!location.internetAccountPreAuthorization.authInfo.token) { throw new Error('Failed to obtain token from internet account. Try reloading the page'); } return pluginManager .getInternetAccountType(location.internetAccountPreAuthorization.internetAccountType) .stateModel.create({ type: location.internetAccountPreAuthorization.internetAccountType, configuration: location.internetAccountPreAuthorization.authInfo.configuration, }); } return undefined; } async function checkAuthNeededFetch(url, opts) { var _a; const response = await fetch(url, opts); if (response.status === 401 && ((_a = response.headers.get('WWW-Authenticate')) === null || _a === void 0 ? void 0 : _a.includes('Basic'))) { throw new types_1.AuthNeededError('Accessing HTTPBasic resource without authentication', url.toString()); } return response; } var RemoteFileWithRangeCache_2 = require("./RemoteFileWithRangeCache"); Object.defineProperty(exports, "RemoteFileWithRangeCache", { enumerable: true, get: function () { return RemoteFileWithRangeCache_2.RemoteFileWithRangeCache; } });