@tari-project/ootle-indexer 0.10.0 → 0.11.1

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
@@ -77,12 +77,23 @@ export declare interface IndexerSseEvent {
77
77
 
78
78
  export { ListWatchedTemplatesResponse }
79
79
 
80
+ /** Most `data` accepted for one event, in UTF-16 code units, across all of its `data:` lines. */
81
+ export declare const MAX_SSE_EVENT_DATA_LENGTH: number;
82
+
83
+ /**
84
+ * Longest SSE line accepted, in UTF-16 code units, excluding its terminator.
85
+ * Indexer events are a few KiB of JSON, so this bounds the read buffer.
86
+ */
87
+ export declare const MAX_SSE_LINE_LENGTH: number;
88
+
80
89
  /**
81
90
  * Opens a persistent SSE connection to `url` using `fetch` and yields
82
91
  * parsed events as an async generator.
83
92
  *
84
93
  * Works in browsers (Fetch + ReadableStream) and Node.js ≥ 18.
85
- * Automatically reconnects after errors with a 5 s back-off.
94
+ * Automatically reconnects after errors with a 5 s back-off. A line longer than
95
+ * {@link MAX_SSE_LINE_LENGTH} or an event with more than
96
+ * {@link MAX_SSE_EVENT_DATA_LENGTH} of data is treated as a stream error.
86
97
  * Stops when `signal` is aborted.
87
98
  *
88
99
  * Mirrors the `EventStream` / `into_stream()` pattern from the Rust ootle-rs crate.
@@ -95,6 +106,10 @@ export declare function openEventStream(url: string, signal: AbortSignal): Async
95
106
  /**
96
107
  * Parses a partial SSE buffer into discrete events, returning any unparsed
97
108
  * remainder that should be carried forward to the next chunk.
109
+ *
110
+ * @deprecated Carrying the remainder forward and re-parsing it on every chunk
111
+ * is unbounded in memory and quadratic in CPU. {@link openEventStream} uses an
112
+ * incremental, size-bounded parser instead.
98
113
  */
99
114
  export declare function parseSseChunk(buffer: string): {
100
115
  events: IndexerSseEvent[];
package/dist/index.js CHANGED
@@ -270,8 +270,8 @@ var h = class e2 {
270
270
  signal: t3.signal
271
271
  });
272
272
  }
273
- };
274
- function v(e5) {
273
+ }, v = 1024 * 1024, y = 1024 * 1024;
274
+ function b(e5) {
275
275
  let t3 = [], n2 = e5.split(/\n\n|\r\n\r\n/), r2 = n2.pop() ?? "";
276
276
  for (let e6 of n2) {
277
277
  if (!e6.trim()) continue;
@@ -293,7 +293,58 @@ function v(e5) {
293
293
  remainder: r2
294
294
  };
295
295
  }
296
- async function* y(t3, n2) {
296
+ var x = class {
297
+ constructor(e5) {
298
+ __publicField(this, "lineEnding", /\r\n|\r|\n/g);
299
+ __publicField(this, "pending", []);
300
+ __publicField(this, "pendingLength", 0);
301
+ __publicField(this, "afterCr", false);
302
+ __publicField(this, "type", "message");
303
+ __publicField(this, "dataLines", []);
304
+ __publicField(this, "dataLength", 0);
305
+ this.url = e5;
306
+ }
307
+ push(t3) {
308
+ let n2 = [];
309
+ this.afterCr && t3.length > 0 && (t3.startsWith("\n") && (t3 = t3.slice(1)), this.afterCr = false);
310
+ let r2 = 0;
311
+ this.lineEnding.lastIndex = 0;
312
+ let i2;
313
+ for (; (i2 = this.lineEnding.exec(t3)) !== null; ) {
314
+ this.pending.push(t3.slice(r2, i2.index));
315
+ let e5 = this.pending.join("");
316
+ this.pending = [], this.pendingLength = 0, r2 = this.lineEnding.lastIndex, this.afterCr = i2[0] === "\r" && r2 === t3.length;
317
+ let a2 = this.processLine(e5);
318
+ a2 && n2.push(a2);
319
+ }
320
+ if (r2 < t3.length && (this.pending.push(t3.slice(r2)), this.pendingLength += t3.length - r2, this.pendingLength > 1048576)) throw new e(`SSE line exceeds the maximum length of ${v}`, { url: this.url });
321
+ return n2;
322
+ }
323
+ processLine(t3) {
324
+ if (t3.length > 1048576) throw new e(`SSE line exceeds the maximum length of ${v}`, { url: this.url });
325
+ if (t3 === "") return this.dispatch();
326
+ if (t3.startsWith("event:")) this.type = t3.slice(6).trim();
327
+ else if (t3.startsWith("data:")) {
328
+ let n2 = t3.slice(5).trim();
329
+ if (this.dataLength += n2.length + (this.dataLines.length > 0 ? 1 : 0), this.dataLength > 1048576) throw new e(`SSE event data exceeds the maximum length of ${y}`, { url: this.url });
330
+ this.dataLines.push(n2);
331
+ }
332
+ }
333
+ dispatch() {
334
+ let { type: e5, dataLines: t3 } = this;
335
+ if (this.type = "message", this.dataLines = [], this.dataLength = 0, t3.length === 0) return;
336
+ let n2 = t3.join("\n"), r2 = n2;
337
+ try {
338
+ r2 = JSON.parse(n2);
339
+ } catch {
340
+ }
341
+ return {
342
+ type: e5,
343
+ data: r2
344
+ };
345
+ }
346
+ };
347
+ async function* S(t3, n2) {
297
348
  for (; !n2.aborted; ) {
298
349
  let r2 = new AbortController(), i2 = () => r2.abort();
299
350
  n2.addEventListener("abort", i2, { once: true });
@@ -312,18 +363,16 @@ async function* y(t3, n2) {
312
363
  url: t3
313
364
  });
314
365
  }
315
- let a2 = i3.body.getReader(), o2 = new TextDecoder(), s2 = "";
366
+ let a2 = i3.body.getReader(), o2 = new TextDecoder(), s2 = new x(t3);
316
367
  try {
317
368
  for (; !n2.aborted; ) {
318
369
  let { done: e5, value: t4 } = await a2.read();
319
370
  if (e5) break;
320
- s2 += o2.decode(t4, { stream: true });
321
- let { events: n3, remainder: r3 } = v(s2);
322
- s2 = r3;
323
- for (let e6 of n3) yield e6;
371
+ for (let e6 of s2.push(o2.decode(t4, { stream: true }))) yield e6;
324
372
  }
325
373
  } finally {
326
- a2.cancel();
374
+ a2.cancel().catch(() => {
375
+ });
327
376
  }
328
377
  } catch (e5) {
329
378
  if (n2.aborted || e5.name === "AbortError") return;
@@ -338,7 +387,7 @@ async function* y(t3, n2) {
338
387
  }
339
388
  }
340
389
  }
