Add sys.pump
This commit is contained in:
parent
20905d9d62
commit
f62979da6b
@ -837,6 +837,14 @@ Example of inspecting all properties of the `sys` object:
|
|||||||
sys.puts(sys.inspect(sys, true, null));
|
sys.puts(sys.inspect(sys, true, null));
|
||||||
|
|
||||||
|
|
||||||
|
### sys.pump(readableStream, writeableStream, [callback])
|
||||||
|
|
||||||
|
Experimental
|
||||||
|
|
||||||
|
Read the data from `readableStream` and send it to the `writableStream`.
|
||||||
|
When `writeableStream.write(data)` returns `false` `readableStream` will be
|
||||||
|
paused until the `drain` event occurs on the `writableStream`. `callback` is
|
||||||
|
called when `writableStream` is closed.
|
||||||
|
|
||||||
|
|
||||||
## Timers
|
## Timers
|
||||||
|
18
lib/sys.js
18
lib/sys.js
@ -283,6 +283,24 @@ exports.exec = function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
exports.pump = function (readStream, writeStream, callback) {
|
||||||
|
readStream.addListener("data", function (chunk) {
|
||||||
|
if (writeStream.write(chunk) === false) readStream.pause();
|
||||||
|
});
|
||||||
|
|
||||||
|
writeStream.addListener("drain", function () {
|
||||||
|
readStream.resume();
|
||||||
|
});
|
||||||
|
|
||||||
|
readStream.addListener("end", function () {
|
||||||
|
writeStream.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
readStream.addListener("close", function () {
|
||||||
|
if (callback) callback();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inherit the prototype methods from one constructor into another.
|
* Inherit the prototype methods from one constructor into another.
|
||||||
*
|
*
|
||||||
|
43
test/simple/test-pump-file2tcp.js
Normal file
43
test/simple/test-pump-file2tcp.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
require("../common");
|
||||||
|
net = require("net");
|
||||||
|
fs = require("fs");
|
||||||
|
sys = require("sys");
|
||||||
|
path = require("path");
|
||||||
|
fn = path.join(fixturesDir, 'elipses.txt');
|
||||||
|
|
||||||
|
expected = fs.readFileSync(fn, 'utf8');
|
||||||
|
|
||||||
|
server = net.createServer(function (stream) {
|
||||||
|
error('pump!');
|
||||||
|
sys.pump(fs.createReadStream(fn), stream, function () {
|
||||||
|
error('server stream close');
|
||||||
|
error('server close');
|
||||||
|
server.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(PORT, function () {
|
||||||
|
conn = net.createConnection(PORT);
|
||||||
|
conn.setEncoding('utf8');
|
||||||
|
conn.addListener("data", function (chunk) {
|
||||||
|
error('recv data! nchars = ' + chunk.length);
|
||||||
|
buffer += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.addListener("end", function () {
|
||||||
|
conn.end();
|
||||||
|
});
|
||||||
|
conn.addListener("close", function () {
|
||||||
|
error('client connection close');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
var buffer = '';
|
||||||
|
count = 0;
|
||||||
|
|
||||||
|
server.addListener('listening', function () {
|
||||||
|
});
|
||||||
|
|
||||||
|
process.addListener('exit', function () {
|
||||||
|
assert.equal(expected, buffer);
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user