cloud

package
v0.99.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 7 Imported by: 2 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cloud is what Cloudflare gives a Go Worker that workers-go does not, or gives differently: for everything else, import workers-go's own packages (github.com/syumai/workers-go/cloudflare/…).

Database, KV, Bucket, Queue, Object,   what the Worker is bound to, as typed values: an app's are written
RateLimit, Mailer, Variable            from wrangler.jsonc into env/env.gen.go (names.go)
env.DB.Open(), env.WRITES.Allow(key)   a database for the queries; a Rate Limiting binding asked
env.MAIL.Send, ConsumeEmail            email out and in (email_js.go)
env.APP_NAME.Value()                   a variable of the Worker
HTTPClient()                           an http.Client over the Worker's fetch
Batch, and what queries.gen.go calls   D1 as typed rows and batches (d1.go)

A Worker runs on Cloudflare: on wrangler dev locally, deployed remotely. Nothing here runs anywhere else. The package still compiles for the machine it is written on (other.go), so editors, go vet and the program that writes an API's specs need no special setup; every call there answers ErrNotCloudflare.

Index

Constants

View Source
const (
	KindDatabase      = "database"
	KindKV            = "kv"
	KindBucket        = "bucket"
	KindQueue         = "queue"
	KindDurableObject = "durable object"
	KindRateLimit     = "rate limit"
	KindEmail         = "email"
	KindService       = "service"
	KindOther         = "other"
	KindText          = "text"
)

The kinds of binding.

View Source
const (
	BookmarkCookie = "d1"
	BookmarkHeader = "X-D1-Bookmark"
)

BookmarkCookie and BookmarkHeader are where a response gives a reader the bookmark after their write, and where their next request brings it back: the cookie for a browser, the header for a program. Each holds Bookmarks' String.

View Source
const BookmarkLasts = 300

BookmarkLasts is how long a browser keeps the cookie, in seconds. A copy is behind by less than a second as a rule; the cookie outlives that by far, and is gone before it is one more thing every request carries.

View Source
const NewestHeader = "X-D1-Newest"

NewestHeader is a request header that says the request reads what is newest: a part of a page asking for itself again because its feed said something changed (live.Follows). app.Serve reads it. Anyone may send it: it costs the trip to the primary, which is what every read cost before there were copies.

View Source
const OnWorkers = false

OnWorkers reports whether the code runs as a Worker.

Variables

View Source
var ErrNoBinding = errors.New("cloud: binding not configured")

ErrNoBinding means the Worker has no binding of that name (wrangler.jsonc). Allow still allows (it fails open, so a missing binding never takes an app down): log it.

View Source
var ErrNoProbe = errors.New("cloud: nothing to ask a binding of this kind")

ErrNoProbe is Probe's answer for a binding that has nothing harmless to ask.

View Source
var ErrNoRows = errors.New("d1: no rows")

ErrNoRows is One's error for a statement that returned no row.

View Source
var ErrNotCloudflare = errors.New("cloud: not running on Cloudflare (go tool charter dev, or deployed)")

ErrNotCloudflare is what every call answers in a program that is not a Worker.

Functions

func Batch

func Batch(DB, ...Statement) error

func Begin added in v0.55.0

func Begin(Bookmarks)

A request's session, which only a Worker has (session.go).

func ClientKey

func ClientKey(r *http.Request) string

ClientKey is a request's caller for a per-client limit: the client's IP, which Cloudflare puts in CF-Connecting-IP, or "local" without one.

func End added in v0.55.0

func End()

func Exec

func Exec(DB, string, ...any) error

func ExecRows

func ExecRows(DB, string, ...any) (int64, error)

func HTTPClient

func HTTPClient() *http.Client

func Newer added in v0.55.0

func Newer(a, b string) string

