@4qwerty7/syspipe 中文文档教程

发布于 3年前 浏览 30 项目主页 更新于 3年前

Syspipe

构建状态

node.js 模块访问操作系统本机管道实现。 欲了解更多信息,请查看“man 2 pipe”

此接口仅让您能够调用操作系统 pipe 系统调用。 它将返回一个带有输入和输出文件描述符的对象

$ node
> require('syspipe').pipe()
{ read: 13, write: 14 }

这对于大多数节点程序通常没有用,因为操作系统强加了管道的大小限制,如果你超过了它,你的程序将阻塞直到它被读取 - 和由于节点本质上是单线程的,因此您将无法从管道中读取数据并最终陷入僵局。

但是,如果您有一个本机线程扩展需要从 FD 读取数据,那么此扩展很有用,这是一种将数据从节点环境传递到扩展的简便方法。

Install

$ npm install syspipe

Usage

var fs = require('fs');
var syspipe = require('syspipe');

var buf = new Buffer(1024);
var read = 0;

var pipe = syspipe.pipe();
fs.writeSync(pipe.write, 'hello world');
read = fs.readSync(pipe.read, buf, 0, 1024, null);

console.log('Pipe read: ' + buf.slice(0, read).toString());

本机管道还可以用于以同步方式与从 node.js 执行的子进程进行交互。

例子:

var proc = require('child_process');
var fs = require('fs');
var syspipe = require('syspipe');

var buf = new Buffer(1024);
var read = 0;


var pipe = syspipe.pipe();
var options = { stdio: ['pipe', pipe.write, 'pipe'] };
var ls = proc.spawn('r2', ['-q0', '/bin/ls'], options);

var OUT = pipe.read;
var IN = ls.stdin['_handle'].fd;


read = fs.readSync(OUT, buf, 0, 1024, null);
console.log('[+] read ' + read + ' bytes');

fs.writeSync(IN, 'f\n');
read = fs.readSync(OUT, buf, 0, 1024, null);
var result = buf.slice(0, read-1);

console.log(result.toString());
console.log('[+] Read: ' + read + ' bytes');

fs.writeSync(IN, 'q\n');

Syspipe

Build Status

node.js module to access operating system native pipe implementation. For more info check "man 2 pipe"

This interface simply gives you the ability to call the operating system pipe system call. It will return an object with the input and output file descriptors

$ node
> require('syspipe').pipe()
{ read: 13, write: 14 }

This is generally not useful for most node program, as the pipe has a limited size imposed by the os, and if you exceed that your program will block until it is read -- and since node is single threaded by nature, you will not be able to read from the pipe and you end up deadlocked.

However where this extension is useful, is if you have a native threaded extension which requires data to be read from a FD, then is a handy way of passing data from the node envirionment to the extension.

Install

$ npm install syspipe

Usage

var fs = require('fs');
var syspipe = require('syspipe');

var buf = new Buffer(1024);
var read = 0;

var pipe = syspipe.pipe();
fs.writeSync(pipe.write, 'hello world');
read = fs.readSync(pipe.read, buf, 0, 1024, null);

console.log('Pipe read: ' + buf.slice(0, read).toString());

Native pipes can also be useful to interact with child processes executed from node.js in a sync manner.

Example:

var proc = require('child_process');
var fs = require('fs');
var syspipe = require('syspipe');

var buf = new Buffer(1024);
var read = 0;


var pipe = syspipe.pipe();
var options = { stdio: ['pipe', pipe.write, 'pipe'] };
var ls = proc.spawn('r2', ['-q0', '/bin/ls'], options);

var OUT = pipe.read;
var IN = ls.stdin['_handle'].fd;


read = fs.readSync(OUT, buf, 0, 1024, null);
console.log('[+] read ' + read + ' bytes');

fs.writeSync(IN, 'f\n');
read = fs.readSync(OUT, buf, 0, 1024, null);
var result = buf.slice(0, read-1);

console.log(result.toString());
console.log('[+] Read: ' + read + ' bytes');

fs.writeSync(IN, 'q\n');
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文