feat: add custom version HTTP header string to RPC requests

This commit is contained in:
steveluscher 2022-06-28 22:11:17 -07:00 committed by mergify[bot]
parent 265f36c044
commit eb12983785
3 changed files with 20 additions and 5 deletions

View File

@ -47,11 +47,12 @@ impl HttpSender {
/// The URL is an HTTP URL, usually for port 8899. /// The URL is an HTTP URL, usually for port 8899.
pub fn new_with_timeout<U: ToString>(url: U, timeout: Duration) -> Self { pub fn new_with_timeout<U: ToString>(url: U, timeout: Duration) -> Self {
let mut default_headers = header::HeaderMap::new(); let mut default_headers = header::HeaderMap::new();
let user_agent_string =
format!("rust-solana-client/{}", solana_version::Version::default());
default_headers.append( default_headers.append(
header::USER_AGENT, header::HeaderName::from_static("solana-client"),
header::HeaderValue::from_str(user_agent_string.as_str()).unwrap(), header::HeaderValue::from_str(
format!("rust/{}", solana_version::Version::default()).as_str(),
)
.unwrap(),
); );
let client = Arc::new( let client = Arc::new(

View File

@ -71,6 +71,9 @@ function generateConfig(configType, format) {
values: { values: {
'process.env.NODE_ENV': JSON.stringify(env), 'process.env.NODE_ENV': JSON.stringify(env),
'process.env.BROWSER': JSON.stringify(browser), 'process.env.BROWSER': JSON.stringify(browser),
'process.env.npm_package_version': JSON.stringify(
process.env.npm_package_version,
),
}, },
}), }),
], ],

View File

@ -995,6 +995,7 @@ function createRpcClient(
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
httpHeaders || {}, httpHeaders || {},
COMMON_HTTP_HEADERS,
), ),
}; };
@ -2158,7 +2159,12 @@ export type ConfirmedSignatureInfo = {
/** /**
* An object defining headers to be passed to the RPC server * 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 * The type of the JavaScript `fetch()` API
@ -2194,6 +2200,11 @@ export type ConnectionConfig = {
confirmTransactionInitialTimeout?: number; confirmTransactionInitialTimeout?: number;
}; };
/** @internal */
const COMMON_HTTP_HEADERS = {
'solana-client': `js/${process.env.npm_package_version ?? 'UNKNOWN'}`,
};
/** /**
* A connection to a fullnode JSON RPC endpoint * A connection to a fullnode JSON RPC endpoint
*/ */