Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let isEnd = true
if (messages.length === count + 1) {
// We intentionally included another message to see if we are at the end.
// We are not.
isEnd = false
messages.splice(messages.length - 1, 1)
}
const lastMessage = messages[messages.length - 1]
const nextPosition = forward
? BigInteger(lastMessage.position)
.plus(BigInteger.one)
.toString()
: // nextVersion will be 0 at the end, but that always includes the first message in
// the stream. There's no way around this that does not skip the first message.
BigInteger.max(
BigInteger(lastMessage.position).minus(BigInteger.one),
BigInteger(-1)
).toString()
return {
isEnd,
nextPosition,
messages: await maybeFilterExpiredMessages(messages)
}
}
function readAllBackward(startIndex: number, count: number): ReadAllResult {
const slice = inMemoryAllStream.slice(
Math.max(0, startIndex - count),
startIndex + 1
)
const isEnd = slice.length < count + 1
if (!isEnd) {
slice.splice(0, 1)
}
const lastMessage = slice.length > 0 ? slice[0] : null
slice.reverse()
return {
isEnd,
messages: slice.map(m => toStreamMessage(m)),
nextPosition: BigInteger.max(
BigInteger(-1),
isEnd
? BigInteger(-1)
: BigInteger(lastMessage!.position).minus(BigInteger(1))
).toString()
}
}