我正在向我的服务器发送base64字符串.在服务器上,我想创建一个可读的流,我将base64块推入其中然后转到可写流并写入文件.我的问题是只有第一个块被写入文件.我的猜测是因为我为每个块创建了一个新的缓冲区,这就是导致问题的原因,但如果我只发送字符串块而没有创建缓冲区,则图像文件已损坏.
var readable = new stream.Readable(); readable._read = function() {} req.on('data', function(data) { var dataText = data.toString(); var dataMatch = dataText.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/); var bufferData = null; if (dataMatch) { bufferData = new Buffer(dataMatch[2], 'base64') } else { bufferData = new Buffer(dataText, 'base64') } readable.push(bufferData) }) req.on('end', function() { readable.push(null); })
vkurchatkin.. 6
这并不像你想象的那么简单:
使用Transform
,而不是Readable
.您可以pipe
请求流转换,从而处理背压.
您不能使用正则表达式,因为您期望的文本可以在两个或更多块中被破坏.您可以尝试每次累积块和执行正则表达式,但如果流的格式不正确(即,不是 data
uri),您将最终缓冲整个请求并在兆字节长字符串上运行正常表达式很多次.
你不能采取任意的块,new Buffer(chunk, 'base64')
因为它本身可能无效.例如:new Buffer('AQID', 'base64')
收益率new Buffer([1, 2, 3])
,但Buffer.concat([new Buffer('AQ', 'base64'), new Buffer('ID', 'base64')])
收益率new Buffer([1, 32])
对于3问题,您可以使用其中一个可用模块(如base64-stream).这是一个例子:
var base64 = require('base64-stream'); var stream = require('stream'); var decoder = base64.decode(); var input = new stream.PassThrough(); var output = new stream.PassThrough(); input.pipe(decoder).pipe(output); output.on('data', function (data) { console.log(data); }); input.write('AQ'); input.write('ID');
您可以看到它缓冲输入并data
在足够到达时立即发出.
至于2问题,你需要实现简单的流解析器.作为一个想法:等待data:
字符串,然后缓冲块(如果你需要它们)直到;base64,
找到,然后管道base64-stream
.
这并不像你想象的那么简单:
使用Transform
,而不是Readable
.您可以pipe
请求流转换,从而处理背压.
您不能使用正则表达式,因为您期望的文本可以在两个或更多块中被破坏.您可以尝试每次累积块和执行正则表达式,但如果流的格式不正确(即,不是 data
uri),您将最终缓冲整个请求并在兆字节长字符串上运行正常表达式很多次.
你不能采取任意的块,new Buffer(chunk, 'base64')
因为它本身可能无效.例如:new Buffer('AQID', 'base64')
收益率new Buffer([1, 2, 3])
,但Buffer.concat([new Buffer('AQ', 'base64'), new Buffer('ID', 'base64')])
收益率new Buffer([1, 32])
对于3问题,您可以使用其中一个可用模块(如base64-stream).这是一个例子:
var base64 = require('base64-stream'); var stream = require('stream'); var decoder = base64.decode(); var input = new stream.PassThrough(); var output = new stream.PassThrough(); input.pipe(decoder).pipe(output); output.on('data', function (data) { console.log(data); }); input.write('AQ'); input.write('ID');
您可以看到它缓冲输入并data
在足够到达时立即发出.
至于2问题,你需要实现简单的流解析器.作为一个想法:等待data:
字符串,然后缓冲块(如果你需要它们)直到;base64,
找到,然后管道base64-stream
.