@huggingface/blob
v0.0.2
Published
Utilities to convert URLs and files to Blobs, internally used by Hugging Face libs
Downloads
71
Readme
🤗 Hugging Face Blobs
Utilities to convert a string or URL to a Blob object, whether it represents a local file or a remote URL.
fetch
already returns a Blob
object for remote URLs, but it loads the entire file in memory. This utility makes ad-hoc http range requests when calling .slice()
on the blob.
Install
pnpm add @huggingface/blob
npm add @huggingface/blob
yarn add @huggingface/blob
Deno
// esm.sh
import { FileBlob } from "https://esm.sh/@huggingface/blob/FileBlob";
import { WebBlob } from "https://esm.sh/@huggingface/blob/WebBlob";
import { createBlob } from "https://esm.sh/@huggingface/blob";
// or npm:
import { FileBlob } from "npm:@huggingface/blob/FileBlob";
import { WebBlob } from "npm:@huggingface/blob/WebBlob";
import { createBlob } from "npm:@huggingface/blob";
Usage
import { FileBlob } from "@huggingface/blob/FileBlob";
import { WebBlob } from "@huggingface/blob/WebBlob";
import { createBlob } from "@huggingface/blob";
const fileBlob = await FileBlob.create("path/to/file");
const webBlob = await WebBlob.create("https://url/to/file");
const blob = await createBlob("..."); // Automatically detects if it's a file or web URL
API
createBlob
Creates a Blob object from a string or URL. Automatically detects if it's a file or web URL.
await createBlob("...", {
/**
* Custom fetch function to use, in case it resolves to a Web Blob.
*
* Useful for adding headers, etc.
*/
fetch: ...,
});
### FileBlob
```ts
await FileBlob.create("path/to/file");
await FileBlob.create(new URL("file:///path/to/file"));
WebBlob
Creates a Blob object from a URL. If the file is less than 1MB (as indicated by the Content-Length header), by default it will be cached in memory in entirety upon blob creation.
This class is useful for large files that do not need to be loaded all at once in memory, as it makes range requests for the data.
await WebBlob.create("https://url/to/file");
await WebBlob.create(new URL("https://url/to/file"));
await WebBlob.create("https://url/to/file", {
/**
* Custom fetch function to use. Useful for adding headers, etc.
*/
fetch: ...,
/**
* If the file is less than the specified size, it will be cached in memory in entirety upon blob creation,
* instead of doing range requests for the data.
*
* @default 1_000_000
*/
cacheBelow: ...
})