@solana/web3.js 1.5.1 → 1.6.1

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/module.flow.js CHANGED
@@ -1,14 +1,14 @@
1
1
  /**
2
2
  * Flowtype definitions for index
3
3
  * Generated by Flowgen from a Typescript Definition
4
- * Flowgen v1.13.0
4
+ * Flowgen v1.14.0
5
5
  */
6
6
 
7
7
  declare module "@solana/web3.js" {
8
8
  /**
9
9
  * Maximum length of derived pubkey seed
10
10
  */
11
- declare export var MAX_SEED_LENGTH: any; // 32
11
+ declare export var MAX_SEED_LENGTH: 32;
12
12
 
13
13
  /**
14
14
  * A public key
@@ -1621,6 +1621,44 @@ declare module "@solana/web3.js" {
1621
1621
  ...
1622
1622
  };
1623
1623
 
1624
+ /**
1625
+ * An object defining headers to be passed to the RPC server
1626
+ */
1627
+ declare export type HttpHeaders = {
1628
+ [header: string]: string,
1629
+ ...
1630
+ };
1631
+
1632
+ /**
1633
+ * A callback used to augment the outgoing HTTP request
1634
+ */
1635
+ declare export type FetchMiddleware = (
1636
+ url: string,
1637
+ options: any,
1638
+ fetch: Function
1639
+ ) => void;
1640
+
1641
+ /**
1642
+ * Configuration for instantiating a Connection
1643
+ */
1644
+ declare export type ConnectionConfig = {
1645
+ /**
1646
+ * Optional commitment level
1647
+ */
1648
+ commitment?: Commitment,
1649
+
1650
+ /**
1651
+ * Optional HTTP headers object
1652
+ */
1653
+ httpHeaders?: HttpHeaders,
1654
+
1655
+ /**
1656
+ * Optional fetch middleware callback
1657
+ */
1658
+ fetchMiddleware?: FetchMiddleware,
1659
+ ...
1660
+ };
1661
+
1624
1662
  /**
1625
1663
  * A connection to a fullnode JSON RPC endpoint
1626
1664
  */
@@ -1628,9 +1666,12 @@ declare module "@solana/web3.js" {
1628
1666
  /**
1629
1667
  * Establish a JSON RPC connection
1630
1668
  * @param endpoint URL to the fullnode JSON RPC endpoint
1631
- * @param commitment optional default commitment level
1669
+ * @param commitmentOrConfig optional default commitment level or optional ConnectionConfig configuration object
1632
1670
  */
1633
- constructor(endpoint: string, commitment?: Commitment): this;
1671
+ constructor(
1672
+ endpoint: string,
1673
+ commitmentOrConfig?: Commitment | ConnectionConfig
1674
+ ): this;
1634
1675
 
1635
1676
  /**
1636
1677
  * The default commitment used for requests
@@ -3367,5 +3408,5 @@ feeCalculator: FeeCalculator,...
3367
3408
  /**
3368
3409
  * There are 1-billion lamports in one SOL
3369
3410
  */
3370
- declare export var LAMPORTS_PER_SOL: any; // 1000000000
3411
+ declare export var LAMPORTS_PER_SOL: 1000000000;
3371
3412
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/web3.js",
3
- "version": "1.5.1",
3
+ "version": "1.6.1",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
package/src/connection.ts CHANGED
@@ -622,21 +622,45 @@ export type PerfSample = {
622
622
  samplePeriodSecs: number;
623
623
  };
624
624
 
625
- function createRpcClient(url: string, useHttps: boolean): RpcClient {
625
+ function createRpcClient(
626
+ url: string,
627
+ useHttps: boolean,
628
+ httpHeaders?: HttpHeaders,
629
+ fetchMiddleware?: FetchMiddleware,
630
+ ): RpcClient {
626
631
  let agentManager: AgentManager | undefined;
627
632
  if (!process.env.BROWSER) {
628
633
  agentManager = new AgentManager(useHttps);
629
634
  }
630
635
 
636
+ let fetchWithMiddleware: (url: string, options: any) => Promise<Response>;
637
+
638
+ if (fetchMiddleware) {
639
+ fetchWithMiddleware = (url: string, options: any) => {
640
+ return new Promise<Response>((resolve, reject) => {
641
+ fetchMiddleware(url, options, async (url: string, options: any) => {
642
+ try {
643
+ resolve(await fetch(url, options));
644
+ } catch (error) {
645
+ reject(error);
646
+ }
647
+ });
648
+ });
649
+ };
650
+ }
651
+
631
652
  const clientBrowser = new RpcClient(async (request, callback) => {
632
653
  const agent = agentManager ? agentManager.requestStart() : undefined;
633
654
  const options = {
634
655
  method: 'POST',
635
656
  body: request,
636
657
  agent,
637
- headers: {
638
- 'Content-Type': 'application/json',
639
- },
658
+ headers: Object.assign(
659
+ {
660
+ 'Content-Type': 'application/json',
661
+ },
662
+ httpHeaders || {},
663
+ ),
640
664
  };
641
665
 
642
666
  try {
@@ -644,7 +668,12 @@ function createRpcClient(url: string, useHttps: boolean): RpcClient {
644
668
  let res: Response;
645
669
  let waitTime = 500;
646
670
  for (;;) {
647
- res = await fetch(url, options);
671
+ if (fetchWithMiddleware) {
672
+ res = await fetchWithMiddleware(url, options);
673
+ } else {
674
+ res = await fetch(url, options);
675
+ }
676
+
648
677
  if (res.status !== 429 /* Too many requests */) {
649
678
  break;
650
679
  }
@@ -1673,6 +1702,32 @@ export type ConfirmedSignatureInfo = {
1673
1702
  blockTime?: number | null;
1674
1703
  };
1675
1704
 
1705
+ /**
1706
+ * An object defining headers to be passed to the RPC server
1707
+ */
1708
+ export type HttpHeaders = {[header: string]: string};
1709
+
1710
+ /**
1711
+ * A callback used to augment the outgoing HTTP request
1712
+ */
1713
+ export type FetchMiddleware = (
1714
+ url: string,
1715
+ options: any,
1716
+ fetch: Function,
1717
+ ) => void;
1718
+
1719
+ /**
1720
+ * Configuration for instantiating a Connection
1721
+ */
1722
+ export type ConnectionConfig = {
1723
+ /** Optional commitment level */
1724
+ commitment?: Commitment;
1725
+ /** Optional HTTP headers object */
1726
+ httpHeaders?: HttpHeaders;
1727
+ /** Optional fetch middleware callback */
1728
+ fetchMiddleware?: FetchMiddleware;
1729
+ };
1730
+
1676
1731
  /**
1677
1732
  * A connection to a fullnode JSON RPC endpoint
1678
1733
  */
@@ -1734,18 +1789,36 @@ export class Connection {
1734
1789
  * Establish a JSON RPC connection
1735
1790
  *
1736
1791
  * @param endpoint URL to the fullnode JSON RPC endpoint
1737
- * @param commitment optional default commitment level
1792
+ * @param commitmentOrConfig optional default commitment level or optional ConnectionConfig configuration object
1738
1793
  */
1739
- constructor(endpoint: string, commitment?: Commitment) {
1794
+ constructor(
1795
+ endpoint: string,
1796
+ commitmentOrConfig?: Commitment | ConnectionConfig,
1797
+ ) {
1740
1798
  this._rpcEndpoint = endpoint;
1741
1799
 
1742
1800
  let url = urlParse(endpoint);
1743
1801
  const useHttps = url.protocol === 'https:';
1744
1802
 
1745
- this._rpcClient = createRpcClient(url.href, useHttps);
1803
+ let httpHeaders;
1804
+ let fetchMiddleware;
1805
+ if (commitmentOrConfig && typeof commitmentOrConfig === 'string') {
1806
+ this._commitment = commitmentOrConfig;
1807
+ } else if (commitmentOrConfig) {
1808
+ this._commitment = commitmentOrConfig.commitment;
1809
+ httpHeaders = commitmentOrConfig.httpHeaders;
1810
+ fetchMiddleware = commitmentOrConfig.fetchMiddleware;
1811
+ }
1812
+
1813
+ this._rpcClient = createRpcClient(
1814
+ url.href,
1815
+ useHttps,
1816
+ httpHeaders,
1817
+ fetchMiddleware,
1818
+ );
1746
1819
  this._rpcRequest = createRpcRequest(this._rpcClient);
1747
1820
  this._rpcBatchRequest = createRpcBatchRequest(this._rpcClient);
1748
- this._commitment = commitment;
1821
+
1749
1822
  this._blockhashInfo = {
1750
1823
  recentBlockhash: null,
1751
1824
  lastFetch: 0,
@@ -1764,6 +1837,7 @@ export class Connection {
1764
1837
  if (url.port !== null) {
1765
1838
  url.port = String(Number(url.port) + 1);
1766
1839
  }
1840
+
1767
1841
  this._rpcWebSocket = new RpcWebSocketClient(urlFormat(url), {
1768
1842
  autoconnect: false,
1769
1843
  max_reconnects: Infinity,