Compare commits

...

19 Commits

Author SHA1 Message Date
Dave Hadka
2a973a0f4e Add comment for SocketTimeout 2020-05-11 14:32:38 -04:00
Dave Hadka
cbbb8b4d4f Fix lint issue, build .js files 2020-05-11 14:31:13 -04:00
Dave Hadka
5a0add1806 Adds socket timeout and validate file size 2020-05-11 14:28:28 -04:00
Aiqiao Yan
9fe7ad8b07 Use path.sep in path replace 2020-05-11 14:20:33 -04:00
Aiqiao Yan
7c7d003bbb Rebase and rebuild 2020-05-11 14:08:26 -04:00
Aiqiao Yan
96e5a46c57 Fix test 2020-05-11 14:08:26 -04:00
Aiqiao Yan
84e606dfac Fallback to GNU tar if BSD tar is unavailable 2020-05-11 14:08:23 -04:00
Josh Gross
70655ec832 Release v1.1.2 2020-02-05 10:41:57 -05:00
Josh Gross
fe1055e9d1 Release v1.1.1 2020-02-05 10:01:01 -05:00
Josh Gross
a505c2e7a6 Merge branch 'master' into releases/v1 2020-01-06 14:10:16 -05:00
Josh Gross
10a14413e7 Update release binaries 2020-01-06 13:51:23 -05:00
Josh Gross
cf4f44db70 Fix invalid array 2020-01-06 13:50:39 -05:00
Josh Gross
4c4974aff1 Release v1.1 2020-01-06 13:36:33 -05:00
Josh Gross
cffae9552b Release v1.0.3 2019-11-21 14:57:29 -05:00
Josh Gross
44543250bd Release 1.0.2 2019-11-15 10:31:02 -05:00
Josh Gross
6491e51b66 Merge master into releases/v1 2019-11-15 10:29:58 -05:00
Josh Gross
86dff562ab v1.0.1 release binaries 2019-11-05 15:43:33 -05:00
Josh Gross
0f810ad45a Release v1.0.1 2019-11-05 15:42:18 -05:00
Josh Gross
9d8c7b4041 Release v1 2019-11-04 15:13:15 -05:00
6 changed files with 6354 additions and 18 deletions

View File

