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.

stream.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict'
  2. var events = require('events')
  3. var html = require('./compile/html.js')
  4. var parse = require('./parse.js')
  5. var postprocess = require('./postprocess.js')
  6. var preprocess = require('./preprocess.js')
  7. function stream(options) {
  8. var preprocess$1 = preprocess()
  9. var tokenize = parse(options).document().write
  10. var compile = html(options)
  11. var emitter = new events.EventEmitter()
  12. var ended
  13. emitter.writable = emitter.readable = true
  14. emitter.write = write
  15. emitter.end = end
  16. emitter.pipe = pipe
  17. return emitter // Write a chunk into memory.
  18. function write(chunk, encoding, callback) {
  19. if (typeof encoding === 'function') {
  20. callback = encoding
  21. encoding = undefined
  22. }
  23. if (ended) {
  24. throw new Error('Did not expect `write` after `end`')
  25. }
  26. tokenize(preprocess$1(chunk || '', encoding))
  27. if (callback) {
  28. callback()
  29. } // Signal succesful write.
  30. return true
  31. } // End the writing.
  32. // Passes all arguments to a final `write`.
  33. function end(chunk, encoding, callback) {
  34. write(chunk, encoding, callback)
  35. emitter.emit(
  36. 'data',
  37. compile(postprocess(tokenize(preprocess$1('', encoding, true))))
  38. )
  39. emitter.emit('end')
  40. ended = true
  41. return true
  42. } // Pipe the processor into a writable stream.
  43. // Basically `Stream#pipe`, but inlined and simplified to keep the bundled
  44. // size down.
  45. // See: <https://github.com/nodejs/node/blob/43a5170/lib/internal/streams/legacy.js#L13>.
  46. function pipe(dest, options) {
  47. emitter.on('data', ondata)
  48. emitter.on('error', onerror)
  49. emitter.on('end', cleanup)
  50. emitter.on('close', cleanup) // If the `end` option is not supplied, `dest.end()` will be called when the
  51. // `end` or `close` events are received.
  52. if (!dest._isStdio && (!options || options.end !== false)) {
  53. emitter.on('end', onend)
  54. }
  55. dest.on('error', onerror)
  56. dest.on('close', cleanup)
  57. dest.emit('pipe', emitter)
  58. return dest // End destination.
  59. function onend() {
  60. if (dest.end) {
  61. dest.end()
  62. }
  63. } // Handle data.
  64. function ondata(chunk) {
  65. if (dest.writable) {
  66. dest.write(chunk)
  67. }
  68. } // Clean listeners.
  69. function cleanup() {
  70. emitter.removeListener('data', ondata)
  71. emitter.removeListener('end', onend)
  72. emitter.removeListener('error', onerror)
  73. emitter.removeListener('end', cleanup)
  74. emitter.removeListener('close', cleanup)
  75. dest.removeListener('error', onerror)
  76. dest.removeListener('close', cleanup)
  77. } // Close dangling pipes and handle unheard errors.
  78. function onerror(error) {
  79. cleanup()
  80. if (!emitter.listenerCount('error')) {
  81. throw error // Unhandled stream error in pipe.
  82. }
  83. }
  84. }
  85. }
  86. module.exports = stream