Logo

Worker Tools

Tools for writing HTTP servers in Worker Environments such as Cloudflare Workers.

Tools for writing HTTP servers in Worker Environments such as Cloudflare Workers.

Worker Tools are a collection of TypeScript libraries for writing web servers in Worker Environments such as Cloudflare Workers.

Workers Tools accomplish many of the same goals as a web framework, but they are provided as standalone libraries.

All modules are written in TypeScript and provide full type declarations and source maps for the best developer experience (tested in VSCode).

Most have no dependencies beyond what is provided by a Worker Environment. For those that have dependencies, only such that provide ES module exports are used. This makes it possible to use them without a bundler using either UNPKG’s ?module parameter or Skypack. E.g.:

import { html } from 'https://unpkg.com/@worker-tools/html?module';
/* --- or --- */
import { html } from 'https://cdn.skypack.dev/@worker-tools/html?dts';

HTML Templating

HTML templating and streaming response library.

import { html, HTMLResponse } from '@worker-tools/html';
self.addEventListener('fetch', event => event.respondWith(
  new HTMLResponse(html`<html><body>
    <h1>Hello World!</h1>
    <ul>${['Foo', 'Bar', 'Baz'].map(x => html`<li>${x}</li>`)}</ul>
  </body></html>`);
));

View on GitHub

JSON Fetch

A drop-in replacements for fetch, Request, and Response with first class support for JSON objects.

import { JSONRequest } from '@worker-tools/json-fetch';
const body = { json: 'data' };
const response = await fetch(new JSONRequest('/api', { method: 'POST', body }));

View on GitHub

Response Creators

A collection of factory functions for Fetch API Responses with pre-filled status and status-text headers

import { ok } from '@worker-tools/response-creators'
self.addEventListener('fetch', event => event.respondWith(ok()))

View on GitHub

An implementation of the Cookie Store API for use within request handlers.

import { RequestCookieStore } from '@worker-tools/request-cookie-store';
const example = new Request('/', { headers: { 'cookie': 'foo=bar; fizz=buzz' } });
const cookieStore = new RequestCookieStore(example);

View on GitHub

An implementation of the Cookie Store API that transparently signs cookies via Web Cryptography API.

import { SignedCookieStore } from '@worker-tools/signed-cookie-store';
const key = await SignedCookieStore.deriveKey({ secret: 'Password123' });
const sigCookieStore = new SignedCookieStore(cookieStore, key);
await sigCookieStore.set('foo', 'bar');

It accepts any Cookie Store implementation, but it’s mostly meant to be used with Request Cookie Store.

View on GitHub

An implementation of the Cookie Store API that transparently encrypted cookie values via Web Cryptography API.

import { EncryptedCookieStore } from '@worker-tools/encrypted-cookie-store';
const key = await EncryptedCookieStore.deriveKey({ secret: 'Password123' });
const encCookieStore = new EncryptedCookieStore(cookieStore, key);
await encCookieStore.set('foo', 'bar');

It accepts any Cookie Store implementation, but it’s mostly meant to be used with Request Cookie Store.

View on GitHub

KV Storage

Worker Tools provides a suite of packages to bring the (discontinued) KV Storage API to Cloudflare Workers and Deno. While work on the specification itself has stopped, it’s still a good interface for asynchronous data access that feels native to JavaScript.

import { StorageArea } from '@worker-tools/cloudflare-kv-storage';
const storage = new StorageArea('foobar');
await storage.set(['foo', 1], ['bar', 2]);
await storage.get(['foo', 1]); // => ['bar', 2]

There are currently 2 packages with compatible APIs and different backing stores:

The Cloudflare implementation uses Cloudflare’s Workers KV as a backing store, while the Deno implementation comes with built-in SQLite and Postgres adapters and the option to provide custom adapters.

Deno Fetch Event Adapter

A short utility to support Cloudflare Worker’s fetch event in Deno.

self.addEventListener('fetch', event => {
  const ip = event.request.headers.get("x-forwarded-for");
  event.respondWith(new Response(`Hello ${ip}`, { 
    headers: [['content-type', 'text/plain']],
  }));
});

View on GitHub

Parsed HTML Rewriter

A DOM-based implementation of Cloudflare Worker’s HTMLRewriter.

Unlike the original, this implementation parses the entire DOM (provided by linkedom), and runs selectors against this representation. This choice was made to quickly implement the functionality using existing tools.

import '@worker-tools/parsed-html-rewriter/polyfill'
await new HTMLRewriter()
  .transform(new Response('<body></body>'))
  .text();

Note that unlike other Worker Tools, using this module in Deno without a bundler like esbuild may proof difficult due to the extensive tree of dependencies.

View on GitHub

Routing

Worker Tools currently only provides a placeholder for a future routing solution.

In the meantime, here are some alternatives:

  • Tiny Request Router. Highly recommended. It might be a little too tiny for many usecases, but it is a good starting point. Fully typed!

  • Workbox Routing. A routing solution for Service Workers made by Google. I haven’t tried to personally, but it shoud work in Cloudflare Workers as well.

Middleware

Worker Tools currently only provides a placeholder for a future middleware solution.

View on GitHub


© 2021. All rights reserved.

Powered by Hydejack v9.1.0-hysenberg.14