Denoflare CLI

website·Indexed 33 days ago

https://denoflare.dev/

A tool to develop, test, and deploy Cloudflare Workers using Deno. The CLI provides capabilities to run workers locally via denoflare serve, publish scripts to Cloudflare Workers, Deno Deploy, AWS Lambda, and Supabase Edge Functions, and manage Cloudflare R2 storage, D1 databases, and Pub/Sub brokers. It also includes tools for dumping stats via the Cloudflare GraphQL Analytics API and deploying static sites to Cloudflare Pages.

Tokens
16K
Snippets
94
Records
125
Agent score
69%

What's inside Denoflare

  1. Overview of Denoflare CLI capabilities

    Denoflare is a single CLI tool designed to simplify the development and deployment of Cloudflare Workers using Deno. It provides first-class support for ESM-based workers and eliminates the dependency on Wrangler, Node.js, npm, or webpack. Key capabilities include:

    • Local development and testing in a Deno isolate (denoflare serve).
    • Deploying JS/TS workers from local files or HTTPS URLs (denoflare push).
    • Production monitoring via the command line (denoflare tail).
    • Generating static documentation sites for Cloudflare Pages (denoflare site).
    • Tracking Durable Objects and R2 costs/statistics (denoflare analytics).
    • Interacting with the Cloudflare REST API (denoflare cfapi).
    • Managing Cloudflare R2 storage via S3 compatibility API (denoflare r2).
    • Deploying to alternative platforms including Deno Deploy, AWS Lambda, and Supabase Edge Functions (denoflare push-deploy, denoflare push-lambda, denoflare push-supabase).
  2. Overview of Webtail for Cloudflare Workers

    Webtail is an open-source Cloudflare Worker that allows developers to view live requests and logs from Cloudflare Workers in a browser. It provides several enhancements over the default Cloudflare dashboard, including the ability to tail multiple workers simultaneously, advanced filtering with multi-color output (similar to wrangler tail), support for multiple account profiles stored locally in the browser, and the ability to surface Durable Object metadata via logprops.
  3. Overview of denoflare r2 CLI

    The denoflare r2 CLI allows developers to manage Cloudflare R2 storage via the S3 compatibility API. It is built using a lightweight R2 TypeScript API that depends only on fast-xml-parser and web standards, making it suitable for use in Cloudflare Workers. The CLI provides subcommands for bucket management, object manipulation, multipart uploads, credential generation, and presigned URL creation.
    denoflare r2 --help
  4. Manage Cloudflare Queues via denoflare cfapi

    Use these subcommands to manage Cloudflare Queues and their consumers:

    • list-queues: List queues owned by an account
    • create-queue: Create a new queue
    • get-queue: Get information about a specific queue
    • update-queue: Update a queue (does not support partial updates)
    • delete-queue: Delete a queue
    • create-queue-consumer: Create a new consumer for a queue
    • list-queue-consumers: List consumers for a queue
    • update-queue-consumer: Update an existing queue consumer
    • delete-queue-consumer: Delete a queue consumer
    • pull-queue-messages: Pull messages from a queue
    • ack-queue-messages: Acknowledge or retry queue messages
    • preview-queue-messages: Preview queue messages
    • send-queue-message: Send a message to a queue
  5. Generate a static docs site with denoflare site

    The denoflare site command allows you to generate a static documentation site from Markdown files stored in a GitHub repository, mimicking the style of Cloudflare's Docs Engine. The generated output can be hosted on Cloudflare Pages.
  6. Install Denoflare CLI globally via Deno

    Denoflare is a Deno program and can be installed globally using the deno install command. This allows the denoflare binary to be available in your path.
    deno install --global -A --unstable-worker-options --name denoflare --force \
    https://raw.githubusercontent.com/skymethod/denoflare/v0.7.0/cli/cli.ts
  7. Run Denoflare CLI without installation (CI/CD)

    For environments like CI builds where a permanent installation is not desired, you can execute Denoflare directly using deno run. Replace any denoflare command with the full deno run invocation.
    # Example: instead of 'denoflare serve x'
    deno run -A --unstable-worker-options \
    https://raw.githubusercontent.com/skymethod/denoflare/v0.7.0/cli/cli.ts serve x
  8. Prerequisites for deploying R2 public-read worker

    To deploy the R2 public-read worker, ensure you have the following:

    1. A Cloudflare account with R2 billing enabled.
    2. An R2 bucket containing the data to be made public.
    3. A target domain or subdomain on an active Cloudflare zone.
    4. A Cloudflare custom API token with the following permissions:
      • Workers Scripts
      • Custom Domains for Workers
      • Read Stream
    5. Deno and denoflare installed locally.
  9. Enable hot-reloading for Supabase Edge Functions with --watch

    Use the --watch flag to monitor the local file system and automatically re-upload the script to Supabase whenever changes are detected. You can use --watch-include <path>... to specify additional paths to watch, such as directories containing dynamically-imported static resources.
    denoflare push-supabase <script-spec> --watch --watch-include ./static-assets
  10. Enable hot-reloading and watch mode for denoflare push-lambda

    To automatically re-upload the worker script upon local file changes, use the --watch flag. You can specify additional paths to monitor (such as dynamically imported static resources) using --watch-include <path>....
    denoflare push-lambda <script-spec> --watch --watch-include ./static
  11. Enhance Cloudflare Worker logs using logprops

    You can add additional metadata to Webtail's tail output by calling console.log with the logprops: prefix followed by a JSON object. This is particularly useful for surfacing information in Durable Object requests that is not included by default. Supported properties include:

    • colo: The data center location (e.g., DFW).
    • durableObjectClass: The name of the Durable Object class (e.g., MyCounterDO).
    • durableObjectId: The hex ID of the Durable Object instance (available via state.id).
    • durableObjectName: The name used in the idFromName call.
    // Option 1: Using a string prefix
    console.log(`logprops: ${JSON.stringify({ colo: 'DFW' })}`);
    
    // Option 2: Passing as an additional object argument
    console.log('logprops:', { colo: 'DFW' });
  12. Use WebAssembly in module-based workers with importWasm

    Cloudflare requires Wasm modules in module-based workers to be imported using a specific ES6 syntax (import module from './hello.wasm') that is not natively supported by Deno or browsers. To bridge this, Denoflare provides the importWasm utility. This function allows the code to run locally via denoflare serve and is automatically rewritten to the required Cloudflare syntax during denoflare push.
    import { importWasm } from 'https://raw.githubusercontent.com/skymethod/denoflare/v0.7.0/common/import_wasm.ts';
    
    // Load the Wasm module
    const module = await importWasm(import.meta.url, './hello.wasm');
    
    export default {
        fetch(): Response {
            try {
                // Instantiate the module and call an exported function
                const instance = new WebAssembly.Instance(module);
                const result = (instance.exports.main as CallableFunction)();
    
                return new Response(`<h3>Result: ${result}</h3>`, {
                    status: 200, 
                    headers: { 'Content-Type': 'text/html; charset=utf-8' }
                });
            } catch (e) {
                return new Response(`${e}`, { status: 500 });
            }
        }
    };