Skip to content

Commit

Permalink
fix: improve URL detection (#5698)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Jun 3, 2024
1 parent abec9bd commit 3d3f3ab
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/cspell-io/src/node/file/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('util', () => {
${'samples/cities.txt.gz'} | ${false}
${'https://github.com/streetsidesoftware/cspell/raw/main/packages/cspell-io/samples/cities.txt'} | ${true}
${'https://github.com/streetsidesoftware/cspell/raw/main/packages/cspell-io/samples/cities.txt.gz'} | ${true}
${'vsls:/cspell.config.yaml'} | ${true}
`('isUrlLike $file', ({ file, expected }) => {
expect(isUrlLike(file)).toBe(expected);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-io/src/node/file/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { pathToFileURL } from 'node:url';

const isZippedRegExp = /\.gz($|[?#])/i;

const isURLRegExp = /^([\w-]{2,64}:\/\/|data:)/i;
const isURLRegExp = /^([\w-]{2,64}:\/|data:)/i;
const isWindowsPath = /^[a-z]:[\\/]/i;
const supportedProtocols: Record<string, true | undefined> = { 'file:': true, 'http:': true, 'https:': true };

Expand Down
1 change: 1 addition & 0 deletions packages/cspell-lib/src/lib/util/Uri.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('Uri', () => {
${fromStdinFilePath('D:\\home\\prj\\code.c')} | ${{ ...u, scheme: 'stdin', path: '/d:/home/prj/code.c' }}
${fromStdinFilePath(__filename)} | ${{ ...u, scheme: 'stdin', path: normalizePath(__filename) }}
${'example.com/'} | ${{ ...u, ...URIparse('example.com/') }}
${'vsls:/cspell.config.yaml'} | ${{ ...u, scheme: 'vsls', path: '/cspell.config.yaml' }}
`('toUri $uri', ({ uri, expected }) => {
const u = toUri(uri);
expect(j(u)).toEqual(expected);
Expand Down
9 changes: 4 additions & 5 deletions packages/cspell-lib/src/lib/util/Uri.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'node:assert';

import { URI, Utils } from 'vscode-uri';
import { isUrlLike } from 'cspell-io'

export interface Uri {
readonly scheme: string;
Expand All @@ -21,17 +22,15 @@ interface HRef {
const isFile = /^(?:[a-zA-Z]:|[/\\])/;
const isPossibleUri = /\w:\/\//;

const isUrl = /^(file:|stdin:|https?:|s?ftp:)/;

const STDIN_PROTOCOL = 'stdin:';

export function toUri(uriOrFile: string | Uri | URL): UriInstance {
if (UriImpl.isUri(uriOrFile)) return uriOrFile;
if (URI.isUri(uriOrFile)) return UriImpl.from(uriOrFile);
if (uriOrFile instanceof URL) return UriImpl.parse(uriOrFile.toString());
if (isUrlLike(uriOrFile)) return UriImpl.parse(uriOrFile.href);
if (isHRef(uriOrFile)) return UriImpl.parse(uriOrFile.href);
if (isUri(uriOrFile)) return UriImpl.from(uriOrFile);
if (isUrl.test(uriOrFile)) return UriImpl.parse(uriOrFile);
if (isUrlLike(uriOrFile)) return UriImpl.parse(uriOrFile);
return isFile.test(uriOrFile) && !isPossibleUri.test(uriOrFile)
? UriImpl.file(normalizeDriveLetter(uriOrFile))
: UriImpl.parse(uriOrFile);
Expand Down Expand Up @@ -63,7 +62,7 @@ export function normalizeDriveLetter(path: string): string {
return hasDriveLetter.test(path) ? path[0].toLowerCase() + path.slice(1) : path;
}

function isUrlLike(url: unknown): url is HRef {
function isHRef(url: unknown): url is HRef {
return (!!url && typeof url === 'object' && typeof (<HRef>url).href === 'string') || false;
}

Expand Down

0 comments on commit 3d3f3ab

Please sign in to comment.