Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.md 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. # trough
  2. [![Build][build-badge]][build]
  3. [![Coverage][coverage-badge]][coverage]
  4. [![Downloads][downloads-badge]][downloads]
  5. [![Size][size-badge]][size]
  6. > **trough** /trôf/ — a channel used to convey a liquid.
  7. `trough` is like [`ware`][ware] with less sugar, and middleware functions can
  8. change the input of the next.
  9. ## Install
  10. [npm][]:
  11. ```sh
  12. npm install trough
  13. ```
  14. ## Use
  15. ```js
  16. var fs = require('fs')
  17. var path = require('path')
  18. var trough = require('trough')
  19. var pipeline = trough()
  20. .use(function(fileName) {
  21. console.log('Checking… ' + fileName)
  22. })
  23. .use(function(fileName) {
  24. return path.join(process.cwd(), fileName)
  25. })
  26. .use(function(filePath, next) {
  27. fs.stat(filePath, function(err, stats) {
  28. next(err, {filePath, stats})
  29. })
  30. })
  31. .use(function(ctx, next) {
  32. if (ctx.stats.isFile()) {
  33. fs.readFile(ctx.filePath, next)
  34. } else {
  35. next(new Error('Expected file'))
  36. }
  37. })
  38. pipeline.run('readme.md', console.log)
  39. pipeline.run('node_modules', console.log)
  40. ```
  41. Yields:
  42. ```txt
  43. Checking… readme.md
  44. Checking… node_modules
  45. Error: Expected file
  46. at ~/example.js:21:12
  47. at wrapped (~/node_modules/trough/index.js:93:19)
  48. at next (~/node_modules/trough/index.js:56:24)
  49. at done (~/node_modules/trough/index.js:124:12)
  50. at ~/node_modules/example.js:14:7
  51. at FSReqWrap.oncomplete (fs.js:153:5)
  52. null <Buffer 23 20 74 72 6f 75 67 68 20 5b 21 5b 42 75 69 6c 64 20 53 74 61 74 75 73 5d 5b 74 72 61 76 69 73 2d 62 61 64 67 65 5d 5d 5b 74 72 61 76 69 73 5d 20 5b ... >
  53. ```
  54. ## API
  55. ### `trough()`
  56. Create a new [`Trough`][trough].
  57. #### `trough.wrap(middleware, callback[, …input])`
  58. Call `middleware` with all input.
  59. If `middleware` accepts more arguments than given in input, and extra `done`
  60. function is passed in after the input when calling it.
  61. It must be called.
  62. The first value in `input` is called the main input value.
  63. All other input values are called the rest input values.
  64. The values given to `callback` are the input values, merged with every non-nully
  65. output value.
  66. * If `middleware` throws an error, returns a promise that is rejected, or
  67. calls the given `done` function with an error, `callback` is invoked with
  68. that error
  69. * If `middleware` returns a value or returns a promise that is resolved, that
  70. value is the main output value
  71. * If `middleware` calls `done`, all non-nully values except for the first one
  72. (the error) overwrite the output values
  73. ### `Trough`
  74. A pipeline.
  75. #### `Trough#run([input…, ]done)`
  76. Run the pipeline (all [`use()`][use]d middleware).
  77. Invokes [`done`][done] on completion with either an error or the output of the
  78. last middleware.
  79. > Note!
  80. > as the length of input defines whether [async][] functions get a `next`
  81. > function, it’s recommended to keep `input` at one value normally.
  82. ##### `function done(err?, [output…])`
  83. The final handler passed to [`run()`][run], invoked with an error if a
  84. [middleware function][fn] rejected, passed, or threw one, or the output of the
  85. last middleware function.
  86. #### `Trough#use(fn)`
  87. Add `fn`, a [middleware function][fn], to the pipeline.
  88. ##### `function fn([input…, ][next])`
  89. A middleware function invoked with the output of its predecessor.
  90. ###### Synchronous
  91. If `fn` returns or throws an error, the pipeline fails and `done` is invoked
  92. with that error.
  93. If `fn` returns a value (neither `null` nor `undefined`), the first `input` of
  94. the next function is set to that value (all other `input` is passed through).
  95. The following example shows how returning an error stops the pipeline:
  96. ```js
  97. var trough = require('trough')
  98. trough()
  99. .use(function(val) {
  100. return new Error('Got: ' + val)
  101. })
  102. .run('some value', console.log)
  103. ```
  104. Yields:
  105. ```txt
  106. Error: Got: some value
  107. at ~/example.js:5:12
  108. ```
  109. The following example shows how throwing an error stops the pipeline:
  110. ```js
  111. var trough = require('trough')
  112. trough()
  113. .use(function(val) {
  114. throw new Error('Got: ' + val)
  115. })
  116. .run('more value', console.log)
  117. ```
  118. Yields:
  119. ```txt
  120. Error: Got: more value
  121. at ~/example.js:5:11
  122. ```
  123. The following example shows how the first output can be modified:
  124. ```js
  125. var trough = require('trough')
  126. trough()
  127. .use(function(val) {
  128. return 'even ' + val
  129. })
  130. .run('more value', 'untouched', console.log)
  131. ```
  132. Yields:
  133. ```txt
  134. null 'even more value' 'untouched'
  135. ```
  136. ###### Promise
  137. If `fn` returns a promise, and that promise rejects, the pipeline fails and
  138. `done` is invoked with the rejected value.
  139. If `fn` returns a promise, and that promise resolves with a value (neither
  140. `null` nor `undefined`), the first `input` of the next function is set to that
  141. value (all other `input` is passed through).
  142. The following example shows how rejecting a promise stops the pipeline:
  143. ```js
  144. var trough = require('trough')
  145. trough()
  146. .use(function(val) {
  147. return new Promise(function(resolve, reject) {
  148. reject('Got: ' + val)
  149. })
  150. })
  151. .run('val', console.log)
  152. ```
  153. Yields:
  154. ```txt
  155. Got: val
  156. ```
  157. The following example shows how the input isn’t touched by resolving to `null`.
  158. ```js
  159. var trough = require('trough')
  160. trough()
  161. .use(function() {
  162. return new Promise(function(resolve) {
  163. setTimeout(function() {
  164. resolve(null)
  165. }, 100)
  166. })
  167. })
  168. .run('Input', console.log)
  169. ```
  170. Yields:
  171. ```txt
  172. null 'Input'
  173. ```
  174. ###### Asynchronous
  175. If `fn` accepts one more argument than the given `input`, a `next` function is
  176. given (after the input). `next` must be called, but doesn’t have to be called
  177. async.
  178. If `next` is given a value (neither `null` nor `undefined`) as its first
  179. argument, the pipeline fails and `done` is invoked with that value.
  180. If `next` is given no value (either `null` or `undefined`) as the first
  181. argument, all following non-nully values change the input of the following
  182. function, and all nully values default to the `input`.
  183. The following example shows how passing a first argument stops the pipeline:
  184. ```js
  185. var trough = require('trough')
  186. trough()
  187. .use(function(val, next) {
  188. next(new Error('Got: ' + val))
  189. })
  190. .run('val', console.log)
  191. ```
  192. Yields:
  193. ```txt
  194. Error: Got: val
  195. at ~/example.js:5:10
  196. ```
  197. The following example shows how more values than the input are passed.
  198. ```js
  199. var trough = require('trough')
  200. trough()
  201. .use(function(val, next) {
  202. setTimeout(function() {
  203. next(null, null, 'values')
  204. }, 100)
  205. })
  206. .run('some', console.log)
  207. ```
  208. Yields:
  209. ```txt
  210. null 'some' 'values'
  211. ```
  212. ## License
  213. [MIT][license] © [Titus Wormer][author]
  214. <!-- Definitions -->
  215. [build-badge]: https://img.shields.io/travis/wooorm/trough.svg
  216. [build]: https://travis-ci.org/wooorm/trough
  217. [coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/trough.svg
  218. [coverage]: https://codecov.io/github/wooorm/trough
  219. [downloads-badge]: https://img.shields.io/npm/dm/trough.svg
  220. [downloads]: https://www.npmjs.com/package/trough
  221. [size-badge]: https://img.shields.io/bundlephobia/minzip/trough.svg
  222. [size]: https://bundlephobia.com/result?p=trough
  223. [npm]: https://docs.npmjs.com/cli/install
  224. [license]: license
  225. [author]: https://wooorm.com
  226. [ware]: https://github.com/segmentio/ware
  227. [trough]: #trough-1
  228. [use]: #troughusefn
  229. [run]: #troughruninput-done
  230. [fn]: #function-fninput-next
  231. [done]: #function-doneerr-output
  232. [async]: #asynchronous