Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(experimental): create a deduplication key getter specifically for subscriptions #1625

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion packages/library/src/__tests__/rpc-request-deduplication-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { getSolanaRpcPayloadDeduplicationKey } from '../rpc-request-deduplication';
import {
getSolanaRpcPayloadDeduplicationKey,
getSolanaRpcSubscriptionPayloadDeduplicationKey,
} from '../rpc-request-deduplication';

describe('getSolanaRpcPayloadDeduplicationKey', () => {
it('produces no key for undefined payloads', () => {
Expand Down Expand Up @@ -52,3 +55,66 @@ describe('getSolanaRpcPayloadDeduplicationKey', () => {
);
});
});

describe('getSolanaRpcSubscriptionPayloadDeduplicationKey', () => {
it('produces no key for undefined payloads', () => {
expect(getSolanaRpcSubscriptionPayloadDeduplicationKey(undefined)).toBeUndefined();
});
it('produces no key for null payloads', () => {
expect(getSolanaRpcSubscriptionPayloadDeduplicationKey(null)).toBeUndefined();
});
it('produces no key for array payloads', () => {
expect(getSolanaRpcSubscriptionPayloadDeduplicationKey([])).toBeUndefined();
});
it('produces no key for string payloads', () => {
expect(getSolanaRpcSubscriptionPayloadDeduplicationKey('o hai')).toBeUndefined();
});
it('produces no key for numeric payloads', () => {
expect(getSolanaRpcSubscriptionPayloadDeduplicationKey(123)).toBeUndefined();
});
it('produces no key for bigint payloads', () => {
expect(getSolanaRpcSubscriptionPayloadDeduplicationKey(123n)).toBeUndefined();
});
it('produces no key for object payloads that are not JSON-RPC payloads', () => {
expect(getSolanaRpcSubscriptionPayloadDeduplicationKey({})).toBeUndefined();
});
it("produces a key for a JSON-RPC payload whose method ends in 'Subscribe'", () => {
expect(
getSolanaRpcSubscriptionPayloadDeduplicationKey({
id: 1,
jsonrpc: '2.0',
method: 'fooSubscribe',
params: 'foo',
})
).toMatchInlineSnapshot(`"["fooSubscribe","foo"]"`);
});
it("produces no key for a JSON-RPC payload whose method does not end in 'Subscribe'", () => {
expect(
getSolanaRpcSubscriptionPayloadDeduplicationKey({
id: 1,
jsonrpc: '2.0',
method: 'getFoo',
params: 'foo',
})
).toBeUndefined();
});
it('produces identical keys for two materially identical JSON-RPC payloads', () => {
expect(
getSolanaRpcSubscriptionPayloadDeduplicationKey({
id: 1,
jsonrpc: '2.0',
method: 'fooSubscribe',
params: { a: 1, b: { c: [2, 3], d: 4 } },
})
).toEqual(
/* eslint-disable sort-keys-fix/sort-keys-fix */
getSolanaRpcSubscriptionPayloadDeduplicationKey({
jsonrpc: '2.0',
method: 'fooSubscribe',
params: { b: { d: 4, c: [2, 3] }, a: 1 },
id: 2,
})
/* eslint-enable sort-keys-fix/sort-keys-fix */
);
});
});
24 changes: 19 additions & 5 deletions packages/library/src/rpc-request-deduplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@
// @ts-ignore
import fastStableStringify from 'fast-stable-stringify';

export function getSolanaRpcPayloadDeduplicationKey(payload: unknown): string | undefined {
function isJsonRpcPayload(payload: unknown): payload is Readonly<{ method: string; params: unknown }> {
if (payload == null || typeof payload !== 'object' || Array.isArray(payload)) {
return;
}
if ('jsonrpc' in payload && payload.jsonrpc === '2.0' && 'method' in payload && 'params' in payload) {
return fastStableStringify([payload.method, payload.params]);
return false;
}
return (
'jsonrpc' in payload &&
payload.jsonrpc === '2.0' &&
'method' in payload &&
typeof payload.method === 'string' &&
'params' in payload
);
}

export function getSolanaRpcPayloadDeduplicationKey(payload: unknown): string | undefined {
return isJsonRpcPayload(payload) ? fastStableStringify([payload.method, payload.params]) : undefined;
}

export function getSolanaRpcSubscriptionPayloadDeduplicationKey(payload: unknown): string | undefined {
return isJsonRpcPayload(payload) && payload.method.endsWith('Subscribe')
? fastStableStringify([payload.method, payload.params])
: undefined;
}