@secondlayer/sdk 15.5.2 → 16.1.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.
@@ -1076,10 +1076,7 @@ type ConsumerBatchContext<
1076
1076
  blocksBehind: number | null;
1077
1077
  /**
1078
1078
  * The full tip for this page — `StreamsTip` or `IndexTip` depending on the
1079
- * surface. Exposed here so a handler never needs the `envelope` parameter,
1080
- * which is the only way to reach it today and is also how the worst
1081
- * `finalizedOnly` mistake becomes reachable (`return envelope.next_cursor`
1082
- * jumps the cursor past events that were filtered out and never delivered).
1079
+ * surface.
1083
1080
  */
1084
1081
  tip: TTip;
1085
1082
  /**
@@ -1088,9 +1085,6 @@ type ConsumerBatchContext<
1088
1085
  */
1089
1086
  reorgs: readonly TReorg[];
1090
1087
  };
1091
- /** @deprecated Use {@link ConsumerBatchContext} — the Index and Streams loops
1092
- * share one shape now. Kept as an alias so existing annotations compile. */
1093
- type StreamsBatchContext = ConsumerBatchContext;
1094
1088
  /**
1095
1089
  * The checkpoint for a reorg rollback. Persist `cursor` (the rewind position)
1096
1090
  * inside the same transaction as your rollback so the two commit atomically.
@@ -1160,7 +1154,7 @@ type StreamsEventsConsumeParams<
1160
1154
  * labels do not interleave within a page — add `onBatch` when you need
1161
1155
  * strict global order across labels.
1162
1156
  */
1163
- on?: { [K in keyof F] : (events: StreamsEventForFilter<F[K], D>[], ctx: StreamsBatchContext & WithSinkTx<TTx>) => void | Promise<void> };
1157
+ on?: { [K in keyof F] : (events: StreamsEventForFilter<F[K], D>[], ctx: ConsumerBatchContext<StreamsTip, StreamsReorg> & WithSinkTx<TTx>) => void | Promise<void> };
1164
1158
  batchSize?: number;
1165
1159
  /**
1166
1160
  * Apply a page of canonical events. Persist `ctx.cursor` in the same
@@ -1170,7 +1164,7 @@ type StreamsEventsConsumeParams<
1170
1164
  * Optional only when `on` handles the page instead; a consume call with
1171
1165
  * neither throws.
1172
1166
  */
1173
- onBatch?: (events: D extends true ? IndexEvent[] : StreamsEvent[], envelope: StreamsEventsEnvelope, ctx: StreamsBatchContext & WithSinkTx<TTx>) => void | string | null | undefined | Promise<void> | Promise<string | null | undefined>;
1167
+ onBatch?: (events: D extends true ? IndexEvent[] : StreamsEvent[], ctx: ConsumerBatchContext<StreamsTip, StreamsReorg> & WithSinkTx<TTx>) => void | string | null | undefined | Promise<void> | Promise<string | null | undefined>;
1174
1168
  /**
1175
1169
  * Roll your projection back to `reorg.fork_point_height`, persisting
1176
1170
  * `ctx.cursor` in the same transaction. Called once per *new* reorg at or
@@ -1242,7 +1236,7 @@ type StreamsEventsConsumeBitcoinParams<TTx = never> = {
1242
1236
  /** A mainnet address. */
1243
1237
  address?: string;
1244
1238
  batchSize?: number;
1245
- onBatch: (events: RuneStreamsEvent[], envelope: StreamsEventsEnvelope<RuneStreamsEvent, StreamsBitcoinTip>, ctx: StreamsBatchContext & WithSinkTx<TTx>) => void | string | null | undefined | Promise<void> | Promise<string | null | undefined>;
1239
+ onBatch: (events: RuneStreamsEvent[], ctx: ConsumerBatchContext<StreamsBitcoinTip, StreamsReorg> & WithSinkTx<TTx>) => void | string | null | undefined | Promise<void> | Promise<string | null | undefined>;
1246
1240
  onReorg?: (reorg: StreamsReorg, ctx: StreamsReorgContext) => Promise<void> | void;
1247
1241
  maxRollbackDepth?: number;
1248
1242
  retryCount?: number;
@@ -1341,7 +1335,7 @@ type StreamsEventsReplayParams = {
1341
1335
  from: string | null;
1342
1336
  }) => Promise<void> | void;
1343
1337
  /** Called per live page after the dump phase, like `consume`. */
1344
- onBatch: (events: StreamsEvent[], envelope: StreamsEventsEnvelope) => Promise<string | null | undefined> | string | null | undefined;
1338
+ onBatch: (events: StreamsEvent[], ctx: ConsumerBatchContext<StreamsTip, StreamsReorg>) => Promise<string | null | undefined> | string | null | undefined;
1345
1339
  /**
1346
1340
  * Called when the live-tail phase (after the dump backfill) crosses a reorg,
1347
1341
  * before the cursor rewinds and re-reads the now-canonical events — same
@@ -1655,7 +1649,7 @@ type IndexConsumeOptions<
1655
1649
  * an empty page still proves the loop is alive. Feed it to
1656
1650
  * `consumerHealth().record`. */
1657
1651
  onProgress?: (ctx: ConsumerBatchContext) => void;