@@ -2,6 +2,8 @@ import * as exec from "@actions/exec";
import * as io from "@actions/io";
import * as tar from "../src/tar";
import fs = require("fs");
jest.mock("@actions/exec");
jest.mock("@actions/io");
@@ -11,17 +13,19 @@ beforeAll(() => {
});
});
test("extract tar", async () => {
test("extract BSD tar", async () => {
const mkdirMock = jest.spyOn(io, "mkdirP");
const execMock = jest.spyOn(exec, "exec");
const archivePath = "cache.tar";
const IS_WINDOWS = process.platform === "win32";
const archivePath = IS_WINDOWS
? `${process.env["windir"]}\\fakepath\\cache.tar`
: "cache.tar";
const targetDirectory = "~/.npm/cache";
await tar.extractTar(archivePath, targetDirectory);
expect(mkdirMock).toHaveBeenCalledWith(targetDirectory);
const IS_WINDOWS = process.platform === "win32";
const tarPath = IS_WINDOWS
? `${process.env["windir"]}\\System32\\tar.exe`
: "tar";
@@ -29,13 +33,37 @@ test("extract tar", async () => {
expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
"-xz",
"-f",
archivePath,
IS_WINDOWS ? archivePath.replace(/\\/g, "/") : archivePath,
"-C",
targetDirectory
IS_WINDOWS ? targetDirectory?.replace(/\\/g, "/") : targetDirectory
]);
});
test("create tar", async () => {
test("extract GNU tar", async () => {
const IS_WINDOWS = process.platform === "win32";
if (IS_WINDOWS) {
jest.spyOn(fs, "existsSync").mockReturnValueOnce(false);
jest.spyOn(tar, "isGnuTar").mockReturnValue(Promise.resolve(true));
const execMock = jest.spyOn(exec, "exec");
const archivePath = `${process.env["windir"]}\\fakepath\\cache.tar`;
const targetDirectory = "~/.npm/cache";
await tar.extractTar(archivePath, targetDirectory);
expect(execMock).toHaveBeenCalledTimes(2);
expect(execMock).toHaveBeenLastCalledWith(`"tar"`, [
"-xz",
"-f",
archivePath.replace(/\\/g, "/"),
"-C",
targetDirectory?.replace(/\\/g, "/"),
"--force-local"
]);
}
});
test("create BSD tar", async () => {
const execMock = jest.spyOn(exec, "exec");
const archivePath = "cache.tar";
@@ -50,9 +78,9 @@ test("create tar", async () => {
expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
"-cz",
"-f",
archivePath,
IS_WINDOWS ? archivePath.replace(/\\/g, "/") : archivePath,
"-C",
sourceDirectory,
IS_WINDOWS ? sourceDirectory?.replace(/\\/g, "/") : sourceDirectory,
"."
]);
});

3143
dist/restore/index.js vendored Normal file

File diff suppressed because it is too large Load Diff

3104
dist/save/index.js vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -7,6 +7,8 @@ import {
IRequestOptions,
ITypedResponse
} from "@actions/http-client/interfaces";
import { SocketTimeout } from "./constants";
import {
ArtifactCacheEntry,
CommitCacheRequest,
@@ -123,7 +125,33 @@ export async function downloadCache(
const stream = fs.createWriteStream(archivePath);
const httpClient = new HttpClient("actions/cache");
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);
// Validate download size.
const 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

View File

@@ -18,3 +18,8 @@ export enum Events {
Push = "push",
PullRequest = "pull_request"
}
// Socket timeout in milliseconds during download. If no traffic is received
// over the socket during this period, the socket is destroyed and the download
// is aborted.
export const SocketTimeout = 5000;

View File

@@ -1,14 +1,35 @@
import * as core from "@actions/core";
import { exec } from "@actions/exec";
import * as io from "@actions/io";
import { existsSync } from "fs";
import * as path from "path";
async function getTarPath(): Promise<string> {
export async function isGnuTar(): Promise<boolean> {
core.debug("Checking tar --version");
let versionOutput = "";
await exec("tar --version", [], {
ignoreReturnCode: true,
silent: true,
listeners: {
stdout: (data: Buffer): string =>
(versionOutput += data.toString()),
stderr: (data: Buffer): string => (versionOutput += data.toString())
}
});
core.debug(versionOutput.trim());
return versionOutput.toUpperCase().includes("GNU TAR");
}
async function getTarPath(args: string[]): Promise<string> {
// Explicitly use BSD Tar on Windows
const IS_WINDOWS = process.platform === "win32";
if (IS_WINDOWS) {
const systemTar = `${process.env["windir"]}\\System32\\tar.exe`;
if (existsSync(systemTar)) {
return systemTar;
} else if (await isGnuTar()) {
args.push("--force-local");
}
}
return await io.which("tar", true);
@@ -16,14 +37,8 @@ async function getTarPath(): Promise<string> {
async function execTar(args: string[]): Promise<void> {
try {
await exec(`"${await getTarPath()}"`, args);
await exec(`"${await getTarPath(args)}"`, args);
} catch (error) {
const IS_WINDOWS = process.platform === "win32";
if (IS_WINDOWS) {
throw new Error(
`Tar failed with error: ${error?.message}. Ensure BSD tar is installed and on the PATH.`
);
}
throw new Error(`Tar failed with error: ${error?.message}`);
}
}
@@ -34,7 +49,13 @@ export async function extractTar(
): Promise<void> {
// Create directory to extract tar into
await io.mkdirP(targetDirectory);
const args = ["-xz", "-f", archivePath, "-C", targetDirectory];
const args = [
"-xz",
"-f",
archivePath.replace(new RegExp("\\" + path.sep, "g"), "/"),
"-C",
targetDirectory.replace(new RegExp("\\" + path.sep, "g"), "/")
];
await execTar(args);
}
@@ -42,6 +63,13 @@ export async function createTar(
archivePath: string,
sourceDirectory: string
): Promise<void> {
const args = ["-cz", "-f", archivePath, "-C", sourceDirectory, "."];
const args = [
"-cz",
"-f",
archivePath.replace(new RegExp("\\" + path.sep, "g"), "/"),
"-C",
sourceDirectory.replace(new RegExp("\\" + path.sep, "g"), "/"),
"."
];
await execTar(args);
}