JavaScript runtime 2.0 features for CloudFront Functions (original) (raw)

The CloudFront Functions JavaScript runtime environment is compliant with ECMAScript (ES) version 5.1 and also supports some features of ES versions 6 through 12. It also provides some nonstandard methods that are not part of the ES specifications. The following topics list all supported features in this runtime.

Topics

Core features

The following core features of ES are supported.

Types

All ES 5.1 types are supported. This includes boolean values, numbers, strings, objects, arrays, functions, and regular expressions.

Operators

All ES 5.1 operators are supported.

The ES 7 exponentiation operator (**) is supported.

Statements

The following ES 5.1 statements are supported:

The following ES 6 statements are supported:

The following ES 8 statements are supported:

Note

async, await, const, and let are supported in JavaScript runtime 2.0.

await can be used inside async functions only. async arguments and closures are not supported.

Literals

ES 6 template literals are supported: multiline strings, expression interpolation, and nesting templates.

Functions

All ES 5.1 function features are supported.

ES 6 arrow functions are supported, and ES 6 rest parameter syntax is supported.

Unicode

Source text and string literals can contain Unicode-encoded characters. Unicode code point escape sequences of six characters (for example,\uXXXX) are also supported.

Strict mode

Functions operate in strict mode by default, so you don’t need to add ause strict statement in your function code. This cannot be changed.

Primitive objects

The following primitive objects of ES are supported.

Object

The following ES 5.1 methods on objects are supported:

The following ES 6 methods on objects are supported:

The following ES 8 methods on objects are supported:

The following ES 5.1 prototype methods on objects are supported:

The following ES 6 prototype methods on objects are supported:

String

The following ES 5.1 methods on strings are supported:

The following ES 6 methods on strings are supported:

The following ES 5.1 prototype methods on strings are supported:

The following ES 6 prototype methods on strings are supported:

The following ES 8 prototype methods on strings are supported:

The following ES 9 prototype methods on strings are supported:

The following ES 12 prototype methods on strings are supported:

Note

String.prototype.replaceAll() is new in JavaScript runtime 2.0.

Number

ALL ES 5 numbers are supported.

The following ES 6 properties on numbers are supported:

The following ES 6 methods on numbers are supported:

The following ES 5.1 prototype methods on numbers are supported:

ES 12 numeric separators are supported.

Note

ES 12 numeric separators are new in JavaScript runtime 2.0.

Built-in objects

The following built-in objects of ES are supported.

Math

All ES 5.1 math methods are supported.

Note

In the CloudFront Functions runtime environment, theMath.random() implementation uses OpenBSDarc4random seeded with the timestamp of when the function runs.

The following ES 6 math properties are supported:

The following ES 6 math methods are supported:

Date

All ES 5.1 Date features are supported.

Note

For security reasons, Date always returns the same value—the function’s start time—during the lifetime of a single function run. For more information, see Restricted features.

Function

The following ES 5.1 prototype methods are supported:

Function constructors are not supported.

Regular expressions

All ES 5.1 regular expression features are supported. The regular expression language is Perl compatible.

The following ES 5.1 prototype accessor properties are supported:

Note

RegExp.prototype.sticky andRegExp.prototype.flags are new in JavaScript runtime 2.0.

The following ES 5.1 prototype methods are supported:

Note

RegExp.prototype[@@split]() is new in JavaScript runtime 2.0.

The following ES 5.1 instance properties are supported:

ES 9 named capture groups are supported.

JSON

The following ES 5.1 methods are supported:

Array

The following ES 5.1 methods on arrays are supported:

The following ES 6 methods on arrays are supported:

The following ES 5.1 prototype methods are supported:

The following ES 6 prototype methods are supported

The following ES 7 prototype methods are supported:

Typed arrays

The following ES 6 typed array constructors are supported:

The following ES 6 methods are supported:

Note

TypedArray.from() andTypedArray.of() are new in JavaScript runtime 2.0.

The following ES 6 prototype methods are supported:

Note

TypedArray.prototype.every(),TypedArray.prototype.fill(),TypedArray.prototype.filter(),TypedArray.prototype.find(),TypedArray.prototype.findIndex(),TypedArray.prototype.forEach(),TypedArray.prototype.includes(),TypedArray.prototype.indexOf(),TypedArray.prototype.join(),TypedArray.prototype.lastIndexOf(),TypedArray.prototype.map(),TypedArray.prototype.reduce(),TypedArray.prototype.reduceRight(),TypedArray.prototype.reverse(), andTypedArray.prototype.some() are new in JavaScript runtime 2.0.

ArrayBuffer

The following ES 6 methods on ArrayBuffer are supported:

The following ES 6 prototype methods on ArrayBuffer are supported:

Promise

The following ES 6 methods on promises are supported:

Note

Promise.all(), Promise.allSettled(),Promise.any(), and Promise.race() are new in JavaScript runtime 2.0.

The following ES 6 prototype methods on promises are supported:

DataView

The following ES 6 prototype methods are supported:

Note

All Dataview ES 6 prototype methods are new in JavaScript runtime 2.0.

