@adaptic/utils 0.0.378 → 0.0.380

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.mjs CHANGED
@@ -11456,22 +11456,28 @@ class AlpacaMarketDataAPI extends EventEmitter {
11456
11456
  v1beta1url;
11457
11457
  stockStreamUrl = 'wss://stream.data.alpaca.markets/v2/sip'; // production values
11458
11458
  optionStreamUrl = 'wss://stream.data.alpaca.markets/v1beta3/options'; // production values
11459
+ cryptoStreamUrl = 'wss://stream.data.alpaca.markets/v1beta3/crypto/us'; // production values
11459
11460
  stockWs = null;
11460
11461
  optionWs = null;
11462
+ cryptoWs = null;
11461
11463
  stockSubscriptions = { trades: [], quotes: [], bars: [] };
11462
11464
  optionSubscriptions = { trades: [], quotes: [], bars: [] };
11465
+ cryptoSubscriptions = { trades: [], quotes: [], bars: [], dailyBars: [], updatedBars: [], orderbooks: [] };
11463
11466
  setMode(mode = 'production') {
11464
11467
  if (mode === 'sandbox') { // sandbox mode
11465
11468
  this.stockStreamUrl = 'wss://stream.data.sandbox.alpaca.markets/v2/sip';
11466
11469
  this.optionStreamUrl = 'wss://stream.data.sandbox.alpaca.markets/v1beta3/options';
11470
+ this.cryptoStreamUrl = 'wss://stream.data.sandbox.alpaca.markets/v1beta3/crypto/us';
11467
11471
  }
11468
11472
  else if (mode === 'test') { // test mode, can only use ticker FAKEPACA
11469
11473
  this.stockStreamUrl = 'wss://stream.data.alpaca.markets/v2/test';
11470
11474
  this.optionStreamUrl = 'wss://stream.data.alpaca.markets/v1beta3/options'; // there's no test mode for options
11475
+ this.cryptoStreamUrl = 'wss://stream.data.alpaca.markets/v1beta3/crypto/us'; // there's no test mode for crypto
11471
11476
  }
11472
11477
  else { // production
11473
11478
  this.stockStreamUrl = 'wss://stream.data.alpaca.markets/v2/sip';
11474
11479
  this.optionStreamUrl = 'wss://stream.data.alpaca.markets/v1beta3/options';
11480
+ this.cryptoStreamUrl = 'wss://stream.data.alpaca.markets/v1beta3/crypto/us';
11475
11481
  }
11476
11482
  }
11477
11483
  getMode() {
@@ -11513,14 +11519,17 @@ class AlpacaMarketDataAPI extends EventEmitter {
11513
11519
  return super.emit(event, ...args);
11514
11520
  }
11515
11521
  connect(streamType) {
11516
- const url = streamType === 'stock' ? this.stockStreamUrl : this.optionStreamUrl;
11522
+ const url = streamType === 'stock' ? this.stockStreamUrl : streamType === 'option' ? this.optionStreamUrl : this.cryptoStreamUrl;
11517
11523
  const ws = new WebSocket(url);
11518
11524
  if (streamType === 'stock') {
11519
11525
  this.stockWs = ws;
11520
11526
  }
11521
- else {
11527
+ else if (streamType === 'option') {
11522
11528
  this.optionWs = ws;
11523
11529
  }
11530
+ else {
11531
+ this.cryptoWs = ws;
11532
+ }
11524
11533
  ws.on('open', () => {
11525
11534
  log(`${streamType} stream connected`, { type: 'info' });
11526
11535
  const authMessage = {
@@ -11552,9 +11561,12 @@ class AlpacaMarketDataAPI extends EventEmitter {
11552
11561
  if (streamType === 'stock') {
11553
11562
  this.stockWs = null;
11554
11563
  }
11555
- else {
11564
+ else if (streamType === 'option') {
11556
11565
  this.optionWs = null;
11557
11566
  }
11567
+ else {
11568
+ this.cryptoWs = null;
11569
+ }
11558
11570
  // Optional: implement reconnect logic
11559
11571
  });
11560
11572
  ws.on('error', (error) => {
@@ -11562,19 +11574,31 @@ class AlpacaMarketDataAPI extends EventEmitter {
11562
11574
  });
11563
11575
  }
11564
11576
  sendSubscription(streamType) {
11565
- const ws = streamType === 'stock' ? this.stockWs : this.optionWs;
11566
- const subscriptions = streamType === 'stock' ? this.stockSubscriptions : this.optionSubscriptions;
11577
+ const ws = streamType === 'stock' ? this.stockWs : streamType === 'option' ? this.optionWs : this.cryptoWs;
11578
+ const subscriptions = streamType === 'stock' ? this.stockSubscriptions : streamType === 'option' ? this.optionSubscriptions : this.cryptoSubscriptions;
11567
11579
  if (ws && ws.readyState === WebSocket.OPEN) {
11568
11580
  const subMessagePayload = {};
11569
- if (subscriptions.trades.length > 0) {
11581
+ if (subscriptions.trades && subscriptions.trades.length > 0) {
11570
11582
  subMessagePayload.trades = subscriptions.trades;
11571
11583
  }
11572
- if (subscriptions.quotes.length > 0) {
11584
+ if (subscriptions.quotes && subscriptions.quotes.length > 0) {
11573
11585
  subMessagePayload.quotes = subscriptions.quotes;
11574
11586
  }
11575
- if (subscriptions.bars.length > 0) {
11587
+ if (subscriptions.bars && subscriptions.bars.length > 0) {
11576
11588
  subMessagePayload.bars = subscriptions.bars;
11577
11589
  }
11590
+ // Crypto-specific subscription types
11591
+ if (streamType === 'crypto') {
11592
+ if (subscriptions.dailyBars && subscriptions.dailyBars.length > 0) {
11593
+ subMessagePayload.dailyBars = subscriptions.dailyBars;
11594
+ }
11595
+ if (subscriptions.updatedBars && subscriptions.updatedBars.length > 0) {
11596
+ subMessagePayload.updatedBars = subscriptions.updatedBars;
11597
+ }
11598
+ if (subscriptions.orderbooks && subscriptions.orderbooks.length > 0) {
11599
+ subMessagePayload.orderbooks = subscriptions.orderbooks;
11600
+ }
11601
+ }
11578
11602
  if (Object.keys(subMessagePayload).length > 0) {
11579
11603
  const subMessage = {
11580
11604
  action: 'subscribe',
@@ -11604,8 +11628,18 @@ class AlpacaMarketDataAPI extends EventEmitter {
11604
11628
  this.optionWs.close();
11605
11629
  }
11606
11630
  }
11631
+ connectCryptoStream() {
11632
+ if (!this.cryptoWs) {
11633
+ this.connect('crypto');
11634
+ }
11635
+ }
11636
+ disconnectCryptoStream() {
11637
+ if (this.cryptoWs) {
11638
+ this.cryptoWs.close();
11639
+ }
11640
+ }
11607
11641
  subscribe(streamType, subscriptions) {
11608
- const currentSubscriptions = streamType === 'stock' ? this.stockSubscriptions : this.optionSubscriptions;
11642
+ const currentSubscriptions = streamType === 'stock' ? this.stockSubscriptions : streamType === 'option' ? this.optionSubscriptions : this.cryptoSubscriptions;
11609
11643
  Object.entries(subscriptions).forEach(([key, value]) => {
11610
11644
  if (value) {
11611
11645
  currentSubscriptions[key] = [...new Set([...(currentSubscriptions[key] || []), ...value])];
@@ -11614,7 +11648,7 @@ class AlpacaMarketDataAPI extends EventEmitter {
11614
11648
  this.sendSubscription(streamType);
11615
11649
  }
11616
11650
  unsubscribe(streamType, subscriptions) {
11617
- const currentSubscriptions = streamType === 'stock' ? this.stockSubscriptions : this.optionSubscriptions;
11651
+ const currentSubscriptions = streamType === 'stock' ? this.stockSubscriptions : streamType === 'option' ? this.optionSubscriptions : this.cryptoSubscriptions;
11618
11652
  Object.entries(subscriptions).forEach(([key, value]) => {
11619
11653
  if (value) {
11620
11654
  currentSubscriptions[key] = (currentSubscriptions[key] || []).filter(s => !value.includes(s));
@@ -11624,7 +11658,7 @@ class AlpacaMarketDataAPI extends EventEmitter {
11624
11658
  action: 'unsubscribe',
11625
11659
  ...subscriptions,
11626
11660
  };
11627
- const ws = streamType === 'stock' ? this.stockWs : this.optionWs;
11661
+ const ws = streamType === 'stock' ? this.stockWs : streamType === 'option' ? this.optionWs : this.cryptoWs;
11628
11662
  if (ws && ws.readyState === WebSocket.OPEN) {
11629
11663
  ws.send(JSON.stringify(unsubMessage));
11630
11664
  }