From 6a6bc2dea1ef2ac1e1e2818867a92e21d99e93b0 Mon Sep 17 00:00:00 2001 From: dariobugmann Date: Mon, 6 Dec 2021 12:25:04 +0100 Subject: [PATCH] Fixing downloads from private repository --- lib/cache.js | 26 ++++++++++++++++---------- lib/server.js | 4 ++-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/cache.js b/lib/cache.js index 65e9d30..b097001 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -20,7 +20,7 @@ module.exports = class Cache { if (token && !url) { const error = new Error( - 'Neither VERCEL_URL, nor URL are defined, which are mandatory for private repo mode' + 'Neither NOW_URL, nor URL are defined, which are mandatory for private repo mode' ) error.code = 'missing_configuration_properties' throw error @@ -37,19 +37,23 @@ 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 }) + const response = await fetch(url, { headers, redirect: 'manual' }) + + if (response.status === 302) { + return await fetch(response.headers.get('location')) + } 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}` ) } @@ -67,10 +71,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]}` + ) + console.log('content', content) + return content } @@ -140,7 +146,7 @@ module.exports = class Cache { this.latest.files = {} } this.latest.files.RELEASES = await this.cacheReleaseList( - browser_download_url + url ) } catch (err) { console.error(err) diff --git a/lib/server.js b/lib/server.js index f4b2e39..f7869ea 100644 --- a/lib/server.js +++ b/lib/server.js @@ -7,10 +7,10 @@ const { PRE: pre, TOKEN: token, URL: PRIVATE_BASE_URL, - VERCEL_URL + NOW_URL } = process.env -const url = VERCEL_URL || PRIVATE_BASE_URL +const url = NOW_URL || PRIVATE_BASE_URL module.exports = hazel({ interval,