Skip to content

Commit

Permalink
fix: big file uploads (#562)
Browse files Browse the repository at this point in the history
* fix: use readableWebStream() to stream asset contents

This allows the uploads to finish without mismatched Content-Length,
likely because the original method implied a wrong body encoding or
something similar. Unfortunately a GitHub server API mock was not
readily available so I had to test manually with a barebones repository.

Fixes: #555
Fixes: #556
Signed-off-by: WANG Xuerui <[email protected]>

* feat: log when each asset is successfully uploaded

Signed-off-by: WANG Xuerui <[email protected]>

* build: refresh dist

Signed-off-by: WANG Xuerui <[email protected]>

* style: format with prettier

Signed-off-by: WANG Xuerui <[email protected]>

---------

Signed-off-by: WANG Xuerui <[email protected]>
  • Loading branch information
xen0n authored Jan 7, 2025
1 parent 33fcd69 commit deddb09
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
3 changes: 1 addition & 2 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
});
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

49 changes: 27 additions & 22 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -10,7 +11,6 @@ export interface ReleaseAsset {
name: string;
mime: string;
size: number;
data: ReadStream;
}

export interface Release {
Expand Down Expand Up @@ -145,7 +145,6 @@ export const asset = (path: string): ReleaseAsset => {
name: basename(path),
mime: mimeOrDefault(path),
size: statSync(path).size,
data: createReadStream(path, "binary"),
};
};

Expand All @@ -161,7 +160,7 @@ export const upload = async (
currentAssets: Array<{ id: number; name: string }>,
): Promise<any> => {
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
Expand All @@ -179,25 +178,31 @@ 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)}`,
);
}
console.log(`✅ Uploaded ${name}`);
return json;
} finally {
await fh.close();
}
return json;
};

export const release = async (
Expand Down

0 comments on commit deddb09

Please sign in to comment.