1
1
const RdKafka = require ( '../rdkafka' ) ;
2
- const { kafkaJSToRdKafkaConfig, createKafkaJsErrorFromLibRdKafkaError } = require ( './_common' ) ;
2
+ const { kafkaJSToRdKafkaConfig,
3
+ createKafkaJsErrorFromLibRdKafkaError,
4
+ DefaultLogger,
5
+ checkAllowedKeys } = require ( './_common' ) ;
3
6
const error = require ( './_error' ) ;
4
7
5
8
/**
@@ -17,16 +20,16 @@ const AdminState = Object.freeze({
17
20
18
21
class Admin {
19
22
/**
20
- * kJSConfig is the merged kafkaJS config object .
21
- * @type {import("../../types/kafkajs").AdminConfig & import("../../types/kafkajs").KafkaConfig }
23
+ * The config supplied by the user .
24
+ * @type {import("../../types/kafkajs").AdminConstructorConfig|null }
22
25
*/
23
- #kJSConfig = null ;
26
+ #userConfig = null ;
24
27
25
28
/**
26
- * rdKafkaConfig contains the config objects that will be passed to node-rdkafka .
27
- * @type {{globalConfig: import("../../types/config").GlobalConfig} |null }
29
+ * The config realized after processing any compatibility options .
30
+ * @type {import("../../types/config").GlobalConfig|null }
28
31
*/
29
- #rdKafkaConfig = null ;
32
+ #internalConfig = null ;
30
33
31
34
/**
32
35
* internalClient is the node-rdkafka client used by the API.
@@ -39,25 +42,55 @@ class Admin {
39
42
*/
40
43
#state = AdminState . INIT ;
41
44
45
+ /**
46
+ * A logger for the admin client.
47
+ * @type {import("../../types/kafkajs").Logger }
48
+ */
49
+ #logger = new DefaultLogger ( ) ;
50
+
42
51
/**
43
52
* @constructor
44
- * @param {import("../../types/kafkajs").ProducerConfig } kJSConfig
53
+ * @param {import("../../types/kafkajs").AdminConstructorConfig } config
45
54
*/
46
- constructor ( kJSConfig ) {
47
- this . #kJSConfig = kJSConfig ;
55
+ constructor ( config ) {
56
+ this . #userConfig = config ;
48
57
}
49
58
50
- async #config( ) {
51
- if ( ! this . #rdKafkaConfig )
52
- this . #rdKafkaConfig = await this . #finalizedConfig( ) ;
53
- return this . #rdKafkaConfig ;
59
+ #config( ) {
60
+ if ( ! this . #internalConfig )
61
+ this . #internalConfig = this . #finalizedConfig( ) ;
62
+ return this . #internalConfig ;
54
63
}
55
64
56
- async #finalizedConfig( ) {
57
- /* This sets the common configuration options for the client. */
58
- const { globalConfig } = await kafkaJSToRdKafkaConfig ( this . #kJSConfig) ;
65
+ #kafkaJSToAdminConfig( kjsConfig ) {
66
+ if ( ! kjsConfig || Object . keys ( kjsConfig ) . length === 0 ) {
67
+ return { } ;
68
+ }
69
+
70
+ const disallowedKey = checkAllowedKeys ( 'admin' , kjsConfig ) ;
71
+ if ( disallowedKey ) {
72
+ throw new error . KafkaJSError ( CompatibilityErrorMessages . unsupportedKey ( disallowedKey ) , { code : error . ErrorCodes . ERR__INVALID_ARG } ) ;
73
+ }
74
+
75
+ const rdKafkaConfig = kafkaJSToRdKafkaConfig ( kjsConfig ) ;
76
+ return rdKafkaConfig ;
77
+ }
78
+
79
+ #finalizedConfig( ) {
80
+ let compatibleConfig = this . #kafkaJSToAdminConfig( this . #userConfig. kafkaJs ) ;
81
+
82
+ /* Set the logger's level in case we're not in compatibility mode - just set it to DEBUG, the broadest
83
+ * log level, as librdkafka will control the granularity. */
84
+ if ( ! compatibleConfig || Object . keys ( compatibleConfig ) . length === 0 ) {
85
+ this . #logger. setLogLevel ( logLevel . DEBUG ) ;
86
+ }
87
+
88
+ let rdKafkaConfig = Object . assign ( compatibleConfig , this . #userConfig) ;
89
+
90
+ /* Delete properties which are already processed, or cannot be passed to node-rdkafka */
91
+ delete rdKafkaConfig . kafkaJs ;
59
92
60
- return { globalConfig } ;
93
+ return rdKafkaConfig ;
61
94
}
62
95
63
96
/**
@@ -71,12 +104,12 @@ class Admin {
71
104
72
105
this . #state = AdminState . CONNECTING ;
73
106
74
- const { globalConfig } = await this . #config( ) ;
107
+ const config = this . #config( ) ;
75
108
76
109
return new Promise ( ( resolve , reject ) => {
77
110
try {
78
111
/* AdminClient creation is a synchronous operation for node-rdkafka */
79
- this . #internalClient = RdKafka . AdminClient . create ( globalConfig ) ;
112
+ this . #internalClient = RdKafka . AdminClient . create ( config ) ;
80
113
this . #state = AdminState . CONNECTED ;
81
114
resolve ( ) ;
82
115
} catch ( err ) {
0 commit comments