rocketmq-client-nodejs 1.0.8 → 1.1.1

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 (206) hide show
  1. package/README.md +61 -0
  2. package/dist/client/BaseClient.d.ts +44 -1
  3. package/dist/client/BaseClient.js +353 -85
  4. package/dist/client/Logger.js +9 -6
  5. package/dist/client/RpcClient.js +42 -134
  6. package/dist/client/RpcClientManager.d.ts +22 -17
  7. package/dist/client/RpcClientManager.js +61 -56
  8. package/dist/client/TelemetrySession.js +107 -17
  9. package/dist/consumer/ConsumeService.d.ts +2 -1
  10. package/dist/consumer/ConsumeService.js +12 -6
  11. package/dist/consumer/ConsumeTask.d.ts +6 -1
  12. package/dist/consumer/ConsumeTask.js +36 -5
  13. package/dist/consumer/Consumer.d.ts +13 -5
  14. package/dist/consumer/Consumer.js +94 -33
  15. package/dist/consumer/FifoConsumeService.d.ts +3 -1
  16. package/dist/consumer/FifoConsumeService.js +4 -4
  17. package/dist/consumer/LiteConsumerSupport.d.ts +25 -0
  18. package/dist/consumer/LiteConsumerSupport.js +47 -0
  19. package/dist/consumer/LiteFifoConsumeService.d.ts +3 -1
  20. package/dist/consumer/LiteFifoConsumeService.js +4 -4
  21. package/dist/consumer/LitePushConsumerImpl.d.ts +15 -4
  22. package/dist/consumer/LitePushConsumerImpl.js +26 -30
  23. package/dist/consumer/LiteSimpleConsumer.d.ts +177 -0
  24. package/dist/consumer/LiteSimpleConsumer.js +126 -0
  25. package/dist/consumer/LiteSimpleConsumerImpl.d.ts +133 -0
  26. package/dist/consumer/LiteSimpleConsumerImpl.js +179 -0
  27. package/dist/consumer/LiteStandardConsumeService.d.ts +3 -1
  28. package/dist/consumer/LiteStandardConsumeService.js +4 -4
  29. package/dist/consumer/LiteSubscriptionManager.d.ts +27 -6
  30. package/dist/consumer/LiteSubscriptionManager.js +36 -27
  31. package/dist/consumer/OffsetOption.d.ts +1 -0
  32. package/dist/consumer/OffsetOption.js +10 -4
  33. package/dist/consumer/ProcessQueue.d.ts +17 -1
  34. package/dist/consumer/ProcessQueue.js +134 -91
  35. package/dist/consumer/PushConsumer.d.ts +17 -0
  36. package/dist/consumer/PushConsumer.js +117 -26
  37. package/dist/consumer/PushConsumerGaugeObserver.d.ts +33 -0
  38. package/dist/consumer/PushConsumerGaugeObserver.js +60 -0
  39. package/dist/consumer/PushSubscriptionSettings.d.ts +8 -1
  40. package/dist/consumer/PushSubscriptionSettings.js +35 -8
  41. package/dist/consumer/SimpleConsumer.d.ts +5 -0
  42. package/dist/consumer/SimpleConsumer.js +33 -20
  43. package/dist/consumer/SimpleSubscriptionSettings.js +3 -3
  44. package/dist/consumer/StandardConsumeService.d.ts +3 -1
  45. package/dist/consumer/StandardConsumeService.js +17 -8
  46. package/dist/consumer/SubscriptionLoadBalancer.js +3 -3
  47. package/dist/consumer/index.d.ts +1 -0
  48. package/dist/consumer/index.js +2 -1
  49. package/dist/exception/InternalErrorException.d.ts +1 -0
  50. package/dist/exception/InternalErrorException.js +3 -2
  51. package/dist/exception/PayloadEmptyException.d.ts +20 -0
  52. package/dist/exception/PayloadEmptyException.js +28 -0
  53. package/dist/exception/StatusChecker.js +11 -4
  54. package/dist/exception/index.d.ts +1 -0
  55. package/dist/exception/index.js +2 -1
  56. package/dist/exception/receiveStatus.d.ts +18 -0
  57. package/dist/exception/receiveStatus.js +28 -0
  58. package/dist/hook/Attribute.d.ts +26 -0
  59. package/dist/hook/Attribute.js +37 -0
  60. package/dist/hook/AttributeKey.d.ts +32 -0
  61. package/dist/hook/AttributeKey.js +43 -0
  62. package/dist/hook/CompositedMessageInterceptor.d.ts +32 -0
  63. package/dist/hook/CompositedMessageInterceptor.js +87 -0
  64. package/dist/hook/InflightRequestCountInterceptor.d.ts +28 -0
  65. package/dist/hook/InflightRequestCountInterceptor.js +44 -0
  66. package/dist/hook/MessageHookPoints.d.ts +54 -0
  67. package/dist/hook/MessageHookPoints.js +59 -0
  68. package/dist/hook/MessageHookPointsStatus.d.ts +24 -0
  69. package/dist/hook/MessageHookPointsStatus.js +29 -0
  70. package/dist/hook/MessageInterceptor.d.ts +34 -0
  71. package/dist/hook/MessageInterceptor.js +19 -0
  72. package/dist/hook/MessageInterceptorContext.d.ts +31 -0
  73. package/dist/hook/MessageInterceptorContext.js +19 -0
  74. package/dist/hook/MessageInterceptorContextImpl.d.ts +40 -0
  75. package/dist/hook/MessageInterceptorContextImpl.js +71 -0
  76. package/dist/hook/MessageMeterInterceptor.d.ts +39 -0
  77. package/dist/hook/MessageMeterInterceptor.js +149 -0
  78. package/dist/hook/index.d.ts +26 -0
  79. package/dist/hook/index.js +43 -0
  80. package/dist/index.d.ts +1 -0
  81. package/dist/index.js +2 -1
  82. package/dist/message/Message.js +26 -4
  83. package/dist/message/MessageView.js +15 -5
  84. package/dist/message/PublishingMessage.js +4 -4
  85. package/dist/metrics/ClientMeter.d.ts +42 -0
  86. package/dist/metrics/ClientMeter.js +75 -0
  87. package/dist/metrics/ClientMeterManager.d.ts +46 -0
  88. package/dist/metrics/ClientMeterManager.js +203 -0
  89. package/dist/metrics/EmptyGaugeObserver.d.ts +28 -0
  90. package/dist/metrics/EmptyGaugeObserver.js +34 -0
  91. package/dist/metrics/GaugeEnum.d.ts +32 -0
  92. package/dist/metrics/GaugeEnum.js +37 -0
  93. package/dist/metrics/GaugeObserver.d.ts +29 -0
  94. package/dist/metrics/GaugeObserver.js +19 -0
  95. package/dist/metrics/HistogramEnum.d.ts +54 -0
  96. package/dist/metrics/HistogramEnum.js +71 -0
  97. package/dist/metrics/InvocationStatus.d.ts +26 -0
  98. package/dist/metrics/InvocationStatus.js +33 -0
  99. package/dist/metrics/Metric.d.ts +30 -0
  100. package/dist/metrics/Metric.js +44 -0
  101. package/dist/metrics/MetricLabels.d.ts +27 -0
  102. package/dist/metrics/MetricLabels.js +34 -0
  103. package/dist/metrics/index.d.ts +25 -0
  104. package/dist/metrics/index.js +42 -0
  105. package/dist/producer/Producer.d.ts +8 -2
  106. package/dist/producer/Producer.js +83 -34
  107. package/dist/producer/PublishingLoadBalancer.js +11 -5
  108. package/dist/producer/PublishingSettings.js +1 -3
  109. package/dist/producer/SendReceipt.js +3 -2
  110. package/dist/producer/Transaction.d.ts +2 -1
  111. package/dist/producer/Transaction.js +9 -8
  112. package/dist/producer/index.d.ts +1 -0
  113. package/dist/producer/index.js +2 -2
  114. package/dist/retry/CustomizedBackoffRetryPolicy.d.ts +37 -0
  115. package/dist/retry/CustomizedBackoffRetryPolicy.js +72 -0
  116. package/dist/retry/ExponentialBackoffRetryPolicy.js +10 -6
  117. package/dist/retry/RetryPolicy.d.ts +1 -1
  118. package/dist/retry/index.d.ts +1 -0
  119. package/dist/retry/index.js +2 -1
  120. package/dist/route/Endpoints.d.ts +6 -0
  121. package/dist/route/Endpoints.js +69 -9
  122. package/dist/route/IpNameResolver.d.ts +28 -0
  123. package/dist/route/IpNameResolver.js +104 -0
  124. package/dist/route/MessageQueue.js +7 -2
  125. package/dist/route/TopicRouteData.d.ts +5 -0
  126. package/dist/route/TopicRouteData.js +22 -1
  127. package/dist/util/Semaphore.d.ts +47 -0
  128. package/dist/util/Semaphore.js +82 -0
  129. package/dist/util/index.d.ts +1 -1
  130. package/dist/util/index.js +9 -6
  131. package/dist/util/rpcContext.d.ts +20 -0
  132. package/dist/util/rpcContext.js +35 -0
  133. package/package.json +11 -4
  134. package/src/client/BaseClient.ts +392 -91
  135. package/src/client/Logger.ts +9 -5
  136. package/src/client/RpcClient.ts +62 -133
  137. package/src/client/RpcClientManager.ts +103 -60
  138. package/src/client/TelemetrySession.ts +112 -17
  139. package/src/consumer/ConsumeService.ts +16 -5
  140. package/src/consumer/ConsumeTask.ts +43 -3
  141. package/src/consumer/Consumer.ts +116 -35
  142. package/src/consumer/FifoConsumeService.ts +5 -3
  143. package/src/consumer/LiteConsumerSupport.ts +49 -0
  144. package/src/consumer/LiteFifoConsumeService.ts +5 -3
  145. package/src/consumer/LitePushConsumerImpl.ts +34 -23
  146. package/src/consumer/LiteSimpleConsumer.ts +251 -0
  147. package/src/consumer/LiteSimpleConsumerImpl.ts +214 -0
  148. package/src/consumer/LiteStandardConsumeService.ts +5 -3
  149. package/src/consumer/LiteSubscriptionManager.ts +66 -41
  150. package/src/consumer/OffsetOption.ts +10 -3
  151. package/src/consumer/ProcessQueue.ts +165 -107
  152. package/src/consumer/PushConsumer.ts +133 -24
  153. package/src/consumer/PushConsumerGaugeObserver.ts +60 -0
  154. package/src/consumer/PushSubscriptionSettings.ts +40 -9
  155. package/src/consumer/SimpleConsumer.ts +39 -23
  156. package/src/consumer/SimpleSubscriptionSettings.ts +2 -2
  157. package/src/consumer/StandardConsumeService.ts +18 -7
  158. package/src/consumer/SubscriptionLoadBalancer.ts +2 -2
  159. package/src/consumer/index.ts +1 -0
  160. package/src/exception/InternalErrorException.ts +3 -1
  161. package/src/exception/PayloadEmptyException.ts +25 -0
  162. package/src/exception/StatusChecker.ts +10 -2
  163. package/src/exception/index.ts +1 -0
  164. package/src/exception/receiveStatus.ts +25 -0
  165. package/src/hook/Attribute.ts +36 -0
  166. package/src/hook/AttributeKey.ts +42 -0
  167. package/src/hook/CompositedMessageInterceptor.ts +96 -0
  168. package/src/hook/InflightRequestCountInterceptor.ts +46 -0
  169. package/src/hook/MessageHookPoints.ts +55 -0
  170. package/src/hook/MessageHookPointsStatus.ts +25 -0
  171. package/src/hook/MessageInterceptor.ts +35 -0
  172. package/src/hook/MessageInterceptorContext.ts +33 -0
  173. package/src/hook/MessageInterceptorContextImpl.ts +83 -0
  174. package/src/hook/MessageMeterInterceptor.ts +162 -0
  175. package/src/hook/index.ts +27 -0
  176. package/src/index.ts +1 -0
  177. package/src/message/Message.ts +25 -3
  178. package/src/message/MessageView.ts +13 -4
  179. package/src/message/PublishingMessage.ts +3 -3
  180. package/src/metrics/ClientMeter.ts +81 -0
  181. package/src/metrics/ClientMeterManager.ts +227 -0
  182. package/src/metrics/EmptyGaugeObserver.ts +36 -0
  183. package/src/metrics/GaugeEnum.ts +33 -0
  184. package/src/metrics/GaugeObserver.ts +32 -0
  185. package/src/metrics/HistogramEnum.ts +69 -0
  186. package/src/metrics/InvocationStatus.ts +28 -0
  187. package/src/metrics/Metric.ts +42 -0
  188. package/src/metrics/MetricLabels.ts +31 -0
  189. package/src/metrics/index.ts +26 -0
  190. package/src/producer/Producer.ts +94 -38
  191. package/src/producer/PublishingLoadBalancer.ts +10 -4
  192. package/src/producer/PublishingSettings.ts +0 -2
  193. package/src/producer/SendReceipt.ts +2 -1
  194. package/src/producer/Transaction.ts +12 -11
  195. package/src/producer/index.ts +1 -1
  196. package/src/retry/CustomizedBackoffRetryPolicy.ts +81 -0
  197. package/src/retry/ExponentialBackoffRetryPolicy.ts +11 -6
  198. package/src/retry/RetryPolicy.ts +1 -1
  199. package/src/retry/index.ts +1 -0
  200. package/src/route/Endpoints.ts +65 -8
  201. package/src/route/IpNameResolver.ts +119 -0
  202. package/src/route/MessageQueue.ts +6 -1
  203. package/src/route/TopicRouteData.ts +22 -1
  204. package/src/util/Semaphore.ts +82 -0
  205. package/src/util/index.ts +8 -5
  206. package/src/util/rpcContext.ts +34 -0
@@ -21,15 +21,38 @@ import {
21
21
  SyncLiteSubscriptionRequest,
22
22
  SyncLiteSubscriptionResponse,
23
23
  } from '../../proto/apache/rocketmq/v2/service_pb';
24
- import { ClientException } from '../exception';
25
- import { Resource } from '../route';
26
- import { LitePushConsumerImpl } from './LitePushConsumerImpl';
24
+ import { Endpoints } from '../route';
25
+ import { ILogger } from '../client/Logger';
26
+ import { RpcClientManager } from '../client/RpcClientManager';
27
+ import { ClientException, LiteSubscriptionQuotaExceededException, StatusChecker } from '../exception';
28
+ import { Resource } from '../route/Resource';
29
+ import { getRequestId } from '../util/rpcContext';
27
30
  import { OffsetOption } from './OffsetOption';
28
31
 
29
32
  const SYNC_LITE_SUBSCRIPTION_INTERVAL = 30000; // 30 seconds
30
33
 
31
34
  /**
32
- * Manages lite topic subscriptions for LitePushConsumer.
35
+ * Minimal host contract required by LiteSubscriptionManager.
36
+ *
37
+ * Implemented by LitePushConsumerImpl and LiteSimpleConsumerImpl so the same
38
+ * manager can keep lite subscriptions in sync for both consumer flavors.
39
+ */
40
+ export interface LiteSubscriptionHost {
41
+ clientId: string;
42
+ isRunning(): boolean;
43
+ getLogger(): ILogger;
44
+ getRpcClientManager(): RpcClientManager;
45
+ getRequestTimeout(): number;
46
+ /**
47
+ * Endpoints the lite subscription should be synced to. Lite subscriptions
48
+ * must reach every route endpoint, not only the endpoint the client was
49
+ * configured with.
50
+ */
51
+ getSyncEndpoints(): Endpoints[];
52
+ }
53
+
54
+ /**
55
+ * Manages lite topic subscriptions for lite consumers.
33
56
  *
34
57
  * <p>LiteSubscriptionManager handles:
35
58
  * - Maintaining the set of subscribed lite topics
@@ -38,7 +61,7 @@ const SYNC_LITE_SUBSCRIPTION_INTERVAL = 30000; // 30 seconds
38
61
  * - Handling unsubscribe commands from server</p>
39
62
  */
40
63
  export class LiteSubscriptionManager {
41
- private readonly consumerImpl: LitePushConsumerImpl;
64
+ private readonly host: LiteSubscriptionHost;
42
65
  private readonly bindTopic: Resource;
43
66
  private readonly group: Resource;
44
67
  private readonly liteTopicSet = new Set<string>();
@@ -47,11 +70,11 @@ export class LiteSubscriptionManager {
47
70
  private syncTimer?: NodeJS.Timeout;
48
71
 
49
72
  constructor(
50
- consumerImpl: LitePushConsumerImpl,
73
+ host: LiteSubscriptionHost,
51
74
  bindTopic: Resource,
52
75
  group: Resource,
53
76
  ) {
54
- this.consumerImpl = consumerImpl;
77
+ this.host = host;
55
78
  this.bindTopic = bindTopic;
56
79
  this.group = group;
57
80
  this.liteSubscriptionQuota = 100; // Default quota
@@ -62,6 +85,9 @@ export class LiteSubscriptionManager {
62
85
  * Start periodic sync of lite subscriptions.
63
86
  */
64
87
  public startUp() {
88
+ if (this.syncTimer) {
89
+ return;
90
+ }
65
91
  // syncAll after startup for subscribeLite("*")
66
92
  this.syncAllLiteSubscription();
67
93
  this.syncTimer = setInterval(
@@ -129,8 +155,8 @@ export class LiteSubscriptionManager {
129
155
  offsetOption?: OffsetOption | null,
130
156
  ): Promise<void> {
131
157
  // Check if consumer is running
132
- if (!this.consumerImpl.isRunning()) {
133
- throw new ClientException(500, 'Consumer is not running');
158
+ if (!this.host.isRunning()) {
159
+ throw new Error('Consumer is not running');
134
160
  }
135
161
 
136
162
  // Already subscribed
@@ -152,15 +178,15 @@ export class LiteSubscriptionManager {
152
178
  );
153
179
 
154
180
  this.liteTopicSet.add(liteTopic);
155
- this.consumerImpl.getLogger().info(
181
+ this.host.getLogger().info(
156
182
  'SubscribeLite %s, topic=%s, group=%s, clientId=%s',
157
183
  liteTopic,
158
184
  this.getBindTopicName(),
159
185
  this.getConsumerGroupName(),
160
- this.consumerImpl.clientId,
186
+ this.host.clientId,
161
187
  );
162
188
  } catch (error) {
163
- this.consumerImpl.getLogger().error(
189
+ this.host.getLogger().error(
164
190
  'Failed to subscribeLite %s, error=%s',
165
191
  liteTopic,
166
192
  error,
@@ -174,8 +200,8 @@ export class LiteSubscriptionManager {
174
200
  */
175
201
  public async unsubscribeLite(liteTopic: string): Promise<void> {
176
202
  // Check if consumer is running
177
- if (!this.consumerImpl.isRunning()) {
178
- throw new ClientException(500, 'Consumer is not running');
203
+ if (!this.host.isRunning()) {
204
+ throw new Error('Consumer is not running');
179
205
  }
180
206
 
181
207
  // Not subscribed
@@ -191,15 +217,15 @@ export class LiteSubscriptionManager {
191
217
  );
192
218
 
193
219
  this.liteTopicSet.delete(liteTopic);
194
- this.consumerImpl.getLogger().info(
220
+ this.host.getLogger().info(
195
221
  'UnsubscribeLite %s, topic=%s, group=%s, clientId=%s',
196
222
  liteTopic,
197
223
  this.getBindTopicName(),
198
224
  this.getConsumerGroupName(),
199
- this.consumerImpl.clientId,
225
+ this.host.clientId,
200
226
  );
201
227
  } catch (error) {
202
- this.consumerImpl.getLogger().error(
228
+ this.host.getLogger().error(
203
229
  'Failed to unsubscribeLite %s, error=%s',
204
230
  liteTopic,
205
231
  error,
@@ -220,23 +246,23 @@ export class LiteSubscriptionManager {
220
246
  null,
221
247
  );
222
248
  } catch (error) {
223
- this.consumerImpl.getLogger().error(
249
+ this.host.getLogger().error(
224
250
  'Schedule syncAllLiteSubscription error, clientId=%s, error=%s',
225
- this.consumerImpl.clientId,
251
+ this.host.clientId,
226
252
  error,
227
253
  );
228
254
  }
229
255
  }
230
256
 
231
257
  /**
232
- * Sync lite subscription with server.
258
+ * Sync lite subscription with every route endpoint.
233
259
  */
234
260
  private async syncLiteSubscription(
235
261
  action: LiteSubscriptionAction,
236
262
  liteTopics: string[],
237
263
  offsetOption: OffsetOption | null,
238
264
  ): Promise<void> {
239
- const logger = this.consumerImpl.getLogger();
265
+ const logger = this.host.getLogger();
240
266
  if (logger.debug) {
241
267
  logger.debug(
242
268
  'SyncLiteSubscription: action=%s, liteTopics=[%s], offsetOption=%s',
@@ -258,29 +284,27 @@ export class LiteSubscriptionManager {
258
284
  request.setOffsetOption(offsetOption.toProtobuf());
259
285
  }
260
286
 
261
- try {
262
- // Call RPC client using public methods from LitePushConsumerImpl
263
- const response: SyncLiteSubscriptionResponse = await this.consumerImpl.getRpcClientManager().syncLiteSubscription(
264
- this.consumerImpl.getEndpoints(),
265
- request,
266
- this.consumerImpl.getRequestTimeout(),
267
- );
287
+ const endpointsList = this.host.getSyncEndpoints();
288
+ if (endpointsList.length === 0) {
289
+ throw new ClientException(500, 'No endpoints available to sync lite subscription');
290
+ }
268
291
 
269
- // Handle response status
270
- const status = response.getStatus();
271
- if (status && status.getCode() !== Code.OK) {
272
- throw new ClientException(
273
- status.getCode(),
274
- `Failed to sync lite subscription: ${status.getMessage()}`,
275
- );
276
- }
292
+ try {
293
+ // The lite subscription must reach every route endpoint; the sync fails
294
+ // if any endpoint rejects it.
295
+ await Promise.all(endpointsList.map(async endpoints => {
296
+ const response: SyncLiteSubscriptionResponse = await this.host.getRpcClientManager()
297
+ .syncLiteSubscription(endpoints, request, this.host.getRequestTimeout());
298
+ // Map the status to typed exceptions (e.g. LiteSubscriptionQuotaExceededException), as Java does.
299
+ StatusChecker.check(response.getStatus()?.toObject(), getRequestId(response));
300
+ }));
277
301
 
278
302
  if (logger.info) {
279
303
  logger.info(
280
304
  'SyncLiteSubscription success: action=%s, liteTopics=[%s], clientId=%s',
281
305
  LiteSubscriptionAction[action],
282
306
  liteTopics.join(', '),
283
- this.consumerImpl.clientId,
307
+ this.host.clientId,
284
308
  );
285
309
  }
286
310
  } catch (error) {
@@ -289,7 +313,7 @@ export class LiteSubscriptionManager {
289
313
  'SyncLiteSubscription failed: action=%s, liteTopics=[%s], clientId=%s, error=%s',
290
314
  LiteSubscriptionAction[action],
291
315
  liteTopics.join(', '),
292
- this.consumerImpl.clientId,
316
+ this.host.clientId,
293
317
  error,
294
318
  );
295
319
  }
@@ -302,7 +326,7 @@ export class LiteSubscriptionManager {
302
326
  */
303
327
  public onNotifyUnsubscribeLiteCommand(command: NotifyUnsubscribeLiteCommand) {
304
328
  const liteTopic = command.getLiteTopic();
305
- this.consumerImpl.getLogger().info(
329
+ this.host.getLogger().info(
306
330
  'Notify unsubscribe lite: liteTopic=%s, group=%s, bindTopic=%s',
307
331
  liteTopic,
308
332
  this.getConsumerGroupName(),
@@ -336,8 +360,9 @@ export class LiteSubscriptionManager {
336
360
  */
337
361
  private checkLiteSubscriptionQuota(delta: number) {
338
362
  if (this.liteTopicSet.size + delta > this.liteSubscriptionQuota) {
339
- throw new ClientException(
340
- 429,
363
+ throw new LiteSubscriptionQuotaExceededException(
364
+ Code.LITE_SUBSCRIPTION_QUOTA_EXCEEDED,
365
+ null,
341
366
  `Lite subscription quota exceeded ${this.liteSubscriptionQuota}`,
342
367
  );
343
368
  }
@@ -63,6 +63,13 @@ export class OffsetOption {
63
63
  this.value = value;
64
64
  }
65
65
 
66
+ private static toBigInt(value: number | bigint, name: string): bigint {
67
+ if (typeof value === 'number' && !Number.isSafeInteger(value)) {
68
+ throw new RangeError(`${name} must be a safe integer when provided as number`);
69
+ }
70
+ return typeof value === 'number' ? BigInt(value) : value;
71
+ }
72
+
66
73
  /**
67
74
  * Create an OffsetOption from a specific offset.
68
75
  *
@@ -71,7 +78,7 @@ export class OffsetOption {
71
78
  * @throws Error if offset is negative
72
79
  */
73
80
  static ofOffset(offset: number | bigint): OffsetOption {
74
- const value = typeof offset === 'number' ? BigInt(offset) : offset;
81
+ const value = OffsetOption.toBigInt(offset, 'offset');
75
82
  if (value < BigInt(0)) {
76
83
  throw new Error('offset must be greater than or equal to 0');
77
84
  }
@@ -86,7 +93,7 @@ export class OffsetOption {
86
93
  * @throws Error if tailN is negative
87
94
  */
88
95
  static ofTailN(tailN: number | bigint): OffsetOption {
89
- const value = typeof tailN === 'number' ? BigInt(tailN) : tailN;
96
+ const value = OffsetOption.toBigInt(tailN, 'tailN');
90
97
  if (value < BigInt(0)) {
91
98
  throw new Error('tailN must be greater than or equal to 0');
92
99
  }
@@ -101,7 +108,7 @@ export class OffsetOption {
101
108
  * @throws Error if timestamp is negative
102
109
  */
103
110
  static ofTimestamp(timestamp: number | bigint): OffsetOption {
104
- const value = typeof timestamp === 'number' ? BigInt(timestamp) : timestamp;
111
+ const value = OffsetOption.toBigInt(timestamp, 'timestamp');
105
112
  if (value < BigInt(0)) {
106
113
  throw new Error('timestamp must be greater than or equal to 0');
107
114
  }