如何创建允许写入的可读流?

时间:2015-09-22 12:59:36

标签: node.js

我正在使用的一个库需要可读的流,例如:

library(fs.createReadStream('/path/to/file'))

但我想从内存(Buffer)而不是文件传递文件。我怎么能这样做?使用节点0.10.x

var inMemoryFile = new Buffer('content', 'utf8');
library(/* readable stream of inMemoryFile? */)

解决:

var Readable = require('stream').Readable
var stream = new Readable()
stream.push(new Buffer('content', 'utf8'))
stream.push(null) // finish
library(stream)

1 个答案:

答案 0 :(得分:0)

正确答案是(谢谢@Matthew Bakaitis):

var Readable = require('stream').Readable
var stream = new Readable()
stream.push(new Buffer('content', 'utf8'))
stream.push(null) // finish
library(stream)
相关问题