Skip to content
Open
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
1 change: 1 addition & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/json-rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"dependencies": {
"@cosmjs/stream": "workspace:^",
"@cosmjs/utils": "workspace:^",
"xstream": "^11.14.0"
},
"devDependencies": {
Expand Down
125 changes: 0 additions & 125 deletions packages/json-rpc/src/compatibility.spec.ts

This file was deleted.

27 changes: 11 additions & 16 deletions packages/json-rpc/src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
isJsonCompatibleArray,
isJsonCompatibleDictionary,
isJsonCompatibleValue,
JsonCompatibleDictionary,
JsonCompatibleValue,
} from "./compatibility";
import { type JsonObject, type JsonValue, isJsonArray, isJsonObject, isJsonValue } from "@cosmjs/utils";

import {
JsonRpcError,
JsonRpcErrorResponse,
Expand All @@ -20,7 +15,7 @@ import {
* Returns `null` when no valid ID was found.
*/
export function parseJsonRpcId(data: unknown): JsonRpcId | null {
if (!isJsonCompatibleDictionary(data)) {
if (!isJsonObject(data)) {
throw new Error("Data must be JSON compatible dictionary");
}

Expand All @@ -32,7 +27,7 @@ export function parseJsonRpcId(data: unknown): JsonRpcId | null {
}

export function parseJsonRpcRequest(data: unknown): JsonRpcRequest {
if (!isJsonCompatibleDictionary(data)) {
if (!isJsonObject(data)) {
throw new Error("Data must be JSON compatible dictionary");
}

Expand All @@ -53,7 +48,7 @@ export function parseJsonRpcRequest(data: unknown): JsonRpcRequest {
throw new Error(`Invalid "method" field. Must be a string.`);
}

if (!isJsonCompatibleArray(data.params) && !isJsonCompatibleDictionary(data.params)) {
if (!isJsonArray(data.params) && !isJsonObject(data.params)) {
throw new Error("Invalid params field");
}

Expand All @@ -65,7 +60,7 @@ export function parseJsonRpcRequest(data: unknown): JsonRpcRequest {
};
}

function parseError(error: JsonCompatibleDictionary): JsonRpcError {
function parseError(error: JsonObject): JsonRpcError {
if (typeof error.code !== "number") {
throw new Error("Error property 'code' is not a number");
}
Expand All @@ -74,11 +69,11 @@ function parseError(error: JsonCompatibleDictionary): JsonRpcError {
throw new Error("Error property 'message' is not a string");
}

let maybeUndefinedData: JsonCompatibleValue | undefined;
let maybeUndefinedData: JsonValue | undefined;

if (error.data === undefined) {
maybeUndefinedData = undefined;
} else if (isJsonCompatibleValue(error.data)) {
} else if (isJsonValue(error.data)) {
maybeUndefinedData = error.data;
} else {
throw new Error("Error property 'data' is defined but not a JSON compatible value.");
Expand All @@ -93,7 +88,7 @@ function parseError(error: JsonCompatibleDictionary): JsonRpcError {

/** Throws if data is not a JsonRpcErrorResponse */
export function parseJsonRpcErrorResponse(data: unknown): JsonRpcErrorResponse {
if (!isJsonCompatibleDictionary(data)) {
if (!isJsonObject(data)) {
throw new Error("Data must be JSON compatible dictionary");
}

Expand All @@ -106,7 +101,7 @@ export function parseJsonRpcErrorResponse(data: unknown): JsonRpcErrorResponse {
throw new Error("Invalid id field");
}

if (typeof data.error === "undefined" || !isJsonCompatibleDictionary(data.error)) {
if (typeof data.error === "undefined" || !isJsonObject(data.error)) {
throw new Error("Invalid error field");
}

Expand All @@ -119,7 +114,7 @@ export function parseJsonRpcErrorResponse(data: unknown): JsonRpcErrorResponse {

/** Throws if data is not a JsonRpcSuccessResponse */
export function parseJsonRpcSuccessResponse(data: unknown): JsonRpcSuccessResponse {
if (!isJsonCompatibleDictionary(data)) {
if (!isJsonObject(data)) {
throw new Error("Data must be JSON compatible dictionary");
}

Expand Down
6 changes: 3 additions & 3 deletions packages/json-rpc/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { JsonCompatibleArray, JsonCompatibleDictionary, JsonCompatibleValue } from "./compatibility";
import type { JsonArray, JsonObject, JsonValue } from "@cosmjs/utils";

export type JsonRpcId = number | string;

export interface JsonRpcRequest {
readonly jsonrpc: "2.0";
readonly id: JsonRpcId;
readonly method: string;
readonly params: JsonCompatibleArray | JsonCompatibleDictionary;
readonly params: JsonArray | JsonObject;
}

export interface JsonRpcSuccessResponse {
Expand All @@ -18,7 +18,7 @@ export interface JsonRpcSuccessResponse {
export interface JsonRpcError {
readonly code: number;
readonly message: string;
readonly data?: JsonCompatibleValue;
readonly data?: JsonValue;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions packages/json-rpc/src/workers/dummyservice.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

// for testing only

import { isJsonCompatibleDictionary } from "../compatibility";
import { isJsonObject } from "@cosmjs/utils";

import { parseJsonRpcId, parseJsonRpcRequest } from "../parse";
import {
jsonRpcCode,
Expand Down Expand Up @@ -31,7 +32,7 @@ function handleRequest(event: MessageEvent): JsonRpcResponse {

// This is just a text representation of the request. It can be lossy as it is not needed for further processing.
let paramsString: string;
if (isJsonCompatibleDictionary(request.params)) {
if (isJsonObject(request.params)) {
paramsString = JSON.stringify(request.params);
} else {
paramsString = request.params
Expand All @@ -44,7 +45,7 @@ function handleRequest(event: MessageEvent): JsonRpcResponse {
return `"${p}"`;
} else {
// Nested arrays or dictionaries. No need to traverse.
return isJsonCompatibleDictionary(p) ? "{ … }" : "[ … ]";
return isJsonObject(p) ? "{ … }" : "[ … ]";
}
})
.join(", ");
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { arrayContentEquals, arrayContentStartsWith } from "./arrays";
export { assert, assertDefined, assertDefinedAndNotNull } from "./assert";
export type { JsonArray, JsonObject, JsonValue } from "./json";
export { isJsonArray, isJsonObject, isJsonValue } from "./json";
export { sleep } from "./sleep";
export { isDefined, isNonNullObject, isUint8Array } from "./typechecks";
Loading
Loading