ault-sdk-ts 0.7.1 → 0.7.2

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/README.md CHANGED
@@ -581,9 +581,15 @@ const ws = createDexWs("wss://my-dex-api.example.com/ws", {
581
581
  reconnectDelay: 1000, // default: 1000ms
582
582
  maxReconnectDelay: 30000, // default: 30000ms
583
583
  maxReconnectAttempts: Infinity, // default: Infinity
584
+ userAgent: "my-bot/1.0", // default: ault-sdk-ts/<version>; Node and Bun only
584
585
  });
585
586
  ```
586
587
 
588
+ The DEX API rejects handshakes that carry no `User-Agent`. Browsers send their
589
+ own. Under Node or Bun the SDK sends `ault-sdk-ts/<version>`, or whatever you
590
+ put in `userAgent`. If you pass your own `createWebSocket` factory you are on
591
+ your own for the header.
592
+
587
593
  ### Disconnecting
588
594
 
589
595
  ```typescript
@@ -8,6 +8,14 @@ export interface FetchWithRetryOptions {
8
8
  retryableMethods?: string[];
9
9
  retryableStatuses?: number[];
10
10
  retryOnTimeout?: boolean;
11
+ /**
12
+ * `User-Agent` sent on every request under Node or Bun (default:
13
+ * `ault-sdk-ts/<version>`). A `User-Agent` already present in the request
14
+ * headers wins. Browsers are left alone: they send their own, and a custom
15
+ * one would only force a CORS preflight. The hosted DEX API sits behind a
16
+ * WAF that rejects requests without one.
17
+ */
18
+ userAgent?: string;
11
19
  }
12
20
  /** Statuses retried by default when the method is retryable. */
13
21
  export declare const DEFAULT_RETRYABLE_STATUSES: readonly number[];
