@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.d.ts CHANGED
@@ -1041,6 +1041,31 @@ declare module '@solana/web3.js' {
1041
1041
  /** The unix timestamp of when the transaction was processed */
1042
1042
  blockTime?: number | null;
1043
1043
  };
1044
+ /**
1045
+ * An object defining headers to be passed to the RPC server
1046
+ */
1047
+ export type HttpHeaders = {
1048
+ [header: string]: string;
1049
+ };
1050
+ /**
1051
+ * A callback used to augment the outgoing HTTP request
1052
+ */
1053
+ export type FetchMiddleware = (
1054
+ url: string,
1055
+ options: any,
1056
+ fetch: Function,
1057
+ ) => void;
1058
+ /**
1059
+ * Configuration for instantiating a Connection
1060
+ */
1061
+ export type ConnectionConfig = {
1062
+ /** Optional commitment level */
1063
+ commitment?: Commitment;
1064
+ /** Optional HTTP headers object */
1065
+ httpHeaders?: HttpHeaders;
1066
+ /** Optional fetch middleware callback */
1067
+ fetchMiddleware?: FetchMiddleware;
1068
+ };
1044
1069
  /**
1045
1070
  * A connection to a fullnode JSON RPC endpoint
1046
1071
  */
@@ -1049,9 +1074,12 @@ declare module '@solana/web3.js' {
1049
1074
  * Establish a JSON RPC connection
1050
1075
  *
1051
1076
  * @param endpoint URL to the fullnode JSON RPC endpoint
1052
- * @param commitment optional default commitment level
1077
+ * @param commitmentOrConfig optional default commitment level or optional ConnectionConfig configuration object
1053
1078
  */
1054
- constructor(endpoint: string, commitment?: Commitment);
1079
+ constructor(
1080
+ endpoint: string,
1081
+ commitmentOrConfig?: Commitment | ConnectionConfig,
1082
+ );
1055
1083
  /**
1056
1084
  * The default commitment used for requests
1057
1085
  */
package/lib/index.esm.js CHANGED
@@ -2450,22 +2450,38 @@ const SimulatedTransactionResponseStruct = jsonRpcResultAndContext(type({
2450
2450
  logs: nullable(array(string()))
2451
2451
  }));
2452
2452
 
2453
- function createRpcClient(url, useHttps) {
2453
+ function createRpcClient(url, useHttps, httpHeaders, fetchMiddleware) {
2454
2454
  let agentManager;
2455
2455
 
2456
2456
  {
2457
2457
  agentManager = new AgentManager(useHttps);
2458
2458
  }
2459
2459
 
2460
+ let fetchWithMiddleware;
2461
+
2462
+ if (fetchMiddleware) {
2463
+ fetchWithMiddleware = (url, options) => {
2464
+ return new Promise((resolve, reject) => {
2465
+ fetchMiddleware(url, options, async (url, options) => {
2466
+ try {
2467
+ resolve(await fetch(url, options));
2468
+ } catch (error) {
2469
+ reject(error);
2470
+ }
2471
+ });
2472
+ });
2473
+ };
2474
+ }
2475
+
2460
2476
  const clientBrowser = new RpcClient(async (request, callback) => {
2461
2477
  const agent = agentManager ? agentManager.requestStart() : undefined;
2462
2478
  const options = {
2463
2479
  method: 'POST',
2464
2480
  body: request,
2465
2481
  agent,
2466
- headers: {
2482
+ headers: Object.assign({
2467
2483
  'Content-Type': 'application/json'
2468
- }
2484
+ }, httpHeaders || {})
2469
2485
  };
2470
2486
 
2471
2487
  try {
@@ -2474,7 +2490,11 @@ function createRpcClient(url, useHttps) {
2474
2490
  let waitTime = 500;
2475
2491
 
2476
2492
  for (;;) {
2477
- res = await fetch(url, options);
2493
+ if (fetchWithMiddleware) {
2494
+ res = await fetchWithMiddleware(url, options);
2495
+ } else {
2496
+ res = await fetch(url, options);
2497
+ }
2478
2498
 
2479
2499
  if (res.status !== 429
2480
2500
  /* Too many requests */
@@ -3102,9 +3122,9 @@ class Connection {
3102
3122
  * Establish a JSON RPC connection
3103
3123
  *
3104
3124
  * @param endpoint URL to the fullnode JSON RPC endpoint
3105
- * @param commitment optional default commitment level
3125
+ * @param commitmentOrConfig optional default commitment level or optional ConnectionConfig configuration object
3106
3126
  */
3107
- constructor(endpoint, commitment) {
3127
+ constructor(endpoint, commitmentOrConfig) {
3108
3128
  _defineProperty(this, "_commitment", void 0);
3109
3129
 
3110
3130
  _defineProperty(this, "_rpcEndpoint", void 0);
@@ -3156,10 +3176,20 @@ class Connection {
3156
3176
  this._rpcEndpoint = endpoint;
3157
3177
  let url = parse(endpoint);
3158
3178
  const useHttps = url.protocol === 'https:';
3159
- this._rpcClient = createRpcClient(url.href, useHttps);
3179
+ let httpHeaders;
3180
+ let fetchMiddleware;
3181
+
3182
+ if (commitmentOrConfig && typeof commitmentOrConfig === 'string') {
3183
+ this._commitment = commitmentOrConfig;
3184
+ } else if (commitmentOrConfig) {
3185
+ this._commitment = commitmentOrConfig.commitment;
3186
+ httpHeaders = commitmentOrConfig.httpHeaders;
3187
+ fetchMiddleware = commitmentOrConfig.fetchMiddleware;
3188
+ }
3189
+
3190
+ this._rpcClient = createRpcClient(url.href, useHttps, httpHeaders, fetchMiddleware);
3160
3191
  this._rpcRequest = createRpcRequest(this._rpcClient);
3161
3192
  this._rpcBatchRequest = createRpcBatchRequest(this._rpcClient);
3162
- this._commitment = commitment;
3163
3193
  this._blockhashInfo = {
3164
3194
  recentBlockhash: null,
3165
3195
  lastFetch: 0,