Skip to content

Commit 2291aa5

Browse files
Weijia WangZYSzys
authored andcommitted
Translate the comment to zh-cn (#1897)
* Translate the comment in `backpressuring-in-streams.md` to zh-cn * Improve translation, making it more clear
1 parent b638c37 commit 2291aa5

1 file changed

Lines changed: 17 additions & 22 deletions

File tree

locale/zh-cn/docs/guides/backpressuring-in-streams.md

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ layout: docs.hbs
2020
```javascript
2121
const readline = require('readline');
2222

23-
// process.stdin and process.stdout are both instances of Streams
23+
// process.stdin process.stdout 都是 Stream 的实例
2424
const rl = readline.createInterface({
2525
input: process.stdin,
2626
output: process.stdout
@@ -66,9 +66,9 @@ const { pipeline } = require('stream');
6666
const fs = require('fs');
6767
const zlib = require('zlib');
6868

69-
// Use the pipeline API to easily pipe a series of streams
70-
// together and get notified when the pipeline is fully done.
71-
// A pipeline to gzip a potentially huge video file efficiently:
69+
// 使用 pipiline API 可以轻松地把数据流连接到一起,并且在整个数据流
70+
// 处理完毕时得到通知。下面的例子就是把一个很大的视频文件使用 pipeline
71+
// 进行 gzip 压缩。
7272

7373
pipeline(
7474
fs.createReadStream('The.Matrix.1080p.mkv'),
@@ -114,9 +114,8 @@ async function run() {
114114
写入磁盘的速度远比从磁盘读取数据慢得多,因此,当我们试图压缩一个文件并写入磁盘时,积压的问题也就出现了。因为写磁盘的速度不能跟上读磁盘的速度。
115115

116116
```javascript
117-
// Secretly the stream is saying: "whoa, whoa! hang on, this is way too much!"
118-
// Data will begin to build up on the read-side of the data buffer as
119-
// `write` tries to keep up with the incoming data flow.
117+
// 数据流会偷偷地说:“好了,好了!停一下,这太过分了!”
118+
// 然后数据将会在读入侧堆积,这样写入侧才能和数据流的读入速度保持同步。
120119
inp.pipe(gzip).pipe(outputFile);
121120
```
122121
这就是为什么说积压机制很重要——如果积压机制不存在,进程将用完你全部的系统内存,从而对其它进程产生显著影响,它独占系统大量资源直到任务完成为止。
@@ -353,8 +352,8 @@ Readable.pipe(Transformable).pipe(Writable);
353352
这里有个糟糕的使用 [`.push()`][] 的例子:
354353
355354
```javascript
356-
// This is problematic as it completely ignores return value from push
357-
// which may be a signal for backpressure from the destination stream!
355+
// 下面的代码是有问题的,因为它完全忽略了 push 的返回值,而这个返回值是表示
356+
// 目的地是否存在积压的重要信号!
358357
class MyReadable extends Readable {
359358
_read(size) {
360359
let chunk;
@@ -368,9 +367,8 @@ class MyReadable extends Readable {
368367
另外,从定制流之外,忽略积压简直可笑至极。在以下反例中,代码仅关注数据是否到达(通过 [`.data` event][] 订阅):
369368
370369
```javascript
371-
// This ignores the backpressure mechanisms Node.js has set in place,
372-
// and unconditionally pushes through data, regardless if the
373-
// destination stream is ready for it or not.
370+
// 下面的代码忽略了 Node.js 内部处理积压的机制,无条件地写入数据,不管目的地的流
371+
// 有没有做好准备接收。
374372
readable.on('data', (data) =>
375373
writable.write(data)
376374
);
@@ -387,9 +385,8 @@ readable.on('data', (data) =>
387385
388386
<!-- eslint-disable indent -->
389387
```javascript
390-
// This writable is invalid because of the async nature of JavaScript callbacks.
391-
// Without a return statement for each callback prior to the last,
392-
// there is a great chance multiple callbacks will be called.
388+
// 下面的可写流是有问题的,因为 JavaScript 的异步回调机制。
389+
// 每个 callback 都没有返回,将会有很大的几率触发多次 callback。
393390
class MyWritable extends Writable {
394391
_write(chunk, encoding, callback) {
395392
if (chunk.toString().indexOf('a') >= 0)
@@ -400,7 +397,7 @@ class MyWritable extends Writable {
400397
}
401398
}
402399

403-
// The proper way to write this would be:
400+
// 更恰当的写法是下面这样:
404401
if (chunk.contains('a'))
405402
return callback();
406403
else if (chunk.contains('b'))
@@ -411,8 +408,7 @@ class MyWritable extends Writable {
411408
在实现 [`._writev()`][] 方法时还有其它一些东西值得考虑。此函数与 [`.cork()`][] 耦合,但是编写代码的时有一个容易犯的错误:
412409
413410
```javascript
414-
// Using .uncork() twice here makes two calls on the C++ layer, rendering the
415-
// cork/uncork technique useless.
411+
// 这里调用了 .uncork() 两次,将会对 C++ 层产生两次调用,使 cork/uncork 机制无效。
416412
ws.cork();
417413
ws.write('hello ');
418414
ws.write('world ');
@@ -423,8 +419,7 @@ ws.write('from ');
423419
ws.write('Matteo');
424420
ws.uncork();
425421

426-
// The correct way to write this is to utilize process.nextTick(), which fires
427-
// on the next event loop.
422+
// 正确的做法是在 process.nextTick() 中调用 .uncork(),这样就可以在下次事件循环中触发。
428423
ws.cork();
429424
ws.write('hello ');
430425
ws.write('world ');
@@ -435,13 +430,13 @@ ws.write('from ');
435430
ws.write('Matteo');
436431
process.nextTick(doUncork, ws);
437432

438-
// as a global function
433+
// 作为一个全局函数
439434
function doUncork(stream) {
440435
stream.uncork();
441436
}
442437
```
443438
444-
[`.cork()`][] 方法可以让我们调用任意多次,我们只需小心调用 [`.uncork()`][] 方法使得它可以正常流入
439+
[`.cork()`][] 方法可以调用任意多次,但同时也要记得调用 [`.uncork()`][] 方法同样的次数,使得它可以正常流入
445440
446441
## 总结
447442

0 commit comments

Comments
 (0)