Skip to content

Commit 5f4fdab

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 5f4fdab

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-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: 21 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,29 @@ 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 [release.data.name, matchedAsset.id, matchedAsset.name.endsWith(".zip") ? "zip" : "tar" ] as const
2829
}
2930

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

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

0 commit comments

Comments
 (0)