From 09e2f3db1ae79ca623c28907919c174f9d99b2a0 Mon Sep 17 00:00:00 2001 From: tpluscode Date: Tue, 28 Nov 2023 22:05:02 +0100 Subject: [PATCH 1/3] feat: send shapes directly without redirect --- .changeset/slimy-ravens-chew.md | 5 +++++ trifid/redirect.js | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .changeset/slimy-ravens-chew.md 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..45796d2 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,19 @@ function factory () { } } if (shapePath) { - return res.redirect(`https://raw.githubusercontent.com/zazuko/cube-link/${versionPath}/validation/${shapePath}.ttl`) + return fetch(`https://raw.githubusercdontent.com/zazuko/cube-link/${versionPath}/validation/${shapePath}.ttl`) + .then(rawGithub => { + if (rawGithub.ok) { + res.set('Content-Type', 'text/turtle') + } else { + res.status(500) + } + Readable.fromWeb(rawGithub.body).pipe(res) + }) + .catch((e) => { + res.status(502) + res.send(`Error fetching shape: ${e.message}`) + }) } } From a287558839a80b6dff1cbfc61c7c28a77f4ce4e9 Mon Sep 17 00:00:00 2001 From: Tomasz Pluskiewicz Date: Wed, 29 Nov 2023 09:13:31 +0100 Subject: [PATCH 2/3] Update trifid/redirect.js Co-authored-by: Ludovic Muller --- trifid/redirect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trifid/redirect.js b/trifid/redirect.js index 45796d2..67e70f6 100644 --- a/trifid/redirect.js +++ b/trifid/redirect.js @@ -55,7 +55,7 @@ function factory () { } } if (shapePath) { - return fetch(`https://raw.githubusercdontent.com/zazuko/cube-link/${versionPath}/validation/${shapePath}.ttl`) + return fetch(`https://raw.githubusercontent.com/zazuko/cube-link/${versionPath}/validation/${shapePath}.ttl`) .then(rawGithub => { if (rawGithub.ok) { res.set('Content-Type', 'text/turtle') From 8319742b08107c5a261413a92f977efd143f08e2 Mon Sep 17 00:00:00 2001 From: Tomasz Pluskiewicz Date: Wed, 29 Nov 2023 09:17:10 +0100 Subject: [PATCH 3/3] refactor: code review --- trifid/redirect.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/trifid/redirect.js b/trifid/redirect.js index 67e70f6..ab5568b 100644 --- a/trifid/redirect.js +++ b/trifid/redirect.js @@ -55,19 +55,26 @@ function factory () { } } if (shapePath) { - return fetch(`https://raw.githubusercontent.com/zazuko/cube-link/${versionPath}/validation/${shapePath}.ttl`) - .then(rawGithub => { - if (rawGithub.ok) { - res.set('Content-Type', 'text/turtle') - } else { - res.status(500) - } - Readable.fromWeb(rawGithub.body).pipe(res) - }) - .catch((e) => { - res.status(502) - res.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}`) + } } }