Skip to content

Commit b009a11

Browse files
committed
v10.16.0
1 parent 35ab9f2 commit b009a11

File tree

22 files changed

+268
-13
lines changed

22 files changed

+268
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- YAML
2+
added: v11.0.0
3+
-->
4+
5+
* 返回: {boolean}
6+
7+
如果为 true,则 `Immediate` 对象将会使 Node.js 事件循环保持活动状态。
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!-- YAML
2+
added: v11.4.0
3+
changes:
4+
- version: v11.14.0
5+
pr-url: https://github.com/nodejs/node/pull/26989
6+
description: Symbol.asyncIterator support is no longer experimental.
7+
-->
8+
9+
* 返回: {AsyncIterator}
10+
11+
创建一个 `AsyncIterator` 对象,该对象以字符串形式迭代输入流中的每一行。
12+
这个方法允许 `readline.Interface` 对象使用 `for await...of` 循环的异步迭代。
13+
14+
输入流中的错误不会被转发。
15+
16+
如果循环以 `break``throw``return` 终止,则 [`rl.close()`] 将会被调用。
17+
换句话说,对 `readline.Interface` 的迭代将会始终完全消费输入流。
18+
19+
性能比不上传统的 `'line'` 事件的 API。
20+
对于性能敏感的应用程序,请使用 `'line'`
21+
22+
```js
23+
async function processLineByLine() {
24+
const rl = readline.createInterface({
25+
// ...
26+
});
27+
28+
for await (const line of rl) {
29+
// readline 输入中的每一行将会在此处作为 `line`。
30+
}
31+
}
32+
```
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!-- YAML
2+
added: v11.10.0
3+
-->
4+
5+
* `historyPath` {string} 历史文件的路径。
6+
* `callback` {Function} 当历史记录写入已准备好或出错时调用。
7+
* `err` {Error}
8+
* `repl` {repl.REPLServer}
9+
10+
Initializes a history log file for the REPL instance. When executing the
11+
Node.js binary and using the command line REPL, a history file is initialized
12+
by default. However, this is not the case when creating a REPL
13+
programmatically. Use this method to initialize a history log file when working
14+
with REPL instances programmatically.
15+

api-docs/396f688c9c4681bf3c7d192213023be0e4bd0f7b99763c1a5e0b208e0e437ec4.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Node.js 不保证回调被触发的确切时间,也不保证它们的顺序。
1414
回调会在尽可能接近指定的时间调用。
1515

1616
`delay` 大于 `2147483647` 或小于 `1` 时,`delay` 将设置为 `1`
17+
非整数的延迟会被截断为整数。
1718

1819
如果 `callback` 不是函数,则抛出 [`TypeError`]
1920

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!-- YAML
2+
added: v11.13.0
3+
-->
4+
5+
* `filename` {string} 要保存 V8 堆快照的文件路径。
6+
如果未指定,则将会生成具有 `'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'` 模式的文件名,
7+
其中 `{pid}` 将会是 Node.js 进程的 PID,`{thread_id}` 将会为 `0`(当从主 Node.js 线程调用 `writeHeapSnapshot()` 时)或工作线程的 id。
8+
* 返回: {string} 保存快照的文件名。
9+
10+
生成当前 V8 堆的快照并将其写入 JSON 文件。
11+
此文件旨在与 Chrome DevTools 等工具一起使用。
12+
JSON 模式未记录且特定于V8引擎,并且可能从 V8 的一个版本更改为下一个版本。
13+
14+
堆快照特定于单个 V8 隔离。
15+
使用[工作线程][Worker Threads]时,从主线程生成的堆快照将不包含有关工作线程的任何信息,反之亦然。
16+
17+
```js
18+
const { writeHeapSnapshot } = require('v8');
19+
const {
20+
Worker,
21+
isMainThread,
22+
parentPort
23+
} = require('worker_threads');
24+
25+
if (isMainThread) {
26+
const worker = new Worker(__filename);
27+
28+
worker.once('message', (filename) => {
29+
console.log(`工作线程的堆转储: ${filename}`);
30+
// 获取主线程的堆转储。
31+
console.log(`主线程的堆转储: ${writeHeapSnapshot()}`);
32+
});
33+
34+
// 通知工作线程创建一个堆转储。
35+
worker.postMessage('heapdump');
36+
} else {
37+
parentPort.once('message', (message) => {
38+
if (message === 'heapdump') {
39+
// 为工作线程生成一个堆转储,并返回文件名到父线程。
40+
parentPort.postMessage(writeHeapSnapshot());
41+
}
42+
});
43+
}
44+
```
45+

