Giter Club home page Giter Club logo

Comments (1)

wobedi avatar wobedi commented on July 16, 2024 8

This comment provided the missing puzzle piece - thank you!!!
Based on it, the following diff made it work for me:

diff --git a/.gitignore b/.gitignore
index be81578..25c04c4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,5 @@ coverage
 
 # dependencies
 node_modules
+
+.vercel
diff --git a/lib/aliases.js b/lib/aliases.js
index 187c40b..f331675 100644
--- a/lib/aliases.js
+++ b/lib/aliases.js
@@ -4,7 +4,8 @@ const aliases = {
   deb: ['debian'],
   rpm: ['fedora'],
   AppImage: ['appimage'],
-  dmg: ['dmg']
+  dmg: ['dmg'],
+  nupkg: ['nupkg']
 }
 
 for (const existingPlatform of Object.keys(aliases)) {
diff --git a/lib/cache.js b/lib/cache.js
index 65e9d30..7b1a1c4 100644
--- a/lib/cache.js
+++ b/lib/cache.js
@@ -12,6 +12,7 @@ module.exports = class Cache {
     const { account, repository, token, url } = config
     this.config = config
 
+
     if (!account || !repository) {
       const error = new Error('Neither ACCOUNT, nor REPOSITORY are defined')
       error.code = 'missing_configuration_properties'
@@ -35,21 +36,22 @@ module.exports = class Cache {
     this.isOutdated = this.isOutdated.bind(this)
   }
 
-  async cacheReleaseList(url) {
+  async cacheReleaseList({ url, browser_download_url }) {
     const { token } = this.config
-    const headers = { Accept: 'application/vnd.github.preview' }
+    const shouldProxyPrivateDownload = token && typeof token === 'string' && token.length > 0
+    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}`
           )
         }
 
@@ -67,10 +69,6 @@ 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)
-    }
     return content
   }
 
@@ -133,15 +131,16 @@ module.exports = class Cache {
 
     for (const asset of release.assets) {
       const { name, browser_download_url, url, content_type, size } = asset
-
+      
       if (name === 'RELEASES') {
         try {
           if (!this.latest.files) {
             this.latest.files = {}
           }
-          this.latest.files.RELEASES = await this.cacheReleaseList(
+          this.latest.files.RELEASES = await this.cacheReleaseList({
+            url,
             browser_download_url
-          )
+          })
         } catch (err) {
           console.error(err)
         }
@@ -163,7 +162,7 @@ module.exports = class Cache {
       }
     }
 
-    console.log(`Finished caching version ${tag_name}`)
+    console.log(`Finished caching version ${tag_name}`, this.latest)
     this.lastUpdate = Date.now()
   }
 
diff --git a/lib/index.js b/lib/index.js
index 631abb7..e0fc426 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -35,7 +35,7 @@ module.exports = config => {
   router.get('/download', routes.download)
   router.get('/download/:platform', routes.downloadPlatform)
   router.get('/update/:platform/:version', routes.update)
-  router.get('/update/win32/:version/RELEASES', routes.releases)
+  router.get('/update/win32/:version/:filename', routes.squirrelWindows)
 
   return (req, res) => {
     router(req, res, finalhandler(req, res))
diff --git a/lib/platform.js b/lib/platform.js
index a717d0a..4362004 100644
--- a/lib/platform.js
+++ b/lib/platform.js
@@ -12,6 +12,6 @@ module.exports = fileName => {
     return 'darwin' + arch
   }
 
-  const directCache = ['exe', 'dmg', 'rpm', 'deb', 'AppImage']
+  const directCache = ['exe', 'dmg', 'rpm', 'deb', 'AppImage', 'nupkg']
   return directCache.includes(extension) ? (extension + arch) : false
 }
diff --git a/lib/routes.js b/lib/routes.js
index bbd6f14..4e0c2ab 100644
--- a/lib/routes.js
+++ b/lib/routes.js
@@ -87,6 +87,7 @@ module.exports = ({ cache, config }) => {
 
     // Get the latest version from the cache
     const latest = await loadCache()
+    console.log('CACHE: ', latest)
 
     // Check platform for appropiate aliases
     platform = checkAlias(platform)
@@ -159,14 +160,18 @@ module.exports = ({ cache, config }) => {
     if (compare(latest.version, version) !== 0) {
       const { notes, pub_date } = latest
 
-      send(res, 200, {
+      const sanitizedBaseUrl = url.startsWith('http') ? url : `https://${url}`
+
+      const responseData = {
         name: latest.version,
         notes,
         pub_date,
         url: shouldProxyPrivateDownload
-          ? `${url}/download/${platformName}?update=true`
+          ? `${sanitizedBaseUrl}/download/${platformName}?update=true`
           : latest.platforms[platform].url
-      })
+      }
+      console.log('RESPONSE DATA:', responseData)
+      send(res, 200, responseData)
 
       return
     }
@@ -175,25 +180,52 @@ module.exports = ({ cache, config }) => {
     res.end()
   }
 
-  exports.releases = async (req, res) => {
+  exports.squirrelWindows = async (req, res) => {
+    const { filename } = req.params
     // Get the latest version from the cache
     const latest = await loadCache()
 
-    if (!latest.files || !latest.files.RELEASES) {
-      res.statusCode = 204
-      res.end()
+    if (filename.toLowerCase().startsWith('releases')) {
+      if (!latest.files || !latest.files.RELEASES) {
+        res.statusCode = 204
+        res.end()
+  
+        return
+      }
+  
+      const content = latest.files.RELEASES
+  
+      console.log('RESPONSE DATA:', content)
+      res.writeHead(200, {
+        'content-length': Buffer.byteLength(content, 'utf8'),
+        'content-type': 'application/octet-stream'
+      })
+  
+      res.end(content)
+    } else if (filename.toLowerCase().endsWith('nupkg')) {
+      if (!latest.platforms || !latest.platforms['nupkg']) {
+        res.statusCode = 204
+        res.end()
+  
+        return
+      }
 
-      return
-    }
+      const nupkgAsset = latest.platforms['nupkg']
 
-    const content = latest.files.RELEASES
+      if (shouldProxyPrivateDownload) {
+        proxyPrivateDownload(nupkgAsset, req, res)
+        return
+      }
 
-    res.writeHead(200, {
-      'content-length': Buffer.byteLength(content, 'utf8'),
-      'content-type': 'application/octet-stream'
-    })
+      res.writeHead(302, {
+        Location: nupkgAsset.url
+      })
+      res.end()
 
-    res.end(content)
+    } else {
+      res.statusCode = 400
+      res.end()
+    }
   }

☝️ Disclaimer: This is a quick fix and it might or might not work for your case. For example, I think it breaks if you have a private repo and multiple nupkg rows in your RELEASES file.
It would be great to have an official solution by the maintainers for this, given that private repo support is advertised for this project.

from hazel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.