Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

notification.js 3.5KB

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