miaottie
v0.0.1
Published
Miaottie is a library for packing and unpacking lottie-json data.
Readme
Miaottie
CN: README_CN.md
A lightweight library to pack and unpack Lottie JSON. It extracts image assets from the Lottie JSON, bundles them into a binary payload, and supports optional compression, lazy asset loading, and running in both browsers and Node.
Features
- Separates and bundles Lottie
assets(base64 or remote) into a binary payload - Optional compression:
ZstdorDeflate; uses nativeCompressionStream/DecompressionStreamwhen available, falls back topako - Versioned binary container with a fixed header and metadata for validation and extension
- Lazy asset loading that creates Blob URLs on demand with a unified cleanup function
Install
npm i miaottie
pnpm add miaottie
yarn add miaottieQuick Start
Pack a Lottie JSON into binary:
import { MiaottiePacker } from 'miaottie'
const packer = new MiaottiePacker({
// configure if needed
})
const data: Uint8Array = await packer.pack(lottieJson)Unpack binary back to a Lottie object:
import { MiaottieLoader, useMiaottieV1Unpacker } from 'miaottie'
const unpacker = await useMiaottieV1Unpacker({ lazyAssets: true })
const loader = new MiaottieLoader({ unpackerArr: [unpacker] })
const { lottie, destoryLottie } = await loader.unpack(data)
destoryLottie()API Overview
MiaottiePacker- Constructor options:
AllowDownloadPrefix?: string[]default['http', 'https']CompressType?: 0 | 1 | 2equalsNone | Zstd | DeflateCompressLevel?: numberranges: Zstd-13~22, Deflate1~10
- Methods:
pack(lottie: any): Promise<Uint8Array>
- Static:
VERSION = 1
- Constructor options:
useMiaottieV1Unpacker(options?)options.lazyAssets?: booleandefaulttrue- Returns
{ version, handle }forMiaottieLoader
MiaottieLoadernew MiaottieLoader({ unpackerArr: [unpacker] })unpack(data: Uint8Array)returns{ lottie, destoryLottie }
File Format (V1)
The .miaottie file contains:
- 4-byte header:
0x38 0x35 0x27 0x21 - 1-byte version
- Payload encoded by
msgpack:c: compression typea: asset array with items{ i: string, d: Uint8Array }l: msgpack-ed Lottie JSON without assets
Compatibility & Notes
- Environments: modern browsers and Node 18+
- Compression/Decompression: prefers native
CompressionStream/DecompressionStream, falls back topako - Zstd: implemented via
zstd-codec(WASM) for browsers and Node - Remote assets: during pack, assets are downloaded via
fetchif allowed byAllowDownloadPrefix; in Node, ensure globalfetchor use a polyfill - Resource cleanup: call
destoryLottie()to release Blob URLs and memory
License
MIT
