gpt4 book ai didi

javascript - 如何读取流 .pipe(myfunction())

转载 作者:行者123 更新时间:2023-11-29 21:36:13 36 4
gpt4 key购买 nike

如何读取流.pipe(myfunction())

我试了,但是报错。 gulp.src('./userdata.json').pipe()如何读取流?我不知道它是怎么做的。

gulpfile.js

   var upmodul = require("modul-json");
//......
return gulp.src('./userdata.json')
.pipe(upmodul());
......//

node_modules/modul-json/index.js

'use strict';
var Stream = require('stream');


var loger = function () {

var readable = new Stream.Readable({
read: function (n) {

this.push("ll");
}
});


}

module.exports = loger;

错误

    [00:19:39] TypeError: Cannot read property 'on' of undefined
at DestroyableTransform.Readable.pipe (E:\Developers\WebDeveloper\OpenServer
-WebProg\domains\progectapi2\node_modules\vinyl-fs\node_modules\readable-stream\
lib\_stream_readable.js:516:7)
at Gulp.<anonymous> (E:\Developers\WebDeveloper\OpenServer-WebProg\domains\p
rogectapi2\gulpfile.js:159:9)
at module.exports (E:\Developers\WebDeveloper\OpenServer-WebProg\domains\pro
gectapi2\node_modules\orchestrator\lib\runTask.js:34:7)
at Gulp.Orchestrator._runTask (E:\Developers\WebDeveloper\OpenServer-WebProg
\domains\progectapi2\node_modules\orchestrator\index.js:273:3)
at Gulp.Orchestrator._runStep (E:\Developers\WebDeveloper\OpenServer-WebProg
\domains\progectapi2\node_modules\orchestrator\index.js:214:10)
at Gulp.Orchestrator.start (E:\Developers\WebDeveloper\OpenServer-WebProg\do
mains\progectapi2\node_modules\orchestrator\index.js:134:8)
at C:\Users\Tiki
\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:129:20
at nextTickCallbackWith0Args (node.js:433:9)
at process._tickCallback (node.js:362:13)
at Function.Module.runMain (module.js:432:11)

最佳答案

gulp 文档有一些关于 building a plugin 的信息这可能对你有用。只是该页面中的一个示例讨论了转换流。

All gulp plugins essentially boil down to this:

var Transform = require('stream').Transform;

module.exports = function() {
// Monkey patch Transform or create your own subclass,
// implementing `_transform()` and optionally `_flush()`
var transformStream = new Transform({objectMode: true});
/**
* @param {Buffer|string} file
* @param {string=} encoding - ignored if file contains a Buffer
* @param {function(Error, object)} callback - Call this function (optionally with an
* error argument and data) when you are done processing the supplied chunk.
*/
transformStream._transform = function(file, encoding, callback) {
var error = null,
output = doSomethingWithTheFile(file);
callback(error, output);
});

return transformStream;
};

关于javascript - 如何读取流 .pipe(myfunction()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34802275/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com