session.blob stores arbitrary bytes (PDFs, EPUBs, audio, video, model files,
caches) as files on the phone. It’s scoped to the user and your miniapp, so your
keys never collide with another miniapp’s, and the files survive restarts. Where
session.storage holds small strings, blob
holds binary data.
src/background/index.ts
uri is a file:// path in your miniapp’s private
storage, which you can hand to session.speaker.play, session.blob.share, or
another host capability. Its url is where your miniapp’s own screen loads it,
as <img src={meta.url}>: no bytes cross the bridge.
Reading and writing
set takes a Uint8Array, an ArrayBuffer, or a base64 string, and returns the
stored blob’s metadata. get returns that metadata (including the
uri) or null. bytes reads the whole file into memory.
bytes throws once a blob crosses BLOB_READ_ALL_MAX_BYTES (32 MB). Read
anything larger as a stream.
Downloading and importing without crossing the bridge
setFromUrl downloads a URL straight to disk host-side, and importFile opens
the OS file picker and stores the chosen file. In both cases the bytes never pass
through your JS context, which keeps large files off the bridge. setFromUrl
downloads only http: and https: URLs.
Usage and quota
usage reports how much you’ve stored and the quota ceiling, all in bytes.
usage() again before a large write rather than
caching quotaBytes. A write past it fails with BLOB_QUOTA_EXCEEDED.
Cache
Store a copy of something you can get again, such as a file from your server, withcache: true. Like a browser’s HTTP cache, a cached blob doesn’t count
toward the quota: the phone keeps your miniapp’s cached blobs within a budget
(a quarter of the room they could use, at most 512 MB) and deletes the least
recently read first. Reading a blob (get, has, bytes, a read stream,
share, upload, or copying from it) counts as reading it; loading its url
in your screen doesn’t, so ask for the URL with get each time you show it.
get is null, so download it again.
An image whose file was deleted while your screen showed it fails to load: ask
for it again in its onerror. Keep permanent only what isn’t anywhere else yet,
and once your server has it, make it cached without copying:
await session.blob.setCached(key, true). setCached(key, false) makes a
cached blob permanent again, under the quota. The camera’s photos are cached
blobs.
Large files: streaming
For files past the in-memory caps, stream them.createWriteStream opens a
BlobWriter: call write or writeBase64 as many times as you need (each call
auto-splits into bridge-safe chunks of BLOB_WRITE_CHUNK_BYTES, 1 MB raw), then
close to publish or abort to discard the partial blob.
createReadStream opens a BlobReader. Call read for the next chunk until
done is true, then close.
BlobWriter
BlobReader
Method reference
BlobMeta
get, stat, list, copy, and close resolve to a BlobMeta.
Size limits
Errors
For small strings (counters, flags, serialized settings), use
session.storage instead. To share or export
a file you didn’t store as a blob, see session.system.
