feat: read the release assets asynchronously

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 <git@xen0n.name>
This commit is contained in:
WANG Xuerui 2024-12-06 10:58:59 +08:00
parent 92bc83c421
commit 6b394ae0f8
No known key found for this signature in database
GPG key ID: 99797B0EB13A6337
2 changed files with 5 additions and 6 deletions

View file

@ -1,6 +1,5 @@
//import * as assert from "assert";
//const assert = require('assert');
import * as assert from "assert"; import * as assert from "assert";
import { text } from "stream/consumers";
import { mimeOrDefault, asset } from "../src/github"; import { mimeOrDefault, asset } from "../src/github";
describe("github", () => { describe("github", () => {
@ -19,7 +18,7 @@ describe("github", () => {
assert.equal(name, "bar.txt"); assert.equal(name, "bar.txt");
assert.equal(mime, "text/plain"); assert.equal(mime, "text/plain");
assert.equal(size, 10); assert.equal(size, 10);
assert.equal(data.toString(), "release me"); assert.equal(await text(data), "release me");
}); });
}); });
}); });

View file

@ -1,6 +1,6 @@
import { GitHub } from "@actions/github/lib/utils"; import { GitHub } from "@actions/github/lib/utils";
import { Config, isTag, releaseBody, alignAssetName } from "./util"; import { Config, isTag, releaseBody, alignAssetName } from "./util";
import { statSync, readFileSync } from "fs"; import { createReadStream, statSync, type ReadStream } from "fs";
import { getType } from "mime"; import { getType } from "mime";
import { basename } from "path"; import { basename } from "path";
@ -10,7 +10,7 @@ export interface ReleaseAsset {
name: string; name: string;
mime: string; mime: string;
size: number; size: number;
data: Buffer; data: ReadStream;
} }
export interface Release { export interface Release {
@ -145,7 +145,7 @@ export const asset = (path: string): ReleaseAsset => {
name: basename(path), name: basename(path),
mime: mimeOrDefault(path), mime: mimeOrDefault(path),
size: statSync(path).size, size: statSync(path).size,
data: readFileSync(path), data: createReadStream(path, "binary"),
}; };
}; };