oopDownloadBrowserMain.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. "use strict";
  2. var _fs = _interopRequireDefault(require("fs"));
  3. var _path = _interopRequireDefault(require("path"));
  4. var _network = require("../../utils/network");
  5. var _manualPromise = require("../../utils/manualPromise");
  6. var _zipBundle = require("../../zipBundle");
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. /**
  9. * Copyright (c) Microsoft Corporation.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. function log(message) {
  24. var _process$send, _process;
  25. (_process$send = (_process = process).send) === null || _process$send === void 0 ? void 0 : _process$send.call(_process, {
  26. method: 'log',
  27. params: {
  28. message
  29. }
  30. });
  31. }
  32. function progress(done, total) {
  33. var _process$send2, _process2;
  34. (_process$send2 = (_process2 = process).send) === null || _process$send2 === void 0 ? void 0 : _process$send2.call(_process2, {
  35. method: 'progress',
  36. params: {
  37. done,
  38. total
  39. }
  40. });
  41. }
  42. function browserDirectoryToMarkerFilePath(browserDirectory) {
  43. return _path.default.join(browserDirectory, 'INSTALLATION_COMPLETE');
  44. }
  45. function downloadFile(options) {
  46. let downloadedBytes = 0;
  47. let totalBytes = 0;
  48. const promise = new _manualPromise.ManualPromise();
  49. (0, _network.httpRequest)({
  50. url: options.url,
  51. headers: {
  52. 'User-Agent': options.userAgent
  53. },
  54. timeout: options.connectionTimeout
  55. }, response => {
  56. log(`-- response status code: ${response.statusCode}`);
  57. if (response.statusCode !== 200) {
  58. let content = '';
  59. const handleError = () => {
  60. const error = new Error(`Download failed: server returned code ${response.statusCode} body '${content}'. URL: ${options.url}`);
  61. // consume response data to free up memory
  62. response.resume();
  63. promise.reject(error);
  64. };
  65. response.on('data', chunk => content += chunk).on('end', handleError).on('error', handleError);
  66. return;
  67. }
  68. totalBytes = parseInt(response.headers['content-length'] || '0', 10);
  69. log(`-- total bytes: ${totalBytes}`);
  70. const file = _fs.default.createWriteStream(options.zipPath);
  71. file.on('finish', () => {
  72. if (downloadedBytes !== totalBytes) {
  73. log(`-- download failed, size mismatch: ${downloadedBytes} != ${totalBytes}`);
  74. promise.reject(new Error(`Download failed: size mismatch, file size: ${downloadedBytes}, expected size: ${totalBytes} URL: ${options.url}`));
  75. } else {
  76. log(`-- download complete, size: ${downloadedBytes}`);
  77. promise.resolve();
  78. }
  79. });
  80. file.on('error', error => promise.reject(error));
  81. response.pipe(file);
  82. response.on('data', onData);
  83. response.on('error', error => {
  84. file.close();
  85. if ((error === null || error === void 0 ? void 0 : error.code) === 'ECONNRESET') {
  86. log(`-- download failed, server closed connection`);
  87. promise.reject(new Error(`Download failed: server closed connection. URL: ${options.url}`));
  88. } else {
  89. var _error$message;
  90. log(`-- download failed, unexpected error`);
  91. promise.reject(new Error(`Download failed: ${(_error$message = error === null || error === void 0 ? void 0 : error.message) !== null && _error$message !== void 0 ? _error$message : error}. URL: ${options.url}`));
  92. }
  93. });
  94. }, error => promise.reject(error));
  95. return promise;
  96. function onData(chunk) {
  97. downloadedBytes += chunk.length;
  98. progress(downloadedBytes, totalBytes);
  99. }
  100. }
  101. async function main(options) {
  102. await downloadFile(options);
  103. log(`SUCCESS downloading ${options.title}`);
  104. log(`extracting archive`);
  105. await (0, _zipBundle.extract)(options.zipPath, {
  106. dir: options.browserDirectory
  107. });
  108. if (options.executablePath) {
  109. log(`fixing permissions at ${options.executablePath}`);
  110. await _fs.default.promises.chmod(options.executablePath, 0o755);
  111. }
  112. await _fs.default.promises.writeFile(browserDirectoryToMarkerFilePath(options.browserDirectory), '');
  113. }
  114. process.on('message', async message => {
  115. const {
  116. method,
  117. params
  118. } = message;
  119. if (method === 'download') {
  120. try {
  121. await main(params);
  122. // eslint-disable-next-line no-restricted-properties
  123. process.exit(0);
  124. } catch (e) {
  125. // eslint-disable-next-line no-console
  126. console.error(e);
  127. // eslint-disable-next-line no-restricted-properties
  128. process.exit(1);
  129. }
  130. }
  131. });
  132. // eslint-disable-next-line no-restricted-properties
  133. process.on('disconnect', () => {
  134. process.exit(0);
  135. });