@@ -12,13 +12,19 @@ limitations under the License.
12
12
*/
13
13
14
14
import GRPCClient from "./GRPCClient" ;
15
- import { PublishEventRequest } from "../../../proto/dapr/proto/runtime/v1/dapr_pb" ;
15
+ import {
16
+ BulkPublishRequest ,
17
+ BulkPublishRequestEntry ,
18
+ PublishEventRequest ,
19
+ } from "../../../proto/dapr/proto/runtime/v1/dapr_pb" ;
16
20
import IClientPubSub from "../../../interfaces/Client/IClientPubSub" ;
17
21
import { Logger } from "../../../logger/Logger" ;
18
22
import * as SerializerUtil from "../../../utils/Serializer.util" ;
19
23
import { KeyValueType } from "../../../types/KeyValue.type" ;
20
- import { addMetadataToMap } from "../../../utils/Client.util" ;
24
+ import { addMetadataToMap , getBulkPublishEntries , getBulkPublishResponse } from "../../../utils/Client.util" ;
21
25
import { PubSubPublishResponseType } from "../../../types/pubsub/PubSubPublishResponse.type" ;
26
+ import { PubSubBulkPublishResponse } from "../../../types/pubsub/PubSubBulkPublishResponse.type" ;
27
+ import { PubSubBulkPublishMessage } from "../../../types/pubsub/PubSubBulkPublishMessage.type" ;
22
28
23
29
// https://docs.dapr.io/reference/api/pubsub_api/
24
30
export default class GRPCClientPubSub implements IClientPubSub {
@@ -61,4 +67,54 @@ export default class GRPCClientPubSub implements IClientPubSub {
61
67
} ) ;
62
68
} ) ;
63
69
}
70
+
71
+ async publishBulk (
72
+ pubSubName : string ,
73
+ topic : string ,
74
+ messages : PubSubBulkPublishMessage [ ] ,
75
+ metadata ?: KeyValueType | undefined ,
76
+ ) : Promise < PubSubBulkPublishResponse > {
77
+ const bulkPublishRequest = new BulkPublishRequest ( ) ;
78
+ bulkPublishRequest . setPubsubName ( pubSubName ) ;
79
+ bulkPublishRequest . setTopic ( topic ) ;
80
+
81
+ const entries = getBulkPublishEntries ( messages ) ;
82
+ const serializedEntries = entries . map ( ( entry ) => {
83
+ const serialized = SerializerUtil . serializeGrpc ( entry . event ) ;
84
+ const bulkPublishEntry = new BulkPublishRequestEntry ( ) ;
85
+ bulkPublishEntry . setEvent ( serialized . serializedData ) ;
86
+ bulkPublishEntry . setContentType ( serialized . contentType ) ;
87
+ bulkPublishEntry . setEntryId ( entry . entryID ) ;
88
+ return bulkPublishEntry ;
89
+ } ) ;
90
+
91
+ bulkPublishRequest . setEntriesList ( serializedEntries ) ;
92
+ addMetadataToMap ( bulkPublishRequest . getMetadataMap ( ) , metadata ) ;
93
+
94
+ const client = await this . client . getClient ( ) ;
95
+ return new Promise ( ( resolve , _reject ) => {
96
+ client . bulkPublishEventAlpha1 ( bulkPublishRequest , ( err , res ) => {
97
+ if ( err ) {
98
+ return resolve ( getBulkPublishResponse ( { entries : entries , error : err } ) ) ;
99
+ }
100
+
101
+ const failedEntries = res . getFailedentriesList ( ) ;
102
+ if ( failedEntries . length > 0 ) {
103
+ return resolve (
104
+ getBulkPublishResponse ( {
105
+ entries : entries ,
106
+ response : {
107
+ failedEntries : failedEntries . map ( ( entry ) => ( {
108
+ entryID : entry . getEntryId ( ) ,
109
+ error : entry . getError ( ) ,
110
+ } ) ) ,
111
+ } ,
112
+ } ) ,
113
+ ) ;
114
+ }
115
+
116
+ return resolve ( { failedMessages : [ ] } ) ;
117
+ } ) ;
118
+ } ) ;
119
+ }
64
120
}
0 commit comments