Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit aded26b

Browse files
committed
feat(webdriver-manager): update download logic with streaming
Also adds a content length check to make sure the downloaded binaries are the correct size. This seems to fix up some unreliable download issues that we were previously having.
1 parent 0262268 commit aded26b

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

bin/webdriver-manager

+22-12
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ var httpGetFile = function(fileUrl, fileName, outputDir, callback) {
133133
var filePath = path.join(outputDir, fileName);
134134
var file = fs.createWriteStream(filePath);
135135
var ignoreSSL = argv.ignore_ssl;
136+
var contentLength = 0;
136137

137138
if (ignoreSSL) {
138139
console.log('Ignoring SSL certificate');
@@ -150,22 +151,31 @@ var httpGetFile = function(fileUrl, fileName, outputDir, callback) {
150151
fs.unlink(filePath);
151152
console.error('Error: Got code ' + response.statusCode + ' from ' + fileUrl);
152153
}
154+
contentLength = response.headers['content-length'];
153155
})
154156
.on('error', function(error) {
155-
fs.unlink(filePath);
156157
console.error('Error: Got error ' + error + ' from ' + fileUrl);
158+
fs.unlink(filePath);
157159
})
158-
.on('data', function(data) {
159-
file.write(data);
160-
})
161-
.on('end', function() {
162-
file.end(function() {
163-
console.log(fileName + ' downloaded to ' + filePath);
164-
if (callback) {
165-
callback(filePath);
166-
}
167-
});
168-
});
160+
.pipe(file);
161+
162+
file.on('close', function() {
163+
fs.stat(filePath, function(err, stats) {
164+
if (err) {
165+
console.error('Error: Got error ' + error + ' from ' + fileUrl);
166+
}
167+
if (stats.size != contentLength) {
168+
console.error('Error: corrupt download for ' + fileName +
169+
'. Please re-run webdriver-manager update');
170+
fs.unlink(filePath);
171+
return;
172+
}
173+
console.log(fileName + ' downloaded to ' + filePath);
174+
if (callback) {
175+
callback(filePath);
176+
}
177+
});
178+
});
169179
};
170180

171181
/**

0 commit comments

Comments
 (0)