Examples

Example 1

import { env, waitUntil } from 'cloudflare:workers';
import { createApplication } from '@setu-ts/kernel';
import { RuntimePlugin } from '@setu-ts/runtime';
import { CloudflarePlugin } from '@setu-ts/cloudflare-plugin';

const app = createApplication({
  plugins: [
    RuntimePlugin({ env }),
    CloudflarePlugin({ env, waitUntil, cache: { binding: 'CACHE_KV', prefix: 'cache:' } }),
  ],
});

await app.start();
export default { fetch: app.fetch };

Classes

c
CloudflareBindingMissingError(message: string)

A binding the configuration names is absent from the Worker's env, or is present with the wrong shape.

c
CloudflareObjectNotFoundError(path: string)

An R2 object read found nothing.

c
CloudflareRemoteHandlerError(message: string)

A responder threw, and its failure was relayed to the caller.

c
CloudflareRequestTimeoutError(
topic: string,
timeoutMs: number
)

A brokered request received no reply within its budget.

c
CloudflareUnsupportedError(message: string)

The requested operation has no counterpart on the Cloudflare binding.

c
DurableObjectBackplane(
namespace: IDurableObjectNamespace,
options: DurableObjectBackplaneOptions
)

Carries RealtimeFrames between replicas over one WebSocket to a Durable Object.

c
R2Storage(
bucket: IR2Bucket,
options?: R2StorageOptions
)

Object storage backed by Cloudflare R2.

c
WorkersBroker(
producer: IQueueProducer,
runtime: BrokerRuntime,
options?: WorkersBrokerOptions
)

A message broker backed by Cloudflare Queues.

c
WorkersCron(options?: WorkersCronOptions)

A registry of Cron Trigger handlers, keyed by cron expression.

Functions

f
assessCacheability(input: CacheabilityInput): readonly CacheRefusal[]

Lists every reason the edge cache would refuse this response.

f
asUpgradeResponse(
response: unknown,
binding: string
): DurableObjectUpgradeResponse

Narrows a Durable Object stub's response to one carrying a socket.

f
f
createDefaultDurableObjectWebSocketHost(): DurableObjectWebSocketHost

Builds the default host from the real Workers global.

f
createMessagingHandler(
app: IApplication,
options?: MessagingHandlerOptions
): MessagingHandler

Builds the handler an application exports as queue to consume messages.

f
createScheduledHandler(cron: WorkersCron): ScheduledHandler

Builds the handler an application exports as scheduled.

f
isD1Database(value: unknown): value is ID1Database

Reports whether a binding is D1-shaped.

f
isDurableObjectNamespace(value: unknown): value is IDurableObjectNamespace

Reports whether a binding is Durable-Object-namespace-shaped.

f
isKvNamespace(value: unknown): value is IKvNamespace

Reports whether a binding is KV-shaped.

f
isQueueProducer(value: unknown): value is IQueueProducer

Reports whether a binding is Queues-producer-shaped.

f
isR2Bucket(value: unknown): value is IR2Bucket

Reports whether a binding is R2-shaped.

Interfaces

I
BrokerRuntime

The runtime capabilities this broker needs. IRuntimeServices satisfies it.

I
CacheabilityInput

What assessCacheability needs to decide.

I
CacheApiMiddlewareOptions

Options for cacheApiMiddleware.

  • bypass: (ctx: IRequestContext) => boolean

    Returning true skips the cache entirely for this request.

  • cache: ICacheApi

    The cache handle. Omitted resolves caches.default from the global scope; when that is also absent — every runtime other than Cloudflare Workers — the middleware passes through instead of throwing.

  • cacheableStatuses: readonly number[]

    Statuses worth caching. Defaults to [200]. Does not override the platform's unconditional refusal of 206.

  • key: (ctx: IRequestContext) => string

    Builds the cache key from the request. Omitted uses the full request URL, which is what the platform's own cache keys on.

  • ttlSeconds: number

    Adds Cache-Control: public, max-age=<n> to the stored copy when the response carries no Cache-Control of its own. The edge honors the stored response's own directive, so without one an entry has no freshness lifetime and is of little use. The client's response is left untouched.

I
CacheClock

The clock shape this store needs. IRuntimeServices satisfies it.

I
CloudflarePluginOptions

Options for CloudflarePlugin.

