request.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. /*---------------------------------------------------------------------------------------------
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. * Licensed under the MIT License. See License.txt in the project root for license information.
  5. *--------------------------------------------------------------------------------------------*/
  6. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  7. if (k2 === undefined) k2 = k;
  8. var desc = Object.getOwnPropertyDescriptor(m, k);
  9. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  10. desc = { enumerable: true, get: function() { return m[k]; } };
  11. }
  12. Object.defineProperty(o, k2, desc);
  13. }) : (function(o, m, k, k2) {
  14. if (k2 === undefined) k2 = k;
  15. o[k2] = m[k];
  16. }));
  17. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  18. Object.defineProperty(o, "default", { enumerable: true, value: v });
  19. }) : function(o, v) {
  20. o["default"] = v;
  21. });
  22. var __importStar = (this && this.__importStar) || function (mod) {
  23. if (mod && mod.__esModule) return mod;
  24. var result = {};
  25. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  26. __setModuleDefault(result, mod);
  27. return result;
  28. };
  29. Object.defineProperty(exports, "__esModule", { value: true });
  30. exports.TimeoutError = exports.TimeoutController = void 0;
  31. exports.getStream = getStream;
  32. exports.getJSON = getJSON;
  33. const https = __importStar(require("https"));
  34. const util_1 = require("./util");
  35. async function getStream(api, timeout) {
  36. const ctrl = new TimeoutController(timeout);
  37. return new Promise((resolve, reject) => {
  38. ctrl.signal.addEventListener('abort', () => {
  39. reject(new TimeoutError(timeout));
  40. req.destroy();
  41. });
  42. const req = https.get(api, (0, util_1.urlToOptions)(api), (res) => resolve(res)).on('error', reject);
  43. }).finally(() => ctrl.dispose());
  44. }
  45. async function getJSON(api, timeout) {
  46. const ctrl = new TimeoutController(timeout);
  47. return new Promise((resolve, reject) => {
  48. ctrl.signal.addEventListener('abort', () => {
  49. reject(new TimeoutError(timeout));
  50. req.destroy();
  51. });
  52. const req = https
  53. .get(api, (0, util_1.urlToOptions)(api), (res) => {
  54. if (res.statusCode !== 200) {
  55. reject('Failed to get JSON');
  56. }
  57. let data = '';
  58. res.on('data', (chunk) => {
  59. ctrl.touch();
  60. data += chunk;
  61. });
  62. res.on('end', () => {
  63. ctrl.dispose();
  64. try {
  65. const jsonData = JSON.parse(data);
  66. resolve(jsonData);
  67. }
  68. catch (err) {
  69. console.error(`Failed to parse response from ${api} as JSON`);
  70. reject(err);
  71. }
  72. });
  73. res.on('error', reject);
  74. })
  75. .on('error', reject);
  76. }).finally(() => ctrl.dispose());
  77. }
  78. class TimeoutController {
  79. get signal() {
  80. return this.ctrl.signal;
  81. }
  82. constructor(timeout) {
  83. this.timeout = timeout;
  84. this.ctrl = new AbortController();
  85. this.reject = () => {
  86. this.ctrl.abort();
  87. };
  88. this.handle = setTimeout(this.reject, timeout);
  89. }
  90. touch() {
  91. clearTimeout(this.handle);
  92. this.handle = setTimeout(this.reject, this.timeout);
  93. }
  94. dispose() {
  95. clearTimeout(this.handle);
  96. }
  97. }
  98. exports.TimeoutController = TimeoutController;
  99. class TimeoutError extends Error {
  100. constructor(duration) {
  101. super(`@vscode/test-electron request timeout out after ${duration}ms`);
  102. }
  103. }
  104. exports.TimeoutError = TimeoutError;