@arctics/utils
Version:
A JavaScript/TypeScript package providing utilities around certain functionalities
132 lines (131 loc) • 5.07 kB
TypeScript
/**
* A utility class for converting files, URLs, buffers, and streams to base64 and hexadecimal strings,
* and vice-versa.
*/
export declare class FileConverter {
/**
* Converts a file, URL, Buffer, or ReadableStream to a base64 string.
* @param source The source to convert. Can be a File, Blob, URL (string), Buffer, or ReadableStream.
* @returns A Promise that resolves to the base64 string, or null if an error occurs.
*/
fileToBase64(source: File | Blob | string | Buffer | ReadableStream): Promise<string | null>;
/**
* Converts a file, URL, Buffer, or ReadableStream to a hexadecimal string.
* @param source The source to convert. Can be a File, Blob, URL (string), Buffer, or ReadableStream.
* @returns A Promise that resolves to the hexadecimal string, or null if an error occurs.
*/
fileToHex(source: File | Blob | string | Buffer | ReadableStream): Promise<string | null>;
/**
* Converts a base64 string to a File object or Buffer.
* @param base64String The base64 string to convert.
* @param fileName The name of the file (required in browser environment).
* @returns A Promise that resolves to the File object (in browser) or Buffer (in Node.js), or rejects if an error occurs.
* @throws If an error occurs during conversion.
*/
base64ToFile(base64String: string, fileName?: string): Promise<File | Buffer>;
/**
* Converts a hexadecimal string to a File object or Buffer.
* @param hexString The hexadecimal string to convert.
* @param fileName The name of the file (required in browser environment).
* @returns A Promise that resolves to the File object (in browser) or Buffer (in Node.js), or rejects if an error occurs.
* @throws If an error occurs during conversion.
*/
hexToFile(hexString: string, fileName?: string): Promise<File | Buffer>;
/**
* Checks if the current environment is a browser.
* @returns True if the current environment is a browser, false otherwise.
* @private
*/
private isBrowser;
/**
* Checks if a string is a valid URL.
* @param str The string to check.
* @returns True if the string is a URL, false otherwise.
* @private
*/
private isURL;
/**
* Converts a File or Blob to a base64 string. (Helper function)
* @param file The File or Blob.
* @returns A Promise that resolves to the base64 string, or null if an error occurs.
* @private
*/
private convertFromFile;
/**
* Converts a File or Blob to a hexadecimal string. (Helper function)
* @param file The File or Blob.
* @returns A Promise that resolves to the hexadecimal string, or null if an error occurs.
* @private
*/
private convertFromFileToHex;
/**
* Converts a URL to a base64 string. (Helper function)
* @param url The URL.
* @returns A Promise that resolves to the base64 string, or null if an error occurs.
* @private
*/
private convertFromURL;
/**
* Converts a URL to a hexadecimal string. (Helper function)
* @param url The URL.
* @returns A Promise that resolves to the hexadecimal string, or null if an error occurs.
* @private
*/
private convertFromURLToHex;
/**
* Converts a Buffer to a base64 string. (Helper function)
* @param buffer The Buffer.
* @returns The base64 string.
* @private
*/
private convertFromBuffer;
/**
* Converts a Buffer to a hexadecimal string. (Helper function)
* @param buffer The Buffer.
* @returns The hexadecimal string.
* @private
*/
private convertFromBufferToHex;
/**
* Converts a ReadableStream to a base64 string. (Helper function)
* @param stream The ReadableStream.
* @returns A Promise that resolves to the base64 string, or null if an error occurs.
* @private
*/
private convertFromStream;
/**
* Converts a ReadableStream to a hexadecimal string. (Helper function)
* @param stream The ReadableStream.
* @returns A Promise that resolves to the hexadecimal string, or null if an error occurs.
* @private
*/
private convertFromStreamToHex;
/**
* Converts an ArrayBuffer to a base64 string.
* @param arrayBuffer The ArrayBuffer to convert.
* @returns The base64 string.
* @private
*/
private arrayBufferToBase64;
/**
* Converts a base64 string to an ArrayBuffer.
* @param base64 The base64 string to convert.
* @returns The ArrayBuffer.
* @private
*/
private base64ToArrayBuffer;
/**
* Converts an ArrayBuffer to a hexadecimal string.
* @param arrayBuffer The ArrayBuffer to convert.
* @returns The hexadecimal string.
* @private
*/
private arrayBufferToHex;
/**
* Converts a hexadecimal string to an ArrayBuffer.
* @param hex The hexadecimal string to convert.
* @returns The ArrayBuffer.
* @private
*/
private hexToArrayBuffer;
}