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

Commit

Permalink
The default RPC transport now requests wire compression in Node
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher committed Oct 6, 2024
1 parent 386496e commit 1cf75f3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-olives-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solana/rpc': patch
---

Enabled Brotli, gzip, and Deflate compression by default when running in Node.js, where compression headers are not added by the runtime like they are in browsers
25 changes: 25 additions & 0 deletions packages/rpc/src/__tests__/rpc-transport-header-config-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ import { createDefaultRpcTransport } from '../rpc-transport';
jest.mock('@solana/rpc-transport-http');

describe('createDefaultRpcTransport', () => {
if (__NODEJS__) {
it('adds default compression headers', () => {
createDefaultRpcTransport({ url: 'fake://url' });
expect(createHttpTransportForSolanaRpc).toHaveBeenCalledWith(
expect.objectContaining({
headers: expect.objectContaining({
'accept-encoding': 'br,gzip,deflate',
}),
}),
);
});
it('allows the override of the default compression headers', () => {
createDefaultRpcTransport({
headers: { 'aCcEpT-eNcOdInG': 'zstd' },
url: 'fake://url',
});
expect(createHttpTransportForSolanaRpc).toHaveBeenCalledWith(
expect.objectContaining({
headers: expect.objectContaining({
'accept-encoding': 'zstd',
}),
}),
);
});
}
it('adds configured headers to each request with downcased property names', () => {
createDefaultRpcTransport({
headers: { aUtHoRiZaTiOn: 'Picard, 4 7 Alpha Tango' },
Expand Down
7 changes: 7 additions & 0 deletions packages/rpc/src/rpc-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export function createDefaultRpcTransport<TClusterUrl extends ClusterUrl>(
createHttpTransportForSolanaRpc({
...config,
headers: {
...(__NODEJS__ &&
({
// Keep these headers lowercase so they will be overriden by any user-supplied headers below.
'accept-encoding':
// Natively supported by Node LTS v20.18.0 and above.
'br,gzip,deflate', // Brotli, gzip, and Deflate, in that order.
} as { [overrideHeader: string]: string })),
...(config.headers ? normalizeHeaders(config.headers) : undefined),
...({
// Keep these headers lowercase so they will override any user-supplied headers above.
Expand Down

0 comments on commit 1cf75f3

Please sign in to comment.