@secondlayer/sdk 9.0.0 → 9.0.2

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.
@@ -512,6 +512,13 @@ class Contracts extends BaseClient {
512
512
  }
513
513
  }
514
514
 
515
+ // src/streams/cursor.ts
516
+ import {
517
+ blockEndCursor,
518
+ decodeStreamsCursor,
519
+ encodeStreamsCursor
520
+ } from "@secondlayer/shared";
521
+
515
522
  // src/streams/errors.ts
516
523
  class StreamsServerError extends ApiError {
517
524
  constructor(message, status, body) {
@@ -528,21 +535,23 @@ class StreamsSignatureError extends SecondLayerError {
528
535
  }
529
536
 
530
537
  // src/streams/cursor.ts
531
- var REWIND_FOOT_INDEX_SENTINEL = 2147483647;
532
538
  var Cursor = {
533
539
  atHeight(height) {
534
- if (height <= 0)
535
- return "0:0";
536
- return `${height - 1}:${REWIND_FOOT_INDEX_SENTINEL}`;
540
+ if (height <= 0) {
541
+ return encodeStreamsCursor({ block_height: 0, event_index: 0 });
542
+ }
543
+ return encodeStreamsCursor(blockEndCursor(height - 1));
537
544
  },
538
545
  parse(cursor) {
539
- const parts = cursor.split(":");
540
- const blockHeight = Number(parts[0]);
541
- const eventIndex = Number(parts[1]);
542
- if (parts.length !== 2 || !Number.isInteger(blockHeight) || !Number.isInteger(eventIndex)) {
546
+ try {
547
+ const decoded = decodeStreamsCursor(cursor);
548
+ return {
549
+ blockHeight: decoded.block_height,
550
+ eventIndex: decoded.event_index
551
+ };
552
+ } catch {
543
553
  throw new ValidationError(`Invalid stream cursor "${cursor}"; expected "<block>:<index>" (e.g. "951475:3").`, 400);
544
554
  }
545
- return { blockHeight, eventIndex };
546
555
  }
547
556
  };
548
557
 
@@ -1476,7 +1485,7 @@ function subscribeStreamsEvents(opts) {
1476
1485
  })}`;
1477
1486
  const res = await opts.fetchImpl(url, {
1478
1487
  headers: {
1479
- Authorization: `Bearer ${opts.apiKey}`,
1488
+ ...opts.apiKey ? { Authorization: `Bearer ${opts.apiKey}` } : {},
1480
1489
  Accept: "text/event-stream"
1481
1490
  },
1482
1491
  signal: controller.signal
@@ -1585,12 +1594,8 @@ function parseFrame(raw) {
1585
1594
  function cursorTuple(cursor) {
1586
1595
  if (!cursor)
1587
1596
  return [-1, -1];
1588
- const parts = cursor.split(":");
1589
- const [block, index] = parts.map(Number);
1590
- if (parts.length !== 2 || !Number.isInteger(block) || !Number.isInteger(index)) {
1591
- throw new ValidationError(`Invalid stream cursor "${cursor}"; expected "<block>:<index>" (e.g. "951475:3").`, 400);
1592
- }
1593
- return [block, index];
1597
+ const { blockHeight, eventIndex } = Cursor.parse(cursor);
1598
+ return [blockHeight, eventIndex];
1594
1599
  }
1595
1600
  function maxCursor(a, b) {
1596
1601
  const [ah, ai] = cursorTuple(a);
@@ -1687,7 +1692,7 @@ function createStreamsClient(options) {
1687
1692
  });
1688
1693
  async function request(path) {
1689
1694
  const response = await fetchImpl(`${baseUrl}${path}`, {
1690
- headers: { Authorization: `Bearer ${options.apiKey}` }
1695
+ headers: options.apiKey ? { Authorization: `Bearer ${options.apiKey}` } : {}
1691
1696
  });
1692
1697
  if (!response.ok)
1693
1698
  await mapStreamsError(response);
@@ -1922,7 +1927,7 @@ class SecondLayer extends BaseClient {
1922
1927
  constructor(options = {}) {
1923
1928
  super(options);
1924
1929
  this.streams = createStreamsClient({
1925
- apiKey: resolveApiKey(options.apiKey) ?? "",
1930
+ apiKey: resolveApiKey(options.apiKey),
1926
1931
  baseUrl: options.baseUrl,
1927
1932
  fetchImpl: options.fetchImpl,
1928
1933
  dumpsBaseUrl: options.dumpsBaseUrl
@@ -1994,5 +1999,5 @@ export {
1994
1999
  Subgraphs
1995
2000
  };
1996
2001
 
1997
- //# debugId=B1359A6B3968ACD364756E2164756E21
2002
+ //# debugId=8ABB9C2E57DE462064756E2164756E21
1998
2003
  //# sourceMappingURL=index.js.map