Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { downloadBinary, findRelease } from "./release"
async function main() {
if (!(await isReady())) {
try {
const [name, assets] = await findRelease(VERSION)
const [name, asset_id, asset_filetype] = await findRelease(VERSION)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (not blocking at all), but just saying, we should use the convention of the language for naming variables and in JavaScript it's camelCase.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. I probably chose that name from the GitHub API call parameter where assetId is ultimately being used. Ideally I'd have passed the entire asset but I'm currently not clever enough for understanding how to either define or find out the proper typescript type for that..

console.info(`Downloading ${name}`)
await downloadBinary(assets.browser_download_url)
await downloadBinary(asset_id, asset_filetype)
} catch (e) {
console.error(`Failed to download binary:\n${e}`)
await fs.rm(COMBINED_PATH, { recursive: true })
Expand Down
31 changes: 25 additions & 6 deletions src/release.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { writeFile } from "node:fs/promises"
import os from "node:os"

import { Octokit } from "@octokit/rest"
import { createWriteStream } from "node:fs"
import { pipeline } from "node:stream/promises"
import { getProxyForUrl } from "proxy-from-env"
import { fetch, ProxyAgent } from "undici"
import type { RequestInit } from "undici"
Expand All @@ -24,15 +25,33 @@ export async function findRelease(version: string) {
if (!matchedAsset) {
throw new Error(`The binary '${releasePrefix}*' not found`)
}
return [release.data.name, matchedAsset] as const
return [
release.data.name,
matchedAsset.id,
matchedAsset.name.endsWith(".zip") ? "zip" : "tar",
] as const
}

export async function downloadBinary(url: string) {
const response = await proxiedFetch(url)
export async function downloadBinary(asset_id: number, asset_filetype: string) {
// downloading the asset is copied from https://github.com/octokit/rest.js/issues/12#issuecomment-1916023479
const asset = await octokit.repos.getReleaseAsset({
owner: NAME,
repo: NAME,
asset_id: asset_id,
headers: {
accept: "application/octet-stream",
},
request: {
parseSuccessResponseBody: false, // required to access response as stream
},
})
const tmpfile = await tmp.file()
await writeFile(tmpfile.path, Buffer.from(await response.arrayBuffer()))

if (url.endsWith(".zip")) {
const assetStream = asset.data as unknown as NodeJS.ReadableStream
const outputFile = createWriteStream(tmpfile.path)
await pipeline(assetStream, outputFile)

if (asset_filetype === ".zip") {
const zip = new admzip(tmpfile.path)
zip.extractAllTo(COMBINED_PATH, true)
} else {
Expand Down
Loading