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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@specprotected/spec-proxy-cloudflare-worker

v0.2.4

Published

Spec Proxy implementation for Cloudflare Edge Workers

Downloads

221

Readme

Spec Proxy Cloudflare Worker API Integration

This document describes a method of integrating with Spec Proxy through a Cloudflare Edge Worker.

We won't cover in detail how to use a Cloudflare Worker and will instead direct you to follow their documentation to get a worker project setup and deployed. Once deployed, all you need do is copy our sample code into your edge worker function as well as communicate with the Spec team to coordinate the Spec Proxy deployment.

Cloudflare Worker Documentation

Note: Cloudflare has moved to more prominently display their new module-based workers. The Service Worker API will still be supported, though.

Implementation Example

Using our library is simple. We require only a single function call if we are the only library you are using.

// Import the Spec Proxy library function
import { specProxyProcess } from "@specprotected/spec-proxy-cloudflare-worker";

// Option 1: Cloudflare ES Modules
export default {
  async fetch(request, env, ctx) {
    // If we don't catch the exception, fail open to original traffic path
    ctx.passThroughOnException();

    // wrap up the context methods that our library uses alongside the request
    let eventWrapper = {
      waitUntil: ctx.waitUntil.bind(ctx),
      request,
    };

    return specProxyProcess(eventWrapper, {
      inlineMode: true,
    }));
  }
}

// Option 2: Cloudflare Service Worker
addEventListener('fetch', event => {
  // If we don't catch the exception, fail open to original traffic path
  event.passThroughOnException();

  event.respondWith(
    specProxyProcess(event, {
      inlineMode: true,
    })
  );
});

There's no additional configuration of our library required in order to use it. Your Cloudflare configuration and your Spec Proxy deployment take care of all of the inner details of routing traffic. The only additional thing you will have to configure is how to route traffic to your worker, which is typically done through the use of Cloudflare Routes.

Runtime Configuration

You can use Cloudflare KV to dynamically configure how the Spec Proxy library behaves during runtime. When you create your KV store in the Cloudflare web UI, be sure to add it to your worker. Below is an example of how to integrate the KV store with your Spec Proxy Cloudflare worker after you have created it.

import { specProxyProcess } from "@specprotected/spec-proxy-cloudflare-worker";

// Option 1: Cloudflare ES Modules
export default {
  async fetch(request, env, ctx) {
    // If we don't catch the exception, fail open to original traffic path
    ctx.passThroughOnException();

    return await handler(request, env, ctx);
  },
};

async function handler(request, env, ctx) {
    // Note: null is returned if the key isn't present, so the value
    // of inlineMode and disableSpecProxy would default to false
    let inlineMode = (await env.SPEC_PROXY.get("inline_mode")) === "true";
    let disableSpecProxy =
      (await env.SPEC_PROXY.get("disable_spec_proxy")) === "true";

    // configure our use of the spec library
    let config = {
      inlineMode,
      disableSpecProxy,
    };

    // only assign percentage of IPs if it's valid
    let percentageIPs = parseInt(await env.SPEC_PROXY.get("percentage_of_ips"));
    if (!isNaN(percentageIPs)) {
      config.percentageOfIPs = percentageIPs;
    }

    // wrap up the context methods that our library uses alongside the request
    let eventWrapper = {
      waitUntil: ctx.waitUntil.bind(ctx),
      request,
    };

    return await specProxyProcess(eventWrapper, config);
  },
};

// Option 2: Cloudflare Service Worker
addEventListener('fetch', event => {
  // If we don't catch the exception, fail open to original traffic path
  event.passThroughOnException();

  // respond with an async function so we can `await` the KV accesses
  event.respondWith(handler(event));
});

async function handler(event) {
  // Note: null is returned if the key isn't present, so the value
  // of inlineMode and disableSpecProxy would default to false
  let inlineMode = (await SPEC_PROXY.get("inline_mode")) === "true";
  let disableSpecProxy =
    (await SPEC_PROXY.get("disable_spec_proxy")) === "true";

  // configure our use of the spec library
  let config = {
    inlineMode,
    disableSpecProxy,
  };

  // only assign percentage of IPs if it's valid
  let percentageIPs = parseInt(dictionary.get("percentage_of_ips"));
  if (!isNaN(percentageIPs)) {
    config.percentageOfIPs = percentageIPs;
  }

  return await specProxyProcess(event, config);
};