api-docs/57e50967944bd6b6c66ba10363adb75ab0aaa93ae223c0fd90866cb05929188c.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11

22
默认情况下,在把输出写入到提供的可写流(默认为 `process.stdout`)之前,[`repl.REPLServer`] 实例会使用 [`util.inspect()`] 方法对输出进行格式化。
3-
使用 `util.inspect()` 方法时,`useColors` 选项可被指定是否在建立默认输出器时使用 ANSI 风格的代码给输出上色。
3+
`showProxy` 检查选项会默认设置为 true,`colors` 选项会设置为 true,具体取决于 REPL 的 `useColors` 选项。
4+
5+
可以在构造时指定 `useColors` 布尔值选项,以指示默认的编写器使用 ANSI 样式代码来着色来自 `util.inspect()` 方法的输出。
6+
7+
如果 REPL 作为独立程序运行,则还可以使用 `inspect.replDefaults` 属性从 REPL 内部更改 REPL 的检查默认值[`util.inspect()`],该属性是 [`util.inspect()`] 中的 `defaultOptions` 的镜像。
8+
9+
```console
10+
> util.inspect.replDefaults.compact = false;
11+
false
12+
> [1]
13+
[
14+
1
15+
]
16+
>
17+
```
418

519
在构造时,通过在 `writer` 选项传入一个新的函数,可以完全地自定义一个 [`repl.REPLServer`] 实例的输出。
620
例子,把输入的任何文本转换为大写:
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,70 @@
11

2-
`readline` 的一个常见用例是每次一行地从文件系统[可读流][Readable]消费输入:
2+
`readline` 的一个常见用例是每次一行地输入文件。
3+
最简单的方法是利用 [`fs.ReadStream`] API 以及 `for await...of` 循环:
34

45
```js
6+
const fs = require('fs');
57
const readline = require('readline');
8+
9+
async function processLineByLine() {
10+
const fileStream = fs.createReadStream('input.txt');
11+
12+
const rl = readline.createInterface({
13+
input: fileStream,
14+
crlfDelay: Infinity
15+
});
16+
// 注意:我们使用 crlfDelay 选项将 input.txt 中的所有 CR LF 实例('\r\n')识别为单个换行符。
17+
18+
for await (const line of rl) {
19+
// input.txt 中的每一行在这里将会被连续地用作 `line`。
20+
console.log(`Line from file: ${line}`);
21+
}
22+
}
23+
24+
processLineByLine();
25+
```
26+
27+
或者,可以使用 [`'line'`] 事件:
28+
29+
```js
630
const fs = require('fs');
31+
const readline = require('readline');
732

833
const rl = readline.createInterface({
934
input: fs.createReadStream('sample.txt'),
1035
crlfDelay: Infinity
1136
});
1237

1338
rl.on('line', (line) => {
14-
console.log(`文件的每行内容:${line}`);
39+
console.log(`文件中的每一行: ${line}`);
1540
});
1641
```
1742

43+
目前,`for await...of` 循环可能会慢一点。
44+
如果 `async` / `await` 流和速度都是必不可少的,可以应用混合方法:
45+
46+
```js
47+
const { once } = require('events');
48+
const { createReadStream } = require('fs');
49+
const { createInterface } = require('readline');
50+
51+
(async function processLineByLine() {
52+
try {
53+
const rl = createInterface({
54+
input: createReadStream('big-file.txt'),
55+
crlfDelay: Infinity
56+
});
57+
58+
rl.on('line', (line) => {
59+
// 处理行。
60+
});
61+
62+
await once(rl, 'close');
63+
64+
console.log('文件已处理');
65+
} catch (err) {
66+
console.error(err);
67+
}
68+
})();
69+
```
70+

api-docs/67f65398e8ea16305f43cee7f26bf7e1d1bdfc82174490bee78828b874858819.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
added: v0.9.1
33
-->
44

5-
* `callback` {Function} 在当前回合的 [Node.js 事件循环][the Node.js Event Loop]结束时调用的函数。
5+
* `callback` {Function} 在当前回合的 [Node.js 事件循环][Event Loop]结束时调用的函数。
66
* `...args` {any} 当调用 `callback` 时传入的可选参数。
77
* 返回: {Immediate} 用于 [`clearImmediate()`]
88

api-docs/7b97aa3880f3a720e2113cc95634975b8b0e936a79e75b9ab7b7d239f5c0d847.md

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ changes:
3232
**默认值:** `100`
3333
* `removeHistoryDuplicates` {boolean} 如果为 `true`, 则当添加到历史列表的新输入行与旧的输入行重复时,将从列表中删除旧行。
3434
**默认值:** `false`
35+
* `escapeCodeTimeout` {number} `readline` 将会等待一个字符的持续时间(当以毫秒为单位读取模糊键序列时,可以使用输入读取到目前为止形成完整的键序列,并且可以采取额外的输入来完成更长的键序列)。
36+
**默认值:** `500`
3537

3638
`readline.createInterface()` 方法创建一个新的 `readline.Interface` 实例。
3739

api-docs/9726adbd6932c36947e96c84aaec2a185488cdf2928139f67f38233051d18c74.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ changes:
2525
`ca``cert``ciphers``clientCertEngine``crl``dhparam``ecdhCurve``honorCipherOrder``key``passphrase``pfx``rejectUnauthorized``secureOptions``secureProtocol``servername``sessionIdContext`
2626

2727
`options` 可以是对象、字符串、或 [`URL`] 对象。
28-
如果 `options` 是一个字符串, 则自动使用 [`url.parse()`] 解析它。
28+
如果 `options` 是一个字符串, 则自动使用 [`new URL()`] 解析它。
2929
如果它是一个 [`URL`] 对象,则将自动转换为普通的 `options` 对象。
3030

3131
```js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!-- YAML
2+
added: v11.13.0
3+
-->
4+
5+
* 返回: {stream.Readable} 包含 V8 堆快照的可读流。
6+
7+
生成当前 V8 堆的快照,并返回可读流,该可读流可用于读取 JSON 序列化表示。
8+
此 JSON 流格式旨在与 Chrome DevTools 等工具一起使用。
9+
JSON 模式未记录且特定于V8引擎,并且可能从 V8 的一个版本更改为下一个版本。
10+
11+
```js
12+
const stream = v8.getHeapSnapshot();
13+
stream.pipe(process.stdout);
14+
```
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- YAML
2+
added: v11.0.0
3+
-->
4+
5+
* 返回: {boolean}
6+
7+
如果为 true,则 `Timeout` 对象将会使 Node.js 事件循环保持活动状态。
8+

api-docs/a905f5e97552b5740be72caebb9e8110451f980680e71d55e143bfca72d2f777.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ changes:
2323
* `malloced_memory` {number}
2424
* `peak_malloced_memory` {number}
2525
* `does_zap_garbage` {number}
26+
* `number_of_native_contexts` {number}
27+
* `number_of_detached_contexts` {number}
2628

2729
`does_zap_garbage` 是一个 0/1 布尔值,表示是否启用了 `--zap_code_space` 选项。
2830
这使得 V8 用位模式覆盖堆垃圾。
2931
RSS 占用空间(常驻内存集)会变得越来越大,因为它不断触及所有堆页面,这使得它们不太可能被操作系统交换出。
3032

33+
`number_of_native_contexts` native_context 的值是当前活动的顶层上下文的数量。
34+
随着时间的推移,此数字的增加表示内存泄漏。
35+
36+
`number_of_detached_contexts` detached_context 的值是已分离但尚未回收垃圾的上下文数。
37+
该数字不为零表示潜在的内存泄漏。
38+
3139
<!-- eslint-skip -->
3240
```js
3341
{
@@ -39,7 +47,9 @@ RSS 占用空间(常驻内存集)会变得越来越大,因为它不断触
3947
heap_size_limit: 1535115264,
4048
malloced_memory: 16384,
4149
peak_malloced_memory: 1127496,
42-
does_zap_garbage: 0
50+
does_zap_garbage: 0,
51+
number_of_native_contexts: 1,
52+
number_of_detached_contexts: 0
4353
}
4454
```
4555

api-docs/aaa7e5ca9d0bb0b0a8aeb9d4a03259bafc3643f880d17d33e8d2f45629e90056.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
<!-- YAML
22
added: v0.4.5
3+
changes:
4+
- version: v2.5.0
5+
pr-url: https://github.com/nodejs/node/pull/2228
6+
description: parameter `maxCachedSessions` added to `options` for TLS
7+
sessions reuse.
8+
- version: v5.3.0
9+
pr-url: https://github.com/nodejs/node/pull/4252
10+
description: support `0` `maxCachedSessions` to disable TLS session caching.
311
-->
412

513
HTTPS 的 [`Agent`] 对象,类似于 [`http.Agent`]

api-docs/ab404272c190fec3a6488ad03e947320397137d65c41b7e2714e9a818c271cea.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
`timer` 模块暴露了一个全局的 API,用于预定在将来某个时间段调用的函数。
77
因为定时器函数是全局变量,所以不需要调用 `require('timers')` 来使用 API。
88

9-
Node.js 中的定时器函数实现了与 Web 浏览器提供的定时器 API 类似的 API,但是使用了不同的内部实现(基于 [Node.js 事件循环][the Node.js Event Loop]构建)。
9+
Node.js 中的定时器函数实现了与 Web 浏览器提供的定时器 API 类似的 API,但是使用了不同的内部实现(基于 Node.js [事件循环][Event Loop]构建)。
1010

1111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- YAML
2+
changes:
3+
- version: v12.5.0
4+
pr-url: https://github.com/nodejs/node/pull/28209
5+
description: do not automatically set servername if the target host was
6+
specified using an IP address.
7+
-->
8+
* `options` {Object} 在代理上设置的一组可配置的选项。可以具有与 [`http.Agent(options)`] 相同的字段,以及:
9+
* `maxCachedSessions` {number} TLS 缓存会话的最大数量。使用 `0` 可禁用 TLS 会话缓存。**默认值:** `100`
10+
* `servername` {string} 要发送到服务器的[服务器名称指示的扩展名][sni wiki]的值。使用空字符串 `''` 可以禁用发送扩展名。**默认值:** 目标服务器的主机名,除非目标服务器被指定为使用 IP 地址,在这种情况下默认为 `''`(无扩展名)。
11+
12+
有关 TLS 会话复用的信息,请参阅[会话恢复][`Session Resumption`]
13+

api-docs/c046594d15762a90d81acfa7db68302f4709d068ba4319f6d08362412a3d3a28.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ added: v0.0.1
1010
预定每隔 `delay` 毫秒重复执行 `callback`
1111

1212
`delay` 大于 `2147483647` 或小于 `1` 时,`delay` 将设置为 `1`
13+
非整数的延迟会被截断为整数。
1314

1415
如果 `callback` 不是函数,则抛出 [`TypeError`]
1516

api-docs/d92fdb582d9f54f8d9cc49cb54d02a9242d683e49df3c57863bc5ef173bd7e70.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

2-
当创建一个新的 `repl.REPLServer` 时,可以提供一个自定义的解释函数。
2+
当创建一个新的 [`repl.REPLServer`] 时,可以提供一个自定义的解释函数。
33
这可以用于实现完全定制化的 REPL 应用。
44

5-
例子,一个执行文本翻译的 REPL:
5+
以下是 REPL 的一个假设的示例,执行从一种语言到另一种语言的文本转换
66

77
```js
88
const repl = require('repl');
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
1+
<!-- YAML
2+
changes:
3+
- version: v12.3.0
4+
pr-url: https://github.com/nodejs/node/pull/27151
5+
description: The `'uncaughtException'` event is from now on triggered if the
6+
repl is used as standalone program.
7+
-->
18

