@@ -65,13 +65,39 @@ class Producer extends KafkaClient {
65
65
* @TODO will delivery report be synchronized with produce?
66
66
*/
67
67
publish ( message , topic = this . _config . topics [ 0 ] , partition = - 1 , key = null , opaque = null ) {
68
- // eslint-disable-next-line new-cap
69
- return this . publishBuffer ( new Buffer . from ( message ) , topic , partition , key , opaque ) ;
68
+ return new Promise ( ( resolve , reject ) => {
69
+
70
+ try {
71
+ this . kafkaProducer . produce (
72
+ topic ,
73
+ partition ,
74
+ // eslint-disable-next-line new-cap
75
+ new Buffer . from ( message ) ,
76
+ key ,
77
+ Date . now ( ) ,
78
+ opaque
79
+ ) ;
80
+
81
+ this . kafkaProducer . prependListener ( 'delivery-report' , ( err , report ) => {
82
+ if ( err ) {
83
+ super . emitError ( err ) ;
84
+ }
85
+ console . log ( 'Delivery Report Operation:' , new Date ( ) , report ) ;
86
+ this . _deliveryReportDispatcher . next ( report ) ;
87
+ resolve ( report ) ;
88
+ } ) ;
89
+
90
+ } catch ( err ) {
91
+ console . error ( 'Producer Operation (Error)' , new Date ( ) , err ) ;
92
+ super . emitError ( err ) ;
93
+ return Promise . reject ( err ) ;
94
+ }
95
+ } ) ;
70
96
}
71
97
72
98
/**
73
99
* Publish a message
74
- * @param {Buffer } messageBuffer - message buffer to send
100
+ * @param {String } message - message to send
75
101
* @param {String } [topic=this._config.topics[0]] - topic to send to
76
102
* @param {number } [partition=-1] - optionally specify a partition for the message, this defaults to -1 - which will
77
103
* use librdkafka's default partitioner (consistent random for keyed messages, random for unkeyed messages)
@@ -80,21 +106,22 @@ class Producer extends KafkaClient {
80
106
* @return {Promise<DeliveryReport> }
81
107
* @TODO will delivery report be synchronized with produce?
82
108
*/
83
- publishBuffer ( messageBuffer , topic = this . _config . topics [ 0 ] , partition = - 1 , key = null , opaque = null ) {
109
+ publish ( message , topic = this . _config . topics [ 0 ] , partition = - 1 , key = null , opaque = null ) {
84
110
return new Promise ( ( resolve , reject ) => {
85
111
86
112
try {
87
113
this . kafkaProducer . produce (
88
114
topic ,
89
115
partition ,
90
- message ,
116
+ this . _createBuffer ( message ) ,
91
117
key ,
92
118
Date . now ( ) ,
93
119
opaque
94
120
) ;
95
121
96
122
this . kafkaProducer . prependListener ( 'delivery-report' , ( err , report ) => {
97
123
if ( err ) {
124
+ // @TODO don't error here?
98
125
super . emitError ( err ) ;
99
126
}
100
127
console . log ( 'Delivery Report Operation:' , new Date ( ) , report ) ;
@@ -105,7 +132,7 @@ class Producer extends KafkaClient {
105
132
} catch ( err ) {
106
133
console . error ( 'Producer Operation (Error)' , new Date ( ) , err ) ;
107
134
super . emitError ( err ) ;
108
- return Promise . reject ( err ) ;
135
+ return reject ( err ) ;
109
136
}
110
137
} ) ;
111
138
}
@@ -125,6 +152,39 @@ class Producer extends KafkaClient {
125
152
return this . _deliveryReportDispatcher . asObservable ( ) ;
126
153
}
127
154
155
+ /**
156
+ * checks to see if obj is a buffer
157
+ * @param {object } obj to check
158
+ * @return {boolean|* }
159
+ * @private
160
+ */
161
+ _isBuffer ( obj ) {
162
+ return obj != null && obj . constructor != null &&
163
+ typeof obj . constructor . isBuffer === 'function' && obj . constructor . isBuffer ( obj ) ;
164
+ }
165
+
166
+ /**
167
+ * create buffer from message
168
+ * @param {object } message
169
+ * @return {* }
170
+ * @private {Buffer}
171
+ */
172
+ _createBuffer ( message ) {
173
+ if ( this . _isBuffer ( message ) ) {
174
+ // eslint-disable-next-line new-cap
175
+ return message ;
176
+ } else if ( typeof message === 'string' ) {
177
+ // eslint-disable-next-line new-cap
178
+ return new Buffer . from ( message ) ;
179
+ } else {
180
+ try {
181
+ // eslint-disable-next-line new-cap
182
+ return new Buffer . from ( JSON . stringify ( message ) ) ;
183
+ } catch ( err ) {
184
+ throw new Error ( 'Invalid message input ' + err ) ;
185
+ }
186
+ }
187
+ }
128
188
129
189
/**
130
190
* Initializes the events
0 commit comments