├── .gitignore ├── LICENSE ├── README.md ├── index.d.ts ├── index.js ├── lib ├── ReadableStream.js └── WritableStream.js ├── package.json └── test ├── test-readablestream.js └── test-writablestream.js /.gitignore: -------------------------------------------------------------------------------- 1 | # OS (Mac OSX) 2 | .DS_store 3 | */.DS_store 4 | 5 | # Node JS 6 | lib-cov 7 | *.seed 8 | *.log 9 | *.csv 10 | *.dat 11 | *.out 12 | *.pid 13 | *.gz 14 | 15 | pids 16 | logs 17 | results 18 | 19 | npm-debug.log 20 | node_modules 21 | 22 | # SublimeText project files 23 | *.sublime-workspace 24 | 25 | # Textmate 26 | *.tmproj 27 | *.tmproject 28 | tmtags 29 | 30 | # Redis 31 | *.rdb -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Paul Jackson 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Memory Streams JS 2 | _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. 3 | 4 | ## Installation 5 | Install with: 6 | 7 | ```bash 8 | npm install memory-streams --save 9 | ``` 10 | 11 | ## Usage 12 | Sample usage, using the `ReadableStream` class and piping: 13 | 14 | ```js 15 | var streams = require('memory-streams'); 16 | 17 | // Initialize with the string 18 | var reader = new streams.ReadableStream('Hello World\n'); 19 | 20 | // Send all output to stdout 21 | reader.pipe(process.stdout); // outputs: "Hello World\n" 22 | 23 | // Add more data to the stream 24 | reader.append('Hello Universe\n'); // outputs "Hello Universe\n"; 25 | ``` 26 | 27 | Using the `ReadableStream` class and reading manually: 28 | 29 | ```js 30 | var streams = require('memory-streams'); 31 | 32 | // Initialize with the string 33 | var reader = new streams.ReadableStream('Hello World\n'); 34 | 35 | // Add more data to the stream 36 | reader.append('Hello Universe\n'); // outputs "Hello Universe\n"; 37 | 38 | // Read the data out 39 | console.log(reader.read().toString()); // outputs: "Hello World\nHello Universe\n" 40 | ``` 41 | 42 | Using the `WritableStream` class and piping the contents of a file: 43 | 44 | ```js 45 | var streams = require('memory-streams') 46 | , fs = require('fs'); 47 | 48 | // Pipe 49 | var reader = fs.createReadStream('index.js'); 50 | var writer = new streams.WritableStream(); 51 | reader.pipe(writer); 52 | reader.on('readable', function() { 53 | 54 | // Output the content as a string 55 | console.log(writer.toString()); 56 | 57 | // Output the content as a Buffer 58 | console.log(writer.toBuffer()); 59 | }); 60 | ``` 61 | 62 | You can also call the `write` method directly to store data to the stream: 63 | 64 | ```js 65 | var streams = require('memory-streams'); 66 | 67 | // Write method 68 | var writer = new streams.WritableStream(); 69 | writer.write('Hello World\n'); 70 | 71 | // Output the content as a string 72 | console.log(writer.toString()); // Outputs: "Hello World\n" 73 | ``` 74 | 75 | For more examples you can look at the tests for the module. 76 | 77 | ## License 78 | MIT 79 | 80 | Copyright (c) 2017 Paul Jackson 81 | 82 | Permission is hereby granted, free of charge, to any person obtaining a copy 83 | of this software and associated documentation files (the "Software"), to deal 84 | in the Software without restriction, including without limitation the rights 85 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 86 | copies of the Software, and to permit persons to whom the Software is 87 | furnished to do so, subject to the following conditions: 88 | 89 | The above copyright notice and this permission notice shall be included in 90 | all copies or substantial portions of the Software. 91 | 92 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 93 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 94 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 95 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 96 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 97 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 98 | THE SOFTWARE. 99 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | // Describes the package to typescript users. 2 | 3 | import {Readable, Writable} from 'stream'; 4 | 5 | /** A stream that reads from a string given at creation time. */ 6 | export declare class ReadableStream extends Readable { 7 | constructor(contents: string); 8 | } 9 | 10 | /** A stream that writes into an in-memory buffer. */ 11 | export declare class WritableStream extends Writable { 12 | /** Returns the written contents as a string. */ 13 | toString(): string; 14 | 15 | /** Returns the written contents as a buffer. */ 16 | toBuffer(): Buffer; 17 | } 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports.ReadableStream = require('./lib/ReadableStream'); 2 | module.exports.WritableStream = require('./lib/WritableStream'); -------------------------------------------------------------------------------- /lib/ReadableStream.js: -------------------------------------------------------------------------------- 1 | 2 | //------------------------------------------------------------------ 3 | // Dependencies 4 | 5 | var Stream = require('readable-stream') 6 | , util = require('util'); 7 | 8 | 9 | //------------------------------------------------------------------ 10 | // Exports 11 | 12 | module.exports = ReadableStream; 13 | 14 | 15 | //------------------------------------------------------------------ 16 | // ReadableStream class 17 | 18 | util.inherits(ReadableStream, Stream.Readable); 19 | 20 | function ReadableStream (data) { 21 | Stream.Readable.call(this); 22 | this._data = data; 23 | } 24 | 25 | ReadableStream.prototype._read = function(n) { 26 | this.push(this._data); 27 | this._data = ''; 28 | }; 29 | 30 | ReadableStream.prototype.append = function(data) { 31 | this.push(data); 32 | }; -------------------------------------------------------------------------------- /lib/WritableStream.js: -------------------------------------------------------------------------------- 1 | 2 | //------------------------------------------------------------------ 3 | // Dependencies 4 | 5 | var Stream = require('readable-stream') 6 | , util = require('util'); 7 | 8 | 9 | //------------------------------------------------------------------ 10 | // Exports 11 | 12 | module.exports = WritableStream; 13 | 14 | 15 | //------------------------------------------------------------------ 16 | // WritableStream class 17 | 18 | util.inherits(WritableStream, Stream.Writable); 19 | 20 | function WritableStream (options) { 21 | Stream.Writable.call(this, options); 22 | } 23 | 24 | WritableStream.prototype.write = function(chunk, encoding, callback) { 25 | var ret = Stream.Writable.prototype.write.apply(this, arguments); 26 | if (!ret) this.emit('drain'); 27 | return ret; 28 | } 29 | 30 | WritableStream.prototype._write = function(chunk, encoding, callback) { 31 | this.write(chunk, encoding, callback); 32 | }; 33 | 34 | WritableStream.prototype.toString = function() { 35 | return this.toBuffer().toString(); 36 | }; 37 | 38 | WritableStream.prototype.toBuffer = function() { 39 | var buffers = []; 40 | this._writableState.buffer.forEach(function(data) { 41 | buffers.push(data.chunk); 42 | }); 43 | 44 | return Buffer.concat(buffers); 45 | }; 46 | 47 | WritableStream.prototype.end = function(chunk, encoding, callback) { 48 | var ret = Stream.Writable.prototype.end.apply(this, arguments); 49 | // In memory stream doesn't need to flush anything so emit `finish` right away 50 | // base implementation in Stream.Writable doesn't emit finish 51 | this.emit('finish'); 52 | return ret; 53 | }; 54 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "memory-streams", 3 | "description": "Simple implmentation of Stream.Readable and Stream.Writable holding the data in memory.", 4 | "version": "0.1.3", 5 | "author": "Paul Jackson (http://jaaco.uk/)", 6 | "repository": { 7 | "type": "git", 8 | "url": "git@github.com:paulja/memory-streams-js.git" 9 | }, 10 | "homepage": "https://github.com/paulja/memory-streams-js", 11 | "main": "index.js", 12 | "typings": "index.d.ts", 13 | "directories": { 14 | "test": "test" 15 | }, 16 | "dependencies": { 17 | "readable-stream": "~1.0.2" 18 | }, 19 | "devDependencies": { 20 | "should": "~1.2.2" 21 | }, 22 | "scripts": { 23 | "test": "node ./test/test-readablestream.js && node ./test/test-writablestream.js" 24 | }, 25 | "keywords": [ 26 | "stream", 27 | "string", 28 | "memory", 29 | "Readable", 30 | "Writable" 31 | ], 32 | "license": "MIT", 33 | "readmeFilename": "README.md" 34 | } 35 | -------------------------------------------------------------------------------- /test/test-readablestream.js: -------------------------------------------------------------------------------- 1 | var streams = require('../') 2 | , should = require('should'); 3 | 4 | // Definition 5 | streams.should.not.be.undefined; 6 | streams.should.have.property('ReadableStream'); 7 | 8 | // Read method 9 | var reader = new streams.ReadableStream('Hello World\n'); 10 | reader.read().toString().should.equal('Hello World\n'); 11 | should.not.exist(reader.read()); 12 | 13 | // Append method 14 | reader.append('Hello Universe\n'); 15 | reader.read().toString().should.equal('Hello Universe\n'); 16 | 17 | // Done 18 | console.log( '> ReadableStream tests complete.'); -------------------------------------------------------------------------------- /test/test-writablestream.js: -------------------------------------------------------------------------------- 1 | var streams = require('../') 2 | , should = require('should') 3 | , fs = require('fs'); 4 | 5 | // Definition 6 | streams.should.not.be.undefined; 7 | streams.should.have.property('WritableStream'); 8 | 9 | // Write method 10 | var writer = new streams.WritableStream(); 11 | writer.toString().should.be.empty; 12 | writer.write('Hello World\n'); 13 | writer.toString().should.equal('Hello World\n'); 14 | writer.toBuffer().should.not.be.null; 15 | writer.toBuffer().toString().should.equal('Hello World\n'); 16 | 17 | // Write more 18 | writer.write('Hello Universe\n'); 19 | writer.toString().should.equal('Hello World\nHello Universe\n'); 20 | 21 | // Pipe test 22 | var content = fs.readFileSync('index.js'); 23 | var reader = fs.createReadStream('index.js'); 24 | writer = new streams.WritableStream(); 25 | reader.pipe(writer); 26 | reader.on('readable', function() { 27 | writer.toString().should.equal(content.toString()); 28 | }); 29 | 30 | // Done 31 | console.log( '> WritableStream tests complete.'); --------------------------------------------------------------------------------