29
REPL使用 [`domain`] 模块来捕获会话期间的所有未捕获异常。
310

411
在REPL中使用 [`domain`] 模块有如下副作用:
512

6-
* 未捕获的异常不会产生 [`'uncaughtException'`] 事件。
13+
* 如果将 `repl` 用作独立程序,则未捕获的异常仅触发 [`'uncaughtException'`] 事件。
14+
如果 `repl` 包含在另一个应用程序的任何位置,则为此事件添加监听器将会抛出 [`ERR_INVALID_REPL_INPUT`] 异常。
715
* 如果尝试使用 [`process.setUncaughtExceptionCaptureCallback()`] 将产生一个 [`ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE`] 错误。
816

17+
作为独立程序:
18+
19+
```js
20+
process.on('uncaughtException', () => console.log('未捕获的异常'));
21+
22+
throw new Error('foobar');
23+
// 打印:未捕获的异常
24+
```
25+
26+
当在其他应用程序中使用时:
27+
28+
```js
29+
process.on('uncaughtException', () => console.log('未捕获的异常'));
30+
// 抛出 TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException` cannot be used in the REPL
31+
32+
throw new Error('foobar');
33+
// 抛出:
34+
// Error: foobar
35+
```
36+

api-docs/e545317c41c8a0b49c71fd696aa67c653c4cc4d95babc85dd2fd09020556ef94.md

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ added: v1.0.0
88
虚拟机启动后更改设置可能会导致不可预测的行为,包括崩溃和数据丢失,或者它可能根本就什么都不做。
99

1010
可以通过运行 `node --v8-options` 来检测可用于 Node.js 版本的 V8 选项。
11-
[这里][here]提供了一个非官方的、社区维护的选项及其效果列表。
1211

1312
用法:
1413

api-docs/e7058f3a5547a20227857a47536a317563dcd9f509345b8906c5e328630cf4a9.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
1+
<!-- YAML
2+
added: v11.3.0
3+
-->
24
- {number} **默认值:** `40000`
35

46
请参阅 [`http.Server#headersTimeout`]

0 commit comments

Comments
 (0)