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

Adjust rpc coder types #5630

Merged
merged 1 commit into from
May 4, 2023
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
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@polkadot/types-known": "10.5.2-4-x",
"@polkadot/util": "^12.1.2",
"@polkadot/util-crypto": "^12.1.2",
"eventemitter3": "^5.0.0",
"eventemitter3": "^5.0.1",
"rxjs": "^7.8.1",
"tslib": "^2.5.0"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
"@polkadot/x-fetch": "^12.1.2",
"@polkadot/x-global": "^12.1.2",
"@polkadot/x-ws": "^12.1.2",
"eventemitter3": "^5.0.0",
"eventemitter3": "^5.0.1",
"mock-socket": "^9.2.1",
"nock": "^13.3.1",
"tslib": "^2.5.0"
},
"devDependencies": {
"@substrate/connect": "0.7.24"
"@substrate/connect": "0.7.25"
},
"optionalDependencies": {
"@substrate/connect": "0.7.24"
"@substrate/connect": "0.7.25"
}
}
18 changes: 9 additions & 9 deletions packages/rpc-provider/src/coder/decodeResponse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,55 @@ describe('decodeResponse', (): void => {

it('expects a non-empty input object', (): void => {
expect(
() => coder.decodeResponse(undefined as unknown as JsonRpcResponse)
() => coder.decodeResponse(undefined as unknown as JsonRpcResponse<unknown>)
).toThrow(/Invalid jsonrpc/);
});

it('expects a valid jsonrpc field', (): void => {
expect(
() => coder.decodeResponse({} as JsonRpcResponse)
() => coder.decodeResponse({} as JsonRpcResponse<unknown>)
).toThrow(/Invalid jsonrpc/);
});

it('expects a valid id field', (): void => {
expect(
() => coder.decodeResponse({ jsonrpc: '2.0' } as JsonRpcResponse)
() => coder.decodeResponse({ jsonrpc: '2.0' } as JsonRpcResponse<unknown>)
).toThrow(/Invalid id/);
});

it('expects a valid result field', (): void => {
expect(
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0' } as JsonRpcResponse)
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0' } as JsonRpcResponse<unknown>)
).toThrow(/No result/);
});

it('throws any error found', (): void => {
expect(
() => coder.decodeResponse({ error: { code: 123, message: 'test error' }, id: 1, jsonrpc: '2.0' } as JsonRpcResponse)
() => coder.decodeResponse({ error: { code: 123, message: 'test error' }, id: 1, jsonrpc: '2.0' } as JsonRpcResponse<unknown>)
).toThrow(/123: test error/);
});

it('throws any error found, with data', (): void => {
expect(
() => coder.decodeResponse({ error: { code: 123, data: 'Error("Some random error description")', message: 'test error' }, id: 1, jsonrpc: '2.0' } as JsonRpcResponse)
() => coder.decodeResponse({ error: { code: 123, data: 'Error("Some random error description")', message: 'test error' }, id: 1, jsonrpc: '2.0' } as JsonRpcResponse<unknown>)
).toThrow(/123: test error: Some random error description/);
});

it('allows for number subscription ids', (): void => {
expect(
coder.decodeResponse({ id: 1, jsonrpc: '2.0', method: 'test', params: { result: 'test result', subscription: 1 } } as JsonRpcResponse)
coder.decodeResponse({ id: 1, jsonrpc: '2.0', method: 'test', params: { result: 'test result', subscription: 1 } } as JsonRpcResponse<unknown>)
).toEqual('test result');
});

it('allows for string subscription ids', (): void => {
expect(
coder.decodeResponse({ id: 1, jsonrpc: '2.0', method: 'test', params: { result: 'test result', subscription: 'abc' } } as JsonRpcResponse)
coder.decodeResponse({ id: 1, jsonrpc: '2.0', method: 'test', params: { result: 'test result', subscription: 'abc' } } as JsonRpcResponse<unknown>)
).toEqual('test result');
});

it('returns the result', (): void => {
expect(
coder.decodeResponse({ id: 1, jsonrpc: '2.0', result: 'some result' } as JsonRpcResponse)
coder.decodeResponse({ id: 1, jsonrpc: '2.0', result: 'some result' } as JsonRpcResponse<unknown>)
).toEqual('some result');
});
});
6 changes: 3 additions & 3 deletions packages/rpc-provider/src/coder/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ function extend<Data, K extends keyof RpcError<Data>> (that: RpcError<Data>, nam
* throw new RpcError('some message', RpcError.CODES.METHOD_NOT_FOUND); // => error.code = -32601
* ```
*/
export default class RpcError<Data = never> extends Error implements RpcErrorInterface<Data> {
export default class RpcError<T = never> extends Error implements RpcErrorInterface<T> {
public code!: number;

public data?: Data;
public data?: T;

public override message!: string;

public override name!: string;

public override stack!: string;

public constructor (message = '', code: number = UNKNOWN, data?: Data) {
public constructor (message = '', code: number = UNKNOWN, data?: T) {
super();

extend(this, 'message', String(message));
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc-provider/src/coder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function checkError (error?: JsonRpcResponseBaseError): void {
export class RpcCoder {
#id = 0;

public decodeResponse (response?: JsonRpcResponse): unknown {
public decodeResponse <T> (response?: JsonRpcResponse<T>): T {
if (!response || response.jsonrpc !== '2.0') {
throw new Error('Invalid jsonrpc field in decoded object');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc-provider/src/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class HttpProvider implements ProviderInterface {

this.#stats.total.bytesRecv += result.length;

const decoded = this.#coder.decodeResponse(JSON.parse(result) as JsonRpcResponse) as T;
const decoded = this.#coder.decodeResponse(JSON.parse(result) as JsonRpcResponse<T>);

this.#stats.active.requests--;

Expand Down
4 changes: 2 additions & 2 deletions packages/rpc-provider/src/substrate-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ export class ScProvider implements ProviderInterface {
return;
}

const response = JSON.parse(hcRes) as JsonRpcResponse;
const response = JSON.parse(hcRes) as JsonRpcResponse<string>;
let decodedResponse: string | Error;

try {
decodedResponse = this.#coder.decodeResponse(response) as string;
decodedResponse = this.#coder.decodeResponse(response);
} catch (e) {
decodedResponse = e as Error;
}
Expand Down
16 changes: 8 additions & 8 deletions packages/rpc-provider/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ export interface JsonRpcResponseBaseError {
message: string;
}

export interface RpcErrorInterface<Data> {
export interface RpcErrorInterface<T> {
code: number;
data?: Data;
data?: T;
message: string;
stack: string;
}

interface JsonRpcResponseSingle {
interface JsonRpcResponseSingle<T> {
error?: JsonRpcResponseBaseError;
result?: unknown;
result: T;
}

interface JsonRpcResponseSubscription {
interface JsonRpcResponseSubscription<T> {
method?: string;
params: {
error?: JsonRpcResponseBaseError;
result: unknown;
result: T;
subscription: number | string;
};
}

export type JsonRpcResponseBase = JsonRpcResponseSingle & JsonRpcResponseSubscription;
export type JsonRpcResponseBase<T> = JsonRpcResponseSingle<T> & JsonRpcResponseSubscription<T>;

export type JsonRpcResponse = JsonRpcObject & JsonRpcResponseBase;
export type JsonRpcResponse<T> = JsonRpcObject & JsonRpcResponseBase<T>;

export type ProviderInterfaceCallback = (error: Error | null, result: any) => void;

Expand Down
10 changes: 5 additions & 5 deletions packages/rpc-provider/src/ws/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class WsProvider implements ProviderInterface {
readonly #handlers: Record<string, WsStateAwaiting> = {};
readonly #isReadyPromise: Promise<WsProvider>;
readonly #stats: ProviderStats;
readonly #waitingForId: Record<string, JsonRpcResponse> = {};
readonly #waitingForId: Record<string, JsonRpcResponse<unknown>> = {};

#autoConnectMs: number;
#endpointIndex: number;
Expand Down Expand Up @@ -489,14 +489,14 @@ export class WsProvider implements ProviderInterface {
this.#endpointStats.bytesRecv += bytesRecv;
this.#stats.total.bytesRecv += bytesRecv;

const response = JSON.parse(message.data) as JsonRpcResponse;
const response = JSON.parse(message.data) as JsonRpcResponse<string>;

return isUndefined(response.method)
? this.#onSocketMessageResult(response)
: this.#onSocketMessageSubscribe(response);
};

#onSocketMessageResult = (response: JsonRpcResponse): void => {
#onSocketMessageResult = (response: JsonRpcResponse<string>): void => {
const handler = this.#handlers[response.id];

if (!handler) {
Expand All @@ -507,7 +507,7 @@ export class WsProvider implements ProviderInterface {

try {
const { method, params, subscription } = handler;
const result = this.#coder.decodeResponse(response) as string;
const result = this.#coder.decodeResponse<string>(response);

// first send the result - in case of subs, we may have an update
// immediately if we have some queued results already
Expand Down Expand Up @@ -536,7 +536,7 @@ export class WsProvider implements ProviderInterface {
delete this.#handlers[response.id];
};

#onSocketMessageSubscribe = (response: JsonRpcResponse): void => {
#onSocketMessageSubscribe = (response: JsonRpcResponse<unknown>): void => {
const method = ALIASES[response.method as string] || response.method || 'invalid';
const subId = `${method}::${response.params.subscription}`;
const handler = this.#subscriptions[subId];
Expand Down
32 changes: 16 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ __metadata:
"@polkadot/types-support": 10.5.2-4-x
"@polkadot/util": ^12.1.2
"@polkadot/util-crypto": ^12.1.2
eventemitter3: ^5.0.0
eventemitter3: ^5.0.1
rxjs: ^7.8.1
tslib: ^2.5.0
languageName: unknown
Expand Down Expand Up @@ -610,8 +610,8 @@ __metadata:
"@polkadot/x-fetch": ^12.1.2
"@polkadot/x-global": ^12.1.2
"@polkadot/x-ws": ^12.1.2
"@substrate/connect": 0.7.24
eventemitter3: ^5.0.0
"@substrate/connect": 0.7.25
eventemitter3: ^5.0.1
mock-socket: ^9.2.1
nock: ^13.3.1
tslib: ^2.5.0
Expand Down Expand Up @@ -1058,14 +1058,14 @@ __metadata:
languageName: node
linkType: hard

"@substrate/connect@npm:0.7.24":
version: 0.7.24
resolution: "@substrate/connect@npm:0.7.24"
"@substrate/connect@npm:0.7.25":
version: 0.7.25
resolution: "@substrate/connect@npm:0.7.25"
dependencies:
"@substrate/connect-extension-protocol": ^1.0.1
eventemitter3: ^4.0.7
smoldot: 1.0.2
checksum: ffede7dd5cdc7512f5a6f97d4ff2655dc294c78d21856aa9d215e39b84c623ebfda3557d970652eba7c4ac4e02eb765cf7140b17345aface1f700af200e0bf2d
smoldot: 1.0.3
checksum: d480216988db28acdb2da9afa4c1fc568c780fc6953de68cb38bb9081fa994bad3756e26c854e5a4e7be4faaf59a134396fb710ebaa31b478d06503ded0117b8
languageName: node
linkType: hard

Expand Down Expand Up @@ -3848,10 +3848,10 @@ __metadata:
languageName: node
linkType: hard

"eventemitter3@npm:^5.0.0":
version: 5.0.0
resolution: "eventemitter3@npm:5.0.0"
checksum: b974bafbab860e0a5bbb21add4c4e82f9d5691c583c03f2e4c5d44a2d6c4556d79223621bdcfc6c8e14366a4af9df6b5ea9d6caf65fbffc80b66f3e1dceacbc9
"eventemitter3@npm:^5.0.1":
version: 5.0.1
resolution: "eventemitter3@npm:5.0.1"
checksum: 543d6c858ab699303c3c32e0f0f47fc64d360bf73c3daf0ac0b5079710e340d6fe9f15487f94e66c629f5f82cd1a8678d692f3dbb6f6fcd1190e1b97fcad36f8
languageName: node
linkType: hard

Expand Down Expand Up @@ -7717,13 +7717,13 @@ fsevents@~2.3.2:
languageName: node
linkType: hard

"smoldot@npm:1.0.2":
version: 1.0.2
resolution: "smoldot@npm:1.0.2"
"smoldot@npm:1.0.3":
version: 1.0.3
resolution: "smoldot@npm:1.0.3"
dependencies:
pako: ^2.0.4
ws: ^8.8.1
checksum: 2e069c49e520e72c63d16d94c51a074a6f90ae598a2299afbf5b90c3a0ba607e1640bf51d8e2a3b06dc7843e571c96b5838209a1d9c9d0dae66c8693fa0369c9
checksum: 70f12f23fac3d5eabd1300fccaa5e3a34e36fa8e40a21a138db63075fb881b3208f5d5d3fc1b59a082670b4d3036abc90c23858ed6457a59574b8fb7ef4987de
languageName: node
linkType: hard

Expand Down