提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.7 KiB

  1. [![Build Status](https://travis-ci.org/JSBizon/node-memorystream.svg?branch=master)](https://travis-ci.org/JSBizon/node-memorystream)
  2. # Introduction
  3. node-memorystream - this module allow create streams in memory. It can be used for emulating file streams, filtering/mutating data between one stream and another, buffering incoming data, being 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.
  4. Module supports streams for node > 0.10 now.
  5. Original module is here git://github.com/ollym/memstream.git was remade and improved.
  6. ## Installation
  7. If you have npm installed, you can simply type:
  8. npm install memorystream
  9. Or you can clone this repository using the git command:
  10. git clone git://github.com/JSBizon/node-memorystream.git
  11. ## Usage
  12. Some examples how to use memorystream module.
  13. #### Basic I/O Operation
  14. In this example I illustrate the basic I/O operations of the memory stream.
  15. var MemoryStream = require('memorystream');
  16. var memStream = new MemoryStream(['Hello',' ']);
  17. var data = '';
  18. memStream.on('data', function(chunk) {
  19. data += chunk.toString();
  20. });
  21. memStream.write('World');
  22. memStream.on('end', function() {
  23. // outputs 'Hello World!'
  24. console.log(data);
  25. });
  26. memStream.end('!');
  27. #### Piping
  28. In this example I'm piping all data from the memory stream to the process's stdout stream.
  29. var MemoryStream = require('memorystream');
  30. var memStream = new MemoryStream();
  31. memStream.pipe(process.stdout, { end: false });
  32. memStream.write('Hello World!');
  33. In this example I'm piping all data from the response stream to the memory stream.
  34. var http = require('http'),
  35. MemoryStream = require('memorystream');
  36. var options = {
  37. host: 'google.com'
  38. };
  39. var memStream = new MemoryStream(null, {
  40. readable : false
  41. });
  42. var req = http.get(options, function(res) {
  43. res.pipe(memStream);
  44. res.on('end', function() {
  45. console.log(memStream.toString());
  46. });
  47. });
  48. #### Delayed Response
  49. In 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.
  50. var MemoryStream = require('memorystream');
  51. var memStream = new MemoryStream('Hello');
  52. var data = '';
  53. memStream.on('data', function(chunk) {
  54. data += chunk;
  55. });
  56. memStream.pause();
  57. memStream.write('World!');
  58. setTimeout(function() {
  59. memStream.resume();
  60. }, 1000);
  61. ## Documentation
  62. The memory stream adopts all the same methods and events as node's Stream implementation.
  63. Documentation is [available here](http://github.com/JSBizon/node-memorystream/wiki/API/ "Documentation").