我正在使用一个库,ya-csv,期望文件或流作为输入,但我有一个字符串。

如何将该字符串转换为节点中的流?


当前回答

JavaScript是鸭子类型的,所以如果你只是复制一个可读流的API,它会工作得很好。事实上,你可能不能实现这些方法中的大多数,或者只是把它们作为存根;您需要实现的只是库使用的内容。您也可以使用Node预先构建的EventEmitter类来处理事件,因此您不必自己实现addListener等。

下面是如何在CoffeeScript中实现它:

class StringStream extends require('events').EventEmitter
  constructor: (@string) -> super()

  readable: true
  writable: false

  setEncoding: -> throw 'not implemented'
  pause: ->    # nothing to do
  resume: ->   # nothing to do
  destroy: ->  # nothing to do
  pipe: -> throw 'not implemented'

  send: ->
    @emit 'data', @string
    @emit 'end'

然后你可以这样使用它:

stream = new StringStream someString
doSomethingWith stream
stream.send()

其他回答

JavaScript是鸭子类型的,所以如果你只是复制一个可读流的API,它会工作得很好。事实上,你可能不能实现这些方法中的大多数,或者只是把它们作为存根;您需要实现的只是库使用的内容。您也可以使用Node预先构建的EventEmitter类来处理事件,因此您不必自己实现addListener等。

下面是如何在CoffeeScript中实现它:

class StringStream extends require('events').EventEmitter
  constructor: (@string) -> super()

  readable: true
  writable: false

  setEncoding: -> throw 'not implemented'
  pause: ->    # nothing to do
  resume: ->   # nothing to do
  destroy: ->  # nothing to do
  pipe: -> throw 'not implemented'

  send: ->
    @emit 'data', @string
    @emit 'end'

然后你可以这样使用它:

stream = new StringStream someString
doSomethingWith stream
stream.send()

只需创建一个流模块的新实例,并根据您的需要定制它:

var Stream = require('stream');
var stream = new Stream();

stream.pipe = function(dest) {
  dest.write('your string');
  return dest;
};

stream.pipe(process.stdout); // in this case the terminal, change to ya-csv

or

var Stream = require('stream');
var stream = new Stream();

stream.on('data', function(data) {
  process.stdout.write(data); // change process.stdout to ya-csv
});

stream.emit('data', 'this is my string');

不要用Jo Liss的简历回答。这在大多数情况下都是可行的,但在我的案例中,它却让我花了4或5个小时去寻找漏洞。不需要第三方模块来做这件事。

新回答:

var Readable = require('stream').Readable

var s = new Readable()
s.push('beep')    // the string you want
s.push(null)      // indicates end-of-file basically - the end of the stream

这应该是一个完全兼容的可读流。有关如何正确使用流的更多信息,请参阅这里。

旧的回答: 只需要使用本地PassThrough流:

var stream = require("stream")
var a = new stream.PassThrough()
a.write("your string")
a.end()

a.pipe(process.stdout) // piping will work as normal
/*stream.on('data', function(x) {
   // using the 'data' event works too
   console.log('data '+x)
})*/
/*setTimeout(function() {
   // you can even pipe after the scheduler has had time to do other things
   a.pipe(process.stdout) 
},100)*/

a.on('end', function() {
    console.log('ended') // the end event will be called properly
})

注意,'close'事件不会被触发(流接口不需要)。

编辑:Garth的答案可能更好。

我以前的答案文本保存在下面。


要将字符串转换为流,可以使用pause through流:

through().pause().queue('your string').end()

例子:

var through = require('through')

// Create a paused stream and buffer some data into it:
var stream = through().pause().queue('your string').end()

// Pass stream around:
callback(null, stream)

// Now that a consumer has attached, remember to resume the stream:
stream.resume()

我厌倦了每六个月重新学习一次,所以我发布了一个npm模块来抽象实现细节:

https://www.npmjs.com/package/streamify-string

这是该模块的核心:

const Readable = require('stream').Readable;
const util     = require('util');

function Streamify(str, options) {

  if (! (this instanceof Streamify)) {
    return new Streamify(str, options);
  }

  Readable.call(this, options);
  this.str = str;
}

util.inherits(Streamify, Readable);

Streamify.prototype._read = function (size) {

  var chunk = this.str.slice(0, size);

  if (chunk) {
    this.str = this.str.slice(size);
    this.push(chunk);
  }

  else {
    this.push(null);
  }

};

module.exports = Streamify;

STR是在调用时必须传递给构造函数的字符串,并将由流作为数据输出。根据文档,选项是可以传递给流的典型选项。

根据Travis CI,它应该与大多数版本的节点兼容。