1
+ const LibrdKafkaError = require ( '../error' ) ;
1
2
const RdKafka = require ( '../rdkafka' ) ;
2
3
const { kafkaJSToRdKafkaConfig } = require ( './_common' ) ;
3
4
@@ -29,7 +30,7 @@ class Consumer {
29
30
async #finalizedConfig( ) {
30
31
const config = await kafkaJSToRdKafkaConfig ( this . #kJSConfig) ;
31
32
if ( this . #kJSConfig. groupId != null ) {
32
- config [ " group.id" ] = this . #kJSConfig. groupId ;
33
+ config [ ' group.id' ] = this . #kJSConfig. groupId ;
33
34
}
34
35
config [ 'offset_commit_cb' ] = true ;
35
36
return config ;
@@ -43,20 +44,20 @@ class Consumer {
43
44
this . #state = ConsumerState . CONNECTED ;
44
45
45
46
// Resolve the promise.
46
- this . #connectPromiseFunc[ " resolve" ] ( ) ;
47
+ this . #connectPromiseFunc[ ' resolve' ] ( ) ;
47
48
}
48
49
49
50
#errorCb( args ) {
50
51
console . log ( 'error' , args ) ;
51
52
if ( this . #state === ConsumerState . CONNECTING ) {
52
- this . #connectPromiseFunc[ " reject" ] ( args ) ;
53
+ this . #connectPromiseFunc[ ' reject' ] ( args ) ;
53
54
} else {
54
55
// do nothing for now.
55
56
}
56
57
}
57
58
58
59
#notImplemented( ) {
59
- throw new Error ( " Not implemented" ) ;
60
+ throw new Error ( ' Not implemented' ) ;
60
61
}
61
62
62
63
#createPayload( message ) {
@@ -66,7 +67,7 @@ class Consumer {
66
67
}
67
68
68
69
let timestamp = message . timestamp ? new Date ( message . timestamp ) . toISOString ( )
69
- : "" ;
70
+ : '' ;
70
71
71
72
var headers = undefined ;
72
73
if ( message . headers ) {
@@ -113,9 +114,17 @@ class Consumer {
113
114
} ) ;
114
115
}
115
116
117
+ #topicPartitionOffsetToRdKafka( tpo ) {
118
+ return {
119
+ topic : tpo . topic ,
120
+ partition : tpo . partition ,
121
+ offset : Number ( tpo . offset ) ,
122
+ }
123
+ }
124
+
116
125
async connect ( ) {
117
126
if ( this . #state !== ConsumerState . INIT ) {
118
- return Promise . reject ( " Connect has already been called elsewhere." ) ;
127
+ return Promise . reject ( ' Connect has already been called elsewhere.' ) ;
119
128
}
120
129
121
130
this . #state = ConsumerState . CONNECTING ;
@@ -126,9 +135,9 @@ class Consumer {
126
135
127
136
return new Promise ( ( resolve , reject ) => {
128
137
this . #connectPromiseFunc = { resolve, reject} ;
129
- console . log ( " Connecting...." ) ;
138
+ console . log ( ' Connecting....' ) ;
130
139
this . #internalClient. connect ( ) ;
131
- console . log ( " connect() called" ) ;
140
+ console . log ( ' connect() called' ) ;
132
141
} ) ;
133
142
}
134
143
@@ -142,7 +151,7 @@ class Consumer {
142
151
143
152
async run ( config ) {
144
153
if ( this . #state !== ConsumerState . CONNECTED ) {
145
- throw new Error ( " Run must be called in state CONNECTED." ) ;
154
+ throw new Error ( ' Run must be called in state CONNECTED.' ) ;
146
155
}
147
156
148
157
while ( this . #state === ConsumerState . CONNECTED ) {
@@ -156,21 +165,34 @@ class Consumer {
156
165
}
157
166
158
167
commitOffsets ( topicPartitions = null ) {
159
- if ( topicPartitions == null ) {
160
- this . #internalClient. commitSync ( ) ;
161
- } else {
162
- const topicPartitions = topicPartitions . map ( ( tpo ) => ( {
163
- topic : tpo . topic ,
164
- partition : tpo . partition ,
165
- offset : Number ( tpo . offset ) ,
166
- } ) )
167
- this . #internalClient. commitSync ( topicPartitions ) ;
168
+ try {
169
+ if ( topicPartitions == null ) {
170
+ this . #internalClient. commitSync ( ) ;
171
+ } else {
172
+ const topicPartitions = topicPartitions . map (
173
+ this . #topicPartitionOffsetToRdKafka) ;
174
+ this . #internalClient. commitSync ( topicPartitions ) ;
175
+ }
176
+ } catch ( e ) {
177
+ if ( ! e . code || e . code != LibrdKafkaError . codes . ERR__NO_OFFSET ) {
178
+ throw e ;
179
+ }
168
180
}
169
181
return Promise . resolve ( )
170
182
}
171
183
172
184
seek ( topicPartitionOffset ) {
173
- this . #notImplemented( ) ;
185
+ return new Promise ( ( resolve , reject ) => {
186
+ const rdKafkaTopicPartitionOffset =
187
+ this . #topicPartitionOffsetToRdKafka( topicPartitionOffset ) ;
188
+ this . #internalClient. seek ( rdKafkaTopicPartitionOffset , 0 , ( err ) => {
189
+ if ( err ) {
190
+ reject ( new Error ( `Seek error code ${ err . code } ` ) ) ;
191
+ } else {
192
+ resolve ( ) ;
193
+ }
194
+ } ) ;
195
+ } ) . catch ( console . error ) ; // Default handler
174
196
}
175
197
176
198
async describeGroup ( ) {
0 commit comments