I
D1AdapterOptions

Options for D1Adapter.

I
D1EntityMapping

How one entity name maps onto a physical D1 table.

  • primaryKey: string | readonly string[]

    The primary-key column(s). A scalar name keeps today's single-column behaviour; an array enables a composite key whose columns are matched in declaration order. Defaults to ['id'].

  • table: string

    The table name. Defaults to the entity name itself, so getRepository('users') needs no mapping at all.

I
D1Result

A D1 statement result.

I
DistributedLockObjectCoreOptions
I
DurableObjectArm

Wires a Durable Object namespace up as the application's IRealtimeBackplane under CAPABILITIES.REALTIME_BACKPLANE, so WebSocket rooms and SSE channels reach clients on other replicas.

  • binding: string

    The Durable Object namespace binding name from wrangler.toml.

  • name: string

    Instance name. 'default' (the default) claims the bare realtime-backplane token; anything else derives realtime-backplane.<name>.

  • topic: string

    The object name every replica shares, passed to idFromName.

I
DurableObjectBackplaneOptions
I
DurableObjectLockOptions

Options for DurableObjectLock.

I
DurableObjectMessageEvent

One message arriving on a Durable Object WebSocket.

I
DurableObjectUpgradeResponse

A Durable Object stub's response to a WebSocket upgrade.

I
DurableObjectWebSocketHost

Supplies the socket pair a Durable Object upgrade needs.

I
DurableObjectWebSocketPair

A created WebSocketPair.

I
ICloudflareBindings

Typed access to a Cloudflare Worker's platform bindings.

I
ID1Database

A D1 database binding.

I
ID1PreparedStatement

A prepared D1 statement.

I
IDurableObjectClientSocket

The client half of a WebSocketPair, or the socket a Worker gets back from a Durable Object upgrade.

I
IDurableObjectNamespace

A Durable Object namespace binding.

I
IDurableObjectState

The DurableObjectState (ctx) members this package calls.

I
IDurableObjectStorage

The subset of DurableObjectStorage the lock object uses.

I
IDurableObjectWebSocket

A WebSocket held by a Durable Object, as the hibernation API hands it back.

I
IKvNamespace

A Workers KV namespace binding.

I
IQueueMessage

One message delivered to a Queues consumer.

I
IQueueMessageBatch

A batch of messages delivered to a Queues consumer.

I
IQueueProducer

A Cloudflare Queues producer binding.

I
IR2Bucket
I
IR2Object

Metadata common to every R2 object.

I
IR2ObjectBody

An R2 object together with its body.

I
IScheduledController

The controller handed to a Cron Trigger's scheduled handler.

I
IServiceBinding

A service binding to another Worker — a fetch-shaped RPC channel.

I
JobIdSource

The id source this queue needs. IRuntimeServices satisfies it.

I
KvCacheOptions

Wires a KV namespace up as the application's ICacheStore under CAPABILITIES.CACHE.

  • binding: string

    The KV namespace binding name from wrangler.toml.

  • defaultTtlSeconds: number

    TTL in seconds applied when set omits one. Omitted means no expiry.

  • name: string

    Instance name. 'default' (the default) claims the bare cache token; anything else derives cache.<name>, matching CachePlugin's convention so several caches can coexist.

  • prefix: string

    Prefix applied to every cache key. Required to call clear(), and recommended whenever the namespace is shared.

I
KvCacheStoreOptions

Options for KvCacheStore.

  • defaultTtlSeconds: number

    TTL in seconds applied when set omits one. Omitted means no expiry.

  • prefix: string

    Prefix applied to every key. Required for KvCacheStore.clear, which otherwise has no way to tell this store's keys from anything else sharing the namespace.

I
KvListOptions

Options for IKvNamespace.list.

I
KvListResult

One page of IKvNamespace.list results.

I
KvPutOptions

Options for IKvNamespace.put.

  • expirationTtl: number

    Seconds until KV removes the entry. The platform minimum is 60; a smaller value is rejected, which is why physicalTtlSeconds floors it and a logical expiry is carried inside the value.

I
KvSessionStoreOptions

Options for KvSessionStore.

  • prefix: string

    Prefix applied to every session key. Defaults to 'session:', so sharing one namespace with a cache store is safe by default.

