Skip to content

Commit

Permalink
Merge pull request #3 from blinemedical/SCLD-10380-proxy-s3
Browse files Browse the repository at this point in the history
SCLD-10380: Proxy asset downloads through S3
  • Loading branch information
BLineBrian authored Jan 4, 2023
2 parents f74442c + a92d212 commit a7eb3e6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 68 deletions.
26 changes: 0 additions & 26 deletions .circleci/config.yml

This file was deleted.

20 changes: 19 additions & 1 deletion lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = class Cache {
this.refreshCache = this.refreshCache.bind(this)
this.loadCache = this.loadCache.bind(this)
this.isOutdated = this.isOutdated.bind(this)
this.cleanDownloadUrl = this.cleanDownloadUrl.bind(this)
}

async cacheReleaseList(url) {
Expand Down Expand Up @@ -70,7 +71,24 @@ module.exports = class Cache {
return content
}

cleanDownloadUrl(url) {
return url
.replace(
`https://github.com/${this.config.account}/${
this.config.repository
}/releases/download/v`,
`${this.config.proxyUrl}/`
)
.replace(
`https://github.com/${this.config.account}/${
this.config.repository
}/releases/download`,
this.config.proxyUrl
)
}

async refreshCache() {
console.log('Refreshing cache');
const { account, repository, pre, token } = this.config
const repo = account + '/' + repository
const url = `https://api.github.com/repos/${repo}/releases?per_page=100`
Expand Down Expand Up @@ -161,7 +179,7 @@ module.exports = class Cache {
this.latest.platforms[platform] = {
name,
api_url: url,
url: browser_download_url,
url: this.cleanDownloadUrl(browser_download_url),
content_type,
size: Math.round(size / 1000000 * 10) / 10
}
Expand Down
40 changes: 1 addition & 39 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const urlHelpers = require('url')
const { send } = require('micro')
const { valid, compare } = require('semver')
const { parse } = require('express-useragent')
const fetch = require('node-fetch')
const distanceInWordsToNow = require('date-fns/distance_in_words_to_now')

// Utilities
Expand All @@ -15,27 +14,8 @@ const prepareView = require('./view')
module.exports = ({ cache, config }) => {
const { loadCache } = cache
const exports = {}
const { token, url } = config
const shouldProxyPrivateDownload =
token && typeof token === 'string' && token.length > 0

// Helpers
const proxyPrivateDownload = (asset, req, res) => {
const redirect = 'manual'
const headers = { Accept: 'application/octet-stream' }
const options = { headers, redirect }
const { api_url: rawUrl } = asset
const finalUrl = rawUrl.replace(
'https://api.github.com/',
`https://${token}@api.github.com/`
)

fetch(finalUrl, options).then(assetRes => {
res.setHeader('Location', assetRes.headers.get('Location'))
send(res, 302)
})
}

exports.download = async (req, res) => {
const userAgent = parse(req.headers['user-agent'])
const params = urlHelpers.parse(req.url, true).query
Expand All @@ -59,11 +39,6 @@ module.exports = ({ cache, config }) => {
return
}

if (shouldProxyPrivateDownload) {
proxyPrivateDownload(platforms[platform], req, res)
return
}

res.writeHead(302, {
Location: platforms[platform].url
})
Expand Down Expand Up @@ -101,11 +76,6 @@ module.exports = ({ cache, config }) => {
return
}

if (token && typeof token === 'string' && token.length > 0) {
proxyPrivateDownload(latest.platforms[platform], req, res)
return
}

res.writeHead(302, {
Location: latest.platforms[platform].url
})
Expand Down Expand Up @@ -163,9 +133,7 @@ module.exports = ({ cache, config }) => {
name: latest.version,
notes,
pub_date,
url: shouldProxyPrivateDownload
? `https://${url}/download/${platformName}?update=true`
: latest.platforms[platform].url
url: latest.platforms[platform].url
})

return
Expand All @@ -190,7 +158,6 @@ module.exports = ({ cache, config }) => {

const content = latest.files.RELEASES

console.log('RESPONSE DATA:', content)
res.writeHead(200, {
'content-length': Buffer.byteLength(content, 'utf8'),
'content-type': 'application/octet-stream'
Expand All @@ -207,11 +174,6 @@ module.exports = ({ cache, config }) => {

const nupkgAsset = latest.platforms.nupkg

if (shouldProxyPrivateDownload) {
proxyPrivateDownload(nupkgAsset, req, res)
return
}

res.writeHead(302, {
Location: nupkgAsset.url
})
Expand Down
6 changes: 4 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const {
PRE: pre,
TOKEN: token,
URL: PRIVATE_BASE_URL,
VERCEL_URL
VERCEL_URL,
PROXY_URL: proxyUrl
} = process.env

const url = VERCEL_URL || PRIVATE_BASE_URL
Expand All @@ -18,5 +19,6 @@ module.exports = hazel({
repository,
pre,
token,
url
url,
proxyUrl
})

1 comment on commit a7eb3e6

@vercel
Copy link

@vercel vercel bot commented on a7eb3e6 Jan 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.