Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: /ref/hash/shape links #133

Merged
merged 4 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-pans-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cube-link": patch
---

Support git refs when serving shapes from Trifid
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ For example, to get version 0.0.4 of `standalone-cube-constraint.ttl`, fetch htt

Otherwise, to get the latest version, fetch https://cube.link/latest/shape/standalone-cube-constraint instead.

Finally, it is possible to request shapes from a branch or specific commit by building the URI as `/ref/{REF}/shape/...`.
For example, to get the `standalone-cube-constraint` shape from the `main` branch, fetch https://cube.link/ref/main/shape/standalone-cube-constraint and to get it from the commit `a1b2c3d4e5f6`, fetch https://cube.link/ref/a1b2c3d4e5f6/shape/standalone-cube-constraint .

## How to Contribute

Please open [Issues](https://github.com/zazuko/cube-link/issues) on this repository or provide PRs for contributions.
Expand Down
56 changes: 30 additions & 26 deletions trifid/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ function factory () {
return res.redirect('/relation/')
}

// Redirect to a version of a shape (for routes: `/vX.Y.Z/shape/NAME` and `/latest/shape/NAME`)
const shapePath = requestPath.split('/').slice(3).join('/')
const versionMatch = requestPath.match(/^\/(?<version>v[0-9]+\.[0-9]+\.[0-9]+)\/shape\//)
if (versionMatch || requestPath.startsWith('/latest/shape/')) {
let versionPath = versionMatch?.groups?.version
if (!versionPath) {
// Redirect to a version of a shape (for routes: `/vX.Y.Z/shape/NAME`, `/ref/COMMIT_ID/shape/NAME` and `/latest/shape/NAME`)
const versionMatch = requestPath.match(/^\/((?<version>v[0-9]+\.[0-9]+\.[0-9]+)|(ref\/(?<ref>.+))|latest)\/shape\/(?<shapePath>.+)/)
if (versionMatch) {
let versionPath

const { version, ref, shapePath } = versionMatch?.groups || {}
if (!version && !ref) {
const tags = await cachedFetch('https://api.github.com/repos/zazuko/cube-link/tags').then(async (res) => {
if (!res.ok) {
await res.ejectFromCache()
Expand All @@ -53,28 +54,31 @@ function factory () {
} else {
versionPath = tags[0].name
}
} else if (ref) {
versionPath = ref
} else if (version) {
versionPath = version
}
tpluscode marked this conversation as resolved.
Show resolved Hide resolved
if (shapePath) {
try {
const rawGithub = await fetch(`https://raw.githubusercontent.com/zazuko/cube-link/${versionPath}/validation/${shapePath}.ttl`)
if (rawGithub.ok) {
res.set('Content-Type', 'text/turtle')
} else {
res.status(500)
}
// if the shape does not exist, we return a 404
if (rawGithub.status === 404) {
return res.sendStatus(404)
}
/** @type {any | null} */
const body = rawGithub.body
if (!rawGithub.body) {
throw new Error('No body')
}
return Readable.fromWeb(body).pipe(res)
} catch (e) {
return res.status(502).send(`Error fetching shape: ${e.message}`)

try {
const rawGithub = await fetch(`https://raw.githubusercontent.com/zazuko/cube-link/${versionPath}/validation/${shapePath}.ttl`)
if (rawGithub.ok) {
res.set('Content-Type', 'text/turtle')
} else {
res.status(500)
}
// if the shape does not exist, we return a 404
if (rawGithub.status === 404) {
return res.sendStatus(404)
}
/** @type {any | null} */
const body = rawGithub.body
if (!rawGithub.body) {
throw new Error('No body')
}
return Readable.fromWeb(body).pipe(res)
} catch (e) {
return res.status(502).send(`Error fetching shape: ${e.message}`)
}
}

Expand Down