Skip to content

Built-in transforms

timeout rejects the caller with CallerTimeoutError after a deadline:

const value = await stub.with(timeout, 5_000).read();

The error includes method and timeoutMs. Remote work is not cancelled.

retry repeats failed RPC calls sequentially. Its count is retries after the first call:

const value = await stub.with(retry, {
retries: 3,
delay: ({ error, attempt }) => {
console.warn('retrying', { error, attempt });
return Math.min(1_000 * 2 ** (attempt - 1), 30_000);
},
}).read();

Without delay, it uses capped exponential backoff with full jitter. A numeric shorthand is supported:

stub.with(retry, 3);

Register betterResultCodec on the callee and apply it on the caller to serialize and rehydrate better-result values in a versioned envelope:

applyTransforms(MyObject, {
all: [registerTransform(betterResultCodec)],
});
const result = await stub.with(betterResultCodec).read();

Version 0.1.0 payloads are rejected by default because their unmarked shape is ambiguous. During a migration only:

stub.with(betterResultCodec, { acceptLegacy: true });

errorBoundary converts values and caller-visible throws to Better Results:

const result = await stub.with(errorBoundary).read();

abortAsSuccess converts Cloudflare errors marked durableObjectReset to Result.ok(undefined) and preserves other throws. Use it only when resetting the object is an expected success condition.

largeObjectStream transfers bounded JSON object results over a marked ReadableStream. Configure the callee and require a Standard Schema validator at the caller:

applyTransforms(MyObject, {
methods: {
snapshot: [
registerTransform(largeObjectStream, {
thresholdBytes: 1_000_000,
maxEncodeBytes: 8 * 1024 * 1024,
}),
],
},
});
const snapshot = await stub.with(largeObjectStream, {
schema: snapshotSchema,
maxDecodeBytes: 8 * 1024 * 1024,
}).snapshot();

Defaults are a 1 MiB streaming threshold and 8 MiB encode/decode limits. Existing ReadableStream values pass through unchanged.

Limit and validation failures throw LargeObjectEncodeLimitError, LargeObjectDecodeLimitError, or LargeObjectValidationError.