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

add possibility to download asset from private repo #18

Closed
Closed
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
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"workbench.colorCustomizations": {
"activityBar.background": "#013433",
"titleBar.activeBackground": "#014947",
"titleBar.activeForeground": "#E9FFFE"
}
}
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 44 additions & 24 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ const SendJSON = (data: Record<string, unknown>) => {
const responses = {
NotFound: () => new Response('Not found', { status: 404 }),
NoContent: () => new Response(null, { status: 204 }),
SendUpdate: (data: TauriUpdateResponse) =>
SendJSON(data),
SendJSON

SendUpdate: (data: TauriUpdateResponse) => SendJSON(data),
SendJSON,
}

type RequestPathParts = [
Expand Down Expand Up @@ -65,9 +63,15 @@ const handleV1Request = async (request: Request) => {
return responses.NotFound()
}

const signature = await findAssetSignature(match.name, release.assets)
const proxy = GITHUB_TOKEN?.length;
const downloadURL = proxy ? createProxiedFileUrl(match.browser_download_url, request) : match.browser_download_url
const signature = await findAssetSignature(
match.name,
release.assets,
request,
)
const proxy = GITHUB_TOKEN?.length
const downloadURL = proxy
? createProxiedFileUrl(match.browser_download_url, request)
: match.browser_download_url
const data: TauriUpdateResponse = {
url: downloadURL,
version: remoteVersion,
Expand All @@ -80,10 +84,10 @@ const handleV1Request = async (request: Request) => {
}

const createProxiedFileUrl = (downloadURL: string, request: Request) => {

const fileName = downloadURL.split('/')?.at(-1)
if (!fileName) { throw new Error('Could not get file name from download URL') }

if (!fileName) {
throw new Error('Could not get file name from download URL')
}

const path = new URL(request.url)
const root = `${path.protocol}//${path.host}`
Expand All @@ -93,28 +97,44 @@ const createProxiedFileUrl = (downloadURL: string, request: Request) => {

const getLatestAssets = async (request: Request) => {
const fileName = request.url.split('/')?.at(-1)
if (!fileName) { throw new Error('Could not get file name from download URL') }

if (!fileName) {
throw new Error('Could not get file name from download URL')
}
const release = await getLatestRelease(request)
const downloadPath = release.assets.find(({ name }) => name === fileName)?.browser_download_url
const asset = release.assets.find(({ name }) => name === fileName)

if (!downloadPath) { throw new Error('Could not get file path from download URL') }

const { readable, writable } = new TransformStream();
const file_response = await fetch(downloadPath, {
method: 'GET',
redirect: 'follow'
})

file_response?.body?.pipeTo(writable);
return new Response(readable, file_response);
if (!asset) {
throw new Error('Could not get file path from download URL')
}

const { readable, writable } = new TransformStream()

let file_response
if (GITHUB_TOKEN?.length) {
const headers = new Headers({
Accept: 'application/octet-stream',
'User-Agent': request.headers.get('User-Agent') as string,
Authorization: `token ${GITHUB_TOKEN}`,
'X-GitHub-Api-Version': '2022-11-28',
})
file_response = await fetch(asset.url, {
method: 'GET',
redirect: 'follow',
headers,
})
} else {
file_response = await fetch(asset.browser_download_url, {
method: 'GET',
redirect: 'follow',
})
}
file_response?.body?.pipeTo(writable)
return new Response(readable, file_response)
}

export async function handleRequest(request: Request): Promise<Response> {
const path = new URL(request.url).pathname


if (path.includes('/latest')) {
return getLatestAssets(request)
}
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const handleLegacyRequest = async (
}

// try to find signature for this asset
const signature = await findAssetSignature(name, release.assets)
const signature = await findAssetSignature(name, release.assets, request)
const data: TauriUpdateResponse = {
url: browser_download_url,
version: remoteVersion,
Expand Down
32 changes: 27 additions & 5 deletions src/services/github.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export type Asset = { name: string; browser_download_url: string }
export type Asset = {
name: string
browser_download_url: string
url: string
id: number
label: string
}
export const getReleases = async (request: Request): Promise<Response> => {
const reqUrl = new URL(
`https://api.github.com/repos/${GITHUB_ACCOUNT}/${GITHUB_REPO}/releases/latest`,
Expand All @@ -8,7 +14,8 @@ export const getReleases = async (request: Request): Promise<Response> => {
'User-Agent': request.headers.get('User-Agent') as string,
})

if (GITHUB_TOKEN?.length) headers.set('Authorization', `token ${GITHUB_TOKEN}`)
if (GITHUB_TOKEN?.length)
headers.set('Authorization', `token ${GITHUB_TOKEN}`)

return await fetch(reqUrl.toString(), {
method: 'GET',
Expand All @@ -32,19 +39,34 @@ export const getLatestRelease = async (request: Request): Promise<Release> => {
export async function findAssetSignature(
fileName: string,
assets: Asset[],
request: Request,
): Promise<string | undefined> {
// check in our assets if we have a file: `fileName.sig`
// by example fileName can be: App-1.0.0.zip

const foundSignature = assets.find(
(asset) => asset.name.toLowerCase() === `${fileName.toLowerCase()}.sig`,
)

if (!foundSignature) {
return undefined
}

const response = await fetch(foundSignature.browser_download_url)
let response
if (GITHUB_TOKEN?.length) {
const headers = new Headers({
Accept: 'application/octet-stream',
Authorization: `Bearer ${GITHUB_TOKEN}`,
'User-Agent': request.headers.get('User-Agent') as string,
'X-GitHub-Api-Version': '2022-11-28',
})
response = await fetch(foundSignature.url, {
method: 'GET',
redirect: 'follow',
headers,
})
console.log(foundSignature.url, response.status)
} else {
response = await fetch(foundSignature.browser_download_url)
}
if (response.status !== 200) {
return undefined
}
Expand Down