UNPKG

thebe-core

Version:

Typescript based core functionality for Thebe

127 lines 5.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeBinderUrls = exports.WELL_KNOWN_REPO_PROVIDERS = exports.GIST_SPEC = exports.GIT_SPEC = exports.GITLAB_SPEC = exports.GITHUB_SPEC = exports.makeDefaultStorageKey = void 0; function makeDefaultStorageKey(storagePrefix, url) { const urlObj = new URL(url); // ignore the query string and hash return `${storagePrefix}-${urlObj.origin + urlObj.pathname}`; } exports.makeDefaultStorageKey = makeDefaultStorageKey; function makeDefaultBuildSpec(storagePrefix, binderUrl, stub) { const build = `${binderUrl}/build/${stub}`; const launch = `${binderUrl}/v2/${stub}`; return { build, launch, storageKey: makeDefaultStorageKey(storagePrefix, build), }; } /** * Make a binder url for git providers * * - trim trailing or leading '/' on repo * - trailing / on binderUrl * - convert to URL acceptable string. Required for git * * @param opts BinderOptions * @returns a binder compatible url */ function makeGitUrls(config) { if (!config.binder.repo) throw Error('repo is required for git provider'); const { repo, binderUrl, ref } = config.binder; const encodedRepo = encodeURIComponent(repo.replace(/(^\/)|(\/?$)/g, '')); const base = binderUrl === null || binderUrl === void 0 ? void 0 : binderUrl.replace(/(\/?$)/g, ''); const stub = `git/${encodedRepo}/${ref !== null && ref !== void 0 ? ref : 'HEAD'}`; return makeDefaultBuildSpec(config.savedSessions.storagePrefix, base, stub); } /** * Make a binder url for gitlab providers * * - trim gitlab.com from repo * - trim trailing or leading '/' on repo * - convert to URL acceptable string. Required for gitlab * - trailing / on binderUrl * * @param opts BinderOptions * @returns a binder compatible url */ function makeGitLabUrl(config) { var _a, _b, _c; if (!config.binder.repo) throw Error('repo is required for gitlab provider'); const binderUrl = (_a = config.binder.binderUrl) === null || _a === void 0 ? void 0 : _a.replace(/(\/?$)/g, ''); const repo = encodeURIComponent(((_b = config.binder.repo) !== null && _b !== void 0 ? _b : '') .replace(/^(https?:\/\/)?gitlab.com\//, '') .replace(/(^\/)|(\/?$)/g, '')); const stub = `gl/${repo}/${(_c = config.binder.ref) !== null && _c !== void 0 ? _c : 'HEAD'}`; return makeDefaultBuildSpec(config.savedSessions.storagePrefix, binderUrl, stub); } /** * Make a binder url for gitlab providers * * - trim github.com from repo * - trim trailing or leading '/' on repo * - convert to URL acceptable string. Required for gitlab * - trailing / on binderUrl * * @param opts BinderOptions * @returns a binder compatible url */ function makeGitHubUrl(config) { var _a, _b; if (!config.binder.repo) throw Error('repo is required for github provider'); const repo = config.binder.repo .replace(/^(https?:\/\/)?github.com\//, '') .replace(/(^\/)|(\/?$)/g, ''); const binderUrl = (_a = config.binder.binderUrl) === null || _a === void 0 ? void 0 : _a.replace(/(\/?$)/g, ''); const stub = `gh/${repo}/${(_b = config.binder.ref) !== null && _b !== void 0 ? _b : 'HEAD'}`; return makeDefaultBuildSpec(config.savedSessions.storagePrefix, binderUrl, stub); } function makeGistUrl(config) { var _a, _b; if (!config.binder.repo) throw Error('repo is required for gist provider'); const repo = config.binder.repo .replace(/^(https?:\/\/)?github.com\//, '') .replace(/(^\/)|(\/?$)/g, ''); const binderUrl = (_a = config.binder.binderUrl) === null || _a === void 0 ? void 0 : _a.replace(/(\/?$)/g, ''); const stub = `gist/${repo}/${(_b = config.binder.ref) !== null && _b !== void 0 ? _b : 'HEAD'}`; return makeDefaultBuildSpec(config.savedSessions.storagePrefix, binderUrl, stub); } exports.GITHUB_SPEC = { name: 'github', makeUrls: makeGitHubUrl, }; exports.GITLAB_SPEC = { name: 'gitlab', makeUrls: makeGitLabUrl, }; exports.GIT_SPEC = { name: 'git', makeUrls: makeGitUrls, }; exports.GIST_SPEC = { name: 'gist', makeUrls: makeGistUrl, }; exports.WELL_KNOWN_REPO_PROVIDERS = [exports.GITHUB_SPEC, exports.GITLAB_SPEC, exports.GIT_SPEC, exports.GIST_SPEC]; /** * Make a binder url for both well known or custom providers * * Custom providers are supported by passing in an array of CustomRepoProviderSpecs. * */ function makeBinderUrls(config, repoProviders) { var _a, _b; const providerMap = (_a = repoProviders.reduce((obj, spec) => (Object.assign(Object.assign({}, obj), { [spec.name]: spec })), {})) !== null && _a !== void 0 ? _a : {}; const provider = (_b = config.binder.repoProvider) !== null && _b !== void 0 ? _b : 'github'; if (!Object.keys(providerMap).includes(provider)) throw Error(`Unknown provider ${config.binder.repoProvider}`); if (!providerMap[provider].makeUrls) throw Error(`No makeUrls function for ${provider}`); return providerMap[provider].makeUrls(config); } exports.makeBinderUrls = makeBinderUrls; //# sourceMappingURL=url.js.map