NodeHttpClient.js 4.3 KB

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