fix: big file uploads (#562)
* 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 <git@xen0n.name> * feat: log when each asset is successfully uploaded Signed-off-by: WANG Xuerui <git@xen0n.name> * build: refresh dist Signed-off-by: WANG Xuerui <git@xen0n.name> * style: format with prettier Signed-off-by: WANG Xuerui <git@xen0n.name> --------- Signed-off-by: WANG Xuerui <git@xen0n.name>
This commit is contained in:
parent
33fcd69d45
commit
deddb09c64
@ -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
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
@ -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<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
|
||||
@ -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 (
|
||||
|
Loading…
x
Reference in New Issue
Block a user