UNPKG

12.5 kBJavaScriptView Raw
1"use strict";
2
3function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
4function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
5function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
6/**
7 * Module dependencies.
8 */
9
10// eslint-disable-next-line node/no-deprecated-api
11const _require = require('node:url'),
12 parse = _require.parse;
13const _require2 = require('cookiejar'),
14 CookieJar = _require2.CookieJar;
15const _require3 = require('cookiejar'),
16 CookieAccessInfo = _require3.CookieAccessInfo;
17const methods = require('methods');
18const request = require('../..');
19const AgentBase = require('../agent-base');
20
21/**
22 * Expose `Agent`.
23 */
24
25module.exports = Agent;
26
27/**
28 * Initialize a new `Agent`.
29 *
30 * @api public
31 */
32
33function Agent(options) {
34 if (!(this instanceof Agent)) {
35 return new Agent(options);
36 }
37 AgentBase.call(this);
38 this.jar = new CookieJar();
39 if (options) {
40 if (options.ca) {
41 this.ca(options.ca);
42 }
43 if (options.key) {
44 this.key(options.key);
45 }
46 if (options.pfx) {
47 this.pfx(options.pfx);
48 }
49 if (options.cert) {
50 this.cert(options.cert);
51 }
52 if (options.rejectUnauthorized === false) {
53 this.disableTLSCerts();
54 }
55 }
56}
57Agent.prototype = Object.create(AgentBase.prototype);
58
59/**
60 * Save the cookies in the given `res` to
61 * the agent's cookie jar for persistence.
62 *
63 * @param {Response} res
64 * @api private
65 */
66
67Agent.prototype._saveCookies = function (res) {
68 const cookies = res.headers['set-cookie'];
69 if (cookies) {
70 var _res$request;
71 const url = parse(((_res$request = res.request) === null || _res$request === void 0 ? void 0 : _res$request.url) || '');
72 this.jar.setCookies(cookies, url.hostname, url.pathname);
73 }
74};
75
76/**
77 * Attach cookies when available to the given `req`.
78 *
79 * @param {Request} req
80 * @api private
81 */
82
83Agent.prototype._attachCookies = function (request_) {
84 const url = parse(request_.url);
85 const access = new CookieAccessInfo(url.hostname, url.pathname, url.protocol === 'https:');
86 const cookies = this.jar.getCookies(access).toValueString();
87 request_.cookies = cookies;
88};
89var _iterator = _createForOfIteratorHelper(methods),
90 _step;
91try {
92 for (_iterator.s(); !(_step = _iterator.n()).done;) {
93 const name = _step.value;
94 const method = name.toUpperCase();
95 Agent.prototype[name] = function (url, fn) {
96 const request_ = new request.Request(method, url);
97 request_.on('response', this._saveCookies.bind(this));
98 request_.on('redirect', this._saveCookies.bind(this));
99 request_.on('redirect', this._attachCookies.bind(this, request_));
100 this._setDefaults(request_);
101 this._attachCookies(request_);
102 if (fn) {
103 request_.end(fn);
104 }
105 return request_;
106 };
107 }
108} catch (err) {
109 _iterator.e(err);
110} finally {
111 _iterator.f();
112}
113Agent.prototype.del = Agent.prototype.delete;
114//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfcmVxdWlyZ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