@smithy/fetch-http-handler
Advanced tools
+190
-238
@@ -1,264 +0,216 @@ | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| 'use strict'; | ||
| // src/index.ts | ||
| var index_exports = {}; | ||
| __export(index_exports, { | ||
| FetchHttpHandler: () => FetchHttpHandler, | ||
| keepAliveSupport: () => keepAliveSupport, | ||
| streamCollector: () => streamCollector | ||
| }); | ||
| module.exports = __toCommonJS(index_exports); | ||
| var protocolHttp = require('@smithy/protocol-http'); | ||
| var querystringBuilder = require('@smithy/querystring-builder'); | ||
| var utilBase64 = require('@smithy/util-base64'); | ||
| // src/fetch-http-handler.ts | ||
| var import_protocol_http = require("@smithy/protocol-http"); | ||
| var import_querystring_builder = require("@smithy/querystring-builder"); | ||
| // src/create-request.ts | ||
| function createRequest(url, requestOptions) { | ||
| return new Request(url, requestOptions); | ||
| return new Request(url, requestOptions); | ||
| } | ||
| __name(createRequest, "createRequest"); | ||
| // src/request-timeout.ts | ||
| function requestTimeout(timeoutInMs = 0) { | ||
| return new Promise((resolve, reject) => { | ||
| if (timeoutInMs) { | ||
| setTimeout(() => { | ||
| const timeoutError = new Error(`Request did not complete within ${timeoutInMs} ms`); | ||
| timeoutError.name = "TimeoutError"; | ||
| reject(timeoutError); | ||
| }, timeoutInMs); | ||
| } | ||
| }); | ||
| return new Promise((resolve, reject) => { | ||
| if (timeoutInMs) { | ||
| setTimeout(() => { | ||
| const timeoutError = new Error(`Request did not complete within ${timeoutInMs} ms`); | ||
| timeoutError.name = "TimeoutError"; | ||
| reject(timeoutError); | ||
| }, timeoutInMs); | ||
| } | ||
| }); | ||
| } | ||
| __name(requestTimeout, "requestTimeout"); | ||
| // src/fetch-http-handler.ts | ||
| var keepAliveSupport = { | ||
| supported: void 0 | ||
| const keepAliveSupport = { | ||
| supported: undefined, | ||
| }; | ||
| var FetchHttpHandler = class _FetchHttpHandler { | ||
| static { | ||
| __name(this, "FetchHttpHandler"); | ||
| } | ||
| /** | ||
| * @returns the input if it is an HttpHandler of any class, | ||
| * or instantiates a new instance of this handler. | ||
| */ | ||
| static create(instanceOrOptions) { | ||
| if (typeof instanceOrOptions?.handle === "function") { | ||
| return instanceOrOptions; | ||
| class FetchHttpHandler { | ||
| config; | ||
| configProvider; | ||
| static create(instanceOrOptions) { | ||
| if (typeof instanceOrOptions?.handle === "function") { | ||
| return instanceOrOptions; | ||
| } | ||
| return new FetchHttpHandler(instanceOrOptions); | ||
| } | ||
| return new _FetchHttpHandler(instanceOrOptions); | ||
| } | ||
| constructor(options) { | ||
| if (typeof options === "function") { | ||
| this.configProvider = options().then((opts) => opts || {}); | ||
| } else { | ||
| this.config = options ?? {}; | ||
| this.configProvider = Promise.resolve(this.config); | ||
| constructor(options) { | ||
| if (typeof options === "function") { | ||
| this.configProvider = options().then((opts) => opts || {}); | ||
| } | ||
| else { | ||
| this.config = options ?? {}; | ||
| this.configProvider = Promise.resolve(this.config); | ||
| } | ||
| if (keepAliveSupport.supported === undefined) { | ||
| keepAliveSupport.supported = Boolean(typeof Request !== "undefined" && "keepalive" in createRequest("https://[::1]")); | ||
| } | ||
| } | ||
| if (keepAliveSupport.supported === void 0) { | ||
| keepAliveSupport.supported = Boolean( | ||
| typeof Request !== "undefined" && "keepalive" in createRequest("https://[::1]") | ||
| ); | ||
| destroy() { | ||
| } | ||
| } | ||
| destroy() { | ||
| } | ||
| async handle(request, { abortSignal, requestTimeout: requestTimeout2 } = {}) { | ||
| if (!this.config) { | ||
| this.config = await this.configProvider; | ||
| async handle(request, { abortSignal, requestTimeout: requestTimeout$1 } = {}) { | ||
| if (!this.config) { | ||
| this.config = await this.configProvider; | ||
| } | ||
| const requestTimeoutInMs = requestTimeout$1 ?? this.config.requestTimeout; | ||
| const keepAlive = this.config.keepAlive === true; | ||
| const credentials = this.config.credentials; | ||
| if (abortSignal?.aborted) { | ||
| const abortError = new Error("Request aborted"); | ||
| abortError.name = "AbortError"; | ||
| return Promise.reject(abortError); | ||
| } | ||
| let path = request.path; | ||
| const queryString = querystringBuilder.buildQueryString(request.query || {}); | ||
| if (queryString) { | ||
| path += `?${queryString}`; | ||
| } | ||
| if (request.fragment) { | ||
| path += `#${request.fragment}`; | ||
| } | ||
| let auth = ""; | ||
| if (request.username != null || request.password != null) { | ||
| const username = request.username ?? ""; | ||
| const password = request.password ?? ""; | ||
| auth = `${username}:${password}@`; | ||
| } | ||
| const { port, method } = request; | ||
| const url = `${request.protocol}//${auth}${request.hostname}${port ? `:${port}` : ""}${path}`; | ||
| const body = method === "GET" || method === "HEAD" ? undefined : request.body; | ||
| const requestOptions = { | ||
| body, | ||
| headers: new Headers(request.headers), | ||
| method: method, | ||
| credentials, | ||
| }; | ||
| if (this.config?.cache) { | ||
| requestOptions.cache = this.config.cache; | ||
| } | ||
| if (body) { | ||
| requestOptions.duplex = "half"; | ||
| } | ||
| if (typeof AbortController !== "undefined") { | ||
| requestOptions.signal = abortSignal; | ||
| } | ||
| if (keepAliveSupport.supported) { | ||
| requestOptions.keepalive = keepAlive; | ||
| } | ||
| if (typeof this.config.requestInit === "function") { | ||
| Object.assign(requestOptions, this.config.requestInit(request)); | ||
| } | ||
| let removeSignalEventListener = () => { }; | ||
| const fetchRequest = createRequest(url, requestOptions); | ||
| const raceOfPromises = [ | ||
| fetch(fetchRequest).then((response) => { | ||
| const fetchHeaders = response.headers; | ||
| const transformedHeaders = {}; | ||
| for (const pair of fetchHeaders.entries()) { | ||
| transformedHeaders[pair[0]] = pair[1]; | ||
| } | ||
| const hasReadableStream = response.body != undefined; | ||
| if (!hasReadableStream) { | ||
| return response.blob().then((body) => ({ | ||
| response: new protocolHttp.HttpResponse({ | ||
| headers: transformedHeaders, | ||
| reason: response.statusText, | ||
| statusCode: response.status, | ||
| body, | ||
| }), | ||
| })); | ||
| } | ||
| return { | ||
| response: new protocolHttp.HttpResponse({ | ||
| headers: transformedHeaders, | ||
| reason: response.statusText, | ||
| statusCode: response.status, | ||
| body: response.body, | ||
| }), | ||
| }; | ||
| }), | ||
| requestTimeout(requestTimeoutInMs), | ||
| ]; | ||
| if (abortSignal) { | ||
| raceOfPromises.push(new Promise((resolve, reject) => { | ||
| const onAbort = () => { | ||
| const abortError = new Error("Request aborted"); | ||
| abortError.name = "AbortError"; | ||
| reject(abortError); | ||
| }; | ||
| if (typeof abortSignal.addEventListener === "function") { | ||
| const signal = abortSignal; | ||
| signal.addEventListener("abort", onAbort, { once: true }); | ||
| removeSignalEventListener = () => signal.removeEventListener("abort", onAbort); | ||
| } | ||
| else { | ||
| abortSignal.onabort = onAbort; | ||
| } | ||
| })); | ||
| } | ||
| return Promise.race(raceOfPromises).finally(removeSignalEventListener); | ||
| } | ||
| const requestTimeoutInMs = requestTimeout2 ?? this.config.requestTimeout; | ||
| const keepAlive = this.config.keepAlive === true; | ||
| const credentials = this.config.credentials; | ||
| if (abortSignal?.aborted) { | ||
| const abortError = new Error("Request aborted"); | ||
| abortError.name = "AbortError"; | ||
| return Promise.reject(abortError); | ||
| updateHttpClientConfig(key, value) { | ||
| this.config = undefined; | ||
| this.configProvider = this.configProvider.then((config) => { | ||
| config[key] = value; | ||
| return config; | ||
| }); | ||
| } | ||
| let path = request.path; | ||
| const queryString = (0, import_querystring_builder.buildQueryString)(request.query || {}); | ||
| if (queryString) { | ||
| path += `?${queryString}`; | ||
| httpHandlerConfigs() { | ||
| return this.config ?? {}; | ||
| } | ||
| if (request.fragment) { | ||
| path += `#${request.fragment}`; | ||
| } | ||
| let auth = ""; | ||
| if (request.username != null || request.password != null) { | ||
| const username = request.username ?? ""; | ||
| const password = request.password ?? ""; | ||
| auth = `${username}:${password}@`; | ||
| } | ||
| const { port, method } = request; | ||
| const url = `${request.protocol}//${auth}${request.hostname}${port ? `:${port}` : ""}${path}`; | ||
| const body = method === "GET" || method === "HEAD" ? void 0 : request.body; | ||
| const requestOptions = { | ||
| body, | ||
| headers: new Headers(request.headers), | ||
| method, | ||
| credentials | ||
| }; | ||
| if (this.config?.cache) { | ||
| requestOptions.cache = this.config.cache; | ||
| } | ||
| if (body) { | ||
| requestOptions.duplex = "half"; | ||
| } | ||
| if (typeof AbortController !== "undefined") { | ||
| requestOptions.signal = abortSignal; | ||
| } | ||
| if (keepAliveSupport.supported) { | ||
| requestOptions.keepalive = keepAlive; | ||
| } | ||
| if (typeof this.config.requestInit === "function") { | ||
| Object.assign(requestOptions, this.config.requestInit(request)); | ||
| } | ||
| let removeSignalEventListener = /* @__PURE__ */ __name(() => { | ||
| }, "removeSignalEventListener"); | ||
| const fetchRequest = createRequest(url, requestOptions); | ||
| const raceOfPromises = [ | ||
| fetch(fetchRequest).then((response) => { | ||
| const fetchHeaders = response.headers; | ||
| const transformedHeaders = {}; | ||
| for (const pair of fetchHeaders.entries()) { | ||
| transformedHeaders[pair[0]] = pair[1]; | ||
| } | ||
| const streamCollector = async (stream) => { | ||
| if ((typeof Blob === "function" && stream instanceof Blob) || stream.constructor?.name === "Blob") { | ||
| if (Blob.prototype.arrayBuffer !== undefined) { | ||
| return new Uint8Array(await stream.arrayBuffer()); | ||
| } | ||
| const hasReadableStream = response.body != void 0; | ||
| if (!hasReadableStream) { | ||
| return response.blob().then((body2) => ({ | ||
| response: new import_protocol_http.HttpResponse({ | ||
| headers: transformedHeaders, | ||
| reason: response.statusText, | ||
| statusCode: response.status, | ||
| body: body2 | ||
| }) | ||
| })); | ||
| } | ||
| return { | ||
| response: new import_protocol_http.HttpResponse({ | ||
| headers: transformedHeaders, | ||
| reason: response.statusText, | ||
| statusCode: response.status, | ||
| body: response.body | ||
| }) | ||
| }; | ||
| }), | ||
| requestTimeout(requestTimeoutInMs) | ||
| ]; | ||
| if (abortSignal) { | ||
| raceOfPromises.push( | ||
| new Promise((resolve, reject) => { | ||
| const onAbort = /* @__PURE__ */ __name(() => { | ||
| const abortError = new Error("Request aborted"); | ||
| abortError.name = "AbortError"; | ||
| reject(abortError); | ||
| }, "onAbort"); | ||
| if (typeof abortSignal.addEventListener === "function") { | ||
| const signal = abortSignal; | ||
| signal.addEventListener("abort", onAbort, { once: true }); | ||
| removeSignalEventListener = /* @__PURE__ */ __name(() => signal.removeEventListener("abort", onAbort), "removeSignalEventListener"); | ||
| } else { | ||
| abortSignal.onabort = onAbort; | ||
| } | ||
| }) | ||
| ); | ||
| return collectBlob(stream); | ||
| } | ||
| return Promise.race(raceOfPromises).finally(removeSignalEventListener); | ||
| } | ||
| updateHttpClientConfig(key, value) { | ||
| this.config = void 0; | ||
| this.configProvider = this.configProvider.then((config) => { | ||
| config[key] = value; | ||
| return config; | ||
| }); | ||
| } | ||
| httpHandlerConfigs() { | ||
| return this.config ?? {}; | ||
| } | ||
| return collectStream(stream); | ||
| }; | ||
| // src/stream-collector.ts | ||
| var import_util_base64 = require("@smithy/util-base64"); | ||
| var streamCollector = /* @__PURE__ */ __name(async (stream) => { | ||
| if (typeof Blob === "function" && stream instanceof Blob || stream.constructor?.name === "Blob") { | ||
| if (Blob.prototype.arrayBuffer !== void 0) { | ||
| return new Uint8Array(await stream.arrayBuffer()); | ||
| } | ||
| return collectBlob(stream); | ||
| } | ||
| return collectStream(stream); | ||
| }, "streamCollector"); | ||
| async function collectBlob(blob) { | ||
| const base64 = await readToBase64(blob); | ||
| const arrayBuffer = (0, import_util_base64.fromBase64)(base64); | ||
| return new Uint8Array(arrayBuffer); | ||
| const base64 = await readToBase64(blob); | ||
| const arrayBuffer = utilBase64.fromBase64(base64); | ||
| return new Uint8Array(arrayBuffer); | ||
| } | ||
| __name(collectBlob, "collectBlob"); | ||
| async function collectStream(stream) { | ||
| const chunks = []; | ||
| const reader = stream.getReader(); | ||
| let isDone = false; | ||
| let length = 0; | ||
| while (!isDone) { | ||
| const { done, value } = await reader.read(); | ||
| if (value) { | ||
| chunks.push(value); | ||
| length += value.length; | ||
| const chunks = []; | ||
| const reader = stream.getReader(); | ||
| let isDone = false; | ||
| let length = 0; | ||
| while (!isDone) { | ||
| const { done, value } = await reader.read(); | ||
| if (value) { | ||
| chunks.push(value); | ||
| length += value.length; | ||
| } | ||
| isDone = done; | ||
| } | ||
| isDone = done; | ||
| } | ||
| const collected = new Uint8Array(length); | ||
| let offset = 0; | ||
| for (const chunk of chunks) { | ||
| collected.set(chunk, offset); | ||
| offset += chunk.length; | ||
| } | ||
| return collected; | ||
| const collected = new Uint8Array(length); | ||
| let offset = 0; | ||
| for (const chunk of chunks) { | ||
| collected.set(chunk, offset); | ||
| offset += chunk.length; | ||
| } | ||
| return collected; | ||
| } | ||
| __name(collectStream, "collectStream"); | ||
| function readToBase64(blob) { | ||
| return new Promise((resolve, reject) => { | ||
| const reader = new FileReader(); | ||
| reader.onloadend = () => { | ||
| if (reader.readyState !== 2) { | ||
| return reject(new Error("Reader aborted too early")); | ||
| } | ||
| const result = reader.result ?? ""; | ||
| const commaIndex = result.indexOf(","); | ||
| const dataOffset = commaIndex > -1 ? commaIndex + 1 : result.length; | ||
| resolve(result.substring(dataOffset)); | ||
| }; | ||
| reader.onabort = () => reject(new Error("Read aborted")); | ||
| reader.onerror = () => reject(reader.error); | ||
| reader.readAsDataURL(blob); | ||
| }); | ||
| return new Promise((resolve, reject) => { | ||
| const reader = new FileReader(); | ||
| reader.onloadend = () => { | ||
| if (reader.readyState !== 2) { | ||
| return reject(new Error("Reader aborted too early")); | ||
| } | ||
| const result = (reader.result ?? ""); | ||
| const commaIndex = result.indexOf(","); | ||
| const dataOffset = commaIndex > -1 ? commaIndex + 1 : result.length; | ||
| resolve(result.substring(dataOffset)); | ||
| }; | ||
| reader.onabort = () => reject(new Error("Read aborted")); | ||
| reader.onerror = () => reject(reader.error); | ||
| reader.readAsDataURL(blob); | ||
| }); | ||
| } | ||
| __name(readToBase64, "readToBase64"); | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| keepAliveSupport, | ||
| FetchHttpHandler, | ||
| streamCollector | ||
| }); | ||
| exports.FetchHttpHandler = FetchHttpHandler; | ||
| exports.keepAliveSupport = keepAliveSupport; | ||
| exports.streamCollector = streamCollector; |
@@ -9,2 +9,4 @@ import { HttpResponse } from "@smithy/protocol-http"; | ||
| export class FetchHttpHandler { | ||
| config; | ||
| configProvider; | ||
| static create(instanceOrOptions) { | ||
@@ -11,0 +13,0 @@ if (typeof instanceOrOptions?.handle === "function") { |
+6
-6
| { | ||
| "name": "@smithy/fetch-http-handler", | ||
| "version": "5.2.1", | ||
| "version": "5.3.0", | ||
| "description": "Provides a way to make requests", | ||
@@ -31,10 +31,10 @@ "scripts": { | ||
| "dependencies": { | ||
| "@smithy/protocol-http": "^5.2.1", | ||
| "@smithy/querystring-builder": "^4.1.1", | ||
| "@smithy/types": "^4.5.0", | ||
| "@smithy/util-base64": "^4.1.0", | ||
| "@smithy/protocol-http": "^5.3.0", | ||
| "@smithy/querystring-builder": "^4.2.0", | ||
| "@smithy/types": "^4.6.0", | ||
| "@smithy/util-base64": "^4.2.0", | ||
| "tslib": "^2.6.2" | ||
| }, | ||
| "devDependencies": { | ||
| "@smithy/abort-controller": "^4.1.1", | ||
| "@smithy/abort-controller": "^4.2.0", | ||
| "concurrently": "7.0.0", | ||
@@ -41,0 +41,0 @@ "downlevel-dts": "0.10.1", |
| module.exports = require("./index.js"); |
| module.exports = require("./index.js"); |
| module.exports = require("./index.js"); |
| module.exports = require("./index.js"); |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
9
-18.18%34426
-2.91%19
-17.39%526
-7.72%Updated
Updated
Updated