@tari-project/ootle-indexer 0.11.0 → 0.12.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.
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
@@ -22,7 +22,8 @@ function f(e5) {
22
22
  function p(e5, t3) {
23
23
  return typeof t3 == "bigint" ? t3.toString() : t3;
24
24
  }
25
- function m(e5, t3) {
25
+ var m = 1024 * 1024, h = 1024 * 1024;
26
+ function g(e5, t3) {
26
27
  let n2 = new AbortController();
27
28
  return t3.signal && (t3.signal.aborted ? n2.abort() : t3.signal.addEventListener("abort", () => n2.abort(), { once: true })), (async () => {
28
29
  let r2 = await fetch(e5, {
@@ -35,54 +36,66 @@ function m(e5, t3) {
35
36
  throw new d(r2.status, r2.statusText, e6);
36
37
  }
37
38
  if (!r2.body) throw Error("Response body is not readable");
38
- let i2 = r2.body.getReader(), a2 = new TextDecoder(), o2 = "", s2 = "", c2 = [], l2, u2, f2 = () => {
39
- if (c2.length === 0) {
40
- s2 = "", l2 = void 0, u2 = void 0;
39
+ let i2 = r2.body.getReader(), a2 = new TextDecoder(), o2 = /\r\n|\r|\n/g, s2 = [], c2 = 0, l2 = false, u2 = "", f2 = [], p2 = 0, g2, _2, v2 = () => {
40
+ if (f2.length === 0) {
41
+ u2 = "", g2 = void 0, _2 = void 0;
41
42
  return;
42
43
  }
43
44
  let e6 = {
44
- event: s2 || "message",
45
- data: c2.join("\n"),
46
- id: l2,
47
- retry: u2
45
+ event: u2 || "message",
46
+ data: f2.join("\n"),
47
+ id: g2,
48
+ retry: _2
48
49
  };
49
- t3.onEvent(e6), s2 = "", c2 = [], l2 = void 0, u2 = void 0;
50
- }, p2 = (e6) => {
50
+ t3.onEvent(e6), u2 = "", f2 = [], p2 = 0, g2 = void 0, _2 = void 0;
51
+ }, y2 = (e6) => {
52
+ if (e6.length > m) throw Error(`SSE line exceeds the maximum length of ${m}`);
51
53
  if (e6 === "") {
52
- f2();
54
+ v2();
53
55
  return;
54
56
  }
55
57
  if (e6.startsWith(":")) return;
56
58
  let t4 = e6.indexOf(":"), n3, r3;
57
59
  switch (t4 === -1 ? (n3 = e6, r3 = "") : (n3 = e6.slice(0, t4), r3 = e6.slice(t4 + 1), r3.startsWith(" ") && (r3 = r3.slice(1))), n3) {
58
60
  case "event":
59
- s2 = r3;
61
+ u2 = r3;
60
62
  break;
61
63
  case "data":
62
- c2.push(r3);
64
+ if (p2 += r3.length + (f2.length > 0 ? 1 : 0), p2 > h) throw Error(`SSE event data exceeds the maximum length of ${h}`);
65
+ f2.push(r3);
63
66
  break;
64
67
  case "id":
65
- l2 = r3;
68
+ g2 = r3;
66
69
  break;
67
70
  case "retry": {
68
71
  let e7 = parseInt(r3, 10);
69
- isNaN(e7) || (u2 = e7);
72
+ isNaN(e7) || (_2 = e7);
70
73
  break;
71
74
  }
72
75
  }
73
76
  };
74
77
  try {
75
78
  for (; ; ) {
76
- let { done: e6, value: t4 } = await i2.read();
77
- if (e6) break;
78
- o2 += a2.decode(t4, { stream: true });
79
- let n3 = /\r\n|\r|\n/, r3;
80
- for (; (r3 = n3.exec(o2)) !== null; ) {
81
- let e7 = o2.slice(0, r3.index);
82
- o2 = o2.slice(r3.index + r3[0].length), p2(e7);
79
+ let { done: e7, value: t4 } = await i2.read();
80
+ if (e7) break;
81
+ let n3 = a2.decode(t4, { stream: true });
82
+ l2 && n3.length > 0 && (n3.startsWith("\n") && (n3 = n3.slice(1)), l2 = false);
83
+ let r3 = 0;
84
+ o2.lastIndex = 0;
85
+ let u3;
86
+ for (; (u3 = o2.exec(n3)) !== null; ) {
87
+ s2.push(n3.slice(r3, u3.index));
88
+ let e8 = s2.join("");
89
+ s2 = [], c2 = 0, y2(e8), r3 = o2.lastIndex, l2 = u3[0] === "\r" && r3 === n3.length;
83
90
  }
91
+ if (r3 < n3.length && (s2.push(n3.slice(r3)), c2 += n3.length - r3, c2 > m)) throw Error(`SSE line exceeds the maximum length of ${m}`);
84
92
  }
85
- o2.length > 0 && (p2(o2), f2());
93
+ s2.push(a2.decode());
94
+ let e6 = s2.join("");
95
+ e6.length > 0 && (y2(e6), v2());
96
+ } catch (e6) {
97
+ throw await i2.cancel().catch(() => {
98
+ }), e6;
86
99
  } finally {
87
100
  i2.releaseLock();
88
101
  }
@@ -96,7 +109,7 @@ function m(e5, t3) {
96
109
  n2.abort();
97
110
  } };
98
111
  }
99
- var h = class e2 {
112
+ var _ = class e2 {
100
113
  constructor(e5) {
101
114
  __publicField(this, "url");
102
115
  this.url = e5;
@@ -144,7 +157,7 @@ var h = class e2 {
144
157
  let n3 = e6.toString();
145
158
  n3.length > 0 && (r2 += `?${n3}`);
146
159
  }
147
- return m(r2, n2);
160
+ return g(r2, n2);
148
161
  }
149
162
  async sendRequest(e5, t3, n2) {
150
163
  let r2 = new AbortController(), i2 = r2.signal, a2 = (n2 == null ? void 0 : n2.timeout_millis) ? setTimeout(() => {
@@ -172,7 +185,7 @@ var h = class e2 {
172
185
  if (l2.error) throw Error(`${l2.error.code}: ${l2.error.message}`);
173
186
  return l2;
174
187
  }
175
- }, g = /* @__PURE__ */ u({ FetchTransport: () => h }), _ = class e3 {
188
+ }, v = /* @__PURE__ */ u({ FetchTransport: () => _ }), y = class e3 {
176
189
  constructor(e5) {
177
190
  __publicField(this, "transport");
178
191
  this.transport = e5;
@@ -181,7 +194,7 @@ var h = class e2 {
181
194
  return new e3(t3);
182
195
  }
183
196
  static usingFetchTransport(t3) {
184
- return e3.new(h.new(t3));
197
+ return e3.new(_.new(t3));
185
198
  }
186
199
  getTransport() {
187
200
  return this.transport;
@@ -270,8 +283,8 @@ var h = class e2 {
270
283
  signal: t3.signal
271
284
  });
272
285
  }
273
- };
274
- function v(e5) {
286
+ }, b = 1024 * 1024, x = 1024 * 1024;
287
+ function S(e5) {
275
288
  let t3 = [], n2 = e5.split(/\n\n|\r\n\r\n/), r2 = n2.pop() ?? "";
276
289
  for (let e6 of n2) {
277
290
  if (!e6.trim()) continue;
@@ -293,7 +306,58 @@ function v(e5) {
293
306
  remainder: r2
294
307
  };
295
308
  }
296
- async function* y(t3, n2) {
309
+ var C = class {
310
+ constructor(e5) {
311
+ __publicField(this, "lineEnding", /\r\n|\r|\n/g);
312
+ __publicField(this, "pending", []);
313
+ __publicField(this, "pendingLength", 0);
314
+ __publicField(this, "afterCr", false);
315
+ __publicField(this, "type", "message");
316
+ __publicField(this, "dataLines", []);
317
+ __publicField(this, "dataLength", 0);
318
+ this.url = e5;
319
+ }
320
+ push(t3) {
321
+ let n2 = [];
322
+ this.afterCr && t3.length > 0 && (t3.startsWith("\n") && (t3 = t3.slice(1)), this.afterCr = false);
323
+ let r2 = 0;
324
+ this.lineEnding.lastIndex = 0;
325
+ let i2;
326
+ for (; (i2 = this.lineEnding.exec(t3)) !== null; ) {
327
+ this.pending.push(t3.slice(r2, i2.index));
328
+ let e5 = this.pending.join("");
329
+ this.pending = [], this.pendingLength = 0, r2 = this.lineEnding.lastIndex, this.afterCr = i2[0] === "\r" && r2 === t3.length;
330
+ let a2 = this.processLine(e5);
331
+ a2 && n2.push(a2);
332
+ }
333
+ 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 ${b}`, { url: this.url });
334
+ return n2;
335
+ }
336
+ processLine(t3) {
337
+ if (t3.length > 1048576) throw new e(`SSE line exceeds the maximum length of ${b}`, { url: this.url });
338
+ if (t3 === "") return this.dispatch();
339
+ if (t3.startsWith("event:")) this.type = t3.slice(6).trim();
340
+ else if (t3.startsWith("data:")) {
341
+ let n2 = t3.slice(5).trim();
342
+ 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 ${x}`, { url: this.url });
343
+ this.dataLines.push(n2);
344
+ }
345
+ }
346
+ dispatch() {
347
+ let { type: e5, dataLines: t3 } = this;
348
+ if (this.type = "message", this.dataLines = [], this.dataLength = 0, t3.length === 0) return;
349
+ let n2 = t3.join("\n"), r2 = n2;
350
+ try {
351
+ r2 = JSON.parse(n2);
352
+ } catch {
353
+ }
354
+ return {
355
+ type: e5,
356
+ data: r2
357
+ };
358
+ }
359
+ };
360
+ async function* w(t3, n2) {
297
361
  for (; !n2.aborted; ) {
298
362
  let r2 = new AbortController(), i2 = () => r2.abort();
299
363
  n2.addEventListener("abort", i2, { once: true });
@@ -312,18 +376,16 @@ async function* y(t3, n2) {
312
376
  url: t3
313
377
  });
314
378
  }
315
- let a2 = i3.body.getReader(), o2 = new TextDecoder(), s2 = "";
379
+ let a2 = i3.body.getReader(), o2 = new TextDecoder(), s2 = new C(t3);
316
380
  try {
317
381
  for (; !n2.aborted; ) {
318
382
  let { done: e5, value: t4 } = await a2.read();
319
383
  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;
384
+ for (let e6 of s2.push(o2.decode(t4, { stream: true }))) yield e6;
324
385
  }
325
386
  } finally {
326
- a2.cancel();
387
+ a2.cancel().catch(() => {
388
+ });
327
389
  }
328
390
  } catch (e5) {
329
391
  if (n2.aborted || e5.name === "AbortError") return;
@@ -338,7 +400,7 @@ async function* y(t3, n2) {
338
400
  }
339
401
  }
340
402
  }
341
- var b = 500, x = 3e3, S = class {
403
+ var T = 500, E = 3e3, D = class {
342
404
  constructor(e5) {
343
405
  __publicField(this, "baseUrl");
344
406
  __publicField(this, "pending", /* @__PURE__ */ new Map());
@@ -357,7 +419,7 @@ var b = 500, x = 3e3, S = class {
357
419
  this.pending.clear();
358
420
  }
359
421
  watch(e5, t3, n2 = 32e3) {
360
- return this.start(), new C(e5, this, t3, n2);
422
+ return this.start(), new O(e5, this, t3, n2);
361
423
  }
362
424
  register(e5) {
363
425
  return new Promise((t3, n2) => {
@@ -372,7 +434,7 @@ var b = 500, x = 3e3, S = class {
372
434
  }
373
435
  async run(e5) {
374
436
  let t3 = `${this.baseUrl}/events`;
375
- for await (let n2 of y(t3, e5)) {
437
+ for await (let n2 of w(t3, e5)) {
376
438
  if (n2.type !== "TransactionFinalized") continue;
377
439
  let e6 = n2.data, t4 = this.pending.get(e6.transaction_id);
378
440
  if (!t4) continue;
@@ -388,7 +450,7 @@ var b = 500, x = 3e3, S = class {
388
450
  } else t4.resolve({ decision: "Indeterminate" });
389
451
  }
390
452
  }
391
- }, C = class {
453
+ }, O = class {
392
454
  constructor(e5, t3, n2, r2) {
393
455
  __publicField(this, "txId");
394
456
  __publicField(this, "watcher");
@@ -415,7 +477,7 @@ var b = 500, x = 3e3, S = class {
415
477
  r2,
416
478
  this.cancellation.promise
417
479
  ]);
418
- if (n2 !== null && clearTimeout(n2), i3 === "sse-timeout") return await Promise.race([this.restPollUntilFinal(Date.now() + x, o2.signal, x), a2]);
480
+ if (n2 !== null && clearTimeout(n2), i3 === "sse-timeout") return await Promise.race([this.restPollUntilFinal(Date.now() + E, o2.signal, E), a2]);
419
481
  if (i3.decision === "Indeterminate") return await Promise.race([this.restPollUntilFinal(t3, o2.signal, 0), a2]);
420
482
  if (i3.decision === "Commit") return { outcome: "Commit" };
421
483
  let s2 = await this.client.getTransactionResult(this.txId);
@@ -451,7 +513,7 @@ var b = 500, x = 3e3, S = class {
451
513
  if (e6) return e6.outcome === "Commit" ? { outcome: "Commit" } : this.throwFromReceipt(r3, e6.reason ?? "");
452
514
  }
453
515
  if (Date.now() >= e5) break;
454
- await this.delay(b, t3);
516
+ await this.delay(T, t3);
455
517
  } while (Date.now() < e5 && !t3.aborted);
456
518
  if (t3.aborted) throw new n(`Wait for transaction ${this.txId} was cancelled`);
457
519
  let s2 = this.timeoutMs + r2, c2 = r2 > 0 ? ` (incl. ${r2}ms grace)` : "";
@@ -482,11 +544,11 @@ var b = 500, x = 3e3, S = class {
482
544
  return this.client.getTransactionResult(this.txId);
483
545
  }
484
546
  };
485
- function w(e5) {
547
+ function k(e5) {
486
548
  let t3 = e5 instanceof Error ? e5.message : String(e5);
487
549
  return /not found/i.test(t3) || t3.includes("404");
488
550
  }
489
- var T = class t2 {
551
+ var A = class t2 {
490
552
  constructor(e5, t3, n2, r2) {
491
553
  __publicField(this, "client");
492
554
  __publicField(this, "_network");
@@ -496,14 +558,14 @@ var T = class t2 {
496
558
  this.client = e5, this._network = t3, this._url = n2, this.defaultTransactionTimeoutMs = r2;
497
559
  }
498
560
  static async connect(e5) {
499
- let n2 = _.usingFetchTransport(e5.url);
561
+ let n2 = y.usingFetchTransport(e5.url);
500
562
  return await n2.identityGet(), new t2(n2, e5.network, e5.url, e5.defaultTransactionTimeoutMs ?? 6e4);
501
563
  }
502
564
  getClient() {
503
565
  return this.client;
504
566
  }
505
567
  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);
568
+ return this._watcher || (this._watcher = new D(this._url), this._watcher.start()), this._watcher.watch(e5, this.client, t3 ?? this.defaultTransactionTimeoutMs);
507
569
  }
508
570
  stopWatcher() {
509
571
  var _a;
@@ -527,7 +589,7 @@ var T = class t2 {
527
589
  try {
528
590
  return await this.getSubstate(n2);
529
591
  } catch (e6) {
530
- if (w(e6)) return null;
592
+ if (k(e6)) return null;
531
593
  throw e6;
532
594
  }
533
595
  }
@@ -562,7 +624,7 @@ var T = class t2 {
562
624
  };
563
625
  } catch (n2) {
564
626
  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.`, {
627
+ throw k(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
628
  cause: n2,
567
629
  url: this._url
568
630
  }) : new e(`Failed to resolve input "${t4.substate_id}": ${r2}. Check the indexer URL points at the same network as the substate.`, {
@@ -575,7 +637,7 @@ var T = class t2 {
575
637
  async listRecentTransactions(e5) {
576
638
  return await this.client.listRecentTransactions(e5);
577
639
  }
578
- }, E = class e4 {
640
+ }, j = class e4 {
579
641
  constructor() {
580
642
  __publicField(this, "_network", t.LocalNet);
581
643
  __publicField(this, "_url", null);
@@ -599,10 +661,10 @@ var T = class t2 {
599
661
  network: this._network,
600
662
  defaultTransactionTimeoutMs: this._transactionTimeoutMs
601
663
  };
602
- return T.connect(e5);
664
+ return A.connect(e5);
603
665
  }
604
666
  };
605
- async function D(e5, t3) {
667
+ async function M(e5, t3) {
606
668
  return Promise.all(t3.map(async (t4) => {
607
669
  if (t4.type === "SpecificSubstate") {
608
670
  if (t4.version != null) return {
@@ -632,13 +694,15 @@ async function D(e5, t3) {
632
694
  }));
633
695
  }
634
696
  export {
635
- _ 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,
643
- g as transports
697
+ y as IndexerClient,
698
+ A as IndexerProvider,
699
+ x as MAX_SSE_EVENT_DATA_LENGTH,
700
+ b as MAX_SSE_LINE_LENGTH,
701
+ O as PendingTransaction,
702
+ j as ProviderBuilder,
703
+ D as TransactionWatcher,
704
+ w as openEventStream,
705
+ S as parseSseChunk,
706
+ M as resolveWantInputs,
707
+ v as transports
644
708
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tari-project/ootle-indexer",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/tari-project/ootle.ts.git",
@@ -25,9 +25,9 @@
25
25
  }
26
26
  },
27
27
  "dependencies": {
28
- "@tari-project/indexer-client": "^1.11.0",
29
- "@tari-project/ootle-ts-bindings": "^1.60.0",
30
- "@tari-project/ootle": "0.11.0"
28
+ "@tari-project/indexer-client": "^1.13.0",
29
+ "@tari-project/ootle-ts-bindings": "^1.62.0",
30
+ "@tari-project/ootle": "0.12.0"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/node": "^25.5.0",