Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
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.

readme.markdown 8.9KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # EventStream
  2. <img src=https://secure.travis-ci.org/dominictarr/event-stream.png?branch=master>
  3. [![browser status](http://ci.testling.com/dominictarr/event-stream.png)]
  4. (http://ci.testling.com/dominictarr/event-stream)
  5. [Streams](http://nodejs.org/api/stream.html "Stream") are node's best and most misunderstood idea, and
  6. _<em>EventStream</em>_ is a toolkit to make creating and working with streams <em>easy</em>.
  7. Normally, streams are only used for IO,
  8. but in event stream we send all kinds of objects down the pipe.
  9. If your application's <em>input</em> and <em>output</em> are streams,
  10. shouldn't the <em>throughput</em> be a stream too?
  11. The *EventStream* functions resemble the array functions,
  12. because Streams are like Arrays, but laid out in time, rather than in memory.
  13. <em>All the `event-stream` functions return instances of `Stream`</em>.
  14. `event-stream` creates
  15. [0.8 streams](https://github.com/joyent/node/blob/v0.8/doc/api/stream.markdown)
  16. , which are compatible with [0.10 streams](http://nodejs.org/api/stream.html "Stream").
  17. >NOTE: I shall use the term <em>"through stream"</em> to refer to a stream that is writable <em>and</em> readable.
  18. ### [simple example](https://github.com/dominictarr/event-stream/blob/master/examples/pretty.js):
  19. ``` js
  20. //pretty.js
  21. if(!module.parent) {
  22. var es = require('event-stream')
  23. var inspect = require('util').inspect
  24. process.stdin //connect streams together with `pipe`
  25. .pipe(es.split()) //split stream to break on newlines
  26. .pipe(es.map(function (data, cb) { //turn this async function into a stream
  27. cb(null
  28. , inspect(JSON.parse(data))) //render it nicely
  29. }))
  30. .pipe(process.stdout) // pipe it to stdout !
  31. }
  32. ```
  33. run it ...
  34. ``` bash
  35. curl -sS registry.npmjs.org/event-stream | node pretty.js
  36. ```
  37. [node Stream documentation](http://nodejs.org/api/stream.html)
  38. ## through (write?, end?)
  39. Re-emits data synchronously. Easy way to create synchronous through streams.
  40. Pass in optional `write` and `end` methods. They will be called in the
  41. context of the stream. Use `this.pause()` and `this.resume()` to manage flow.
  42. Check `this.paused` to see current flow state. (write always returns `!this.paused`)
  43. this function is the basis for most of the synchronous streams in `event-stream`.
  44. ``` js
  45. es.through(function write(data) {
  46. this.emit('data', data)
  47. //this.pause()
  48. },
  49. function end () { //optional
  50. this.emit('end')
  51. })
  52. ```
  53. ## map (asyncFunction)
  54. Create a through stream from an asynchronous function.
  55. ``` js
  56. var es = require('event-stream')
  57. es.map(function (data, callback) {
  58. //transform data
  59. // ...
  60. callback(null, data)
  61. })
  62. ```
  63. Each map MUST call the callback. It may callback with data, with an error or with no arguments,
  64. * `callback()` drop this data.
  65. this makes the map work like `filter`,
  66. note:`callback(null,null)` is not the same, and will emit `null`
  67. * `callback(null, newData)` turn data into newData
  68. * `callback(error)` emit an error for this item.
  69. >Note: if a callback is not called, `map` will think that it is still being processed,
  70. >every call must be answered or the stream will not know when to end.
  71. >
  72. >Also, if the callback is called more than once, every call but the first will be ignored.
  73. ## mapSync (syncFunction)
  74. Same as `map`, but the callback is called synchronously. Based on `es.through`
  75. ## split (matcher)
  76. Break up a stream and reassemble it so that each line is a chunk. matcher may be a `String`, or a `RegExp`
  77. Example, read every line in a file ...
  78. ``` js
  79. fs.createReadStream(file, {flags: 'r'})
  80. .pipe(es.split())
  81. .pipe(es.map(function (line, cb) {
  82. //do something with the line
  83. cb(null, line)
  84. }))
  85. ```
  86. `split` takes the same arguments as `string.split` except it defaults to '\n' instead of ',', and the optional `limit` parameter is ignored.
  87. [String#split](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split)
  88. ## join (separator)
  89. Create a through stream that emits `separator` between each chunk, just like Array#join.
  90. (for legacy reasons, if you pass a callback instead of a string, join is a synonym for `es.wait`)
  91. ## merge (stream1,...,streamN) or merge (streamArray)
  92. > concat → merge
  93. Merges streams into one and returns it.
  94. Incoming data will be emitted as soon it comes into - no ordering will be applied (for example: `data1 data1 data2 data1 data2` - where `data1` and `data2` is data from two streams).
  95. Counts how many streams were passed to it and emits end only when all streams emitted end.
  96. ```js
  97. es.merge(
  98. process.stdout,
  99. process.stderr
  100. ).pipe(fs.createWriteStream('output.log'));
  101. ```
  102. It can also take an Array of streams as input like this:
  103. ```js
  104. es.merge([
  105. fs.createReadStream('input1.txt'),
  106. fs.createReadStream('input2.txt')
  107. ]).pipe(fs.createWriteStream('output.log'));
  108. ```
  109. ## replace (from, to)
  110. Replace all occurrences of `from` with `to`. `from` may be a `String` or a `RegExp`.
  111. Works just like `string.split(from).join(to)`, but streaming.
  112. ## parse
  113. Convenience function for parsing JSON chunks. For newline separated JSON,
  114. use with `es.split`. By default it logs parsing errors by `console.error`;
  115. for another behaviour, transforms created by `es.parse({error: true})` will
  116. emit error events for exceptions thrown from `JSON.parse`, unmodified.
  117. ``` js
  118. fs.createReadStream(filename)
  119. .pipe(es.split()) //defaults to lines.
  120. .pipe(es.parse())
  121. ```
  122. ## stringify
  123. convert javascript objects into lines of text. The text will have whitespace escaped and have a `\n` appended, so it will be compatible with `es.parse`
  124. ``` js
  125. objectStream
  126. .pipe(es.stringify())
  127. .pipe(fs.createWriteStream(filename))
  128. ```
  129. ## readable (asyncFunction)
  130. create a readable stream (that respects pause) from an async function.
  131. while the stream is not paused,
  132. the function will be polled with `(count, callback)`,
  133. and `this` will be the readable stream.
  134. ``` js
  135. es.readable(function (count, callback) {
  136. if(streamHasEnded)
  137. return this.emit('end')
  138. //...
  139. this.emit('data', data) //use this way to emit multiple chunks per call.
  140. callback() // you MUST always call the callback eventually.
  141. // the function will not be called again until you do this.
  142. })
  143. ```
  144. you can also pass the data and the error to the callback.
  145. you may only call the callback once.
  146. calling the same callback more than once will have no effect.
  147. ## readArray (array)
  148. Create a readable stream from an Array.
  149. Just emit each item as a data event, respecting `pause` and `resume`.
  150. ``` js
  151. var es = require('event-stream')
  152. , reader = es.readArray([1,2,3])
  153. reader.pipe(...)
  154. ```
  155. If you want the stream behave like a 0.10 stream you will need to wrap it using [`Readable.wrap()`](http://nodejs.org/api/stream.html#stream_readable_wrap_stream) function. Example:
  156. ``` js
  157. var s = new stream.Readable({objectMode: true}).wrap(es.readArray([1,2,3]));
  158. ```
  159. ## writeArray (callback)
  160. create a writeable stream from a callback,
  161. all `data` events are stored in an array, which is passed to the callback when the stream ends.
  162. ``` js
  163. var es = require('event-stream')
  164. , reader = es.readArray([1, 2, 3])
  165. , writer = es.writeArray(function (err, array){
  166. //array deepEqual [1, 2, 3]
  167. })
  168. reader.pipe(writer)
  169. ```
  170. ## pause ()
  171. A stream that buffers all chunks when paused.
  172. ``` js
  173. var ps = es.pause()
  174. ps.pause() //buffer the stream, also do not allow 'end'
  175. ps.resume() //allow chunks through
  176. ```
  177. ## duplex (writeStream, readStream)
  178. Takes a writable stream and a readable stream and makes them appear as a readable writable stream.
  179. It is assumed that the two streams are connected to each other in some way.
  180. (This is used by `pipeline` and `child`.)
  181. ``` js
  182. var grep = cp.exec('grep Stream')
  183. es.duplex(grep.stdin, grep.stdout)
  184. ```
  185. ## child (child_process)
  186. Create a through stream from a child process ...
  187. ``` js
  188. var cp = require('child_process')
  189. es.child(cp.exec('grep Stream')) // a through stream
  190. ```
  191. ## wait (callback)
  192. waits for stream to emit 'end'.
  193. joins chunks of a stream into a single string or buffer.
  194. takes an optional callback, which will be passed the
  195. complete string/buffer when it receives the 'end' event.
  196. also, emits a single 'data' event.
  197. ``` js
  198. readStream.pipe(es.wait(function (err, body) {
  199. // have complete text here.
  200. }))
  201. ```
  202. # Other Stream Modules
  203. These modules are not included as a part of *EventStream* but may be
  204. useful when working with streams.
  205. ## [reduce (syncFunction, initial)](https://github.com/parshap/node-stream-reduce)
  206. Like `Array.prototype.reduce` but for streams. Given a sync reduce
  207. function and an initial value it will return a through stream that emits
  208. a single data event with the reduced value once the input stream ends.
  209. ``` js
  210. var reduce = require("stream-reduce");
  211. process.stdin.pipe(reduce(function(acc, data) {
  212. return acc + data.length;
  213. }, 0)).on("data", function(length) {
  214. console.log("stdin size:", length);
  215. });
  216. ```