diff --git a/lib/helpers.js b/lib/helpers.js index 7bfc6cec8..9446b5daf 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -7,7 +7,7 @@ import B from 'bluebird'; import { tempDir, system, fs } from 'appium-support'; import { exec } from 'teen_process'; import AdmZip from 'adm-zip'; -import request from 'request-promise'; +import requestWithoutPromises from 'request'; const ZIP_EXTS = ['.zip', '.ipa']; @@ -57,13 +57,24 @@ async function downloadApp (app, appExt) { } async function downloadFile (fileUrl, suffix) { + // We will be downloading the files to a directory, so make sure it's there // This step is not required if you have manually created the directory let fileInfo = await tempDir.open({prefix: 'appium-app', suffix: suffix}); _fs.close(fileInfo.fd); - await fs.writeFile(fileInfo.path, await request(fileUrl)); - logger.debug(`${fileUrl} downloaded to ${fileInfo.path}`); + // we use the non-promise version of request because the 'pipe' function + // won't overuse memory. request-promise has to store the whole response + // in memory (and also has encoding trickiness) + let writeStream = _fs.createWriteStream(fileInfo.path); + writeStream.on('error', (e) => logger.errorAndThrow(e)); + + let onClose = B.promisify(writeStream.on, writeStream)('close'); + + requestWithoutPromises.get(fileUrl).pipe(writeStream); + + await onClose; + logger.debug(`${fileUrl} downloaded to ${fileInfo.path}`); return fileInfo.path; } diff --git a/package.json b/package.json index 05fb3fca1..34575c71c 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "bluebird": "^2.9.25", "lodash": "^3.9.1", "mobile-json-wire-protocol": "latest", - "request-promise": "^0.4.2", + "request": "^2.61.0", "source-map-support": "^0.3.2", "teen_process": "^1.2.2", "uuid-js": "^0.7.5", @@ -49,6 +49,7 @@ "chai-as-promised": "^5.0.0", "gulp": "^3.8.11", "mochawait": "^2.0.0", + "request-promise": "^0.4.2", "sinon": "^1.14.1", "yargs": "^3.9.0" }