Newer is the later of two bookmarks of one database: they sort as text, oldest first (https://developers.cloudflare.com/d1/reference/time-travel/#bookmarks). The empty one is none.

func Newest added in v0.55.0

func Newest()

func One

func One[T any](db DB, query string, args ...any) (T, error)

One runs one statement and returns its first row: a SELECT that finds it, or a statement with a RETURNING clause. No row is ErrNoRows.

func OrNull

func OrNull[T any](value *T) any

OrNull is a nullable argument as a statement binds it: its value, or nil (NULL).

func Probe added in v0.49.0

func Probe(string) (time.Duration, error)

func Probes added in v0.49.0

func Probes(kind string) bool

Probes reports whether Probe has something to ask a binding of this kind.

func Query

func Query[T any](DB, string, ...any) ([]T, error)

func Runtimes added in v0.51.0

func Runtimes() (int, int64, int64, bool)

func Served added in v0.55.0

func Served() []string

func StaticAssets

func StaticAssets(string) http.Handler

func Var

func Var(string) string

Types

type Binding added in v0.49.0

type Binding struct {
	Name string `json:"name"`
	Kind string `json:"kind"`
	// Set, for a variable or a secret: whether it has a value. The value is never given.
	Set bool `json:"set,omitempty"`
}

Binding is one name of the Worker's environment, and what kind of thing is bound there.

func Bindings added in v0.49.0

func Bindings() []Binding

type Bookmarks added in v0.55.0

type Bookmarks map[string]string

Bookmarks is a bookmark for each database, by its binding's name.

func Brought added in v0.55.0

func Brought() Bookmarks

func ParseBookmarks added in v0.55.0

func ParseBookmarks(value string) Bookmarks

ParseBookmarks reads what a request brought (a cookie's value, a header's). What is not a bookmark is left out, so a value that is malformed, or someone's invention, starts nothing.

func Reached added in v0.62.0

func Reached() Bookmarks

func (Bookmarks) Join added in v0.55.0

func (b Bookmarks) Join(other Bookmarks) Bookmarks

Join is b with each bookmark of other that is newer, or that b has not: what a response gives a reader is never older than what their request brought, so two responses that cross cannot set a reader back.

func (Bookmarks) String added in v0.55.0

func (b Bookmarks) String() string

String is the bookmarks as a cookie's value or a header's: DB=0000007b-…, the databases in order.

type Browser added in v0.83.0

type Browser struct {
	// contains filtered or unexported fields
}

Browser is a browser binding (wrangler.jsonc's browser): Cloudflare's own headless Chrome, which draws a page of the app as a reader's browser would. It has no call in Go: a browser takes seconds, and nothing long holds a Go runtime, so Go says what is to be drawn and the Worker's own JavaScript asks for it (api's documents.go, worker/go.mjs). An app hands the binding to its People.

func BoundBrowser added in v0.83.0

func BoundBrowser(name string) Browser

func (Browser) Name added in v0.83.0

func (b Browser) Name() string

type Bucket added in v0.64.0

type Bucket struct {
	// contains filtered or unexported fields
}

Bucket is an R2 bucket (r2_buckets): Open is workers-go's *r2.Bucket.

func BoundBucket added in v0.64.0

func BoundBucket(name string) Bucket

func (Bucket) Name added in v0.64.0

func (b Bucket) Name() string

type DB

type DB struct{}

DB is a D1 database.

type Database added in v0.64.0

type Database struct {
	// contains filtered or unexported fields
}

Database is a D1 database (wrangler.jsonc's d1_databases): Open is it, for the queries.

func BoundDatabase added in v0.64.0

func BoundDatabase(name string) Database

func (Database) Name added in v0.64.0

func (b Database) Name() string

Name is the binding's name, as wrangler.jsonc has it.

func (Database) Open added in v0.64.0

func (b Database) Open() (DB, error)

Open is the database, for the queries. On a Worker that has no such binding it is an error that says so.

type KV added in v0.64.0

type KV struct {
	// contains filtered or unexported fields
}

KV is a KV namespace (kv_namespaces): Open is workers-go's *kv.Namespace.

func BoundKV added in v0.64.0

func BoundKV(name string) KV

func (KV) Name added in v0.64.0

func (b KV) Name() string

type Mailer added in v0.64.0

type Mailer struct {
	// contains filtered or unexported fields
}

Mailer is a send_email binding: Send sends through it.

func BoundMailer added in v0.64.0

func BoundMailer(name string) Mailer

func (Mailer) Name added in v0.64.0

func (b Mailer) Name() string

func (Mailer) Send added in v0.64.0

func (b Mailer) Send(from, to string, raw []byte) error

Send sends raw, a whole message as RFC 5322 has it (headers, a blank line, the body), from one address to another. Cloudflare delivers only to addresses the binding allows.

type Object added in v0.64.0

type Object struct {
	// contains filtered or unexported fields
}

Object is a Durable Object namespace (durable_objects.bindings): what a live.Feed's hub is in.

func BoundObject added in v0.64.0

func BoundObject(name string) Object

func (Object) Name added in v0.64.0

func (b Object) Name() string

type Queue added in v0.64.0

type Queue struct {
	// contains filtered or unexported fields
}

Queue is a queue the Worker sends to (queues.producers): Open is workers-go's *queues.Producer.

func BoundQueue added in v0.64.0

func BoundQueue(name string) Queue

func (Queue) Name added in v0.64.0

func (b Queue) Name() string

type RateLimit added in v0.64.0

type RateLimit struct {
	// contains filtered or unexported fields
}

RateLimit is a Rate Limiting binding (ratelimits), with what wrangler.jsonc says it allows: Calls in each span of Seconds. Allow asks it.

func BoundRateLimit added in v0.64.0

func BoundRateLimit(name string, calls, seconds int) RateLimit

BoundRateLimit is a Rate Limiting binding that allows calls in each span of seconds.

func (RateLimit) Allow added in v0.64.0

func (b RateLimit) Allow(key string) (bool, error)

Allow asks the binding whether key may make one more call. Its counter is local to the Cloudflare location, so it answers without a network round trip. A missing binding allows, with ErrNoBinding: it never takes an app down.

func (RateLimit) Calls added in v0.64.0

func (b RateLimit) Calls() int

Calls and Seconds are what the binding allows a caller: at most Calls in each span of Seconds.

func (RateLimit) Name added in v0.64.0

func (b RateLimit) Name() string

func (RateLimit) Seconds added in v0.64.0

func (b RateLimit) Seconds() int

type Statement

type Statement struct {
	Query string
	Args  []any
}

Statement is one statement of a Batch: its SQL and the arguments bound to its ? in order.

func Stmt

func Stmt(query string, args ...any) Statement

Stmt is a Statement, written as a call to Exec is: Stmt("DELETE FROM notes WHERE id = ?", id).

type Variable added in v0.64.0

type Variable struct {
	// contains filtered or unexported fields
}

Variable is a variable of the Worker (wrangler.jsonc's vars) or a secret (.dev.vars.example, fnox.toml): Value is what it holds, "" when it is not set.

func BoundVariable added in v0.64.0

func BoundVariable(name string) Variable

func (Variable) Name added in v0.64.0

func (b Variable) Name() string

func (Variable) Value added in v0.64.0

func (b Variable) Value() string

Value is what the variable or the secret holds, "" when it is not set.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL