@liberfi.io/react-predict 0.3.46 → 0.3.48

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/server.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { httpGet, httpDelete, httpPost } from '@liberfi.io/utils';
1
+ import { httpGet, httpPost, httpDelete } from '@liberfi.io/utils';
2
2
 
3
3
  // src/hooks/predict/events.params.ts
4
4
  var DEFAULT_PAGE_SIZE = 24;
@@ -190,6 +190,17 @@ var PredictClient = class {
190
190
  const url = `${this.endpoint}/api/v1/markets/${encodeURIComponent(slug)}/orderbook${query}`;
191
191
  return await httpGet(url);
192
192
  }
193
+ /**
194
+ * Maps to `POST /api/v1/markets/orderbooks`.
195
+ * Fetches order books for many markets in a single request (max 100 items).
196
+ * Results are returned in the same order as `items`; per-item failures are
197
+ * reported via `OrderbookBatchResult.error` and never fail the whole batch.
198
+ */
199
+ async getOrderbooks(items) {
200
+ const url = `${this.endpoint}/api/v1/markets/orderbooks`;
201
+ const res = await httpPost(url, { items });
202
+ return res.results;
203
+ }
193
204
  /** Maps to `GET /api/v1/markets/:slug/trades?source=...`. */
194
205
  async listMarketTrades(slug, params) {
195
206
  const query = buildQuery(params);
@@ -614,6 +625,13 @@ var DEFAULT_RECONNECT_BASE = 1e3;
614
625
  var DEFAULT_RECONNECT_MAX = 3e4;
615
626
  var DEFAULT_PING_INTERVAL = 2e4;
616
627
  var DEFAULT_PONG_TIMEOUT = 1e4;
628
+ var MAX_SUBSCRIPTION_FRAME_BYTES = 3500;
629
+ function byteLength(str) {
630
+ if (typeof TextEncoder !== "undefined") {
631
+ return new TextEncoder().encode(str).length;
632
+ }
633
+ return str.length;
634
+ }
617
635
  var PredictWsClient = class {
618
636
  ws = null;
619
637
  wsUrl;
@@ -731,7 +749,7 @@ var PredictWsClient = class {
731
749
  const set = this.subs.channels.get(ch);
732
750
  for (const slug of marketSlugs) set.add(slug);
733
751
  }
734
- this.send({ type: "subscribe", channels, market_slugs: marketSlugs });
752
+ this.sendChunkedSubscription("subscribe", channels, marketSlugs);
735
753
  }
736
754
  /**
737
755
  * Unsubscribe from one or more channels for the given market slugs.
@@ -741,7 +759,7 @@ var PredictWsClient = class {
741
759
  const set = this.subs.channels.get(ch);
742
760
  for (const slug of marketSlugs) set.delete(slug);
743
761
  }
744
- this.send({ type: "unsubscribe", channels, market_slugs: marketSlugs });
762
+ this.sendChunkedSubscription("unsubscribe", channels, marketSlugs);
745
763
  }
746
764
  // -------------------------------------------------------------------------
747
765
  // Subscription — convenience (single channel)
@@ -897,13 +915,39 @@ var PredictWsClient = class {
897
915
  restoreSubscriptions() {
898
916
  for (const [channel, slugs] of this.subs.channels.entries()) {
899
917
  if (slugs.size > 0) {
900
- this.send({
901
- type: "subscribe",
902
- channels: [channel],
903
- market_slugs: Array.from(slugs)
904
- });
918
+ this.sendChunkedSubscription("subscribe", [channel], Array.from(slugs));
919
+ }
920
+ }
921
+ }
922
+ /**
923
+ * Send a subscribe/unsubscribe message, splitting `marketSlugs` across
924
+ * multiple frames so no single frame exceeds the server read limit.
925
+ */
926
+ sendChunkedSubscription(type, channels, marketSlugs) {
927
+ if (marketSlugs.length === 0) {
928
+ this.send({ type, channels, market_slugs: [] });
929
+ return;
930
+ }
931
+ const envelopeBytes = byteLength(
932
+ JSON.stringify({ type, channels, market_slugs: [] })
933
+ );
934
+ const budget = MAX_SUBSCRIPTION_FRAME_BYTES - envelopeBytes;
935
+ let chunk = [];
936
+ let chunkBytes = 0;
937
+ const flush = () => {
938
+ if (chunk.length > 0) {
939
+ this.send({ type, channels, market_slugs: chunk });
940
+ chunk = [];
941
+ chunkBytes = 0;
905
942
  }
943
+ };
944
+ for (const slug of marketSlugs) {
945
+ const slugBytes = byteLength(JSON.stringify(slug)) + 1;
946
+ if (chunk.length > 0 && chunkBytes + slugBytes > budget) flush();
947
+ chunk.push(slug);
948
+ chunkBytes += slugBytes;
906
949
  }
950
+ flush();
907
951
  }
908
952
  startPing() {
909
953
  this.stopPing();