Cloudflare - v1.3.0
    Preparing search index...

    Class FlagshipBinding<E>

    Accessor for a Flagship feature-flag binding.

    Flagship evaluates a flag at request time, so a value that is a vars entry today — read once per deploy, changed only by deploying again — becomes something a targeting rule can move for a percentage of traffic, or for named callers first. That is the whole reason to reach for it: a rollback stops being a deploy.

    Every other accessor here answers a missing binding with a MissingBindingError, because a Worker that lost its database cannot serve the request either way and should say so. A flag is the opposite. The value exists to decide between two paths that both work, so a Worker that cannot reach Flagship should take the path it would have taken before flags existed — not fail.

    So every method here takes a fallback and returns it when the binding is absent, when evaluation fails, or when the flag's type does not match. An evaluation error is logged and swallowed, the way AnalyticsBinding's writeSafe treats a failed telemetry write: the caller asked a question that has a safe answer, and raising instead would make the flag service a new way for the Worker to be down.

    The corollary is that the fallback is not boilerplate. It is the deployed behaviour, and choosing it badly is how a flag outage becomes an incident — so pass the value the Worker used before the flag existed, which for the migration this package was written for means the vars entry it replaces.

    resolve(env) is still inherited and still throws, for the rare caller that genuinely cannot proceed without a live evaluation.

    Same reason as WorkflowBinding: the kernel names no capability this could implement. Unlike durable execution, feature flagging is the strongest candidate in this package for one — it has many providers, and none of its vocabulary is Cloudflare's. When a second provider is actually in play, a FeatureFlags interface belongs in @bayudwiyansatria/core and this becomes its adapter. Declaring it now, against one implementation and no consumers, would be ceremony rather than abstraction.

    const flags = new FlagshipBinding()

    // env.RELEASE_CHANNEL is the deployed value and the fallback if Flagship is unreachable.
    const channel = await flags.string(env, 'release-channel', env.RELEASE_CHANNEL ?? 'stable')

    // Per-request attributes for a targeting rule.
    const enabled = await flags.boolean(env, 'new-checkout', false, { cohort })

    Bayu Dwiyan Satria

    1.0.0

    1.3.0

    Type Parameters

    Hierarchy (View Summary)

    Index
    settings: () => FlagshipSettings

    This accessor's settings, resolved on first read.

    • get name(): string

      The binding name this instance resolves.

      Returns string

      The binding name as declared in wrangler.json.

    • Evaluates a boolean flag.

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The flag key, as it is named in the Flagship app.

      • fallback: boolean

        The value to use when the flag cannot be evaluated. Pass the deployed behaviour.

      • Optionalcontext: FlagshipEvaluationContext

        Per-request attributes for targeting, merged over the configured context.

      Returns Promise<boolean>

      The evaluated value, or fallback.

    • Merges a call's evaluation context over the configured one.

      Parameters

      • Optionalcontext: FlagshipEvaluationContext

        Per-request attributes, if the caller supplied any.

      Returns FlagshipEvaluationContext

      The combined context, or undefined when neither side has one.

      The call wins on any key both name. The configured context describes the deployment and the call describes the request, so the more specific of the two is the one that arrived last.

    • Runs an evaluation, answering with the fallback rather than raising.

      Type Parameters

      • T

        The flag's value type.

      Parameters

      • env: E

        The Worker environment.

      • fallback: T

        The value to answer with when evaluation cannot happen.

      • evaluate: (flags: Flagship) => Promise<T>

        The evaluation to attempt against the resolved binding.

      Returns Promise<T>

      The evaluated value, or fallback.

    • Reports whether this deployment can evaluate flags at all.

      Parameters

      • env: E

        The Worker environment.

      Returns boolean

      true when the binding is present.

      Rarely needed, since every method above already answers with its fallback. It is here for the one caller that wants to say so out loud — a health endpoint reporting degraded flag evaluation, or a log line explaining why every flag read the deployed value.

    • Checks whether the binding is available on the given environment.

      Use this to degrade gracefully when a binding is optional.

      Parameters

      • env: E

        The Worker environment.

      Returns boolean

      true when the binding is present.

    • Evaluates a numeric flag.

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The flag key, as it is named in the Flagship app.

      • fallback: number

        The value to use when the flag cannot be evaluated. Pass the deployed behaviour.

      • Optionalcontext: FlagshipEvaluationContext

        Per-request attributes for targeting, merged over the configured context.

      Returns Promise<number>

      The evaluated value, or fallback.

    • Evaluates a structured flag.

      Type Parameters

      • T extends object

        The shape the flag carries.

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The flag key, as it is named in the Flagship app.

      • fallback: T

        The value to use when the flag cannot be evaluated. Pass the deployed behaviour.

      • Optionalcontext: FlagshipEvaluationContext

        Per-request attributes for targeting, merged over the configured context.

      Returns Promise<T>

      The evaluated value, or fallback.

    • Evaluates a string flag.

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The flag key, as it is named in the Flagship app.

      • fallback: string

        The value to use when the flag cannot be evaluated. Pass the deployed behaviour.

      • Optionalcontext: FlagshipEvaluationContext

        Per-request attributes for targeting, merged over the configured context.

      Returns Promise<string>

      The evaluated value, or fallback.

      Useful for named modes, where the deployed fallback is a string rather than a boolean.

    • Resolves the binding without throwing.

      Parameters

      • env: E

        The Worker environment.

      Returns Flagship

      The resolved binding, or null when it is not configured.