UNPKG

2.62 kBMarkdownView Raw
1# Introduction
2node-memorystream - this module allow create streams in memory. It's can be used for emulating file streams, as a buffer for incoming data that you want to pipe to another stream, the gap between two data/network streams of variable rates, etc. MemoryStream support read/write states or only read state or only write state. The API is meant to follow node's Stream implementation.
3
4Original module is here git://github.com/ollym/memstream.git was remaked and improved.
5
6## Installation
7If you have npm installed, you can simply type:
8
9 npm install memorystream
10
11Or you can clone this repository using the git command:
12
13 git clone git://github.com/JSBizon/node-memorystream.git
14
15## Usage
16Some examples how to use memorystream module.
17
18#### Basic IO Operation
19In this example i illustrate the basic IO operations of the memory stream.
20
21 var MemoryStream = require('memorystream');
22 var memStream = new MemoryStream(['Hello',' ']);
23
24 var data = '';
25 memStream.on('data',function(chunk){
26 data += chunk.toString();
27 });
28
29 memStream.write('World');
30
31 memStream.on('end',function(){
32 console.log(data);//output 'Hello World!'
33 });
34 memStream.end('!');
35
36#### Piping
37In this example i'm piping all data from the memory stream to the process' stdout stream.
38
39 var MemoryStream = require('memorystream');
40 var memStream = new MemoryStream();
41 memStream.pipe(process.stdout, { end: false });
42
43 memStream.write('Hello World!');
44
45#### Pumping
46In this example i'm pumping all data from the response stream to the memorystream. Memorystream works like buffer.
47
48 var http = require('http'),
49 MemoryStream = require('memorystream'),
50 util = require('util');
51
52 var options = {
53 host: 'google.com'
54 };
55 var memStream = new MemoryStream(null,{
56 readable : false
57 });
58
59 var req = http.request(options, function(res) {
60 util.pump(res, memStream);
61 res.on('end',function(){
62 console.log(memStream.getAll());
63 });
64 });
65 req.end();
66
67#### Delayed Response
68In the example below, we first pause the stream before writing the data to it. The stream is then resumed after 1 second, and the data is written to the console.
69
70 var MemoryStream = require('memorystream');
71
72 var memStream = new MemoryStream('Hello');
73 var data = '';
74 memStream.on('data',function(chunk){
75 data += chunk;
76 });
77
78 memStream.pause();
79 memStream.write('World!');
80
81 setTimeout(function() {
82 stream.resume();
83 }, 1000);
84
85## Documentation
86The memory stream adopts all the same methods and events as node's Stream implementation.
87Documentation is [available here](http://github.com/JSBizon/node-memorystream/wiki/API/ "Documentation").
88
89
90
91
\No newline at end of file