Skip to content

Commit

Permalink
fix: support any URL protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Jun 19, 2024
1 parent 73e53a0 commit 886a99f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
10 changes: 10 additions & 0 deletions vike/utils/parseUrl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,16 @@ describe('parseUrl', () => {
})
})

// https://github.com/vikejs/vike/issues/1706
it('capacitor', () => {
expect(parseUrl('capacitor://localhost/assets/chunks/chunk-v3mOCch-.js', '/')).toEqual({
...resultBase,
origin: 'capacitor://localhost',
pathname: '/assets/chunks/chunk-v3mOCch-.js',
pathnameOriginal: '/assets/chunks/chunk-v3mOCch-.js'
})
})

it('relative paths', () => {
expect(parseUrl('.', '/b1/b2/')).toEqual({
...resultBase,
Expand Down
24 changes: 11 additions & 13 deletions vike/utils/parseUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,10 @@ import { slice } from './slice.js'
import { assert, assertUsage } from './assert.js'
import pc from '@brillout/picocolors'

const PROTOCOLS = [
'http://',
'https://',
// For [Tauri](https://tauri.app/)
'tauri://',
// For Electron: https://github.com/vikejs/vike/issues/1557
'file://',
// For Capacitor: https://github.com/vikejs/vike/issues/1706
'capacitor://'
]

function isParsable(url: string): boolean {
// `parseUrl()` works with these URLs
return (
PROTOCOLS.some((p) => url.startsWith(p)) ||
isUrlWithProtocol(url) ||
url.startsWith('/') ||
url.startsWith('.') ||
url.startsWith('?') ||
Expand Down Expand Up @@ -172,7 +161,7 @@ function getPathname(url: string, baseServer: string): { origin: null | string;
}

function parseOrigin(url: string): { pathname: string; origin: null | string } {
if (!PROTOCOLS.some((protocol) => url.startsWith(protocol))) {
if (!isUrlWithProtocol(url)) {
return { pathname: url, origin: null }
} else {
const [originPart1, originPart2, originPart3, ...pathnameParts] = url.split('/')
Expand Down Expand Up @@ -294,3 +283,12 @@ function isUriWithProtocol(uri: string): boolean {
// https://en.wikipedia.org/wiki/List_of_URI_schemes
return /^[a-z0-9][a-z0-9\.\+\-]*:/i.test(uri)
}

function isUrlWithProtocol(url: string) {
// http://
// https://
// tauri:// # For [Tauri](https://tauri.app/)
// file:// # For Electron: https://github.com/vikejs/vike/issues/1557
// capacitor:// # For Capacitor: https://github.com/vikejs/vike/issues/1706
return /[a-z]+\:\/\//i.test(url)

This comment has been minimized.

Copy link
@pdanpdan

pdanpdan Jun 19, 2024

I would go with "start with letter and allow letter or digit after" - I can think of s3

This comment has been minimized.

Copy link
@brillout

brillout Jun 19, 2024

Author Member

👍 Up for a little PR?

This comment has been minimized.

Copy link
@pdanpdan

pdanpdan Jun 19, 2024

I don't get why there are 2 functions (isUriWithProtocol and isUrlWithProtocol).
But in order to support all current schemes the regex should be /^[a-z][a-z0-9\+\-]*:/i
I have not checked rfc7595 for all details, but i think what is used now is enough.

This comment has been minimized.

Copy link
@brillout

brillout Jun 19, 2024

Author Member

Good question :) I guess we can remove isUrlWithProtocol and only use isUriWithProtocol.

This comment has been minimized.

Copy link
@brillout

brillout Jun 27, 2024

Author Member

Done: c873ce8

This comment has been minimized.

Copy link
@brillout

brillout Jun 27, 2024

Author Member

@pdanpdan How did you come up with /^[a-z][a-z0-9\+\-]*:/i?

}

1 comment on commit 886a99f

@pdanpdan
Copy link

@pdanpdan pdanpdan commented on 886a99f Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the url schemes and this regex matches the registered/official ones and the unofficial ones.

  • Digits
  • + shows up in composed schemes
  • - shows mostly in Microsoft ones
  • . is possible but discouraged in rfc, and I haven't seen it used

Please sign in to comment.