@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.
package/dist/index.d.ts CHANGED
@@ -2435,7 +2435,7 @@ declare function getSubgraph<T extends {
2435
2435
  schema: Record<string, unknown>;
2436
2436
  }>(def: T, options?: Partial<SecondLayerOptions> | SecondLayer | Subgraphs): InferSubgraphClient2<T>;
2437
2437
  type CreateStreamsClientOptions = {
2438
- apiKey: string;
2438
+ apiKey?: string;
2439
2439
  baseUrl?: string;
2440
2440
  fetchImpl?: FetchLike2;
2441
2441
  /**
@@ -2606,7 +2606,9 @@ import { DecodedEventColumns, DecodedEventRow, DecodedFtBurn, DecodedFtBurnPaylo
2606
2606
  * Helpers for Streams cursors. A cursor is the opaque `<block>:<index>` string
2607
2607
  * that marks a position in the event stream; treat the format as an
2608
2608
  * implementation detail and go through these helpers instead of string-building
2609
- * it at call sites.
2609
+ * it at call sites. Encode/decode and the rewind sentinel come from the
2610
+ * canonical codec in `@secondlayer/shared` so the SDK cannot accept a spelling
2611
+ * the server would 400, or rewind to a different foot than Index/Streams.
2610
2612
  */
2611
2613
  declare const Cursor: {
2612
2614
  /**
@@ -2618,7 +2620,7 @@ declare const Cursor: {
2618
2620
  * is `height`: the new canonical block at `height` carries a fresh first
2619
2621
  * event at `(height, 0)` that the consumer MUST re-read.
2620
2622
  *
2621
- * Encoded as `${height-1}:${SENTINEL}` rather than the seemingly-natural
2623
+ * Encoded as `blockEndCursor(height - 1)` rather than the seemingly-natural
2622
2624
  * `${height}:0` — that earlier form was an off-by-one: being exclusive, it
2623
2625
  * skipped `(height, 0)`, silently dropping the fork block's first row on
2624
2626
  * every reorg. The sentinel is int4 max (the `event_index`/`tx_index` column
package/dist/index.js CHANGED
@@ -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
@@ -2276,5 +2281,5 @@ export {
2276
2281
  ApiError
2277
2282
  };
2278
2283
 
2279
- //# debugId=968797F41364DCA764756E2164756E21
2284
+ //# debugId=9A05B56B79C1360464756E2164756E21
2280
2285
  //# sourceMappingURL=index.js.map