Skip to content
Query.Farm
Talk with Us

State storage

On this page

State that outlives one call, or crosses worker processes.

source
export class BoundStorage

Methods

source
put(state: Uint8Array): Promise<void>
source
collect(): Promise<Uint8Array[]>
source
workerScan(): Promise<Array<[number, Uint8Array]>>
source
queuePush(items: Uint8Array[]): Promise<number>
source
queuePop(): Promise<Uint8Array | null>
source
queueClear(): Promise<number>
source
stateGet(ns: Uint8Array, key: Uint8Array): Promise<Uint8Array | null>
source
statePut(ns: Uint8Array, key: Uint8Array, value: Uint8Array): Promise<void>
source
stateAppend(ns: Uint8Array, key: Uint8Array, item: Uint8Array): Promise<number>
source
stateLogScan(
ns: Uint8Array,
key: Uint8Array,
afterId: number = -1,
limit: number | null = null,
): Promise<Array<[number, Uint8Array]>>
source
executionClear(): Promise<number>
source
export type FetchLike = (
input: string,
init?: { method?: string; headers?: Record<string, string>; body?: string },
) => Promise<Response>;

Description

Minimal fetch signature satisfied by both the global fetch and a Cloudflare service-binding Fetcher (env.<BINDING>).

source
const storage: FunctionStorage

Description

Default FunctionStorage instance. Backend is selected via env on first access.

source
export interface FunctionStorage

Fields

workerPut(executionId: Uint8Array, workerId: number, state: Uint8Array): Promise<void>
workerCollect(executionId: Uint8Array): Promise<Uint8Array[]>
workerScan(executionId: Uint8Array): Promise<Array<[number, Uint8Array]>>
queuePush(executionId: Uint8Array, items: Uint8Array[]): Promise<number>
queuePop(executionId: Uint8Array): Promise<Uint8Array | null>
queueClear(executionId: Uint8Array): Promise<number>
stateGet(scopeId: Uint8Array, ns: Uint8Array, key: Uint8Array): Promise<Uint8Array | null>
statePut(scopeId: Uint8Array, ns: Uint8Array, key: Uint8Array, value: Uint8Array): Promise<void>
stateAppend(scopeId: Uint8Array, ns: Uint8Array, key: Uint8Array, item: Uint8Array): Promise<number>
stateLogScan( scopeId: Uint8Array, ns: Uint8Array, key: Uint8Array, afterId?: number, limit?: number | null, ): Promise<Array<[number, Uint8Array]>>
executionClear(scopeId: Uint8Array): Promise<number>

Wipe all state + log rows for a scope across every namespace.

close(): Promise<void> | voidoptional

Optional cleanup hook. Implementations holding a connection should release it here.

source
export class FunctionStorageCfDo implements FunctionStorage

Methods

source
forShard(shardKey: string): FunctionStorageCfDo

Return a view of this backend pinned to one shard key, so callers can route per logical ATTACH. Shares the URL/token; only the shard key differs.

source
async workerPut(executionId: Uint8Array, workerId: number, state: Uint8Array): Promise<void>
source
async workerCollect(executionId: Uint8Array): Promise<Uint8Array[]>
source
async workerScan(executionId: Uint8Array): Promise<Array<[number, Uint8Array]>>
source
async queuePush(executionId: Uint8Array, items: Uint8Array[]): Promise<number>
source
async queuePop(executionId: Uint8Array): Promise<Uint8Array | null>
source
async queueClear(executionId: Uint8Array): Promise<number>
source
async stateGet(scopeId: Uint8Array, ns: Uint8Array, key: Uint8Array): Promise<Uint8Array | null>
source
async statePut(scopeId: Uint8Array, ns: Uint8Array, key: Uint8Array, value: Uint8Array): Promise<void>
source
async stateAppend(scopeId: Uint8Array, ns: Uint8Array, key: Uint8Array, item: Uint8Array): Promise<number>
source
async stateLogScan(
scopeId: Uint8Array,
ns: Uint8Array,
key: Uint8Array,
afterId: number = -1,
limit: number | null = null,
): Promise<Array<[number, Uint8Array]>>
source
async executionClear(scopeId: Uint8Array): Promise<number>

Wipe all state + log rows for a scope across every namespace.

source
export class FunctionStorageSqlite implements FunctionStorage

Properties

source
readonly dbPath: string;

Methods

source
close(): void

Optional cleanup hook. Implementations holding a connection should release it here.

source
async stateGet(scopeId: Uint8Array, ns: Uint8Array, key: Uint8Array): Promise<Uint8Array | null>
source
async statePut(scopeId: Uint8Array, ns: Uint8Array, key: Uint8Array, value: Uint8Array): Promise<void>
source
async stateAppend(scopeId: Uint8Array, ns: Uint8Array, key: Uint8Array, item: Uint8Array): Promise<number>
source
async stateLogScan(
scopeId: Uint8Array,
ns: Uint8Array,
key: Uint8Array,
afterId: number = -1,
limit: number | null = null,
): Promise<Array<[number, Uint8Array]>>
source
async executionClear(scopeId: Uint8Array): Promise<number>

Wipe all state + log rows for a scope across every namespace.

source
async workerPut(
executionId: Uint8Array,
workerId: number,
state: Uint8Array
): Promise<void>
source
async workerCollect(executionId: Uint8Array): Promise<Uint8Array[]>
source
async workerScan(executionId: Uint8Array): Promise<Array<[number, Uint8Array]>>
source
async queuePush(executionId: Uint8Array, items: Uint8Array[]): Promise<number>
source
async queuePop(executionId: Uint8Array): Promise<Uint8Array | null>
source
async queueClear(executionId: Uint8Array): Promise<number>
source
export function resolveStorageFromEnv(): FunctionStorage

Description

Resolve the FunctionStorage backend from environment variables.

Mirrors vgi-python’s _resolve_storage in vgi/function.py.

VGI_WORKER_SHARED_STORAGE selects the backend (default: sqlite):

  • memory — FunctionStorageSqlite at :memory:. Process-local with no cross-process coordination — single-process deployments only. Ignores VGI_WORKER_SQLITE_PATH.
  • sqlite — FunctionStorageSqlite. Honors VGI_WORKER_SQLITE_PATH (including :memory:).
  • cloudflare-do — FunctionStorageCfDo. Requires VGI_CF_DO_URL; optional VGI_CF_DO_TOKEN for bearer auth.
source
export function setStorage(impl: FunctionStorage): void

Description

Override the default backend used by the storage singleton. Call once at worker startup to inject a pre-built backend that env-driven selection can’t construct — notably a FunctionStorageCfDo wired to a Cloudflare service-binding Fetcher (the binding lives on env, not process.env, and a same-zone public-URL fetch is rejected with CF error 1042). Takes precedence over VGI_WORKER_SHARED_STORAGE.

source
export class UnknownInvocationError extends Error