@@ -0,0 +1,15 @@
1
+ export declare const SDK_VERSION: string;
2
+ /**
3
+ * Default `User-Agent` sent on every REST request and WebSocket handshake
4
+ * when running under Node or Bun. The hosted DEX API sits behind a WAF that
5
+ * rejects requests without one. Browsers always send their own, and setting
6
+ * it there would only add a CORS preflight, so the SDK leaves them alone.
7
+ */
8
+ export declare const USER_AGENT: string;
9
+ /**
10
+ * True under Node or Bun, where the SDK owns the `User-Agent`: `fetch` accepts
11
+ * the header and `WebSocket` accepts `{ headers }` as its second argument.
12
+ * False in browsers (which send their own and would preflight on a custom
13
+ * one) and Deno (whose `WebSocket` only takes a protocol list).
14
+ */
15
+ export declare function isServerRuntime(): boolean;
package/dist/index.cjs CHANGED
@@ -125,6 +125,7 @@ __export(index_exports, {
125
125
  TxError: () => TxError,
126
126
  TxNotConfirmedError: () => TxNotConfirmedError,
127
127
  TxSimulationError: () => TxSimulationError,
128
+ USER_AGENT: () => USER_AGENT,
128
129
  ValidatorStatus: () => BondStatus,
129
130
  WebSocketError: () => WebSocketError,
130
131
  WithdrawRewardsPartialError: () => WithdrawRewardsPartialError,
@@ -319,6 +320,16 @@ __export(index_exports, {
319
320
  });
320
321
  module.exports = __toCommonJS(index_exports);
321
322
 
323
+ // src/core/user-agent.ts
324
+ var SDK_VERSION = "0.7.2";
325
+ var USER_AGENT = `ault-sdk-ts/${SDK_VERSION}`;
326
+ function isServerRuntime() {
327
+ if (typeof document !== "undefined") return false;
328
+ if (typeof globalThis.Deno !== "undefined") return false;
329
+ const versions = globalThis.process?.versions;
330
+ return Boolean(versions?.node || versions?.bun);
331
+ }
332
+
322
333
  // src/proto/gen/google/protobuf/duration.ts
323
334
  var import_wire = require("@bufbuild/protobuf/wire");
324
335
  function createBaseDuration() {
@@ -522,7 +533,8 @@ var DEFAULT_OPTIONS = {
522
533
  exponentialBackoff: true,
523
534
  retryableMethods: ["GET", "HEAD", "OPTIONS"],
524
535
  retryableStatuses: [...DEFAULT_RETRYABLE_STATUSES],
525
- retryOnTimeout: false
536
+ retryOnTimeout: false,
537
+ userAgent: USER_AGENT
526
538
  };
527
539
  function ensureFetch(fetchFn) {
528
540
  const resolved = fetchFn ?? globalThis.fetch;
@@ -567,6 +579,21 @@ async function fetchWithTimeout(fetchFn, url, options, timeout) {
567
579
  removeAbortListener?.();
568
580
  }
569
581
  }
582
+ function withUserAgent(options, userAgent) {
583
+ if (!isServerRuntime()) return options;
584
+ const entries = [];
585
+ const raw = options.headers;
586
+ if (Array.isArray(raw)) {
587
+ entries.push(...raw.map(([k, v]) => [k, v]));
588
+ } else if (raw && typeof raw.forEach === "function") {
589
+ raw.forEach((value, key) => entries.push([key, value]));
590
+ } else if (raw) {
591
+ entries.push(...Object.entries(raw));
592
+ }
593
+ if (entries.some(([k]) => k.toLowerCase() === "user-agent")) return options;
594
+ entries.push(["user-agent", userAgent]);
595
+ return { ...options, headers: entries };
596
+ }
570
597
  function calculateDelay(attempt, baseDelay, exponential) {
571
598
  if (exponential) {
572
599
  const exponentialDelay = baseDelay * Math.pow(2, attempt);
@@ -583,11 +610,12 @@ async function fetchWithRetry(url, options = {}, fetchOptions = {}, fetchFn) {
583
610
  const method = (options.method || "GET").toUpperCase();
584
611
  const canRetry = opts.retryableMethods.includes(method);
585
612
  const resolvedFetch = ensureFetch(fetchFn);
613
+ const init = withUserAgent(options, opts.userAgent);
586
614
  const isAbortError = (error) => error instanceof Error && error.name === "AbortError";
587
615
  let lastError = null;
588
616
  for (let attempt = 0; attempt <= opts.retries; attempt++) {
589
617
  try {
590
- const response = await fetchWithTimeout(resolvedFetch, url, options, opts.timeout);
618
+ const response = await fetchWithTimeout(resolvedFetch, url, init, opts.timeout);
591
619
  if (!response.ok && canRetry && opts.retryableStatuses.includes(response.status) && attempt < opts.retries) {
592
620
  const delay = calculateDelay(attempt, opts.retryDelay, opts.exponentialBackoff);
593
621
  await sleep(delay);
@@ -12448,22 +12476,31 @@ function frameMatches(sub, data) {
12448
12476
  }
12449
12477
  }
12450
12478
  var isowsModulePromise = null;
12451
- function wrapCtor(Ctor) {
12452
- return (url, protocols) => protocols === void 0 ? new Ctor(url) : new Ctor(url, protocols);
12479
+ function wrapCtor(Ctor, userAgent) {
12480
+ return (url, protocols) => {
12481
+ if (protocols !== void 0) return new Ctor(url, protocols);
12482
+ if (isServerRuntime()) {
12483
+ try {
12484
+ return new Ctor(url, { headers: { "User-Agent": userAgent } });
12485
+ } catch {
12486
+ }
12487
+ }
12488
+ return new Ctor(url);
12489
+ };
12453
12490
  }
12454
12491
  function currentGlobalCtor() {
12455
12492
  return typeof globalThis !== "undefined" && typeof globalThis.WebSocket === "function" ? globalThis.WebSocket : null;
12456
12493
  }
12457
- function getDefaultWebSocketFactory() {
12494
+ function getDefaultWebSocketFactory(userAgent) {
12458
12495
  const G = currentGlobalCtor();
12459
- if (G) return wrapCtor(G);
12496
+ if (G) return wrapCtor(G, userAgent);
12460
12497
  if (!isowsModulePromise) {
12461
12498
  isowsModulePromise = import("isows");
12462
12499
  }
12463
12500
  return isowsModulePromise.then((mod) => {
12464
12501
  const G2 = currentGlobalCtor();
12465
- if (G2) return wrapCtor(G2);
12466
- return wrapCtor(mod.WebSocket);
12502
+ if (G2) return wrapCtor(G2, userAgent);
12503
+ return wrapCtor(mod.WebSocket, userAgent);
12467
12504
  });
12468
12505
  }
12469
12506
  var DexWsClient = class {
@@ -12491,7 +12528,8 @@ var DexWsClient = class {
12491
12528
  reconnect: opts?.reconnect ?? true,
12492
12529
  maxReconnectAttempts: opts?.maxReconnectAttempts ?? Infinity,
12493
12530
  reconnectDelay: opts?.reconnectDelay ?? 1e3,
12494
- maxReconnectDelay: opts?.maxReconnectDelay ?? 3e4
12531
+ maxReconnectDelay: opts?.maxReconnectDelay ?? 3e4,
12532
+ userAgent: opts?.userAgent ?? USER_AGENT
12495
12533
  };
12496
12534
  this.userFactory = opts?.createWebSocket;
12497
12535
  }
@@ -12709,7 +12747,7 @@ var DexWsClient = class {
12709
12747
  }
12710
12748
  resolveFactory() {
12711
12749
  if (this.userFactory) return this.userFactory;
12712
- return getDefaultWebSocketFactory();
12750
+ return getDefaultWebSocketFactory(this.options.userAgent);
12713
12751
  }
12714
12752
  handleMessage(raw) {
12715
12753
  let msg2;
@@ -28940,9 +28978,6 @@ var GetUDFTimescaleMarksResponse = zod.array(GetUDFTimescaleMarksResponseItem);
28940
28978
  function createDexWs(url, options) {
28941
28979
  return new DexWsClient(url, options);
28942
28980
  }
28943
-
28944
- // src/index.ts
28945
- var SDK_VERSION = "0.7.1";
28946
28981
  // Annotate the CommonJS export names for ESM import in node:
28947
28982
  0 && (module.exports = {
28948
28983
  AGENT_DEFAULTS,
@@ -29040,6 +29075,7 @@ var SDK_VERSION = "0.7.1";
29040
29075
  TxError,
29041
29076
  TxNotConfirmedError,
29042
29077
  TxSimulationError,
29078
+ USER_AGENT,
29043
29079
  ValidatorStatus,
29044
29080
  WebSocketError,
29045
29081
  WithdrawRewardsPartialError,