0
$ node --version
v0.10.29

类似于“现在无法切换到旧模式”- tls.connect 函数中的 Node.JS apn 模块错误我在暂停可读流时抛出“现在无法切换到旧模式”错误(在这种情况下是 HTTP 响应/传入消息

_stream_readable.js:732
    throw new Error('Cannot switch to old mode now.');
          ^
Error: Cannot switch to old mode now.
    at emitDataEvents (_stream_readable.js:732:11)
    at IncomingMessage.Readable.pause (_stream_readable.js:723:3)

IncomingMessage 在正文中有 XML,我将其输入到sax-js处理程序中。然后,处理程序将输出写入通过管道传输到某个目的地的PassThrough流。阅读文档,我不确定我做错了什么readable.pause()readable.resume()是否有一些关于流动流的东西我错过了,或者我在下面实现了错误的模式?

function wrapStream(incoming) {
  var dest = new PassThrough();
  dest.setEncoding("utf8");

  dest.on("drain", function () {
    incoming.resume();
  });

  var saxStream = sax.createStream(true, {});

  saxStream.print = function() {
    var args = Array.prototype.slice.apply(arguments);

    /*
     * It's my understanding that if the dest stream is full,
     * we should pause the IncomingMessage
     * until the dest drains.
     */
    if (!dest.write.apply(dest, args)) {
      // throws error here.
      incoming.pause();
    }
  };

  /*
   * Attach handlers to saxStream here.
   * They use the print() function to write data to dest.
   */

  saxStream.on("end", function() {
    dest.end();
  });

  incoming.pipe(saxStream);

  return dest;
}

var output = fs.createWriteStream("/tmp/test.xml");

http.get(url, function(res) {
  var saxStream = wrapStream(res);

  saxStream.pipe(output);
});
4

1 回答 1

0

Streams 嵌入了一种名为背压的机制。当目标流忙时,将流传输到另一个流会自动处理。除非您使用旧流(节点 0.8),但我认为情况并非如此。

你应该只做req.pipe(saxStream).

于 2015-08-11T16:58:20.817 回答