1- import { writeFile } from "node:fs/promises"
21import os from "node:os"
32
43import { Octokit } from "@octokit/rest"
4+ import { createWriteStream } from "node:fs"
5+ import { pipeline } from "node:stream/promises"
56import { getProxyForUrl } from "proxy-from-env"
67import { fetch , ProxyAgent } from "undici"
78import 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