Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add readme

  • Loading branch information...
commit 47284c475e510031177721cbaf65be9de11f5da6 1 parent b7666e4
Konstantin Käfer authored June 19, 2011

Showing 1 changed file with 37 additions and 0 deletions. Show diff stats Hide diff stats

  1. 37  README.md
37  README.md
Source Rendered
... ...
@@ -0,0 +1,37 @@
  1
+# ChunkedStream
  2
+
  3
+ChunkedStream simplifies implementing line-based protocols by providing an interface to obtain lines and fixed-length buffers from a streaming source. It acquires input by acting as a target for a piped stream.
  4
+
  5
+## Usage
  6
+
  7
+```javascript
  8
+var ChunkedStream = require('chunkedstream');
  9
+
  10
+var cs = new ChunkedStream();
  11
+socket.pipe(cs);  // obtain socket e.g. by opening a network connection
  12
+
  13
+cs.getLine(function(line) {
  14
+    // line a line terminated with \r\n
  15
+});
  16
+```
  17
+
  18
+Note that `.getLine()` will only call the callback once. If you want to read lines repeatedly, you have to call `.getLine()` again from the callback.
  19
+
  20
+To obtain a fixed number of bytes from the stream:
  21
+
  22
+```javascript
  23
+cs.getBytes(512, onchunk, oncomplete);
  24
+
  25
+function onchunk(buffer) {
  26
+    // Called repeatedly with a buffer as they come in.
  27
+    // The total combined length of all buffer objects passed to this function
  28
+    // is 512 bytes as specified above.
  29
+}
  30
+
  31
+function oncomplete() {
  32
+    // Called after onchunk() has been called with total combined buffer
  33
+    // length of 512 bytes.
  34
+}
  35
+```
  36
+
  37
+**Note**: When the stream never contains `\r\n`, `getLine` will never call the callback function.

0 notes on commit 47284c4

Please sign in to comment.
Something went wrong with that request. Please try again.