Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: use proper content types for uploads #103

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions packages/cypress-cloud/lib/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ScreenshotArtifact, ScreenshotUploadInstruction } from "../types";
import { updateInstanceStdout } from "./api";
import { safe } from "./lang";
import { warn } from "./log";
import { uploadFile } from "./upload";
import { uploadImage, uploadVideo } from "./upload";
const debug = Debug("currents:artifacts");
interface UploadArtifacts {
videoPath: string | null;
Expand Down Expand Up @@ -35,11 +35,9 @@ export async function uploadArtifacts({
// upload video
if (videoUploadUrl && videoPath) {
await safe(
uploadFile,
// () => info("- Failed Uploading", red(videoPath)),
// () => info("- Done Uploading", cyan(videoPath))
() => {},
() => {}
uploadVideo,
(e) => debug("failed uploading video %s. Error: %o", videoPath, e),
() => debug("success uploading", videoPath)
)(videoPath, videoUploadUrl);
}
// upload screenshots
Expand All @@ -59,11 +57,14 @@ export async function uploadArtifacts({
return Promise.resolve();
}
return safe(
uploadFile,
// () => warn("- Failed Uploading", red(screenshot.path)),
// () => info("- Done Uploading", cyan(screenshot.path))
() => {},
() => {}
uploadImage,
(e) =>
debug(
"failed uploading screenshot %s. Error: %o",
screenshot.path,
e
),
() => debug("success uploading", screenshot.path)
)(screenshot.path, url);
})
);
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress-cloud/lib/httpClient/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export function getClient() {
const req = {
...config,
headers: {
"Content-Type": "application/json",
...config.headers,

// @ts-ignore
"x-cypress-request-attempt": config["axios-retry"]?.retryCount ?? 0,
"x-cypress-run-id": _runId,
"x-cypress-version": _cypressVersion,
"x-ccy-version": _currentsVersion ?? "0.0.0",
"Content-Type": "application/json",
},
};
debug("network request: %o", req);
Expand Down
4 changes: 2 additions & 2 deletions packages/cypress-cloud/lib/lang.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const safe =
<T extends any[], R extends any>(
fn: (...args: T) => Promise<R>,
ifFaled: () => any,
ifFaled: (e: unknown) => any,
ifSucceed: () => any
) =>
async (...args: T) => {
Expand All @@ -10,6 +10,6 @@ export const safe =
ifSucceed();
return r;
} catch (e) {
ifFaled();
ifFaled(e);
}
};
14 changes: 13 additions & 1 deletion packages/cypress-cloud/lib/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ import { makeRequest } from "./httpClient";
const readFile = fs.promises.readFile;
const debug = Debug("currents:upload");

export async function uploadFile(file: string, url: string) {
export function uploadVideo(file: string, url: string) {
return uploadFile(file, url, "application/octet-stream");
}

export function uploadImage(file: string, url: string) {
return uploadFile(file, url, "image/png");
}

type UploadTypes = "application/octet-stream" | "image/png" | "plain/text";
async function uploadFile(file: string, url: string, type: UploadTypes) {
debug('uploading file "%s" to "%s"', file, url);
const f = await readFile(file);
await makeRequest({
url,
method: "PUT",
data: f,
headers: {
"Content-Type": type,
},
});
}