http.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.download = exports.createRequest = exports.PROXY_ENVIRONMENT_VARIABLES = void 0;
  4. const tslib_1 = require("tslib");
  5. const utils_array_1 = require("@ionic/utils-array");
  6. const utils_fs_1 = require("@ionic/utils-fs");
  7. const debug_1 = require("debug");
  8. const superagent_proxy_1 = tslib_1.__importDefault(require("./superagent-proxy"));
  9. const debug = (0, debug_1.debug)('ionic:lib:utils:http');
  10. exports.PROXY_ENVIRONMENT_VARIABLES = ['IONIC_HTTP_PROXY', 'HTTPS_PROXY', 'HTTP_PROXY', 'PROXY', 'https_proxy', 'http_proxy', 'proxy'];
  11. function getGlobalProxy() {
  12. for (const envvar of exports.PROXY_ENVIRONMENT_VARIABLES) {
  13. const envval = process.env[envvar];
  14. if (envval) {
  15. return { envval, envvar };
  16. }
  17. }
  18. }
  19. async function createRequest(method, url, { userAgent, proxy, ssl }) {
  20. const superagent = (await Promise.resolve().then(() => tslib_1.__importStar(require('superagent')))).default;
  21. if (!proxy) {
  22. const gproxy = getGlobalProxy();
  23. if (gproxy) {
  24. proxy = gproxy.envval;
  25. }
  26. }
  27. const req = superagent(method, url);
  28. req
  29. .set('User-Agent', userAgent)
  30. .redirects(25);
  31. if (proxy) {
  32. (0, superagent_proxy_1.default)(superagent);
  33. if (req.proxy) {
  34. req.proxy(proxy);
  35. }
  36. else {
  37. debug(`Cannot install proxy--req.proxy not defined`);
  38. }
  39. }
  40. if (ssl) {
  41. const cafiles = (0, utils_array_1.conform)(ssl.cafile);
  42. const certfiles = (0, utils_array_1.conform)(ssl.certfile);
  43. const keyfiles = (0, utils_array_1.conform)(ssl.keyfile);
  44. if (cafiles.length > 0) {
  45. req.ca(await Promise.all(cafiles.map(p => (0, utils_fs_1.readFile)(p, { encoding: 'utf8' }))));
  46. }
  47. if (certfiles.length > 0) {
  48. req.cert(await Promise.all(certfiles.map(p => (0, utils_fs_1.readFile)(p, { encoding: 'utf8' }))));
  49. }
  50. if (keyfiles.length > 0) {
  51. req.key(await Promise.all(keyfiles.map(p => (0, utils_fs_1.readFile)(p, { encoding: 'utf8' }))));
  52. }
  53. }
  54. return { req };
  55. }
  56. exports.createRequest = createRequest;
  57. /**
  58. * Initiate a request, downloading the contents to a writable stream.
  59. *
  60. * @param req The request to download to the writable stream.
  61. * @param ws Must be a dedicated writable stream that calls the 'close' event.
  62. */
  63. async function download(req, ws, { progress }) {
  64. return new Promise((resolve, reject) => {
  65. req
  66. .on('response', res => {
  67. if (res.status !== 200) {
  68. reject(new Error(`Encountered bad status code (${res.status}) for ${req.url}\n` +
  69. `This could mean the server is experiencing difficulties right now--please try again later.`));
  70. }
  71. if (progress) {
  72. let loaded = 0;
  73. const total = Number(res.header['content-length']);
  74. res.on('data', chunk => {
  75. loaded += chunk.length;
  76. progress(loaded, total);
  77. });
  78. }
  79. })
  80. .on('error', err => {
  81. if (err.code === 'ECONNABORTED') {
  82. reject(new Error(`Timeout of ${err.timeout}ms reached for ${req.url}`));
  83. }
  84. else {
  85. reject(err);
  86. }
  87. });
  88. ws.on('close', resolve);
  89. req.pipe(ws);
  90. });
  91. }
  92. exports.download = download;