|
|
@@ -1,4 +1,4 @@
|
|
1
|
|
-const { allStreams } = require('../utils')
|
|
|
1
|
+const _allStreams = {}
|
|
2
|
2
|
|
|
3
|
3
|
const NotificationRoute = require('../routes/notification')
|
|
4
|
4
|
|
|
|
@@ -16,7 +16,7 @@ const ENDER = { event: 'end', data: '' }
|
|
16
|
16
|
* @param {Stream} event
|
|
17
|
17
|
* @returns {string}
|
|
18
|
18
|
*/
|
|
19
|
|
-const stringifyEvent = function (event) {
|
|
|
19
|
+const _stringifyEvent = function (event) {
|
|
20
|
20
|
let str = ''
|
|
21
|
21
|
const endl = '\r\n'
|
|
22
|
22
|
for (const i in event) {
|
|
|
@@ -57,11 +57,11 @@ class Transformer extends Transform {
|
|
57
|
57
|
if (this.event) {
|
|
58
|
58
|
event.event = this.event
|
|
59
|
59
|
}
|
|
60
|
|
- this.push(stringifyEvent(event))
|
|
|
60
|
+ this.push(_stringifyEvent(event))
|
|
61
|
61
|
callback()
|
|
62
|
62
|
}
|
|
63
|
63
|
_flush(callback) {
|
|
64
|
|
- this.push(stringifyEvent(ENDER))
|
|
|
64
|
+ this.push(_stringifyEvent(ENDER))
|
|
65
|
65
|
callback()
|
|
66
|
66
|
}
|
|
67
|
67
|
}
|
|
|
@@ -74,7 +74,7 @@ class Transformer extends Transform {
|
|
74
|
74
|
* @param {Toolkit} h hapi common response toolkit
|
|
75
|
75
|
* @param {object} streamOptions
|
|
76
|
76
|
*/
|
|
77
|
|
-const onEvent = (event, h, streamOptions) => {
|
|
|
77
|
+const _event = (event, h, streamOptions) => {
|
|
78
|
78
|
let active
|
|
79
|
79
|
if (event instanceof Stream.Readable) {
|
|
80
|
80
|
if (event._readableState.objectMode) {
|
|
|
@@ -90,12 +90,30 @@ const onEvent = (event, h, streamOptions) => {
|
|
90
|
90
|
}
|
|
91
|
91
|
}
|
|
92
|
92
|
|
|
|
93
|
+/**
|
|
|
94
|
+ * Takes an open HTTP stream and writes
|
|
|
95
|
+ * a msg to it, then fires notification plugin's
|
|
|
96
|
+ * _event callback
|
|
|
97
|
+ * @param {object} msg you want to send
|
|
|
98
|
+ * @param {string} name <profileId>.<eventType>
|
|
|
99
|
+ * @param {boolean} shouldInitialize
|
|
|
100
|
+ * @param {Toolkit} h hapi common response toolkit
|
|
|
101
|
+ */
|
|
|
102
|
+const onNotify = (name, msg, h, shouldInitialize = false) => {
|
|
|
103
|
+ if (shouldInitialize) {
|
|
|
104
|
+ _allStreams[name] = new PassThrough({ objectMode: true })
|
|
|
105
|
+ }
|
|
|
106
|
+ if (!_allStreams[name]) return
|
|
|
107
|
+ _allStreams[name].write(msg)
|
|
|
108
|
+
|
|
|
109
|
+ return _event(_allStreams[name], h, { event: name })
|
|
|
110
|
+}
|
|
|
111
|
+
|
|
93
|
112
|
module.exports = {
|
|
94
|
113
|
name: 'notification-plugin',
|
|
95
|
114
|
version: '1.0.0',
|
|
96
|
115
|
register: async server => {
|
|
97
|
116
|
await server.route(NotificationRoute)
|
|
98
|
|
- server.decorate('toolkit', 'event', onEvent)
|
|
99
|
|
- server.expose('streams', allStreams)
|
|
|
117
|
+ server.method('notify', onNotify)
|
|
100
|
118
|
},
|
|
101
|
119
|
}
|