@secondlayer/sdk 6.48.0 → 6.49.0

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.
@@ -597,13 +597,24 @@ function assertSinkModeCompatible(sink, finalizedOnly) {
597
597
  throw new ValidationError("This sink declares capabilities.finalizedOnly — it cannot roll back reorged rows, and following the unfinalized tip would corrupt it at the first fork. Pass finalizedOnly: true to consume(), or use a sink that implements rollback.", 400);
598
598
  }
599
599
  }
600
- function batchContext(cursor, height, tip, reorgs = []) {
600
+ function resumeHeight(cursor) {
601
+ if (cursor === null)
602
+ return null;
603
+ try {
604
+ return Cursor.parse(cursor).blockHeight;
605
+ } catch {
606
+ return null;
607
+ }
608
+ }
609
+ function batchContext(cursor, height, tip, reorgs = [], scannedHeight = null) {
601
610
  const tipHeight = tip.block_height;
611
+ const position = scannedHeight ?? height;
602
612
  return {
603
613
  cursor,
604
614
  height,
615
+ scannedHeight: position,
605
616
  tipHeight,
606
- blocksBehind: height === null ? null : Math.max(0, tipHeight - height),
617
+ blocksBehind: position === null ? null : Math.max(0, tipHeight - position),
607
618
  tip,
608
619
  reorgs
609
620
  };
@@ -669,7 +680,8 @@ async function consumeStreamsEvents(opts) {
669
680
  const handledReorgs = new Set;
670
681
  let pages = 0;
671
682
  let emptyPolls = 0;
672
- let height = null;
683
+ let height = resumeHeight(cursor);
684
+ let scanned = height;
673
685
  while (pages < maxPages && emptyPolls < maxEmptyPolls && !opts.signal?.aborted) {
674
686
  const envelope = await fetchPageWithRetry(() => opts.fetchEvents({
675
687
  cursor,
@@ -701,6 +713,7 @@ async function consumeStreamsEvents(opts) {
701
713
  }
702
714
  cursor = rewind;
703
715
  height = forkPoint > 0 ? forkPoint - 1 : null;
716
+ scanned = height;
704
717
  emptyPolls = 0;
705
718
  continue;
706
719
  }
@@ -708,7 +721,14 @@ async function consumeStreamsEvents(opts) {
708
721
  const emitted = finalizedOnly ? envelope.events.filter((event) => event.finalized) : envelope.events;
709
722
  const checkpoint = finalizedOnly ? emitted.at(-1)?.cursor ?? cursor : envelope.next_cursor;
710
723
  height = emitted.at(-1)?.block_height ?? height;
711
- const ctx = batchContext(checkpoint, height, envelope.tip, envelope.reorgs);
724
+ if (emitted.length > 0) {
725
+ scanned = height;
726
+ } else if (checkpoint !== null && checkpoint !== cursor) {
727
+ scanned = resumeHeight(checkpoint) ?? scanned;
728
+ } else if (envelope.events.length === 0) {
729
+ scanned = envelope.tip.block_height;
730
+ }
731
+ const ctx = batchContext(checkpoint ?? cursor, height, envelope.tip, envelope.reorgs, scanned);
712
732
  opts.onProgress?.(ctx);
713
733
  let nextCursor;
714
734
  if (opts.sink) {
@@ -836,7 +856,8 @@ async function consumeIndexFeed(opts) {
836
856
  const handledReorgs = new Set;
837
857
  let pages = 0;
838
858
  let emptyPolls = 0;
839
- let height = null;
859
+ let height = resumeHeight(cursor);
860
+ let scanned = height;
840
861
  while (pages < maxPages && emptyPolls < maxEmptyPolls && !opts.signal?.aborted) {
841
862
  const envelope = await fetchPageWithRetry(() => opts.fetchPage({
842
863
  cursor,
@@ -862,6 +883,7 @@ async function consumeIndexFeed(opts) {
862
883
  }
863
884
  cursor = rewind;
864
885
  height = forkPoint > 0 ? forkPoint - 1 : null;
886
+ scanned = height;
865
887
  emptyPolls = 0;
866
888
  continue;
867
889
  }
@@ -870,7 +892,14 @@ async function consumeIndexFeed(opts) {
870
892
  const emitted = finalizedOnly ? items.filter((item) => item.block_height <= envelope.tip.finalized_height) : items;
871
893
  const checkpoint = finalizedOnly ? emitted.at(-1)?.cursor ?? cursor : envelope.next_cursor;
872
894
  height = emitted.at(-1)?.block_height ?? height;
873
- const ctx = batchContext(checkpoint, height, envelope.tip, envelope.reorgs);
895
+ if (emitted.length > 0) {
896
+ scanned = height;
897
+ } else if (checkpoint !== null && checkpoint !== cursor) {
898
+ scanned = resumeHeight(checkpoint) ?? scanned;
899
+ } else {
900
+ scanned = finalizedOnly ? envelope.tip.finalized_height : envelope.tip.block_height;
901
+ }
902
+ const ctx = batchContext(checkpoint ?? cursor, height, envelope.tip, envelope.reorgs, scanned);
874
903
  opts.onProgress?.(ctx);
875
904
  let nextCursor;
876
905
  if (opts.sink) {
@@ -2203,5 +2232,5 @@ export {
2203
2232
  Subgraphs
2204
2233
  };
2205
2234
 
2206
- //# debugId=5079C103BC48773D64756E2164756E21
2235
+ //# debugId=0E0B0BAAF8B4C44E64756E2164756E21
2207
2236
  //# sourceMappingURL=index.js.map