-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getOffset and storeOffset command, fix typos
- Loading branch information
Showing
13 changed files
with
152 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
import type { CommandResponse } from '../../tcp.client.js'; | ||
import type { Id } from '../identifier.utils.js'; | ||
import { serializeGetOffset, type Consumer, type OffsetResponse } from './offset.utils.js'; | ||
|
||
export const GET_OFFSET = { | ||
code: 120, | ||
|
||
serialize: ( | ||
streamId: Id, | ||
topicId: Id, | ||
consumer: Consumer, | ||
partitionId?: number | ||
) => { | ||
return serializeGetOffset(streamId, topicId, consumer, partitionId); | ||
}, | ||
|
||
deserialize: (r: CommandResponse): OffsetResponse => { | ||
const partitionId = r.data.readUInt32LE(0); | ||
const currentOffset = r.data.readBigUInt64LE(4); | ||
const storedOffset = r.data.readBigUInt64LE(12); | ||
|
||
return { | ||
partitionId, | ||
currentOffset, | ||
storedOffset | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
import { serializeIdentifier, type Id } from '../identifier.utils.js'; | ||
|
||
export enum ConsumerKind { | ||
Single = 1, | ||
Group = 2 | ||
} | ||
|
||
export type Consumer = { | ||
kind: ConsumerKind, | ||
id: Id | ||
} | ||
|
||
export type OffsetResponse = { | ||
partitionId: number, | ||
currentOffset: bigint, | ||
storedOffset: bigint | ||
}; | ||
|
||
export const serializeGetOffset = ( | ||
streamId: Id, | ||
topicId: Id, | ||
consumer: Consumer, | ||
partitionId?: number | ||
) => { | ||
|
||
if (consumer.kind === ConsumerKind.Single && (!partitionId || partitionId < 1)) | ||
throw new Error('getOffset error: partitionId must be > 0 for single consumer kind'); | ||
|
||
const streamIdentifier = serializeIdentifier(streamId); | ||
const topicIdentifier = serializeIdentifier(topicId); | ||
const consumerIdentifier = serializeIdentifier(consumer.id); | ||
|
||
const b1 = Buffer.alloc(1); | ||
b1.writeUInt8(consumer.kind); | ||
|
||
const b2 = Buffer.alloc(4); | ||
b2.writeUInt32LE(partitionId || 0); | ||
|
||
return Buffer.concat([ | ||
b1, | ||
consumerIdentifier, | ||
streamIdentifier, | ||
topicIdentifier, | ||
b2 | ||
]); | ||
}; | ||
|
||
export const serializeStoreOffset = ( | ||
streamId: Id, | ||
topicId: Id, | ||
consumer: Consumer, | ||
partitionId: number, | ||
offset: bigint | ||
) => { | ||
const b = Buffer.alloc(8); | ||
b.writeBigUInt64LE(offset, 0); | ||
|
||
return Buffer.concat([ | ||
serializeGetOffset(streamId, topicId, consumer, partitionId), | ||
b | ||
]); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
import type { CommandResponse } from '../../tcp.client.js'; | ||
import { type Id } from '../identifier.utils.js'; | ||
import { serializeStoreOffset, type Consumer } from './offset.utils.js'; | ||
|
||
export const STORE_OFFSET = { | ||
code: 121, | ||
|
||
serialize: ( | ||
streamId: Id, | ||
topicId: Id, | ||
consumer: Consumer, | ||
partitionId: number, | ||
offset: bigint | ||
) => { | ||
return serializeStoreOffset( | ||
streamId, topicId, consumer, partitionId, offset | ||
); | ||
}, | ||
|
||
deserialize: (r: CommandResponse) => { | ||
return r.status === 0 && r.length === 0; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters