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

Enhance timeout upload #126

Merged
merged 11 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
6 changes: 6 additions & 0 deletions visual-js/.changeset/afraid-balloons-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@saucelabs/visual": minor
"@saucelabs/visual-storybook": patch
---

Debugging info when upload is reaching timeout
46 changes: 32 additions & 14 deletions visual-js/visual/src/common/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,48 @@ const uploadToUrl = async ({
contentType,
file,
compress,
uploadTimeoutMs,
}: {
uploadUrl: string;
contentType: string;
file: DataContent | DataPath;
compress?: boolean;
uploadTimeoutMs?: number;
}) => {
const uploadBody = isDataPath(file) ? fs.readFileSync(file.path) : file.data;

const hash = crypto.createHash('md5').update(uploadBody).digest('base64');
try {
uploadTimeoutMs ||= 10_000;
const result = await fetch(uploadUrl, {
method: 'PUT',
headers: {
'Content-Length': `${uploadBody.byteLength}`,
'Content-Type': contentType,
'Content-MD5': hash,
},
body: uploadBody,
compress,
...fetchOptions,
signal: AbortSignal.timeout(uploadTimeoutMs),
});

const result = await fetch(uploadUrl, {
method: 'PUT',
headers: {
'Content-Length': `${uploadBody.byteLength}`,
'Content-Type': contentType,
'Content-MD5': hash,
},
body: uploadBody,
compress,
...fetchOptions,
});

if (!result.ok) {
throw new Error(`Failed to upload snapshot: ${result.statusText}`);
if (!result.ok) {
throw new Error(`Failed to upload snapshot: ${result.statusText}`);
}
} catch (ex: unknown) {
const isObject = (value: unknown): value is any =>
!!value && typeof value === 'object';
if (isObject(ex)) {
if (ex?.name === 'AbortError') {
FriggaHel marked this conversation as resolved.
Show resolved Hide resolved
console.error();
FriggaHel marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Uploading snapshot reached timeout.
Please check that you have connectivity and are able to do HTTP PUT requests.
URL: ${uploadUrl}
Note: This URL is valid only for a couple of minutes`);
}
}
throw new Error(`Upload failed with an unknown error: ${ex}`);
}
};

Expand Down
4 changes: 2 additions & 2 deletions visual-js/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2842,7 +2842,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@saucelabs/cypress-visual-plugin@workspace:visual-cypress"
dependencies:
"@saucelabs/visual": ^0.7.0
"@saucelabs/visual": ^0.7.3
"@tsconfig/node18": ^2.0.1
"@types/cypress": ^1.1.3
"@types/node": ^20.4.4
Expand Down Expand Up @@ -2920,7 +2920,7 @@ __metadata:
languageName: unknown
linkType: soft

"@saucelabs/visual@^0.7.0, @saucelabs/visual@workspace:visual":
"@saucelabs/visual@^0.7.0, @saucelabs/visual@^0.7.3, @saucelabs/visual@workspace:visual":
version: 0.0.0-use.local
resolution: "@saucelabs/visual@workspace:visual"
dependencies:
Expand Down