1 | import { __awaiter, __generator, __values } from "tslib";
|
2 | import { HttpResponse } from "@aws-sdk/protocol-http";
|
3 | import { buildQueryString } from "@aws-sdk/querystring-builder";
|
4 | import { requestTimeout } from "./request-timeout";
|
5 | var FetchHttpHandler = (function () {
|
6 | function FetchHttpHandler(options) {
|
7 | var _this = this;
|
8 | if (typeof options === "function") {
|
9 | this.configProvider = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
10 | switch (_a.label) {
|
11 | case 0: return [4, options()];
|
12 | case 1: return [2, (_a.sent()) || {}];
|
13 | }
|
14 | }); }); };
|
15 | }
|
16 | else {
|
17 | this.config = options !== null && options !== void 0 ? options : {};
|
18 | }
|
19 | }
|
20 | FetchHttpHandler.prototype.destroy = function () {
|
21 | };
|
22 | FetchHttpHandler.prototype.handle = function (request, _a) {
|
23 | var _b = _a === void 0 ? {} : _a, abortSignal = _b.abortSignal;
|
24 | return __awaiter(this, void 0, void 0, function () {
|
25 | var _c, requestTimeoutInMs, abortError, path, queryString, port, method, url, body, requestOptions, fetchRequest, raceOfPromises;
|
26 | return __generator(this, function (_d) {
|
27 | switch (_d.label) {
|
28 | case 0:
|
29 | if (!(!this.config && this.configProvider)) return [3, 2];
|
30 | _c = this;
|
31 | return [4, this.configProvider()];
|
32 | case 1:
|
33 | _c.config = _d.sent();
|
34 | _d.label = 2;
|
35 | case 2:
|
36 | requestTimeoutInMs = this.config.requestTimeout;
|
37 | if (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted) {
|
38 | abortError = new Error("Request aborted");
|
39 | abortError.name = "AbortError";
|
40 | return [2, Promise.reject(abortError)];
|
41 | }
|
42 | path = request.path;
|
43 | if (request.query) {
|
44 | queryString = buildQueryString(request.query);
|
45 | if (queryString) {
|
46 | path += "?".concat(queryString);
|
47 | }
|
48 | }
|
49 | port = request.port, method = request.method;
|
50 | url = "".concat(request.protocol, "//").concat(request.hostname).concat(port ? ":".concat(port) : "").concat(path);
|
51 | body = method === "GET" || method === "HEAD" ? undefined : request.body;
|
52 | requestOptions = {
|
53 | body: body,
|
54 | headers: new Headers(request.headers),
|
55 | method: method,
|
56 | };
|
57 | if (typeof AbortController !== "undefined") {
|
58 | requestOptions["signal"] = abortSignal;
|
59 | }
|
60 | fetchRequest = new Request(url, requestOptions);
|
61 | raceOfPromises = [
|
62 | fetch(fetchRequest).then(function (response) {
|
63 | var e_1, _a;
|
64 | var fetchHeaders = response.headers;
|
65 | var transformedHeaders = {};
|
66 | try {
|
67 | for (var _b = __values(fetchHeaders.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
68 | var pair = _c.value;
|
69 | transformedHeaders[pair[0]] = pair[1];
|
70 | }
|
71 | }
|
72 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
73 | finally {
|
74 | try {
|
75 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
76 | }
|
77 | finally { if (e_1) throw e_1.error; }
|
78 | }
|
79 | var hasReadableStream = response.body !== undefined;
|
80 | if (!hasReadableStream) {
|
81 | return response.blob().then(function (body) { return ({
|
82 | response: new HttpResponse({
|
83 | headers: transformedHeaders,
|
84 | statusCode: response.status,
|
85 | body: body,
|
86 | }),
|
87 | }); });
|
88 | }
|
89 | return {
|
90 | response: new HttpResponse({
|
91 | headers: transformedHeaders,
|
92 | statusCode: response.status,
|
93 | body: response.body,
|
94 | }),
|
95 | };
|
96 | }),
|
97 | requestTimeout(requestTimeoutInMs),
|
98 | ];
|
99 | if (abortSignal) {
|
100 | raceOfPromises.push(new Promise(function (resolve, reject) {
|
101 | abortSignal.onabort = function () {
|
102 | var abortError = new Error("Request aborted");
|
103 | abortError.name = "AbortError";
|
104 | reject(abortError);
|
105 | };
|
106 | }));
|
107 | }
|
108 | return [2, Promise.race(raceOfPromises)];
|
109 | }
|
110 | });
|
111 | });
|
112 | };
|
113 | return FetchHttpHandler;
|
114 | }());
|
115 | export { FetchHttpHandler };
|