High-performance processing for the file formats of the web, written in Rust and published for Node.js and Python.
| Family | npm package | PyPI package | Formats detected today |
|---|---|---|---|
| Image | @swc/anyformat-image |
anyformat-image |
PNG, JPEG, GIF, WebP, AVIF, HEIC, JPEG XL, TIFF, BMP, ICO, QOI |
| Audio | @swc/anyformat-audio |
anyformat-audio |
MP3, AAC (ADTS), M4A, FLAC, WAV, AIFF, Opus, Ogg, MIDI, AMR |
| Video | @swc/anyformat-video |
anyformat-video |
MP4, MOV, 3GP, WebM, Matroska, AVI, FLV, MPEG-TS, MPEG-PS |
| Font | @swc/anyformat-font |
anyformat-font |
TTF, OTF, TTC, WOFF, WOFF2, EOT |
@swc/anyformat-pdf |
anyformat-pdf |
PDF (header version) | |
| Office | @swc/anyformat-office |
anyformat-office |
DOCX, XLSX, PPTX, ODT, ODS, ODP, CFB (DOC/XLS/PPT), RTF |
Each family is implemented in a Rust crate (crates/anyformat_<family>) and
starts with format detection from the leading bytes of a file. Decoding,
encoding, and conversion are added per family.
The image family also decodes and encodes PNG, compares images pixel by pixel with the same results as pixelmatch 8.0.0, 7.2.0, 6.0.0, or 5.3.0, and resizes and crops images. It replaces pngjs and pixelmatch. See the image API reference and Migrate from pngjs and pixelmatch. The Rust crates are not published to crates.io yet.
apps/
docs/ Documentation site (Rspress), @swc/anyformat-docs
crates/
anyformat_core/ Shared container helpers (ISO BMFF, RIFF) and macros
anyformat_<family>/ One crate per format family
bindings/
node/<family>/ napi-rs binding, published as @swc/anyformat-<family>
python/<family>/ PyO3 binding, published as anyformat-<family>
import { readFile } from 'node:fs/promises'
import { detect } from '@swc/anyformat-image'
detect(await readFile('photo.avif'))
// => { format: 'avif', mimeType: 'image/avif' }Compare two screenshots and write a diff image, off the main thread:
import { readFile, writeFile } from 'node:fs/promises'
import { compareImages } from '@swc/anyformat-image'
const result = await compareImages(
await readFile('expected.png'),
await readFile('actual.png'),
{ threshold: 0.1, diff: { format: 'png' } },
)
if (result.diffPixels > 0) await writeFile('diff.png', result.diffEncoded)from pathlib import Path
import anyformat_image
anyformat_image.detect(Path("photo.avif").read_bytes())
# => DetectedFormat(format='avif', mime_type='image/avif')
result = anyformat_image.compare_images(
Path("expected.png").read_bytes(),
Path("actual.png").read_bytes(),
threshold=0.1,
diff=anyformat_image.PngEncodeOptions(),
)
if result.diff_pixels > 0 and result.diff_encoded is not None:
Path("diff.png").write_bytes(result.diff_encoded)The documentation site is in apps/docs. It uses
Rspress.
pnpm --filter @swc/anyformat-docs dev # start a development server
pnpm --filter @swc/anyformat-docs build # build the site into apps/docs/distCloudflare Pages builds the site from main and serves it at
https://anyformat.swc.rs.
The repository is a Turborepo monorepo on a pnpm
workspace. Each Node.js and Python binding is a workspace package, so Turborepo
orders, runs, and caches the tasks of every language. The Python packages have
a private package.json only to define their Turborepo tasks; it is not
published to npm.
Requirements:
- A stable Rust toolchain (see
rust-toolchain.toml) - Node.js 18 or later and pnpm
- uv, for the Python bindings
- Git LFS, for the binary sample files
Run git lfs install once before you clone. If you cloned without Git LFS,
run git lfs pull to replace the pointer files with the sample files.
.gitattributes lists the file extensions that Git LFS stores.
pnpm install
pnpm test # Rust tests and the tests of every binding
pnpm lint # rustfmt and clippy
pnpm build # release builds of every binding and the docs site
pnpm build:debug # debug builds of the Node.js bindingsUse turbo filters to work on a subset:
pnpm turbo run test --filter=@swc/anyformat-image # one Node.js package
pnpm turbo run test --filter='./bindings/python/*' # all Python packages| Task | Node.js package | Python package | Root (//) |
|---|---|---|---|
build |
napi build --release |
uv build --wheel into dist/ |
|
build:debug |
napi build |
||
test |
node --test |
uv run pytest (builds the module) |
|
test:rust |
cargo test --workspace |
||
lint:rust |
cargo clippy --workspace |
||
fmt:check |
cargo fmt --check |
Every binding task lists crates/** as an input, so a change to a Rust crate
invalidates the cached results of the bindings. turbo.json uses the loose
environment mode, because Cargo, rustup, MSVC, and uv need many platform
environment variables. Variables that change build output are listed in
globalEnv so that they are part of the cache key.
Each Node.js binding has two layers. napi build generates binding.js and
binding.d.ts, which load the native addon. Commit them when the binding API
changes. The hand-written index.js and index.d.ts are the public entry
point of the package: they select the exports of the generated binding that
are the public API. When you add a binding function, also export it from
index.js and index.d.ts.
Each Python binding has the same two layers. The native extension module is
the private anyformat_<family>._native module, and its hand-written type stub
is python/anyformat_<family>/_native.pyi. The hand-written
python/anyformat_<family>/__init__.py is the public API of the package. When
you add a binding function, update the type stub and export the function from
__init__.py.
.cargo/config.toml lets plain Cargo commands build the Python binding crates
without a Python interpreter. maturin replaces that configuration when it
builds wheels.
The Publish workflow builds and publishes the packages. npm and PyPI trust this workflow (trusted publishing with OIDC), so the repository stores no registry token.
- npm: each family has a main package and one package for each platform, for
example
@swc/anyformat-image-linux-x64-gnu.napi create-npm-dirsmakes the platform packages in the workflow, and the main package lists them inoptionalDependencies. - PyPI: each family has abi3 wheels for each platform and an sdist.
To release:
- Set the new version in
[workspace.package]of the rootCargo.toml(the Python packages read it) and in eachbindings/node/<family>/package.json. - Merge the change to
main. - Run the Publish workflow on
mainfrom the Actions tab. The workflow skips versions that are already published.
A pull request that changes the workflow or the package manifests runs the same builds and packs the packages, but does not publish them.
Apache-2.0. See LICENSE.