From 64f1fa19ef459b1f7a022d7e159cb9131810c277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?W=C3=81NG=20Xu=C4=9Bru=C3=AC?= Date: Wed, 11 Dec 2024 10:19:13 +0800 Subject: [PATCH] feat: read the release assets asynchronously (#552) Previously all assets were being read synchronously into memory, making the action unsuitable for releasing very large assets. Because the client library allows stream body inputs (it just forwards it to the underlying `fetch` implementation), just do it. The idea is also suggested by @enumag in https://github.com/softprops/action-gh-release/issues/353#issuecomment-1793865790. Fixes: #353 Signed-off-by: WANG Xuerui --- __tests__/github.test.ts | 5 ++--- src/github.ts | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index 52c831dc8..62ca4195a 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -1,6 +1,5 @@ -//import * as assert from "assert"; -//const assert = require('assert'); import * as assert from "assert"; +import { text } from "stream/consumers"; import { mimeOrDefault, asset } from "../src/github"; describe("github", () => { @@ -19,7 +18,7 @@ describe("github", () => { assert.equal(name, "bar.txt"); assert.equal(mime, "text/plain"); assert.equal(size, 10); - assert.equal(data.toString(), "release me"); + assert.equal(await text(data), "release me"); }); }); }); diff --git a/src/github.ts b/src/github.ts index 24b0f8671..0014dab3d 100644 --- a/src/github.ts +++ b/src/github.ts @@ -1,6 +1,6 @@ import { GitHub } from "@actions/github/lib/utils"; import { Config, isTag, releaseBody, alignAssetName } from "./util"; -import { statSync, readFileSync } from "fs"; +import { createReadStream, statSync, type ReadStream } from "fs"; import { getType } from "mime"; import { basename } from "path"; @@ -10,7 +10,7 @@ export interface ReleaseAsset { name: string; mime: string; size: number; - data: Buffer; + data: ReadStream; } export interface Release { @@ -145,7 +145,7 @@ export const asset = (path: string): ReleaseAsset => { name: basename(path), mime: mimeOrDefault(path), size: statSync(path).size, - data: readFileSync(path), + data: createReadStream(path, "binary"), }; };