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
- Variables
- func Batch(DB, ...Statement) error
- func Begin(Bookmarks)
- func ClientKey(r *http.Request) string
- func End()
- func Exec(DB, string, ...any) error
- func ExecRows(DB, string, ...any) (int64, error)
- func HTTPClient() *http.Client
- func Newer(a, b string) string
- func Newest()
- func One[T any](db DB, query string, args ...any) (T, error)
- func OrNull[T any](value *T) any
- func Probe(string) (time.Duration, error)
- func Probes(kind string) bool
- func Query[T any](DB, string, ...any) ([]T, error)
- func Runtimes() (int, int64, int64, bool)
- func Served() []string
- func StaticAssets(string) http.Handler
- func Var(string) string
- type Binding
- type Bookmarks
- type Browser
- type Bucket
- type DB
- type Database
- type KV
- type Mailer
- type Object
- type Queue
- type RateLimit
- type Statement
- type Variable
Constants ¶
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.
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.
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.
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.
const OnWorkers = false
OnWorkers reports whether the code runs as a Worker.
Variables ¶
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.
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.
var ErrNoRows = errors.New("d1: no rows")
ErrNoRows is One's error for a statement that returned no row.
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 Begin ¶ added in v0.55.0
func Begin(Bookmarks)
A request's session, which only a Worker has (session.go).
func ClientKey ¶
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 HTTPClient ¶
func Newer ¶ added in v0.55.0
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 One ¶
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 Probes ¶ added in v0.49.0
Probes reports whether Probe has something to ask a binding of this kind.
func StaticAssets ¶
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.
type Bookmarks ¶ added in v0.55.0
Bookmarks is a bookmark for each database, by its binding's name.
func ParseBookmarks ¶ added in v0.55.0
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.
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
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
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
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.
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
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
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
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
BoundRateLimit is a Rate Limiting binding that allows calls in each span of seconds.
func (RateLimit) Allow ¶ added in v0.64.0
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.
type Statement ¶
Statement is one statement of a Batch: its SQL and the arguments bound to its ? in order.
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.