Skip to content

Commit

Permalink
fix: video download issue on lambdatest (#238)
Browse files Browse the repository at this point in the history
* fix: video download issue on lambdatest

* chore: added changeset
  • Loading branch information
pushpam5 authored Dec 9, 2024
1 parent 2d64a85 commit 4074400
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 102 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-eagles-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"appwright": patch
---

fix: video download issue on lambdatest
109 changes: 58 additions & 51 deletions src/providers/browserstack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,62 +198,69 @@ export class BrowserStackDeviceProvider implements DeviceProvider {
*/
await new Promise((resolve) => setTimeout(resolve, 10_000));
const fileStream = fs.createWriteStream(tempPathForWriting);
if (videoURL) {
await retry(
async () => {
const response = await fetch(videoURL, {
method: "GET",
});
if (response.status !== 200) {
// Retry if not 200
throw new Error(
`Video not found: ${response.status} (URL: ${videoURL})`,
);
}
const reader = response.body?.getReader();
if (!reader) {
throw new Error("Failed to get reader from response body.");
}
const streamToFile = async () => {
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();
if (done) break;
fileStream.write(value);
try {
if (videoURL) {
await retry(
async () => {
const response = await fetch(videoURL, {
method: "GET",
});
if (response.status !== 200) {
// Retry if not 200
throw new Error(
`Video not found: ${response.status} (URL: ${videoURL})`,
);
}
};
await streamToFile();
fileStream.close();
},
{
retries: 10,
minTimeout: 3_000,
onRetry: (err, i) => {
if (i > 5) {
logger.warn(`Retry attempt ${i} failed: ${err.message}`);
const reader = response.body?.getReader();
if (!reader) {
throw new Error("Failed to get reader from response body.");
}
const streamToFile = async () => {
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();
if (done) break;
fileStream.write(value);
}
};
await streamToFile();
fileStream.close();
},
},
);
return new Promise((resolve, reject) => {
// Ensure file stream is closed even in case of an error
fileStream.on("finish", () => {
try {
fs.renameSync(tempPathForWriting, pathToTestVideo);
logger.log(`Download finished and file closed: ${pathToTestVideo}`);
resolve({ path: pathToTestVideo, contentType: "video/mp4" });
} catch (err) {
logger.error(`Failed to rename file: `, err);
reject(err);
}
});
{
retries: 10,
minTimeout: 3_000,
onRetry: (err, i) => {
if (i > 5) {
logger.warn(`Retry attempt ${i} failed: ${err.message}`);
}
},
},
);
return new Promise((resolve, reject) => {
// Ensure file stream is closed even in case of an error
fileStream.on("finish", () => {
try {
fs.renameSync(tempPathForWriting, pathToTestVideo);
logger.log(
`Download finished and file closed: ${pathToTestVideo}`,
);
resolve({ path: pathToTestVideo, contentType: "video/mp4" });
} catch (err) {
logger.error(`Failed to rename file: `, err);
reject(err);
}
});

fileStream.on("error", (err) => {
logger.error(`Failed to write file: ${err.message}`);
reject(err);
fileStream.on("error", (err) => {
logger.error(`Failed to write file: ${err.message}`);
reject(err);
});
});
});
} else {
} else {
return null;
}
} catch (e) {
logger.log(`Error Downloading video: `, e);
return null;
}
}
Expand Down
109 changes: 58 additions & 51 deletions src/providers/lambdatest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,62 +181,69 @@ export class LambdaTestDeviceProvider implements DeviceProvider {
const dir = path.dirname(pathToTestVideo);
fs.mkdirSync(dir, { recursive: true });
const fileStream = fs.createWriteStream(tempPathForWriting);
if (videoURL) {
await retry(
async () => {
const response = await fetch(videoURL, {
method: "GET",
});
if (response.status !== 200) {
// Retry if not 200
throw new Error(
`Video not found: ${response.status} (URL: ${videoURL})`,
);
}
const reader = response.body?.getReader();
if (!reader) {
throw new Error("Failed to get reader from response body.");
}
const streamToFile = async () => {
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();
if (done) break;
fileStream.write(value);
try {
if (videoURL) {
await retry(
async () => {
const response = await fetch(videoURL, {
method: "GET",
});
if (response.status !== 200) {
// Retry if not 200
throw new Error(
`Video not found: ${response.status} (URL: ${videoURL})`,
);
}
};
await streamToFile();
fileStream.close();
},
{
retries: 10,
minTimeout: 3_000,
onRetry: (err, i) => {
if (i > 5) {
logger.warn(`Retry attempt ${i} failed: ${err.message}`);
const reader = response.body?.getReader();
if (!reader) {
throw new Error("Failed to get reader from response body.");
}
const streamToFile = async () => {
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();
if (done) break;
fileStream.write(value);
}
};
await streamToFile();
fileStream.close();
},
},
);
return new Promise((resolve, reject) => {
// Ensure file stream is closed even in case of an error
fileStream.on("finish", () => {
try {
fs.renameSync(tempPathForWriting, pathToTestVideo);
logger.log(`Download finished and file closed: ${pathToTestVideo}`);
resolve({ path: pathToTestVideo, contentType: "video/mp4" });
} catch (err) {
logger.error(`Failed to rename file: `, err);
reject(err);
}
});
{
retries: 10,
minTimeout: 3_000,
onRetry: (err, i) => {
if (i > 5) {
logger.warn(`Retry attempt ${i} failed: ${err.message}`);
}
},
},
);
return new Promise((resolve, reject) => {
// Ensure file stream is closed even in case of an error
fileStream.on("finish", () => {
try {
fs.renameSync(tempPathForWriting, pathToTestVideo);
logger.log(
`Download finished and file closed: ${pathToTestVideo}`,
);
resolve({ path: pathToTestVideo, contentType: "video/mp4" });
} catch (err) {
logger.error(`Failed to rename file: `, err);
reject(err);
}
});

fileStream.on("error", (err) => {
logger.error(`Failed to write file: ${err.message}`);
reject(err);
fileStream.on("error", (err) => {
logger.error(`Failed to write file: ${err.message}`);
reject(err);
});
});
});
} else {
} else {
return null;
}
} catch (e) {
logger.log(`Error Downloading video: `, e);
return null;
}
}
Expand Down

0 comments on commit 4074400

Please sign in to comment.