@luisjpf/alpaca-sdk 0.3.2 → 0.3.4

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.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4;// src/core/errors.ts
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5;// src/core/errors.ts
2
2
  var ErrorType = {
3
3
  Authentication: "authentication",
4
4
  Forbidden: "forbidden",
@@ -546,7 +546,7 @@ function createTradingClient(config) {
546
546
  })
547
547
  );
548
548
  },
549
- /** Cancel an order */
549
+ /** Cancel an order. Returns void on success (204 No Content). */
550
550
  async cancel(orderId, options) {
551
551
  unwrapOptional(
552
552
  await client.DELETE("/v2/orders/{order_id}", {
@@ -706,7 +706,7 @@ function createTradingClient(config) {
706
706
  })
707
707
  );
708
708
  },
709
- /** Delete a watchlist */
709
+ /** Delete a watchlist. Returns void on success (204 No Content). */
710
710
  async delete(watchlistId, options) {
711
711
  unwrapOptional(
712
712
  await client.DELETE("/v2/watchlists/{watchlist_id}", {
@@ -1228,7 +1228,7 @@ function createBrokerClient(config) {
1228
1228
  })
1229
1229
  );
1230
1230
  },
1231
- /** Delete/cancel a transfer */
1231
+ /** Delete/cancel a transfer. Returns void on success (204 No Content). */
1232
1232
  async delete(accountId, transferId, options) {
1233
1233
  unwrapOptional(
1234
1234
  await client.DELETE("/v1/accounts/{account_id}/transfers/{transfer_id}", {
@@ -1259,7 +1259,7 @@ function createBrokerClient(config) {
1259
1259
  })
1260
1260
  );
1261
1261
  },
1262
- /** Delete an ACH relationship */
1262
+ /** Delete an ACH relationship. Returns void on success (204 No Content). */
1263
1263
  async delete(accountId, achRelationshipId, options) {
1264
1264
  unwrapOptional(
1265
1265
  await client.DELETE("/v1/accounts/{account_id}/ach_relationships/{ach_relationship_id}", {
@@ -1311,7 +1311,7 @@ function createBrokerClient(config) {
1311
1311
  })
1312
1312
  );
1313
1313
  },
1314
- /** Cancel an order */
1314
+ /** Cancel an order. Returns void on success (204 No Content). */
1315
1315
  async cancel(accountId, orderId, options) {
1316
1316
  unwrapOptional(
1317
1317
  await client.DELETE("/v1/trading/accounts/{account_id}/orders/{order_id}", {
@@ -1451,6 +1451,8 @@ var RECONNECT_INITIAL_DELAY = 1e3;
1451
1451
  var RECONNECT_MAX_DELAY = 3e4;
1452
1452
  var RECONNECT_BACKOFF_MULTIPLIER = 2;
1453
1453
  var CONNECTION_TIMEOUT = 3e4;
1454
+ var MAX_PENDING_MESSAGES = 1e3;
1455
+ var MAX_PENDING_SUBSCRIPTIONS = 1e3;
1454
1456
  var BaseStream = (_class = class {
1455
1457
  __init() {this.ws = null}
1456
1458
  __init2() {this.state = "disconnected"}
@@ -1460,10 +1462,17 @@ var BaseStream = (_class = class {
1460
1462
  __init5() {this.connectionTimer = null}
1461
1463
  __init6() {this.shouldReconnect = true}
1462
1464
  __init7() {this.pendingSubscriptions = []}
1463
- __init8() {this.eventHandlers = /* @__PURE__ */ new Map()}
1464
- constructor(config) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);_class.prototype.__init6.call(this);_class.prototype.__init7.call(this);_class.prototype.__init8.call(this);
1465
+ __init8() {this.pendingMessages = []}
1466
+ __init9() {this.eventHandlers = /* @__PURE__ */ new Map()}
1467
+ constructor(config) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);_class.prototype.__init6.call(this);_class.prototype.__init7.call(this);_class.prototype.__init8.call(this);_class.prototype.__init9.call(this);
1465
1468
  this.config = config;
1466
1469
  }
1470
+ /**
1471
+ * Called after successful authentication to restore subscriptions.
1472
+ * Override in subclasses to implement subscription restoration.
1473
+ */
1474
+ onReconnected() {
1475
+ }
1467
1476
  /**
1468
1477
  * Get the current connection state.
1469
1478
  */
@@ -1497,6 +1506,8 @@ var BaseStream = (_class = class {
1497
1506
  this.ws.close();
1498
1507
  this.ws = null;
1499
1508
  }
1509
+ this.pendingMessages = [];
1510
+ this.pendingSubscriptions = [];
1500
1511
  this.setState("disconnected");
1501
1512
  }
1502
1513
  /**
@@ -1660,20 +1671,40 @@ var BaseStream = (_class = class {
1660
1671
  */
1661
1672
  onAuthenticated() {
1662
1673
  this.clearConnectionTimer();
1674
+ const wasReconnect = this.reconnectAttempts > 0;
1663
1675
  this.setState("connected");
1664
1676
  this.reconnectAttempts = 0;
1665
1677
  this.emit("connected");
1666
1678
  this.emit("authenticated");
1679
+ if (wasReconnect) {
1680
+ this.onReconnected();
1681
+ }
1667
1682
  for (const subscribe of this.pendingSubscriptions) {
1668
1683
  subscribe();
1669
1684
  }
1670
1685
  this.pendingSubscriptions = [];
1686
+ for (const message of this.pendingMessages) {
1687
+ this.send(message);
1688
+ }
1689
+ this.pendingMessages = [];
1671
1690
  }
1672
1691
  /**
1673
1692
  * Send a message to the WebSocket server.
1693
+ * If not connected, queues the message to be sent after reconnection.
1694
+ * If the queue is full, emits an error and drops the message.
1674
1695
  */
1675
1696
  send(message) {
1676
1697
  if (_optionalChain([this, 'access', _103 => _103.ws, 'optionalAccess', _104 => _104.readyState]) !== _ws2.default.OPEN) {
1698
+ if (this.pendingMessages.length >= MAX_PENDING_MESSAGES) {
1699
+ this.emit(
1700
+ "error",
1701
+ new Error(
1702
+ `Message queue full (max ${String(MAX_PENDING_MESSAGES)}). Message dropped. Consider reconnecting.`
1703
+ )
1704
+ );
1705
+ return;
1706
+ }
1707
+ this.pendingMessages.push(message);
1677
1708
  return;
1678
1709
  }
1679
1710
  if (this.config.useMsgpack) {
@@ -1684,11 +1715,21 @@ var BaseStream = (_class = class {
1684
1715
  }
1685
1716
  /**
1686
1717
  * Queue a subscription to be sent when connected.
1718
+ * If the queue is full, emits an error and drops the subscription.
1687
1719
  */
1688
1720
  queueOrSend(action) {
1689
1721
  if (this.state === "connected") {
1690
1722
  action();
1691
1723
  } else {
1724
+ if (this.pendingSubscriptions.length >= MAX_PENDING_SUBSCRIPTIONS) {
1725
+ this.emit(
1726
+ "error",
1727
+ new Error(
1728
+ `Subscription queue full (max ${String(MAX_PENDING_SUBSCRIPTIONS)}). Subscription dropped.`
1729
+ )
1730
+ );
1731
+ return;
1732
+ }
1692
1733
  this.pendingSubscriptions.push(action);
1693
1734
  }
1694
1735
  }
@@ -1735,15 +1776,130 @@ var BaseStream = (_class = class {
1735
1776
  }
1736
1777
  }, _class);
1737
1778
 
1779
+ // src/streaming/subscription-manager.ts
1780
+ var SubscriptionManager = (_class2 = class {constructor() { _class2.prototype.__init10.call(this);_class2.prototype.__init11.call(this);_class2.prototype.__init12.call(this); }
1781
+ __init10() {this.subscribedTrades = /* @__PURE__ */ new Set()}
1782
+ __init11() {this.subscribedQuotes = /* @__PURE__ */ new Set()}
1783
+ __init12() {this.subscribedBars = /* @__PURE__ */ new Set()}
1784
+ getSet(type) {
1785
+ switch (type) {
1786
+ case "trades":
1787
+ return this.subscribedTrades;
1788
+ case "quotes":
1789
+ return this.subscribedQuotes;
1790
+ case "bars":
1791
+ return this.subscribedBars;
1792
+ }
1793
+ }
1794
+ /**
1795
+ * Track symbols as subscribed and return only new symbols that weren't already subscribed.
1796
+ * Returns null if there are no new symbols to subscribe to.
1797
+ */
1798
+ subscribe(type, symbols) {
1799
+ const set = this.getSet(type);
1800
+ const newSymbols = symbols.filter((s) => !set.has(s));
1801
+ if (newSymbols.length === 0) {
1802
+ return null;
1803
+ }
1804
+ for (const symbol of newSymbols) {
1805
+ set.add(symbol);
1806
+ }
1807
+ return {
1808
+ action: "subscribe",
1809
+ [type]: newSymbols
1810
+ };
1811
+ }
1812
+ /**
1813
+ * Untrack symbols and return only symbols that were actually subscribed.
1814
+ * Returns null if there are no symbols to unsubscribe from.
1815
+ */
1816
+ unsubscribe(type, symbols) {
1817
+ const set = this.getSet(type);
1818
+ const existingSymbols = symbols.filter((s) => set.has(s));
1819
+ if (existingSymbols.length === 0) {
1820
+ return null;
1821
+ }
1822
+ for (const symbol of existingSymbols) {
1823
+ set.delete(symbol);
1824
+ }
1825
+ return {
1826
+ action: "unsubscribe",
1827
+ [type]: existingSymbols
1828
+ };
1829
+ }
1830
+ /**
1831
+ * Clear all subscriptions (e.g., on disconnect)
1832
+ */
1833
+ clear() {
1834
+ this.subscribedTrades.clear();
1835
+ this.subscribedQuotes.clear();
1836
+ this.subscribedBars.clear();
1837
+ }
1838
+ /**
1839
+ * Get all currently subscribed symbols for a type
1840
+ */
1841
+ getSubscribed(type) {
1842
+ return Array.from(this.getSet(type));
1843
+ }
1844
+ /**
1845
+ * Check if a symbol is subscribed for a type
1846
+ */
1847
+ isSubscribed(type, symbol) {
1848
+ return this.getSet(type).has(symbol);
1849
+ }
1850
+ /**
1851
+ * Check if there are any subscriptions
1852
+ */
1853
+ hasSubscriptions() {
1854
+ return this.subscribedTrades.size > 0 || this.subscribedQuotes.size > 0 || this.subscribedBars.size > 0;
1855
+ }
1856
+ /**
1857
+ * Get subscription messages to restore all current subscriptions.
1858
+ * Useful for re-subscribing after reconnection.
1859
+ * Does NOT clear internal state - subscriptions remain tracked.
1860
+ */
1861
+ getResubscribeMessages() {
1862
+ const messages = [];
1863
+ if (this.subscribedTrades.size > 0) {
1864
+ messages.push({
1865
+ action: "subscribe",
1866
+ trades: Array.from(this.subscribedTrades)
1867
+ });
1868
+ }
1869
+ if (this.subscribedQuotes.size > 0) {
1870
+ messages.push({
1871
+ action: "subscribe",
1872
+ quotes: Array.from(this.subscribedQuotes)
1873
+ });
1874
+ }
1875
+ if (this.subscribedBars.size > 0) {
1876
+ messages.push({
1877
+ action: "subscribe",
1878
+ bars: Array.from(this.subscribedBars)
1879
+ });
1880
+ }
1881
+ return messages;
1882
+ }
1883
+ }, _class2);
1884
+
1885
+ // src/streaming/types.ts
1886
+ function isTrade(message) {
1887
+ return message !== null && typeof message === "object" && "T" in message && "S" in message && message.T === "t";
1888
+ }
1889
+ function isQuote(message) {
1890
+ return message !== null && typeof message === "object" && "T" in message && "S" in message && message.T === "q";
1891
+ }
1892
+ function isBar(message) {
1893
+ return message !== null && typeof message === "object" && "T" in message && "S" in message && message.T === "b";
1894
+ }
1895
+
1738
1896
  // src/streaming/stock-stream.ts
1739
1897
  var STOCK_STREAM_BASE_URL = "wss://stream.data.alpaca.markets/v2";
1740
- var StockStreamImpl = (_class2 = class extends BaseStream {
1898
+ var StockStreamImpl = (_class3 = class extends BaseStream {
1741
1899
 
1742
- __init9() {this.subscribedTrades = /* @__PURE__ */ new Set()}
1743
- __init10() {this.subscribedQuotes = /* @__PURE__ */ new Set()}
1744
- __init11() {this.subscribedBars = /* @__PURE__ */ new Set()}
1900
+ __init13() {this.subscriptions = new SubscriptionManager()}
1745
1901
  constructor(config) {
1746
- super(config);_class2.prototype.__init9.call(this);_class2.prototype.__init10.call(this);_class2.prototype.__init11.call(this);;
1902
+ super(config);_class3.prototype.__init13.call(this);;
1747
1903
  this.feed = _nullishCoalesce(config.feed, () => ( "iex"));
1748
1904
  }
1749
1905
  getUrl() {
@@ -1757,101 +1913,57 @@ var StockStreamImpl = (_class2 = class extends BaseStream {
1757
1913
  };
1758
1914
  }
1759
1915
  handleMessage(message) {
1760
- const type = message.T;
1761
- switch (type) {
1762
- case "t":
1763
- this.emit("trade", message);
1764
- break;
1765
- case "q":
1766
- this.emit("quote", message);
1767
- break;
1768
- case "b":
1769
- this.emit("bar", message);
1770
- break;
1916
+ if (isTrade(message)) {
1917
+ this.emit("trade", message);
1918
+ } else if (isQuote(message)) {
1919
+ this.emit("quote", message);
1920
+ } else if (isBar(message)) {
1921
+ this.emit("bar", message);
1922
+ }
1923
+ }
1924
+ /**
1925
+ * Restore subscriptions after reconnection.
1926
+ */
1927
+ onReconnected() {
1928
+ const messages = this.subscriptions.getResubscribeMessages();
1929
+ for (const message of messages) {
1930
+ this.send(message);
1771
1931
  }
1772
1932
  }
1773
1933
  subscribeForTrades(symbols) {
1774
1934
  this.queueOrSend(() => {
1775
- const newSymbols = symbols.filter((s) => !this.subscribedTrades.has(s));
1776
- if (newSymbols.length === 0) return;
1777
- for (const symbol of newSymbols) {
1778
- this.subscribedTrades.add(symbol);
1779
- }
1780
- const message = {
1781
- action: "subscribe",
1782
- trades: newSymbols
1783
- };
1784
- this.send(message);
1935
+ const message = this.subscriptions.subscribe("trades", symbols);
1936
+ if (message) this.send(message);
1785
1937
  });
1786
1938
  }
1787
1939
  subscribeForQuotes(symbols) {
1788
1940
  this.queueOrSend(() => {
1789
- const newSymbols = symbols.filter((s) => !this.subscribedQuotes.has(s));
1790
- if (newSymbols.length === 0) return;
1791
- for (const symbol of newSymbols) {
1792
- this.subscribedQuotes.add(symbol);
1793
- }
1794
- const message = {
1795
- action: "subscribe",
1796
- quotes: newSymbols
1797
- };
1798
- this.send(message);
1941
+ const message = this.subscriptions.subscribe("quotes", symbols);
1942
+ if (message) this.send(message);
1799
1943
  });
1800
1944
  }
1801
1945
  subscribeForBars(symbols) {
1802
1946
  this.queueOrSend(() => {
1803
- const newSymbols = symbols.filter((s) => !this.subscribedBars.has(s));
1804
- if (newSymbols.length === 0) return;
1805
- for (const symbol of newSymbols) {
1806
- this.subscribedBars.add(symbol);
1807
- }
1808
- const message = {
1809
- action: "subscribe",
1810
- bars: newSymbols
1811
- };
1812
- this.send(message);
1947
+ const message = this.subscriptions.subscribe("bars", symbols);
1948
+ if (message) this.send(message);
1813
1949
  });
1814
1950
  }
1815
1951
  unsubscribeFromTrades(symbols) {
1816
1952
  this.queueOrSend(() => {
1817
- const existingSymbols = symbols.filter((s) => this.subscribedTrades.has(s));
1818
- if (existingSymbols.length === 0) return;
1819
- for (const symbol of existingSymbols) {
1820
- this.subscribedTrades.delete(symbol);
1821
- }
1822
- const message = {
1823
- action: "unsubscribe",
1824
- trades: existingSymbols
1825
- };
1826
- this.send(message);
1953
+ const message = this.subscriptions.unsubscribe("trades", symbols);
1954
+ if (message) this.send(message);
1827
1955
  });
1828
1956
  }
1829
1957
  unsubscribeFromQuotes(symbols) {
1830
1958
  this.queueOrSend(() => {
1831
- const existingSymbols = symbols.filter((s) => this.subscribedQuotes.has(s));
1832
- if (existingSymbols.length === 0) return;
1833
- for (const symbol of existingSymbols) {
1834
- this.subscribedQuotes.delete(symbol);
1835
- }
1836
- const message = {
1837
- action: "unsubscribe",
1838
- quotes: existingSymbols
1839
- };
1840
- this.send(message);
1959
+ const message = this.subscriptions.unsubscribe("quotes", symbols);
1960
+ if (message) this.send(message);
1841
1961
  });
1842
1962
  }
1843
1963
  unsubscribeFromBars(symbols) {
1844
1964
  this.queueOrSend(() => {
1845
- const existingSymbols = symbols.filter((s) => this.subscribedBars.has(s));
1846
- if (existingSymbols.length === 0) return;
1847
- for (const symbol of existingSymbols) {
1848
- this.subscribedBars.delete(symbol);
1849
- }
1850
- const message = {
1851
- action: "unsubscribe",
1852
- bars: existingSymbols
1853
- };
1854
- this.send(message);
1965
+ const message = this.subscriptions.unsubscribe("bars", symbols);
1966
+ if (message) this.send(message);
1855
1967
  });
1856
1968
  }
1857
1969
  onTrade(handler) {
@@ -1872,7 +1984,7 @@ var StockStreamImpl = (_class2 = class extends BaseStream {
1872
1984
  onError(handler) {
1873
1985
  this.on("error", handler);
1874
1986
  }
1875
- }, _class2);
1987
+ }, _class3);
1876
1988
  function createStockStream(config) {
1877
1989
  const impl = new StockStreamImpl(config);
1878
1990
  return {
@@ -1924,13 +2036,11 @@ function createStockStream(config) {
1924
2036
 
1925
2037
  // src/streaming/crypto-stream.ts
1926
2038
  var CRYPTO_STREAM_BASE_URL = "wss://stream.data.alpaca.markets/v1beta3/crypto";
1927
- var CryptoStreamImpl = (_class3 = class extends BaseStream {
2039
+ var CryptoStreamImpl = (_class4 = class extends BaseStream {
1928
2040
 
1929
- __init12() {this.subscribedTrades = /* @__PURE__ */ new Set()}
1930
- __init13() {this.subscribedQuotes = /* @__PURE__ */ new Set()}
1931
- __init14() {this.subscribedBars = /* @__PURE__ */ new Set()}
2041
+ __init14() {this.subscriptions = new SubscriptionManager()}
1932
2042
  constructor(config) {
1933
- super(config);_class3.prototype.__init12.call(this);_class3.prototype.__init13.call(this);_class3.prototype.__init14.call(this);;
2043
+ super(config);_class4.prototype.__init14.call(this);;
1934
2044
  this.location = _nullishCoalesce(config.location, () => ( "us"));
1935
2045
  }
1936
2046
  getUrl() {
@@ -1944,101 +2054,57 @@ var CryptoStreamImpl = (_class3 = class extends BaseStream {
1944
2054
  };
1945
2055
  }
1946
2056
  handleMessage(message) {
1947
- const type = message.T;
1948
- switch (type) {
1949
- case "t":
1950
- this.emit("trade", message);
1951
- break;
1952
- case "q":
1953
- this.emit("quote", message);
1954
- break;
1955
- case "b":
1956
- this.emit("bar", message);
1957
- break;
2057
+ if (isTrade(message)) {
2058
+ this.emit("trade", message);
2059
+ } else if (isQuote(message)) {
2060
+ this.emit("quote", message);
2061
+ } else if (isBar(message)) {
2062
+ this.emit("bar", message);
2063
+ }
2064
+ }
2065
+ /**
2066
+ * Restore subscriptions after reconnection.
2067
+ */
2068
+ onReconnected() {
2069
+ const messages = this.subscriptions.getResubscribeMessages();
2070
+ for (const message of messages) {
2071
+ this.send(message);
1958
2072
  }
1959
2073
  }
1960
2074
  subscribeForTrades(symbols) {
1961
2075
  this.queueOrSend(() => {
1962
- const newSymbols = symbols.filter((s) => !this.subscribedTrades.has(s));
1963
- if (newSymbols.length === 0) return;
1964
- for (const symbol of newSymbols) {
1965
- this.subscribedTrades.add(symbol);
1966
- }
1967
- const message = {
1968
- action: "subscribe",
1969
- trades: newSymbols
1970
- };
1971
- this.send(message);
2076
+ const message = this.subscriptions.subscribe("trades", symbols);
2077
+ if (message) this.send(message);
1972
2078
  });
1973
2079
  }
1974
2080
  subscribeForQuotes(symbols) {
1975
2081
  this.queueOrSend(() => {
1976
- const newSymbols = symbols.filter((s) => !this.subscribedQuotes.has(s));
1977
- if (newSymbols.length === 0) return;
1978
- for (const symbol of newSymbols) {
1979
- this.subscribedQuotes.add(symbol);
1980
- }
1981
- const message = {
1982
- action: "subscribe",
1983
- quotes: newSymbols
1984
- };
1985
- this.send(message);
2082
+ const message = this.subscriptions.subscribe("quotes", symbols);
2083
+ if (message) this.send(message);
1986
2084
  });
1987
2085
  }
1988
2086
  subscribeForBars(symbols) {
1989
2087
  this.queueOrSend(() => {
1990
- const newSymbols = symbols.filter((s) => !this.subscribedBars.has(s));
1991
- if (newSymbols.length === 0) return;
1992
- for (const symbol of newSymbols) {
1993
- this.subscribedBars.add(symbol);
1994
- }
1995
- const message = {
1996
- action: "subscribe",
1997
- bars: newSymbols
1998
- };
1999
- this.send(message);
2088
+ const message = this.subscriptions.subscribe("bars", symbols);
2089
+ if (message) this.send(message);
2000
2090
  });
2001
2091
  }
2002
2092
  unsubscribeFromTrades(symbols) {
2003
2093
  this.queueOrSend(() => {
2004
- const existingSymbols = symbols.filter((s) => this.subscribedTrades.has(s));
2005
- if (existingSymbols.length === 0) return;
2006
- for (const symbol of existingSymbols) {
2007
- this.subscribedTrades.delete(symbol);
2008
- }
2009
- const message = {
2010
- action: "unsubscribe",
2011
- trades: existingSymbols
2012
- };
2013
- this.send(message);
2094
+ const message = this.subscriptions.unsubscribe("trades", symbols);
2095
+ if (message) this.send(message);
2014
2096
  });
2015
2097
  }
2016
2098
  unsubscribeFromQuotes(symbols) {
2017
2099
  this.queueOrSend(() => {
2018
- const existingSymbols = symbols.filter((s) => this.subscribedQuotes.has(s));
2019
- if (existingSymbols.length === 0) return;
2020
- for (const symbol of existingSymbols) {
2021
- this.subscribedQuotes.delete(symbol);
2022
- }
2023
- const message = {
2024
- action: "unsubscribe",
2025
- quotes: existingSymbols
2026
- };
2027
- this.send(message);
2100
+ const message = this.subscriptions.unsubscribe("quotes", symbols);
2101
+ if (message) this.send(message);
2028
2102
  });
2029
2103
  }
2030
2104
  unsubscribeFromBars(symbols) {
2031
2105
  this.queueOrSend(() => {
2032
- const existingSymbols = symbols.filter((s) => this.subscribedBars.has(s));
2033
- if (existingSymbols.length === 0) return;
2034
- for (const symbol of existingSymbols) {
2035
- this.subscribedBars.delete(symbol);
2036
- }
2037
- const message = {
2038
- action: "unsubscribe",
2039
- bars: existingSymbols
2040
- };
2041
- this.send(message);
2106
+ const message = this.subscriptions.unsubscribe("bars", symbols);
2107
+ if (message) this.send(message);
2042
2108
  });
2043
2109
  }
2044
2110
  onTrade(handler) {
@@ -2059,7 +2125,7 @@ var CryptoStreamImpl = (_class3 = class extends BaseStream {
2059
2125
  onError(handler) {
2060
2126
  this.on("error", handler);
2061
2127
  }
2062
- }, _class3);
2128
+ }, _class4);
2063
2129
  function createCryptoStream(config) {
2064
2130
  const impl = new CryptoStreamImpl(config);
2065
2131
  return {
@@ -2117,7 +2183,7 @@ var LIVE_TRADING_URL = "wss://api.alpaca.markets/stream";
2117
2183
  var RECONNECT_INITIAL_DELAY2 = 1e3;
2118
2184
  var RECONNECT_MAX_DELAY2 = 3e4;
2119
2185
  var RECONNECT_BACKOFF_MULTIPLIER2 = 2;
2120
- var TradeUpdatesStreamImpl = (_class4 = class {
2186
+ var TradeUpdatesStreamImpl = (_class5 = class {
2121
2187
  __init15() {this.ws = null}
2122
2188
  __init16() {this.state = "disconnected"}
2123
2189
 
@@ -2127,7 +2193,7 @@ var TradeUpdatesStreamImpl = (_class4 = class {
2127
2193
  __init20() {this.isSubscribed = false}
2128
2194
  __init21() {this.pendingSubscribe = false}
2129
2195
  __init22() {this.eventHandlers = /* @__PURE__ */ new Map()}
2130
- constructor(config) {;_class4.prototype.__init15.call(this);_class4.prototype.__init16.call(this);_class4.prototype.__init17.call(this);_class4.prototype.__init18.call(this);_class4.prototype.__init19.call(this);_class4.prototype.__init20.call(this);_class4.prototype.__init21.call(this);_class4.prototype.__init22.call(this);
2196
+ constructor(config) {;_class5.prototype.__init15.call(this);_class5.prototype.__init16.call(this);_class5.prototype.__init17.call(this);_class5.prototype.__init18.call(this);_class5.prototype.__init19.call(this);_class5.prototype.__init20.call(this);_class5.prototype.__init21.call(this);_class5.prototype.__init22.call(this);
2131
2197
  this.config = config;
2132
2198
  }
2133
2199
  getUrl() {
@@ -2356,7 +2422,7 @@ var TradeUpdatesStreamImpl = (_class4 = class {
2356
2422
  this.state = state;
2357
2423
  }
2358
2424
  }
2359
- }, _class4);
2425
+ }, _class5);
2360
2426
  function createTradeUpdatesStream(config) {
2361
2427
  const impl = new TradeUpdatesStreamImpl(config);
2362
2428
  return {
@@ -2451,5 +2517,8 @@ function createAlpacaClient(config) {
2451
2517
 
2452
2518
 
2453
2519
 
2454
- exports.ALPACA_URLS = ALPACA_URLS; exports.AlpacaError = AlpacaError; exports.AuthenticationError = AuthenticationError; exports.ErrorType = ErrorType; exports.ForbiddenError = ForbiddenError; exports.InsufficientFundsError = InsufficientFundsError; exports.MarketClosedError = MarketClosedError; exports.NotFoundError = NotFoundError; exports.NotImplementedError = NotImplementedError; exports.RateLimitError = RateLimitError; exports.ServerError = ServerError; exports.ValidationError = ValidationError; exports.createAlpacaClient = createAlpacaClient; exports.createAlpacaError = createAlpacaError; exports.createApiError = createApiError; exports.createApiFetch = createApiFetch; exports.createApiKeyAuth = createApiKeyAuth; exports.createBasicAuth = createBasicAuth; exports.createBrokerClient = createBrokerClient; exports.createCryptoStream = createCryptoStream; exports.createHeaders = createHeaders; exports.createMarketDataClient = createMarketDataClient; exports.createOAuthAuth = createOAuthAuth; exports.createStockStream = createStockStream; exports.createTradeUpdatesStream = createTradeUpdatesStream; exports.createTradingClient = createTradingClient; exports.createWebSocketAuth = createWebSocketAuth; exports.createWebSocketOAuth = createWebSocketOAuth; exports.err = err; exports.fetchWithRetry = fetchWithRetry; exports.isApiError = isApiError; exports.isAuthenticationError = isAuthenticationError; exports.isInsufficientFundsError = isInsufficientFundsError; exports.isMarketClosedError = isMarketClosedError; exports.isNotFoundError = isNotFoundError; exports.isRateLimitError = isRateLimitError; exports.isServerError = isServerError; exports.isValidationError = isValidationError; exports.ok = ok; exports.resolveConfig = resolveConfig; exports.toApiError = toApiError; exports.unwrap = unwrap; exports.unwrapList = unwrapList; exports.unwrapOptional = unwrapOptional;
2520
+
2521
+
2522
+
2523
+ exports.ALPACA_URLS = ALPACA_URLS; exports.AlpacaError = AlpacaError; exports.AuthenticationError = AuthenticationError; exports.ErrorType = ErrorType; exports.ForbiddenError = ForbiddenError; exports.InsufficientFundsError = InsufficientFundsError; exports.MarketClosedError = MarketClosedError; exports.NotFoundError = NotFoundError; exports.NotImplementedError = NotImplementedError; exports.RateLimitError = RateLimitError; exports.ServerError = ServerError; exports.ValidationError = ValidationError; exports.createAlpacaClient = createAlpacaClient; exports.createAlpacaError = createAlpacaError; exports.createApiError = createApiError; exports.createApiFetch = createApiFetch; exports.createApiKeyAuth = createApiKeyAuth; exports.createBasicAuth = createBasicAuth; exports.createBrokerClient = createBrokerClient; exports.createCryptoStream = createCryptoStream; exports.createHeaders = createHeaders; exports.createMarketDataClient = createMarketDataClient; exports.createOAuthAuth = createOAuthAuth; exports.createStockStream = createStockStream; exports.createTradeUpdatesStream = createTradeUpdatesStream; exports.createTradingClient = createTradingClient; exports.createWebSocketAuth = createWebSocketAuth; exports.createWebSocketOAuth = createWebSocketOAuth; exports.err = err; exports.fetchWithRetry = fetchWithRetry; exports.isApiError = isApiError; exports.isAuthenticationError = isAuthenticationError; exports.isBar = isBar; exports.isInsufficientFundsError = isInsufficientFundsError; exports.isMarketClosedError = isMarketClosedError; exports.isNotFoundError = isNotFoundError; exports.isQuote = isQuote; exports.isRateLimitError = isRateLimitError; exports.isServerError = isServerError; exports.isTrade = isTrade; exports.isValidationError = isValidationError; exports.ok = ok; exports.resolveConfig = resolveConfig; exports.toApiError = toApiError; exports.unwrap = unwrap; exports.unwrapList = unwrapList; exports.unwrapOptional = unwrapOptional;
2455
2524
  //# sourceMappingURL=index.cjs.map