Created
July 31, 2012 22:54
-
-
Save ryan-roemer/3221453 to your computer and use it in GitHub Desktop.
Read/Write Streams Example Code (for: http://loose-bits.com/2012/08/02/nodejs-read-write-streams-pipes.html)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
out.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get Google's home page. | |
require('http').get("http://www.google.com/", function(response) { | |
// The callback provides the response readable stream. | |
// Then, we open our output text stream. | |
var outStream = require('fs').createWriteStream("out.txt"); | |
// Pipe the input to the output, which writes the file. | |
response.pipe(outStream); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set both readable and writable in constructor. | |
var NopStream = function () { | |
this.readable = true; | |
this.writable = true; | |
}; | |
// Inherit from base stream class. | |
require('util').inherits(NopStream, require('stream')); | |
// Extract args to `write` and emit as `data` event. | |
NopStream.prototype.write = function () { | |
args = Array.prototype.slice.call(arguments, 0); | |
this.emit.apply(this, ['data'].concat(args)) | |
}; | |
// Extract args to `end` and emit as `end` event. | |
NopStream.prototype.end = function () { | |
args = Array.prototype.slice.call(arguments, 0); | |
this.emit.apply(this, ['end'].concat(args)) | |
}; | |
// Download the same page again, but with the NOP stream | |
// in the middle. | |
require('http').get("http://www.google.com/", function(response) { | |
var outStream = require('fs').createWriteStream("out.txt"); | |
response | |
.pipe(new NopStream()) | |
.pipe(new NopStream()) | |
.pipe(new NopStream()) | |
.pipe(outStream); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* A simple upper-case stream converter. | |
*/ | |
var UpperCaseStream = function () { | |
this.readable = true; | |
this.writable = true; | |
}; | |
require("util").inherits(UpperCaseStream, require("stream")); | |
/** | |
* Handle various params and upper-case string data. | |
* | |
* Signature can be in format of: | |
* - string, [encoding] | |
* - buffer | |
* | |
* Our example implementation hacks the data into a simpler | |
# (string) form -- real implementations would need more. | |
*/ | |
UpperCaseStream.prototype._transform = function (data) { | |
// Here, we'll just shortcut to a string. | |
data = data ? data.toString() : ""; | |
// Upper-case the string and emit data event with transformed data. | |
this.emit("data", data.toUpperCase()); | |
}; | |
/** | |
* Stream write (override). | |
*/ | |
UpperCaseStream.prototype.write = function () { | |
this._transform.apply(this, arguments); | |
}; | |
/** | |
* Stream end (override). | |
*/ | |
UpperCaseStream.prototype.end = function () { | |
this._transform.apply(this, arguments); | |
this.emit("end"); | |
}; | |
/** | |
* Test script. | |
* | |
* Read in this script, upper-case it, and write to "out.txt" | |
*/ | |
function convertToUpperCase() { | |
var fs = require("fs"), | |
input = fs.createReadStream(__filename), | |
output = fs.createWriteStream("out.txt"), | |
upperCase = new UpperCaseStream(); | |
// Open our read input, uppercase it, then write out. | |
input | |
.pipe(upperCase) | |
.pipe(output); | |
} | |
// Run script if directly executed. | |
if (require.main === module) { | |
convertToUpperCase(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment