阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js stream 兼容旧版本的 Node.js

      Node.js stream 兼容旧版本的 Node.js

      Prior to Node.js 0.10, the Readable stream interface was simpler, but also less powerful and less useful.

      • 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.
      • 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.

      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:

      No ‘data’ event listener is added. 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);
      

      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.

      The workaround in this situation is to call the

      // 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);
      

      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.


      更多内容请参考:Node.js stream 流,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录