Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RELEASES downloading from private repo #103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/aliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const aliases = {
deb: ['debian'],
rpm: ['fedora'],
AppImage: ['appimage'],
dmg: ['dmg']
dmg: ['dmg'],
nupkg: ['nupkg']
}

module.exports = platform => {
Expand Down
31 changes: 17 additions & 14 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,21 @@ module.exports = class Cache {

async cacheReleaseList(url) {
const { token } = this.config
const headers = { Accept: 'application/vnd.github.preview' }
const headers = { Accept: 'application/octet-stream' }

if (token && typeof token === 'string' && token.length > 0) {
headers.Authorization = `token ${token}`
}

const { status, body } = await retry(
const { body } = await retry(
async () => {
const response = await fetch(url, { headers })

if (response.status !== 200) {
throw new Error(
`Tried to cache RELEASES, but failed fetching ${url}, status ${status}`
`Tried to cache RELEASES, but failed fetching ${url}, status ${
response.status
}`
)
}

Expand All @@ -67,10 +69,12 @@ module.exports = class Cache {
)
}

for (let i = 0; i < matches.length; i += 1) {
const nuPKG = url.replace('RELEASES', matches[i])
content = content.replace(matches[i], nuPKG)
}
content = content.replace(
matches[0],
`${this.config.url}/download/latest/${matches[0]}`
Copy link

@Juned-H2 Juned-H2 Feb 1, 2022

Choose a reason for hiding this comment

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

change
${this.config.url}/download/latest/${matches[0]}
to
https://${this.config.url}/download/latest/${matches[0]}

To avoid/fix Squirrel 4294967295 error: System.Exception: Filename can either be an absolute HTTP[s] URL, or a file name

Choose a reason for hiding this comment

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

Can confirm that PR works. Any plans on merging it to master?

)
console.log('content', content)

return content
}

Expand Down Expand Up @@ -127,6 +131,7 @@ module.exports = class Cache {
this.latest.version = tag_name
this.latest.notes = release.body
this.latest.pub_date = release.published_at
this.latest.files = {}

// Clear list of download links
this.latest.platforms = {}
Expand All @@ -136,12 +141,7 @@ module.exports = class Cache {

if (name === 'RELEASES') {
try {
if (!this.latest.files) {
this.latest.files = {}
}
this.latest.files.RELEASES = await this.cacheReleaseList(
browser_download_url
)
this.latest.files.RELEASES = await this.cacheReleaseList(url)
} catch (err) {
console.error(err)
}
Expand All @@ -154,13 +154,16 @@ module.exports = class Cache {
continue
}

this.latest.platforms[platform] = {
const entry = {
name,
api_url: url,
url: browser_download_url,
content_type,
size: Math.round(size / 1000000 * 10) / 10
}

this.latest.platforms[platform] = entry
this.latest.files[entry.name] = entry
}

console.log(`Finished caching version ${tag_name}`)
Expand Down
21 changes: 12 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Cache = require('./cache')

module.exports = config => {
const router = Router()
let cache = null;
let cache = null

try {
cache = new Cache(config)
Expand All @@ -14,14 +14,16 @@ module.exports = config => {

if (code) {
return (req, res) => {
res.statusCode = 400;

res.end(JSON.stringify({
error: {
code,
message
}
}))
res.statusCode = 400

res.end(
JSON.stringify({
error: {
code,
message
}
})
)
}
}

Expand All @@ -34,6 +36,7 @@ module.exports = config => {
router.get('/', routes.overview)
router.get('/download', routes.download)
router.get('/download/:platform', routes.downloadPlatform)
router.get('/download/latest/:file', routes.downloadLatest)
router.get('/update/:platform/:version', routes.update)
router.get('/update/win32/:version/RELEASES', routes.releases)

Expand Down
2 changes: 1 addition & 1 deletion lib/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ module.exports = fileName => {
return 'darwin'
}

const directCache = ['exe', 'dmg', 'rpm', 'deb', 'AppImage']
const directCache = ['exe', 'dmg', 'rpm', 'deb', 'AppImage', 'nupkg']
return directCache.find(ext => ext === extension) || false
}
25 changes: 24 additions & 1 deletion lib/routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Native
const urlHelpers = require('url');
const urlHelpers = require('url')

// Packages
const { send } = require('micro')
Expand Down Expand Up @@ -109,6 +109,29 @@ module.exports = ({ cache, config }) => {
res.end()
}

exports.downloadLatest = async (req, res) => {
const { file } = req.params

// Get the latest version from the cache
const latest = await loadCache()

if (!latest.files || !latest.files[file]) {
send(res, 404, 'File not found')
return
}

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

res.writeHead(302, {
Location: latest.files[file].url
})

res.end()
}

exports.update = async (req, res) => {
const { platform: platformName, version } = req.params

Expand Down
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hazel = require('./index')
const hazel = require('.')

const {
INTERVAL: interval,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"lint-staged": {
"*.js": [
"yarn test && :",
"yarn test --passWithNoTests && :",
"prettier --single-quote --no-semi --write --no-editorconfig",
"git add"
]
Expand Down