与旧 Node.js 版本的兼容性
¥Compatibility with older Node.js versions
在 Node.js 0.10 之前,Readable
流接口更简单,但功能和用处也不大。
¥Prior to Node.js 0.10, the Readable
stream interface was simpler, but also
less powerful and less useful.
-
'data'
事件将立即开始触发,而不是等待对stream.read()
方法的调用。需要执行一些工作来决定如何处理数据的应用需要将读取的数据存储到缓冲区中,这样数据就不会丢失。¥Rather than waiting for calls to the
stream.read()
method,'data'
events would begin emitting immediately. Applications that would need to perform some amount of work to decide how to handle data were required to store read data into buffers so the data would not be lost. -
stream.pause()
方法是建议性的,而不是保证。这意味着即使流处于暂停状态,仍然需要准备接收'data'
事件。¥The
stream.pause()
method was advisory, rather than guaranteed. This meant that it was still necessary to be prepared to receive'data'
events even when the stream was in a paused state.
在 Node.js 0.10 中,添加了 Readable
类。为了与旧的 Node.js 程序向后兼容,当添加 'data'
事件处理程序或调用 stream.resume()
方法时,Readable
流切换到 "流动模式"。效果是,即使不使用新的 stream.read()
方法和 'readable'
事件,也不必再担心丢失 'data'
块。
¥In Node.js 0.10, the Readable
class was added. For backward
compatibility with older Node.js programs, Readable
streams switch into
"flowing mode" when a 'data'
event handler is added, or when the
stream.resume()
method is called. The effect is that, even
when not using the new stream.read()
method and
'readable'
event, it is no longer necessary to worry about losing
'data'
chunks.
虽然大多数应用将继续正常运行,但这会在以下情况下引入边缘情况:
¥While most applications will continue to function normally, this introduces an edge case in the following conditions:
-
没有添加
'data'
事件监听器。¥No
'data'
event listener is added. -
永远不会调用
stream.resume()
方法。¥The
stream.resume()
method is never called. -
流不会通过管道传输到任何可写目标。
¥The stream is not piped to any writable destination.
例如,考虑以下代码:
¥For example, consider the following code:
// WARNING! BROKEN!
net.createServer((socket) => {
// We add an 'end' listener, but never consume the data.
socket.on('end', () => {
// It will never get here.
socket.end('The message was received but was not processed.\n');
});
}).listen(1337);
在 Node.js 0.10 之前,传入的消息数据将被简单地丢弃。但是,在 Node.js 0.10 及更高版本中,套接字将永远保持暂停状态。
¥Prior to Node.js 0.10, the incoming message data would be simply discarded. However, in Node.js 0.10 and beyond, the socket remains paused forever.
这种情况下的解决方法是调用 stream.resume()
方法开始数据流:
¥The workaround in this situation is to call the
stream.resume()
method to begin the flow of data:
// Workaround.
net.createServer((socket) => {
socket.on('end', () => {
socket.end('The message was received but was not processed.\n');
});
// Start the flow of data, discarding it.
socket.resume();
}).listen(1337);
除了新的 Readable
流切换到流动模式之外,还可以使用 readable.wrap()
方法将 0.10 之前的样式流封装在 Readable
类中。
¥In addition to new Readable
streams switching into flowing mode,
pre-0.10 style streams can be wrapped in a Readable
class using the
readable.wrap()
method.