1 |
|
2 |
|
3 | import { __awaiter, __extends, __generator } from "tslib";
|
4 | import * as tough from "tough-cookie";
|
5 | import * as http from "http";
|
6 | import * as https from "https";
|
7 | import node_fetch from "node-fetch";
|
8 | import { FetchHttpClient, } from "./fetchHttpClient";
|
9 | import { createProxyAgent } from "./proxyAgent";
|
10 | var NodeFetchHttpClient = (function (_super) {
|
11 | __extends(NodeFetchHttpClient, _super);
|
12 | function NodeFetchHttpClient() {
|
13 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
14 | _this.cookieJar = new tough.CookieJar(undefined, { looseMode: true });
|
15 | return _this;
|
16 | }
|
17 | NodeFetchHttpClient.prototype.fetch = function (input, init) {
|
18 | return __awaiter(this, void 0, void 0, function () {
|
19 | return __generator(this, function (_a) {
|
20 | return [2 , node_fetch(input, init)];
|
21 | });
|
22 | });
|
23 | };
|
24 | NodeFetchHttpClient.prototype.prepareRequest = function (httpRequest) {
|
25 | return __awaiter(this, void 0, void 0, function () {
|
26 | var requestInit, cookieString, _a, httpAgent, httpsAgent, tunnel, options, agent;
|
27 | var _this = this;
|
28 | return __generator(this, function (_b) {
|
29 | switch (_b.label) {
|
30 | case 0:
|
31 | requestInit = {};
|
32 | if (!(this.cookieJar && !httpRequest.headers.get("Cookie"))) return [3 , 2];
|
33 | return [4 , new Promise(function (resolve, reject) {
|
34 | _this.cookieJar.getCookieString(httpRequest.url, function (err, cookie) {
|
35 | if (err) {
|
36 | reject(err);
|
37 | }
|
38 | else {
|
39 | resolve(cookie);
|
40 | }
|
41 | });
|
42 | })];
|
43 | case 1:
|
44 | cookieString = _b.sent();
|
45 | httpRequest.headers.set("Cookie", cookieString);
|
46 | _b.label = 2;
|
47 | case 2:
|
48 | if (httpRequest.agentSettings) {
|
49 | _a = httpRequest.agentSettings, httpAgent = _a.http, httpsAgent = _a.https;
|
50 | if (httpsAgent && httpRequest.url.startsWith("https")) {
|
51 | requestInit.agent = httpsAgent;
|
52 | }
|
53 | else if (httpAgent) {
|
54 | requestInit.agent = httpAgent;
|
55 | }
|
56 | }
|
57 | else if (httpRequest.proxySettings) {
|
58 | tunnel = createProxyAgent(httpRequest.url, httpRequest.proxySettings, httpRequest.headers);
|
59 | requestInit.agent = tunnel.agent;
|
60 | }
|
61 | if (httpRequest.keepAlive === true) {
|
62 | if (requestInit.agent) {
|
63 | requestInit.agent.keepAlive = true;
|
64 | }
|
65 | else {
|
66 | options = { keepAlive: true };
|
67 | agent = httpRequest.url.startsWith("https")
|
68 | ? new https.Agent(options)
|
69 | : new http.Agent(options);
|
70 | requestInit.agent = agent;
|
71 | }
|
72 | }
|
73 | return [2 , requestInit];
|
74 | }
|
75 | });
|
76 | });
|
77 | };
|
78 | NodeFetchHttpClient.prototype.processRequest = function (operationResponse) {
|
79 | return __awaiter(this, void 0, void 0, function () {
|
80 | var setCookieHeader_1;
|
81 | var _this = this;
|
82 | return __generator(this, function (_a) {
|
83 | switch (_a.label) {
|
84 | case 0:
|
85 | if (!this.cookieJar) return [3 , 2];
|
86 | setCookieHeader_1 = operationResponse.headers.get("Set-Cookie");
|
87 | if (!(setCookieHeader_1 != undefined)) return [3 , 2];
|
88 | return [4 , new Promise(function (resolve, reject) {
|
89 | _this.cookieJar.setCookie(setCookieHeader_1, operationResponse.request.url, { ignoreError: true }, function (err) {
|
90 | if (err) {
|
91 | reject(err);
|
92 | }
|
93 | else {
|
94 | resolve();
|
95 | }
|
96 | });
|
97 | })];
|
98 | case 1:
|
99 | _a.sent();
|
100 | _a.label = 2;
|
101 | case 2: return [2 ];
|
102 | }
|
103 | });
|
104 | });
|
105 | };
|
106 | return NodeFetchHttpClient;
|
107 | }(FetchHttpClient));
|
108 | export { NodeFetchHttpClient };
|
109 |
|
\ | No newline at end of file |