Cloudflare Docs
Workers
Workers
Visit Workers on GitHub
Set theme to dark (⇧+D)

JavaScript and web standards

The Workers runtime provides the following standardized APIs for use by Workers running on Cloudflare’s global network.


​​ JavaScript standards

Cloudflare Workers uses the V8 JavaScript engine from Google Chrome. The Workers runtime is updated at least once a week, to at least the version that is currently used by Chrome’s stable release. This means you can safely use the latest JavaScript features, with no need for transpilers.

All of the standard built-in objects supported by the current Google Chrome stable release are supported, with a few notable exceptions:

  • eval() is not allowed for security reasons.
  • new Function is not allowed for security reasons.
  • Date.now() returns the time of the last I/O; it does not advance during code execution.

​​ Web global APIs

The following methods are available per the Worker Global Scope:

​​ Base64 utility methods

  • atob()

    • Decodes a string of data which has been encoded using base-64 encoding.
  • btoa()

    • Creates a base-64 encoded ASCII string from a string of binary data.

​​ Timers

​​ EventTarget and Event

The EventTarget and Event API allow objects to publish and subscribe to events.

​​ AbortController and AbortSignal

The AbortController and AbortSignal APIs provide a common model for canceling asynchronous operations.

​​ Fetch global


​​ Encoding API

Both TextEncoder and TextDecoder support UTF-8 encoding/decoding.

Refer to the MDN documentation for more information.

The TextEncoderStream and TextDecoderStream classes are also available.


​​ URL API

The URL API supports URLs conforming to HTTP and HTTPS schemes.

Refer to the MDN documentation for more information


​​ Compression Streams

The CompressionStream and DecompressionStream classes support the deflate, deflate-raw and gzip compression methods.

Refer to the MDN documentation for more information


​​ URLPattern API

The URLPattern API provides a mechanism for matching URLs based on a convenient pattern syntax.

Refer to the MDN documentation for more information.


When the global_navigator compatibility flag is set, the navigator.userAgent property is available with the value 'Cloudflare-Workers'. This can be used, for example, to reliably determine that code is running within the Workers environment.

​​ Unhandled promise rejections

The unhandledrejection event is emitted by the global scope when a JavaScript promise is rejected without a rejection handler attached.

The rejectionhandled event is emitted by the global scope when a JavaScript promise rejection is handled late (after a rejection handler is attached to the promise after an unhandledrejection event has already been emitted).

worker.js
addEventListener('unhandledrejection', (event) => {
console.log(event.promise); // The promise that was rejected.
console.log(event.reason); // The value or Error with which the promise was rejected.
});
addEventListener('rejectionhandled', (event) => {
console.log(event.promise); // The promise that was rejected.
console.log(event.reason); // The value or Error with which the promise was rejected.
});