Official TypeScript library providing comprehensive client access to Mux's video infrastructure API including asset management, live streaming, analytics, and webhook handling
File upload utilities and helper functions for handling various input types, multipart form encoding, and file object creation across different JavaScript environments.
Convert various input types to File objects compatible with Mux upload APIs.
/**
* Convert various inputs to File objects for uploads
* @param value - Input data to convert to File
* @param name - Optional filename
* @param options - Optional file properties
* @returns Promise that resolves to File object ready for upload
*/
function toFile(
value: ToFileInput | PromiseLike<ToFileInput>,
name?: string | null | undefined,
options?: FilePropertyBag | undefined
): Promise<FileLike>;
type Uploadable = FileLike | ResponseLike | FsReadStream;
type ToFileInput = Uploadable | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>;
type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | Uint8Array | DataView;
interface FilePropertyBag {
/** MIME type of the file */
type?: string;
/** Last modified timestamp */
lastModified?: number;
}Usage Examples:
import { toFile } from '@mux/mux-node';
import { createReadStream } from 'fs';
// Convert Buffer to File
const buffer = Buffer.from('video data');
const file1 = await toFile(buffer, 'video.mp4', { type: 'video/mp4' });
// Convert ReadStream to File
const stream = createReadStream('./video.mp4');
const file2 = await toFile(stream, 'video.mp4');
// Convert fetch Response to File
const response = await fetch('https://example.com/video.mp4');
const file3 = await toFile(response, 'downloaded-video.mp4');
// Convert existing File-like object
const existingFile = document.getElementById('fileInput').files[0]; // Browser
const file4 = await toFile(existingFile);Create File objects directly from file system paths (Node.js environments).
/**
* Create File object from file system path
* @param path - Absolute or relative path to file
* @returns File object created from file system
*/
function fileFromPath(path: string): File;Usage Examples:
import { fileFromPath } from '@mux/mux-node';
// Create File from file system path
const file = fileFromPath('./videos/sample.mp4');
// Use with upload creation
const upload = await mux.video.uploads.create({
new_asset_settings: {
playback_policies: ['public'],
},
});
// Upload the file (this is conceptual - actual upload happens client-side)
// The fileFromPath result would be used in multipart form constructioninterface FileLike extends BlobLike {
/** Last modified timestamp */
readonly lastModified: number;
/** Filename */
readonly name: string;
}
interface BlobLike {
/** Blob size in bytes */
readonly size: number;
/** MIME type */
readonly type: string;
/** Read as text */
text(): Promise<string>;
/** Slice the blob */
slice(start?: number, end?: number): BlobLike;
}
interface ResponseLike {
/** Response URL */
url: string;
/** Convert to blob */
blob(): Promise<BlobLike>;
}
interface FsReadStream {
/** File path */
path: string;
/** Read stream */
pipe<T extends NodeJS.WritableStream>(destination: T): T;
/** Add event listener */
on(event: string, listener: (...args: any[]) => void): this;
/** Remove event listener */
off(event: string, listener: (...args: any[]) => void): this;
}interface HeadersLike {
/** Get header value */
get(name: string): string | null;
/** Check if header exists */
has(name: string): boolean;
/** Iterate over headers */
[Symbol.iterator](): Iterator<[string, string]>;
}
interface FormDataEntryValue {
/** Form field name */
name: string;
/** Form field value */
value: string | File;
}import { createReadStream } from 'fs';
import { toFile, fileFromPath } from '@mux/mux-node';
// File system integration
const fileFromDisk = fileFromPath('./video.mp4');
// Stream handling
const stream = createReadStream('./video.mp4');
const fileFromStream = await toFile(stream, 'video.mp4');
// Buffer handling
const buffer = Buffer.from(videoData);
const fileFromBuffer = await toFile(buffer, 'video.mp4', {
type: 'video/mp4',
lastModified: Date.now(),
});import { toFile } from '@mux/mux-node';
// File input handling
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
const browserFile = input.files?.[0];
if (browserFile) {
const file = await toFile(browserFile);
// Use file for upload
}
// Fetch response handling
const response = await fetch('/api/video');
const fileFromResponse = await toFile(response, 'downloaded.mp4');
// Blob handling
const blob = new Blob([videoData], { type: 'video/mp4' });
const fileFromBlob = await toFile(blob, 'video.mp4');async function createUploadableFile(
source: string | Buffer | Blob | File | Response,
filename: string
): Promise<File> {
if (typeof source === 'string') {
// Assume it's a file path in Node.js
return fileFromPath(source);
} else {
// Convert other types using toFile
return await toFile(source, filename);
}
}
// Usage across environments
const file1 = await createUploadableFile('./video.mp4', 'video.mp4'); // Node.js
const file2 = await createUploadableFile(browserFile, 'video.mp4'); // Browser
const file3 = await createUploadableFile(buffer, 'video.mp4'); // Universalimport { toFile } from '@mux/mux-node';
async function safeFileConversion(input: unknown, filename: string): Promise<File | null> {
try {
return await toFile(input, filename);
} catch (error) {
console.error('File conversion failed:', error);
return null;
}
}
// Usage with error handling
const file = await safeFileConversion(userInput, 'upload.mp4');
if (file) {
console.log('File ready for upload:', file.name);
} else {
console.error('Unable to create file for upload');
}Install with Tessl CLI
npx tessl i tessl/npm-mux--mux-nodedocs