I
MessagingHandlerOptions
  • name: string

    Which broker instance to dispatch into, matching CloudflarePluginOptions.messaging.name. Omitted resolves the bare CAPABILITIES.MESSAGING token.

I
QueueHandlerOptions

Options for createQueueHandler.

  • name: string

    Which queue instance to dispatch into, matching CloudflarePluginOptions.queue.name. Omitted resolves the bare CAPABILITIES.QUEUE token.

I
QueueSendOptions

Options for IQueueProducer.send.

I
R2PutOptions

The subset of R2's put options this package writes.

I
R2StorageArm

Wires an R2 bucket up as the application's IStorage under CAPABILITIES.STORAGE.

  • binding: string

    The R2 bucket binding name from wrangler.toml.

  • name: string

    Instance name. 'default' (the default) claims the bare storage token; anything else derives storage.<name>.

  • prefix: string

    Prefix applied to every object key.

I
R2StorageOptions

Options for R2Storage.

  • prefix: string

    Prefix applied to every object key, so one bucket can host several uses.

I
ReplyInboxBinding

The Durable Object namespace serving reply inboxes, plus its binding name.

I
ReplyInboxObjectCoreOptions

Options for ReplyInboxObjectCore.

I
WorkersBrokerOptions

Options for WorkersBroker.

I
WorkersCronOptions

Options for WorkersCron.

  • logger: ILogger

    Reports the two paths that would otherwise be silent: a trigger firing with no handler registered for its expression, and a handler that rejects. Omitted leaves both silent.

I
WorkersMessagingArm

Wires a Cloudflare Queues producer binding up as the application's IMessageBroker under CAPABILITIES.MESSAGING.

I
WorkersMessagingRpcArm

Enables brokered request-reply on a WorkersMessagingArm.

I
WorkersQueueArm

Wires a Cloudflare Queues producer binding up as the application's IQueue under CAPABILITIES.QUEUE.

  • binding: string

    The Queues producer binding name from wrangler.toml.

  • maxDelaySeconds: number

    Largest accepted AddJobOptions.delayMs, in seconds. Defaults to 86400, the platform maximum; a larger delay throws rather than being silently truncated.

  • name: string

    Instance name. 'default' (the default) claims the bare queue token; anything else derives queue.<name>, which QueueHandlerOptions.name must then match.

I
WorkersQueueOptions

Options for WorkersQueue.

  • logger: LoggerSource

    Resolves the logger at the moment a dispatch path needs it, reporting on the four cases that would otherwise be silent: a message whose name has no processor, an unreadable body, a processor that threw, and a job that exhausted its attempts.

  • maxDelaySeconds: number

    Largest delayMs this queue will accept, in seconds. Defaults to 86400, the platform maximum. A larger delay throws rather than being silently truncated by the platform.

Type Aliases

T
CloudflareWorkerEnv = Readonly<Record<string, unknown>>

The Worker env record, as import { env } from 'cloudflare:workers' provides it: a mix of string variables and object bindings.

T
LoggerSource = () => ILogger | undefined

Resolves the logger at the moment a background task fails.

T
MessagingHandler = (batch: IQueueMessageBatch) => Promise<void>

The queue export's shape, as Cloudflare invokes it.

T
QueueHandler = (batch: IQueueMessageBatch) => Promise<void>

The queue export's shape, as Cloudflare invokes it.

T
ScheduledHandler = (controller: IScheduledController) => Promise<void>

The scheduled export's shape, as Cloudflare invokes it.

T
WaitUntilHost = (promise: Promise<unknown>) => void

A sink that keeps a Worker alive until the promise settles.

Usage

import * as Cloudflare_Workers_platform_bindings_for_Setu_TS___The_framework_has_served_traffic_on_Workers_since_the_Hono_migration__but_had_no_way_to_reach_the_platform_s_own_primitives___CloudflarePlugin__publishes_a_Worker_s_bindings_under__CAPABILITIES_CLOUDFLARE___and_optionally_serves_the_committed_cache_and_storage_capabilities_from_KV_and_R2___Zero_npm_dependencies__and_nothing_here_imports__cloudflare_workers____the_application_passes__env___and__waitUntil___in__which_keeps_the_package_type_checkable_on_every_runtime_and_trivially_testable___Every_export_is_documented_in_PUBLIC_API_md__ from "cloudflare-plugin/src/index.ts";