@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/lib/index.cjs.js CHANGED
@@ -2488,22 +2488,38 @@ const SimulatedTransactionResponseStruct = jsonRpcResultAndContext(superstruct.t
2488
2488
  logs: superstruct.nullable(superstruct.array(superstruct.string()))
2489
2489
  }));
2490
2490
 
2491
- function createRpcClient(url, useHttps) {
2491
+ function createRpcClient(url, useHttps, httpHeaders, fetchMiddleware) {
2492
2492
  let agentManager;
2493
2493
 
2494
2494
  {
2495
2495
  agentManager = new AgentManager(useHttps);
2496
2496
  }
2497
2497
 
2498
+ let fetchWithMiddleware;
2499
+
2500
+ if (fetchMiddleware) {
2501
+ fetchWithMiddleware = (url, options) => {
2502
+ return new Promise((resolve, reject) => {
2503
+ fetchMiddleware(url, options, async (url, options) => {
2504
+ try {
2505
+ resolve(await fetch__default['default'](url, options));
2506
+ } catch (error) {
2507
+ reject(error);
2508
+ }
2509
+ });
2510
+ });
2511
+ };
2512
+ }
2513
+
2498
2514
  const clientBrowser = new RpcClient__default['default'](async (request, callback) => {
2499
2515
  const agent = agentManager ? agentManager.requestStart() : undefined;
2500
2516
  const options = {
2501
2517
  method: 'POST',
2502
2518
  body: request,
2503
2519
  agent,
2504
- headers: {
2520
+ headers: Object.assign({
2505
2521
  'Content-Type': 'application/json'
2506
- }
2522
+ }, httpHeaders || {})
2507
2523
  };
2508
2524
 
2509
2525
  try {
@@ -2512,7 +2528,11 @@ function createRpcClient(url, useHttps) {
2512
2528
  let waitTime = 500;
2513
2529
 
2514
2530
  for (;;) {
2515
- res = await fetch__default['default'](url, options);
2531
+ if (fetchWithMiddleware) {
2532
+ res = await fetchWithMiddleware(url, options);
2533
+ } else {
2534
+ res = await fetch__default['default'](url, options);
2535
+ }
2516
2536
 
2517
2537
  if (res.status !== 429
2518
2538
  /* Too many requests */
@@ -3140,9 +3160,9 @@ class Connection {
3140
3160
  * Establish a JSON RPC connection
3141
3161
  *
3142
3162
  * @param endpoint URL to the fullnode JSON RPC endpoint
3143
- * @param commitment optional default commitment level
3163
+ * @param commitmentOrConfig optional default commitment level or optional ConnectionConfig configuration object
3144
3164
  */
3145
- constructor(endpoint, commitment) {
3165
+ constructor(endpoint, commitmentOrConfig) {
3146
3166
  _defineProperty__default['default'](this, "_commitment", void 0);
3147
3167
 
3148
3168
  _defineProperty__default['default'](this, "_rpcEndpoint", void 0);
@@ -3194,10 +3214,20 @@ class Connection {
3194
3214
  this._rpcEndpoint = endpoint;
3195
3215
  let url$1 = url.parse(endpoint);
3196
3216
  const useHttps = url$1.protocol === 'https:';
3197
- this._rpcClient = createRpcClient(url$1.href, useHttps);
3217
+ let httpHeaders;
3218
+ let fetchMiddleware;
3219
+
3220
+ if (commitmentOrConfig && typeof commitmentOrConfig === 'string') {
3221
+ this._commitment = commitmentOrConfig;
3222
+ } else if (commitmentOrConfig) {
3223
+ this._commitment = commitmentOrConfig.commitment;
3224
+ httpHeaders = commitmentOrConfig.httpHeaders;
3225
+ fetchMiddleware = commitmentOrConfig.fetchMiddleware;
3226
+ }
3227
+
3228
+ this._rpcClient = createRpcClient(url$1.href, useHttps, httpHeaders, fetchMiddleware);
3198
3229
  this._rpcRequest = createRpcRequest(this._rpcClient);
3199
3230
  this._rpcBatchRequest = createRpcBatchRequest(this._rpcClient);
3200
- this._commitment = commitment;
3201
3231
  this._blockhashInfo = {
3202
3232
  recentBlockhash: null,
3203
3233
  lastFetch: 0,