Skip to content

Commit

Permalink
refactor(experimental): create a deduplication key getter specificall…
Browse files Browse the repository at this point in the history
…y for subscriptions
  • Loading branch information
steveluscher committed Sep 23, 2023
1 parent 2f518cc commit a638dfd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
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;
}

0 comments on commit a638dfd

Please sign in to comment.