diff --git a/.changeset/slimy-ravens-chew.md b/.changeset/slimy-ravens-chew.md new file mode 100644 index 0000000..d3f8b52 --- /dev/null +++ b/.changeset/slimy-ravens-chew.md @@ -0,0 +1,5 @@ +--- +"cube-link": patch +--- + +Forward profiles directly from GitHub diff --git a/trifid/redirect.js b/trifid/redirect.js index 2384a54..ab5568b 100644 --- a/trifid/redirect.js +++ b/trifid/redirect.js @@ -1,4 +1,5 @@ // @ts-check +const { Readable } = require('stream') const { fetchBuilder, MemoryCache } = require('node-fetch-cache') /** @@ -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}`) + } } }