如何编写 Node.js 模块来处理传入的管道流 [英] How do I write a Node.js module to handle an incoming piped stream
问题描述
我正在尝试编写一个节点模块,它接受传入的管道二进制(或 base-64 编码)流,但坦率地说,我什至不知道从哪里开始.我在 Node 文档中看不到任何关于处理传入流的示例;我只看到使用它们的示例?
比如说我希望能够做到这一点:
var asset = new ProjectAsset('myFile', __dirname + '/image.jpg')var stream = fs.createReadStream(__dirname + '/image.jpg', { encoding: 'base64' }).pipe(asset)stream.on('完成', function() {完毕()})
我已经让 ProjectAsset
看起来像这样,但我不知道下一步该去哪里:
'使用严格'var stream = require('stream'),util = require('util')var ProjectAsset = function() {var self = thisObject.defineProperty(self, 'binaryData', {可配置:真,可写:真})stream.Stream.call(self)self.on('pipe', function(src) {//它发生在这里吗?我如何设置 self.binaryData?})回归自我}util.inherits(ProjectAsset, stream.Stream)module.exports = ProjectAssetmodule.exports.DEFAULT_FILE_NAME = '文件'
可以从 stream.Stream
继承并使其工作,但是基于 文档 我建议从 stream.Writable
.管道到 stream.Writable
你需要有 _write(chunk, encoding, done)
定义为处理管道.下面是一个例子:
var asset = new ProjectAsset('myFile', __dirname + '/image.jpg')var stream = fs.createReadStream(__dirname + '/image.jpg', { encoding: 'base64' }).pipe(asset)stream.on('完成', function() {控制台日志(资产.二进制数据);})
项目资产
'使用严格'var stream = require('stream'),util = require('util')var ProjectAsset = function() {var self = this自我数据self.binaryData = [];stream.Writable.call(self)self._write = 函数(块,编码,完成){//可以根据需要处理这些数据self.binaryData.push(chunk.toString())//处理完数据后调用完毕()}self.on('完成', function() {self.data = Buffer.concat(self.binaryData)})回归自我}util.inherits(ProjectAsset, stream.Writable)module.exports = ProjectAssetmodule.exports.DEFAULT_FILE_NAME = '文件'
如果您还希望从 stream
中读取数据,请查看从 stream.Duplex
并且还包括 _read(size)
方法.
还有 简化构造函数 api 如果您正在做一些更简单的事情.>
I'm trying to write a node module that accepts an incoming piped binary (or base-64-encoded) stream, but frankly I don't even know where to start. I can't see any examples in the Node docs about handling incoming streams; I only see examples on consuming them?
Say for example I want to be able to do this:
var asset = new ProjectAsset('myFile', __dirname + '/image.jpg')
var stream = fs.createReadStream(__dirname + '/image.jpg', { encoding: 'base64' }).pipe(asset)
stream.on('finish', function() {
done()
})
I've gotten ProjectAsset
looking like this, but I'm at a loss of where to go next:
'use strict'
var stream = require('stream'),
util = require('util')
var ProjectAsset = function() {
var self = this
Object.defineProperty(self, 'binaryData', {
configurable: true,
writable: true
})
stream.Stream.call(self)
self.on('pipe', function(src) {
// does it happen here? how do I set self.binaryData?
})
return self
}
util.inherits(ProjectAsset, stream.Stream)
module.exports = ProjectAsset
module.exports.DEFAULT_FILE_NAME = 'file'
It is possible to inherit from stream.Stream
and make it work, however based on what's available in the documentation I would suggest inheriting from stream.Writable
. Piping into a stream.Writable
you'll need to have _write(chunk, encoding, done)
defined to handle the piping. Here is an example:
var asset = new ProjectAsset('myFile', __dirname + '/image.jpg')
var stream = fs.createReadStream(__dirname + '/image.jpg', { encoding: 'base64' }).pipe(asset)
stream.on('finish', function() {
console.log(asset.binaryData);
})
Project Asset
'use strict'
var stream = require('stream'),
util = require('util')
var ProjectAsset = function() {
var self = this
self.data
self.binaryData = [];
stream.Writable.call(self)
self._write = function(chunk, encoding, done) {
// Can handle this data however you want
self.binaryData.push(chunk.toString())
// Call after processing data
done()
}
self.on('finish', function() {
self.data = Buffer.concat(self.binaryData)
})
return self
}
util.inherits(ProjectAsset, stream.Writable)
module.exports = ProjectAsset
module.exports.DEFAULT_FILE_NAME = 'file'
If you're looking to also read from the stream
, take a look at inheriting from stream.Duplex
and also including the _read(size)
method.
There's also the simplified constructors api if you're doing something simpler.
这篇关于如何编写 Node.js 模块来处理传入的管道流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!