Skip to content

Commit

Permalink
Merge pull request #120 from zazuko/no-redirect
Browse files Browse the repository at this point in the history
feat: send shapes directly without redirect
  • Loading branch information
tpluscode authored Nov 29, 2023
2 parents bc0d36e + 8319742 commit 62d7ec8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/slimy-ravens-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cube-link": patch
---

Forward profiles directly from GitHub
22 changes: 21 additions & 1 deletion trifid/redirect.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-check
const { Readable } = require('stream')
const { fetchBuilder, MemoryCache } = require('node-fetch-cache')

/**
Expand Down Expand Up @@ -54,7 +55,26 @@ function factory () {
}
}
if (shapePath) {
return res.redirect(`https://raw.githubusercontent.com/zazuko/cube-link/${versionPath}/validation/${shapePath}.ttl`)
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

0 comments on commit 62d7ec8

Please sign in to comment.