

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

# 適用於 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) 和通訊端。