341
- var b = 500, x = 3e3, S = class {
390
+ var C = 500, w = 3e3, T = class {
342
391
  constructor(e5) {
343
392
  __publicField(this, "baseUrl");
344
393
  __publicField(this, "pending", /* @__PURE__ */ new Map());
@@ -357,7 +406,7 @@ var b = 500, x = 3e3, S = class {
357
406
  this.pending.clear();
358
407
  }
359
408
  watch(e5, t3, n2 = 32e3) {
360
- return this.start(), new C(e5, this, t3, n2);
409
+ return this.start(), new E(e5, this, t3, n2);
361
410
  }
362
411
  register(e5) {
363
412
  return new Promise((t3, n2) => {
@@ -372,7 +421,7 @@ var b = 500, x = 3e3, S = class {
372
421
  }
373
422
  async run(e5) {
374
423
  let t3 = `${this.baseUrl}/events`;
375
- for await (let n2 of y(t3, e5)) {
424
+ for await (let n2 of S(t3, e5)) {
376
425
  if (n2.type !== "TransactionFinalized") continue;
377
426
  let e6 = n2.data, t4 = this.pending.get(e6.transaction_id);
378
427
  if (!t4) continue;
@@ -388,7 +437,7 @@ var b = 500, x = 3e3, S = class {
388
437
  } else t4.resolve({ decision: "Indeterminate" });
389
438
  }
390
439
  }
391
- }, C = class {
440
+ }, E = class {
392
441
  constructor(e5, t3, n2, r2) {
393
442
  __publicField(this, "txId");
394
443
  __publicField(this, "watcher");
@@ -415,7 +464,7 @@ var b = 500, x = 3e3, S = class {
415
464
  r2,
416
465
  this.cancellation.promise
417
466
  ]);
418
- if (n2 !== null && clearTimeout(n2), i3 === "sse-timeout") return await Promise.race([this.restPollUntilFinal(Date.now() + x, o2.signal, x), a2]);
467
+ if (n2 !== null && clearTimeout(n2), i3 === "sse-timeout") return await Promise.race([this.restPollUntilFinal(Date.now() + w, o2.signal, w), a2]);
419
468
  if (i3.decision === "Indeterminate") return await Promise.race([this.restPollUntilFinal(t3, o2.signal, 0), a2]);
420
469
  if (i3.decision === "Commit") return { outcome: "Commit" };
421
470
  let s2 = await this.client.getTransactionResult(this.txId);
@@ -451,7 +500,7 @@ var b = 500, x = 3e3, S = class {
451
500
  if (e6) return e6.outcome === "Commit" ? { outcome: "Commit" } : this.throwFromReceipt(r3, e6.reason ?? "");
452
501
  }
453
502
  if (Date.now() >= e5) break;
454
- await this.delay(b, t3);
503
+ await this.delay(C, t3);
455
504
  } while (Date.now() < e5 && !t3.aborted);
456
505
  if (t3.aborted) throw new n(`Wait for transaction ${this.txId} was cancelled`);
457
506
  let s2 = this.timeoutMs + r2, c2 = r2 > 0 ? ` (incl. ${r2}ms grace)` : "";
@@ -482,11 +531,11 @@ var b = 500, x = 3e3, S = class {
482
531
  return this.client.getTransactionResult(this.txId);
483
532
  }
484
533
  };
485
- function w(e5) {
534
+ function D(e5) {
486
535
  let t3 = e5 instanceof Error ? e5.message : String(e5);
487
536
  return /not found/i.test(t3) || t3.includes("404");
488
537
  }
489
- var T = class t2 {
538
+ var O = class t2 {
490
539
  constructor(e5, t3, n2, r2) {
491
540
  __publicField(this, "client");
492
541
  __publicField(this, "_network");
@@ -503,7 +552,7 @@ var T = class t2 {
503
552
  return this.client;
504
553
  }
505
554
  watchTransactionSSE(e5, t3) {
506
- return this._watcher || (this._watcher = new S(this._url), this._watcher.start()), this._watcher.watch(e5, this.client, t3 ?? this.defaultTransactionTimeoutMs);
555
+ return this._watcher || (this._watcher = new T(this._url), this._watcher.start()), this._watcher.watch(e5, this.client, t3 ?? this.defaultTransactionTimeoutMs);
507
556
  }
508
557
  stopWatcher() {
509
558
  var _a;
@@ -527,7 +576,7 @@ var T = class t2 {
527
576
  try {
528
577
  return await this.getSubstate(n2);
529
578
  } catch (e6) {
530
- if (w(e6)) return null;
579
+ if (D(e6)) return null;
531
580
  throw e6;
532
581
  }
533
582
  }
@@ -562,7 +611,7 @@ var T = class t2 {
562
611
  };
563
612
  } catch (n2) {
564
613
  let r2 = n2 instanceof Error ? n2.message : String(n2);
565
- throw w(n2) ? new e(`Failed to find input "${t4.substate_id}": ${r2}. Verify the substate id is correct (typo? wrong network?) or wait for the producing transaction to finalize.`, {
614
+ throw D(n2) ? new e(`Failed to find input "${t4.substate_id}": ${r2}. Verify the substate id is correct (typo? wrong network?) or wait for the producing transaction to finalize.`, {
566
615
  cause: n2,
567
616
  url: this._url
568
617
  }) : new e(`Failed to resolve input "${t4.substate_id}": ${r2}. Check the indexer URL points at the same network as the substate.`, {
@@ -575,7 +624,7 @@ var T = class t2 {
575
624
  async listRecentTransactions(e5) {
576
625
  return await this.client.listRecentTransactions(e5);
577
626
  }
578
- }, E = class e4 {
627
+ }, k = class e4 {
579
628
  constructor() {
580
629
  __publicField(this, "_network", t.LocalNet);
581
630
  __publicField(this, "_url", null);
@@ -599,10 +648,10 @@ var T = class t2 {
599
648
  network: this._network,
600
649
  defaultTransactionTimeoutMs: this._transactionTimeoutMs
601
650
  };
602
- return T.connect(e5);
651
+ return O.connect(e5);
603
652
  }
604
653
  };
605
- async function D(e5, t3) {
654
+ async function A(e5, t3) {
606
655
  return Promise.all(t3.map(async (t4) => {
607
656
  if (t4.type === "SpecificSubstate") {
608
657
  if (t4.version != null) return {
@@ -633,12 +682,14 @@ async function D(e5, t3) {
633
682
  }
634
683
  export {
635
684
  _ as IndexerClient,
636
- T as IndexerProvider,
637
- C as PendingTransaction,
638
- E as ProviderBuilder,
639
- S as TransactionWatcher,
640
- y as openEventStream,
641
- v as parseSseChunk,
642
- D as resolveWantInputs,
685
+ O as IndexerProvider,
686
+ y as MAX_SSE_EVENT_DATA_LENGTH,
687
+ v as MAX_SSE_LINE_LENGTH,
688
+ E as PendingTransaction,
689
+ k as ProviderBuilder,
690
+ T as TransactionWatcher,
691
+ S as openEventStream,
692
+ b as parseSseChunk,
693
+ A as resolveWantInputs,
643
694
  g as transports
644
695
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tari-project/ootle-indexer",
3
- "version": "0.10.0",
3
+ "version": "0.11.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/tari-project/ootle.ts.git",
@@ -27,7 +27,7 @@
27
27
  "dependencies": {
28
28
  "@tari-project/indexer-client": "^1.11.0",
29
29
  "@tari-project/ootle-ts-bindings": "^1.60.0",
30
- "@tari-project/ootle": "0.10.0"
30
+ "@tari-project/ootle": "0.11.1"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/node": "^25.5.0",