### Install undio Package Source: https://github.com/unjs/undio/blob/main/README.md This snippet shows how to install the undio package using various package managers like npm, yarn, pnpm, and bun. It covers auto-detection with npx and specific commands for each manager. ```sh # ✨ Auto-detect npx nypm install undio # npm npm install undio # yarn yarn add undio # pnpm pnpm install undio # bun bun install undio # deno deno install undio ``` -------------------------------- ### Runtime Type Checking and Assertions with undio Source: https://github.com/unjs/undio/blob/main/README.md Provides examples of using undio's runtime type checking functions. It demonstrates how to check if a value is a ReadableStream using `isReadableStream` and how to assert that a value is an ArrayBuffer using `assertArrayBuffer`, which throws an error if the assertion fails. ```ts import { isReadableStream, assertArrayBuffer } from "undio"; if (isReadableStream(value)) { /* do something */ } assertArrayBuffer(value); // Throws an error if value is not ArrayBuffer // do something ``` -------------------------------- ### Import undio Modules Source: https://github.com/unjs/undio/blob/main/README.md Demonstrates how to import utilities from the undio library for different JavaScript module systems: ESM (for Node.js, Bun, Deno), CommonJS (for legacy Node.js), and CDN (for browsers and environments supporting external scripts). ```js // ESM (Node.js, Bun, Deno) import {} from "undio"; ``` ```js // CommonJS (Legacy Node.js) const {} = require("undio"); ``` ```js // CDN (Deno, Bun and Browsers) import {} from "https://esm.sh/undio"; ``` -------------------------------- ### Auto Convert Data Types with undio Source: https://github.com/unjs/undio/blob/main/README.md Illustrates the automatic type detection and conversion capabilities of undio. It shows how to convert various input types to Text or ReadableStream using `toText` and `toReadableStream`, and how to detect the type of a value using `detectType`. ```ts import { detectType, toText, toReadableStream } from "undio"; // Convert any supported type (auto-detected) const string = await toText(value); const stream = await toReadableStream(value); // "ArrayBuffer" | "Blob"| "DataView" | "NumberArray" | "ReadableStream" | "String" | "Uint8Array"; const type = detectType(value); ``` -------------------------------- ### Convert Base64 to Various Types with undio Source: https://github.com/unjs/undio/blob/main/README.md Demonstrates utility functions for converting Base64 strings to other data types such as ArrayBuffer, Blob, DataView, Number Array, ReadableStream, Response, and Text. Includes assertion and testing functions for Base64. ```js import { base64ToArrayBuffer, base64ToBlob, base64ToDataView, base64ToNumberArray, base64ToReadableStream, base64ToResponse, assertBase64 } from "undio"; // Example conversion: const base64String = "SGVsbG8gV29ybGQh"; // "Hello World!" const arrayBuffer = base64ToArrayBuffer(base64String); // Example assertion: assertBase64(base64String); // Will not throw if input is valid Base64 ``` -------------------------------- ### Function: textToBase64(string, opts?) Source: https://github.com/unjs/undio/blob/main/README.md This function converts Text to Base64. It takes a string and optional options as input. ```APIDOC ## textToBase64(string, opts?) ### Description Converts from Text to Base64. ### Method Function ### Endpoint textToBase64(string, opts?) ### Parameters #### Path Parameters - **string** (string) - Required - The string to convert. - **opts** (object, optional) - Optional options. ### Response #### Success Response (200) - **string** (string) - The converted Base64 string. ``` -------------------------------- ### Function: toResponse(input) Source: https://github.com/unjs/undio/blob/main/README.md This function converts any value to a Response. This function takes any value as input. ```APIDOC ## toResponse(input) ### Description Converts from any value to Response. ### Method Function ### Endpoint toResponse(input) ### Parameters #### Path Parameters - **input** (any) - Required - The value to convert. ### Response #### Success Response (200) - **Response** (Response) - The converted Response. ``` -------------------------------- ### Function: responseToBase64(response, base64Options?) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Response to a Base64 string. It takes a Response and optional base64 options as input. ```APIDOC ## responseToBase64(response, base64Options?) ### Description Converts from Response to Base64. ### Method Function ### Endpoint responseToBase64(response, base64Options?) ### Parameters #### Path Parameters - **response** (Response) - Required - The Response to convert. - **base64Options** (object, optional) - Optional Base64 options. ### Response #### Success Response (200) - **string** (string) - The converted Base64 string. ``` -------------------------------- ### Base64 Encoding and Decoding Source: https://context7.com/unjs/undio/llms.txt Utilities for handling Base64 data, including encoding text to Base64, decoding Base64 to various formats, and validating Base64 data URLs. ```APIDOC ## Base64 Encoding and Decoding ### Description Handle Base64 data URLs with proper encoding, decoding, and MIME type support. ### Methods - `textToBase64(text: string, options?: { dataURL?: boolean, type?: string }): string` - `uint8ArrayToBase64(uint8Array: Uint8Array, options?: { dataURL?: boolean, type?: string, urlSafe?: boolean }): string` - `base64ToText(base64: string): string` - `base64ToUint8Array(base64: string): Uint8Array` - `base64ToBlob(base64: string): Blob` - `base64ToArrayBuffer(base64: string): ArrayBuffer` - `isBase64DataURL(str: string): boolean` ### Usage Examples ```javascript import { base64ToText, base64ToUint8Array, base64ToBlob, base64ToArrayBuffer, textToBase64, uint8ArrayToBase64, isBase64DataURL } from 'undio'; // Create Base64 from text const originalText = "Hello, World! 🌍"; const base64 = textToBase64(originalText, { dataURL: true, type: 'text/plain' }); console.log(base64); // "data:text/plain;base64,..." // Validate Base64 data URL console.log(isBase64DataURL(base64)); // true console.log(isBase64DataURL("not a base64")); // false // Decode Base64 to text const decodedText = base64ToText(base64); console.log(decodedText === originalText); // true // Decode Base64 to Uint8Array const uint8 = base64ToUint8Array(base64); console.log(uint8); // Uint8Array with decoded bytes // Decode Base64 to Blob with type preservation const blob = base64ToBlob(base64); console.log(blob.type); // "text/plain" // Decode Base64 to ArrayBuffer const buffer = base64ToArrayBuffer(base64); console.log(buffer.byteLength); // Size of decoded data // URL-safe Base64 encoding const urlSafeBase64 = uint8ArrayToBase64( new Uint8Array([1, 2, 3]), { urlSafe: true } ); console.log(urlSafeBase64); // Base64 string with - and _ instead of + and / ``` ``` -------------------------------- ### Base64 Encoding and Decoding with Data URL Support Source: https://context7.com/unjs/undio/llms.txt This snippet covers Base64 encoding and decoding functionalities, including support for data URLs and MIME types. It allows conversion between text and Base64, validation of Base64 data URLs, and decoding to Uint8Array, Blob, or ArrayBuffer. It also demonstrates URL-safe Base64 encoding. ```javascript import { base64ToText, base64ToUint8Array, base64ToBlob, base64ToArrayBuffer, textToBase64, uint8ArrayToBase64, isBase64DataURL } from 'undio'; // Create Base64 from text const originalText = "Hello, World! 🌍"; const base64 = textToBase64(originalText, { dataURL: true, type: 'text/plain' }); console.log(base64); // "data:text/plain;base64,SGVsbG8sIFdvcmxkIS ^{"} // Validate Base64 data URL console.log(isBase64DataURL(base64)); // true console.log(isBase64DataURL("not a base64")); // false // Decode Base64 to text const decodedText = base64ToText(base64); console.log(decodedText === originalText); // true // Decode Base64 to Uint8Array const uint8 = base64ToUint8Array(base64); console.log(uint8); // Uint8Array with decoded bytes // Decode Base64 to Blob with type preservation const blob = base64ToBlob(base64); console.log(blob.type); // "text/plain" // Decode Base64 to ArrayBuffer const buffer = base64ToArrayBuffer(base64); console.log(buffer.byteLength); // Size of decoded data // URL-safe Base64 encoding const urlSafeBase64 = uint8ArrayToBase64( new Uint8Array([1, 2, 3]), { urlSafe: true } ); console.log(urlSafeBase64); // Base64 string with - and _ instead of + and / ``` -------------------------------- ### Function: toText(input) Source: https://github.com/unjs/undio/blob/main/README.md This function converts any value to Text. It takes an input value. ```APIDOC ## toText(input) ### Description Converts from any value to Text. ### Method Function ### Endpoint toText(input) ### Parameters #### Path Parameters - **input** (any) - Required - The value to convert. ### Response #### Success Response (200) - **string** (string) - The converted Text. ``` -------------------------------- ### Function: textToBlob(string, options?) Source: https://github.com/unjs/undio/blob/main/README.md This function converts Text to a Blob. It takes a string and optional options as input. ```APIDOC ## textToBlob(string, options?) ### Description Converts from Text to Blob. ### Method Function ### Endpoint textToBlob(string, options?) ### Parameters #### Path Parameters - **string** (string) - Required - The string to convert. - **options** (object, optional) - Optional options. ### Response #### Success Response (200) - **Blob** (Blob) - The converted Blob. ``` -------------------------------- ### Convert Uint8Array to Text, ArrayBuffer, Blob, Base64, Number Array, ReadableStream Source: https://context7.com/unjs/undio/llms.txt This snippet demonstrates converting a Uint8Array to various formats. It shows how to transform binary data into human-readable text, ArrayBuffer, Blob, Base64 string, an array of numbers, or a ReadableStream. It requires the 'undio' library and handles binary data conversion efficiently. ```javascript import { uint8ArrayToText, uint8ArrayToArrayBuffer, uint8ArrayToBlob, uint8ArrayToBase64, uint8ArrayToNumberArray, uint8ArrayToReadableStream } from 'undio'; // Sample binary data const uint8 = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]); // Convert to text const text = uint8ArrayToText(uint8); console.log(text); // "Hello World" // Convert to ArrayBuffer const buffer = uint8ArrayToArrayBuffer(uint8); console.log(buffer.byteLength); // 11 // Convert to Blob const blob = uint8ArrayToBlob(uint8, { type: 'application/octet-stream' }); console.log(blob.size); // 11 // Convert to Base64 const base64 = uint8ArrayToBase64(uint8, { dataURL: true, type: 'text/plain' }); console.log(base64); // "data:text/plain;base64,SGVsbG8gV29ybGQ=" // Convert to number array const numbers = uint8ArrayToNumberArray(uint8); console.log(numbers); // [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] // Convert to ReadableStream const stream = uint8ArrayToReadableStream(uint8); const reader = stream.getReader(); const { value, done } = await reader.read(); console.log(value); // Uint8Array [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] ``` -------------------------------- ### Function: responseToText(response) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Response to text. It takes a Response as input. ```APIDOC ## responseToText(response) ### Description Converts from Response to Text. ### Method Function ### Endpoint responseToText(response) ### Parameters #### Path Parameters - **response** (Response) - Required - The Response to convert. ### Response #### Success Response (200) - **string** (string) - The converted Text. ``` -------------------------------- ### Asynchronous Blob Conversions to Text, Uint8Array, ArrayBuffer, ReadableStream, Base64 Source: https://context7.com/unjs/undio/llms.txt This snippet demonstrates asynchronous conversions of Blob objects to various formats, including text, Uint8Array, ArrayBuffer, ReadableStream, and Base64. It also shows how to create a Blob from text. These operations are asynchronous due to the nature of reading Blob content. ```javascript import { blobToText, blobToUint8Array, blobToArrayBuffer, blobToReadableStream, blobToBase64, textToBlob } from 'undio'; // Create a Blob const blob = new Blob( [JSON.stringify({ message: "Hello World" })], { type: 'application/json' } ); // Convert to text (async) const text = await blobToText(blob); console.log(JSON.parse(text).message); // "Hello World" // Convert to Uint8Array (async) const uint8 = await blobToUint8Array(blob); console.log(uint8.length); // Byte length // Convert to ArrayBuffer (async) const buffer = await blobToArrayBuffer(blob); console.log(buffer.byteLength); // Byte length // Convert to ReadableStream const stream = blobToReadableStream(blob); const reader = stream.getReader(); const chunks = []; while (true) { const { done, value } = await reader.read(); if (done) break; chunks.push(value); } console.log(chunks.length); // Number of chunks read // Convert to Base64 const base64 = await blobToBase64(blob, { dataURL: true, type: 'application/json' }); console.log(base64.startsWith('data:application/json;base64,')); // true ``` -------------------------------- ### Function: uint8ArrayToBlob(uint8Array, options?) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Uint8Array to a Blob. It takes a Uint8Array and optional options as input. ```APIDOC ## uint8ArrayToBlob(uint8Array, options?) ### Description Converts from Uint8Array to Blob. ### Method Function ### Endpoint uint8ArrayToBlob(uint8Array, options?) ### Parameters #### Path Parameters - **uint8Array** (Uint8Array) - Required - The Uint8Array to convert. - **options** (object, optional) - Optional options. ### Response #### Success Response (200) - **Blob** (Blob) - The converted Blob. ``` -------------------------------- ### Function: uint8ArrayToBase64(uint8Array, base64Options?) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Uint8Array to Base64. It takes a Uint8Array and optional base64Options as input. ```APIDOC ## uint8ArrayToBase64(uint8Array, base64Options?) ### Description Converts from Uint8Array to Base64. ### Method Function ### Endpoint uint8ArrayToBase64(uint8Array, base64Options?) ### Parameters #### Path Parameters - **uint8Array** (Uint8Array) - Required - The Uint8Array to convert. - **base64Options** (object, optional) - Optional base64Options. ### Response #### Success Response (200) - **string** (string) - The converted Base64 string. ``` -------------------------------- ### Function: textToReadableStream(string) Source: https://github.com/unjs/undio/blob/main/README.md This function converts Text to a ReadableStream. It takes a string as input. ```APIDOC ## textToReadableStream(string) ### Description Converts from Text to ReadableStream. ### Method Function ### Endpoint textToReadableStream(string) ### Parameters #### Path Parameters - **string** (string) - Required - The string to convert. ### Response #### Success Response (200) - **ReadableStream** (ReadableStream) - The converted ReadableStream. ``` -------------------------------- ### Function: readableStreamToBlob(readableStream, options?) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a ReadableStream to a Blob. It takes a ReadableStream and optional options as input. ```APIDOC ## readableStreamToBlob(readableStream, options?) ### Description Converts a ReadableStream to a Blob. ### Method Function ### Endpoint readableStreamToBlob(readableStream, options?) ### Parameters #### Path Parameters - **readableStream** (ReadableStream) - Required - The ReadableStream to convert. - **options** (object, optional) - Optional options for the Blob. ### Response #### Success Response (200) - **Blob** (Blob) - The converted Blob. ``` -------------------------------- ### Function: assertUint8Array(input) Source: https://github.com/unjs/undio/blob/main/README.md This function asserts that input is an instance of Uint8Array or throws a TypeError. It takes an input as a parameter. ```APIDOC ## assertUint8Array(input) ### Description Asserts that input is an instance of Uint8Array or throw a TypeError. ### Method Function ### Endpoint assertUint8Array(input) ### Parameters #### Path Parameters - **input** (any) - Required - The input to check. ### Response #### Success Response (200) - **void** (void) - No return value on success, throws TypeError on failure. ``` -------------------------------- ### Function: isText(input) Source: https://github.com/unjs/undio/blob/main/README.md This function checks if the input is an instance of Text and returns true or false. It takes an input as a parameter. ```APIDOC ## isText(input) ### Description Tests if input is an instance of Text and return true or false. ### Method Function ### Endpoint isText(input) ### Parameters #### Path Parameters - **input** (any) - Required - The input to check. ### Response #### Success Response (200) - **boolean** (boolean) - Returns true if the input is a Text, false otherwise. ``` -------------------------------- ### Function: assertResponse(input) Source: https://github.com/unjs/undio/blob/main/README.md This function asserts that the input is an instance of Response or throws a TypeError. It takes an input as a parameter. ```APIDOC ## assertResponse(input) ### Description Asserts that input is an instance of Response or throw a TypeError. ### Method Function ### Endpoint assertResponse(input) ### Parameters #### Path Parameters - **input** (any) - Required - The input to check. ### Response #### Success Response (200) - **void** (void) - No return value on success, throws TypeError on failure. ``` -------------------------------- ### Function: responseToBlob(response) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Response to a Blob. It takes a Response as input. ```APIDOC ## responseToBlob(response) ### Description Converts from Response to Blob. ### Method Function ### Endpoint responseToBlob(response) ### Parameters #### Path Parameters - **response** (Response) - Required - The Response to convert. ### Response #### Success Response (200) - **Blob** (Blob) - The converted Blob. ``` -------------------------------- ### Function: isResponse(input) Source: https://github.com/unjs/undio/blob/main/README.md This function checks if the input is an instance of Response and returns true or false. It takes an input as a parameter. ```APIDOC ## isResponse(input) ### Description Tests if input is an instance of Response and return true or false. ### Method Function ### Endpoint isResponse(input) ### Parameters #### Path Parameters - **input** (any) - Required - The input to check. ### Response #### Success Response (200) - **boolean** (boolean) - Returns true if the input is a Response, false otherwise. ``` -------------------------------- ### Function: readableStreamToBase64(readableStream, base64Options?) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a ReadableStream to a Base64 string. It takes a ReadableStream and optional base64 options as input. ```APIDOC ## readableStreamToBase64(readableStream, base64Options?) ### Description Converts a ReadableStream to Base64. ### Method Function ### Endpoint readableStreamToBase64(readableStream, base64Options?) ### Parameters #### Path Parameters - **readableStream** (ReadableStream) - Required - The ReadableStream to convert. - **base64Options** (object, optional) - Optional Base64 options. ### Response #### Success Response (200) - **string** (string) - The converted Base64 string. ``` -------------------------------- ### Blob Conversions Source: https://context7.com/unjs/undio/llms.txt Asynchronous functions for converting Blob objects to other formats such as text, Uint8Array, ArrayBuffer, ReadableStream, and Base64. Also includes a function to create a Blob from text. ```APIDOC ## Blob Conversions ### Description Work with Blob objects and convert them to other formats asynchronously. ### Methods - `blobToText(blob: Blob): Promise` - `blobToUint8Array(blob: Blob): Promise` - `blobToArrayBuffer(blob: Blob): Promise` - `blobToReadableStream(blob: Blob): ReadableStream` - `blobToBase64(blob: Blob, options?: { dataURL?: boolean, type?: string }): Promise` - `textToBlob(text: string, options?: BlobPropertyBag): Blob` ### Usage Examples ```javascript import { blobToText, blobToUint8Array, blobToArrayBuffer, blobToReadableStream, blobToBase64, textToBlob } from 'undio'; // Create a Blob const blob = new Blob( [JSON.stringify({ message: "Hello World" })], { type: 'application/json' } ); // Convert to text (async) const text = await blobToText(blob); console.log(JSON.parse(text).message); // "Hello World" // Convert to Uint8Array (async) const uint8 = await blobToUint8Array(blob); console.log(uint8.length); // Byte length // Convert to ArrayBuffer (async) const buffer = await blobToArrayBuffer(blob); console.log(buffer.byteLength); // Byte length // Convert to ReadableStream const stream = blobToReadableStream(blob); const reader = stream.getReader(); const chunks = []; while (true) { const { done, value } = await reader.read(); if (done) break; chunks.push(value); } console.log(chunks.length); // Number of chunks read // Convert to Base64 const base64 = await blobToBase64(blob, { dataURL: true, type: 'application/json' }); console.log(base64.startsWith('data:application/json;base64,')); // true ``` ``` -------------------------------- ### Function: responseToDataView(response) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Response to a DataView. It takes a Response as input. ```APIDOC ## responseToDataView(response) ### Description Converts from Response to DataView. ### Method Function ### Endpoint responseToDataView(response) ### Parameters #### Path Parameters - **response** (Response) - Required - The Response to convert. ### Response #### Success Response (200) - **DataView** (DataView) - The converted DataView. ``` -------------------------------- ### Function: textToDataView(string) Source: https://github.com/unjs/undio/blob/main/README.md This function converts Text to DataView. It takes a string as input. ```APIDOC ## textToDataView(string) ### Description Converts from Text to DataView. ### Method Function ### Endpoint textToDataView(string) ### Parameters #### Path Parameters - **string** (string) - Required - The string to convert. ### Response #### Success Response (200) - **DataView** (DataView) - The converted DataView. ``` -------------------------------- ### Function: assertText(input) Source: https://github.com/unjs/undio/blob/main/README.md This function asserts that the input is an instance of Text or throws a TypeError. It takes an input as a parameter. ```APIDOC ## assertText(input) ### Description Asserts that input is an instance of Text or throw a TypeError. ### Method Function ### Endpoint assertText(input) ### Parameters #### Path Parameters - **input** (any) - Required - The input to check. ### Response #### Success Response (200) - **void** (void) - No return value on success, throws TypeError on failure. ``` -------------------------------- ### Uint8Array Conversions Source: https://context7.com/unjs/undio/llms.txt Provides functions to convert Uint8Array instances to various other formats including text, ArrayBuffer, Blob, Base64, number arrays, and ReadableStream. ```APIDOC ## Uint8Array Conversions ### Description Convert typed arrays to other formats with proper handling of binary data. ### Methods - `uint8ArrayToText(uint8Array: Uint8Array): string` - `uint8ArrayToArrayBuffer(uint8Array: Uint8Array): ArrayBuffer` - `uint8ArrayToBlob(uint8Array: Uint8Array, options?: BlobPropertyBag): Blob` - `uint8ArrayToBase64(uint8Array: Uint8Array, options?: { dataURL?: boolean, type?: string, urlSafe?: boolean }): string` - `uint8ArrayToNumberArray(uint8Array: Uint8Array): number[]` - `uint8ArrayToReadableStream(uint8Array: Uint8Array): ReadableStream` ### Usage Examples ```javascript import { uint8ArrayToText, uint8ArrayToArrayBuffer, uint8ArrayToBlob, uint8ArrayToBase64, uint8ArrayToNumberArray, uint8ArrayToReadableStream } from 'undio'; // Sample binary data const uint8 = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]); // Convert to text const text = uint8ArrayToText(uint8); console.log(text); // "Hello World" // Convert to ArrayBuffer const buffer = uint8ArrayToArrayBuffer(uint8); console.log(buffer.byteLength); // 11 // Convert to Blob const blob = uint8ArrayToBlob(uint8, { type: 'application/octet-stream' }); console.log(blob.size); // 11 // Convert to Base64 const base64 = uint8ArrayToBase64(uint8, { dataURL: true, type: 'text/plain' }); console.log(base64); // "data:text/plain;base64,SGVsbG8gV29ybGQ=" // Convert to number array const numbers = uint8ArrayToNumberArray(uint8); console.log(numbers); // [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] // Convert to ReadableStream const stream = uint8ArrayToReadableStream(uint8); const reader = stream.getReader(); const { value, done } = await reader.read(); console.log(value); // Uint8Array [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] ``` ``` -------------------------------- ### Auto-Detect Type Conversion with undio Source: https://context7.com/unjs/undio/llms.txt These functions automatically detect the input data type and convert it to the desired format (Text, Uint8Array, Blob, ArrayBuffer). They support various input types and handle asynchronous operations. ```javascript import { toText, toUint8Array, toBlob, toArrayBuffer } from 'undio'; // Convert any supported type to text const blob = new Blob(['Hello World']); const text = await toText(blob); console.log(text); // "Hello World" // Convert any type to Uint8Array const arrayBuffer = new ArrayBuffer(8); const uint8 = await toUint8Array(arrayBuffer); console.log(uint8); // Uint8Array(8) [0, 0, 0, 0, 0, 0, 0, 0] // Convert any type to Blob const string = "Hello World"; const blobResult = await toBlob(string); console.log(blobResult.size); // 11 // Convert any type to ArrayBuffer const uint8Array = new Uint8Array([72, 101, 108, 108, 111]); const buffer = await toArrayBuffer(uint8Array); console.log(buffer.byteLength); // 5 ``` -------------------------------- ### Function: responseToArrayBuffer(response) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Response to an ArrayBuffer. It takes a Response as input. ```APIDOC ## responseToArrayBuffer(response) ### Description Converts from Response to ArrayBuffer. ### Method Function ### Endpoint responseToArrayBuffer(response) ### Parameters #### Path Parameters - **response** (Response) - Required - The Response to convert. ### Response #### Success Response (200) - **ArrayBuffer** (ArrayBuffer) - The converted ArrayBuffer. ``` -------------------------------- ### Type Detection with undio Source: https://context7.com/unjs/undio/llms.txt The `detectType` function automatically identifies the data type of any supported value, returning a string representation of the type. It can handle a wide range of JavaScript data types. ```javascript import { detectType } from 'undio'; // Detect various data types const types = [ new ArrayBuffer(8), new Uint8Array([1, 2, 3]), new Blob(['hello']), 'text string', new DataView(new ArrayBuffer(8)), [1, 2, 3, 4], new ReadableStream(), new Response('data'), 'data:text/plain;base64,SGVsbG8=' ]; for (const value of types) { console.log(detectType(value)); } // Output: "ArrayBuffer", "Uint8Array", "Blob", "Text", "DataView", // "NumberArray", "ReadableStream", "Response", "Base64" ``` -------------------------------- ### JavaScript Type Validation and Assertions with undio Source: https://context7.com/unjs/undio/llms.txt Utilize undio's functions for runtime type checking (returning booleans) and type assertions (throwing errors on mismatch). This includes support for Text, Uint8Array, ArrayBuffer, Blob, ReadableStream, and Base64DataURL. ```javascript import { isText, isUint8Array, isArrayBuffer, isBlob, isReadableStream, isBase64DataURL, assertText, assertUint8Array, assertArrayBuffer } from 'undio'; // Type checking (returns boolean) const data1 = "Hello World"; console.log(isText(data1)); // true console.log(isUint8Array(data1)); // false const data2 = new Uint8Array([1, 2, 3]); console.log(isUint8Array(data2)); // true console.log(isArrayBuffer(data2)); // false const data3 = new ArrayBuffer(8); console.log(isArrayBuffer(data3)); // true const data4 = new Blob(['hello']); console.log(isBlob(data4)); // true const data5 = new ReadableStream(); console.log(isReadableStream(data5)); // true const data6 = "data:text/plain;base64,SGVsbG8="; console.log(isBase64DataURL(data6)); // true // Type assertions (throws TypeError if invalid) try { assertText("valid string"); // No error assertText(123); // Throws TypeError } catch (error) { console.log(error.message); // "Expected Text, got ..." } try { assertUint8Array(new Uint8Array([1, 2, 3])); // No error assertUint8Array([1, 2, 3]); // Throws TypeError } catch (error) { console.log(error.message); // "Expected Uint8Array, got ..." } try { assertArrayBuffer(new ArrayBuffer(8)); // No error assertArrayBuffer({}); // Throws TypeError } catch (error) { console.log(error.message); // "Expected ArrayBuffer, got ..." } ``` -------------------------------- ### Function: toUint8Array(input) Source: https://github.com/unjs/undio/blob/main/README.md This function converts any value to Uint8Array. It takes an input value. ```APIDOC ## toUint8Array(input) ### Description Converts from any value to Uint8Array. ### Method Function ### Endpoint toUint8Array(input) ### Parameters #### Path Parameters - **input** (any) - Required - The value to convert. ### Response #### Success Response (200) - **Uint8Array** (Uint8Array) - The converted Uint8Array. ``` -------------------------------- ### JavaScript Advanced Data Conversion with undio's convertTo Source: https://context7.com/unjs/undio/llms.txt Perform explicit data conversions using undio's `convertTo` function. Supports automatic type detection, specifying source types for efficiency, chaining conversions, and converting to various target types including streams and responses. ```javascript import { convertTo } from 'undio'; // Basic usage - automatic type detection const text = "Hello World"; const uint8 = await convertTo("Uint8Array", text); console.log(uint8); // Uint8Array with encoded text // Convert with explicit source type (more efficient) const base64 = "data:text/plain;base64,SGVsbG8gV29ybGQ="; const arrayBuffer = await convertTo("ArrayBuffer", base64, "Base64"); console.log(arrayBuffer.byteLength); // 11 // Chain conversions const originalBlob = new Blob(["Test data"]); const numberArray = await convertTo("NumberArray", originalBlob); console.log(numberArray); // [84, 101, 115, 116, 32, 100, 97, 116, 97] // Convert to all supported types const sourceData = new Uint8Array([72, 101, 108, 108, 111]); const conversions = { text: await convertTo("Text", sourceData), blob: await convertTo("Blob", sourceData), arrayBuffer: await convertTo("ArrayBuffer", sourceData), dataView: await convertTo("DataView", sourceData), numberArray: await convertTo("NumberArray", sourceData), readableStream: await convertTo("ReadableStream", sourceData), response: await convertTo("Response", sourceData), base64: await convertTo("Base64", sourceData) }; console.log(conversions.text); // "Hello" console.log(conversions.blob.size); // 5 console.log(conversions.arrayBuffer.byteLength); // 5 console.log(conversions.numberArray); // [72, 101, 108, 108, 111] ``` -------------------------------- ### Function: responseToReadableStream(response) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Response to a ReadableStream. It takes a Response as input. ```APIDOC ## responseToReadableStream(response) ### Description Converts from Response to ReadableStream. ### Method Function ### Endpoint responseToReadableStream(response) ### Parameters #### Path Parameters - **response** (Response) - Required - The Response to convert. ### Response #### Success Response (200) - **ReadableStream** (ReadableStream) - The converted ReadableStream. ``` -------------------------------- ### Function: readableStreamToText(readableStream) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a ReadableStream to Text. It takes a ReadableStream as input. ```APIDOC ## readableStreamToText(readableStream) ### Description Converts a ReadableStream to Text. ### Method Function ### Endpoint readableStreamToText(readableStream) ### Parameters #### Path Parameters - **readableStream** (ReadableStream) - Required - The ReadableStream to convert. ### Response #### Success Response (200) - **string** (string) - The converted Text. ``` -------------------------------- ### Text String Conversions with undio Source: https://context7.com/unjs/undio/llms.txt This set of functions facilitates conversions from text strings to other data types like Uint8Array, ArrayBuffer, Blob, Base64 data URLs, ReadableStreams, and number arrays. Options are available for specifying MIME types and data URL formats. ```javascript import { textToUint8Array, textToArrayBuffer, textToBlob, textToBase64, textToReadableStream, textToNumberArray } from 'undio'; const text = "Hello, World! 🎉"; // Convert to Uint8Array const uint8 = textToUint8Array(text); console.log(uint8); // Uint8Array containing UTF-8 encoded bytes // Convert to ArrayBuffer const buffer = textToArrayBuffer(text); console.log(buffer.byteLength); // Byte length of UTF-8 encoded string // Convert to Blob with MIME type const blob = textToBlob(text, { type: 'text/plain' }); console.log(blob.type); // "text/plain" // Convert to Base64 data URL const base64 = textToBase64(text, { dataURL: true, type: 'text/plain' }); console.log(base64); // "data:text/plain;base64,SGVsbG8sIFdvcmxkISDwn46J" // Convert to ReadableStream const stream = textToReadableStream(text); const reader = stream.getReader(); const { value } = await reader.read(); console.log(new TextDecoder().decode(value)); // "Hello, World! 🎉" // Convert to number array const numbers = textToNumberArray(text); console.log(numbers); // Array of byte values ``` -------------------------------- ### Function: uint8ArrayToDataView(uint8Array) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Uint8Array to a DataView. It takes a Uint8Array as input. ```APIDOC ## uint8ArrayToDataView(uint8Array) ### Description Converts from Uint8Array to DataView. ### Method Function ### Endpoint uint8ArrayToDataView(uint8Array) ### Parameters #### Path Parameters - **uint8Array** (Uint8Array) - Required - The Uint8Array to convert. ### Response #### Success Response (200) - **DataView** (DataView) - The converted DataView. ``` -------------------------------- ### Convert ArrayBuffer to Various Types with undio Source: https://github.com/unjs/undio/blob/main/README.md Showcases utility functions for converting an ArrayBuffer to other data types like Base64, Blob, DataView, Number Array, ReadableStream, Response, Text, and Uint8Array. It also includes assertion and testing functions for ArrayBuffer. ```js import { arrayBufferToBase64, arrayBufferToBlob, arrayBufferToDataView, arrayBufferToNumberArray, arrayBufferToReadableStream, arrayBufferToResponse, arrayBufferToText, arrayBufferToUint8Array, assertArrayBuffer, isArrayBuffer, toArrayBuffer } from "undio"; // Example conversion: const arrayBuffer = new ArrayBuffer(16); const text = await arrayBufferToText(arrayBuffer); // Example assertion: assertArrayBuffer(arrayBuffer); // Will not throw if input is ArrayBuffer // Example test: const isBuffer = isArrayBuffer(arrayBuffer); ``` -------------------------------- ### Function: uint8ArrayToArrayBuffer(uint8Array) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Uint8Array to an ArrayBuffer. It takes a Uint8Array as input. ```APIDOC ## uint8ArrayToArrayBuffer(uint8Array) ### Description Converts from Uint8Array to ArrayBuffer. ### Method Function ### Endpoint uint8ArrayToArrayBuffer(uint8Array) ### Parameters #### Path Parameters - **uint8Array** (Uint8Array) - Required - The Uint8Array to convert. ### Response #### Success Response (200) - **ArrayBuffer** (ArrayBuffer) - The converted ArrayBuffer. ``` -------------------------------- ### Function: readableStreamToDataView(readableStream) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a ReadableStream to a DataView. It takes a ReadableStream as input. ```APIDOC ## readableStreamToDataView(readableStream) ### Description Converts a ReadableStream to a DataView. ### Method Function ### Endpoint readableStreamToDataView(readableStream) ### Parameters #### Path Parameters - **readableStream** (ReadableStream) - Required - The ReadableStream to convert. ### Response #### Success Response (200) - **DataView** (DataView) - The converted DataView. ``` -------------------------------- ### Function: isUint8Array(input) Source: https://github.com/unjs/undio/blob/main/README.md This function checks if the input is an instance of Uint8Array and returns true or false. It takes an input as a parameter. ```APIDOC ## isUint8Array(input) ### Description Tests if input is an instance of Uint8Array and return true or false. ### Method Function ### Endpoint isUint8Array(input) ### Parameters #### Path Parameters - **input** (any) - Required - The input to check. ### Response #### Success Response (200) - **boolean** (boolean) - Returns true if the input is a Uint8Array, false otherwise. ``` -------------------------------- ### Function: responseToUint8Array(response) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Response to a Uint8Array. It takes a Response as input. ```APIDOC ## responseToUint8Array(response) ### Description Converts from Response to Uint8Array. ### Method Function ### Endpoint responseToUint8Array(response) ### Parameters #### Path Parameters - **response** (Response) - Required - The Response to convert. ### Response #### Success Response (200) - **Uint8Array** (Uint8Array) - The converted Uint8Array. ``` -------------------------------- ### Function: textToArrayBuffer(string) Source: https://github.com/unjs/undio/blob/main/README.md This function converts Text to an ArrayBuffer. It takes a string as input. ```APIDOC ## textToArrayBuffer(string) ### Description Converts from Text to ArrayBuffer. ### Method Function ### Endpoint textToArrayBuffer(string) ### Parameters #### Path Parameters - **string** (string) - Required - The string to convert. ### Response #### Success Response (200) - **ArrayBuffer** (ArrayBuffer) - The converted ArrayBuffer. ``` -------------------------------- ### Convert Fetch Response Objects to Text, Uint8Array, Blob, ArrayBuffer, ReadableStream in JavaScript Source: https://context7.com/unjs/undio/llms.txt This functionality allows conversion of Fetch API Response objects to various formats including text, Uint8Array, Blob, ArrayBuffer, and ReadableStream. It also includes functions to create Response objects from text or Uint8Array data. ```javascript import { responseToText, responseToUint8Array, responseToBlob, responseToArrayBuffer, responseToReadableStream, textToResponse, uint8ArrayToResponse } from 'undio'; // Create Response from text const response = textToResponse("Hello World", { status: 200, headers: { 'Content-Type': 'text/plain' } }); // Convert Response to text const text = await responseToText(response); console.log(text); // "Hello World" // Create Response from binary data const binaryData = new Uint8Array([1, 2, 3, 4, 5]); const binaryResponse = uint8ArrayToResponse(binaryData, { status: 200, headers: { 'Content-Type': 'application/octet-stream' } }); // Convert Response to Uint8Array const uint8 = await responseToUint8Array(binaryResponse); console.log(uint8); // Uint8Array [1, 2, 3, 4, 5] // Create Response and convert to Blob const response2 = textToResponse("Blob content"); const blob = await responseToBlob(response2); console.log(await blob.text()); // "Blob content" // Convert Response to ReadableStream const response3 = textToResponse("Stream content"); const stream = responseToReadableStream(response3); const reader = stream.getReader(); const { value } = await reader.read(); console.log(new TextDecoder().decode(value)); // "Stream content" ``` -------------------------------- ### Function: readableStreamToArrayBuffer(readableStream) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a ReadableStream to an ArrayBuffer. It takes a ReadableStream as input and returns an ArrayBuffer. ```APIDOC ## readableStreamToArrayBuffer(readableStream) ### Description Converts a ReadableStream to an ArrayBuffer. ### Method Function ### Endpoint readableStreamToArrayBuffer(readableStream) ### Parameters #### Path Parameters - **readableStream** (ReadableStream) - Required - The ReadableStream to convert. ### Response #### Success Response (200) - **ArrayBuffer** (ArrayBuffer) - The converted ArrayBuffer. ``` -------------------------------- ### Function: isReadableStream(input) Source: https://github.com/unjs/undio/blob/main/README.md This function checks if the provided input is an instance of ReadableStream. It returns a boolean value indicating whether the input is a ReadableStream. ```APIDOC ## isReadableStream(input) ### Description Tests if input is an instance of ReadableStream. ### Method Function ### Endpoint isReadableStream(input) ### Parameters #### Path Parameters - **input** (any) - Required - The input to check. ### Response #### Success Response (200) - **boolean** (boolean) - Indicates if the input is a ReadableStream. ``` -------------------------------- ### Function: uint8ArrayToReadableStream(uint8Array) Source: https://github.com/unjs/undio/blob/main/README.md This function converts a Uint8Array to a ReadableStream. It takes a Uint8Array as input. ```APIDOC ## uint8ArrayToReadableStream(uint8Array) ### Description Converts from Uint8Array to ReadableStream. ### Method Function ### Endpoint ``` -------------------------------- ### Function: toReadableStream(input) Source: https://github.com/unjs/undio/blob/main/README.md This function converts any value to a ReadableStream. This function takes any value as input. ```APIDOC ## toReadableStream(input) ### Description Converts from any value to ReadableStream. ### Method Function ### Endpoint toReadableStream(input) ### Parameters #### Path Parameters - **input** (any) - Required - The value to convert. ### Response #### Success Response (200) - **ReadableStream** (ReadableStream) - The converted ReadableStream. ```