Skip to content

Instantly share code, notes, and snippets.

@borodean
Created December 8, 2018 22:46
Show Gist options
  • Select an option

  • Save borodean/9add954706a54dd4965faa6576932b14 to your computer and use it in GitHub Desktop.

Select an option

Save borodean/9add954706a54dd4965faa6576932b14 to your computer and use it in GitHub Desktop.
event-stream 3.3.4 vs 3.3.5
Only in 3.3.4: .npmignore
Only in 3.3.5/examples: data
Only in 3.3.5/examples: map.js
diff --recursive --unified --exclude test --exclude Makefile 3.3.4/examples/pretty.js 3.3.5/examples/pretty.js
--- 3.3.4/examples/pretty.js 2016-06-19 02:37:21.000000000 +0300
+++ 3.3.5/examples/pretty.js 2018-09-05 07:12:55.000000000 +0300
@@ -1,25 +1,18 @@
var inspect = require('util').inspect
+var es = require('..')
-if(!module.parent) {
- var es = require('..') //load event-stream
- es.pipe( //pipe joins streams together
- process.openStdin(), //open stdin
- es.split(), //split stream to break on newlines
- es.map(function (data, callback) {//turn this async function into a stream
- var j
- try {
- j = JSON.parse(data) //try to parse input into json
- } catch (err) {
- return callback(null, data) //if it fails just pass it anyway
- }
- callback(null, inspect(j)) //render it nicely
- }),
- process.stdout // pipe it to stdout !
- )
- }
-
-// run this
-//
-// curl -sS registry.npmjs.org/event-stream | node pretty.js
-//
+es.pipe( //pipe joins streams together
+ process.openStdin(), //open stdin
+ es.split(null, null, {trailing: false}), //split stream to break on newlines
+ es.map(function (data, callback) { //turn this async function into a stream
+ var obj = JSON.parse(data) //parse input into json
+ callback(null, inspect(obj) + '\n') //render it nicely
+ }),
+ process.stdout // pipe it to stdout !
+)
+
+// cat data | node pretty.js
+// { foo: 1 }
+// { foo: 2 }
+// { foo: 3, bar: 'test' }
\ No newline at end of file
Only in 3.3.5/examples: split.js
diff --recursive --unified --exclude test --exclude Makefile 3.3.4/package.json 3.3.5/package.json
--- 3.3.4/package.json 2016-07-17 10:23:27.000000000 +0300
+++ 3.3.5/package.json 2018-09-05 08:23:40.000000000 +0300
@@ -1,6 +1,6 @@
{
"name": "event-stream",
- "version": "3.3.4",
+ "version": "3.3.5",
"description": "construct pipes of streams of events",
"homepage": "http://github.com/dominictarr/event-stream",
"repository": {
@@ -8,20 +8,20 @@
"url": "git://github.com/dominictarr/event-stream.git"
},
"dependencies": {
- "through": "~2.3.1",
- "duplexer": "~0.1.1",
- "from": "~0",
- "map-stream": "~0.1.0",
- "pause-stream": "0.0.11",
- "split": "0.3",
- "stream-combiner": "~0.0.4"
+ "duplexer": "^0.1.1",
+ "from": "^0.1.7",
+ "map-stream": "0.0.7",
+ "pause-stream": "^0.0.11",
+ "split": "^1.0.1",
+ "stream-combiner": "^0.2.2",
+ "through": "^2.3.8"
},
"devDependencies": {
- "asynct": "*",
- "it-is": "1",
- "ubelt": "~3.2.2",
- "stream-spec": "~0.3.5",
- "tape": "~2.3.0"
+ "asynct": "^1.1.0",
+ "it-is": "^1.0.3",
+ "stream-spec": "^0.3.6",
+ "tape": "^4.9.1",
+ "ubelt": "^3.2.2"
},
"scripts": {
"prepublish": "npm ls && npm test",
diff --recursive --unified --exclude test --exclude Makefile 3.3.4/readme.markdown 3.3.5/readme.markdown
--- 3.3.4/readme.markdown 2016-06-19 02:37:21.000000000 +0300
+++ 3.3.5/readme.markdown 2018-09-05 08:05:52.000000000 +0300
@@ -1,33 +1,21 @@
# EventStream
-<img src=https://secure.travis-ci.org/dominictarr/event-stream.png?branch=master>
+[Streams](http://nodejs.org/api/stream.html "Stream") are node's best and most misunderstood idea, and EventStream is a toolkit to make creating and working with streams easy.
-[![browser status](http://ci.testling.com/dominictarr/event-stream.png)]
-(http://ci.testling.com/dominictarr/event-stream)
+Normally, streams are only used for IO, but in event stream we send all kinds of objects down the pipe. If your application's input and output are streams,
+shouldn't the throughput be a stream too?
-[Streams](http://nodejs.org/api/stream.html "Stream") are node's best and most misunderstood idea, and
-_<em>EventStream</em>_ is a toolkit to make creating and working with streams <em>easy</em>.
+The *EventStream* functions resemble the array functions, because Streams are like Arrays, but laid out in time, rather than in memory.
-Normally, streams are only used for IO,
-but in event stream we send all kinds of objects down the pipe.
-If your application's <em>input</em> and <em>output</em> are streams,
-shouldn't the <em>throughput</em> be a stream too?
+All the `event-stream` functions return instances of `Stream`.
-The *EventStream* functions resemble the array functions,
-because Streams are like Arrays, but laid out in time, rather than in memory.
-
-<em>All the `event-stream` functions return instances of `Stream`</em>.
-
-`event-stream` creates
-[0.8 streams](https://github.com/joyent/node/blob/v0.8/doc/api/stream.markdown)
-, which are compatible with [0.10 streams](http://nodejs.org/api/stream.html "Stream").
+`event-stream` creates [0.8 streams](https://github.com/joyent/node/blob/v0.8/doc/api/stream.markdown), which are compatible with [0.10 streams](http://nodejs.org/api/stream.html "Stream").
>NOTE: I shall use the term <em>"through stream"</em> to refer to a stream that is writable <em>and</em> readable.
### [simple example](https://github.com/dominictarr/event-stream/blob/master/examples/pretty.js):
``` js
-
//pretty.js
if(!module.parent) {
@@ -124,6 +112,20 @@
`split` takes the same arguments as `string.split` except it defaults to '\n' instead of ',', and the optional `limit` parameter is ignored.
[String#split](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split)
+**NOTE** - Maintaining Line Breaks
+If you want to process each line of the stream, transform the data, reassemble, and **KEEP** the line breaks the example will look like this:
+
+```javascript
+fs.createReadStream(file, {flags: 'r'})
+ .pipe(es.split(/(\r?\n)/))
+ .pipe(es.map(function (line, cb) {
+ //do something with the line
+ cb(null, line)
+ }))
+```
+
+This technique is mentioned in the [underlying documentation](https://www.npmjs.com/package/split#keep-matched-splitter) for the split npm package.
+
## join (separator)
Create a through stream that emits `separator` between each chunk, just like Array#join.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment