mirror of https://github.com/lukechilds/node.git
Ryan Dahl
14 years ago
5 changed files with 86 additions and 7 deletions
@ -0,0 +1,57 @@ |
|||||
|
var events = require('events'); |
||||
|
var inherits = require('sys').inherits; |
||||
|
|
||||
|
function Stream () { |
||||
|
events.EventEmitter.call(this); |
||||
|
} |
||||
|
inherits(Stream, events.EventEmitter); |
||||
|
exports.Stream = Stream; |
||||
|
|
||||
|
Stream.prototype.pipe = function (dest, options) { |
||||
|
var source = this; |
||||
|
|
||||
|
source.on("data", function (chunk) { |
||||
|
if (false === dest.write(chunk)) source.pause(); |
||||
|
}); |
||||
|
|
||||
|
dest.on("drain", function () { |
||||
|
if (source.readable) source.resume(); |
||||
|
}); |
||||
|
|
||||
|
/* |
||||
|
* If the 'end' option is not supplied, dest.end() will be called when |
||||
|
* source gets the 'end' event. |
||||
|
*/ |
||||
|
|
||||
|
options.end = options && options.end === false ? false : true; |
||||
|
|
||||
|
if (options.end) { |
||||
|
source.on("end", function () { |
||||
|
dest.end(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/* |
||||
|
* Questionable: |
||||
|
*/ |
||||
|
|
||||
|
if (!source.pause) { |
||||
|
source.pause = function () { |
||||
|
source.emit("pause"); |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
if (!source.resume) { |
||||
|
source.resume = function () { |
||||
|
source.emit("resume"); |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
dest.on("pause", function () { |
||||
|
source.pause(); |
||||
|
}); |
||||
|
|
||||
|
dest.on("resume", function () { |
||||
|
if (source.readable) source.resume(); |
||||
|
}); |
||||
|
}; |
@ -0,0 +1,20 @@ |
|||||
|
/* |
||||
|
* try with |
||||
|
* curl -d @/usr/share/dict/words http://localhost:8000/123
|
||||
|
*/ |
||||
|
|
||||
|
http = require('http'); |
||||
|
|
||||
|
s = http.Server(function (req, res) { |
||||
|
console.log(req.headers); |
||||
|
|
||||
|
req.pipe(process.stdout, { end: false }); |
||||
|
|
||||
|
req.on('end', function () { |
||||
|
res.writeHead(200); |
||||
|
res.write("thanks"); |
||||
|
res.end(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
s.listen(8000); |
Loading…
Reference in new issue