@cosmjs/tendermint-rpc
Advanced tools
Comparing version 0.28.3 to 0.28.4
export { pubkeyToAddress, pubkeyToRawAddress, rawEd25519PubkeyToRawAddress, rawSecp256k1PubkeyToRawAddress, } from "./addresses"; | ||
export { DateTime, fromRfc3339WithNanoseconds, fromSeconds, ReadonlyDateWithNanoseconds, toRfc3339WithNanoseconds, toSeconds, } from "./dates"; | ||
export { HttpEndpoint, } from "./rpcclients"; | ||
export { HttpClient, WebsocketClient } from "./rpcclients"; | ||
@@ -4,0 +5,0 @@ export { AbciInfoRequest, AbciInfoResponse, AbciQueryParams, AbciQueryRequest, AbciQueryResponse, Attribute, Block, BlockchainRequest, BlockchainResponse, BlockGossipParams, BlockId, BlockMeta, BlockParams, BlockRequest, BlockResponse, BlockResultsRequest, BlockResultsResponse, BroadcastTxAsyncResponse, BroadcastTxCommitResponse, broadcastTxCommitSuccess, BroadcastTxParams, BroadcastTxRequest, BroadcastTxSyncResponse, broadcastTxSyncSuccess, Commit, CommitRequest, CommitResponse, ConsensusParams, Event, Evidence, EvidenceParams, GenesisRequest, GenesisResponse, Header, HealthRequest, HealthResponse, Method, NewBlockEvent, NewBlockHeaderEvent, NodeInfo, ProofOp, QueryProof, QueryTag, Request, Response, StatusRequest, StatusResponse, SubscriptionEventType, SyncInfo, TxData, TxEvent, TxParams, TxProof, TxRequest, TxResponse, TxSearchParams, TxSearchRequest, TxSearchResponse, TxSizeParams, Validator, ValidatorsParams, ValidatorsRequest, ValidatorsResponse, Version, Vote, VoteType, } from "./tendermint34"; |
@@ -8,8 +8,22 @@ import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc"; | ||
*/ | ||
export declare function http(method: "POST", url: string, request?: any): Promise<any>; | ||
export declare function http(method: "POST", url: string, headers: Record<string, string> | undefined, request?: any): Promise<any>; | ||
export interface HttpEndpoint { | ||
/** | ||
* The URL of the HTTP endpoint. | ||
* | ||
* For POST APIs like Tendermint RPC in CosmJS, | ||
* this is without the method specific paths (e.g. https://cosmoshub-4--rpc--full.datahub.figment.io/) | ||
*/ | ||
readonly url: string; | ||
/** | ||
* HTTP headers that are sent with every request, such as authorization information. | ||
*/ | ||
readonly headers: Record<string, string>; | ||
} | ||
export declare class HttpClient implements RpcClient { | ||
protected readonly url: string; | ||
constructor(url: string); | ||
protected readonly headers: Record<string, string> | undefined; | ||
constructor(endpoint: string | HttpEndpoint); | ||
disconnect(): void; | ||
execute(request: JsonRpcRequest): Promise<JsonRpcSuccessResponse>; | ||
} |
@@ -22,6 +22,14 @@ "use strict"; | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
async function http(method, url, request) { | ||
async function http(method, url, headers, request) { | ||
if (typeof fetch !== "undefined") { | ||
const body = request ? JSON.stringify(request) : undefined; | ||
return fetch(url, { method: method, body: body }) | ||
const settings = { | ||
method: method, | ||
body: request ? JSON.stringify(request) : undefined, | ||
headers: { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
"Content-Type": "application/json", | ||
...headers, | ||
}, | ||
}; | ||
return fetch(url, settings) | ||
.then(filterBadStatus) | ||
@@ -31,3 +39,5 @@ .then((res) => res.json()); | ||
else { | ||
return axios_1.default.request({ url: url, method: method, data: request }).then((res) => res.data); | ||
return axios_1.default | ||
.request({ url: url, method: method, data: request, headers: headers }) | ||
.then((res) => res.data); | ||
} | ||
@@ -37,5 +47,11 @@ } | ||
class HttpClient { | ||
constructor(url) { | ||
// accept host.name:port and assume http protocol | ||
this.url = (0, rpcclient_1.hasProtocol)(url) ? url : "http://" + url; | ||
constructor(endpoint) { | ||
if (typeof endpoint === "string") { | ||
// accept host.name:port and assume http protocol | ||
this.url = (0, rpcclient_1.hasProtocol)(endpoint) ? endpoint : "http://" + endpoint; | ||
} | ||
else { | ||
this.url = endpoint.url; | ||
this.headers = endpoint.headers; | ||
} | ||
} | ||
@@ -46,3 +62,3 @@ disconnect() { | ||
async execute(request) { | ||
const response = (0, json_rpc_1.parseJsonRpcResponse)(await http("POST", this.url, request)); | ||
const response = (0, json_rpc_1.parseJsonRpcResponse)(await http("POST", this.url, this.headers, request)); | ||
if ((0, json_rpc_1.isJsonRpcErrorResponse)(response)) { | ||
@@ -49,0 +65,0 @@ throw new Error(JSON.stringify(response.error)); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
const jsonrpc_1 = require("../jsonrpc"); | ||
@@ -8,15 +9,50 @@ const testutil_spec_1 = require("../testutil.spec"); | ||
if (!process.env.TENDERMINT_ENABLED) { | ||
pending("Set TENDERMINT_ENABLED to enable tendermint rpc tests"); | ||
pending("Set TENDERMINT_ENABLED to enable Tendermint RPC tests"); | ||
} | ||
} | ||
function pendingWithoutHttpServer() { | ||
if (!process.env.HTTPSERVER_ENABLED) { | ||
pending("Set HTTPSERVER_ENABLED to enable HTTP tests"); | ||
} | ||
} | ||
const tendermintUrl = testutil_spec_1.defaultInstance.url; | ||
const echoUrl = "http://localhost:5555/echo_headers"; | ||
describe("http", () => { | ||
it("can send a health request", async () => { | ||
pendingWithoutTendermint(); | ||
const response = await (0, httpclient_1.http)("POST", `http://${tendermintUrl}`, (0, jsonrpc_1.createJsonRpcRequest)("health")); | ||
const response = await (0, httpclient_1.http)("POST", `http://${tendermintUrl}`, undefined, (0, jsonrpc_1.createJsonRpcRequest)("health")); | ||
expect(response).toEqual(jasmine.objectContaining({ jsonrpc: "2.0" })); | ||
}); | ||
it("errors for non-open port", async () => { | ||
await expectAsync((0, httpclient_1.http)("POST", `http://localhost:56745`, (0, jsonrpc_1.createJsonRpcRequest)("health"))).toBeRejectedWithError(/(ECONNREFUSED|Failed to fetch)/i); | ||
await expectAsync((0, httpclient_1.http)("POST", `http://localhost:56745`, undefined, (0, jsonrpc_1.createJsonRpcRequest)("health"))).toBeRejectedWithError(/(ECONNREFUSED|Failed to fetch)/i); | ||
}); | ||
it("can send custom headers", async () => { | ||
pendingWithoutHttpServer(); | ||
// Without custom headers | ||
const response1 = await (0, httpclient_1.http)("POST", echoUrl, undefined, (0, jsonrpc_1.createJsonRpcRequest)("health")); | ||
expect(response1).toEqual({ | ||
request_headers: jasmine.objectContaining({ | ||
// Basic headers from http client | ||
Accept: jasmine.any(String), | ||
"Content-Length": jasmine.any(String), | ||
"Content-Type": "application/json", | ||
Host: jasmine.any(String), | ||
"User-Agent": jasmine.any(String), | ||
}), | ||
}); | ||
// With custom headers | ||
const response2 = await (0, httpclient_1.http)("POST", echoUrl, { foo: "bar123", Authorization: "Basic Z3Vlc3Q6bm9QYXNzMTIz" }, (0, jsonrpc_1.createJsonRpcRequest)("health")); | ||
expect(response2).toEqual({ | ||
request_headers: jasmine.objectContaining({ | ||
// Basic headers from http client | ||
"Content-Length": jasmine.any(String), | ||
"Content-Type": "application/json", | ||
Host: jasmine.any(String), | ||
"User-Agent": jasmine.any(String), | ||
// Custom headers | ||
foo: "bar123", | ||
Authorization: "Basic Z3Vlc3Q6bm9QYXNzMTIz", | ||
}), | ||
}); | ||
}); | ||
}); | ||
@@ -23,0 +59,0 @@ describe("HttpClient", () => { |
@@ -1,3 +0,3 @@ | ||
export { HttpClient } from "./httpclient"; | ||
export { HttpClient, HttpEndpoint } from "./httpclient"; | ||
export { instanceOfRpcStreamingClient, RpcClient, RpcStreamingClient, SubscriptionEvent } from "./rpcclient"; | ||
export { WebsocketClient } from "./websocketclient"; |
@@ -10,3 +10,3 @@ "use strict"; | ||
if (!process.env.TENDERMINT_ENABLED) { | ||
pending("Set TENDERMINT_ENABLED to enable tendermint rpc tests"); | ||
pending("Set TENDERMINT_ENABLED to enable Tendermint RPC tests"); | ||
} | ||
@@ -13,0 +13,0 @@ } |
@@ -11,3 +11,3 @@ "use strict"; | ||
if (!process.env.TENDERMINT_ENABLED) { | ||
pending("Set TENDERMINT_ENABLED to enable tendermint rpc tests"); | ||
pending("Set TENDERMINT_ENABLED to enable Tendermint RPC tests"); | ||
} | ||
@@ -14,0 +14,0 @@ } |
import { Stream } from "xstream"; | ||
import { RpcClient } from "../rpcclients"; | ||
import { HttpEndpoint, RpcClient } from "../rpcclients"; | ||
import * as requests from "./requests"; | ||
@@ -11,3 +11,3 @@ import * as responses from "./responses"; | ||
*/ | ||
static connect(url: string): Promise<Tendermint34Client>; | ||
static connect(endpoint: string | HttpEndpoint): Promise<Tendermint34Client>; | ||
/** | ||
@@ -14,0 +14,0 @@ * Creates a new Tendermint client given an RPC client. |
@@ -41,6 +41,11 @@ "use strict"; | ||
*/ | ||
static async connect(url) { | ||
const useHttp = url.startsWith("http://") || url.startsWith("https://"); | ||
const rpcClient = useHttp ? new rpcclients_1.HttpClient(url) : new rpcclients_1.WebsocketClient(url); | ||
return Tendermint34Client.create(rpcClient); | ||
static async connect(endpoint) { | ||
if (typeof endpoint === "object") { | ||
return Tendermint34Client.create(new rpcclients_1.HttpClient(endpoint)); | ||
} | ||
else { | ||
const useHttp = endpoint.startsWith("http://") || endpoint.startsWith("https://"); | ||
const rpcClient = useHttp ? new rpcclients_1.HttpClient(endpoint) : new rpcclients_1.WebsocketClient(endpoint); | ||
return Tendermint34Client.create(rpcClient); | ||
} | ||
} | ||
@@ -47,0 +52,0 @@ /** |
{ | ||
"name": "@cosmjs/tendermint-rpc", | ||
"version": "0.28.3", | ||
"version": "0.28.4", | ||
"description": "Tendermint RPC clients", | ||
@@ -45,9 +45,9 @@ "contributors": [ | ||
"dependencies": { | ||
"@cosmjs/crypto": "0.28.3", | ||
"@cosmjs/encoding": "0.28.3", | ||
"@cosmjs/json-rpc": "0.28.3", | ||
"@cosmjs/math": "0.28.3", | ||
"@cosmjs/socket": "0.28.3", | ||
"@cosmjs/stream": "0.28.3", | ||
"@cosmjs/utils": "0.28.3", | ||
"@cosmjs/crypto": "0.28.4", | ||
"@cosmjs/encoding": "0.28.4", | ||
"@cosmjs/json-rpc": "0.28.4", | ||
"@cosmjs/math": "0.28.4", | ||
"@cosmjs/socket": "0.28.4", | ||
"@cosmjs/stream": "0.28.4", | ||
"@cosmjs/utils": "0.28.4", | ||
"axios": "^0.21.2", | ||
@@ -75,4 +75,3 @@ "readonly-date": "^1.0.0", | ||
"glob": "^7.1.6", | ||
"jasmine": "^3.8", | ||
"jasmine-core": "^3.7.1", | ||
"jasmine": "^3.99", | ||
"jasmine-spec-reporter": "^6", | ||
@@ -79,0 +78,0 @@ "karma": "^6.3.14", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
306069
33
4023
6
+ Added@cosmjs/crypto@0.28.4(transitive)
+ Added@cosmjs/encoding@0.28.4(transitive)
+ Added@cosmjs/json-rpc@0.28.4(transitive)
+ Added@cosmjs/math@0.28.4(transitive)
+ Added@cosmjs/socket@0.28.4(transitive)
+ Added@cosmjs/stream@0.28.4(transitive)
+ Added@cosmjs/utils@0.28.4(transitive)
- Removed@cosmjs/crypto@0.28.3(transitive)
- Removed@cosmjs/encoding@0.28.3(transitive)
- Removed@cosmjs/json-rpc@0.28.3(transitive)
- Removed@cosmjs/math@0.28.3(transitive)
- Removed@cosmjs/socket@0.28.3(transitive)
- Removed@cosmjs/stream@0.28.3(transitive)
- Removed@cosmjs/utils@0.28.3(transitive)
Updated@cosmjs/crypto@0.28.4
Updated@cosmjs/encoding@0.28.4
Updated@cosmjs/json-rpc@0.28.4
Updated@cosmjs/math@0.28.4
Updated@cosmjs/socket@0.28.4
Updated@cosmjs/stream@0.28.4
Updated@cosmjs/utils@0.28.4