API Reference
sniffMimeType(bytes)
Section titled “sniffMimeType(bytes)”Detects an image MIME type from magic bytes. Returns null if the bytes don’t match a known signature.
| param | type | required |
|---|---|---|
bytes |
Uint8Array | ArrayBuffer |
yes |
Returns: "image/png" \| "image/jpeg" \| "image/gif" \| "image/webp" \| "image/avif" \| null
import { sniffMimeType } from "edge64";
const mime = sniffMimeType(bytes);// "image/png"imageUrlToBase64(url, init?)
Section titled “imageUrlToBase64(url, init?)”Fetches an image from a URL and returns its base64 encoding, data URI, and detected MIME type. Prefers a valid content-type response header; falls back to magic-byte sniffing if the header is missing or not an image/* type.
| param | type | default | required |
|---|---|---|---|
url |
string | — | yes |
init |
RequestInit |
undefined |
no |
Returns: Promise<{ mime: string, base64: string, dataUri: string }>
Throws if the fetch response is not ok, or if no MIME type could be determined.
import { imageUrlToBase64 } from "edge64";
const { mime, base64, dataUri } = await imageUrlToBase64("https://example.com/cat.png");base64ToBytes(input)
Section titled “base64ToBytes(input)”Decodes a base64 string or a full data: URI back into bytes.
| param | type | required |
|---|---|---|
input |
string | yes |
Returns: Uint8Array
import { base64ToBytes } from "edge64";
const bytes = base64ToBytes("data:image/png;base64,iVBORw0KGgo...");// orconst bytes2 = base64ToBytes("iVBORw0KGgo...");bytesToDataUri(bytes, mimeHint?)
Section titled “bytesToDataUri(bytes, mimeHint?)”Builds a data: URI from raw bytes. If mimeHint is omitted, the MIME type is detected with sniffMimeType.
| param | type | default | required |
|---|---|---|---|
bytes |
Uint8Array | ArrayBuffer |
— | yes |
mimeHint |
string | undefined |
no |
Returns: string
Throws if no mimeHint is given and the MIME type can’t be sniffed.
import { bytesToDataUri } from "edge64";
const uri = bytesToDataUri(bytes);// "data:image/png;base64,..."