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

Add PendingJsonRpcResponse type #43

Merged
merged 2 commits into from
Oct 6, 2022
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
319 changes: 319 additions & 0 deletions src/__fixtures__/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,325 @@ export const JSON_RPC_RESPONSE_FIXTURES = {
],
};

export const JSON_RPC_PENDING_RESPONSE_FIXTURES = {
valid: [
...JSON_RPC_SUCCESS_FIXTURES.valid,
...JSON_RPC_FAILURE_FIXTURES.valid,
{
id: 1,
jsonrpc: '2.0',
},
{
id: 1,
jsonrpc: '2.0',
error: undefined,
},
{
id: 1,
jsonrpc: '2.0',
result: undefined,
},
{
id: 1,
jsonrpc: '2.0',
result: undefined,
error: undefined,
},
{
id: 1,
jsonrpc: '2.0',
result: {
foo: 'bar',
},
error: {
code: -32000,
message: 'Internal error',
},
Comment on lines +698 to +707
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original type in @metamask/types allows this. Please verify that this is intended, as I'm not sure where PendingJsonRpcResponse is used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that this is intended behavior. PendingJsonRpcResponse was created to represent a response object within middleware. The idea here is that while a request is being sent through layers of a middleware stack, we don't know what the final response object will be — we don't know whether it will end up representing a successful or failed response. So PendingJsonRpcResponse is like the Schrodinger's cat of responses. It looks like the possibility that both result and error could be set was acknowledged but accepted as a compromise to prevent from having to typecast the response in middleware: MetaMask/json-rpc-engine#75

},
],
invalid: [
{},
[],
true,
false,
null,
undefined,
1,
'foo',
{
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: {},
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: [],
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: true,
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: false,
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: undefined,
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '1.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: 2.0,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: {},
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: [],
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: true,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: false,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: null,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: undefined,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: [],
},
{
id: 1,
jsonrpc: '2.0',
error: {},
},
{
id: 1,
jsonrpc: '2.0',
error: true,
},
{
id: 1,
jsonrpc: '2.0',
error: false,
},
{
id: 1,
jsonrpc: '2.0',
error: null,
},
{
id: 1,
jsonrpc: '2.0',
error: 'foo',
},
{
id: 1,
jsonrpc: '2.0',
error: 1,
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: {},
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: [],
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: true,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: false,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: null,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: undefined,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: 'foo',
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: {},
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: [],
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: true,
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: false,
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: null,
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: undefined,
},
},
],
};

export const COMPLEX_OBJECT = {
data: {
account: {
Expand Down
47 changes: 47 additions & 0 deletions src/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
JSON_FIXTURES,
JSON_RPC_FAILURE_FIXTURES,
JSON_RPC_NOTIFICATION_FIXTURES,
JSON_RPC_PENDING_RESPONSE_FIXTURES,
JSON_RPC_REQUEST_FIXTURES,
JSON_RPC_RESPONSE_FIXTURES,
JSON_RPC_SUCCESS_FIXTURES,
Expand All @@ -18,12 +19,14 @@ import {
assertIsJsonRpcRequest,
assertIsJsonRpcResponse,
assertIsJsonRpcSuccess,
assertIsPendingJsonRpcResponse,
getJsonRpcIdValidator,
isJsonRpcFailure,
isJsonRpcNotification,
isJsonRpcRequest,
isJsonRpcResponse,
isJsonRpcSuccess,
isPendingJsonRpcResponse,
isValidJson,
validateJsonAndGetSize,
} from '.';
Expand Down Expand Up @@ -254,6 +257,50 @@ describe('json', () => {
});
});

describe('isPendingJsonRpcResponse', () => {
it.each(JSON_RPC_PENDING_RESPONSE_FIXTURES.valid)(
'returns true for a valid pending JSON-RPC response',
(response) => {
expect(isPendingJsonRpcResponse(response)).toBe(true);
},
);

it.each(JSON_RPC_PENDING_RESPONSE_FIXTURES.invalid)(
'returns false for an invalid pending JSON-RPC response',
(response) => {
expect(isPendingJsonRpcResponse(response)).toBe(false);
},
);
});

describe('assertIsPendingJsonRpcResponse', () => {
it.each(JSON_RPC_PENDING_RESPONSE_FIXTURES.valid)(
'does not throw for a valid pending JSON-RPC response',
(response) => {
expect(() => assertIsPendingJsonRpcResponse(response)).not.toThrow();
},
);

it.each(JSON_RPC_PENDING_RESPONSE_FIXTURES.invalid)(
'throws for an invalid pending JSON-RPC response',
(response) => {
expect(() => assertIsPendingJsonRpcResponse(response)).toThrow(
'Not a pending JSON-RPC response',
);
},
);

it('includes the value thrown in the message if it is not an error', () => {
jest.spyOn(superstructModule, 'assert').mockImplementation(() => {
throw 'oops';
});

expect(() =>
assertIsPendingJsonRpcResponse(JSON_RPC_FAILURE_FIXTURES.invalid[0]),
).toThrow('Not a pending JSON-RPC response: oops');
});
});

describe('isJsonRpcResponse', () => {
it.each(JSON_RPC_RESPONSE_FIXTURES.valid)(
'returns true for a valid JSON-RPC response',
Expand Down
Loading