Web standards

最終更新日:2024-08-14 19:50:33

The CDNetworks Edge Cloud Apps Function runtime is built on top of the V8 JavaScript and WebAssembly engine and is regularly updated to the latest version, ensuring you can safely use the latest JavaScript features in your functions without transpilers. Almost all standard built-in objects supported by the stable release of Google Chrome are supported.

Web Standards and Global APIs

The following methods are available per the Worker Global Scope:

Base64 Utility Methods

atob()

Description: Decodes a string of data that has been encoded using base-64 encoding.

btoa()

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

Timers

setInterval()
setInterval(func: function): number;
setInterval(func: function, delay: number): number;
setInterval(func: function, delay: number, ...args: any[]): number;

Parameters:

  • func: The function to be executed.
  • delay (optional): The delay time in milliseconds.
  • ...args (optional): The arguments to be passed to the function.

Return Value: Returns a number representing the ID of the timer.

Description: Schedules a function to execute every time a given number of milliseconds elapses.

clearInterval()
clearInterval(intervalID: number): void;

Parameters:

  • intervalID: The ID of the timer.

Description: Cancels the repeated execution set using setInterval().

setTimeout()
setTimeout(func: function): number;
setTimeout(func: function, delay: number): number;
setTimeout(func: function, delay: number, ...args: any[]): number;

Parameters:

  • func: The function to be executed.
  • delay (optional): The delay time in milliseconds.
  • ...args (optional): The arguments to be passed to the function.

Return Value: Returns a number representing the ID of the timer.

Description: Schedules a function to execute after a given amount of time.

clearTimeout()
clearTimeout(timeoutID: number): void;

Parameters:

  • timeoutID: The ID of the timer.

Description: Cancels the delayed execution set using setTimeout().

Note: Timers are only available inside of the Request Context.

performance.timeOrigin and performance.now()

performance.timeOrigin

Description: Returns the high-resolution time origin. Workers uses the UNIX epoch as the time origin, meaning that performance.timeOrigin will always return 0.

performance.now()

Description: Returns a DOMHighResTimeStamp representing the number of milliseconds elapsed since performance.timeOrigin. Note that Workers intentionally reduces the precision of performance.now() such that it returns the time of the last I/O and does not advance during code execution. Effectively, because of this, and because performance.timeOrigin is always 0, performance.now() will always equal Date.now(), yielding a consistent view of the passage of time within a Worker.

EventTarget and Event

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

AbortController and AbortSignal

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

Fetch Global

fetch()

Description: Starts the process of fetching a resource from the network. Refer to Fetch API.

Note: The Fetch API is only available inside of the Request Context.

Encoding API

Both TextEncoder and TextDecoder support UTF-8 encoding/decoding. The TextEncoderStream and TextDecoderStream classes are also available.

URL API

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

Note:

The default URL class behavior differs from the URL Spec documented above.

A new spec-compliant implementation of the URL class can be enabled using the url_standard compatibility flag.

Compression Streams

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

URLPattern API

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

Intl

The Intl API allows you to format dates, times, numbers, and more to the format that is used by a provided locale (language and region).

navigator.userAgent

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).

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.
});

navigator.sendBeacon(url[, data])

When the global_navigator compatibility flag is set, the navigator.sendBeacon(...) API is available to send an HTTP POST request containing a small amount of data to a web server. This API is intended as a means of transmitting analytics or diagnostics information asynchronously on a best-effort basis.

For example, you can replace:

const promise = fetch('https://example.com', { method: 'POST', body: 'hello world' });
ctx.waitUntil(promise);

with navigator.sendBeacon(...):

navigator.sendBeacon('https://example.com', 'hello world');

References