Skip to content

Commit 1304f6d

Browse files
authored
feat: download purely using octokit (#424)
1 parent a27f06d commit 1304f6d

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { downloadBinary, findRelease } from "./release"
99
async function main() {
1010
if (!(await isReady())) {
1111
try {
12-
const [name, assets] = await findRelease(VERSION)
12+
const [name, assetId, assetFiletype] = await findRelease(VERSION)
1313
console.info(`Downloading ${name}`)
14-
await downloadBinary(assets.browser_download_url)
14+
await downloadBinary(assetId, assetFiletype)
1515
} catch (e) {
1616
console.error(`Failed to download binary:\n${e}`)
1717
await fs.rm(COMBINED_PATH, { recursive: true })

src/release.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { writeFile } from "node:fs/promises"
21
import os from "node:os"
32

43
import { Octokit } from "@octokit/rest"
4+
import { createWriteStream } from "node:fs"
5+
import { pipeline } from "node:stream/promises"
56
import { getProxyForUrl } from "proxy-from-env"
67
import { fetch, ProxyAgent } from "undici"
78
import type { RequestInit } from "undici"
@@ -24,15 +25,33 @@ export async function findRelease(version: string) {
2425
if (!matchedAsset) {
2526
throw new Error(`The binary '${releasePrefix}*' not found`)
2627
}
27-
return [release.data.name, matchedAsset] as const
28+
return [
29+
release.data.name,
30+
matchedAsset.id,
31+
matchedAsset.name.endsWith(".zip") ? "zip" : "tar",
32+
] as const
2833
}
2934

30-
export async function downloadBinary(url: string) {
31-
const response = await proxiedFetch(url)
35+
export async function downloadBinary(assetId: number, assetFiletype: string) {
36+
// downloading the asset is copied from https://github.com/octokit/rest.js/issues/12#issuecomment-1916023479
37+
const asset = await octokit.repos.getReleaseAsset({
38+
owner: NAME,
39+
repo: NAME,
40+
asset_id: assetId,
41+
headers: {
42+
accept: "application/octet-stream",
43+
},
44+
request: {
45+
parseSuccessResponseBody: false, // required to access response as stream
46+
},
47+
})
3248
const tmpfile = await tmp.file()
33-
await writeFile(tmpfile.path, Buffer.from(await response.arrayBuffer()))
3449

35-
if (url.endsWith(".zip")) {
50+
const assetStream = asset.data as unknown as NodeJS.ReadableStream
51+
const outputFile = createWriteStream(tmpfile.path)
52+
await pipeline(assetStream, outputFile)
53+
54+
if (assetFiletype === ".zip") {
3655
const zip = new admzip(tmpfile.path)
3756
zip.extractAllTo(COMBINED_PATH, true)
3857
} else {

0 commit comments

Comments
 (0)