-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from hypermedia-app/uri-resolution
refactor: URI resolution
- Loading branch information
Showing
24 changed files
with
561 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@hydrofoil/talos-core": minor | ||
"@hydrofoil/talos": minor | ||
--- | ||
|
||
SPARQL Queries are adjusted to use the base URI calculated from the resource path. For example, in query `/tables/generate.ru`, | ||
the effective base URI would be `/tables/generate/`. This is to align this behavior with how static sources are parsed. | ||
In such case, rename the file to `index.ru` to remove the file name from resolves URIs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@hydrofoil/talos-core": minor | ||
"@hydrofoil/talos": minor | ||
--- | ||
|
||
Ensures trailing slash in bare-domain resources |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
"@hydrofoil/talos-core": minor | ||
"@hydrofoil/talos": minor | ||
--- | ||
|
||
Base URI behavior changed. Now relative URIs will be resolved against the calculated base including a trailing slash. | ||
The exception is an empty `<>` reference which will be resolved against the base without a trailing slash. | ||
Use `<./>` to create a resource with a trailing slash. | ||
|
||
| File path | URI reference | Resulting URI | | ||
|-------------------|---------------|--------------------| | ||
| `/api/people.ttl` | `<>` | `/api/people` | | ||
| `/api/people.ttl` | `<.>` | `/api/people` | | ||
| `/api/people.ttl` | `<./>` | `/api/people/` | | ||
| `/api/people.ttl` | `<john>` | `/api/people/john` | | ||
| `/api/people.ttl` | `<#john>` | `/api/people#john` | | ||
| `/api/people.ttl` | `<../people>` | `/api/people` | | ||
| `/api/people.ttl` | `<./people>` | `/api/people` | | ||
| `/api/people.ttl` | `</projects>` | `/projects` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@hydrofoil/talos-core": patch | ||
"@hydrofoil/talos": patch | ||
--- | ||
|
||
Trailing slash in base URI is truncated when resolving relative URI references |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function resourcePathFromFilePath(resourcePath: string) { | ||
resourcePath = resourcePath | ||
.replace(/\.[^.]+$/, '') | ||
.replace(/\/?index$/, '') | ||
return resourcePath === '' | ||
? '' | ||
: encodeURI(resourcePath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
export function resolveResourceUri(baseUri: string, resourcePath: string) { | ||
const baseUriUri = new URL(baseUri) | ||
const basePath = baseUriUri.pathname.split('/').filter(Boolean) | ||
|
||
if (resourcePath.endsWith('/')) { | ||
throw new Error('Resource URL must not end with a slash') | ||
} | ||
if (resourcePath.startsWith('/')) { | ||
throw new Error('Resource URL must not start with a slash') | ||
} | ||
|
||
return (path: string) => { | ||
if (path === '/') { | ||
return baseUriUri.toString() | ||
} | ||
if ((path === '' || path === '.') && resourcePath === '') { | ||
return baseUriUri.toString() | ||
} | ||
|
||
if (path.startsWith('/')) { | ||
return new URL('/' + mergePaths(basePath, path.split('/').slice(1)).join('/'), baseUri).toString() | ||
} | ||
|
||
const combinedPath = [...basePath, ...resourcePath.split('/').filter(Boolean)] | ||
if (path.startsWith('#') || path === '') { | ||
const url = new URL('/' + combinedPath.join('/'), baseUri) | ||
if (path) { | ||
url.hash = path | ||
} | ||
return url.toString() | ||
} | ||
|
||
return new URL('/' + mergePaths(combinedPath, path.split('/')).join('/'), baseUri).toString() | ||
} | ||
} | ||
|
||
function mergePaths(basePath: string[], resourcePath: string[]) { | ||
const result = basePath.slice() | ||
for (const segment of resourcePath) { | ||
if (segment === '.') { | ||
continue | ||
} | ||
if (segment === '..') { | ||
result.pop() | ||
continue | ||
} | ||
result.push(segment) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.