

# 适用于 CloudFront Functions 的 JavaScript 运行时系统特征
<a name="functions-javascript-runtime-features"></a>

CloudFront Functions JavaScript 运行时系统环境符合 [ECMAScript (ES) 5.1](https://www.ecma-international.org/ecma-262/5.1/) 的要求，也支持 ES 版本 6 至 9 的某些特征。

为了获得最新的功能，建议您使用 JavaScript 运行时 2.0。

与 1.0 相比，JavaScript 运行时 2.0 有以下变化：
+ 可使用缓冲区模块方法
+ 以下非标准字符串原型方法不可用：
  + `String.prototype.bytesFrom()`
  + `String.prototype.fromBytes()`
  + `String.prototype.fromUTF8()`
  + `String.prototype.toBytes()`
  + `String.prototype.toUTF8()`
+ 加密模块包含以下更改：
  + `hash.digest()` - 如果未提供编码，则返回类型更改为 `Buffer`
  + `hmac.digest()` - 如果未提供编码，则返回类型更改为 `Buffer`
+ 有关其他新功能的更多信息，请参阅[适用于 CloudFront Functions 的 JavaScript 运行时系统 2.0 特征](functions-javascript-runtime-20.md)。

**Topics**
+ [JavaScript 运行时 1.0 功能](functions-javascript-runtime-10.md)
+ [JavaScript 运行时系统 2.0 特征](functions-javascript-runtime-20.md)

# 适用于 CloudFront Functions 的 JavaScript 运行时 1.0 功能
<a name="functions-javascript-runtime-10"></a>

CloudFront Functions JavaScript 运行时环境符合 [ECMAScript (ES) 版本 5.1](https://262.ecma-international.org/5.1/)，也支持 ES 版本 6 至 9 的某些功能。它还提供了一些不属于 ES 规范的非标准方法。

以下主题列出了所有支持的语言功能。

**Topics**
+ [

## 核心功能
](#writing-functions-javascript-features-core)
+ [

## 原语对象
](#writing-functions-javascript-features-primitive-objects)
+ [

## 内置对象
](#writing-functions-javascript-features-builtin-objects)
+ [

## 错误类型
](#writing-functions-javascript-features-error-types)
+ [

## 全局变量
](#writing-functions-javascript-features-globals)
+ [

## 内置模块
](#writing-functions-javascript-features-builtin-modules)
+ [

## 受限功能
](#writing-functions-javascript-features-restricted-features)

## 核心功能
<a name="writing-functions-javascript-features-core"></a>

支持 ES 的以下核心功能。

**类型**  
支持所有 ES 5.1 类型。这包括布尔值、数字、字符串、对象、数组、函数、函数构造函数和正则表达式。

**运算符**  
支持所有 ES 5.1 运算符。  
支持 ES 7 幂运算符 (`**`)。

**语句**  
不支持 `const` 和 `let` 语句。
支持以下 ES 5.1 语句：  
+ `break`
+ `catch`
+ `continue`
+ `do-while`
+ `else`
+ `finally`
+ `for`
+ `for-in`
+ `if`
+ `return`
+ `switch`
+ `throw`
+ `try`
+ `var`
+ `while`
+ 带标签的语句

**文本**  
支持 ES 6 模板文本：多行字符串、表达式插值和嵌套模板。

**函数**  
支持所有的 ES 5.1 函数功能。  
支持 ES 6 箭头函数，并支持 ES 6 rest 参数语法。

**Unicode**  
源文本和字符串文本可以包含 Unicode 编码的字符。还支持由六个字符组成的 Unicode 代码点转义序列（例如，`\uXXXX`）。

**严格模式**  
默认情况下，函数在严格模式下运行，因此您无需在函数代码中添加 `use strict` 语句。无法对其进行更改。

## 原语对象
<a name="writing-functions-javascript-features-primitive-objects"></a>

支持 ES 的以下原语对象。

**对象**  
支持对对象使用以下 ES 5.1 方法：  
+ `create`（没有属性列表）
+ `defineProperties`
+ `defineProperty`
+ `freeze`
+ `getOwnPropertyDescriptor`
+ `getOwnPropertyNames`
+ `getPrototypeOf`
+ `hasOwnProperty`
+ `isExtensible`
+ `isFrozen`
+ `prototype.isPrototypeOf`
+ `isSealed`
+ `keys`
+ `preventExtensions`
+ `prototype.propertyIsEnumerable`
+ `seal`
+ `prototype.toString`
+ `prototype.valueOf`
支持对对象使用以下 ES 6 方法：  
+ `assign`
+ `is`
+ `prototype.setPrototypeOf`
支持对对象使用以下 ES 8 方法：  
+ `entries`
+ `values`

**字符串**  
支持对字符串使用以下 ES 5.1 方法：  
+ `fromCharCode`
+ `prototype.charAt`
+ `prototype.concat`
+ `prototype.indexOf`
+ `prototype.lastIndexOf`
+ `prototype.match`
+ `prototype.replace`
+ `prototype.search`
+ `prototype.slice`
+ `prototype.split`
+ `prototype.substr`
+ `prototype.substring`
+ `prototype.toLowerCase`
+ `prototype.trim`
+ `prototype.toUpperCase`
支持对字符串使用以下 ES 6 方法：  
+ `fromCodePoint`
+ `prototype.codePointAt`
+ `prototype.endsWith`
+ `prototype.includes`
+ `prototype.repeat`
+ `prototype.startsWith`
支持对字符串使用以下 ES 8 方法：  
+ `prototype.padStart`
+ `prototype.padEnd`
支持对字符串使用以下 ES 9 方法：  
+ `prototype.trimStart`
+ `prototype.trimEnd`
支持对字符串使用以下非标准方法：  
+ `prototype.bytesFrom(array | string, encoding)`

  从八位数组或编码字符串创建字节字符串。字符串编码选项为 `hex`、`base64` 和 `base64url`。
+ `prototype.fromBytes(start[, end])`

  从字节字符串创建 Unicode 字符串，其中每个字节都被替换为相应的 Unicode 代码点。
+ `prototype.fromUTF8(start[, end])`

  从 UTF-8 编码的字节字符串创建 Unicode 字符串。如果编码不正确，则会返回 `null`。
+ `prototype.toBytes(start[, end])`

  从 Unicode 字符串创建字节字符串。所有字符都必须在 [0,255] 范围内。如果不在该范围内，它会返回 `null`。
+ `prototype.toUTF8(start[, end])`

  从 Unicode 字符串创建 UTF-8 编码的字节字符串。

**数字**  
支持对数字使用所有 ES 5.1 方法。  
支持对数字使用以下 ES 6 方法：  
+ `isFinite`
+ `isInteger`
+ `isNaN`
+ `isSafeInteger`
+ `parseFloat`
+ `parseInt`
+ `prototype.toExponential`
+ `prototype.toFixed`
+ `prototype.toPrecision`
+ `EPSILON`
+ `MAX_SAFE_INTEGER`
+ `MAX_VALUE`
+ `MIN_SAFE_INTEGER`
+ `MIN_VALUE`
+ `NEGATIVE_INFINITY`
+ `NaN`
+ `POSITIVE_INFINITY`

## 内置对象
<a name="writing-functions-javascript-features-builtin-objects"></a>

支持 ES 的以下内置对象。

**数学**  
支持所有 ES 5.1 数学方法。  
在 CloudFront Functions 运行时环境中，`Math.random()` 实现使用植入有函数运行时间戳的 OpenBSD `arc4random`。
支持以下 ES 6 数学方法：  
+ `acosh`
+ `asinh`
+ `atanh`
+ `cbrt`
+ `clz32`
+ `cosh`
+ `expm1`
+ `fround`
+ `hypot`
+ `imul`
+ `log10`
+ `log1p`
+ `log2`
+ `sign`
+ `sinh`
+ `tanh`
+ `trunc`
+ `E`
+ `LN10`
+ `LN2`
+ `LOG10E`
+ `LOG2E`
+ `PI`
+ `SQRT1_2`
+ `SQRT2`

**日期**  
支持所有 ES 5.1 `Date` 功能。  
出于安全原因，在单个函数运行的生命周期内，`Date` 始终返回相同的值（函数的开始时间）。有关更多信息，请参阅 [受限功能](#writing-functions-javascript-features-restricted-features)。

**函数**  
支持 `apply`、`bind` 和 `call` 方法。  
不支持函数构造函数。

**正则表达式**  
支持所有 ES 5.1 正则表达式功能。正则表达式语言与 Perl 兼容。支持 ES 9 命名的捕获组。

**JSON**  
支持所有 ES 5.1 JSON 功能，包括 `parse` 和 `stringify`。

**数组**  
支持对数组使用以下 ES 5.1 方法：  
+ `isArray`
+ `prototype.concat`
+ `prototype.every`
+ `prototype.filter`
+ `prototype.forEach`
+ `prototype.indexOf`
+ `prototype.join`
+ `prototype.lastIndexOf`
+ `prototype.map`
+ `prototype.pop`
+ `prototype.push`
+ `prototype.reduce`
+ `prototype.reduceRight`
+ `prototype.reverse`
+ `prototype.shift`
+ `prototype.slice`
+ `prototype.some`
+ `prototype.sort`
+ `prototype.splice`
+ `prototype.unshift`
支持对数组使用以下 ES 6 方法：  
+ `of`
+ `prototype.copyWithin`
+ `prototype.fill`
+ `prototype.find`
+ `prototype.findIndex`
支持对数组使用以下 ES 7 方法：  
+ `prototype.includes`

**类型化数组**  
支持对数组使用以下 ES 6 类型化数组：  
+ `Int8Array`
+ `Uint8Array`
+ `Uint8ClampedArray`
+ `Int16Array`
+ `Uint16Array`
+ `Int32Array`
+ `Uint32Array`
+ `Float32Array`
+ `Float64Array`
+ `prototype.copyWithin`
+ `prototype.fill`
+ `prototype.join`
+ `prototype.set`
+ `prototype.slice`
+ `prototype.subarray`
+ `prototype.toString`

**ArrayBuffer**  
支持对 `ArrayBuffer` 使用以下方法：  
+ `prototype.isView`
+ `prototype.slice`

**Promise**  
支持以下承诺方法：  
+ `reject`
+ `resolve`
+ `prototype.catch`
+ `prototype.finally`
+ `prototype.then`

**加密**  
加密模块提供标准哈希和基于哈希的消息身份验证码 (HMAC) 帮助程序。您可以使用 `require('crypto')` 加载模块。该模块公开了以下方法，这些方法的行为与 Node.js 对应方法完全相同：  
+ `createHash(algorithm)`
+ `hash.update(data)`
+ `hash.digest([encoding])`
+ `createHmac(algorithm, secret key)`
+ `hmac.update(data)`
+ `hmac.digest([encoding])`
有关更多信息，请参阅内置模块部分中的 [加密（哈希和 HMAC）](#writing-functions-javascript-features-builtin-modules-crypto)。

**控制台**  
这是调试的帮助对象。它只支持 `log()` 方法来记录日志消息。  
CloudFront Functions 不支持逗号语法，例如 `console.log('a', 'b')`。改为使用 `console.log('a' + ' ' + 'b')` 格式。

## 错误类型
<a name="writing-functions-javascript-features-error-types"></a>

支持以下错误对象：
+ `Error`
+ `EvalError`
+ `InternalError`
+ `MemoryError`
+ `RangeError`
+ `ReferenceError`
+ `SyntaxError`
+ `TypeError`
+ `URIError`

## 全局变量
<a name="writing-functions-javascript-features-globals"></a>

支持 `globalThis` 对象。

支持以下 ES 5.1 全局函数：
+ `decodeURI`
+ `decodeURIComponent`
+ `encodeURI`
+ `encodeURIComponent`
+ `isFinite`
+ `isNaN`
+ `parseFloat`
+ `parseInt`

支持以下全局常数：
+ `NaN`
+ `Infinity`
+ `undefined`

## 内置模块
<a name="writing-functions-javascript-features-builtin-modules"></a>

支持以下内置模块。

**Topics**
+ [

### 加密（哈希和 HMAC）
](#writing-functions-javascript-features-builtin-modules-crypto)
+ [

### 查询字符串
](#writing-functions-javascript-features-builtin-modules-query-string)

### 加密（哈希和 HMAC）
<a name="writing-functions-javascript-features-builtin-modules-crypto"></a>

加密模块 (`crypto`) 提供标准哈希和基于哈希的消息身份验证码 (HMAC) 帮助程序。您可以使用 `require('crypto')` 加载模块。该模块提供了以下方法，这些方法的行为与 Node.js 对应方法完全相同：

**哈希方法**

`crypto.createHash(algorithm)`  
创建并返回哈希对象，您可以使用给定的算法：`md5`、`sha1` 或 `sha256` 通过它生成哈希摘要。

`hash.update(data)`  
用给定的 `data` 更新哈希内容。

`hash.digest([encoding])`  
计算使用 `hash.update()` 传递的所有数据的摘要。编码可以是 `hex`、`base64` 或 `base64url`。

**HMAC 方法**

`crypto.createHmac(algorithm, secret key)`  
创建并返回使用给定的 `algorithm` 和 `secret key` 的 HMAC 对象。算法可以是 `md5`、`sha1` 或 `sha256`。

`hmac.update(data)`  
用给定的 `data` 更新 HMAC 内容。

`hmac.digest([encoding])`  
计算使用 `hmac.update()` 传递的所有数据的摘要。编码可以是 `hex`、`base64` 或 `base64url`。

### 查询字符串
<a name="writing-functions-javascript-features-builtin-modules-query-string"></a>

**注意**  
[CloudFront Functions 事件对象](functions-event-structure.md)自动为您解析 URL 查询字符串。这意味着，在大多数情况下您不需要使用此模块。

查询字符串模块 (`querystring`) 提供了解析和格式化 URL 查询字符串的方法。您可以使用 `require('querystring')` 加载模块。该模块提供了以下方法。

`querystring.escape(string)`  
URL - 对给定的 `string` 进行编码，从而返回转义的查询字符串。该方法由 `querystring.stringify()` 使用，不应直接使用。

`querystring.parse(string[, separator[, equal[, options]]])`  
解析查询字符串 (`string`) 并返回对象。  
`separator` 参数是用于在查询字符串中分隔键和值对的子字符串。默认为 `&`。  
`equal` 参数是用于在查询字符串中分隔键和值的子字符串。默认为 `=`。  
`options` 参数是具有以下键的对象：    
`decodeURIComponent function`  
用于解码查询字符串中百分比编码字符的函数。默认为 `querystring.unescape()`。  
`maxKeys number`  
要解析的最大密钥数。默认为 `1000`。使用 `0` 的值取消键的计数限制。
默认情况下，假定查询字符串中的百分比编码字符使用 UTF-8 编码。无效的 UTF-8 序列将被替换为 `U+FFFD` 替换字符。  
例如，对于以下查询字符串：  

```
'name=value&abc=xyz&abc=123'
```
`querystring.parse()` 的返回值为：  

```
{
name: 'value',
abc: ['xyz', '123']
}
```
`querystring.decode()` 是 `querystring.parse()` 的别名。

`querystring.stringify(object[, separator[, equal[, options]]])`  
序列化 `object` 并返回查询字符串。  
`separator` 参数是用于在查询字符串中分隔键和值对的子字符串。默认为 `&`。  
`equal` 参数是用于在查询字符串中分隔键和值的子字符串。默认为 `=`。  
`options` 参数是具有以下键的对象：    
`encodeURIComponent function`  
用于将 URL 不安全字符转换为查询字符串中的百分比编码的函数。默认为 `querystring.escape()`。
默认情况下，查询字符串中需要百分比编码的字符将编码为 UTF-8。要使用其他编码，请指定 `encodeURIComponent` 选项。  
例如，在下面的代码中：  

```
querystring.stringify({ name: 'value', abc: ['xyz', '123'], anotherName: '' });
```
返回值为：  

```
'name=value&abc=xyz&abc=123&anotherName='
```
`querystring.encode()` 是 `querystring.stringify()` 的别名。

`querystring.unescape(string)`  
对给定的 `string` 中的 URL 百分比编码字符进行解码，以返回未转义的查询字符串。此方法由 `querystring.parse()` 使用，不应直接使用。

## 受限功能
<a name="writing-functions-javascript-features-restricted-features"></a>

由于安全考虑，以下 JavaScript 语言功能或不受支持，或收到限制。

**动态代码评估**  
不支持动态代码评估。如果尝试，`eval()` 和 `Function` 构造函数都会引发错误。例如，`const sum = new Function('a', 'b', 'return a + b')` 引发错误。

**计时器**  
不支持 `setTimeout()`、`setImmediate()` 和 `clearTimeout()` 函数。在函数运行中没有可以推迟或生成的预置。您的函数必须同步运行才能完成。

**日期和时间戳**  
出于安全原因，无法访问高分辨率计时器。在单个函数运行的生命周期内，查询当前时间的所有 `Date` 方法始终返回相同的值。返回的时间戳是函数开始运行的时间。因此，您无法度量函数中的经过时间。

**文件系统访问**  
没有文件系统访问权限。例如，没有像 Node.js 的 `fs` 模块可以进行文件系统访问。

**处理访问权限**  
没有处理访问权限。例如，没有像 Node.js 中那样用于处理信息访问权限的 `process` 全局对象。

**环境变量**  
无法访问环境变量。  
相反，可以使用 CloudFront KeyValueStore 为 CloudFront Functions 创建集中式的键值对数据存储。借助 CloudFront KeyValueStore，无需部署代码更改即对配置数据进行动态更新。必须使用 [JavaScript 运行时 2.0](functions-javascript-runtime-20.md) 才能使用 CloudFront KeyValueStore。有关更多信息，请参阅 [Amazon CloudFront KeyValueStore](kvs-with-functions.md)。

**网络访问**  
不支持网络调用。例如，不支持 XHR、HTTP (S) 和套接字。

# 适用于 CloudFront Functions 的 JavaScript 运行时系统 2.0 特征
<a name="functions-javascript-runtime-20"></a>

CloudFront Functions JavaScript 运行时系统环境符合 [ECMAScript (ES) 5.1](https://262.ecma-international.org/5.1/) 的要求，也支持 ES 版本 6 至 9 的某些特征。它还提供了一些不属于 ES 规范的非标准方法。以下主题列出了此运行时系统中支持的所有特征。

**Topics**
+ [

## 核心功能
](#writing-functions-javascript-features-core-20)
+ [

## 原语对象
](#writing-functions-javascript-features-primitive-objects-20)
+ [

## 内置对象
](#writing-functions-javascript-features-builtin-objects-20)
+ [

## 错误类型
](#writing-functions-javascript-features-error-types-20)
+ [

## 全局变量
](#writing-functions-javascript-features-globals-20)
+ [

## 内置模块
](#writing-functions-javascript-features-builtin-modules-20)
+ [

## 受限功能
](#writing-functions-javascript-features-restricted-features-20)

## 核心功能
<a name="writing-functions-javascript-features-core-20"></a>

支持 ES 的以下核心功能。

**类型**  
支持所有 ES 5.1 类型。其中包括布尔值、数字、字符串、对象、数组、函数和正则表达式。

**运算符**  
支持所有 ES 5.1 运算符。  
支持 ES 7 幂运算符 (`**`)。

**语句**  
支持以下 ES 5.1 语句：  
+ `break`
+ `catch`
+ `continue`
+ `do-while`
+ `else`
+ `finally`
+ `for`
+ `for-in`
+ `if`
+ `label`
+ `return`
+ `switch`
+ `throw`
+ `try`
+ `var`
+ `while`
支持以下 ES 6 语句：  
+ `const`
+ `let`
支持以下 ES 8 语句：  
+ `async`
+ `await`
JavaScript 运行时 2.0 支持 `async`、`await`、`const` 和 `let`。  
`await` 只能在 `async` 函数内部使用。不支持 `async` 参数和闭包。

**文本**  
支持 ES 6 模板文本：多行字符串、表达式插值和嵌套模板。

**函数**  
支持所有的 ES 5.1 函数功能。  
支持 ES 6 箭头函数，并支持 ES 6 rest 参数语法。

**Unicode**  
源文本和字符串文本可以包含 Unicode 编码的字符。还支持由六个字符组成的 Unicode 代码点转义序列（例如，`\uXXXX`）。

**严格模式**  
默认情况下，函数在严格模式下运行，因此您无需在函数代码中添加 `use strict` 语句。无法对其进行更改。

## 原语对象
<a name="writing-functions-javascript-features-primitive-objects-20"></a>

支持 ES 的以下原语对象。

**对象**  
支持对对象使用以下 ES 5.1 方法：  
+ `Object.create()`（没有属性列表）
+ `Object.defineProperties()`
+ `Object.defineProperty()`
+ `Object.freeze()`
+ `Object.getOwnPropertyDescriptor()`
+ `Object.getOwnPropertyDescriptors()`
+ `Object.getOwnPropertyNames()`
+ `Object.getPrototypeOf()`
+ `Object.isExtensible()`
+ `Object.isFrozen()`
+ `Object.isSealed()`
+ `Object.keys()`
+ `Object.preventExtensions()`
+ `Object.seal()`
支持对对象使用以下 ES 6 方法：  
+ `Object.assign()`
支持对对象使用以下 ES 8 方法：  
+ `Object.entries()`
+ `Object.values()`
支持对对象使用以下 ES 5.1 原型方法：  
+ `Object.prototype.hasOwnProperty()`
+ `Object.prototype.isPrototypeOf()`
+ `Object.prototype.propertyIsEnumerable()`
+ `Object.prototype.toString()`
+ `Object.prototype.valueOf()`
支持对对象使用以下 ES 6 原型方法：  
+ `Object.prototype.is()`
+ `Object.prototype.setPrototypeOf()`

**字符串**  
支持对字符串使用以下 ES 5.1 方法：  
+ `String.fromCharCode()`
支持对字符串使用以下 ES 6 方法：  
+ `String.fromCodePoint()`
支持对字符串使用以下 ES 5.1 原型方法：  
+ `String.prototype.charAt()`
+ `String.prototype.concat()`
+ `String.prototype.indexOf()`
+ `String.prototype.lastIndexOf()`
+ `String.prototype.match()`
+ `String.prototype.replace()`
+ `String.prototype.search()`
+ `String.prototype.slice()`
+ `String.prototype.split()`
+ `String.prototype.substr()`
+ `String.prototype.substring()`
+ `String.prototype.toLowerCase()`
+ `String.prototype.trim()`
+ `String.prototype.toUpperCase()`
支持对字符串使用以下 ES 6 原型方法：  
+ `String.prototype.codePointAt()`
+ `String.prototype.endsWith()`
+ `String.prototype.includes()`
+ `String.prototype.repeat()`
+ `String.prototype.startsWith()`
支持对字符串使用以下 ES 8 原型方法：  
+ `String.prototype.padStart()`
+ `String.prototype.padEnd()`
支持对字符串使用以下 ES 9 原型方法：  
+ `String.prototype.trimStart()`
+ `String.prototype.trimEnd()`
支持对字符串使用以下 ES 12 原型方法：  
+ `String.prototype.replaceAll()`
**注意**  
`String.prototype.replaceAll()` 是 JavaScript 运行时系统 2.0 中的新功能。

**数字**  
支持所有 ES 5 数字。  
支持对数字使用以下 ES 6 属性：  
+ `Number.EPSILON`
+ `Number.MAX_SAFE_INTEGER`
+ `Number.MIN_SAFE_INTEGER`
+ `Number.MAX_VALUE`
+ `Number.MIN_VALUE`
+ `Number.NaN`
+ `Number.NEGATIVE_INFINITY`
+ `Number.POSITIVE_INFINITY`
支持对数字使用以下 ES 6 方法：  
+ `Number.isFinite()`
+ `Number.isInteger()`
+ `Number.isNaN()`
+ `Number.isSafeInteger()`
+ `Number.parseInt()`
+ `Number.parseFloat()`
支持对数字使用以下 ES 5.1 原型方法：  
+ `Number.prototype.toExponential()`
+ `Number.prototype.toFixed()`
+ `Number.prototype.toPrecision()`
支持 ES 12 数字分隔符。  
ES 12 数字分隔符是 JavaScript 运行时系统 2.0 中的新功能。

## 内置对象
<a name="writing-functions-javascript-features-builtin-objects-20"></a>

支持 ES 的以下内置对象。

**数学**  
支持所有 ES 5.1 数学方法。  
在 CloudFront Functions 运行时环境中，`Math.random()` 实现使用植入有函数运行时间戳的 OpenBSD `arc4random`。
支持以下 ES 6 数学属性：  
+ `Math.E`
+ `Math.LN10`
+ `Math.LN2`
+ `Math.LOG10E`
+ `Math.LOG2E`
+ `Math.PI`
+ `Math.SQRT1_2`
+ `Math.SQRT2`
支持以下 ES 6 数学方法：  
+ `Math.abs()`
+ `Math.acos()`
+ `Math.acosh()`
+ `Math.asin()`
+ `Math.asinh()`
+ `Math.atan()`
+ `Math.atan2()`
+ `Math.atanh()`
+ `Math.cbrt()`
+ `Math.ceil()`
+ `Math.clz32()`
+ `Math.cos()`
+ `Math.cosh()`
+ `Math.exp()`
+ `Math.expm1()`
+ `Math.floor()`
+ `Math.fround()`
+ `Math.hypot()`
+ `Math.imul()`
+ `Math.log()`
+ `Math.log1p()`
+ `Math.log2()`
+ `Math.log10()`
+ `Math.max()`
+ `Math.min()`
+ `Math.pow()`
+ `Math.random()`
+ `Math.round()`
+ `Math.sign()`
+ `Math.sinh()`
+ `Math.sin()`
+ `Math.sqrt()`
+ `Math.tan()`
+ `Math.tanh()`
+ `Math.trunc()`

**日期**  
支持所有 ES 5.1 `Date` 功能。  
出于安全原因，在单个函数运行的生命周期内，`Date` 始终返回相同的值（函数的开始时间）。有关更多信息，请参阅 [受限功能](functions-javascript-runtime-10.md#writing-functions-javascript-features-restricted-features)。

**函数**  
支持以下 ES 5.1 原型方法：  
+ `Function.prototype.apply()`
+ `Function.prototype.bind()`
+ `Function.prototype.call()`
不支持函数构造函数。

**正则表达式**  
支持所有 ES 5.1 正则表达式功能。正则表达式语言与 Perl 兼容。  
支持以下 ES 5.1 原型存取器属性：  
+ `RegExp.prototype.global`
+ `RegExp.prototype.ignoreCase`
+ `RegExp.protoype.multiline`
+ `RegExp.protoype.source`
+ `RegExp.prototype.sticky`
+ `RegExp.prototype.flags`
**注意**  
`RegExp.prototype.sticky` 和 `RegExp.prototype.flags` 是 JavaScript 运行时系统 2.0 中的新功能。
支持以下 ES 5.1 原型方法：  
+ `RegExp.prototype.exec()`
+ `RegExp.prototype.test()`
+ `RegExp.prototype.toString()`
+ `RegExp.prototype[@@replace]()`
+ `RegExp.prototype[@@split]()`
**注意**  
`RegExp.prototype[@@split]()` 是 JavaScript 运行时系统 2.0 中的新功能。
支持以下 ES 5.1 实例属性：  
+ `lastIndex`
支持 ES 9 命名的捕获组。

**JSON**  
支持以下 ES 5.1 方法：  
+ `JSON.parse()`
+ `JSON.stringify()`

**数组**  
支持对数组使用以下 ES 5.1 方法：  
+ `Array.isArray()`
支持对数组使用以下 ES 6 方法：  
+ `Array.of()`
支持以下 ES 5.1 原型方法：  
+ `Array.prototype.concat()`
+ `Array.prototype.every()`
+ `Array.prototype.filter()`
+ `Array.prototype.forEach()`
+ `Array.prototype.indexOf()`
+ `Array.prototype.join()`
+ `Array.prototype.lastIndexOf()`
+ `Array.prototype.map()`
+ `Array.prototype.pop()`
+ `Array.prototype.push()`
+ `Array.prototype.reduce()`
+ `Array.prototype.reduceRight()`
+ `Array.prototype.reverse()`
+ `Array.prototype.shift()`
+ `Array.prototype.slice()`
+ `Array.prototype.some()`
+ `Array.prototype.sort()`
+ `Array.prototype.splice()`
+ `Array.prototype.unshift()`
支持以下 ES 6 原型方法  
+ `Array.prototype.copyWithin()`
+ `Array.prototype.fill()`
+ `Array.prototype.find()`
+ `Array.prototype.findIndex()`
支持以下 ES 7 原型方法：  
+ `Array.prototype.includes()`

**类型化数组**  
支持以下 ES 6 类型化数组构造函数：  
+ `Float32Array`
+ `Float64Array`
+ `Int8Array`
+ `Int16Array`
+ `Int32Array`
+ `Uint8Array`
+ `Uint8ClampedArray`
+ `Uint16Array`
+ `Uint32Array`
支持以下 ES 6 方法：  
+ `TypedArray.from()`
+ `TypedArray.of()`
**注意**  
`TypedArray.from()` 和 `TypedArray.of()` 是 JavaScript 运行时系统 2.0 中的新功能。
支持以下 ES 6 原型方法：  
+ `TypedArray.prototype.copyWithin()`
+ `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()`
+ `TypedArray.prototype.some()`
+ `TypedArray.prototype.set()`
+ `TypedArray.prototype.slice()`
+ `TypedArray.prototype.sort()`
+ `TypedArray.prototype.subarray()`
+ `TypedArray.prototype.toString()`
**注意**  
`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()` 和 `TypedArray.prototype.some()` 是 JavaScript 运行时系统 2.0 中的新功能。

**ArrayBuffer**  
支持对 ArrayBuffer 使用以下 ES 6 方法：  
+ `isView()`
支持对 ArrayBuffer 使用以下 ES 6 原型方法：  
+ `ArrayBuffer.prototype.slice()`

**Promise**  
支持对 Promise 使用 ES 6 方法：  
+ `Promise.all()`
+ `Promise.allSettled()`
+ `Promise.any()`
+ `Promise.reject()`
+ `Promise.resolve()`
+ `Promise.race()`
**注意**  
`Promise.all()`、`Promise.allSettled()`、`Promise.any()` 和 `Promise.race()` 是 JavaScript 运行时系统 2.0 中的新功能。
支持对 Promise 使用 ES 6 原型方法：  
+ `Promise.prototype.catch()`
+ `Promise.prototype.finally()`
+ `Promise.prototype.then()`

**DataView**  
支持以下 ES 6 原型方法：  
+ `DataView.prototype.getFloat32()`
+ `DataView.prototype.getFloat64()`
+ `DataView.prototype.getInt16()`
+ `DataView.prototype.getInt32()`
+ `DataView.prototype.getInt8()`
+ `DataView.prototype.getUint16()`
+ `DataView.prototype.getUint32()`
+ `DataView.prototype.getUint8()`
+ `DataView.prototype.setFloat32()`
+ `DataView.prototype.setFloat64()`
+ `DataView.prototype.setInt16()`
+ `DataView.prototype.setInt32()`
+ `DataView.prototype.setInt8()`
+ `DataView.prototype.setUint16()`
+ `DataView.prototype.setUint32()`
+ `DataView.prototype.setUint8()`
**注意**  
所有 Dataview ES 6 原型方法都是 JavaScript 运行时系统 2.0 中的新功能。

**符号**  
支持以下 ES 6 方法：  
+ `Symbol.for()`
+ `Symbol.keyfor()`
**注意**  
所有符号 ES 6 方法都是 JavaScript 运行时系统 2.0 中的新功能。

**文本解码器**  
支持以下原型方法：  
+ `TextDecoder.prototype.decode()`
支持以下原型存取器属性：  
+ `TextDecoder.prototype.encoding`
+ `TextDecoder.prototype.fatal`
+ `TextDecoder.prototype.ignoreBOM`

**文本编码器**  
支持以下原型方法：  
+ `TextEncoder.prototype.encode()`
+ `TextEncoder.prototype.encodeInto()`

## 错误类型
<a name="writing-functions-javascript-features-error-types-20"></a>

支持以下错误对象：
+ `Error`
+ `EvalError`
+ `InternalError`
+ `RangeError`
+ `ReferenceError`
+ `SyntaxError`
+ `TypeError`
+ `URIError`

## 全局变量
<a name="writing-functions-javascript-features-globals-20"></a>

支持 `globalThis` 对象。

支持以下 ES 5.1 全局函数：
+ `decodeURI()`
+ `decodeURIComponent()`
+ `encodeURI()`
+ `encodeURIComponent()`
+ `isFinite()`
+ `isNaN()`
+ `parseFloat()`
+ `parseInt()`

支持以下 ES 6 全局函数：
+ `atob()`
+ `btoa()`
**注意**  
`atob()` 和 `btoa()` 是 JavaScript 运行时系统 2.0 中的新功能。

支持以下全局常数：
+ `NaN`
+ `Infinity`
+ `undefined`
+ `arguments`

## 内置模块
<a name="writing-functions-javascript-features-builtin-modules-20"></a>

支持以下内置模块。

**Topics**
+ [

### Buffer
](#writing-functions-javascript-features-builtin-modules-buffer-20)
+ [

### 查询字符串
](#writing-functions-javascript-features-builtin-modules-query-string-20)
+ [

### 加密
](#writing-functions-javascript-features-builtin-modules-crypto-20)

### Buffer
<a name="writing-functions-javascript-features-builtin-modules-buffer-20"></a>

该模块提供了以下方法：
+ `Buffer.alloc(size[, fill[, encoding]])`

  分配 `Buffer`。
  + `size`：缓冲区大小。输入一个整数。
  + `fill`：可选。输入字符串、`Buffer`、Uint8Array 或整数。默认值为 `0`。
  + `encoding`：可选。如果 `fill` 是字符串，请输入以下值之一：`utf8`、`hex`、`base64`、`base64url`。默认值为 `utf8`。
+ `Buffer.allocUnsafe(size)`

  分配一个未初始化的 `Buffer`。
  + `size`：输入一个整数。
+ `Buffer.byteLength(value[, encoding])`

  返回值的长度，以字节为单位。
  + `value`：字符串、`Buffer`、TypedArray、Dataview 或 Arraybuffer。
  + `encoding`：可选。如果 `value` 是字符串，请输入以下值之一：`utf8`、`hex`、`base64`、`base64url`。默认值为 `utf8`。
+ `Buffer.compare(buffer1, buffer2)`

  比较两个 `Buffer` 以帮助对数组进行排序。如果它们相同，则返回 `0`，如果 `buffer1` 排在第一位，则返回 `-1`，如果 `buffer2` 排在第一位，则返回 `1`。
  + `buffer1`：输入一个 `Buffer`。
  + `buffer2`：输入不同的 `Buffer`。
+ `Buffer.concat(list[, totalLength])`

  连接多个 `Buffer`。如果为无，则返回 `0`。最多可返回 `totalLength`。
  + `list`：输入 `Buffer` 的列表。请注意，这将被截断为 `totalLength`。
  + `totalLength`：可选。输入一个无符号整数。如果为空，则使用列表中的 `Buffer` 实例总和。
+ `Buffer.from(array)`

  从数组中创建 `Buffer`。
  + `array`：输入从 `0` 到 `255` 的字节数组。
+ `Buffer.from(arrayBuffer, byteOffset[, length]))`

  从 `arrayBuffer` 中创建视图，从偏移量 `byteOffset` 开始，长度为 `length`。
  + `arrayBuffer`：输入 `Buffer` 数组。
  + `byteOffset`：输入一个整数。
  + `length`：可选。输入一个整数。
+ `Buffer.from(buffer)`

  创建 `Buffer` 的副本。
  + `buffer`：输入一个 `Buffer`。
+ `Buffer.from(object[, offsetOrEncoding[, length]])`

  从对象创建 `Buffer`。如果 `valueOf()` 不等于对象，则返回 `Buffer.from(object.valueOf(), offsetOrEncoding, length)`。
  + `object`：输入一个对象。
  + `offsetOrEncoding`：可选。输入整数或编码字符串。
  + `length`：可选。输入一个整数。
+ `Buffer.from(string[, encoding])`

  从字符串创建 `Buffer`。
  + `string`：输入一个字符串。
  + `encoding`：可选。输入以下值之一：`utf8`、`hex`、`base64`、`base64url`。默认值为 `utf8`。
+ `Buffer.isBuffer(object)`

  检查 `object` 是否为缓冲区。返回 `true` 或 `false`。
  + `object`：输入一个对象。
+ `Buffer.isEncoding(encoding)`

  检查 `encoding` 是否受支持。返回 `true` 或 `false`。
  + `encoding`：可选。输入以下值之一：`utf8`、`hex`、`base64`、`base64url`。默认值为 `utf8`。

该模块提供以下缓冲区原型方法：
+ `Buffer.prototype.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])`

  将 `Buffer` 与目标进行比较。如果它们相同，则返回 `0`，如果 `buffer` 排在第一位，则返回 `1`，如果 `target` 排在第一位，则返回 `-1`。
  + `target`：输入一个 `Buffer`。
  + `targetStart`：可选。输入一个整数。默认值为 0。
  + `targetEnd`：可选。输入一个整数。默认为 `target` 长度。
  + `sourceStart`：可选。输入一个整数。默认值为 0。
  + `sourceEnd`：可选。输入一个整数。默认为 `Buffer` 长度。
+ `Buffer.prototype.copy(target[, targetStart[, sourceStart[, sourceEnd]]])`

  将缓冲区复制到 `target`。
  + `target`：输入 `Buffer` 或 `Uint8Array`。
  + `targetStart`：可选。输入一个整数。默认值为 0。
  + `sourceStart`：可选。输入一个整数。默认值为 0。
  + `sourceEnd`：可选。输入一个整数。默认为 `Buffer` 长度。
+ `Buffer.prototype.equals(otherBuffer)`

  将 `Buffer` 与 `otherBuffer` 进行比较。返回 `true` 或 `false`。
  + `otherBuffer`：输入一个字符串。
+ `Buffer.prototype.fill(value[, offset[, end][, encoding])`

  使用 `value` 填充 `Buffer`。
  + `value`：输入字符串、`Buffer` 或整数。
  + `offset`：可选。输入一个整数。
  + `end`：可选。输入一个整数。
  + `encoding`：可选。输入以下值之一：`utf8`、`hex`、`base64`、`base64url`。默认值为 `utf8`。
+ `Buffer.prototype.includes(value[, byteOffset][, encoding])`

  在 `Buffer` 中搜索 `value`。返回 `true` 或 `false`。
  + `value`：输入字符串、`Buffer`、`Uint8Array` 或整数。
  + `byteOffset`：可选。输入一个整数。
  + `encoding`：可选。输入以下值之一：`utf8`、`hex`、`base64`、`base64url`。默认值为 `utf8`。
+ `Buffer.prototype.indexOf(value[, byteOffset][, encoding])`

  在 `Buffer` 中搜索第一个 `value`。如果找到，则返回 `index`；如果找不到，则返回 `-1`。
  + `value`：输入一个字符串、`Buffer`、Unit8Array 或 0 到 255 之间的整数。
  + `byteOffset`：可选。输入一个整数。
  + `encoding`：可选。如果 `value` 是字符串，则输入以下值之一：`utf8`、`hex`、`base64`、`base64url`。默认值为 `utf8`。
+ `Buffer.prototype.lastIndexOf(value[, byteOffset][, encoding])`

  在 `Buffer` 中搜索最后一个 `value`。如果找到，则返回 `index`；如果找不到，则返回 `-1`。
  + `value`：输入一个字符串、`Buffer`、Unit8Array 或 0 到 255 之间的整数。
  + `byteOffset`：可选。输入一个整数。
  + `encoding`：可选。如果 `value` 是字符串，则输入以下值之一：`utf8`、`hex`、`base64`、`base64url`。默认值为 `utf8`。
+ `Buffer.prototype.readInt8(offset)`

  从 `Buffer` 中按 `offset` 偏移量读取 `Int8`。
  + `offset`：输入一个整数。
+ `Buffer.prototype.readIntBE(offset, byteLength)`

  从 `Buffer` 中按 `offset` 偏移量以大端序形式读取 `Int`。
  + `offset`：输入一个整数。
  + `byteLength`：可选。输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.readInt16BE(offset)`

  从 `Buffer` 中按 `offset` 偏移量以大端序形式读取 `Int16`。
  + `offset`：输入一个整数。
+ `Buffer.prototype.readInt32BE(offset)`

  从 `Buffer` 中按 `offset` 偏移量以大端序形式读取 `Int32`。
  + `offset`：输入一个整数。
+ `Buffer.prototype.readIntLE(offset, byteLength)`

  从 `Buffer` 中按 `offset` 偏移量以小端序形式读取 `Int`。
  + `offset`：输入一个整数。
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.readInt16LE(offset)`

  从 `Buffer` 中按 `offset` 偏移量以小端序形式读取 `Int16`。
  + `offset`：输入一个整数。
+ `Buffer.prototype.readInt32LE(offset)`

  从 `Buffer` 中按 `offset` 偏移量以小端序形式读取 `Int32`。
  + `offset`：输入一个整数。
+ `Buffer.prototype.readUInt8(offset)`

  从 `Buffer` 中按 `offset` 偏移量读取 `UInt8`。
  + `offset`：输入一个整数。
+ `Buffer.prototype.readUIntBE(offset, byteLength)`

  从 `Buffer` 中按 `offset` 偏移量以大端序形式读取 `UInt`。
  + `offset`：输入一个整数。
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.readUInt16BE(offset)`

  从 `Buffer` 中按 `offset` 偏移量以大端序形式读取 `UInt16`。
+ 
  + `offset`：输入一个整数。
+ `Buffer.prototype.readUInt32BE(offset)`

  从 `Buffer` 中按 `offset` 偏移量以大端序形式读取 `UInt32`。
  + `offset`：输入一个整数。
+ `Buffer.prototype.readUIntLE(offset, byteLength)`

  从 `Buffer` 中按 `offset` 偏移量以小端序形式读取 `UInt`。
  + `offset`：输入一个整数。
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.readUInt16LE(offset)`

  从 `Buffer` 中按 `offset` 偏移量以小端序形式读取 `UInt16`。
  + `offset`：输入一个整数。
+ `Buffer.prototype.readUInt32LE(offset)`

  从 `Buffer` 中按 `offset` 偏移量以小端序形式读取 `UInt32`。
  + `offset`：输入一个整数。
+ `Buffer.prototype.readDoubleBE([offset])`

  从 `Buffer` 中按 `offset` 偏移量以大端序形式读取 64 位双精度浮点数。
  + `offset`：可选。输入一个整数。
+ `Buffer.prototype.readDoubleLE([offset])`

  从 `Buffer` 中按 `offset` 偏移量以小端序形式读取 64 位双精度浮点数。
  + `offset`：可选。输入一个整数。
+ `Buffer.prototype.readFloatBE([offset])`

  从 `Buffer` 中按 `offset` 偏移量以大端序形式读取 32 位浮点数。
  + `offset`：可选。输入一个整数。
+ `Buffer.prototype.readFloatLE([offset])`

  从 `Buffer` 中按 `offset` 偏移量以小端序形式读取 32 位浮点数。
  + `offset`：可选。输入一个整数。
+ `Buffer.prototype.subarray([start[, end]])`

  返回 `Buffer` 的一个副本，该副本经过偏移并用新的 `start` 和 `end` 进行裁剪。
  + `start`：可选。输入一个整数。默认值为 0。
  + `end`：可选。输入一个整数。默认为缓冲区长度。
+ `Buffer.prototype.swap16()`

  交换 `Buffer` 数组字节顺序，将其视为一个 16 位数字的数组。`Buffer` 长度必须可以被 2 整除，否则您将收到错误。
+ `Buffer.prototype.swap32()`

  交换 `Buffer` 数组字节顺序，将其视为一个 32 位数字的数组。`Buffer` 长度必须可以被 4 整除，否则您将收到错误。
+ `Buffer.prototype.swap64()`

  交换 `Buffer` 数组字节顺序，将其视为一个 64 位数字的数组。`Buffer` 长度必须可以被 8 整除，否则您将收到错误。
+ `Buffer.prototype.toJSON()`

  以 JSON 格式返回 `Buffer`。
+ `Buffer.prototype.toString([encoding[, start[, end]]])`

  将 `Buffer` 从 `start` 到 `end` 转换为编码字符串。
  + `encoding`：可选。输入以下值之一：`utf8`、`hex`、`base64` 或 `base64url`。默认值为 `utf8`。
  + `start`：可选。输入一个整数。默认值为 0。
  + `end`：可选。输入一个整数。默认为缓冲区长度。
+ `Buffer.prototype.write(string[, offset[, length]][, encoding])`

  如果有空格，则将编码的 `string` 写入 `Buffer`，如果空间不足，则写入截断的 `string`。
  + `string`：输入一个字符串。
  + `offset`：可选。输入一个整数。默认值为 0。
  + `length`：可选。输入一个整数。默认为字符串的长度。
  + `encoding`：可选。（可选）输入以下值之一：`utf8`、`hex`、`base64` 或 `base64url`。默认值为 `utf8`。
+ `Buffer.prototype.writeInt8(value, offset, byteLength)`

  按 `offset` 偏移量将 `byteLength` 的 `Int8` `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：输入一个整数
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeIntBE(value, offset, byteLength)`

  使用大端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：输入一个整数
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeInt16BE(value, offset, byteLength)`

  使用大端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：输入一个整数
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeInt32BE(value, offset, byteLength)`

  使用大端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：输入一个整数
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeIntLE(offset, byteLength)`

  使用小端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `offset`：输入一个整数。
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeInt16LE(offset, byteLength)`

  使用小端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `offset`：输入一个整数。
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeInt32LE(offset, byteLength)`

  使用小端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `offset`：输入一个整数。
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeUInt8(value, offset, byteLength)`

  按 `offset` 偏移量将 `byteLength` 的 `UInt8` `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：输入一个整数
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeUIntBE(value, offset, byteLength)`

  使用大端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：输入一个整数
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeUInt16BE(value, offset, byteLength)`

  使用大端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：输入一个整数
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeUInt32BE(value, offset, byteLength)`

  使用大端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：输入一个整数
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeUIntLE(value, offset, byteLength)`

  使用小端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：输入一个整数
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeUInt16LE(value, offset, byteLength)`

  使用小端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：输入一个整数
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeUInt32LE(value, offset, byteLength)`

  使用小端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：输入一个整数
  + `byteLength`：输入一个从 `1` 到 `6` 的整数。
+ `Buffer.prototype.writeDoubleBE(value, [offset])`

  使用大端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：可选。输入一个整数。默认值为 0。
+ `Buffer.prototype.writeDoubleLE(value, [offset])`

  使用小端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：可选。输入一个整数。默认值为 0。
+ `Buffer.prototype.writeFloatBE(value, [offset])`

  使用大端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：可选。输入一个整数。默认值为 0。
+ `Buffer.prototype.writeFloatLE(value, [offset])`

  使用小端序按 `offset` 偏移量将 `value` 写入 `Buffer`。
  + `value`：输入一个整数。
  + `offset`：可选。输入一个整数。默认值为 0。

支持以下实例方法：
+ `buffer[index]`

  在 `Buffer` 中的 `index` 处获取和设置八位字节（字节）。
  + 获取一个从 `0` 到 `255` 的数字。或者设置一个从 `0` 到 `255` 的数字。

支持以下实例属性：
+ `buffer`

  获取缓冲区的 `ArrayBuffer` 对象。
+ `byteOffset`

  获取缓冲区的 `Arraybuffer` 对象的 `byteOffset`。
+ `length`

  获取缓冲区字节计数。

**注意**  
所有缓冲区模块方法都是 JavaScript 运行时系统 2.0 中的新功能。

### 查询字符串
<a name="writing-functions-javascript-features-builtin-modules-query-string-20"></a>

**注意**  
[CloudFront Functions 事件对象](functions-event-structure.md)自动为您解析 URL 查询字符串。这意味着，在大多数情况下您不需要使用此模块。

查询字符串模块 (`querystring`) 提供了解析和格式化 URL 查询字符串的方法。您可以使用 `require('querystring')` 加载模块。该模块提供了以下方法。

`querystring.escape(string)`  
URL - 对给定的 `string` 进行编码，从而返回转义的查询字符串。该方法由 `querystring.stringify()` 使用，不应直接使用。

`querystring.parse(string[, separator[, equal[, options]]])`  
解析查询字符串 (`string`) 并返回对象。  
`separator` 参数是用于在查询字符串中分隔键和值对的子字符串。默认为 `&`。  
`equal` 参数是用于在查询字符串中分隔键和值的子字符串。默认为 `=`。  
`options` 参数是具有以下键的对象：    
`decodeURIComponent function`  
用于解码查询字符串中百分比编码字符的函数。默认为 `querystring.unescape()`。  
`maxKeys number`  
要解析的最大密钥数。默认为 `1000`。使用 `0` 的值取消键的计数限制。
默认情况下，假定查询字符串中的百分比编码字符使用 UTF-8 编码。无效的 UTF-8 序列将被替换为 `U+FFFD` 替换字符。  
例如，对于以下查询字符串：  

```
'name=value&abc=xyz&abc=123'
```
`querystring.parse()` 的返回值为：  

```
{
name: 'value',
abc: ['xyz', '123']
}
```
`querystring.decode()` 是 `querystring.parse()` 的别名。

`querystring.stringify(object[, separator[, equal[, options]]])`  
序列化 `object` 并返回查询字符串。  
`separator` 参数是用于在查询字符串中分隔键和值对的子字符串。默认为 `&`。  
`equal` 参数是用于在查询字符串中分隔键和值的子字符串。默认为 `=`。  
`options` 参数是具有以下键的对象：    
`encodeURIComponent function`  
用于将 URL 不安全字符转换为查询字符串中的百分比编码的函数。默认为 `querystring.escape()`。
默认情况下，查询字符串中需要百分比编码的字符将编码为 UTF-8。要使用其他编码，请指定 `encodeURIComponent` 选项。  
例如，在下面的代码中：  

```
querystring.stringify({ name: 'value', abc: ['xyz', '123'], anotherName: '' });
```
返回值为：  

```
'name=value&abc=xyz&abc=123&anotherName='
```
`querystring.encode()` 是 `querystring.stringify()` 的别名。

`querystring.unescape(string)`  
对给定的 `string` 中的 URL 百分比编码字符进行解码，以返回未转义的查询字符串。此方法由 `querystring.parse()` 使用，不应直接使用。

### 加密
<a name="writing-functions-javascript-features-builtin-modules-crypto-20"></a>

加密模块 (`crypto`) 提供标准哈希和基于哈希的消息身份验证码 (HMAC) 帮助程序。您可以使用 `require('crypto')` 加载模块。

**哈希方法**

`crypto.createHash(algorithm)`  
创建并返回哈希对象，您可以使用给定的算法：`md5`、`sha1` 或 `sha256` 通过它生成哈希摘要。

`hash.update(data)`  
用给定的 `data` 更新哈希内容。

`hash.digest([encoding])`  
计算使用 `hash.update()` 传递的所有数据的摘要。编码可以是 `hex`、`base64` 或 `base64url`。

**HMAC 方法**

`crypto.createHmac(algorithm, secret key)`  
创建并返回使用给定的 `algorithm` 和 `secret key` 的 HMAC 对象。算法可以是 `md5`、`sha1` 或 `sha256`。

`hmac.update(data)`  
用给定的 `data` 更新 HMAC 内容。

`hmac.digest([encoding])`  
计算使用 `hmac.update()` 传递的所有数据的摘要。编码可以是 `hex`、`base64` 或 `base64url`。

## 受限功能
<a name="writing-functions-javascript-features-restricted-features-20"></a>

由于安全考虑，以下 JavaScript 语言功能或不受支持，或收到限制。

**动态代码评估**  
不支持动态代码评估。如果尝试，`eval()` 和 `Function` 构造函数都会引发错误。例如，`const sum = new Function('a', 'b', 'return a + b')` 引发错误。

**计时器**  
不支持 `setTimeout()`、`setImmediate()` 和 `clearTimeout()` 函数。在函数运行中没有可以推迟或生成的预置。您的函数必须同步运行才能完成。

**日期和时间戳**  
出于安全原因，无法访问高分辨率计时器。在单个函数运行的生命周期内，查询当前时间的所有 `Date` 方法始终返回相同的值。返回的时间戳是函数开始运行的时间。因此，您无法度量函数中的经过时间。

**文件系统访问**  
没有文件系统访问权限。例如，没有像 Node.js 的 `fs` 模块可以进行文件系统访问。

**处理访问权限**  
没有处理访问权限。例如，没有像 Node.js 中那样用于处理信息访问权限的 `process` 全局对象。

**环境变量**  
无法访问环境变量。相反，可以使用 CloudFront KeyValueStore 为 CloudFront Functions 创建集中式的键值对数据存储。借助 CloudFront KeyValueStore，无需部署代码更改即对配置数据进行动态更新。有关更多信息，请参阅 [Amazon CloudFront KeyValueStore](kvs-with-functions.md)。

**网络访问**  
不支持网络调用。例如，不支持 XHR、HTTP (S) 和套接字。