Last active
October 30, 2021 11:55
-
-
Save theolivenbaum/0002b1eaa53967ab84bca9b4fa4be3c0 to your computer and use it in GitHub Desktop.
CloudFlare worker to retrieve the latest release from a repository and return to your worker endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener("fetch", event => { | |
event.respondWith(fetchAndStream(event.request)) | |
}); | |
async function fetchAndStream() { | |
const init = { | |
headers: { | |
"User-Agent": "CloudFlare Worker/v42", | |
}, | |
}; | |
//Change here to match your repository and file extension | |
let repo = 'my-user/my-repo'; | |
let extension = '.exe'; | |
let latest = await fetch('https://api.github.com/repos/' + repo + '/releases/latest', init); | |
let jsonLatest = await latest.json(); | |
let assets = jsonLatest.assets; | |
let latestFile = assets.filter(a => a.browser_download_url.endsWith(extension))[0].browser_download_url; | |
let fileName = latestFile.substring(latestFile.lastIndexOf('/')+1); | |
let response = await fetch(latestFile,init); | |
const responseInit = { | |
headers: { | |
'Content-Type': 'application/octet-stream', | |
'Content-Disposition' : 'attachment; filename="' + fileName + '"' | |
} | |
}; | |
return new Response(response.body, responseInit) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment