Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
feat: add custom version HTTP header string to RPC requests
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher authored and mergify[bot] committed Jun 29, 2022
1 parent 265f36c commit eb12983
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 5 additions & 4 deletions client/src/http_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ impl HttpSender {
/// The URL is an HTTP URL, usually for port 8899.
pub fn new_with_timeout<U: ToString>(url: U, timeout: Duration) -> Self {
let mut default_headers = header::HeaderMap::new();
let user_agent_string =
format!("rust-solana-client/{}", solana_version::Version::default());
default_headers.append(
header::USER_AGENT,
header::HeaderValue::from_str(user_agent_string.as_str()).unwrap(),
header::HeaderName::from_static("solana-client"),
header::HeaderValue::from_str(
format!("rust/{}", solana_version::Version::default()).as_str(),
)
.unwrap(),
);

let client = Arc::new(
Expand Down
3 changes: 3 additions & 0 deletions web3.js/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ function generateConfig(configType, format) {
values: {
'process.env.NODE_ENV': JSON.stringify(env),
'process.env.BROWSER': JSON.stringify(browser),
'process.env.npm_package_version': JSON.stringify(
process.env.npm_package_version,
),
},
}),
],
Expand Down
13 changes: 12 additions & 1 deletion web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ function createRpcClient(
'Content-Type': 'application/json',
},
httpHeaders || {},
COMMON_HTTP_HEADERS,
),
};

Expand Down Expand Up @@ -2158,7 +2159,12 @@ export type ConfirmedSignatureInfo = {
/**
* An object defining headers to be passed to the RPC server
*/
export type HttpHeaders = {[header: string]: string};
export type HttpHeaders = {
[header: string]: string;
} & {
// Prohibited headers; for internal use only.
'solana-client'?: never;
};

/**
* The type of the JavaScript `fetch()` API
Expand Down Expand Up @@ -2194,6 +2200,11 @@ export type ConnectionConfig = {
confirmTransactionInitialTimeout?: number;
};

/** @internal */
const COMMON_HTTP_HEADERS = {
'solana-client': `js/${process.env.npm_package_version ?? 'UNKNOWN'}`,
};

/**
* A connection to a fullnode JSON RPC endpoint
*/
Expand Down

0 comments on commit eb12983

Please sign in to comment.