Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hq

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hq

stream middleware framework.

  • 0.0.4
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

hq

流处理中件间框架

npm version

Installation

npm i hq

Contents

  • use() 添加中间件;合并其它 hq 对象的中间件
  • mix() 挂载其它 hq 对象的中间件
  • exec() 重用流
  • pipe() 数据写入其它流

Usage

中间件

1. 添加中间件

use() 中间件是 Generator Function,ES6 的特性。

var h1 = hq();

h1.use(function *(next){
    console.log('h1-1');
    yield next;
});

// 一次添加多个
h1.use(function *(next){
	yield next;
	console.log('h1-2');
}, function *(next){
	console.log('h1-3');
});
2. 合并其它 hq 对象的中间件

use() 将目标 hq 对象的所有中间件合并到当前对象中。

var h2 = hq();

h2.use(h1);
3. 挂载其它 hq 对象的中间件

mix() 从挂载点开新分支,进入被挂载对象的中间件,先执行完挂载对象的所有中间件再返回主对象的挂载点继续往下执行。

var h1 = hq();
var h2 = hq();

h1.use(function *(next){
	console.log('h1-1');
	yield next;
});

h2.use(function *(next){
	yield next;
	console.log('h2-1');
}, function *(next){
	console.log('h2-2');
});

// mix instance `h2`
h1.mix(h2);

h1.use(function *(next){
	console.log('h1-2');
}); 

/**
* console:
* h1-1
* h2-2
* h2-1
* h1-2
*/

1. 流输入输出
var hq = require('hq');
var request = require('request');

var h1 = hq();

h1.use(function *(){
	// 流接收的数据会存在 this.buffer 里面,自行处理
	this.data = Buffer.concat(this.buffer).toString('utf-8');
    yield next;
});

request('http://example.com/').pipe(h1).pipe(some_writable_stream);

2. 重用流

exec() 默认 stream 只能使用一次,重用流可以实现类似多个流对它的写入,达到重用中件间的目的。

[url1, url2, url3].forEach(function(url){
	request(url).pipe(h1.exec());
})

错误处理

全局错误捕捉,一处定义就够了。

h1.on('error', function(err){
	console.log(err);
});

License

MIT

Keywords

FAQs

Package last updated on 28 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc