Skip to content

Commit

Permalink
add retry for file uploads in visual SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Logan Graham committed Oct 15, 2024
1 parent 0bb4a4d commit b1e6f88
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions visual-js/.changeset/modern-poets-hang.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
---
"@saucelabs/visual-playwright": minor
"@saucelabs/visual-storybook": minor
"@saucelabs/cypress-visual-plugin": minor
"@saucelabs/wdio-sauce-visual-service": minor
"@saucelabs/visual": minor
---

Unify result checking behavior into utility
Add automatic retry mechanism to file uploads for timeouts
16 changes: 11 additions & 5 deletions visual-js/visual/src/common/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
VisualApiRegion,
resolveRegionFromOndemandHostname,
} from './regions.js';
import { backOff } from 'exponential-backoff';

export * from '../graphql/__generated__/graphql.js';
export * from './selective-region.js';
Expand Down Expand Up @@ -157,11 +158,11 @@ const uploadToUrl = async ({
uploadTimeoutMs?: number;
}) => {
const uploadBody = isDataPath(file) ? fs.readFileSync(file.path) : file.data;

const timeout = uploadTimeoutMs ?? 10_000;
const hash = crypto.createHash('md5').update(uploadBody).digest('base64');
try {
uploadTimeoutMs ||= 10_000;
const result = await fetch(uploadUrl, {

const uploadItem = async () =>
await fetch(uploadUrl, {
method: 'PUT',
headers: {
'Content-Length': `${uploadBody.byteLength}`,
Expand All @@ -171,7 +172,12 @@ const uploadToUrl = async ({
body: uploadBody,
compress,
...fetchOptions,
signal: AbortSignal.timeout(uploadTimeoutMs),
signal: AbortSignal.timeout(timeout),
});

try {
const result = await backOff(uploadItem, {
numOfAttempts: 3,
});

if (!result.ok) {
Expand Down

0 comments on commit b1e6f88

Please sign in to comment.