Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

notification.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /** Heavily lifted from: https://github.com/mtharrison/susie/blob/master/lib/index.js */
  2. const Stream = require('stream')
  3. const PassThrough = Stream.PassThrough
  4. const Transform = Stream.Transform
  5. const ENDER = { event: 'end', data: '' }
  6. /**
  7. * Stringify a stream
  8. * ?: I don't really get what this is doing
  9. * @param {Stream} event
  10. * @returns {string}
  11. */
  12. const stringifyEvent = function (event) {
  13. let str = ''
  14. const endl = '\r\n'
  15. for (const i in event) {
  16. let val = event[i]
  17. if (val instanceof Buffer) {
  18. val = val.toString()
  19. }
  20. if (typeof val === 'object') {
  21. val = JSON.stringify(val)
  22. }
  23. str += i + ': ' + val + endl
  24. }
  25. str += endl
  26. return str
  27. }
  28. /**
  29. * Transform extension
  30. * ?: I don't really get what this is doing
  31. * @param {object} options
  32. * @param {object} objectMode
  33. */
  34. class Transformer extends Transform {
  35. constructor(options, objectMode) {
  36. super({ objectMode })
  37. options = options || {}
  38. this.counter = 1
  39. this.event = options.event || null
  40. this.generateId = options.generateId
  41. ? options.generateId
  42. : () => {
  43. return this.counter++
  44. }
  45. }
  46. _transform(chunk, encoding, callback) {
  47. const event = {
  48. id: this.generateId(chunk),
  49. data: chunk,
  50. }
  51. if (this.event) {
  52. event.event = this.event
  53. }
  54. this.push(stringifyEvent(event))
  55. callback()
  56. }
  57. _flush(callback) {
  58. this.push(stringifyEvent(ENDER))
  59. callback()
  60. }
  61. }
  62. /**
  63. * Take an event stream and write content to another stream
  64. * ?: Save this for future extension
  65. * @param {Stream} event stream input
  66. * @param {Stream} stream to write to
  67. */
  68. const writeEvent = function (event, stream) {
  69. if (event) {
  70. stream.write(stringifyEvent(event))
  71. } else {
  72. // closing time
  73. stream.write(stringifyEvent(ENDER))
  74. stream.end()
  75. }
  76. }
  77. /**
  78. * Callback to decorate server toolkit (h)
  79. * !: Currently we only support ObjectMode streams
  80. * ?: I don't really get what this is doing
  81. * @param {Stream} event stream input
  82. * @param {Toolkit} h hapi common response toolkit
  83. * @param {object} streamOptions
  84. */
  85. const onEvent = (event, h, streamOptions) => {
  86. // const state = h.request.plugins.notifications = h.request.plugins.notifications || {}
  87. let active
  88. if (event instanceof Stream.Readable) {
  89. if (event._readableState.objectMode) {
  90. const through = new Transformer(streamOptions, true)
  91. active = new PassThrough()
  92. through.pipe(active)
  93. event.pipe(through)
  94. }
  95. // else {
  96. // stream = new Transformer(streamOptions, false)
  97. // event.pipe(stream)
  98. // }
  99. console.log('streamOptions :', streamOptions)
  100. return h
  101. .response(active)
  102. .header('content-type', 'text/event-stream')
  103. .header('content-encoding', 'identity')
  104. }
  105. // Uncomment to do stream state stuff
  106. // handle a first object arg
  107. // if (!state.stream) {
  108. // active = new PassThrough()
  109. // state.stream = active
  110. // state.mode = 'object'
  111. // const response = h.response(active)
  112. // .header('content-type', 'text/event-stream')
  113. // .header('content-encoding', 'identity')
  114. // writeEvent(event, active)
  115. // return response
  116. // }
  117. // already have an object stream flowing, just write next event
  118. // active = state.stream
  119. // internals.writeEvent(event, active)
  120. }
  121. module.exports = { onEvent }