npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

wrangler-convert

v1.0.4

Published

Convert Wrangler config to Cloudflare Worker metadata format

Downloads

13

Readme

Goal: wrangler-compatible workers augmentation

Wrangler Config Converter

This is a CLI and library that allows easily converting a wrangler.toml|json|jsonc and .dev.vars|.env into a metadata.json file.

A utility function to convert Cloudflare Wrangler configuration files (wrangler.toml) into the format expected by the Cloudflare Workers API.

Sources used:

  • https://unpkg.com/wrangler@latest/config-schema.json (Amazing!)
  • https://oapis.org/openapi/cloudflare/worker-script-upload-worker-module

Limitations

  • Does not take into account environment. Normally workers can have env: { staging, ... } but this is now completely ignored
  • Does not take into account that .ts files need to be built into a .js first before upload.
  • See considerations for additional things that need to be done before worker script upload

Installation

npm install wrangler-convert

Usage

Basic Usage

import { convertWranglerToWorkerConfig } from "wrangler-convert";

const wranglerConfig = {
  name: "my-worker",
  main: "src/index.js",
  compatibility_date: "2023-10-01",
  vars: {
    API_URL: "https://api.example.com",
    DEBUG: "true",
  },
  kv_namespaces: [
    {
      binding: "MY_KV",
      id: "your-kv-namespace-id",
    },
  ],
};

const result = convertWranglerToWorkerConfig(wranglerConfig);
console.log(result);

With Environment Variables

You can also pass environment variables that will override config variables:

import { convertWranglerToWorkerConfig } from "wrangler-convert";

const wranglerConfig = {
  name: "my-worker",
  main: "src/index.js",
  vars: {
    API_URL: "https://api.example.com",
  },
};

const envVars = {
  API_URL: "https://api.production.com", // This will override the config value
  SECRET_KEY: "prod-secret-123",
};

const result = convertWranglerToWorkerConfig(wranglerConfig, envVars);

Complete Example

import { convertWranglerToWorkerConfig } from "wrangler-convert";

const wranglerConfig = {
  name: "my-full-worker",
  main: "src/index.js",
  compatibility_date: "2023-10-01",
  compatibility_flags: ["nodejs_compat"],

  // Variables
  vars: {
    API_URL: "https://api.example.com",
    DEBUG: "false",
    CONFIG_OBJECT: { nested: "value" },
  },

  // KV Namespaces
  kv_namespaces: [
    {
      binding: "MY_KV",
      id: "kv-namespace-id",
      preview_id: "preview-kv-id",
    },
  ],

  // R2 Buckets
  r2_buckets: [
    {
      binding: "MY_BUCKET",
      bucket_name: "my-r2-bucket",
    },
  ],

  // D1 Databases
  d1_databases: [
    {
      binding: "DB",
      database_id: "d1-database-id",
    },
  ],

  // Durable Objects
  durable_objects: {
    bindings: [
      {
        name: "CHAT_ROOM",
        class_name: "ChatRoom",
        script_name: "chat-worker",
      },
    ],
  },

  // Services
  services: [
    {
      binding: "AUTH_SERVICE",
      service: "auth-worker",
      environment: "production",
    },
  ],

  // Routes
  routes: [
    "example.com/*",
    {
      pattern: "api.example.com/*",
      zone_id: "your-zone-id",
    },
  ],

  // AI Binding
  ai: {
    binding: "AI",
  },

  // Analytics Engine
  analytics_engine_datasets: [
    {
      binding: "ANALYTICS",
      dataset: "my-dataset",
    },
  ],

  // Queue Producers
  queues: {
    producers: [
      {
        binding: "MY_QUEUE",
        queue: "my-queue-name",
      },
    ],
  },

  // Assets
  assets: {
    directory: "dist",
    binding: "ASSETS",
    html_handling: "auto-trailing-slash",
  },
};

const result = convertWranglerToWorkerConfig(wranglerConfig);

// Result contains:
// - metadata: Worker metadata for API deployment
// - routes: Array of route configurations
// - scriptName: The worker's name
// - mainModule: The main module file (if module worker)

API Reference

convertWranglerToWorkerConfig(config, env?)

Converts a Wrangler configuration object to Worker API format.

Parameters

  • config (WranglerConfig): The Wrangler configuration object
  • env (Record<string, string>, optional): Environment variables that override config vars

Returns

Returns a ConversionResult object with the following properties:

  • metadata: Worker metadata object for API deployment
  • routes: Array of route configurations
  • scriptName: The worker's script name
  • mainModule: The main module file path (for module workers)

Supported Configuration Options

The converter supports all major Wrangler configuration options:

  • Basic Config: name, main, compatibility_date, compatibility_flags
  • Variables: vars (supports strings, numbers, objects)
  • Storage: kv_namespaces, r2_buckets, d1_databases
  • Compute: durable_objects, services, ai, browser
  • Networking: routes, route
  • Observability: analytics_engine_datasets, observability, logpush
  • Advanced: queues, vectorize, hyperdrive, mtls_certificates
  • Assets: assets configuration for static assets
  • Unsafe: unsafe.bindings for custom binding types

Example Output

{
  metadata: {
    main_module: "src/index.js",
    compatibility_date: "2023-10-01",
    compatibility_flags: ["nodejs_compat"],
    bindings: [
      {
        name: "API_URL",
        type: "plain_text",
        text: "https://api.example.com"
      },
      {
        name: "MY_KV",
        type: "kv_namespace",
        namespace_id: "kv-namespace-id"
      }
      // ... more bindings
    ]
  },
  routes: [
    {
      pattern: "example.com/*",
      script: "my-worker"
    }
  ],
  scriptName: "my-worker",
  mainModule: "src/index.js"
}

Considerations

Files that can be part of a Worker script

The multipart upload accepts these content types:

  • application/javascript+module / text/javascript+module - ES modules
  • application/javascript / text/javascript - Regular JS files
  • application/wasm - WebAssembly modules
  • text/plain - Text files
  • application/octet-stream - Binary files
  • application/source-map - Source maps

JWT, _headers, and _redirects placement

Since you already have the JWT from the asset upload service, here's where each goes:

{
  "metadata": {
    "main_module": "worker.js",
    "compatibility_date": "2023-10-01",
    "assets": {
      "jwt": "your-jwt-from-asset-service",
      "config": {
        "_headers": "# Headers file content\n/dashboard/*\nX-Frame-Options: DENY\n\n/static/*\nAccess-Control-Allow-Origin: *",
        "_redirects": "# Redirects file content\n/foo /bar 301\n/news/* /blog/:splat",
        "html_handling": "auto-trailing-slash",
        "not_found_handling": "404-page",
        "run_worker_first": ["/api/*", "/oauth/callback", "!/api/assets/*"]
      }
    },
    "bindings": [
      // ... your converted bindings
    ]
  }
}