

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 適用於 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 到 12 的一些功能。

我們建議您使用 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 剩餘參數語法。

**Unicode**  
來源文字和字串常值可以包含 Unicode 編碼的字元。也支援由六個字元 (例如 `\uXXXX`) 組成的 Unicode 字碼指標逸出序列。

**嚴格模式**  
函數按預設會在嚴格模式下運作，因此您不需要在函數程式碼中新增 `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`。

**Array**  
支援陣列上的以下 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` 上的以下方法：  
+ `prototype.isView`
+ `prototype.slice`

**Promise**  
支援 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` 模組。

**處理存取**  
沒有處理存取。例如沒有 `process` 全域物件可像 Node.js 一樣處理資訊存取。

**環境變數**  
無法存取環境變數。  
您可以改為使用 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 到 12 的一些功能。它還提供了一些不屬於 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`
`async`、`await`、`const` 和 `let` 是 JavaScript 執行時期 2.0 支援的功能。  
`await` 只能在 `async` 函數內使用，不支援 `async` 引數和關閉。

**文字**  
支援 ES 6 範本文字：多行字串、表達式插補和巢狀範本。

**函數**  
支援所有 ES 5.1 函數功能。  
支援 ES 6 箭頭函數，支援 ES 6 剩餘參數語法。

**Unicode**  
來源文字和字串常值可以包含 Unicode 編碼的字元。也支援由六個字元 (例如 `\uXXXX`) 組成的 Unicode 字碼指標逸出序列。

**嚴格模式**  
函數按預設會在嚴格模式下運作，因此您不需要在函數程式碼中新增 `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()`

**String**  
支援以下針對字串的 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 的新功能。

**Number**  
支援所有 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 的新功能。

**Symbol**  
支援以下 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()`

**主控台**  
這是一個用於偵錯的協助程式物件。它僅支援 `log()` 方法以記錄日誌訊息。  
CloudFront Functions 不支援逗號語法，例如 `console.log('a', 'b')`。因此請改為使用 `console.log('a' + ' ' + 'b')` 格式。

## 錯誤類型
<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**
+ [緩衝區](#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)

### 緩衝區
<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)`

  將 `Int8` `value` (長度為 `byteLength`) 寫入 `Buffer` 的偏移值 `offset`。
  + `value`：輸入整數。
  + `offset`：輸入整數
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeIntBE(value, offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用大端序。
  + `value`：輸入整數。
  + `offset`：輸入整數
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeInt16BE(value, offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用大端序。
  + `value`：輸入整數。
  + `offset`：輸入整數
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeInt32BE(value, offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用大端序。
  + `value`：輸入整數。
  + `offset`：輸入整數
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeIntLE(offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用小端序。
  + `offset`：輸入整數。
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeInt16LE(offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用小端序。
  + `offset`：輸入整數。
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeInt32LE(offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用小端序。
  + `offset`：輸入整數。
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeUInt8(value, offset, byteLength)`

  將 `UInt8` `value` (長度為 `byteLength`) 寫入 `Buffer` 的偏移值 `offset`。
  + `value`：輸入整數。
  + `offset`：輸入整數
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeUIntBE(value, offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用大端序。
  + `value`：輸入整數。
  + `offset`：輸入整數
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeUInt16BE(value, offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用大端序。
  + `value`：輸入整數。
  + `offset`：輸入整數
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeUInt32BE(value, offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用大端序。
  + `value`：輸入整數。
  + `offset`：輸入整數
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeUIntLE(value, offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用小端序。
  + `value`：輸入整數。
  + `offset`：輸入整數
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeUInt16LE(value, offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用小端序。
  + `value`：輸入整數。
  + `offset`：輸入整數
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeUInt32LE(value, offset, byteLength)`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用小端序。
  + `value`：輸入整數。
  + `offset`：輸入整數
  + `byteLength`：輸入從 `1` 到 `6` 的整數。
+ `Buffer.prototype.writeDoubleBE(value, [offset])`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用大端序。
  + `value`：輸入整數。
  + `offset`：選用。輸入整數。預設值為 0。
+ `Buffer.prototype.writeDoubleLE(value, [offset])`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用小端序。
  + `value`：輸入整數。
  + `offset`：選用。輸入整數。預設值為 0。
+ `Buffer.prototype.writeFloatBE(value, [offset])`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用大端序。
  + `value`：輸入整數。
  + `offset`：選用。輸入整數。預設值為 0。
+ `Buffer.prototype.writeFloatLE(value, [offset])`

  將 `value` 寫入 `Buffer` 的偏移值 `offset`，使用小端序。
  + `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` 模組。

**處理存取**  
沒有處理存取。例如沒有 `process` 全域物件可像 Node.js 一樣處理資訊存取。

**環境變數**  
無法存取環境變數。您可以改為使用 CloudFront KeyValueStore 為您的 CloudFront Functions 建立鍵值對的集中式資料儲存。CloudFront KeyValueStore 可動態更新組態資料，不需要部署程式碼變更。如需詳細資訊，請參閱[Amazon CloudFront KeyValueStore](kvs-with-functions.md)。

**網路存取**  
不支援網路呼叫。例如，不支援 XHR、HTTP(S) 和通訊端。