Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
25 changes: 22 additions & 3 deletions packages/cli-kit/src/public/node/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {readFile} from './fs.js'
import {isExecutable} from 'is-executable'
import {describe, expect, test, vi} from 'vitest'
import {Response} from 'node-fetch'
import {Readable} from 'stream'

vi.mock('./http.js')

Expand Down Expand Up @@ -180,10 +181,10 @@ describe('downloadGitHubRelease', () => {
testWithTempDir('successfully downloads the release asset', async ({tempDir}) => {
// GIVEN
const downloadContent = 'hello'
const content = Buffer.from(downloadContent)
const content = Readable.from(downloadContent)
const mockResponse = {
ok: true,
arrayBuffer: vi.fn().mockResolvedValue(content),
body: content,
}
vi.mocked(fetch).mockResolvedValue(mockResponse as any)

Expand Down Expand Up @@ -221,7 +222,25 @@ describe('downloadGitHubRelease', () => {

testWithTempDir('throws an AbortError when the response is not ok', async ({tempDir}) => {
// GIVEN
vi.mocked(downloadFile).mockRejectedValue(new Error('Not Found'))
vi.mocked(fetch).mockResolvedValue({
ok: false,
statusText: 'Not Found',
} as any)
const targetPath = joinPath(tempDir, 'downloads', 'example')

// WHEN
const result = downloadGitHubRelease(repo, version, asset, targetPath)

// THEN
await expect(result).rejects.toThrow(AbortError)
})

testWithTempDir('throws an AbortError when the response body is missing', async ({tempDir}) => {
// GIVEN
vi.mocked(fetch).mockResolvedValue({
ok: true,
body: null,
} as any)
const targetPath = joinPath(tempDir, 'downloads', 'example')

// WHEN
Expand Down
9 changes: 6 additions & 3 deletions packages/cli-kit/src/public/node/github.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {outputContent, outputDebug, outputToken} from './output.js'
import {err, ok, Result} from './result.js'
import {fetch, Response} from './http.js'
import {writeFile, mkdir, inTemporaryDirectory, moveFile, chmod} from './fs.js'
import {mkdir, inTemporaryDirectory, moveFile, chmod, createFileWriteStream} from './fs.js'
import {dirname, joinPath} from './path.js'
import {runWithTimer} from './metadata.js'
import {AbortError} from './error.js'
import {pipeline} from 'stream/promises'

class GitHubClientError extends Error {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -162,8 +163,10 @@ export async function downloadGitHubRelease(
)
}

const buffer = await response.arrayBuffer()
await writeFile(tempPath, Buffer.from(buffer))
if (!response.body) {
throw new AbortError(`Failed to download ${assetName}: No response body`)
}
await pipeline(response.body, createFileWriteStream(tempPath))

await chmod(tempPath, 0o755)
await mkdir(dirname(targetPath))
Expand Down
Loading