Symbol

The following ES 6 methods are supported:

Note

All Symbol ES 6 methods are new in JavaScript runtime 2.0.

Text Decoder

The following prototype methods are supported:

The following prototype accessor properties are supported:

Text Encoder

The following prototype methods are supported:

Error types

The following error objects are supported:

Globals

The globalThis object is supported.

The following ES 5.1 global functions are supported:

The following ES 6 global functions are supported:

Note

atob() and btoa() are new in JavaScript runtime 2.0.

The following global constants are supported:

Built-in modules

The following built-in modules are supported.

Buffer

The module provides the following methods:

The module provides the following buffer prototype methods:

The following instance methods are supported:

The following instance properties are supported:

Note

All Buffer module methods are new in JavaScript runtime 2.0.

Query string

The query string module (querystring) provides methods for parsing and formatting URL query strings. You can load the module usingrequire('querystring'). The module provides the following methods.

querystring.escape(_string_)

URL-encodes the given string, returning an escaped query string. The method is used by querystring.stringify() and should not be used directly.

querystring.parse(_string_[,_separator_[, _equal_[, _options_]]])

Parses a query string (string) and returns an object.

The separator parameter is a substring for delimiting key and value pairs in the query string. By default it is&.

The equal parameter is a substring for delimiting keys and values in the query string. By default it is =.

The options parameter is an object with the following keys:

decodeURIComponent _function_

A function to decode percent-encoded characters in the query string. By default it isquerystring.unescape().

maxKeys _number_

The maximum number of keys to parse. By default it is1000. Use a value of 0 to remove the limitations for counting keys.

By default, percent-encoded characters within the query string are assumed to use the UTF-8 encoding. Invalid UTF-8 sequences are replaced with the U+FFFD replacement character.

For example, for the following query string:

'name=value&abc=xyz&abc=123'

The return value of querystring.parse() is:

{
name: 'value',
abc: ['xyz', '123']
}

querystring.decode() is an alias forquerystring.parse().

querystring.stringify(_object_[,_separator_[, _equal_[, _options_]]])

Serializes an object and returns a query string.

The separator parameter is a substring for delimiting key and value pairs in the query string. By default it is&.

The equal parameter is a substring for delimiting keys and values in the query string. By default it is =.

The options parameter is an object with the following keys:

encodeURIComponent _function_

The function to use for converting URL-unsafe characters to percent-encoding in the query string. By default it isquerystring.escape().

By default, characters that require percent-encoding within the query string are encoded as UTF-8. To use a different encoding, specify theencodeURIComponent option.

For example, for the following code:

querystring.stringify({ name: 'value', abc: ['xyz', '123'], anotherName: '' });

The return value is:

'name=value&abc=xyz&abc=123&anotherName='

querystring.encode() is an alias forquerystring.stringify().

querystring.unescape(_string_)

Decodes URL percent-encoded characters in the givenstring, returning an unescaped query string. This method is used by querystring.parse() and should not be used directly.

Crypto

The cryptographic module (crypto) provides standard hashing and hash-based message authentication code (HMAC) helpers. You can load the module usingrequire('crypto').

Hashing methods

crypto.createHash(_algorithm_)

Creates and returns a hash object that you can use to generate hash digests using the given algorithm: md5, sha1, or sha256.

hash.update(_data_)

Updates the hash content with the given data.

hash.digest([_encoding_])

Calculates the digest of all of the data passed usinghash.update(). The encoding can be hex,base64, or base64url.

HMAC methods

crypto.createHmac(_algorithm_,_secret key_)

Creates and returns an HMAC object that uses the givenalgorithm and secret key. The algorithm can be md5, sha1, orsha256.

hmac.update(_data_)

Updates the HMAC content with the given data.

hmac.digest([_encoding_])

Calculates the digest of all of the data passed usinghmac.update(). The encoding can be hex,base64, or base64url.

Restricted features

The following JavaScript language features are either unsupported or restricted due to security concerns.

Dynamic code evaluation

Dynamic code evaluation is not supported. Both eval() andFunction constructors throw an error if attempted. For example, const sum = new Function('a', 'b', 'return a + b') throws an error.

Timers

The setTimeout(), setImmediate(), andclearTimeout() functions are not supported. There is no provision to defer or yield within a function run. Your function must synchronously run to completion.

Date and timestamps

For security reasons, there is no access to high-resolution timers. AllDate methods to query the current time always return the same value during the lifetime of a single function run. The returned timestamp is the time when the function started running. Consequently, you cannot measure elapsed time in your function.

File system access

There is no file system access. For example, there is no fs module for file system access like there is in Node.js.

Process access

There is no process access. For example, there is no process global object for processing information access like there is in Node.js.

Environment variables

There is no access to environment variables. Instead, you can use CloudFront KeyValueStore to create a centralized datastore of key-value pairs for your CloudFront Functions. CloudFront KeyValueStore enables dynamic updates to your configuration data without needing to deploy code changes. For more information, see Amazon CloudFront KeyValueStore.

Network access

There is no support for network calls. For example, XHR, HTTP(S), and socket are not supported.