diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index 62ca419..6202c0b 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -14,11 +14,10 @@ describe("github", () => { describe("asset", () => { it("derives asset info from a path", async () => { - const { name, mime, size, data } = asset("tests/data/foo/bar.txt"); + const { name, mime, size } = asset("tests/data/foo/bar.txt"); assert.equal(name, "bar.txt"); assert.equal(mime, "text/plain"); assert.equal(size, 10); - assert.equal(await text(data), "release me"); }); }); }); diff --git a/src/github.ts b/src/github.ts index 0014dab..d970132 100644 --- a/src/github.ts +++ b/src/github.ts @@ -1,6 +1,7 @@ import { GitHub } from "@actions/github/lib/utils"; import { Config, isTag, releaseBody, alignAssetName } from "./util"; -import { createReadStream, statSync, type ReadStream } from "fs"; +import { statSync } from "fs"; +import { open } from "fs/promises"; import { getType } from "mime"; import { basename } from "path"; @@ -10,7 +11,6 @@ export interface ReleaseAsset { name: string; mime: string; size: number; - data: ReadStream; } export interface Release { @@ -145,7 +145,6 @@ export const asset = (path: string): ReleaseAsset => { name: basename(path), mime: mimeOrDefault(path), size: statSync(path).size, - data: createReadStream(path, "binary"), }; }; @@ -161,7 +160,7 @@ export const upload = async ( currentAssets: Array<{ id: number; name: string }>, ): Promise => { const [owner, repo] = config.github_repository.split("/"); - const { name, size, mime, data: body } = asset(path); + const { name, mime, size } = asset(path); const currentAsset = currentAssets.find( // note: GitHub renames asset filenames that have special characters, non-alphanumeric characters, and leading or trailing periods. The "List release assets" endpoint lists the renamed filenames. // due to this renaming we need to be mindful when we compare the file name we're uploading with a name github may already have rewritten for logical comparison @@ -179,25 +178,30 @@ export const upload = async ( console.log(`⬆️ Uploading ${name}...`); const endpoint = new URL(url); endpoint.searchParams.append("name", name); - const resp = await github.request({ - method: "POST", - url: endpoint.toString(), - headers: { - "content-length": `${size}`, - "content-type": mime, - authorization: `token ${config.github_token}`, - }, - data: body, - }); - const json = resp.data; - if (resp.status !== 201) { - throw new Error( - `Failed to upload release asset ${name}. received status code ${ - resp.status - }\n${json.message}\n${JSON.stringify(json.errors)}`, - ); + const fh = await open(path); + try { + const resp = await github.request({ + method: "POST", + url: endpoint.toString(), + headers: { + "content-length": `${size}`, + "content-type": mime, + authorization: `token ${config.github_token}`, + }, + data: fh.readableWebStream({type: "bytes"}), + }); + const json = resp.data; + if (resp.status !== 201) { + throw new Error( + `Failed to upload release asset ${name}. received status code ${ + resp.status + }\n${json.message}\n${JSON.stringify(json.errors)}`, + ); + } + return json; + } finally { + await fh.close(); } - return json; }; export const release = async ( diff --git a/src/util.ts b/src/util.ts index 20d5c2d..08dcd21 100644 --- a/src/util.ts +++ b/src/util.ts @@ -112,4 +112,4 @@ export const isTag = (ref: string): boolean => { export const alignAssetName = (assetName: string): string => { return assetName.replace(/ /g, "."); -}; +}; \ No newline at end of file