This repository has been archived by the owner on Dec 23, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul
committed
Jun 6, 2013
0 parents
commit 2c65fb6
Showing
9 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# OS (Mac OSX) | ||
.DS_store | ||
*/.DS_store | ||
|
||
# Node JS | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
npm-debug.log | ||
node_modules | ||
|
||
# SublimeText project files | ||
*.sublime-workspace | ||
|
||
# Textmate | ||
*.tmproj | ||
*.tmproject | ||
tmtags | ||
|
||
# Redis | ||
*.rdb |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2013 Paul Jackson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Memory Streams JS | ||
_Memory Streams JS_ is a light-weight implementation of the `Stream.Readable` and `Stream.Writable` abstract classes from node.js. You can use the classes provided to store the result of reading and writing streams in memory. This can be useful when you need pipe your test output for later inspection or to stream files from the web into memory without have to use temporary files on disk. | ||
|
||
## Installation | ||
Install with: | ||
|
||
npm install memory-streams --save | ||
|
||
## Usage | ||
Sample usage, using the `ReadableStream` class and piping: | ||
|
||
var streams = require('memory-streams'); | ||
|
||
// Initialize with the string | ||
var reader = new streams.ReadableStream('Hello World\n'); | ||
|
||
// Send all output to stdout | ||
reader.pipe(process.stdout); // outputs: "Hello World\n" | ||
|
||
// Add more data to the stream | ||
reader.append('Hello Universe\n'); // outputs "Hello Universe\n"; | ||
|
||
Using the `ReadableStream` class and reading manually: | ||
|
||
var streams = require('memory-streams'); | ||
|
||
// Initialize with the string | ||
var reader = new streams.ReadableStream('Hello World\n'); | ||
|
||
// Add more data to the stream | ||
reader.append('Hello Universe\n'); // outputs "Hello Universe\n"; | ||
|
||
// Read the data out | ||
console.log(reader.toString()); // outputs: "Hello World\nHello Universe\n" | ||
|
||
Using the `WritableStream` class and piping the contents of a file: | ||
|
||
var streams = require('memory-streams') | ||
, fs = require('fs'); | ||
|
||
// Pipe | ||
var reader = fs.createReadStream('index.js'); | ||
var writer = new streams.WritableStream(); | ||
reader.pipe(writer); | ||
reader.on('readable', function() { | ||
|
||
// Output the content as a string | ||
console.log(writer.toString()); | ||
// Output the content as a Buffer | ||
console.log(writer.toBuffer()); | ||
}); | ||
|
||
You can also call the `write` method directly to store data to the stream: | ||
|
||
var streams = require('memory-streams'); | ||
|
||
// Write method | ||
var writer = new streams.WritableStream(); | ||
writer.write('Hello World\n'); | ||
|
||
// Output the content as a string | ||
console.log(writer.toString()); // Outputs: "Hello World\n" | ||
|
||
For more examples you can look at the tests for the module. | ||
|
||
## License | ||
MIT | ||
|
||
Copyright (c) 2013 Paul Jackson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports.ReadableStream = require('./lib/ReadableStream'); | ||
module.exports.WritableStream = require('./lib/WritableStream'); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//------------------------------------------------------------------ | ||
// Dependencies | ||
|
||
var Stream = require('stream') | ||
, util = require('util'); | ||
|
||
//------------------------------------------------------------------ | ||
// Exports | ||
|
||
module.exports = ReadableStream; | ||
|
||
//------------------------------------------------------------------ | ||
// ReadableStream class | ||
|
||
util.inherits(ReadableStream, Stream.Readable); | ||
|
||
function ReadableStream (data) { | ||
Stream.Readable.call(this); | ||
this._data = data; | ||
} | ||
|
||
ReadableStream.prototype._read = function(n) { | ||
this.push(this._data); | ||
this._data = ''; | ||
}; | ||
|
||
ReadableStream.prototype.append = function(data) { | ||
this.push(data); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
//------------------------------------------------------------------ | ||
// Dependencies | ||
|
||
var Stream = require('stream') | ||
, util = require('util'); | ||
|
||
|
||
//------------------------------------------------------------------ | ||
// Exports | ||
|
||
module.exports = WritableStream; | ||
|
||
|
||
//------------------------------------------------------------------ | ||
// WritableStream class | ||
|
||
util.inherits(WritableStream, Stream.Writable); | ||
|
||
function WritableStream () { | ||
Stream.Writable.call(this); | ||
} | ||
|
||
WritableStream.prototype._write = function(chunk, encoding) { | ||
this.write(chunk, encoding); | ||
}; | ||
|
||
WritableStream.prototype.toString = function() { | ||
return this.toBuffer().toString(); | ||
}; | ||
|
||
WritableStream.prototype.toBuffer = function() { | ||
var buffers = []; | ||
this._writableState.buffer.forEach(function(data) { | ||
buffers.push(data.chunk); | ||
}); | ||
|
||
return Buffer.concat(buffers); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "memory-streams", | ||
"description": "Simple implmentation of Stream.Readable and Stream.Writable holding the data in memory.", | ||
"version": "0.0.1", | ||
"author": "Paul Jackson (http://paulj.me/)", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:paulja/memory-streams-js.git" | ||
}, | ||
"homepage": "https://github.com/paulja/memory-streams-js", | ||
"main": "index.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"dependencies": { | ||
}, | ||
"devDependencies": { | ||
"should": "~1.2.2" | ||
}, | ||
"scripts": { | ||
"test": "node ./test/test-readablestream.js && node ./test/test-writablestream.js" | ||
}, | ||
"keywords": [ | ||
"stream", | ||
"string", | ||
"memory", | ||
"Readable", | ||
"Writable" | ||
], | ||
"license": "MIT", | ||
"readmeFilename": "README.md" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var streams = require('../') | ||
, should = require('should'); | ||
|
||
// Definition | ||
streams.should.not.be.undefined; | ||
streams.should.have.property('ReadableStream'); | ||
|
||
// Read method | ||
var reader = new streams.ReadableStream('Hello World\n'); | ||
reader.read().toString().should.equal('Hello World\n'); | ||
should.not.exist(reader.read()); | ||
|
||
// Append method | ||
reader.append('Hello Universe\n'); | ||
reader.read().toString().should.equal('Hello Universe\n'); | ||
|
||
// Done | ||
console.log( '> ReadableStream tests complete.'); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var streams = require('../') | ||
, should = require('should') | ||
, fs = require('fs'); | ||
|
||
// Definition | ||
streams.should.not.be.undefined; | ||
streams.should.have.property('WritableStream'); | ||
|
||
// Write method | ||
var writer = new streams.WritableStream(); | ||
writer.toString().should.be.empty; | ||
writer.write('Hello World\n'); | ||
writer.toString().should.equal('Hello World\n'); | ||
writer.toBuffer().should.not.be.null; | ||
writer.toBuffer().toString().should.equal('Hello World\n'); | ||
|
||
// Write more | ||
writer.write('Hello Universe\n'); | ||
writer.toString().should.equal('Hello World\nHello Universe\n'); | ||
|
||
// Pipe test | ||
var content = fs.readFileSync('index.js'); | ||
var reader = fs.createReadStream('index.js'); | ||
writer = new streams.WritableStream(); | ||
reader.pipe(writer); | ||
reader.on('readable', function() { | ||
writer.toString().should.equal(content.toString()); | ||
}); | ||
|
||
// Done | ||
console.log( '> WritableStream tests complete.'); |