@@ -19,12 +19,12 @@ import type {
19
19
DirectMsg ,
20
20
DirectStreamAPI ,
21
21
JetStreamOptions ,
22
+ MaxBytes ,
22
23
StoredMsg ,
23
24
} from "./types.ts" ;
24
25
import { DirectMsgHeaders } from "./types.ts" ;
25
26
import type {
26
27
CallbackFn ,
27
- Codec ,
28
28
Deferred ,
29
29
Delay ,
30
30
Msg ,
@@ -44,14 +44,15 @@ import {
44
44
} from "@nats-io/nats-core/internal" ;
45
45
import type {
46
46
CompletionResult ,
47
+ DirectBatch ,
47
48
DirectBatchOptions ,
48
- DirectFetchOptions ,
49
+ DirectBatchStartSeq ,
50
+ DirectBatchStartTime ,
49
51
DirectLastFor ,
52
+ DirectMaxBytes ,
50
53
DirectMsgRequest ,
51
54
LastForMsgRequest ,
52
55
PullOptions ,
53
- StartSeq ,
54
- StartTime ,
55
56
} from "./jsapi_types.ts" ;
56
57
import { validateStreamName } from "./jsutil.ts" ;
57
58
import { JetStreamStatus , JetStreamStatusError } from "./jserrors.ts" ;
@@ -229,7 +230,6 @@ export class DirectStreamAPIImpl extends BaseApiClientImpl
229
230
export class DirectMsgImpl implements DirectMsg {
230
231
data : Uint8Array ;
231
232
header : MsgHdrs ;
232
- static jc ?: Codec < unknown > ;
233
233
234
234
constructor ( m : Msg ) {
235
235
if ( ! m . headers ) {
@@ -283,17 +283,44 @@ export class DirectMsgImpl implements DirectMsg {
283
283
}
284
284
}
285
285
286
+ /**
287
+ * Options for directly starting a direct consumer. The options can either specify
288
+ * a sequence start or a time start.
289
+ * @property {DirectBatchStartSeq } DirectBatchStartSeq - Specifies a sequence start for the consumer.
290
+ * @property {DirectBatchStartTime } DirectBatchStartTime - Specifies a time start for the consumer.
291
+ */
292
+ export type DirectStartOptions = DirectBatchStartSeq | DirectBatchStartTime ;
293
+
294
+ /**
295
+ * Represents the limits for the operation. For fetch requests it represents the maximum to be retrieved.
296
+ * For consume operations it represents the buffering for the consumer.
297
+ *
298
+ * This type is used to define constraints or configurations for batching processes that
299
+ * operate under specific limits, either in terms of quantity (DirectBatch) or size in bytes (DirectMaxBytes).
300
+ */
301
+ export type DirectBatchLimits = DirectBatch | DirectMaxBytes ;
302
+
303
+ function isDirectBatchStartTime (
304
+ t : DirectStartOptions ,
305
+ ) : t is DirectBatchStartTime {
306
+ return typeof t === "object" && "start_time" in t ;
307
+ }
308
+
309
+ function isMaxBytes ( t : DirectBatchLimits ) : t is MaxBytes {
310
+ return typeof t === "object" && "max_bytes" in t ;
311
+ }
312
+
286
313
export class DirectConsumer {
287
314
stream : string ;
288
315
api : DirectStreamAPIImpl ;
289
316
cursor : { last : number ; pending ?: number } ;
290
317
listeners : QueuedIteratorImpl < ConsumerNotification > [ ] ;
291
- start : StartSeq & StartTime ;
318
+ start : DirectStartOptions ;
292
319
293
320
constructor (
294
321
stream : string ,
295
322
api : DirectStreamAPIImpl ,
296
- start : StartSeq & StartTime ,
323
+ start : DirectStartOptions ,
297
324
) {
298
325
this . stream = stream ;
299
326
this . api = api ;
@@ -303,29 +330,26 @@ export class DirectConsumer {
303
330
}
304
331
305
332
getOptions (
306
- opts : Partial < DirectFetchOptions > = { } ,
307
- ) : Partial < DirectBatchOptions > {
333
+ opts ?: DirectBatchLimits ,
334
+ ) : DirectBatchOptions {
335
+ opts = opts || { } as DirectBatchLimits ;
308
336
const dbo : Partial < DirectBatchOptions > = { } ;
309
337
310
338
if ( this . cursor . last === 0 ) {
311
339
// we have never pulled, honor initial request options
312
- if ( this . start . seq ) {
313
- dbo . seq = this . start . seq ;
314
- } else if ( this . start . start_time ) {
340
+ if ( isDirectBatchStartTime ( this . start ) ) {
315
341
dbo . start_time = this . start . start_time ;
316
342
} else {
317
- dbo . seq = 1 ;
343
+ dbo . seq = this . start . seq || 1 ;
318
344
}
319
345
} else {
320
346
dbo . seq = this . cursor . last + 1 ;
321
347
}
322
348
323
- if ( opts . batch ) {
324
- dbo . batch = opts . batch ;
325
- } else if ( opts . max_bytes ) {
349
+ if ( isMaxBytes ( opts ) ) {
326
350
dbo . max_bytes = opts . max_bytes ;
327
351
} else {
328
- dbo . batch = 100 ;
352
+ dbo . batch = opts . batch ?? 100 ;
329
353
}
330
354
331
355
return dbo ;
@@ -358,7 +382,7 @@ export class DirectConsumer {
358
382
console . log ( this . cursor ) ;
359
383
}
360
384
361
- consume ( opts ?: DirectBatchOptions ) : Promise < QueuedIterator < StoredMsg > > {
385
+ consume ( opts : DirectBatchLimits ) : Promise < QueuedIterator < StoredMsg > > {
362
386
let pending : Delay ;
363
387
let requestDone : Deferred < void > ;
364
388
const qi = new QueuedIteratorImpl < StoredMsg > ( ) ;
@@ -445,7 +469,7 @@ export class DirectConsumer {
445
469
return Promise . resolve ( qi ) ;
446
470
}
447
471
448
- async fetch ( opts ?: DirectBatchOptions ) : Promise < QueuedIterator < StoredMsg > > {
472
+ async fetch ( opts ?: DirectBatchLimits ) : Promise < QueuedIterator < StoredMsg > > {
449
473
const dbo = this . getOptions ( opts ) ;
450
474
const qi = new QueuedIteratorImpl < StoredMsg > ( ) ;
451
475
const src = await this . api . get (
0 commit comments