Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theolivenbaum/0002b1eaa53967ab84bca9b4fa4be3c0 to your computer and use it in GitHub Desktop.
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
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