1
0
Fork 0
mirror of https://code.forgejo.org/actions/cache.git synced 2024-12-23 12:36:01 +01:00

Adds socket timeout and validate file size

This commit is contained in:
Dave Hadka 2020-04-22 18:23:41 -04:00 committed by Aiqiao Yan
parent 9fe7ad8b07
commit 5a0add1806
2 changed files with 30 additions and 0 deletions

View file

@ -7,6 +7,8 @@ import {
IRequestOptions, IRequestOptions,
ITypedResponse ITypedResponse
} from "@actions/http-client/interfaces"; } from "@actions/http-client/interfaces";
import { SocketTimeout } from "./constants";
import { import {
ArtifactCacheEntry, ArtifactCacheEntry,
CommitCacheRequest, CommitCacheRequest,
@ -123,7 +125,33 @@ export async function downloadCache(
const stream = fs.createWriteStream(archivePath); const stream = fs.createWriteStream(archivePath);
const httpClient = new HttpClient("actions/cache"); const httpClient = new HttpClient("actions/cache");
const downloadResponse = await httpClient.get(archiveLocation); const downloadResponse = await httpClient.get(archiveLocation);
// Abort download if no traffic received over the socket.
downloadResponse.message.socket.setTimeout(SocketTimeout, () => {
downloadResponse.message.destroy();
core.debug(
`Aborting download, socket timed out after ${SocketTimeout} ms`
);
});
await pipeResponseToStream(downloadResponse, stream); await pipeResponseToStream(downloadResponse, stream);
// Validate download size.
var contentLengthHeader =
downloadResponse.message.headers["content-length"];
if (contentLengthHeader) {
const expectedLength = parseInt(contentLengthHeader);
const actualLength = utils.getArchiveFileSize(archivePath);
if (actualLength != expectedLength) {
throw new Error(
`Incomplete download. Expected file size: ${expectedLength}, actual file size: ${actualLength}`
);
}
} else {
core.debug("Unable to validate download, no Content-Length header");
}
} }
// Reserve Cache // Reserve Cache

View file

@ -18,3 +18,5 @@ export enum Events {
Push = "push", Push = "push",
PullRequest = "pull_request" PullRequest = "pull_request"
} }
export const SocketTimeout = 5000;