superagent-proxy.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * (The MIT License)
  5. *
  6. * Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  9. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  10. *
  11. * This is a fork of the original source, upgraded to proxy-agent v6 and TypeScript.
  12. * Original source: https://github.com/TooTallNate/superagent-proxy/blob/ef2cc112926b574547cf2f765ae3dfd80bdeb9a0/index.js
  13. *
  14. */
  15. const proxy_agent_1 = require("proxy-agent");
  16. const debug_1 = require("debug");
  17. const debug = (0, debug_1.debug)('superagent-proxy');
  18. /**
  19. * Adds a `.proxy(uri)` function to the "superagent" module's Request class.
  20. *
  21. * ``` js
  22. * var request = require('superagent');
  23. * require('superagent-proxy')(request);
  24. *
  25. * request
  26. * .get(uri)
  27. * .proxy(uri)
  28. * .end(fn);
  29. * ```
  30. *
  31. * Or, you can pass in a `superagent.Request` instance, and it's like calling the
  32. * `.proxy(uri)` function on it, but without extending the prototype:
  33. *
  34. * ``` js
  35. * var request = require('superagent');
  36. * var proxy = require('superagent-proxy');
  37. *
  38. * proxy(request.get(uri), uri).end(fn);
  39. * ```
  40. *
  41. * @param {Object} superagent The `superagent` exports object
  42. * @api public
  43. */
  44. function setup(superagent, uri) {
  45. const Request = superagent.Request;
  46. if (Request) {
  47. // the superagent exports object - extent Request with "proxy"
  48. Request.prototype.proxy = proxy;
  49. return superagent;
  50. }
  51. else {
  52. // assume it's a `superagent.Request` instance
  53. return proxy.call(superagent, uri);
  54. }
  55. }
  56. exports.default = setup;
  57. /**
  58. * Sets the proxy server to use for this HTTP(s) request.
  59. *
  60. * @param {String} uri proxy url
  61. * @api public
  62. */
  63. function proxy(uri) {
  64. debug('Request#proxy(%o)', uri);
  65. // we need to observe the `url` field from now on... Superagent sometimes
  66. // re-uses the `req` instance but changes its `url` field (i.e. in the case of
  67. // a redirect), so when that happens we need to potentially re-set the proxy
  68. // agent
  69. setupUrl(this);
  70. // attempt to get a proxying `http.Agent` instance
  71. const agent = new proxy_agent_1.ProxyAgent(uri !== undefined ? { getProxyForUrl: () => uri } : {});
  72. // if we have an `http.Agent` instance then call the .agent() function
  73. if (agent)
  74. this.agent(agent);
  75. // store the proxy URI in case of changes to the `url` prop in the future
  76. this._proxyUri = uri;
  77. return this;
  78. }
  79. /**
  80. * Sets up a get/set descriptor for the `url` property of the provided `req`
  81. * Request instance. This is so that we can re-run the "proxy agent" logic when
  82. * the `url` field is changed, i.e. during a 302 Redirect scenario.
  83. *
  84. * @api private
  85. */
  86. function setupUrl(req) {
  87. var desc = Object.getOwnPropertyDescriptor(req, 'url');
  88. if (desc?.get == getUrl && desc?.set == setUrl)
  89. return; // already patched
  90. // save current value
  91. req._url = req.url;
  92. if (desc) {
  93. desc.get = getUrl;
  94. desc.set = setUrl;
  95. delete desc.value;
  96. delete desc.writable;
  97. Object.defineProperty(req, 'url', desc);
  98. debug('patched superagent Request "url" property for changes');
  99. }
  100. }
  101. /**
  102. * `url` property getter.
  103. *
  104. * @api protected
  105. */
  106. function getUrl() {
  107. return this._url;
  108. }
  109. /**
  110. * `url` property setter.
  111. *
  112. * @api protected
  113. */
  114. function setUrl(v) {
  115. debug('set `.url`: %o', v);
  116. this._url = v;
  117. proxy.call(this, this._proxyUri);
  118. }