diff --git a/package-lock.json b/package-lock.json index cb6482f24..b931e6f44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,7 +32,6 @@ "vue": "^3.3.4", "vue-toastification": "^2.0.0-rc.5", "vuetify": "^3.1.14", - "whatwg-url": "^12.0.1", "zod": "^3.18.0" }, "devDependencies": { @@ -48,7 +47,6 @@ "@types/mocha": "^9.1.1", "@types/sinon": "^10.0.11", "@types/sinon-chai": "^3.2.8", - "@types/whatwg-url": "^11.0.0", "@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/parser": "^5.62.0", "@vitejs/plugin-vue": "^4.2.3", @@ -4404,21 +4402,6 @@ "resolved": "https://registry.npmjs.org/@types/web-bluetooth/-/web-bluetooth-0.0.14.tgz", "integrity": "sha512-5d2RhCard1nQUC3aHcq/gHzWYO6K0WJmAbjO7mQJgCQKtZpgXxv1rOM6O/dBDhDYYVutk1sciOgNSe+5YyfM8A==" }, - "node_modules/@types/webidl-conversions": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.0.tgz", - "integrity": "sha512-xTE1E+YF4aWPJJeUzaZI5DRntlkY3+BCVJi0axFptnjGmAoWxkyREIh/XMrfxVLejwQxMCfDXdICo0VLxThrog==", - "dev": true - }, - "node_modules/@types/whatwg-url": { - "version": "11.0.0", - "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.0.tgz", - "integrity": "sha512-4F6szvZP3FM5HvJAmcInXBfrAhvM4tLIc8MO1nXwabG5TZVOLxVmAXRpICqXYd3lBlomSRGmLCopYV+yTocgpQ==", - "dev": true, - "dependencies": { - "@types/webidl-conversions": "*" - } - }, "node_modules/@types/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/@types/which/-/which-2.0.2.tgz", @@ -20098,6 +20081,9 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "dev": true, + "optional": true, + "peer": true, "engines": { "node": ">=12" } @@ -20300,6 +20286,9 @@ "version": "12.0.1", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-12.0.1.tgz", "integrity": "sha512-Ed/LrqB8EPlGxjS+TrsXcpUond1mhccS3pchLhzSgPCnTimUCKj3IZE75pAs5m6heB2U2TMerKFUXheyHY+VDQ==", + "dev": true, + "optional": true, + "peer": true, "dependencies": { "tr46": "^4.1.1", "webidl-conversions": "^7.0.0" @@ -20312,6 +20301,9 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/tr46/-/tr46-4.1.1.tgz", "integrity": "sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==", + "dev": true, + "optional": true, + "peer": true, "dependencies": { "punycode": "^2.3.0" }, @@ -24510,21 +24502,6 @@ "resolved": "https://registry.npmjs.org/@types/web-bluetooth/-/web-bluetooth-0.0.14.tgz", "integrity": "sha512-5d2RhCard1nQUC3aHcq/gHzWYO6K0WJmAbjO7mQJgCQKtZpgXxv1rOM6O/dBDhDYYVutk1sciOgNSe+5YyfM8A==" }, - "@types/webidl-conversions": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.0.tgz", - "integrity": "sha512-xTE1E+YF4aWPJJeUzaZI5DRntlkY3+BCVJi0axFptnjGmAoWxkyREIh/XMrfxVLejwQxMCfDXdICo0VLxThrog==", - "dev": true - }, - "@types/whatwg-url": { - "version": "11.0.0", - "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.0.tgz", - "integrity": "sha512-4F6szvZP3FM5HvJAmcInXBfrAhvM4tLIc8MO1nXwabG5TZVOLxVmAXRpICqXYd3lBlomSRGmLCopYV+yTocgpQ==", - "dev": true, - "requires": { - "@types/webidl-conversions": "*" - } - }, "@types/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/@types/which/-/which-2.0.2.tgz", @@ -36279,7 +36256,10 @@ "webidl-conversions": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", - "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==" + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "dev": true, + "optional": true, + "peer": true }, "webpack": { "version": "5.75.0", @@ -36426,6 +36406,9 @@ "version": "12.0.1", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-12.0.1.tgz", "integrity": "sha512-Ed/LrqB8EPlGxjS+TrsXcpUond1mhccS3pchLhzSgPCnTimUCKj3IZE75pAs5m6heB2U2TMerKFUXheyHY+VDQ==", + "dev": true, + "optional": true, + "peer": true, "requires": { "tr46": "^4.1.1", "webidl-conversions": "^7.0.0" @@ -36435,6 +36418,9 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/tr46/-/tr46-4.1.1.tgz", "integrity": "sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==", + "dev": true, + "optional": true, + "peer": true, "requires": { "punycode": "^2.3.0" } diff --git a/package.json b/package.json index fd04fedee..29f987dc4 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,6 @@ "vue": "^3.3.4", "vue-toastification": "^2.0.0-rc.5", "vuetify": "^3.1.14", - "whatwg-url": "^12.0.1", "zod": "^3.18.0" }, "devDependencies": { @@ -63,7 +62,6 @@ "@types/mocha": "^9.1.1", "@types/sinon": "^10.0.11", "@types/sinon-chai": "^3.2.8", - "@types/whatwg-url": "^11.0.0", "@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/parser": "^5.62.0", "@vitejs/plugin-vue": "^4.2.3", diff --git a/src/components/App.vue b/src/components/App.vue index 170094057..cc17abab8 100644 --- a/src/components/App.vue +++ b/src/components/App.vue @@ -229,7 +229,6 @@ import { import { storeToRefs } from 'pinia'; import { UrlParams } from '@vueuse/core'; import vtkURLExtract from '@kitware/vtk.js/Common/Core/URLExtract'; -import { URL } from 'whatwg-url'; import { useDisplay } from 'vuetify'; import { basename } from '@/src/utils/path'; @@ -240,6 +239,7 @@ import { ImportDataSourcesResult, convertSuccessResultToDataSelection, } from '@/src/io/import/importDataSources'; +import { parseUrl } from '@/src/utils/url'; import ToolButton from './ToolButton.vue'; import LayoutGrid from './LayoutGrid.vue'; import ModulePanel from './ModulePanel.vue'; @@ -323,7 +323,9 @@ async function loadRemoteFilesFromURLParams( const sources = urls.map((url, idx) => uriToDataSource( url, - names[idx] || basename(new URL(url, window.location.href).pathname) || url + names[idx] || + basename(parseUrl(url, window.location.href).pathname) || + url ) ); diff --git a/src/io/amazonS3.ts b/src/io/amazonS3.ts index 74c27130c..bed7c5c0a 100644 --- a/src/io/amazonS3.ts +++ b/src/io/amazonS3.ts @@ -1,12 +1,12 @@ +import { parseUrl } from '@/src/utils/url'; import { S3Client, ListObjectsV2Command } from '@aws-sdk/client-s3'; -import { URL } from 'whatwg-url'; /** * Detects `s3://` uri. * @param uri * @returns */ -export const isAmazonS3Uri = (uri: string) => new URL(uri).protocol === 's3:'; +export const isAmazonS3Uri = (uri: string) => parseUrl(uri).protocol === 's3:'; export type ObjectAvailableCallback = (url: string, name: string) => void; @@ -66,7 +66,7 @@ async function fetchObjectsWithPagination( * @returns */ export const extractBucketAndPrefixFromS3Uri = (uri: string) => { - const { hostname: bucket, pathname } = new URL(uri); + const { hostname: bucket, pathname } = parseUrl(uri); // drop the leading forward slash const objectName = pathname.replace(/^\//, ''); return [bucket, objectName] as const; diff --git a/src/io/googleCloudStorage.ts b/src/io/googleCloudStorage.ts index ce19ba514..3ef604e9b 100644 --- a/src/io/googleCloudStorage.ts +++ b/src/io/googleCloudStorage.ts @@ -1,4 +1,4 @@ -import { URL } from 'whatwg-url'; +import { parseUrl } from '@/src/utils/url'; import { fetchJSON } from '../utils/fetch'; export interface GcsObject { @@ -23,7 +23,7 @@ interface GcsObjectListResult { * @returns */ export const isGoogleCloudStorageUri = (uri: string) => - new URL(uri).protocol === 'gs:'; + parseUrl(uri).protocol === 'gs:'; /** * Extracts bucket and prefix from `gs://` URIs @@ -31,7 +31,7 @@ export const isGoogleCloudStorageUri = (uri: string) => * @returns */ export const extractBucketAndPrefixFromGsUri = (uri: string) => { - const { hostname: bucket, pathname } = new URL(uri); + const { hostname: bucket, pathname } = parseUrl(uri); // drop the leading forward slash const objectName = pathname.replace(/^\//, ''); return [bucket, objectName] as const; @@ -50,7 +50,7 @@ async function fetchObjectsWithPagination( const objects: GcsObject[] = []; const paginate = async (nextToken?: string) => { - const url = new URL(getObjectEndpoint(bucket)); + const url = parseUrl(getObjectEndpoint(bucket)); url.searchParams.append('prefix', prefix); url.searchParams.append('maxResults', '1000'); if (nextToken) { diff --git a/src/utils/__tests__/url.spec.ts b/src/utils/__tests__/url.spec.ts new file mode 100644 index 000000000..9aeb12f54 --- /dev/null +++ b/src/utils/__tests__/url.spec.ts @@ -0,0 +1,15 @@ +import { parseUrl } from '@/src/utils/url'; +import { describe, expect, it } from 'vitest'; + +describe('utils/url', () => { + describe('parseUrl', () => { + it('should parse a URL', () => { + expect(parseUrl('https://example.com/path').pathname).to.equal('/path'); + expect(parseUrl('gs://bucket/').protocol).to.equal('gs:'); + expect(parseUrl('gs://bucket/path/object').pathname).to.equal( + '/path/object' + ); + expect(parseUrl('path/object', 'gs://bucket').protocol).to.equal('gs:'); + }); + }); +}); diff --git a/src/utils/fetch.ts b/src/utils/fetch.ts index f1c16533c..bc8659ae3 100644 --- a/src/utils/fetch.ts +++ b/src/utils/fetch.ts @@ -1,5 +1,5 @@ +import { parseUrl } from '@/src/utils/url'; import { Awaitable } from '@vueuse/core'; -import { URL } from 'whatwg-url'; /** * Percent is in [0, 1]. If it's Infinity, then the progress is indeterminate. @@ -25,7 +25,7 @@ interface URLHandler { */ const HTTPHandler: URLHandler = { testURL: (url) => { - const { protocol } = new URL(url, window.location.href); + const { protocol } = parseUrl(url, window.location.href); return protocol === 'http:' || protocol === 'https:'; }, fetchURL: async (url, options = {}) => { diff --git a/src/utils/index.ts b/src/utils/index.ts index c1ac4e746..be56e5921 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,6 +1,6 @@ -import { URL } from 'whatwg-url'; import { z } from 'zod'; import { TypedArray } from 'itk-wasm'; +import { parseUrl } from '@/src/utils/url'; import { EPSILON } from '../constants'; import { Maybe } from '../types'; @@ -192,7 +192,7 @@ export function wrapInArray(maybeArray: T | T[]): T[] { * Extracts the basename from a URL. */ export function getURLBasename(url: string) { - return new URL(url, window.location.href).pathname.split('/').at(-1) ?? ''; + return parseUrl(url, window.location.href).pathname.split('/').at(-1) ?? ''; } // from https://stackoverflow.com/a/18650828 diff --git a/src/utils/url.ts b/src/utils/url.ts new file mode 100644 index 000000000..f110b3e27 --- /dev/null +++ b/src/utils/url.ts @@ -0,0 +1,31 @@ +const SPECIAL_SCHEME_RE = /^(https?|ftp|file|wss?):$/; + +function parseWithNonSpecialScheme(url: string, scheme: string) { + const re = new RegExp(`^${scheme}`); + // Parse with a special scheme. Chrome + Firefox conform to + // whatwg-url on special schemes. + const src = new URL(url.replace(re, 'ws:')); + + // We cannot change a special scheme to a non-special scheme + // and vice-versa, so we use a placeholder non-special scheme + // and copy the components over. + const dst = new URL('tmp://'); + + dst.hash = src.hash; + dst.hostname = src.hostname; + dst.port = src.port; + dst.password = src.password; + dst.username = src.username; + dst.pathname = src.pathname; + dst.search = src.search; + dst.protocol = scheme; + return dst; +} + +export function parseUrl(url: string, base?: string) { + const parsed = new URL(url, base); + if (SPECIAL_SCHEME_RE.test(parsed.protocol)) { + return parsed; + } + return parseWithNonSpecialScheme(parsed.toString(), parsed.protocol); +}