Skip to content

Commit 8fe9058

Browse files
committed
feat: download purely using octokit
In my understanding octokit automatically uses authentication, but the real download was done using fetch directly, hence without authentication. This approach now uses octokit's getReleaseAsset() to download the asset, which should use proper authentication, hence allowing potentially higher rate limits. Closes: #419
1 parent a27f06d commit 8fe9058

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-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, asset_id, asset_filetype] = await findRelease(VERSION)
1313
console.info(`Downloading ${name}`)
14-
await downloadBinary(assets.browser_download_url)
14+
await downloadBinary(asset_id, asset_filetype)
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: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { writeFile } from "node:fs/promises"
21
import os from "node:os"
32

43
import { Octokit } from "@octokit/rest"
4+
import { Endpoints } from "@octokit/types";
5+
import { createWriteStream } from "node:fs";
6+
import { pipeline } from "node:stream/promises";
57
import { getProxyForUrl } from "proxy-from-env"
68
import { fetch, ProxyAgent } from "undici"
79
import type { RequestInit } from "undici"
@@ -24,15 +26,29 @@ export async function findRelease(version: string) {
2426
if (!matchedAsset) {
2527
throw new Error(`The binary '${releasePrefix}*' not found`)
2628
}
27-
return [release.data.name, matchedAsset] as const
29+
return [release.data.name, matchedAsset.id, matchedAsset.name.endsWith(".zip") ? "zip" : "tar" ] as const
2830
}
2931

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

35-
if (url.endsWith(".zip")) {
47+
const assetStream = asset.data as unknown as NodeJS.ReadableStream;
48+
const outputFile = createWriteStream(tmpfile.path);
49+
await pipeline(assetStream, outputFile);
50+
51+
if (asset_filetype === ".zip") {
3652
const zip = new admzip(tmpfile.path)
3753
zip.extractAllTo(COMBINED_PATH, true)
3854
} else {

0 commit comments

Comments
 (0)