Logo

Worker Environments

Worker Environments are the new standard for writing HTTP servers in JavaScript.

Learn More Contribute

Origin

Worker Environments are an adaptation of the Service Workers API, which is a browser standard for offline web applications. To give web developers more freedom over offline experiences, the specification includes a (minimal) HTTP server. Since it was published, other vendors have implemented this API for servers that run in the cloud β€” or on the edge in the case of Cloudflare Workers.

Typically, they also implement other browser APIs such as Fetch, Streams, and Web Cryptography, making their global scope similar to that of a Service Worker. We call them Worker Environments or Worker Contexts.

To see which vendors and APIs are available, check out the State of Worker Environments below. See the example below for a minimal HTTP server:

self.addEventListener('fetch', event => {
  event.respondWith(new Response('Hello World'));
});

Minimal example of a server written in a Worker Environment.

Backend for Frontend

Worker Environments fulfill the original promise of NodeJS: To use one language and share code between client and server. In practice, this never came to be. Instead the worlds of node and browsers have diverged1. Worker Environments are bringing them back together.

This is good news for Frontend Developers in particular: The knowledge ac- and required for building offline web applications can now be applied to writing HTTP servers β€” and so can the tools.

State of Worker Environments

There is currently one fully-featured Worker Environment and one alternative being implemented. There are four offline testing and development implementations.

Cloudflare Workers is the most complete Worker Environment. It is currently the only one implementing the global fetch event, bringing it on par with Service Workers. However, Deno is expected to follow shortly.


Cloudflare Workers Deno CLI Deno Deploy Miniflare cfworker/dev Cloudworker



Β Service WorkersCloudflare WorkersDeno CLIDeno DeployMiniflarecfworker/devCloudworkercloudflare-
worker-local
DomainBrowserEdgeServerEdgeTesting, DevTesting, DevTesting, DevTesting, Dev
Open Sourceβœ…πŸš«βœ…β„ΉοΈβœ…βœ…βœ…βœ…
1.0βœ…βœ…βœ…πŸš«βœ…βœ…πŸ’€βœ…

Legend

βœ…
Supported
ℹ️
Partial support / attention required
πŸ”œ
Implementation in progress
πŸ‘¨β€πŸ’»
Supported via 3rd party library / polyfill
❓
Support to be determined
🚫
Not supported

Browser APIs

The center piece of any Worker Environment is an implementation of the global fetch event. Implementations of other browser APIs are necessary for bridging the gap between different worker environments.

APIService WorkersCloudflare WorkersDeno CLIDeno DeployMiniflarecfworker/devCloudworkercloudflare-
worker-local
fetch eventβœ…βœ…πŸ‘¨β€πŸ’»βœ…βœ…βœ…βœ…βœ…
install eventβœ…πŸš«β“β“πŸš«πŸš«πŸš«πŸš«
activate eventβœ…πŸš«β“β“πŸš«πŸš«πŸš«πŸš«
URL APIβœ…β„ΉοΈβœ…βœ…βœ…βœ…βœ…βœ…
Fetch APIβœ…βœ…βœ…βœ…βœ…βœ…βœ…βœ…
Abort Controllerβœ…βœ…βœ…βœ…βœ…πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»
URL Pattern APIβ„ΉοΈπŸ‘¨β€πŸ’»βœ…βœ…βœ…β“β“β“
Encoding APIβœ…βœ…βœ…βœ…βœ…βœ…βœ…βœ…
Streams APIβ„ΉοΈβ„ΉοΈβœ…βœ…βœ…βœ…β„ΉοΈπŸš«
Encoding Streamsβ„ΉοΈπŸš«βœ…βœ…πŸš«πŸš«πŸš«πŸš«
Web Cryptography APIβœ…βœ…πŸ”œπŸ”œβœ…βœ…βœ…βœ…
crypto.randomUUID()β„ΉοΈβœ…βœ…βœ…βœ…β“β“β“
Cache APIβœ…β„ΉοΈβ“β“β„ΉοΈβœ…β„ΉοΈβ„ΉοΈ
Web Sockets APIβœ…β„ΉοΈβœ…βœ…β„ΉοΈπŸš«πŸš«πŸš«
Location APIβœ…πŸ‘¨β€πŸ’»οΈβœ…βœ…πŸ‘¨β€πŸ’»οΈπŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»
Timersβœ…βœ…βœ…βœ…βœ…βœ…βœ…βœ…
queueMicrotaskβ„ΉοΈβœ…βœ…βœ…βœ…β“β“β“
structuredCloneβ„ΉοΈβœ…βœ…βœ…βœ…β“β“β“
IndexedDBβœ…πŸš«β“β“πŸš«πŸš«πŸš«πŸš«

Working Drafts

The APIs below are either abandoned or do not have buy-in from major browser vendors. However, they can still be reasonable targets for 3rd party libraries, such as KV stores or cookie middleware.

APIService WorkersCloudflare WorkersDeno CLIDeno DeployMiniflarecfworker/devCloudworkercloudflare-
worker-local
KV Storage APIπŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»
Cookie Store APIβ„ΉοΈπŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»

Non-Standard APIs

These are useful APIs provided by one or more Worker Environment that aren’t on any standards track (including abandoned).

APIService WorkersCloudflare WorkersDeno CLIDeno DeployMiniflarecfworker/devCloudworkercloudflare-
worker-local
scheduled eventπŸš«βœ…πŸš«πŸš«βœ…πŸš«πŸš«πŸš«
HTMLRewriterπŸ‘¨β€πŸ’»βœ…πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»β„ΉοΈβœ…πŸ‘¨β€πŸ’»πŸ‘¨β€πŸ’»
KVπŸš«βœ…πŸš«πŸš«βœ…βœ…β„ΉοΈβ„ΉοΈ
Durable ObjectsπŸš«βœ…πŸš«πŸš«βœ…πŸš«πŸš«πŸš«

Frameworks

No HTTP Server is complete without a Web Framework for common tasks such as routing, sessions, authentication, and more. Unfortunately, Worker Environments do not have a framework at the level of polish that is common in Node.js, yet.

However, you can try cfworker/web which is a Web framework inspired by Koa and fastify.

You can also check out worker-tools, a collection of tools and libraries that achieve many of the same goals as a web framework, built by yours truly.

For more tools and libraries that work on Cloudflare Workers, check out Works on Workers.

Contributing

Are you aware of any other Worker Environments available or in development? Did you find any inaccuracies in the tables above? Open a PR in the workers.js.org repository!


  1. Node and the browser have diverged due to a lack of browser APIs for many crucial components, including HTTP, streams, file access, and more more. A lot has changed since then. Standards have been written for all of these and more, often informed by the experience of using the node-equivalent.Β ↩︎


Β© 2021. All rights reserved.

Powered by Hydejack v9.1.0-hysenberg.12