rocketmq-client-nodejs 1.0.3 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/README.md +84 -1
  2. package/dist/client/BaseClient.d.ts +24 -2
  3. package/dist/client/BaseClient.js +173 -28
  4. package/dist/client/ClientId.js +2 -2
  5. package/dist/client/Logger.d.ts +1 -0
  6. package/dist/client/Logger.js +1 -1
  7. package/dist/client/RpcClient.d.ts +3 -1
  8. package/dist/client/RpcClient.js +23 -1
  9. package/dist/client/RpcClientManager.d.ts +3 -1
  10. package/dist/client/RpcClientManager.js +23 -4
  11. package/dist/client/TelemetrySession.d.ts +1 -0
  12. package/dist/client/TelemetrySession.js +47 -9
  13. package/dist/consumer/Assignment.d.ts +24 -0
  14. package/dist/consumer/Assignment.js +40 -0
  15. package/dist/consumer/Assignments.d.ts +25 -0
  16. package/dist/consumer/Assignments.js +55 -0
  17. package/dist/consumer/ConsumeResult.d.ts +20 -0
  18. package/dist/consumer/ConsumeResult.js +25 -0
  19. package/dist/consumer/ConsumeService.d.ts +28 -0
  20. package/dist/consumer/ConsumeService.js +53 -0
  21. package/dist/consumer/ConsumeTask.d.ts +24 -0
  22. package/dist/consumer/ConsumeTask.js +39 -0
  23. package/dist/consumer/Consumer.d.ts +19 -1
  24. package/dist/consumer/Consumer.js +64 -21
  25. package/dist/consumer/FifoConsumeService.d.ts +25 -0
  26. package/dist/consumer/FifoConsumeService.js +130 -0
  27. package/dist/consumer/FilterExpression.d.ts +2 -0
  28. package/dist/consumer/FilterExpression.js +18 -1
  29. package/dist/consumer/LitePushConsumer.d.ts +161 -0
  30. package/dist/consumer/LitePushConsumer.js +172 -0
  31. package/dist/consumer/LitePushConsumerImpl.d.ts +167 -0
  32. package/dist/consumer/LitePushConsumerImpl.js +193 -0
  33. package/dist/consumer/LiteSubscriptionManager.d.ts +97 -0
  34. package/dist/consumer/LiteSubscriptionManager.js +238 -0
  35. package/dist/consumer/MessageListener.d.ts +21 -0
  36. package/dist/consumer/MessageListener.js +19 -0
  37. package/dist/consumer/OffsetOption.d.ts +109 -0
  38. package/dist/consumer/OffsetOption.js +191 -0
  39. package/dist/consumer/ProcessQueue.d.ts +39 -0
  40. package/dist/consumer/ProcessQueue.js +293 -0
  41. package/dist/consumer/PushConsumer.d.ts +88 -0
  42. package/dist/consumer/PushConsumer.js +449 -0
  43. package/dist/consumer/PushSubscriptionSettings.d.ts +31 -0
  44. package/dist/consumer/PushSubscriptionSettings.js +97 -0
  45. package/dist/consumer/SimpleConsumer.d.ts +12 -1
  46. package/dist/consumer/SimpleConsumer.js +54 -10
  47. package/dist/consumer/SimpleSubscriptionSettings.d.ts +2 -2
  48. package/dist/consumer/SimpleSubscriptionSettings.js +3 -3
  49. package/dist/consumer/StandardConsumeService.d.ts +25 -0
  50. package/dist/consumer/StandardConsumeService.js +50 -0
  51. package/dist/consumer/index.d.ts +15 -0
  52. package/dist/consumer/index.js +19 -1
  53. package/dist/exception/InternalErrorException.d.ts +5 -0
  54. package/dist/exception/InternalErrorException.js +10 -1
  55. package/dist/exception/LiteSubscriptionQuotaExceededException.d.ts +23 -0
  56. package/dist/exception/LiteSubscriptionQuotaExceededException.js +31 -0
  57. package/dist/exception/LiteTopicQuotaExceededException.d.ts +23 -0
  58. package/dist/exception/LiteTopicQuotaExceededException.js +31 -0
  59. package/dist/exception/StatusChecker.js +4 -1
  60. package/dist/exception/index.d.ts +2 -0
  61. package/dist/exception/index.js +3 -1
  62. package/dist/index.d.ts +1 -0
  63. package/dist/index.js +2 -1
  64. package/dist/message/Message.d.ts +8 -0
  65. package/dist/message/Message.js +39 -6
  66. package/dist/message/MessageId.d.ts +2 -0
  67. package/dist/message/MessageId.js +17 -1
  68. package/dist/message/MessageView.d.ts +86 -0
  69. package/dist/message/MessageView.js +115 -1
  70. package/dist/message/PublishingMessage.js +21 -5
  71. package/dist/producer/Producer.d.ts +21 -2
  72. package/dist/producer/Producer.js +120 -25
  73. package/dist/producer/PublishingLoadBalancer.d.ts +2 -0
  74. package/dist/producer/PublishingLoadBalancer.js +23 -1
  75. package/dist/producer/RecallReceipt.d.ts +25 -0
  76. package/dist/producer/RecallReceipt.js +34 -0
  77. package/dist/producer/SendReceipt.d.ts +2 -1
  78. package/dist/producer/SendReceipt.js +6 -3
  79. package/dist/producer/Transaction.js +7 -1
  80. package/dist/producer/index.d.ts +1 -0
  81. package/dist/producer/index.js +2 -1
  82. package/dist/route/Broker.d.ts +2 -0
  83. package/dist/route/Broker.js +21 -1
  84. package/dist/route/Endpoints.d.ts +2 -0
  85. package/dist/route/Endpoints.js +36 -1
  86. package/dist/route/MessageQueue.d.ts +2 -0
  87. package/dist/route/MessageQueue.js +23 -1
  88. package/dist/route/Resource.d.ts +54 -0
  89. package/dist/route/Resource.js +100 -0
  90. package/dist/route/TopicRouteData.d.ts +12 -0
  91. package/dist/route/TopicRouteData.js +37 -1
  92. package/dist/route/index.d.ts +1 -0
  93. package/dist/route/index.js +2 -1
  94. package/dist/util/index.d.ts +8 -0
  95. package/dist/util/index.js +20 -1
  96. package/package.json +2 -2
  97. package/proto/apache/rocketmq/v2/definition.proto +56 -0
  98. package/proto/apache/rocketmq/v2/definition_pb.d.ts +100 -0
  99. package/proto/apache/rocketmq/v2/definition_pb.js +570 -4
  100. package/proto/apache/rocketmq/v2/service.proto +65 -0
  101. package/proto/apache/rocketmq/v2/service_grpc_pb.d.ts +34 -0
  102. package/proto/apache/rocketmq/v2/service_grpc_pb.js +81 -4
  103. package/proto/apache/rocketmq/v2/service_pb.d.ts +211 -0
  104. package/proto/apache/rocketmq/v2/service_pb.js +1691 -25
  105. package/src/client/BaseClient.ts +196 -28
  106. package/src/client/ClientId.ts +1 -1
  107. package/src/client/Logger.ts +1 -0
  108. package/src/client/RpcClient.ts +26 -0
  109. package/src/client/RpcClientManager.ts +27 -3
  110. package/src/client/TelemetrySession.ts +54 -8
  111. package/src/consumer/Assignment.ts +40 -0
  112. package/src/consumer/Assignments.ts +55 -0
  113. package/src/consumer/ConsumeResult.ts +21 -0
  114. package/src/consumer/ConsumeService.ts +72 -0
  115. package/src/consumer/ConsumeTask.ts +39 -0
  116. package/src/consumer/Consumer.ts +76 -20
  117. package/src/consumer/FifoConsumeService.ts +146 -0
  118. package/src/consumer/FilterExpression.ts +17 -0
  119. package/src/consumer/LitePushConsumer.ts +256 -0
  120. package/src/consumer/LitePushConsumerImpl.ts +245 -0
  121. package/src/consumer/LiteSubscriptionManager.ts +354 -0
  122. package/src/consumer/MessageListener.ts +23 -0
  123. package/src/consumer/OffsetOption.ts +203 -0
  124. package/src/consumer/ProcessQueue.ts +339 -0
  125. package/src/consumer/PushConsumer.ts +563 -0
  126. package/src/consumer/PushSubscriptionSettings.ts +124 -0
  127. package/src/consumer/SimpleConsumer.ts +68 -11
  128. package/src/consumer/SimpleSubscriptionSettings.ts +2 -2
  129. package/src/consumer/StandardConsumeService.ts +57 -0
  130. package/src/consumer/index.ts +15 -0
  131. package/src/exception/InternalErrorException.ts +10 -0
  132. package/src/exception/LiteSubscriptionQuotaExceededException.ts +28 -0
  133. package/src/exception/LiteTopicQuotaExceededException.ts +28 -0
  134. package/src/exception/StatusChecker.ts +3 -0
  135. package/src/exception/index.ts +2 -0
  136. package/src/index.ts +1 -0
  137. package/src/message/Message.ts +46 -5
  138. package/src/message/MessageId.ts +16 -0
  139. package/src/message/MessageView.ts +127 -0
  140. package/src/message/PublishingMessage.ts +20 -4
  141. package/src/producer/Producer.ts +144 -24
  142. package/src/producer/PublishingLoadBalancer.ts +21 -0
  143. package/src/producer/RecallReceipt.ts +32 -0
  144. package/src/producer/SendReceipt.ts +5 -2
  145. package/src/producer/Transaction.ts +16 -0
  146. package/src/producer/index.ts +1 -0
  147. package/src/route/Broker.ts +20 -0
  148. package/src/route/Endpoints.ts +32 -0
  149. package/src/route/MessageQueue.ts +23 -1
  150. package/src/route/Resource.ts +104 -0
  151. package/src/route/TopicRouteData.ts +37 -0
  152. package/src/route/index.ts +1 -0
  153. package/src/util/index.ts +19 -0
@@ -24,6 +24,11 @@ import { FilterExpression } from './FilterExpression';
24
24
  import { SimpleSubscriptionSettings } from './SimpleSubscriptionSettings';
25
25
  import { SubscriptionLoadBalancer } from './SubscriptionLoadBalancer';
26
26
  import { Consumer, ConsumerOptions } from './Consumer';
27
+ import { InternalErrorException } from '../exception';
28
+
29
+ const RANDOM_INDEX = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
30
+ const DEFAULT_MAX_RETRY_ATTEMPTS = 3;
31
+ const RETRY_DELAY_MS = 1000;
27
32
 
28
33
  export interface SimpleConsumerOptions extends ConsumerOptions {
29
34
  /**
@@ -39,6 +44,10 @@ export interface SimpleConsumerOptions extends ConsumerOptions {
39
44
  * set await duration for long-polling, default is 30000ms
40
45
  */
41
46
  awaitDuration?: number;
47
+ /**
48
+ * max retry attempts for temporary errors (e.g., internal server error), default is 3
49
+ */
50
+ maxRetryAttempts?: number;
42
51
  }
43
52
 
44
53
  export class SimpleConsumer extends Consumer {
@@ -46,7 +55,8 @@ export class SimpleConsumer extends Consumer {
46
55
  readonly #subscriptionExpressions = new Map<string, FilterExpression>();
47
56
  readonly #subscriptionRouteDataCache = new Map<string, SubscriptionLoadBalancer>();
48
57
  readonly #awaitDuration: number;
49
- #topicIndex = 0;
58
+ readonly #maxRetryAttempts: number;
59
+ #topicIndex = RANDOM_INDEX;
50
60
 
51
61
  constructor(options: SimpleConsumerOptions) {
52
62
  options.topics = Array.from(options.subscriptions.keys());
@@ -60,17 +70,28 @@ export class SimpleConsumer extends Consumer {
60
70
  }
61
71
  }
62
72
  this.#awaitDuration = options.awaitDuration ?? 30000;
63
- this.#simpleSubscriptionSettings = new SimpleSubscriptionSettings(options.namespace, this.clientId, this.endpoints,
64
- this.consumerGroup, this.requestTimeout, this.#awaitDuration, this.#subscriptionExpressions);
73
+ this.#maxRetryAttempts = options.maxRetryAttempts ?? DEFAULT_MAX_RETRY_ATTEMPTS;
74
+ this.#simpleSubscriptionSettings = new SimpleSubscriptionSettings(options.namespace, this.clientId,
75
+ this.getClientType(), this.endpoints, this.consumerGroup, this.requestTimeout,
76
+ this.#awaitDuration, this.#subscriptionExpressions);
65
77
  }
66
78
 
67
79
  protected getSettings() {
68
80
  return this.#simpleSubscriptionSettings;
69
81
  }
70
82
 
83
+ /**
84
+ * Get the client type.
85
+ *
86
+ * @return The client type identifier for simple consumer
87
+ */
88
+ protected getClientType(): ClientType {
89
+ return ClientType.SIMPLE_CONSUMER;
90
+ }
91
+
71
92
  protected wrapHeartbeatRequest() {
72
93
  return new HeartbeatRequest()
73
- .setClientType(ClientType.SIMPLE_CONSUMER)
94
+ .setClientType(this.getClientType())
74
95
  .setGroup(createResource(this.consumerGroup));
75
96
  }
76
97
 
@@ -113,28 +134,64 @@ export class SimpleConsumer extends Consumer {
113
134
  }
114
135
 
115
136
  async receive(maxMessageNum = 10, invisibleDuration = 15000) {
137
+ if (maxMessageNum <= 0) {
138
+ throw new Error(`maxMessageNum must be greater than 0, but got ${maxMessageNum}`);
139
+ }
140
+
116
141
  const topic = this.#nextTopic();
117
- const filterExpression = this.#subscriptionExpressions.get(topic)!;
142
+ const filterExpression = this.#subscriptionExpressions.get(topic);
143
+ if (!filterExpression) {
144
+ throw new Error(`No subscription found for topic=${topic}, please subscribe first`);
145
+ }
146
+
118
147
  const loadBalancer = await this.#getSubscriptionLoadBalancer(topic);
119
148
  const mq = loadBalancer.takeMessageQueue();
120
149
  const request = this.wrapReceiveMessageRequest(maxMessageNum, mq, filterExpression,
121
150
  invisibleDuration, this.#awaitDuration);
122
- return await this.receiveMessage(request, mq, this.#awaitDuration);
151
+
152
+ // Retry logic for temporary errors
153
+ let lastError: Error | undefined;
154
+ for (let attempt = 1; attempt <= this.#maxRetryAttempts; attempt++) {
155
+ try {
156
+ return await this.receiveMessage(request, mq, this.#awaitDuration);
157
+ } catch (err) {
158
+ lastError = err as Error;
159
+
160
+ // Only retry on internal server errors (temporary errors)
161
+ if (err instanceof InternalErrorException) {
162
+ if (attempt < this.#maxRetryAttempts) {
163
+ this.logger.warn('Received temporary error from server, will retry after %dms, attempt=%d/%d, topic=%s, clientId=%s, error=%s',
164
+ RETRY_DELAY_MS, attempt, this.#maxRetryAttempts, topic, this.clientId, err.message);
165
+ await new Promise(resolve => setTimeout(resolve, RETRY_DELAY_MS));
166
+ continue;
167
+ }
168
+ }
169
+
170
+ // For non-retryable errors or max attempts reached, throw immediately
171
+ throw err;
172
+ }
173
+ }
174
+
175
+ // This should never be reached, but just in case
176
+ throw lastError;
123
177
  }
124
178
 
125
179
  async ack(message: MessageView) {
126
180
  await this.ackMessage(message);
127
181
  }
128
182
 
129
- async changeInvisibleDuration0(message: MessageView, invisibleDuration: number) {
130
- await this.changeInvisibleDuration0(message, invisibleDuration);
183
+ async changeInvisibleDuration(message: MessageView, invisibleDuration: number) {
184
+ const response = await this.invisibleDuration(message, invisibleDuration);
185
+ // Refresh receipt handle manually
186
+ (message as any).receiptHandle = response;
131
187
  }
132
188
 
133
189
  #nextTopic() {
134
190
  const topics = Array.from(this.#subscriptionExpressions.keys());
135
- if (this.#topicIndex >= topics.length) {
136
- this.#topicIndex = 0;
191
+ if (topics.length === 0) {
192
+ throw new Error('No subscriptions available to receive messages');
137
193
  }
138
- return topics[this.#topicIndex++];
194
+ const index = Math.abs(this.#topicIndex++ % topics.length);
195
+ return topics[index];
139
196
  }
140
197
  }
@@ -30,8 +30,8 @@ export class SimpleSubscriptionSettings extends Settings {
30
30
  readonly group: string;
31
31
  readonly subscriptionExpressions: Map<string, FilterExpression>;
32
32
 
33
- constructor(namespace: string, clientId: string, accessPoint: Endpoints, group: string, requestTimeout: number, longPollingTimeout: number, subscriptionExpressions: Map<string, FilterExpression>) {
34
- super(namespace, clientId, ClientType.SIMPLE_CONSUMER, accessPoint, requestTimeout);
33
+ constructor(namespace: string, clientId: string, clientType: ClientType, accessPoint: Endpoints, group: string, requestTimeout: number, longPollingTimeout: number, subscriptionExpressions: Map<string, FilterExpression>) {
34
+ super(namespace, clientId, clientType, accessPoint, requestTimeout);
35
35
  this.longPollingTimeout = longPollingTimeout;
36
36
  this.group = group;
37
37
  this.subscriptionExpressions = subscriptionExpressions;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Licensed to the Apache Software Foundation (ASF) under one or more
3
+ * contributor license agreements. See the NOTICE file distributed with
4
+ * this work for additional information regarding copyright ownership.
5
+ * The ASF licenses this file to You under the Apache License, Version 2.0
6
+ * (the "License"); you may not use this file except in compliance with
7
+ * the License. You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { MessageView } from '../message';
19
+ import { ConsumeService } from './ConsumeService';
20
+ import { MessageListener } from './MessageListener';
21
+ import type { ProcessQueue } from './ProcessQueue';
22
+ import { getDefaultLogger } from '../client';
23
+
24
+ export class StandardConsumeService extends ConsumeService {
25
+ private readonly logger = getDefaultLogger();
26
+
27
+ // eslint-disable-next-line @typescript-eslint/no-useless-constructor
28
+ constructor(clientId: string, messageListener: MessageListener) {
29
+ super(clientId, messageListener);
30
+ }
31
+
32
+ consume(pq: ProcessQueue, messageViews: MessageView[]): void {
33
+ this.logger.debug?.('StandardConsumeService.consume called, messageCount=%d', messageViews.length);
34
+ for (const messageView of messageViews) {
35
+ if (messageView.corrupted) {
36
+ this.logger.warn('Message corrupted, discarding, messageId=%s', messageView.messageId);
37
+ pq.discardMessage(messageView);
38
+ continue;
39
+ }
40
+
41
+ this.logger.debug?.('Consuming message, messageId=%s, topic=%s',
42
+ messageView.messageId, messageView.topic);
43
+
44
+ this.consumeMessage(messageView)
45
+ .then(result => {
46
+ this.logger.debug?.('Message consumed successfully, messageId=%s, result=%s',
47
+ messageView.messageId, result);
48
+ pq.eraseMessage(messageView, result);
49
+ })
50
+ .catch(err => {
51
+ this.logger.error('Message consumption failed, messageId=%s, error=%s',
52
+ messageView.messageId, err);
53
+ // Should never reach here.
54
+ });
55
+ }
56
+ }
57
+ }
@@ -20,3 +20,18 @@ export * from './FilterExpression';
20
20
  export * from './SimpleConsumer';
21
21
  export * from './SimpleSubscriptionSettings';
22
22
  export * from './SubscriptionLoadBalancer';
23
+ export * from './ConsumeResult';
24
+ export * from './MessageListener';
25
+ export * from './Assignment';
26
+ export * from './Assignments';
27
+ export * from './PushSubscriptionSettings';
28
+ export * from './ConsumeTask';
29
+ export * from './ConsumeService';
30
+ export * from './StandardConsumeService';
31
+ export * from './FifoConsumeService';
32
+ export * from './ProcessQueue';
33
+ export * from './PushConsumer';
34
+ export * from './LitePushConsumer';
35
+ export * from './OffsetOption';
36
+ export { LitePushConsumerImpl } from './LitePushConsumerImpl';
37
+ export { LiteSubscriptionManager } from './LiteSubscriptionManager';
@@ -21,5 +21,15 @@ export class InternalErrorException extends ClientException {
21
21
  constructor(code: number, message: string, requestId?: string) {
22
22
  super(code, message, requestId);
23
23
  this.name = 'InternalErrorException';
24
+ // Mark this as a potentially retryable error (temporary server-side issue)
25
+ (this as any).retryable = true;
26
+ }
27
+
28
+ /**
29
+ * Check if this error is retryable.
30
+ * Internal server errors are typically temporary and can be retried.
31
+ */
32
+ isRetryable(): boolean {
33
+ return true;
24
34
  }
25
35
  }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Licensed to the Apache Software Foundation (ASF) under one or more
3
+ * contributor license agreements. See the NOTICE file distributed with
4
+ * this work for additional information regarding copyright ownership.
5
+ * The ASF licenses this file to You under the Apache License, Version 2.0
6
+ * (the "License"); you may not use this file except in compliance with
7
+ * the License. You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { ClientException } from '../exception/ClientException';
19
+
20
+ /**
21
+ * Exception thrown when lite subscription quota is exceeded.
22
+ */
23
+ export class LiteSubscriptionQuotaExceededException extends ClientException {
24
+ constructor(responseCode: number, requestId: string | null, message: string) {
25
+ super(responseCode, message, requestId ?? undefined);
26
+ this.name = 'LiteSubscriptionQuotaExceededException';
27
+ }
28
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Licensed to the Apache Software Foundation (ASF) under one or more
3
+ * contributor license agreements. See the NOTICE file distributed with
4
+ * this work for additional information regarding copyright ownership.
5
+ * The ASF licenses this file to You under the Apache License, Version 2.0
6
+ * (the "License"); you may not use this file except in compliance with
7
+ * the License. You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { ClientException } from '../exception/ClientException';
19
+
20
+ /**
21
+ * Exception thrown when lite topic quota is exceeded during message sending.
22
+ */
23
+ export class LiteTopicQuotaExceededException extends ClientException {
24
+ constructor(responseCode: number, message: string, requestId?: string) {
25
+ super(responseCode, message, requestId);
26
+ this.name = 'LiteTopicQuotaExceededException';
27
+ }
28
+ }
@@ -19,6 +19,7 @@ import { Status, Code } from '../../proto/apache/rocketmq/v2/definition_pb';
19
19
  import { BadRequestException } from './BadRequestException';
20
20
  import { ForbiddenException } from './ForbiddenException';
21
21
  import { InternalErrorException } from './InternalErrorException';
22
+ import { LiteTopicQuotaExceededException } from './LiteTopicQuotaExceededException';
22
23
  import { NotFoundException } from './NotFoundException';
23
24
  import { PayloadTooLargeException } from './PayloadTooLargeException';
24
25
  import { PaymentRequiredException } from './PaymentRequiredException';
@@ -87,6 +88,8 @@ export class StatusChecker {
87
88
  case Code.VERSION_UNSUPPORTED:
88
89
  case Code.VERIFY_FIFO_MESSAGE_UNSUPPORTED:
89
90
  throw new UnsupportedException(status.code, status.message, requestId);
91
+ case Code.LITE_TOPIC_QUOTA_EXCEEDED:
92
+ throw new LiteTopicQuotaExceededException(status.code, status.message || '', requestId);
90
93
  default:
91
94
  throw new UnsupportedException(status.code, status.message, requestId);
92
95
  }
@@ -19,6 +19,8 @@ export * from './BadRequestException';
19
19
  export * from './ClientException';
20
20
  export * from './ForbiddenException';
21
21
  export * from './InternalErrorException';
22
+ export * from './LiteSubscriptionQuotaExceededException';
23
+ export * from './LiteTopicQuotaExceededException';
22
24
  export * from './NotFoundException';
23
25
  export * from './PayloadTooLargeException';
24
26
  export * from './PaymentRequiredException';
package/src/index.ts CHANGED
@@ -21,3 +21,4 @@ export * from './message';
21
21
  export * from './producer';
22
22
  export * from './retry';
23
23
  export * from './route';
24
+ export * from './client';
@@ -24,6 +24,8 @@ export interface MessageOptions {
24
24
  properties?: Map<string, string>;
25
25
  delay?: number;
26
26
  deliveryTimestamp?: Date;
27
+ priority?: number;
28
+ liteTopic?: string;
27
29
  }
28
30
 
29
31
  export class Message {
@@ -34,18 +36,57 @@ export class Message {
34
36
  keys: string[];
35
37
  properties?: Map<string, string>;
36
38
  deliveryTimestamp?: Date;
39
+ priority?: number;
40
+ liteTopic?: string;
37
41
 
38
42
  constructor(options: MessageOptions) {
43
+ let finalDeliveryTimestamp = options.deliveryTimestamp;
44
+ if (options.delay && !finalDeliveryTimestamp) {
45
+ finalDeliveryTimestamp = new Date(Date.now() + options.delay);
46
+ }
47
+
48
+ const hasPriority = options.priority !== undefined;
49
+ const hasLiteTopic = options.liteTopic !== undefined;
50
+ const hasTimedDelivery = finalDeliveryTimestamp !== undefined;
51
+ const hasGroup = options.messageGroup !== undefined;
52
+
53
+ if (hasPriority) {
54
+ if (options.priority === undefined || options.priority < 0) {
55
+ throw new Error('priority must be greater than or equal to 0');
56
+ }
57
+ if (hasTimedDelivery || hasGroup || hasLiteTopic) {
58
+ throw new Error('priority is mutually exclusive with delay, deliveryTimestamp, messageGroup, and liteTopic');
59
+ }
60
+ }
61
+
62
+ if (hasLiteTopic) {
63
+ if (hasTimedDelivery || hasGroup) {
64
+ throw new Error('liteTopic is mutually exclusive with delay, deliveryTimestamp, and messageGroup');
65
+ }
66
+ }
67
+
68
+ if (hasTimedDelivery && hasGroup) {
69
+ throw new Error('delay/deliveryTimestamp is mutually exclusive with messageGroup');
70
+ }
71
+
72
+ // 4. Assign properties
39
73
  this.topic = options.topic;
40
74
  this.body = options.body;
41
75
  this.tag = options.tag;
42
76
  this.messageGroup = options.messageGroup;
43
77
  this.keys = options.keys ?? [];
44
78
  this.properties = options.properties;
45
- let deliveryTimestamp = options.deliveryTimestamp;
46
- if (options.delay && !deliveryTimestamp) {
47
- deliveryTimestamp = new Date(Date.now() + options.delay);
48
- }
49
- this.deliveryTimestamp = deliveryTimestamp;
79
+ this.deliveryTimestamp = finalDeliveryTimestamp;
80
+ this.priority = options.priority;
81
+ this.liteTopic = options.liteTopic;
82
+ }
83
+
84
+ /**
85
+ * Returns a string representation of the message.
86
+ */
87
+ toString(): string {
88
+ return `Message{topic='${this.topic}', tag=${this.tag}, messageGroup=${this.messageGroup}, ` +
89
+ `liteTopic=${this.liteTopic}, priority=${this.priority}, deliveryTimestamp=${this.deliveryTimestamp}, ` +
90
+ `keys=[${this.keys.join(', ')}], properties=${JSON.stringify(this.properties)}}`;
50
91
  }
51
92
  }
@@ -53,6 +53,22 @@ export class MessageId {
53
53
  toString() {
54
54
  return this.id;
55
55
  }
56
+
57
+ equals(other: MessageId): boolean {
58
+ if (this === other) return true;
59
+ if (!other) return false;
60
+ return this.id === other.id;
61
+ }
62
+
63
+ hashCode(): number {
64
+ let hash = 17;
65
+ for (let i = 0; i < this.id.length; i++) {
66
+ const char = this.id.charCodeAt(i);
67
+ // eslint-disable-next-line no-bitwise
68
+ hash = (hash * 31 + char) | 0;
69
+ }
70
+ return hash;
71
+ }
56
72
  }
57
73
 
58
74
  const MAX_UINT32 = 0xFFFFFFFF;
@@ -42,6 +42,8 @@ export class MessageView {
42
42
  readonly offset?: number;
43
43
  readonly decodeTimestamp: Date;
44
44
  readonly properties = new Map<string, string>();
45
+ readonly liteTopic?: string;
46
+ readonly priority?: number;
45
47
 
46
48
  constructor(message: MessagePB, messageQueue?: MessageQueue, transportDeliveryTimestamp?: Date) {
47
49
  const systemProperties = message.getSystemProperties()!;
@@ -86,9 +88,134 @@ export class MessageView {
86
88
  this.offset = systemProperties.getQueueOffset();
87
89
  this.receiptHandle = systemProperties.getReceiptHandle()!;
88
90
  this.transportDeliveryTimestamp = transportDeliveryTimestamp;
91
+ this.liteTopic = systemProperties.getLiteTopic();
92
+ this.priority = systemProperties.getPriority();
89
93
  if (messageQueue) {
90
94
  this.endpoints = messageQueue.broker.endpoints;
91
95
  }
92
96
  this.body = bodyBytes;
93
97
  }
98
+
99
+ /**
100
+ * Get the lite topic, which makes sense only when the topic type is LITE.
101
+ *
102
+ * @return lite topic, which is optional, undefined means lite topic is not specified.
103
+ */
104
+ getLiteTopic(): string | undefined {
105
+ return this.liteTopic;
106
+ }
107
+
108
+ /**
109
+ * Get the priority of the message, which makes sense only when topic type is priority.
110
+ *
111
+ * @return message priority, which is optional, undefined means priority is not specified.
112
+ */
113
+ getPriority(): number | undefined {
114
+ return this.priority;
115
+ }
116
+
117
+ /**
118
+ * Get the unique id of the message.
119
+ *
120
+ * @return unique id.
121
+ */
122
+ getMessageId(): string {
123
+ return this.messageId;
124
+ }
125
+
126
+ /**
127
+ * Get the topic of the message, which is the first classifier for the message.
128
+ *
129
+ * @return topic of the message.
130
+ */
131
+ getTopic(): string {
132
+ return this.topic;
133
+ }
134
+
135
+ /**
136
+ * Get the deep copy of the message body, which means any modification of the return value does not
137
+ * affect the built-in message body.
138
+ *
139
+ * @return the deep copy of message body.
140
+ */
141
+ getBody(): Buffer {
142
+ return Buffer.from(this.body);
143
+ }
144
+
145
+ /**
146
+ * Get the deep copy of message properties, which makes the modifies of return value does
147
+ * not affect the message itself.
148
+ *
149
+ * @return the deep copy of message properties.
150
+ */
151
+ getProperties(): Map<string, string> {
152
+ return new Map(this.properties);
153
+ }
154
+
155
+ /**
156
+ * Get the tag of the message, which is the second classifier besides the topic.
157
+ *
158
+ * @return the tag of message, which is optional, undefined means tag does not exist.
159
+ */
160
+ getTag(): string | undefined {
161
+ return this.tag;
162
+ }
163
+
164
+ /**
165
+ * Get the key collection of the message, which means any modification of the return value does not affect the
166
+ * built-in message key collection.
167
+ *
168
+ * @return copy of the key collection of the message, empty array means message key is not specified.
169
+ */
170
+ getKeys(): string[] {
171
+ return [ ...this.keys ];
172
+ }
173
+
174
+ /**
175
+ * Get the message group, which makes sense only when the topic type is FIFO(First In, First Out).
176
+ *
177
+ * @return message group, which is optional, undefined means message group is not specified.
178
+ */
179
+ getMessageGroup(): string | undefined {
180
+ return this.messageGroup;
181
+ }
182
+
183
+ /**
184
+ * Get the expected delivery timestamp, which makes sense only when the topic type is delay.
185
+ *
186
+ * @return message expected delivery timestamp, which is optional, undefined means delivery timestamp is not specified.
187
+ */
188
+ getDeliveryTimestamp(): Date | undefined {
189
+ return this.deliveryTimestamp;
190
+ }
191
+
192
+ /**
193
+ * Get the born host of the message.
194
+ *
195
+ * @return born host of the message.
196
+ */
197
+ getBornHost(): string {
198
+ return this.bornHost;
199
+ }
200
+
201
+ /**
202
+ * Get the born timestamp of the message.
203
+ *
204
+ * <p>Born time means the timestamp that the message is prepared to send rather than the timestamp the
205
+ * Message was built.
206
+ *
207
+ * @return born timestamp of the message.
208
+ */
209
+ getBornTimestamp(): Date | undefined {
210
+ return this.bornTimestamp;
211
+ }
212
+
213
+ /**
214
+ * Get the delivery attempt for the message.
215
+ *
216
+ * @return delivery attempt.
217
+ */
218
+ getDeliveryAttempt(): number | undefined {
219
+ return this.deliveryAttempt;
220
+ }
94
221
  }
@@ -40,7 +40,7 @@ export class PublishingMessage extends Message {
40
40
  // Generate message id.
41
41
  this.messageId = MessageIdFactory.create().toString();
42
42
  // Normal message.
43
- if (!this.messageGroup && !this.deliveryTimestamp && !txEnabled) {
43
+ if (!this.messageGroup && !this.liteTopic && !this.deliveryTimestamp && !this.priority && !txEnabled) {
44
44
  this.messageType = MessageType.NORMAL;
45
45
  return;
46
46
  }
@@ -49,19 +49,29 @@ export class PublishingMessage extends Message {
49
49
  this.messageType = MessageType.FIFO;
50
50
  return;
51
51
  }
52
+ // Lite message.
53
+ if (this.liteTopic && !txEnabled) {
54
+ this.messageType = MessageType.LITE;
55
+ return;
56
+ }
52
57
  // Delay message.
53
58
  if (this.deliveryTimestamp && !txEnabled) {
54
59
  this.messageType = MessageType.DELAY;
55
60
  return;
56
61
  }
62
+ // Priority message.
63
+ if (this.priority && !txEnabled) {
64
+ this.messageType = MessageType.PRIORITY;
65
+ return;
66
+ }
57
67
  // Transaction message.
58
68
  if (!this.messageGroup &&
59
- !this.deliveryTimestamp && txEnabled) {
69
+ !this.liteTopic && !this.deliveryTimestamp && !this.priority && txEnabled) {
60
70
  this.messageType = MessageType.TRANSACTION;
61
71
  return;
62
72
  }
63
- // Transaction semantics is conflicted with fifo/delay.
64
- throw new TypeError('Transactional message should not set messageGroup or deliveryTimestamp');
73
+ // Transaction semantics is conflicted with fifo/delay/lite/priority.
74
+ throw new TypeError('Transactional message should not set messageGroup, liteTopic, deliveryTimestamp or priority');
65
75
  }
66
76
 
67
77
  /**
@@ -86,6 +96,12 @@ export class PublishingMessage extends Message {
86
96
  if (this.messageGroup) {
87
97
  systemProperties.setMessageGroup(this.messageGroup);
88
98
  }
99
+ if (this.liteTopic !== undefined) {
100
+ systemProperties.setLiteTopic(this.liteTopic);
101
+ }
102
+ if (this.priority !== undefined) {
103
+ systemProperties.setPriority(this.priority);
104
+ }
89
105
 
90
106
  const resource = createResource(this.topic);
91
107
  resource.setResourceNamespace(namespace);