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