From a56f5534a69f586b05b50a9da0d538edf3d5271c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Tue, 6 Aug 2019 18:29:07 -0400 Subject: [PATCH] fix tempalting to add redirects to package paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- go_vanity.js | 103 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 28 deletions(-) diff --git a/go_vanity.js b/go_vanity.js index f5b8d9a..e1019ec 100644 --- a/go_vanity.js +++ b/go_vanity.js @@ -5,18 +5,54 @@ * - https://developers.cloudflare.com/workers/about/how-workers-work/ * - https://developers.cloudflare.com/workers/recipes/vcl-conversion/delivering-custom-responses/ * - https://developers.cloudflare.com/workers/writing-workers/handling-errors/ + * - https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API + * - https://developer.mozilla.org/en-US/docs/Web/API/Request **/ +/* main config, this is what decides what gets redirected to GitHub */ const PACKAGES = { 'go.status.im/protocol': 'https://github.com/status-im/status-protocol-go', 'go.status.im/status': 'https://github.com/status-im/status-go', 'go.status.im/whisper': 'https://github.com/status-im/whisper', } -const genMetaTags = (pkgs) => { - return Object.keys(pkgs).map(name => ( - `` - )) +/* I could use a fancy templating engine, but why bother? */ +const htmlTemplate = ({meta, body}) => (` + + + + + Status.im Go Vanity + + + ${meta || ''} + + + ${body || ''} + + +`) + +const tableTemplate = ({rows}) => (` + + + + + + + + + ${rows.join('\n ')} + +
AliasOrigin
+`) + +const genMetaTag = (pkg) => { + return `` +} + +const genPkgLink = (pkg) => { + return `

Redirect: ${pkg} to ${PACKAGES[pkg]}

` } const genTabRows = (pkgs) => { @@ -25,36 +61,47 @@ const genTabRows = (pkgs) => { )) } -const INDEX_HTML = ` - - - - - Status.im Go Vanity - - - ${genMetaTags(PACKAGES).join('\n ')} - - - - - - - - ${genTabRows(PACKAGES).join('\n ')} -
AliasOrigin
- - -` +const findMatchingPackage = (url) => { + return Object.keys(PACKAGES).find((key) => url.endsWith(key)) +} /* fetch and modify request object */ const handleRequest = async (request) => { - return new Response(INDEX_HTML, { - headers: {'Content-Type': 'text/html'} + /* Redirect requests to specific repos to GitHub using meta tags */ + let pkg = findMatchingPackage(request.url) + if (pkg != undefined) { + return new Response( + htmlTemplate({ + meta: genMetaTag(pkg), + body: genPkgLink(pkg), + }), + { headers: {'Content-Type': 'text/html'} } + ) + } + + /* All other paths should return a table with all redirects */ + return new Response( + htmlTemplate({ + body: tableTemplate({rows: genTabRows(PACKAGES)}) + }), + { headers: {'Content-Type': 'text/html'} }) } +/* wrapper for returning JS errors in response body */ +const handleErrors = (func) => { + try { + return func() + } catch (e) { + return new Response(`Error:\n${e.stack}`) + } +} + /* handle every new request */ addEventListener('fetch', (event) => { - event.respondWith(handleRequest(event.request)) + event.respondWith( + handleErrors( + handleRequest.bind(null, event.request) + ) + ) })