A blob holds bytes, such as an image or a file. The Blob object stores the hash, size and type of the bytes and where
they are; small blobs hold the bytes inline. An object points at a blob with a reference.
Returns a Type.Type entity — a live, in-memory TypeSchema instance,
NOT a Schema.Schema. Use Type.InstanceType<typeof Foo> for the instance
type and Type.getSchema(Foo) to obtain the underlying Effect Schema.
The entity's id defaults to EntityId.deterministic(typename, version) so
constructing a type never reaches crypto.getRandomValues() — required for
Cloudflare workerd, which forbids RNG calls in global (module-evaluation)
scope. Pass { id } to override (e.g. with EntityId.random() from a
request handler).
Giving the type a nominal class name keeps its inferred type portable for
declaration emit: structural expansion (which can pull in unnameable union
members from referenced schemas) is replaced by a reference to the class.
The second call takes either a schema, or struct fields (wrapped in Schema.Struct) with an optional function
that pipes annotations onto the struct.
NOTE: This export shadows the DOM Blob global within modules that import it as a namespace.
Code that needs the DOM class should use globalThis.Blob explicitly.
db.createBlob hashes the bytes and uploads them to the storage backend, and returns a Blob object that is not yet in
the space. Add it, then reference it. Bytes over the backend’s size limit reject with BlobTooLargeError.
1
import {
import Obj
Obj,
import Ref
Ref } from'@dxos/echo';
2
3
// Hashes the bytes and uploads them; the blob is not in the space yet.
Hashes and uploads bytes via the chosen storage backend, returning an un-added Blob object.
Rejects with Error.BlobTooLargeError (over inline storage's fixed cap, or the backend's own
maxSize), Error.BlobWriteError (backend upload failure), or Error.BlobNotAvailableError
(reason: 'backend-not-registered' — the requested storage name has no registered backend).
@performance ― Async; O(size) to hash, plus the backend upload (a no-op for inline storage).
db.readBlob loads the bytes, from the object itself or from the backend, and rejects with BlobNotAvailableError when
it cannot. db.getBlobUrl gives a URL to show the blob in a page.
@returns ― Promise that will resolves with the target object.
Will load the object from disk if it is not present in the working set.
Short-circuits immediately when the target is already loaded.
@throws ― If the object is not available locally.
@idiom ― org.dxos.echo.refLoad
applies: Resolving a ref inside an async function or Effect handler — guarantees the object is available before proceeding
instead-of: ref.target — not guaranteed to be defined in async contexts; use await ref.load() (or yield* Database.load(ref) in Effect) to ensure the target is present
uses: load related: org.dxos.echo-react.useObjectReactive
@performance ― Async; resolves immediately for an inlined or loaded target, otherwise loads from disk or the network.
Loads a blob's bytes. Rejects with Error.BlobNotAvailableError if the backend for the blob's
storage scheme is not registered, offline, or cannot find the bytes.
@performance ― Async; O(size), read inline or fetched from the backend.