NodeHttpClient.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. // Copyright (c) .NET Foundation. All rights reserved.
  3. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  4. var __extends = (this && this.__extends) || (function () {
  5. var extendStatics = Object.setPrototypeOf ||
  6. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  7. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  8. return function (d, b) {
  9. extendStatics(d, b);
  10. function __() { this.constructor = d; }
  11. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  12. };
  13. })();
  14. var __assign = (this && this.__assign) || Object.assign || function(t) {
  15. for (var s, i = 1, n = arguments.length; i < n; i++) {
  16. s = arguments[i];
  17. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  18. t[p] = s[p];
  19. }
  20. return t;
  21. };
  22. Object.defineProperty(exports, "__esModule", { value: true });
  23. var Errors_1 = require("./Errors");
  24. var HttpClient_1 = require("./HttpClient");
  25. var ILogger_1 = require("./ILogger");
  26. var Utils_1 = require("./Utils");
  27. var requestModule;
  28. if (typeof XMLHttpRequest === "undefined") {
  29. // In order to ignore the dynamic require in webpack builds we need to do this magic
  30. // @ts-ignore: TS doesn't know about these names
  31. var requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
  32. requestModule = requireFunc("request");
  33. }
  34. var NodeHttpClient = /** @class */ (function (_super) {
  35. __extends(NodeHttpClient, _super);
  36. function NodeHttpClient(logger) {
  37. var _this = _super.call(this) || this;
  38. if (typeof requestModule === "undefined") {
  39. throw new Error("The 'request' module could not be loaded.");
  40. }
  41. _this.logger = logger;
  42. _this.cookieJar = requestModule.jar();
  43. _this.request = requestModule.defaults({ jar: _this.cookieJar });
  44. return _this;
  45. }
  46. NodeHttpClient.prototype.send = function (httpRequest) {
  47. var _this = this;
  48. return new Promise(function (resolve, reject) {
  49. var requestBody;
  50. if (Utils_1.isArrayBuffer(httpRequest.content)) {
  51. requestBody = Buffer.from(httpRequest.content);
  52. }
  53. else {
  54. requestBody = httpRequest.content || "";
  55. }
  56. var currentRequest = _this.request(httpRequest.url, {
  57. body: requestBody,
  58. // If binary is expected 'null' should be used, otherwise for text 'utf8'
  59. encoding: httpRequest.responseType === "arraybuffer" ? null : "utf8",
  60. headers: __assign({
  61. // Tell auth middleware to 401 instead of redirecting
  62. "X-Requested-With": "XMLHttpRequest" }, httpRequest.headers),
  63. method: httpRequest.method,
  64. timeout: httpRequest.timeout,
  65. }, function (error, response, body) {
  66. if (httpRequest.abortSignal) {
  67. httpRequest.abortSignal.onabort = null;
  68. }
  69. if (error) {
  70. if (error.code === "ETIMEDOUT") {
  71. _this.logger.log(ILogger_1.LogLevel.Warning, "Timeout from HTTP request.");
  72. reject(new Errors_1.TimeoutError());
  73. }
  74. _this.logger.log(ILogger_1.LogLevel.Warning, "Error from HTTP request. " + error);
  75. reject(error);
  76. return;
  77. }
  78. if (response.statusCode >= 200 && response.statusCode < 300) {
  79. resolve(new HttpClient_1.HttpResponse(response.statusCode, response.statusMessage || "", body));
  80. }
  81. else {
  82. reject(new Errors_1.HttpError(response.statusMessage || "", response.statusCode || 0));
  83. }
  84. });
  85. if (httpRequest.abortSignal) {
  86. httpRequest.abortSignal.onabort = function () {
  87. currentRequest.abort();
  88. reject(new Errors_1.AbortError());
  89. };
  90. }
  91. });
  92. };
  93. NodeHttpClient.prototype.getCookieString = function (url) {
  94. return this.cookieJar.getCookieString(url);
  95. };
  96. return NodeHttpClient;
  97. }(HttpClient_1.HttpClient));
  98. exports.NodeHttpClient = NodeHttpClient;
  99. //# sourceMappingURL=NodeHttpClient.js.map