[Pkg-javascript-commits] [node-stream-splicer] 26/71: documented example
Bastien Roucariès
rouca at moszumanska.debian.org
Fri Dec 15 09:55:49 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-stream-splicer.
commit 156b0a9dae25169eda250492ef5e71127e8cdee4
Author: James Halliday <mail at substack.net>
Date: Mon Jun 9 13:54:23 2014 -0700
documented example
---
example/header.js | 8 ++++----
readme.markdown | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+), 4 deletions(-)
diff --git a/example/header.js b/example/header.js
index a436f26..6597f3b 100644
--- a/example/header.js
+++ b/example/header.js
@@ -1,4 +1,4 @@
-var pipeline = require('../');
+var splicer = require('../');
var through = require('through2');
var JSONStream = require('JSONStream');
var split = require('split');
@@ -8,7 +8,7 @@ var headers = through.obj(function (buf, enc, next) {
var line = buf.toString('utf8');
if (line === '') {
this.push(headerData);
- outer.splice(1, 1, JSONStream.parse([ 'rows', true ]));
+ pipeline.splice(1, 1, JSONStream.parse([ 'rows', true ]));
}
else {
var m = /^(\S+):(.+)/.exec(line);
@@ -18,5 +18,5 @@ var headers = through.obj(function (buf, enc, next) {
}
next();
});
-var outer = pipeline([ split(), headers, JSONStream.stringify() ]);
-process.stdin.pipe(outer).pipe(process.stdout);
+var pipeline = splicer([ split(), headers, JSONStream.stringify() ]);
+process.stdin.pipe(pipeline).pipe(process.stdout);
diff --git a/readme.markdown b/readme.markdown
index 82da092..7ff2967 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -8,6 +8,55 @@ but with a pipeline configuration that can be changed at runtime.
# example
+This example begins with an HTTP header parser that waits for an empty line to
+signify the end of the header. At that point, it switches to a streaming json
+parser to operate on the HTTP body.
+
``` js
+var splicer = require('stream-splicer');
+var through = require('through2');
+var JSONStream = require('JSONStream');
+var split = require('split');
+
+var headerData = {};
+var headers = through.obj(function (buf, enc, next) {
+ var line = buf.toString('utf8');
+ if (line === '') {
+ this.push(headerData);
+ pipeline.splice(1, 1, JSONStream.parse([ 'rows', true ]));
+ }
+ else {
+ var m = /^(\S+):(.+)/.exec(line);
+ var key = m && m[1].trim();
+ var value = m && m[2].trim();
+ if (m) headerData[key] = value;
+ }
+ next();
+});
+var pipeline = splicer([ split(), headers, JSONStream.stringify() ]);
+process.stdin.pipe(pipeline).pipe(process.stdout);
+```
+
+intput:
+
+```
+GET / HTTP/1.1
+Host: substack.net
+User-Agent: echo
+
+{"rows":["beep","boop"]}
+```
+
+output:
+
+```
+$ echo -ne 'GET / HTTP/1.1\nHost: substack.net\nUser-Agent: echo\n\n{"rows":["beep","boop"]}\n' | node example/header.js
+[
+{"Host":"substack.net","User-Agent":"echo"}
+,
+"beep"
+,
+"boop"
+]
```
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-stream-splicer.git
More information about the Pkg-javascript-commits
mailing list