@smithy/fetch-http-handler
Advanced tools
+2
-55
| const { buildQueryString, HttpResponse } = require("@smithy/core/protocols"); | ||
| const { fromBase64 } = require("@smithy/core/serde"); | ||
| const { streamCollector } = require("@smithy/core/serde"); | ||
| exports.streamCollector = streamCollector; | ||
@@ -175,57 +176,3 @@ function createRequest(url, requestOptions) { | ||
| 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()); | ||
| } | ||
| return collectBlob(stream); | ||
| } | ||
| return collectStream(stream); | ||
| }; | ||
| async function collectBlob(blob) { | ||
| const base64 = await readToBase64(blob); | ||
| const arrayBuffer = fromBase64(base64); | ||
| return new Uint8Array(arrayBuffer); | ||
| } | ||
| 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; | ||
| } | ||
| isDone = done; | ||
| } | ||
| const collected = new Uint8Array(length); | ||
| let offset = 0; | ||
| for (const chunk of chunks) { | ||
| collected.set(chunk, offset); | ||
| offset += chunk.length; | ||
| } | ||
| return collected; | ||
| } | ||
| 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); | ||
| }); | ||
| } | ||
| exports.FetchHttpHandler = FetchHttpHandler; | ||
| exports.keepAliveSupport = keepAliveSupport; | ||
| exports.streamCollector = streamCollector; |
+2
-2
@@ -1,2 +0,2 @@ | ||
| export * from "./fetch-http-handler"; | ||
| export * from "./stream-collector"; | ||
| export { FetchHttpHandler, keepAliveSupport, } from "./fetch-http-handler"; | ||
| export { streamCollector } from "@smithy/core/serde"; |
@@ -1,2 +0,2 @@ | ||
| export * from "./fetch-http-handler"; | ||
| export * from "./stream-collector"; | ||
| export { type AdditionalRequestParameters, FetchHttpHandler, type FetchHttpHandlerOptions, keepAliveSupport, } from "./fetch-http-handler"; | ||
| export { streamCollector } from "@smithy/core/serde"; |
+2
-2
| { | ||
| "name": "@smithy/fetch-http-handler", | ||
| "version": "5.5.2", | ||
| "version": "5.6.0", | ||
| "description": "Provides a way to make requests", | ||
@@ -30,3 +30,3 @@ "scripts": { | ||
| "dependencies": { | ||
| "@smithy/core": "^3.26.0", | ||
| "@smithy/core": "^3.27.0", | ||
| "@smithy/types": "^4.15.0", | ||
@@ -33,0 +33,0 @@ "tslib": "^2.6.2" |
| import { fromBase64 } from "@smithy/core/serde"; | ||
| export 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()); | ||
| } | ||
| return collectBlob(stream); | ||
| } | ||
| return collectStream(stream); | ||
| }; | ||
| async function collectBlob(blob) { | ||
| const base64 = await readToBase64(blob); | ||
| const arrayBuffer = fromBase64(base64); | ||
| return new Uint8Array(arrayBuffer); | ||
| } | ||
| 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; | ||
| } | ||
| isDone = done; | ||
| } | ||
| const collected = new Uint8Array(length); | ||
| let offset = 0; | ||
| for (const chunk of chunks) { | ||
| collected.set(chunk, offset); | ||
| offset += chunk.length; | ||
| } | ||
| return collected; | ||
| } | ||
| 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); | ||
| }); | ||
| } |
| import type { StreamCollector } from "@smithy/types"; | ||
| export declare const streamCollector: StreamCollector; |
29669
-10.57%12
-14.29%397
-21.23%Updated