{"id":512,"date":"2021-05-25T07:21:11","date_gmt":"2021-05-25T07:21:11","guid":{"rendered":"http:\/\/robinluo.top\/?p=512"},"modified":"2021-05-25T09:10:58","modified_gmt":"2021-05-25T09:10:58","slug":"nodejs-stream","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=512","title":{"rendered":"nodejs Stream"},"content":{"rendered":"\n<p>nodejs \u6d41\u5bf9\u8c61\u64cd\u4f5c \u8bb0\u5f55<\/p>\n\n\n\n<p>\u81ea\u5b9a\u4e49\u53ef\u5199\u6d41<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const {Writable} = require('stream')\n\nclass CounterStream extends Writable {\n    \n\n\n    constructor(options){\n        super(options)\n        this.counter = 0\n    }\n\n    _write(chunk , encoding, callback){\n        console.log(chunk.toString(\"utf-8\"))\n        this.counter+=chunk.length\n        callback.call()\n    }\n\n    \n}\n\n\nmodule.exports = CounterStream<\/code><\/pre>\n\n\n\n<p>\u5173\u952e\u5728\u4e8e\u7ee7\u627fstream.Writeable  \u5e76\u4e14\u91cd\u5199 _write(chunk , encoding, callback)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u81ea\u5b9a\u4e49\u53ef\u8bfb\u6d41<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const {Readable} = require('stream')\n\nclass MyReadableStream extends Readable {\n    \n\n\n    constructor(options){\n        super(options)\n        this.isFinished = false\n    \n    }\n\n    _read(size){\n        \/\/\u5f85\u5904\u7406\u6570\u636e\u653e\u5728this._buffer\u4e2d\n        if(!this.isFinished){\n            this.push(\"abc123\")\n            this.isFinished = true\n        }else{\n            this.push(null)\n        }\n    }\n}\n\n\nmodule.exports = MyReadableStream<\/code><\/pre>\n\n\n\n<p>\u5173\u952e\u5728\u4e8e\u7ee7\u627fstream.Readable \u5e76\u4e14\u91cd\u5199 _read(size)<\/p>\n\n\n\n<p>_read \u65b9\u6cd5\u4e2d\u8981\u628a\u6570\u636e\u653e\u5230this.push\u7684\u65b9\u6cd5\u4e2d \u4e14 \u6ca1\u6709\u6570\u636e\u8bfb\u53d6\u65f6 this.push(null) \u7ed3\u675f\u8bfb\u53d6<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53ef\u4ee5\u4eceReadable.pipe(Writable) \u4f7f\u7528\u7ba1\u9053\u7684\u65b9\u5f0f \u4ece\u53ef\u8bfb\u6d41\u8bfb\u53d6\u6570\u636e pipe \u5230\u53ef\u5199\u6d41\u4e2d<\/p>\n\n\n\n<p>\u6ce8\u610f \u8be5\u65b9\u6cd5\u4e3a\u5f02\u6b65\u65b9\u6cd5<\/p>\n\n\n\n<p>\u7279\u6b8a\u7684\u6d41 socket \u4e3a\u53cc\u5411\u6d41 \u53ef\u8bfb\u53ef\u5199<\/p>\n\n\n\n<p>process.stdin \u6807\u51c6\u8f93\u5165\u6d41<\/p>\n\n\n\n<p>process.stdout \u6807\u51c6\u8f93\u51fa\u6d41<\/p>\n\n\n\n<p>process.stderr \u6807\u51c6\u5f02\u5e38\u6d41<\/p>\n\n\n\n<p>\u628a\u6807\u51c6\u8f93\u5165\u6d41\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u6d41<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const stdin = require('process').stdin\nconst stdout = require('process').stdout\nconst stderr = require('process').stderr\n\nstdin.pipe(stdout)<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u540e \u4e3b\u7ebf\u7a0b\u963b\u585e \u8bfb\u53d6\u6807\u51c6\u8f93\u5165\u6d41 \u8bfb\u53d6\u5230\u6570\u636e\u540e \u5728 \u6807\u51c6\u8f93\u51fa\u6d41\u8f93\u51fa<\/p>\n\n\n\n<p>\u8f93\u5165\u6d41\u4e8b\u4ef6\u6709\u4ee5\u4e0b\u51e0\u4e2a<\/p>\n\n\n\n<ul><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_close_1\">&#8216;close&#8217; \u4e8b\u4ef6<\/a><\/li><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_data\">&#8216;data&#8217; \u4e8b\u4ef6<\/a><\/li><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_end\">&#8216;end&#8217; \u4e8b\u4ef6<\/a><\/li><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_error_1\">&#8216;error&#8217; \u4e8b\u4ef6<\/a><\/li><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_pause\">&#8216;pause&#8217; \u4e8b\u4ef6<\/a><\/li><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_readable\">&#8216;readable&#8217; \u4e8b\u4ef6<\/a><\/li><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_resume\">&#8216;resume&#8217; \u4e8b\u4ef6<\/a><\/li><\/ul>\n\n\n\n<p>\u8f93\u51fa\u6d41\u4e8b\u4ef6<\/p>\n\n\n\n<ul><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_close\">&#8216;close&#8217; \u4e8b\u4ef6<\/a><\/li><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_drain\">&#8216;drain&#8217; \u4e8b\u4ef6<\/a><\/li><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_error\">&#8216;error&#8217; \u4e8b\u4ef6<\/a><\/li><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_finish\">&#8216;finish&#8217; \u4e8b\u4ef6<\/a><\/li><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_pipe\">&#8216;pipe&#8217; \u4e8b\u4ef6<\/a><\/li><li><a href=\"http:\/\/nodejs.cn\/api\/stream.html#stream_event_unpipe\">&#8216;unpipe&#8217; \u4e8b\u4ef6<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>nodejs \u6d41\u5bf9\u8c61\u64cd\u4f5c \u8bb0\u5f55 \u81ea\u5b9a\u4e49\u53ef\u5199\u6d41 \u5173\u952e\u5728\u4e8e\u7ee7\u627fstream.Writeable \u5e76\u4e14\u91cd\u5199 _write(chunk , encoding, callback) \u81ea\u5b9a\u4e49\u53ef\u8bfb\u6d41 \u5173\u952e\u5728\u4e8e\u7ee7\u627fstream.Readable \u5e76\u4e14\u91cd\u5199 _read(size) _read \u65b9\u6cd5\u4e2d\u8981\u628a\u6570\u636e\u653e\u5230this.push\u7684\u65b9\u6cd5\u4e2d \u4e14 \u6ca1\u6709\u6570\u636e\u8bfb\u53d6\u65f6 this.push(null) \u7ed3\u675f\u8bfb\u53d6 \u53ef\u4ee5\u4eceReadable.pipe(Writable) \u4f7f\u7528\u7ba1\u9053\u7684\u65b9\u5f0f \u4ece\u53ef\u8bfb\u6d41\u8bfb\u53d6\u6570\u636e pipe \u5230\u53ef\u5199\u6d41\u4e2d \u6ce8\u610f \u8be5\u65b9\u6cd5\u4e3a\u5f02\u6b65\u65b9\u6cd5 \u7279\u6b8a\u7684\u6d41 socket \u4e3a\u53cc\u5411\u6d41 \u53ef\u8bfb\u53ef\u5199 process.stdin \u6807\u51c6\u8f93\u5165\u6d41 process.stdout \u6807\u51c6\u8f93\u51fa\u6d41 process.stderr \u6807\u51c6\u5f02\u5e38\u6d41 \u628a\u6807\u51c6\u8f93\u5165\u6d41\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u6d41 \u6267\u884c\u540e \u4e3b\u7ebf\u7a0b\u963b\u585e \u8bfb\u53d6\u6807\u51c6\u8f93\u5165\u6d41 \u8bfb\u53d6\u5230\u6570\u636e\u540e \u5728 \u6807\u51c6\u8f93\u51fa\u6d41\u8f93\u51fa \u8f93\u5165\u6d41\u4e8b\u4ef6\u6709\u4ee5\u4e0b\u51e0\u4e2a &#8216;close&#8217; \u4e8b\u4ef6 &#8216;data&#8217; \u4e8b\u4ef6 &#8216;end&#8217; \u4e8b\u4ef6 &#8216;error&#8217; \u4e8b\u4ef6 &#8216;pause&#8217; \u4e8b\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[83],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/512"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=512"}],"version-history":[{"count":11,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/512\/revisions"}],"predecessor-version":[{"id":524,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/512\/revisions\/524"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}