From 0ad93478c2b85f0c8609bdfe5c6055f3656f0570 Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Thu, 11 Jul 2024 17:46:31 -0700 Subject: [PATCH] Remove unused functionality from binderhub-client UI related stuff like this goes into spec.js --- js/packages/binderhub-client/lib/index.js | 107 ------- .../binderhub-client/tests/index.test.js | 284 +----------------- 2 files changed, 1 insertion(+), 390 deletions(-) diff --git a/js/packages/binderhub-client/lib/index.js b/js/packages/binderhub-client/lib/index.js index f33f35600..ee474d895 100644 --- a/js/packages/binderhub-client/lib/index.js +++ b/js/packages/binderhub-client/lib/index.js @@ -208,111 +208,4 @@ export class BinderRepository { this.abortController = null; } } - - /** - * Get URL to redirect user to on a Jupyter Server to display a given path - - * @param {URL} serverUrl URL to the running jupyter server - * @param {string} token Secret token used to authenticate to the jupyter server - * @param {string} [path] The path of the file or url suffix to launch the user into - * @param {string} [pathType] One of "lab", "file" or "url", denoting what kinda path we are launching the user into - * - * @returns {URL} A URL to redirect the user to - */ - getFullRedirectURL(serverUrl, token, path, pathType) { - // Make a copy of the URL so we don't mangle the original - let url = new URL(serverUrl); - if (path) { - // Ensure there is a trailing / in serverUrl - if (!url.pathname.endsWith("/")) { - url.pathname += "/"; - } - // trim leading '/' from path to launch users into - path = path.replace(/(^\/)/g, ""); - - if (pathType === "lab") { - // The path is a specific *file* we should open with JupyterLab - // trim trailing / on file paths - path = path.replace(/(\/$)/g, ""); - - // /doc/tree is safe because it allows redirect to files - url = new URL("doc/tree/" + encodeURI(path), url); - } else if (pathType === "file") { - // The path is a specific file we should open with *classic notebook* - - // trim trailing / on file paths - path = path.replace(/(\/$)/g, ""); - - url = new URL("tree/" + encodeURI(path), url); - } else { - // pathType is 'url' and we should just pass it on - url = new URL(path, url); - } - } - - url.searchParams.append("token", token); - return url; - } -} - -/** - * Generate a shareable binder URL for given repository - * - * @param {URL} publicBaseUrl Base URL to use for making public URLs. Must end with a trailing slash. - * @param {string} providerPrefix prefix denoting what provider was selected - * @param {string} repository repo to build - * @param {string} ref optional ref in this repo to build - * @param {string} [path] Path to launch after this repo has been built - * @param {string} [pathType] Type of thing to open path with (raw url, notebook file, lab, etc) - * - * @returns {URL} A URL that can be shared with others, and clicking which will launch the repo - */ -export function makeShareableBinderURL( - publicBaseUrl, - providerPrefix, - repository, - ref, - path, - pathType, -) { - if (!publicBaseUrl.pathname.endsWith("/")) { - throw new Error( - `publicBaseUrl must end with a trailing slash, got ${publicBaseUrl}`, - ); - } - const url = new URL( - `v2/${providerPrefix}/${repository}/${ref}`, - publicBaseUrl, - ); - if (path && path.length > 0) { - url.searchParams.append(`${pathType}path`, path); - } - return url; -} - -/** - * Generate markup that people can put on their README or documentation to link to a specific binder - * - * @param {URL} publicBaseUrl Base URL to use for making public URLs - * @param {URL} url Link target URL that represents this binder installation - * @param {string} syntax Kind of markup to generate. Supports 'markdown' and 'rst' - * @returns {string} - */ -export function makeBadgeMarkup(publicBaseUrl, url, syntax) { - if (!publicBaseUrl.pathname.endsWith("/")) { - throw new Error( - `publicBaseUrl must end with a trailing slash, got ${publicBaseUrl}`, - ); - } - const badgeImageUrl = new URL("badge_logo.svg", publicBaseUrl); - - if (syntax === "markdown") { - return `[![Binder](${badgeImageUrl})](${url})`; - } else if (syntax === "rst") { - return `.. image:: ${badgeImageUrl}\n :target: ${url}`; - } else { - throw new Error( - `Only markdown or rst badges are supported, got ${syntax} instead`, - ); - } } diff --git a/js/packages/binderhub-client/tests/index.test.js b/js/packages/binderhub-client/tests/index.test.js index 04e35685b..6f1ed2408 100644 --- a/js/packages/binderhub-client/tests/index.test.js +++ b/js/packages/binderhub-client/tests/index.test.js @@ -10,7 +10,7 @@ import { parseEventSource, simpleEventSourceServer } from "./utils"; import { readFileSync } from "node:fs"; async function wrapFetch(resource, options) { - /* like fetch, but ignore signal input + /* like fetch, but ignore signal input // abort signal shows up as uncaught in tests, despite working fine */ if (options) { @@ -82,155 +82,6 @@ test("Build URL correctly built from Build Endpoint when used with token", () => ); }); -test("Get full redirect URL with correct token but no path", () => { - const br = new BinderRepository( - "gh/test/test", - new URL("https://test-binder.org/build"), - ); - expect( - br - .getFullRedirectURL( - new URL("https://hub.test-binder.org/user/something"), - "token", - ) - .toString(), - ).toBe("https://hub.test-binder.org/user/something?token=token"); -}); - -test("Get full redirect URL with urlpath", () => { - const br = new BinderRepository( - "gh/test/test", - new URL("https://test-binder.org/build"), - ); - expect( - br - .getFullRedirectURL( - new URL("https://hub.test-binder.org/user/something"), - "token", - "rstudio", - "url", - ) - .toString(), - ).toBe("https://hub.test-binder.org/user/something/rstudio?token=token"); -}); - -test("Get full redirect URL when opening a file with jupyterlab", () => { - const br = new BinderRepository( - "gh/test/test", - new URL("https://test-binder.org/build"), - ); - expect( - br - .getFullRedirectURL( - new URL("https://hub.test-binder.org/user/something"), - "token", - "index.ipynb", - "lab", - ) - .toString(), - ).toBe( - "https://hub.test-binder.org/user/something/doc/tree/index.ipynb?token=token", - ); -}); - -test("Get full redirect URL when opening a file with classic notebook (with file= path)", () => { - const br = new BinderRepository( - "gh/test/test", - new URL("https://test-binder.org/build"), - ); - expect( - br - .getFullRedirectURL( - new URL("https://hub.test-binder.org/user/something"), - "token", - "index.ipynb", - "file", - ) - .toString(), - ).toBe( - "https://hub.test-binder.org/user/something/tree/index.ipynb?token=token", - ); -}); - -test("Get full redirect URL and deal with excessive slashes (with pathType=url)", () => { - const br = new BinderRepository( - "gh/test/test", - new URL("https://test-binder.org/build"), - ); - expect( - br - .getFullRedirectURL( - // Trailing slash should not be preserved here - new URL("https://hub.test-binder.org/user/something/"), - "token", - // Trailing slash should be preserved here, but leading slash should not be repeated - "/rstudio/", - "url", - ) - .toString(), - ).toBe("https://hub.test-binder.org/user/something/rstudio/?token=token"); -}); - -test("Get full redirect URL and deal with excessive slashes (with pathType=lab)", () => { - const br = new BinderRepository( - "gh/test/test", - new URL("https://test-binder.org/build"), - ); - expect( - br - .getFullRedirectURL( - new URL("https://hub.test-binder.org/user/something/"), - "token", - // Both leading and trailing slashes should be gone here. - "/directory/index.ipynb/", - "lab", - ) - .toString(), - ).toBe( - "https://hub.test-binder.org/user/something/doc/tree/directory/index.ipynb?token=token", - ); -}); - -test("Get full redirect URL and deal with missing trailing slash", () => { - const br = new BinderRepository( - "gh/test/test", - new URL("https://test-binder.org/build"), - ); - expect( - br - .getFullRedirectURL( - // Missing trailing slash here should not affect target url - new URL("https://hub.test-binder.org/user/something"), - "token", - "/directory/index.ipynb/", - "lab", - ) - .toString(), - ).toBe( - "https://hub.test-binder.org/user/something/doc/tree/directory/index.ipynb?token=token", - ); -}); - -test("Get full redirect URL and deal with excessive slashes (with pathType=file)", () => { - const br = new BinderRepository( - "gh/test/test", - new URL("https://test-binder.org/build"), - ); - expect( - br - .getFullRedirectURL( - new URL("https://hub.test-binder.org/user/something/"), - "token", - // Both leading and trailing slashes should be gone here. - "/directory/index.ipynb/", - "file", - ) - .toString(), - ).toBe( - "https://hub.test-binder.org/user/something/tree/directory/index.ipynb?token=token", - ); -}); - describe("Iterate over full output from calling the binderhub API", () => { let closeServer, serverUrl; @@ -286,136 +137,3 @@ describe("Invalid eventsource response causes failure", () => { ]); }); }); - -test("Get full redirect URL and deal with query and encoded query (with pathType=url)", () => { - const br = new BinderRepository( - "gh/test/test", - new URL("https://test-binder.org/build"), - ); - expect( - br - .getFullRedirectURL( - new URL("https://hub.test-binder.org/user/something/"), - "token", - // url path here is already url encoded - "endpoint?a=1%2F2&b=3%3F%2F", - "url", - ) - .toString(), - ).toBe( - // url path here is exactly as encoded as passed in - not *double* encoded - "https://hub.test-binder.org/user/something/endpoint?a=1%2F2&b=3%3F%2F&token=token", - ); -}); - -test("Get full redirect URL with nbgitpuller URL", () => { - const br = new BinderRepository( - "gh/test/test", - new URL("https://test-binder.org/build"), - ); - expect( - br - .getFullRedirectURL( - new URL("https://hub.test-binder.org/user/something/"), - "token", - // urlpath is not actually url encoded - note that / is / not %2F - "git-pull?repo=https://github.com/alperyilmaz/jupyterlab-python-intro&urlpath=lab/tree/jupyterlab-python-intro/&branch=master", - "url", - ) - .toString(), - ).toBe( - // generated URL path here *is* url encoded - "https://hub.test-binder.org/user/something/git-pull?repo=https%3A%2F%2Fgithub.com%2Falperyilmaz%2Fjupyterlab-python-intro&urlpath=lab%2Ftree%2Fjupyterlab-python-intro%2F&branch=master&token=token", - ); -}); - -test("Make a shareable URL", () => { - const url = makeShareableBinderURL( - new URL("https://test.binder.org"), - "gh", - "yuvipanda", - "requirements", - ); - expect(url.toString()).toBe( - "https://test.binder.org/v2/gh/yuvipanda/requirements", - ); -}); - -test("Make a shareable path with URL", () => { - const url = makeShareableBinderURL( - new URL("https://test.binder.org"), - "gh", - "yuvipanda", - "requirements", - "url", - "git-pull?repo=https://github.com/alperyilmaz/jupyterlab-python-intro&urlpath=lab/tree/jupyterlab-python-intro/&branch=master", - ); - expect(url.toString()).toBe( - "https://test.binder.org/v2/gh/yuvipanda/requirements?git-pull%3Frepo%3Dhttps%3A%2F%2Fgithub.com%2Falperyilmaz%2Fjupyterlab-python-intro%26urlpath%3Dlab%2Ftree%2Fjupyterlab-python-intro%2F%26branch%3Dmasterpath=url", - ); -}); - -test("Making a shareable URL with base URL without trailing / throws error", () => { - expect(() => { - makeShareableBinderURL( - new URL("https://test.binder.org/suffix"), - "gh", - "yuvipanda", - "requirements", - ); - }).toThrow(Error); -}); - -test("Make a markdown badge", () => { - const url = makeShareableBinderURL( - new URL("https://test.binder.org"), - "gh", - "yuvipanda", - "requirements", - ); - const badge = makeBadgeMarkup( - new URL("https://test.binder.org"), - url, - "markdown", - ); - expect(badge).toBe( - "[![Binder](https://test.binder.org/badge_logo.svg)](https://test.binder.org/v2/gh/yuvipanda/requirements)", - ); -}); - -test("Make a rst badge", () => { - const url = makeShareableBinderURL( - new URL("https://test.binder.org"), - "gh", - "yuvipanda", - "requirements", - ); - const badge = makeBadgeMarkup(new URL("https://test.binder.org"), url, "rst"); - expect(badge).toBe( - ".. image:: https://test.binder.org/badge_logo.svg\n :target: https://test.binder.org/v2/gh/yuvipanda/requirements", - ); -}); - -test("Making a badge with an unsupported syntax throws error", () => { - const url = makeShareableBinderURL( - new URL("https://test.binder.org"), - "gh", - "yuvipanda", - "requirements", - ); - expect(() => { - makeBadgeMarkup(new URL("https://test.binder.org"), url, "docx"); - }).toThrow(Error); -}); - -test("Making a badge with base URL without trailing / throws error", () => { - const url = makeShareableBinderURL( - new URL("https://test.binder.org"), - "gh", - "yuvipanda", - "requirements", - ); - expect(() => { - makeBadgeMarkup(new URL("https://test.binder.org/suffix"), url, "markdown"); - }).toThrow(Error); -});