httpUtil.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Copyright 2023 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { createWriteStream } from 'fs';
  17. import * as http from 'http';
  18. import * as https from 'https';
  19. import { URL, urlToHttpOptions } from 'url';
  20. import { ProxyAgent } from 'proxy-agent';
  21. export function headHttpRequest(url) {
  22. return new Promise(resolve => {
  23. const request = httpRequest(url, 'HEAD', response => {
  24. resolve(response.statusCode === 200);
  25. }, false);
  26. request.on('error', () => {
  27. resolve(false);
  28. });
  29. });
  30. }
  31. export function httpRequest(url, method, response, keepAlive = true) {
  32. const options = {
  33. protocol: url.protocol,
  34. hostname: url.hostname,
  35. port: url.port,
  36. path: url.pathname + url.search,
  37. method,
  38. headers: keepAlive ? { Connection: 'keep-alive' } : undefined,
  39. auth: urlToHttpOptions(url).auth,
  40. agent: new ProxyAgent(),
  41. };
  42. const requestCallback = (res) => {
  43. if (res.statusCode &&
  44. res.statusCode >= 300 &&
  45. res.statusCode < 400 &&
  46. res.headers.location) {
  47. httpRequest(new URL(res.headers.location), method, response);
  48. }
  49. else {
  50. response(res);
  51. }
  52. };
  53. const request = options.protocol === 'https:'
  54. ? https.request(options, requestCallback)
  55. : http.request(options, requestCallback);
  56. request.end();
  57. return request;
  58. }
  59. /**
  60. * @internal
  61. */
  62. export function downloadFile(url, destinationPath, progressCallback) {
  63. return new Promise((resolve, reject) => {
  64. let downloadedBytes = 0;
  65. let totalBytes = 0;
  66. function onData(chunk) {
  67. downloadedBytes += chunk.length;
  68. progressCallback(downloadedBytes, totalBytes);
  69. }
  70. const request = httpRequest(url, 'GET', response => {
  71. if (response.statusCode !== 200) {
  72. const error = new Error(`Download failed: server returned code ${response.statusCode}. URL: ${url}`);
  73. // consume response data to free up memory
  74. response.resume();
  75. reject(error);
  76. return;
  77. }
  78. const file = createWriteStream(destinationPath);
  79. file.on('finish', () => {
  80. return resolve();
  81. });
  82. file.on('error', error => {
  83. return reject(error);
  84. });
  85. response.pipe(file);
  86. totalBytes = parseInt(response.headers['content-length'], 10);
  87. if (progressCallback) {
  88. response.on('data', onData);
  89. }
  90. });
  91. request.on('error', error => {
  92. return reject(error);
  93. });
  94. });
  95. }
  96. export async function getJSON(url) {
  97. const text = await getText(url);
  98. try {
  99. return JSON.parse(text);
  100. }
  101. catch {
  102. throw new Error('Could not parse JSON from ' + url.toString());
  103. }
  104. }
  105. export function getText(url) {
  106. return new Promise((resolve, reject) => {
  107. const request = httpRequest(url, 'GET', response => {
  108. let data = '';
  109. if (response.statusCode && response.statusCode >= 400) {
  110. return reject(new Error(`Got status code ${response.statusCode}`));
  111. }
  112. response.on('data', chunk => {
  113. data += chunk;
  114. });
  115. response.on('end', () => {
  116. try {
  117. return resolve(String(data));
  118. }
  119. catch {
  120. return reject(new Error('Chrome version not found'));
  121. }
  122. });
  123. }, false);
  124. request.on('error', err => {
  125. reject(err);
  126. });
  127. });
  128. }
  129. //# sourceMappingURL=httpUtil.js.map