From 01033c465cbccaff26d3f84134669cb110015392 Mon Sep 17 00:00:00 2001 From: Vlad Hashimoto Date: Thu, 16 May 2019 23:33:32 +0300 Subject: [PATCH] feat: add Electron hosted headers redirect (#2522) * feat: add Electron hosted headers redirect * style: prettier * test: add tests --- routes/headers.js | 7 +++++++ server.js | 3 ++- test/index.js | 12 ++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 routes/headers.js diff --git a/routes/headers.js b/routes/headers.js new file mode 100644 index 000000000..947b4577f --- /dev/null +++ b/routes/headers.js @@ -0,0 +1,7 @@ +const DIST_URL = 'https://gh-contractor-zcbenz.s3.amazonaws.com/atom-shell/dist' + +module.exports = (req, res, next) => { + const url = req.params[0] + if (url === []) return next() + return res.redirect(`${DIST_URL}/${url}`) +} diff --git a/server.js b/server.js index 0eae26776..26f6d8510 100644 --- a/server.js +++ b/server.js @@ -58,7 +58,7 @@ app.use(requestLanguage({ languages: Object.keys(i18n.locales), cookie: { name: 'language', - options: {maxAge: 30 * 24 * 60 * 60 * 1000}, + options: { maxAge: 30 * 24 * 60 * 60 * 1000 }, url: '/languages/{language}' } })) @@ -111,6 +111,7 @@ app.get('/userland', routes.userland.index) app.get('/userland/*', routes.userland.show) app.use('/crowdin', routes.languages.proxy) app.use('/donors', routes.donors) +app.use('/headers/*', routes.headers) app.get('/search/:searchIn*?*', (req, res) => res.redirect(req.query.q ? `/?query=${req.query.q}` : `/`)) // Generic 404 handler diff --git a/test/index.js b/test/index.js index b7b999d2f..06b296e1d 100644 --- a/test/index.js +++ b/test/index.js @@ -655,6 +655,18 @@ describe('electronjs.org', () => { res.headers['content-type'].should.equal('application/javascript; charset=UTF-8') }) + describe('node headers', () => { + it('redirects valid to S3 Bucket', async () => { + let res = await supertest(app).get(`/headers/v3.1.6/iojs-3.1.6.tgz.tz`) + res.statusCode.should.equal(302) + }) + + it('show 404 to invalid', async () => { + let res = await supertest(app).get(`/headers`) + res.statusCode.should.equal(404) + }) + }) + describe('search redirects', () => { it('redirect /search/package to home page search', async () => { const res = await supertest(app).get('/search/npmPackages?q=@siberianmh/cosmos')