@4qwerty7/syspipe 中文文档教程
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
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');
你可能也喜欢
- 2co-react 中文文档教程
- @01ht/ht-toolbar-signin 中文文档教程
- @36node/weather-sdk 中文文档教程
- @7isys/loopback-component-passport 中文文档教程
- @aarhus-university/au-designsystem-delphinus 中文文档教程
- @abdulghani/logreducer 中文文档教程
- @abecodes/hasher64 中文文档教程
- @abstractball/json-server 中文文档教程
- @acai/interfaces 中文文档教程
- @acentswap/utils-heini 中文文档教程