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