@@ -43,7 +43,37 @@ function createSerializer(serializer) {
43
43
// operation if the number of parameters the function accepts is > 1
44
44
return {
45
45
apply : applyFn ,
46
- async : serializer . length > 1
46
+ async : serializer . length > 1 ,
47
+ needsTopic : false
48
+ } ;
49
+ }
50
+
51
+ /**
52
+ * Create a serializer that additionally takes the topic name
53
+ *
54
+ * Method simply wraps a serializer provided by a user
55
+ * so it adds context to the error
56
+ *
57
+ * @returns {function } Serialization function
58
+ */
59
+ function createTopicSerializer ( serializer ) {
60
+ var applyFn = function serializationWrapper ( t , v , cb ) {
61
+ try {
62
+ return cb ? serializer ( t , v , cb ) : serializer ( t , v ) ;
63
+ } catch ( e ) {
64
+ var modifiedError = new Error ( 'Could not serialize value: ' + e . message ) ;
65
+ modifiedError . value = v ;
66
+ modifiedError . serializer = serializer ;
67
+ throw modifiedError ;
68
+ }
69
+ } ;
70
+
71
+ // We can check how many parameters the function has and activate the asynchronous
72
+ // operation if the number of parameters the function accepts is > 2
73
+ return {
74
+ apply : applyFn ,
75
+ async : serializer . length > 2 ,
76
+ needsTopic : true
47
77
} ;
48
78
}
49
79
@@ -256,10 +286,20 @@ HighLevelProducer.prototype._modifiedProduce = function(topic, partition, messag
256
286
257
287
try {
258
288
if ( this . valueSerializer . async ) {
259
- // If this is async we need to give it a callback
260
- this . valueSerializer . apply ( message , valueSerializerCallback ) ;
289
+ if ( this . valueSerializer . needsTopic ) {
290
+ // If this is async we need to give it a callback
291
+ this . valueSerializer . apply ( topic , message , valueSerializerCallback ) ;
292
+ } else {
293
+ // If this is async we need to give it a callback
294
+ this . valueSerializer . apply ( message , valueSerializerCallback ) ;
295
+ }
261
296
} else {
262
- var serializedValue = this . valueSerializer . apply ( message ) ;
297
+ var serializedValue ;
298
+ if ( this . valueSerializer . needsTopic ) {
299
+ serializedValue = this . valueSerializer . apply ( topic , message ) ;
300
+ } else {
301
+ serializedValue = this . valueSerializer . apply ( message ) ;
302
+ }
263
303
// Check if we were returned a promise in order to support promise behavior
264
304
if ( serializedValue &&
265
305
typeof serializedValue . then === 'function' &&
@@ -272,10 +312,20 @@ HighLevelProducer.prototype._modifiedProduce = function(topic, partition, messag
272
312
}
273
313
274
314
if ( this . keySerializer . async ) {
275
- // If this is async we need to give it a callback
276
- this . keySerializer . apply ( key , keySerializerCallback ) ;
315
+ if ( this . valueSerializer . needsTopic ) {
316
+ // If this is async we need to give it a callback
317
+ this . keySerializer . apply ( topic , key , keySerializerCallback ) ;
318
+ } else {
319
+ // If this is async we need to give it a callback
320
+ this . keySerializer . apply ( key , keySerializerCallback ) ;
321
+ }
277
322
} else {
278
- var serializedKey = this . keySerializer . apply ( key ) ;
323
+ var serializedKey ;
324
+ if ( this . valueSerializer . needsTopic ) {
325
+ serializedKey = this . keySerializer . apply ( topic , key ) ;
326
+ } else {
327
+ serializedKey = this . keySerializer . apply ( key ) ;
328
+ }
279
329
// Check if we were returned a promise in order to support promise behavior
280
330
if ( serializedKey &&
281
331
typeof serializedKey . then === 'function' &&
@@ -319,3 +369,21 @@ HighLevelProducer.prototype.setKeySerializer = function(serializer) {
319
369
HighLevelProducer . prototype . setValueSerializer = function ( serializer ) {
320
370
this . valueSerializer = createSerializer ( serializer ) ;
321
371
} ;
372
+
373
+ /**
374
+ * Set the topic-key serializer
375
+ *
376
+ * A serializer that takes the topic name in addition to the key.
377
+ */
378
+ HighLevelProducer . prototype . setTopicKeySerializer = function ( serializer ) {
379
+ this . keySerializer = createTopicSerializer ( serializer ) ;
380
+ } ;
381
+
382
+ /**
383
+ * Set the topic-value serializer
384
+ *
385
+ * A serializer that takes the topic name in addition to the value.
386
+ */
387
+ HighLevelProducer . prototype . setTopicValueSerializer = function ( serializer ) {
388
+ this . valueSerializer = createTopicSerializer ( serializer ) ;
389
+ } ;
0 commit comments