1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.FetchHttpHandler = void 0;
|
4 | const protocol_http_1 = require("@aws-sdk/protocol-http");
|
5 | const querystring_builder_1 = require("@aws-sdk/querystring-builder");
|
6 | const request_timeout_1 = require("./request-timeout");
|
7 | class FetchHttpHandler {
|
8 | constructor(options) {
|
9 | if (typeof options === "function") {
|
10 | this.configProvider = async () => (await options()) || {};
|
11 | }
|
12 | else {
|
13 | this.config = options !== null && options !== void 0 ? options : {};
|
14 | }
|
15 | }
|
16 | destroy() {
|
17 | }
|
18 | async handle(request, { abortSignal } = {}) {
|
19 | if (!this.config && this.configProvider) {
|
20 | this.config = await this.configProvider();
|
21 | }
|
22 | const requestTimeoutInMs = this.config.requestTimeout;
|
23 | if (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted) {
|
24 | const abortError = new Error("Request aborted");
|
25 | abortError.name = "AbortError";
|
26 | return Promise.reject(abortError);
|
27 | }
|
28 | let path = request.path;
|
29 | if (request.query) {
|
30 | const queryString = (0, querystring_builder_1.buildQueryString)(request.query);
|
31 | if (queryString) {
|
32 | path += `?${queryString}`;
|
33 | }
|
34 | }
|
35 | const { port, method } = request;
|
36 | const url = `${request.protocol}//${request.hostname}${port ? `:${port}` : ""}${path}`;
|
37 | const body = method === "GET" || method === "HEAD" ? undefined : request.body;
|
38 | const requestOptions = {
|
39 | body,
|
40 | headers: new Headers(request.headers),
|
41 | method: method,
|
42 | };
|
43 | if (typeof AbortController !== "undefined") {
|
44 | requestOptions["signal"] = abortSignal;
|
45 | }
|
46 | const fetchRequest = new Request(url, requestOptions);
|
47 | const raceOfPromises = [
|
48 | fetch(fetchRequest).then((response) => {
|
49 | const fetchHeaders = response.headers;
|
50 | const transformedHeaders = {};
|
51 | for (const pair of fetchHeaders.entries()) {
|
52 | transformedHeaders[pair[0]] = pair[1];
|
53 | }
|
54 | const hasReadableStream = response.body !== undefined;
|
55 | if (!hasReadableStream) {
|
56 | return response.blob().then((body) => ({
|
57 | response: new protocol_http_1.HttpResponse({
|
58 | headers: transformedHeaders,
|
59 | statusCode: response.status,
|
60 | body,
|
61 | }),
|
62 | }));
|
63 | }
|
64 | return {
|
65 | response: new protocol_http_1.HttpResponse({
|
66 | headers: transformedHeaders,
|
67 | statusCode: response.status,
|
68 | body: response.body,
|
69 | }),
|
70 | };
|
71 | }),
|
72 | (0, request_timeout_1.requestTimeout)(requestTimeoutInMs),
|
73 | ];
|
74 | if (abortSignal) {
|
75 | raceOfPromises.push(new Promise((resolve, reject) => {
|
76 | abortSignal.onabort = () => {
|
77 | const abortError = new Error("Request aborted");
|
78 | abortError.name = "AbortError";
|
79 | reject(abortError);
|
80 | };
|
81 | }));
|
82 | }
|
83 | return Promise.race(raceOfPromises);
|
84 | }
|
85 | }
|
86 | exports.FetchHttpHandler = FetchHttpHandler;
|