1658
- onBatch: (items: TItem[], envelope: TEnvelope, ctx: ConsumerBatchContext & WithSinkTx<TTx>) => void | string | null | undefined | Promise<void> | Promise<string | null | undefined>;
1652
+ onBatch: (items: TItem[], ctx: ConsumerBatchContext<TEnvelope["tip"], TEnvelope["reorgs"][number]> & WithSinkTx<TTx>) => void | string | null | undefined | Promise<void> | Promise<string | null | undefined>;
1659
1653
  /** Called once per new reorg at or below the checkpoint, before the loop
1660
1654
  * rewinds to `ctx.cursor` (`null` = pre-genesis) and re-reads. A reorg
1661
1655
  * whose fork sits ABOVE the checkpoint is not a rollback: nothing past
@@ -207,6 +207,7 @@ function raceAbort(promise, signal) {
207
207
  });
208
208
  }
209
209
  var LOCAL_API_URL = "http://127.0.0.1:3800";
210
+ var HOSTED_API_URL = "https://api.secondlayer.tools";
210
211
  var INSTANCE_TOKEN_ENV = "INSTANCE_TOKEN";
211
212
  var ACCOUNT_KEY_ENV = "SECONDLAYER_API_KEY";
212
213
  function resolveBaseUrl(explicit) {
@@ -216,6 +217,9 @@ function resolveBaseUrl(explicit) {
216
217
  const primary = process.env?.SECONDLAYER_API_URL || undefined;
217
218
  if (primary)
218
219
  return primary.replace(/\/+$/, "");
220
+ if (process.env?.[ACCOUNT_KEY_ENV]?.startsWith("sk-sl_") && !process.env?.[INSTANCE_TOKEN_ENV]) {
221
+ return HOSTED_API_URL;
222
+ }
219
223
  }
220
224
  return LOCAL_API_URL;
221
225
  }
@@ -945,7 +949,7 @@ function stripSlash(url) {
945
949
  return url.replace(/\/+$/, "");
946
950
  }
947
951
  function createArchiveClient(options = {}) {
948
- const accountKey = resolveAccountKey(options.accountKey ?? options.apiKey);
952
+ const accountKey = resolveAccountKey(options.accountKey);
949
953
  const fetchImpl = options.fetchImpl ?? ((input, init) => fetch(input, init));
950
954
  const archiveBaseUrl = stripSlash(options.archiveBaseUrl ?? DEFAULT_ARCHIVE_BASE_URL);
951
955
  const archiveOpsUrl = stripSlash(options.archiveOpsUrl ?? DEFAULT_ARCHIVE_OPS_URL);
@@ -1434,7 +1438,7 @@ async function consumeStreamsEvents(opts) {
1434
1438
  const sink = opts.sink;
1435
1439
  if (checkpoint !== null && (checkpoint !== cursor || emitted.length > 0)) {
1436
1440
  await sink.commitBatch(checkpoint, async (tx) => {
1437
- await opts.onBatch(emitted, envelope, {
1441
+ await opts.onBatch(emitted, {
1438
1442
  ...ctx,
1439
1443
  tx
1440
1444
  });
@@ -1442,7 +1446,7 @@ async function consumeStreamsEvents(opts) {
1442
1446
  }
1443
1447
  nextCursor = checkpoint;
1444
1448
  } else {
1445
- const returnedCursor = await opts.onBatch(emitted, envelope, ctx);
1449
+ const returnedCursor = await opts.onBatch(emitted, ctx);
1446
1450
  if (finalizedOnly)
1447
1451
  assertFinalizedCheckpoint(returnedCursor, checkpoint);
1448
1452
  nextCursor = returnedCursor ?? checkpoint;
@@ -1615,7 +1619,7 @@ async function consumeIndexFeed(opts) {
1615
1619
  const sink = opts.sink;
1616
1620
  if (checkpoint !== null && (checkpoint !== cursor || emitted.length > 0)) {
1617
1621
  await sink.commitBatch(checkpoint, async (tx) => {
1618
- await opts.onBatch(emitted, envelope, {
1622
+ await opts.onBatch(emitted, {
1619
1623
  ...ctx,
1620
1624
  tx
1621
1625
  });
@@ -1623,7 +1627,7 @@ async function consumeIndexFeed(opts) {
1623
1627
  }
1624
1628
  nextCursor = checkpoint;
1625
1629
  } else {
1626
- const returnedCursor = await opts.onBatch(emitted, envelope, ctx);
1630
+ const returnedCursor = await opts.onBatch(emitted, ctx);
1627
1631
  if (finalizedOnly)
1628
1632
  assertFinalizedCheckpoint(returnedCursor, checkpoint);
1629
1633
  nextCursor = returnedCursor ?? checkpoint;
@@ -2498,7 +2502,7 @@ function createStreamsClient(options) {
2498
2502
  }
2499
2503
  const dispatch = params.on;
2500
2504
  const project = (events) => params.decoded ? events.map(decode) : events;
2501
- const onBatch = async (events, envelope, ctx) => {
2505
+ const onBatch = async (events, ctx) => {
2502
2506
  for (const label of labels) {
2503
2507
  const forLabel = events.filter((event) => event.matched?.includes(label));
2504
2508
  if (forLabel.length === 0)
@@ -2506,7 +2510,7 @@ function createStreamsClient(options) {
2506
2510
  await dispatch?.[label]?.(project(forLabel), ctx);
2507
2511
  }
2508
2512
  const userOnBatch = params.onBatch;
2509
- return await userOnBatch?.(project(events), envelope, ctx);
2513
+ return await userOnBatch?.(project(events), ctx);
2510
2514
  };
2511
2515
  return consumeStreamsEvents({
2512
2516
  fromCursor: params.fromCursor,
@@ -2843,5 +2847,5 @@ export {
2843
2847
  getSubgraph
2844
2848
  };
2845
2849
 
2846
- //# debugId=9138C23A8DEF74B764756E2164756E21
2850
+ //# debugId=C9F15D050AD076D464756E2164756E21
2847
2851
  //# sourceMappingURL=index.js.map