Require protocol to be set in endpoint URLs

This commit is contained in:
Simon Warta 2023-12-19 13:54:16 +01:00
parent 8ab4b7b7cc
commit b0ccb04dac
12 changed files with 41 additions and 48 deletions

View File

@ -13,6 +13,14 @@ and this project adheres to
[#1522]: https://github.com/cosmos/cosmjs/pull/1522
### Changed
- @cosmjs/tendermint-rpc: Require protocol to be set in endpoint URLs (https://,
http://, wss:// or ws://). Otherwise an error is raised instead of falling
back to ws://. ([#1527])
[#1527]: https://github.com/cosmos/cosmjs/pull/1527
## [0.32.1] - 2023-12-04
### Fixed

View File

@ -885,14 +885,6 @@ describe("Comet38Client", () => {
it("can connect to a given url", async () => {
pendingWithoutTendermint();
// default connection
{
const client = await Comet38Client.connect(url);
const info = await client.abciInfo();
expect(info).toBeTruthy();
client.disconnect();
}
// http connection
{
const client = await Comet38Client.connect("http://" + url);
@ -911,13 +903,13 @@ describe("Comet38Client", () => {
});
describe("With HttpClient", () => {
defaultTestSuite(() => new HttpClient(url), expected);
defaultTestSuite(() => new HttpClient("http://" + url), expected);
});
describe("With WebsocketClient", () => {
// don't print out WebSocket errors if marked pending
const onError = process.env.TENDERMINT_ENABLED ? console.error : () => 0;
const factory = (): WebsocketClient => new WebsocketClient(url, onError);
const factory = (): WebsocketClient => new WebsocketClient("ws://" + url, onError);
defaultTestSuite(factory, expected);
websocketTestSuite(factory, expected);
});

View File

@ -9,9 +9,9 @@ function pendingWithoutTendermint(): void {
}
}
const tendermintUrl = defaultInstance.url;
describe("HttpBatchClient", () => {
const tendermintUrl = "http://" + defaultInstance.url;
it("can make a simple call", async () => {
pendingWithoutTendermint();
const client = new HttpBatchClient(tendermintUrl);

View File

@ -42,8 +42,10 @@ export class HttpBatchClient implements RpcClient {
dispatchInterval: options.dispatchInterval ?? defaultHttpBatchClientOptions.dispatchInterval,
};
if (typeof endpoint === "string") {
// accept host.name:port and assume http protocol
this.url = hasProtocol(endpoint) ? endpoint : "http://" + endpoint;
if (!hasProtocol(endpoint)) {
throw new Error("Endpoint URL is missing a protocol. Expected 'https://' or 'http://'.");
}
this.url = endpoint;
} else {
this.url = endpoint.url;
this.headers = endpoint.headers;

View File

@ -8,9 +8,9 @@ function pendingWithoutTendermint(): void {
}
}
const tendermintUrl = defaultInstance.url;
describe("HttpClient", () => {
const tendermintUrl = "http://" + defaultInstance.url;
it("can make a simple call", async () => {
pendingWithoutTendermint();
const client = new HttpClient(tendermintUrl);

View File

@ -28,8 +28,10 @@ export class HttpClient implements RpcClient {
public constructor(endpoint: string | HttpEndpoint) {
if (typeof endpoint === "string") {
// accept host.name:port and assume http protocol
this.url = hasProtocol(endpoint) ? endpoint : "http://" + endpoint;
if (!hasProtocol(endpoint)) {
throw new Error("Endpoint URL is missing a protocol. Expected 'https://' or 'http://'.");
}
this.url = endpoint;
} else {
this.url = endpoint.url;
this.headers = endpoint.headers;

View File

@ -1,7 +1,7 @@
import { createJsonRpcRequest } from "../jsonrpc";
import { defaultInstance } from "../testutil.spec";
import { HttpClient } from "./httpclient";
import { instanceOfRpcStreamingClient } from "./rpcclient";
import { hasProtocol, instanceOfRpcStreamingClient } from "./rpcclient";
import { WebsocketClient } from "./websocketclient";
function pendingWithoutTendermint(): void {
@ -11,13 +11,14 @@ function pendingWithoutTendermint(): void {
}
describe("RpcClient", () => {
const tendermintUrl = defaultInstance.url;
const httpUrl = "http://" + defaultInstance.url;
const wsUrl = "ws://" + defaultInstance.url;
it("has working instanceOfRpcStreamingClient()", async () => {
pendingWithoutTendermint();
const httpClient = new HttpClient(tendermintUrl);
const wsClient = new WebsocketClient(tendermintUrl);
const httpClient = new HttpClient(httpUrl);
const wsClient = new WebsocketClient(wsUrl);
expect(instanceOfRpcStreamingClient(httpClient)).toEqual(false);
expect(instanceOfRpcStreamingClient(wsClient)).toEqual(true);
@ -32,11 +33,11 @@ describe("RpcClient", () => {
const statusRequest = createJsonRpcRequest("status");
const httpClient = new HttpClient(tendermintUrl + "/");
const httpClient = new HttpClient(httpUrl + "/");
expect(await httpClient.execute(statusRequest)).toBeDefined();
httpClient.disconnect();
const wsClient = new WebsocketClient(tendermintUrl + "/");
const wsClient = new WebsocketClient(wsUrl + "/");
expect(await wsClient.execute(statusRequest)).toBeDefined();
wsClient.disconnect();
});

View File

@ -14,7 +14,8 @@ function pendingWithoutTendermint(): void {
}
describe("WebsocketClient", () => {
const { blockTime, url: tendermintUrl } = defaultInstance;
const { blockTime, url } = defaultInstance;
const tendermintUrl = "ws://" + url;
it("can make a simple call", async () => {
pendingWithoutTendermint();

View File

@ -143,11 +143,13 @@ export class WebsocketClient implements RpcStreamingClient {
private readonly subscriptionStreams = new Map<string, Stream<SubscriptionEvent>>();
public constructor(baseUrl: string, onError: (err: any) => void = defaultErrorHandler) {
// accept host.name:port and assume ws protocol
if (!hasProtocol(baseUrl)) {
throw new Error("Base URL is missing a protocol. Expected 'ws://' or 'wss://'.");
}
// make sure we don't end up with ...//websocket
const path = baseUrl.endsWith("/") ? "websocket" : "/websocket";
const cleanBaseUrl = hasProtocol(baseUrl) ? baseUrl : "ws://" + baseUrl;
this.url = cleanBaseUrl + path;
this.url = baseUrl + path;
this.socket = new ReconnectingSocket(this.url);

View File

@ -876,14 +876,6 @@ describe("Tendermint34Client", () => {
it("can connect to a given url", async () => {
pendingWithoutTendermint();
// default connection
{
const client = await Tendermint34Client.connect(url);
const info = await client.abciInfo();
expect(info).toBeTruthy();
client.disconnect();
}
// http connection
{
const client = await Tendermint34Client.connect("http://" + url);
@ -902,13 +894,13 @@ describe("Tendermint34Client", () => {
});
describe("With HttpClient", () => {
defaultTestSuite(() => new HttpClient(url), expected);
defaultTestSuite(() => new HttpClient("http://" + url), expected);
});
describe("With WebsocketClient", () => {
// don't print out WebSocket errors if marked pending
const onError = process.env.TENDERMINT_ENABLED ? console.error : () => 0;
const factory = (): WebsocketClient => new WebsocketClient(url, onError);
const factory = (): WebsocketClient => new WebsocketClient("ws://" + url, onError);
defaultTestSuite(factory, expected);
websocketTestSuite(factory, expected);
});

View File

@ -885,14 +885,6 @@ describe("Tendermint37Client", () => {
it("can connect to a given url", async () => {
pendingWithoutTendermint();
// default connection
{
const client = await Tendermint37Client.connect(url);
const info = await client.abciInfo();
expect(info).toBeTruthy();
client.disconnect();
}
// http connection
{
const client = await Tendermint37Client.connect("http://" + url);
@ -911,13 +903,13 @@ describe("Tendermint37Client", () => {
});
describe("With HttpClient", () => {
defaultTestSuite(() => new HttpClient(url), expected);
defaultTestSuite(() => new HttpClient("http://" + url), expected);
});
describe("With WebsocketClient", () => {
// don't print out WebSocket errors if marked pending
const onError = process.env.TENDERMINT_ENABLED ? console.error : () => 0;
const factory = (): WebsocketClient => new WebsocketClient(url, onError);
const factory = (): WebsocketClient => new WebsocketClient("ws://" + url, onError);
defaultTestSuite(factory, expected);
websocketTestSuite(factory, expected);
});

View File

@ -15,6 +15,7 @@ export interface ExpectedValues {
}
export interface TendermintInstance {
/** URL without protocol. Protocol will be added in tests. */
readonly url: string;
readonly version: string;
/** rough block time in ms */