forked from nodejscn/node-api-cn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
268 additions
and
13 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
api-docs/0df8a572b7dbefa21c101d8d8822dcd47b53aac4ce7abdde0ef9b9eadabfd086.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<!-- YAML | ||
added: v11.0.0 | ||
--> | ||
|
||
* 返回: {boolean} | ||
|
||
如果为 true,则 `Immediate` 对象将会使 Node.js 事件循环保持活动状态。 | ||
|
33 changes: 33 additions & 0 deletions
33
api-docs/2774854b6d9e401c9184046b475a5a4b51028ad6b0abad22ab17b02b52700f06.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!-- YAML | ||
added: v11.4.0 | ||
changes: | ||
- version: v11.14.0 | ||
pr-url: https://github.com/nodejs/node/pull/26989 | ||
description: Symbol.asyncIterator support is no longer experimental. | ||
--> | ||
|
||
* 返回: {AsyncIterator} | ||
|
||
创建一个 `AsyncIterator` 对象,该对象以字符串形式迭代输入流中的每一行。 | ||
这个方法允许 `readline.Interface` 对象使用 `for await...of` 循环的异步迭代。 | ||
|
||
输入流中的错误不会被转发。 | ||
|
||
如果循环以 `break`,`throw` 或 `return` 终止,则 [`rl.close()`] 将会被调用。 | ||
换句话说,对 `readline.Interface` 的迭代将会始终完全消费输入流。 | ||
|
||
性能比不上传统的 `'line'` 事件的 API。 | ||
对于性能敏感的应用程序,请使用 `'line'`。 | ||
|
||
```js | ||
async function processLineByLine() { | ||
const rl = readline.createInterface({ | ||
// ... | ||
}); | ||
for await (const line of rl) { | ||
// readline 输入中的每一行将会在此处作为 `line`。 | ||
} | ||
} | ||
``` | ||
|
15 changes: 15 additions & 0 deletions
15
api-docs/2dc8208af99f28f9e98eecb5030a3949d8e4a1200271b7b72270d33237cd1b8e.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!-- YAML | ||
added: v11.10.0 | ||
--> | ||
|
||
* `historyPath` {string} 历史文件的路径。 | ||
* `callback` {Function} 当历史记录写入已准备好或出错时调用。 | ||
* `err` {Error} | ||
* `repl` {repl.REPLServer} | ||
|
||
Initializes a history log file for the REPL instance. When executing the | ||
Node.js binary and using the command line REPL, a history file is initialized | ||
by default. However, this is not the case when creating a REPL | ||
programmatically. Use this method to initialize a history log file when working | ||
with REPL instances programmatically. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
api-docs/47553fafd6680b7a6ec34486ef34780126785a9032df717951231922086107e8.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<!-- YAML | ||
added: v11.13.0 | ||
--> | ||
|
||
* `filename` {string} 要保存 V8 堆快照的文件路径。 | ||
如果未指定,则将会生成具有 `'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'` 模式的文件名, | ||
其中 `{pid}` 将会是 Node.js 进程的 PID,`{thread_id}` 将会为 `0`(当从主 Node.js 线程调用 `writeHeapSnapshot()` 时)或工作线程的 id。 | ||
* 返回: {string} 保存快照的文件名。 | ||
|
||
生成当前 V8 堆的快照并将其写入 JSON 文件。 | ||
此文件旨在与 Chrome DevTools 等工具一起使用。 | ||
JSON 模式未记录且特定于V8引擎,并且可能从 V8 的一个版本更改为下一个版本。 | ||
|
||
堆快照特定于单个 V8 隔离。 | ||
使用[工作线程][Worker Threads]时,从主线程生成的堆快照将不包含有关工作线程的任何信息,反之亦然。 | ||
|
||
```js | ||
const { writeHeapSnapshot } = require('v8'); | ||
const { | ||
Worker, | ||
isMainThread, | ||
parentPort | ||
} = require('worker_threads'); | ||
if (isMainThread) { | ||
const worker = new Worker(__filename); | ||
worker.once('message', (filename) => { | ||
console.log(`工作线程的堆转储: ${filename}`); | ||
// 获取主线程的堆转储。 | ||
console.log(`主线程的堆转储: ${writeHeapSnapshot()}`); | ||
}); | ||
// 通知工作线程创建一个堆转储。 | ||
worker.postMessage('heapdump'); | ||
} else { | ||
parentPort.once('message', (message) => { | ||
if (message === 'heapdump') { | ||
// 为工作线程生成一个堆转储,并返回文件名到父线程。 | ||
parentPort.postMessage(writeHeapSnapshot()); | ||
} | ||
}); | ||
} | ||
``` | ||
|
16 changes: 15 additions & 1 deletion
16
api-docs/57e50967944bd6b6c66ba10363adb75ab0aaa93ae223c0fd90866cb05929188c.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 55 additions & 2 deletions
57
api-docs/65ee1bfdc50c4e7ff3c3b60c6b02fc0887ebccedd5862de7d300b8ba39b9aaff.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,70 @@ | ||
|
||
`readline` 的一个常见用例是每次一行地从文件系统[可读流][Readable]消费输入: | ||
`readline` 的一个常见用例是每次一行地输入文件。 | ||
最简单的方法是利用 [`fs.ReadStream`] API 以及 `for await...of` 循环: | ||
|
||
```js | ||
const fs = require('fs'); | ||
const readline = require('readline'); | ||
async function processLineByLine() { | ||
const fileStream = fs.createReadStream('input.txt'); | ||
const rl = readline.createInterface({ | ||
input: fileStream, | ||
crlfDelay: Infinity | ||
}); | ||
// 注意:我们使用 crlfDelay 选项将 input.txt 中的所有 CR LF 实例('\r\n')识别为单个换行符。 | ||
for await (const line of rl) { | ||
// input.txt 中的每一行在这里将会被连续地用作 `line`。 | ||
console.log(`Line from file: ${line}`); | ||
} | ||
} | ||
processLineByLine(); | ||
``` | ||
|
||
或者,可以使用 [`'line'`] 事件: | ||
|
||
```js | ||
const fs = require('fs'); | ||
const readline = require('readline'); | ||
const rl = readline.createInterface({ | ||
input: fs.createReadStream('sample.txt'), | ||
crlfDelay: Infinity | ||
}); | ||
rl.on('line', (line) => { | ||
console.log(`文件的每行内容:${line}`); | ||
console.log(`文件中的每一行: ${line}`); | ||
}); | ||
``` | ||
|
||
目前,`for await...of` 循环可能会慢一点。 | ||
如果 `async` / `await` 流和速度都是必不可少的,可以应用混合方法: | ||
|
||
```js | ||
const { once } = require('events'); | ||
const { createReadStream } = require('fs'); | ||
const { createInterface } = require('readline'); | ||
(async function processLineByLine() { | ||
try { | ||
const rl = createInterface({ | ||
input: createReadStream('big-file.txt'), | ||
crlfDelay: Infinity | ||
}); | ||
rl.on('line', (line) => { | ||
// 处理行。 | ||
}); | ||
await once(rl, 'close'); | ||
console.log('文件已处理'); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
})(); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
api-docs/9b96cb5adb765942f292953779b86bec06d2946fb3be72d357bbe50a18a6a37c.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!-- YAML | ||
added: v11.13.0 | ||
--> | ||
|
||
* 返回: {stream.Readable} 包含 V8 堆快照的可读流。 | ||
|
||
生成当前 V8 堆的快照,并返回可读流,该可读流可用于读取 JSON 序列化表示。 | ||
此 JSON 流格式旨在与 Chrome DevTools 等工具一起使用。 | ||
JSON 模式未记录且特定于V8引擎,并且可能从 V8 的一个版本更改为下一个版本。 | ||
|
||
```js | ||
const stream = v8.getHeapSnapshot(); | ||
stream.pipe(process.stdout); | ||
``` | ||
|
8 changes: 8 additions & 0 deletions
8
api-docs/a5abb5e3a18ceed4cb896c64178e5ab325218e47b7802b09e1578af46b34919d.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<!-- YAML | ||
added: v11.0.0 | ||
--> | ||
|
||
* 返回: {boolean} | ||
|
||
如果为 true,则 `Timeout` 对象将会使 Node.js 事件循环保持活动状态。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
api-docs/aaa7e5ca9d0bb0b0a8aeb9d4a03259bafc3643f880d17d33e8d2f45629e90056.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
api-docs/b76f1af153dd1239bad0510a9f637718b878bee73122027ea8b65297b79ebfa0.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- YAML | ||
changes: | ||
- version: v12.5.0 | ||
pr-url: https://github.com/nodejs/node/pull/28209 | ||
description: do not automatically set servername if the target host was | ||
specified using an IP address. | ||
--> | ||
* `options` {Object} 在代理上设置的一组可配置的选项。可以具有与 [`http.Agent(options)`] 相同的字段,以及: | ||
* `maxCachedSessions` {number} TLS 缓存会话的最大数量。使用 `0` 可禁用 TLS 会话缓存。**默认值:** `100`。 | ||
* `servername` {string} 要发送到服务器的[服务器名称指示的扩展名][sni wiki]的值。使用空字符串 `''` 可以禁用发送扩展名。**默认值:** 目标服务器的主机名,除非目标服务器被指定为使用 IP 地址,在这种情况下默认为 `''`(无扩展名)。 | ||
|
||
有关 TLS 会话复用的信息,请参阅[会话恢复][`Session Resumption`]。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
api-docs/d92fdb582d9f54f8d9cc49cb54d02a9242d683e49df3c57863bc5ef173bd7e70.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 29 additions & 1 deletion
30
api-docs/dd5312afb24e99bef283b7385e484422cc73a22eb386be4d4fde152774a3dfe3.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,36 @@ | ||
<!-- YAML | ||
changes: | ||
- version: v12.3.0 | ||
pr-url: https://github.com/nodejs/node/pull/27151 | ||
description: The `'uncaughtException'` event is from now on triggered if the | ||
repl is used as standalone program. | ||
--> | ||
|
||
REPL使用 [`domain`] 模块来捕获会话期间的所有未捕获异常。 | ||
|
||
在REPL中使用 [`domain`] 模块有如下副作用: | ||
|
||
* 未捕获的异常不会产生 [`'uncaughtException'`] 事件。 | ||
* 如果将 `repl` 用作独立程序,则未捕获的异常仅触发 [`'uncaughtException'`] 事件。 | ||
如果 `repl` 包含在另一个应用程序的任何位置,则为此事件添加监听器将会抛出 [`ERR_INVALID_REPL_INPUT`] 异常。 | ||
* 如果尝试使用 [`process.setUncaughtExceptionCaptureCallback()`] 将产生一个 [`ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE`] 错误。 | ||
|
||
作为独立程序: | ||
|
||
```js | ||
process.on('uncaughtException', () => console.log('未捕获的异常')); | ||
throw new Error('foobar'); | ||
// 打印:未捕获的异常 | ||
``` | ||
|
||
当在其他应用程序中使用时: | ||
|
||
```js | ||
process.on('uncaughtException', () => console.log('未捕获的异常')); | ||
// 抛出 TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException` cannot be used in the REPL | ||
throw new Error('foobar'); | ||
// 抛出: | ||
// Error: foobar | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
api-docs/e7058f3a5547a20227857a47536a317563dcd9f509345b8906c5e328630cf4a9.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
|
||
<!-- YAML | ||
added: v11.3.0 | ||
--> | ||
- {number} **默认值:** `40000`。 | ||
|
||
请参阅 [`http.Server#headersTimeout`]。 | ||
|
Oops, something went wrong.