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

Use RpcRequest in createRpcMessage helper #3394

Merged
merged 1 commit into from
Oct 23, 2024
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
7 changes: 7 additions & 0 deletions .changeset/wet-colts-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@solana/rpc-subscriptions-spec': patch
'@solana/rpc-spec-types': patch
'@solana/rpc-spec': patch
---

Use RpcRequest in createRpcMessage helper
11 changes: 6 additions & 5 deletions packages/rpc-spec-types/src/__tests__/rpc-message-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { createRpcMessage } from '../rpc-message';

describe('createRpcMessage', () => {
it('auto-increments ids with each new message', () => {
const { id: firstId } = createRpcMessage('foo', 'bar');
const { id: secondId } = createRpcMessage('foo', 'bar');
const request = { methodName: 'foo', params: 'bar' };
const { id: firstId } = createRpcMessage(request);
const { id: secondId } = createRpcMessage(request);
expect(secondId - firstId).toBe(1);
});
it('returns a well-formed JSON-RPC 2.0 message', () => {
const params = [1, 2, 3];
expect(createRpcMessage('someMethod', params)).toStrictEqual({
const request = { methodName: 'someMethod', params: [1, 2, 3] };
expect(createRpcMessage(request)).toStrictEqual({
id: expect.any(Number),
jsonrpc: '2.0',
method: 'someMethod',
params,
params: [1, 2, 3],
});
});
});
8 changes: 5 additions & 3 deletions packages/rpc-spec-types/src/rpc-message.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { RpcRequest } from './rpc-request';

let _nextMessageId = 0;
function getNextMessageId() {
const id = _nextMessageId;
_nextMessageId = (_nextMessageId + 1) % Number.MAX_SAFE_INTEGER;
return id;
}

export function createRpcMessage<TParams>(method: string, params: TParams) {
export function createRpcMessage<TParams>(request: RpcRequest<TParams>) {
return {
id: getNextMessageId(),
jsonrpc: '2.0',
method,
params,
method: request.methodName,
params: request.params,
};
}
13 changes: 8 additions & 5 deletions packages/rpc-spec/src/__tests__/rpc-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('JSON-RPC 2.0', () => {
api: new Proxy({} as RpcApi<TestRpcMethods>, {
get(_, methodName) {
return (...params: unknown[]) => ({
payload: createRpcMessage(methodName.toString(), params),
payload: createRpcMessage({ methodName: methodName.toString(), params }),
});
},
}),
Expand All @@ -55,7 +55,7 @@ describe('JSON-RPC 2.0', () => {
.send()
.catch(() => {});
expect(makeHttpRequest).toHaveBeenCalledWith({
payload: { ...createRpcMessage('someMethod', [123]), id: expect.any(Number) },
payload: { ...createRpcMessage({ methodName: 'someMethod', params: [123] }), id: expect.any(Number) },
});
});
it('returns results from the transport', async () => {
Expand All @@ -79,7 +79,10 @@ describe('JSON-RPC 2.0', () => {
api: {
someMethod(...params: unknown[]): RpcApiRequestPlan<unknown> {
return {
payload: createRpcMessage('someMethodAugmented', [...params, 'augmented', 'params']),
payload: createRpcMessage({
methodName: 'someMethodAugmented',
params: [...params, 'augmented', 'params'],
}),
};
},
} as RpcApi<TestRpcMethods>,
Expand All @@ -92,7 +95,7 @@ describe('JSON-RPC 2.0', () => {
.catch(() => {});
expect(makeHttpRequest).toHaveBeenCalledWith({
payload: {
...createRpcMessage('someMethodAugmented', [123, 'augmented', 'params']),
...createRpcMessage({ methodName: 'someMethodAugmented', params: [123, 'augmented', 'params'] }),
id: expect.any(Number),
},
});
Expand All @@ -107,7 +110,7 @@ describe('JSON-RPC 2.0', () => {
api: {
someMethod(...params: unknown[]): RpcApiRequestPlan<unknown> {
return {
payload: createRpcMessage('someMethod', params),
payload: createRpcMessage({ methodName: 'someMethod', params }),
responseTransformer,
};
},
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc-spec/src/rpc-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function createJsonRpcApi<TRpcMethods extends RpcApiMethods>(config?: Rpc
const rawRequest = Object.freeze({ methodName, params: rawParams });
const request = config?.requestTransformer ? config?.requestTransformer(rawRequest) : rawRequest;
return Object.freeze({
payload: createRpcMessage(request.methodName, request.params),
payload: createRpcMessage(request),
...(config?.responseTransformer
? {
responseTransformer: (response: RpcResponse) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ export async function executeRpcPubSubSubscriptionPlan<TNotification>({
* must be careful not to send the unsubscribe message until the last subscriber aborts.
*/
if (decrementSubscriberCountAndReturnNewCount(channel, subscriptionId) === 0) {
const unsubscribePayload = createRpcMessage(unsubscribeMethodName, [subscriptionId]);
const unsubscribePayload = createRpcMessage({
methodName: unsubscribeMethodName,
params: [subscriptionId],
});
subscriptionId = undefined;
channel.send(unsubscribePayload).catch(() => {});
}
Expand All @@ -144,7 +147,7 @@ export async function executeRpcPubSubSubscriptionPlan<TNotification>({
* STEP 2
* Send the subscription request.
*/
const subscribePayload = createRpcMessage(subscribeMethodName, subscribeParams);
const subscribePayload = createRpcMessage({ methodName: subscribeMethodName, params: subscribeParams });
await channel.send(subscribePayload);
/**
* STEP 3
Expand Down