Node.js Stream - 实战篇(node.js 10实战)
wptr33 2025-05-05 19:04 21 浏览
本文转自 “美团点评技术团队”
http://tech.meituan.com/stream-in-action.html
背景
前面两篇(基础篇和进阶篇)主要介绍流的基本用法和原理,本篇从应用的角度,介绍如何使用管道进行程序设计,主要内容包括:
管道的概念
Browserify的管道设计
Gulp的管道设计
两种管道设计模式比较
实例
Pipeline
所谓“管道”,指的是通过a.pipe(b)
的形式连接起来的多个Stream对象的组合。
假如现在有两个Transform
:bold
和red
,分别可将文本流中某些关键字加粗和飘红。
可以按下面的方式对文本同时加粗和飘红:
// source: 输入流// dest: 输出目的地source.pipe(bold).pipe(red).pipe(dest)
bold.pipe(red)
便可以看作一个管道,输入流先后经过bold
和red
的变换再输出。
但如果这种加粗且飘红的功能的应用场景很广,我们期望的使用方式是:
// source: 输入流// dest: 输出目的地// pipeline: 加粗且飘红source.pipe(pipeline).pipe(dest)
此时,pipeline
封装了bold.pipe(red)
,从逻辑上来讲,也称其为管道。
其实现可简化为:
var pipeline = new Duplex()var streams = pipeline._streams = [bold, red]// 底层写逻辑:将数据写入管道的第一个Stream,即boldpipeline._write = function (buf, enc, next) {
streams[0].write(buf, enc, next)
}// 底层读逻辑:从管道的最后一个Stream(即red)中读取数据pipeline._read = function () { var buf var reads = 0
var r = streams[streams.length - 1] // 将缓存读空
while ((buf = r.read()) !== null) {
pipeline.push(buf)
reads++
} if (reads === 0) { // 缓存本来为空,则等待新数据的到来
r.once('readable', function () {
pipeline._read()
})
}
}// 将各个Stream组合起来(此处等同于`bold.pipe(red)`)streams.reduce(function (r, next) {
r.pipe(next) return next
})
往pipeline
写数据时,数据直接写入bold
,再流向red
,最后从pipeline
读数据时再从red
中读出。
如果需要在中间新加一个underline
的Stream,可以:
pipeline._streams.splice(1, 0, underline)
bold.unpipe(red)
bold.pipe(underline).pipe(red)
如果要将red
替换成green
,可以:
// 删除redpipeline._streams.pop()
bold.unpipe(red)// 添加
greenpipeline._streams.push(green)
bold.pipe(green)
可见,这种管道的各个环节是可以修改的。
stream-splicer对上述逻辑进行了进一步封装,提供splice
、push
、pop
等方法,使得pipeline
可以像数组那样被修改:
var splicer = require('stream-splicer')var pipeline = splicer([bold, red])// 在中间添加underlinepipeline.splice(1, 0, underline)// 删除redpipeline.pop()// 添加greenpipeline.push(green)
labeled-stream-splicer在此基础上又添加了使用名字替代下标进行操作的功能:
var splicer = require('labeled-stream-splicer')var pipeline = splicer([ 'bold', bold, 'red', red,
])// 在`red`前添加underlinepipeline.splice('red', 0, underline)// 删除`bold`pipeline.splice('bold', 1)
由于pipeline
本身与其各个环节一样,也是一个Stream对象,因此可以嵌套:
var splicer = require('labeled-stream-splicer')var pipeline = splicer([ 'style', [ bold, red ], 'insert', [ comma ],
])
pipeline.get('style') // 取得管道:[bold, red]
.splice(1, 0, underline) // 添加underline
Browserify
Browserify的功能介绍可见
substack/browserify-handbook,其核心逻辑的实现在于管道的设计:
var splicer = require('labeled-stream-splicer')var pipeline = splicer.obj([ // 记录输入管道的数据,重建管道时直接将记录的数据写入。
// 用于像watch时需要多次打包的情况
'record', [ this._recorder() ], // 依赖解析,预处理
'deps', [ this._mdeps ], // 处理JSON文件
'json', [ this._json() ], // 删除文件前面的BOM
'unbom', [ this._unbom() ], // 删除文件前面的`#!`行
'unshebang', [ this._unshebang() ], // 语法检查
'syntax', [ this._syntax() ], // 排序,以确保打包结果的稳定性
'sort', [ depsSort(dopts) ], // 对拥有同样内容的模块去重
'dedupe', [ this._dedupe() ], // 将id从文件路径转换成数字,避免暴露系统路径信息
'label', [ this._label(opts) ], // 为每个模块触发一次dep事件
'emit-deps', [ this._emitDeps() ], 'debug', [ this._debug(opts) ], // 将模块打包
'pack', [ this._bpack ], // 更多自定义的处理
'wrap', [],
])
每个模块用row
表示,定义如下:
{ // 模块的唯一标识
id: id, // 模块对应的文件路径
file: '/path/to/file', // 模块内容
source: '', // 模块的依赖
deps: { // `require(expr)`
expr: id,
}
}
在wrap
阶段前,所有的阶段都处理这样的对象流,且除pack
外,都输出这样的流。
有的补充row
中的一些信息,有的则对这些信息做一些变换,有的只是读取和输出。
一般row
中的source
、deps
内容都是在deps
阶段解析出来的。
下面提供一个修改Browserify管道的函数。
var Transform = require('stream').Transform// 创建Transform对象function through(write, end) { return Transform({
transform: write,
flush: end,
})
}// `b`为Browserify实例// 该插件可打印出打包时间function log(b) { // watch时需要重新打包,整个pipeline会被重建,所以也要重新修改
b.on('reset', reset) // 修改当前pipeline
reset() function reset () { var time = null
var bytes = 0
b.pipeline.get('record').on('end', function () { // 以record阶段结束为起始时刻
time = Date.now()
}) // `wrap`是最后一个阶段,在其后添加记录结束时刻的Transform
b.pipeline.get('wrap').push(through(write, end)) function write (buf, enc, next) { // 累计大小
bytes += buf.length this.push(buf)
next()
} function end () { // 打包时间
var delta = Date.now() - time
b.emit('time', delta)
b.emit('bytes', bytes)
b.emit('log', bytes + ' bytes written ('
+ (delta / 1000).toFixed(2) + ' seconds)'
) this.push(null)
}
}
}var fs = require('fs')var browserify = require('browserify')var b = browserify(opts)// 应用插件b.plugin(log)
b.bundle().pipe(fs.createWriteStream('bundle.js'))
事实上,这里的b.plugin(log)
就是直接执行了log(b)
。
在插件中,可以修改b.pipeline
中的任何一个环节。
因此,Browserify本身只保留了必要的功能,其它都由插件去实现,如watchify、factor-bundle等。
除了了上述的插件机制外,Browserify还有一套Transform机制,即通过b.transform(transform)
可以新增一些文件内容预处理的Transform。
预处理是发生在deps
阶段的,当模块文件内容被读出来时,会经过这些Transform处理,然后才做依赖解析,如babelify、envify。
Gulp
Gulp的核心逻辑分成两块:任务调度与文件处理。
任务调度是基于orchestrator,而文件处理则是基于vinyl-fs。
类似于Browserify提供的模块定义(用row
表示),vinyl-fs也提供了文件定义(vinyl对象)。
Browserify的管道处理的是row
流,Gulp管道处理vinyl流:
gulp.task('scripts', ['clean'], function() { // Minify and copy all JavaScript (except vendor scripts)
// with sourcemaps all the way down
return gulp.src(paths.scripts)
.pipe(sourcemaps.init())
.pipe(coffee())
.pipe(uglify())
.pipe(concat('all.min.js'))
.pipe(sourcemaps.write())
.pipe(gulp.dest('build/js'));
});
任务中创建的管道起始于gulp.src
,终止于gulp.dest
,中间有若干其它的Transform(插件)。
如果与Browserify的管道对比,可以发现Browserify是确定了一条具有完整功能的管道,而Gulp本身只提供了创建vinyl流和将vinyl流写入磁盘的工具,管道中间经历什么全由用户决定。
这是因为任务中做什么,是没有任何限制的,文件处理也只是常见的情况,并非一定要用gulp.src
与gulp.dest
。
两种模式比较
Browserify与Gulp都借助管道的概念来实现插件机制。
Browserify定义了模块的数据结构,提供了默认的管道以处理这样的数据流,而插件可用来修改管道结构,以定制处理行为。
Gulp虽也定义了文件的数据结构,但只提供产生、消耗这种数据流的接口,完全由用户通过插件去构造处理管道。
当明确具体的处理需求时,可以像Browserify那样,构造一个基本的处理管道,以提供插件机制。
如果需要的是实现任意功能的管道,可以如Gulp那样,只提供数据流的抽象。
实例
本节中实现一个针对Git仓库自动生成changelog的工具,完整代码见ezchangelog。
ezchangelog的输入为git log
生成的文本流,输出默认为markdown格式的文本流,但可以修改为任意的自定义格式。
输入示意:
commit 9c5829ce45567bedccda9beb7f5de17574ea9437
Author: zoubin <zoubin04@gmail.com>
Date: Sat Nov 7 18:42:35 2015 +0800
CHANGELOG
commit 3bf9055b732cc23a9c14f295ff91f48aed5ef31a
Author: zoubin <zoubin04@gmail.com>
Date: Sat Nov 7 18:41:37 2015 +0800
4.0.3
commit 87abe8e12374079f73fc85c432604642059806ae
Author: zoubin <zoubin04@gmail.com>
Date: Sat Nov 7 18:41:32 2015 +0800
fix readme
add more tests
输出示意:
* [[`9c5829c`](https://github.com/zoubin/ezchangelog/commit/9c5829c)] CHANGELOG## [v4.0.3](https://github.com/zoubin/ezchangelog/commit/3bf9055) (2015-11-07)* [[`87abe8e`](https://github.com/zoubin/ezchangelog/commit/87abe8e)] fix readme add more tests
其实需要的是这样一个pipeline
:
source.pipe(pipeline).pipe(dest)
可以分为两个阶段:
parse:从输入文本流中解析出commit信息
format: 将commit流变换为文本流
默认的情况下,要想得到示例中的markdown,需要解析出每个commit的sha1、日期、消息、是否为tag。
定义commit的格式如下:
{
commit: { // commit sha1
long: '3bf9055b732cc23a9c14f295ff91f48aed5ef31a',
short: '3bf9055',
},
committer: { // commit date
date: new Date('Sat Nov 7 18:41:37 2015 +0800'),
}, // raw message lines
messages: ['', ' 4.0.3', ''], // raw headers before the messages
headers: [
['Author', 'zoubin <zoubin04@gmail.com>'],
['Date', 'Sat Nov 7 18:41:37 2015 +0800'],
], // the first non-empty message line
subject: '4.0.3', // other message lines
body: '', // git tag
tag: 'v4.0.3', // link to the commit. opts.baseUrl should be specified.
url: 'https://github.com/zoubin/ezchangelog/commit/3bf9055',
}
于是有:
var splicer = require('labeled-stream-splicer')
pipeline = splicer.obj([ 'parse', [ // 按行分隔
'split', split(), // 生成commit对象,解析出sha1和日期
'commit', commit(), // 解析出tag
'tag', tag(), // 解析出url
'url', url({ baseUrl: opts.baseUrl }),
], 'format', [ // 将commit组合成markdown文本
'markdownify', markdownify(),
],
])
至此,基本功能已经实现。
现在将其封装并提供插件机制。
function Changelog(opts) {
opts = opts || {} this._options = opts // 创建pipeline
this.pipeline = splicer.obj([ 'parse', [ 'split', split(), 'commit', commit(), 'tag', tag(), 'url', url({ baseUrl: opts.baseUrl }),
], 'format', [ 'markdownify', markdownify(),
],
]) // 应用插件
;[].concat(opts.plugin).filter(Boolean).forEach(function (p) { this.plugin(p)
}, this)
}
Changelog.prototype.plugin = function (p, opts) { if (Array.isArray(p)) {
opts = p[1]
p = p[0]
} // 执行插件函数,修改pipeline
p(this, opts) return this}
上面的实现提供了两种方式来应用插件。
一种是通过配置传入,另一种是创建实例后再调用plugin
方法,本质一样。
为了使用方便,还可以简单封装一下。
function changelog(opts) { return new Changelog(opts).pipeline
}
这样,就可以如下方式使用:
source.pipe(changelog()).pipe(dest)
这个已经非常接近我们的预期了。
现在来开发一个插件,修改默认的渲染方式。
var through = require('through2')function customFormatter(c) { // c是`Changelog`实例
// 添加解析author的transform
c.pipeline.get('parse').push(through.obj(function (ci, enc, next) { // parse the author name from: 'zoubin <zoubin04@gmail.com>'
ci.committer.author = ci.headers[0][1].split(/\s+/)[0]
next(null, ci)
})) // 替换原有的渲染
c.pipeline.get('format').splice('markdownify', 1, through.obj(function (ci, enc, next) { var sha1 = ci.commit.short
sha1 = '[`' + sha1 + '`](' + c._options.baseUrl + sha1 + ')'
var date = ci.committer.date.toISOString().slice(0, 10)
next(null, '* ' + sha1 + ' ' + date + ' @' + ci.committer.author + '\n')
}))
}
source
.pipe(changelog({
baseUrl: 'https://github.com/zoubin/ezchangelog/commit/',
plugin: [customFormatter],
}))
.pipe(dest)
同样的输入,输出将会是:
* [`9c5829c`](https://github.com/zoubin/ezchangelog/commit/9c5829c) 2015-11-07 @zoubin* [`3bf9055`](https://github.com/zoubin/ezchangelog/commit/3bf9055) 2015-11-07 @zoubin* [`87abe8e`](https://github.com/zoubin/ezchangelog/commit/87abe8e) 2015-11-07 @zoubin
可以看出,通过创建可修改的管道,ezchangelog保持了本身逻辑的单一性,同时又提供了强大的自定义空间。
参考文献
GitHub,substack/browserify-handbook
GitHub,zoubin/streamify-your-node-program
查看更多技术类文章,请关注微信公众号:美团点评技术团队。
相关推荐
- MySql系列-常用命令
-
本篇是对...
- Record.ToTable 格式转换
-
本期案例对表格格式进行转换,前后转换效果如下:解题套路1.Record.ToTable解题思路:思路就是构造可以透视的样式。使用Record.ToTable对行记录进行转换,获得包含两列的表,首行可以...
- Table.Group 按时期累计计算唯一值
-
本期案例是根据不同id进行汇总统计:组内,相同日期的为一组,统计“from”、“to”中的非重复个数;连续日期的,统计累计数。前后转换效果如下:解题套路1.Table.Group...
- MySQL 9.1正式发布,有哪些值得关注的新特性?
-
MySQL创新版9.1.0于2024年10月15日正式发布。此外,MySQL8.0.40及8.4.3补丁版本也同时发布。8.4.3是目前MySQL的LTS长期支持版本,该版本中将不会增加新的功能与特性...
- SQL基本语句练习(基础版)
-
最近在学习SQL基本语句的练习,在此分享一下笔者做过的练习以及个人的解决教程:首先是基本练习表格的搭建,具体内容如下表所示:...
- SQL 从入门到精通:全面掌握数据库操作
-
学习SQL(StructuredQueryLanguage)是掌握数据库操作的关键步骤。SQL是一种用于管理和处理关系型数据库的标准语言,广泛应用于数据检索、插入、更新和删除等操作。以下是一些...
- ClickHouse学习笔记四ClickHouse基础语法
-
前言这里我们介绍ClickHouse的基本语法,使用环境是腾讯云的ClickHouse。默认情况下,ClickHouse在进行集群纬度执行建表等DDL操作时需要手动添加ONCLUSTERX...
- 程序员总结的常用sql语句大全
-
多年经验程序员总结的我们一般需要使用的sql语句,赶快收藏起来,方便以后使用。以下是一些常用的SQL语句及其用法:一、数据定义语言(DDL)创建库CREATEDATABASE:创建一个新数据库。...
- PQ03-分组求和
-
目标已知:销售清单求:每个销售员的销量合计方法数据准备...
- 好荐:一款数据库元数据管理平台工具
-
“元数据”的定义在不同的软件、项目、工程的定义范围都不太一样。本文这里指的是软件项目开发使用的数据库表结构信息。我今天介绍的这个开源项目叫Databasir,它是一个面向团队的关系型数据库模型文档管理...
- MySQL 8.0 SQL优化黑科技,面试官都不一定知道!
-
前言提到SQL优化,大多数人想到的还是那些经典套路:建索引、避免全表扫描、优化JOIN顺序…这些确实是基础,但如果你还停留在MySQL5.7时代的优化思维,那就out了。MySQL8.0已经发布好...
- MySQL数据库深度优化指南:从基础到架构层面的20个关键策略
-
一、核心性能优化原则数据最小化原则...
- 动物源性食品中兽药残留的检测——喹啉类药物残留
-
喹啉类药物(quinoxaline)是具有喹啉-N1,N4-二氧化物基本结构的一类化学合成的动物专用药,具有广谱抗菌、提高饲料转化率和促生长作用。1965年德国拜耳公司以邻硝基苯胺为原料合成喹乙醇(o...
- 适合普通开发者和产品经理的PHP应用模板开发AI的SaaS应用框架
-
简单到傻!Liang_SaaS适合普通开发者和产品经理的PHP应用模板开发AI的SaaS应用框架,利用Php开发AI的SaaS应用框架,是一个强大的内容管理仪表板模板,基于Bootstrap和...
- Power Query 交错合并表格的方法
-
两张表格合并成一张表格,需要交错排列,表1取一行,表2取一行,这样排列在一起:前提是两张表的行数相同,内容排列顺序相同:我们来看两张表:表1:12列10行表2:11列10行行数相同列数不同,我们在数据...
- 一周热门
-
-
因果推断Matching方式实现代码 因果推断模型
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)