Skip to content

Commit

Permalink
Switch to Hrana 2 by default
Browse files Browse the repository at this point in the history
Let's give Hrana 3 some more time to mature and stay with Hrana 2 by
default.
  • Loading branch information
penberg committed Sep 11, 2023
1 parent 4decb5d commit df7970b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/readme_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import * as hrana from "@libsql/hrana-client";
const url = process.env.URL ?? "ws://localhost:8080"; // Address of the sqld server
const jwt = process.env.JWT; // JWT token for authentication
// Here we are using Hrana over WebSockets:
const client = hrana.openWs(url, jwt);
const client = hrana.openWs(url, jwt, 3);
// But we can also use Hrana over HTTP:
// const client = hrana.openHttp(url, jwt);
// const client = hrana.openHttp(url, jwt, undefined, 3);

// Open a `hrana.Stream`, which is an interactive SQL stream. This corresponds
// to a "connection" from other SQL databases
Expand Down
18 changes: 9 additions & 9 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ function withClient(f: (c: hrana.Client) => Promise<void>): () => Promise<void>
return async () => {
let client: hrana.Client;
if (isWs) {
client = hrana.openWs(url, jwt);
client = hrana.openWs(url, jwt, 3);
} else if (isHttp) {
client = hrana.openHttp(url, jwt);
client = hrana.openHttp(url, jwt, undefined, 3);
} else {
throw new Error("expected either ws or http URL");
}
Expand All @@ -38,7 +38,7 @@ function withClient(f: (c: hrana.Client) => Promise<void>): () => Promise<void>

function withWsClient(f: (c: hrana.WsClient) => Promise<void>): () => Promise<void> {
return async () => {
const client = hrana.openWs(url, jwt);
const client = hrana.openWs(url, jwt, 3);
try {
await f(client);
} finally {
Expand All @@ -49,7 +49,7 @@ function withWsClient(f: (c: hrana.WsClient) => Promise<void>): () => Promise<vo

function withHttpClient(f: (c: hrana.HttpClient) => Promise<void>): () => Promise<void> {
return async () => {
const client = hrana.openHttp(url, jwt);
const client = hrana.openHttp(url, jwt, undefined, 3);
try {
await f(client);
} finally {
Expand Down Expand Up @@ -868,7 +868,7 @@ for (const useCursor of [false, true]) {
function withSqlOwner(f: (s: hrana.Stream, owner: hrana.SqlOwner) => Promise<void>): () => Promise<void> {
return async () => {
if (isWs) {
const client = hrana.openWs(url, jwt);
const client = hrana.openWs(url, jwt, 3);
try {
await client.getVersion();
const stream = client.openStream();
Expand All @@ -877,7 +877,7 @@ for (const useCursor of [false, true]) {
client.close();
}
} else if (isHttp) {
const client = hrana.openHttp(url, jwt);
const client = hrana.openHttp(url, jwt, undefined, 3);
try {
const stream = client.openStream();
await f(stream, stream);
Expand Down Expand Up @@ -980,7 +980,7 @@ test("getVersion()", withClient(async (c) => {
return fetch(request);
}

const c = hrana.openHttp(url, jwt, customFetch);
const c = hrana.openHttp(url, jwt, customFetch, 3);
try {
const s = c.openStream();
const res = await s.queryValue("SELECT 1");
Expand All @@ -997,7 +997,7 @@ test("getVersion()", withClient(async (c) => {
throw new Error("testing exception thrown from customFetch()");
}

const c = hrana.openHttp(url, jwt, customFetch);
const c = hrana.openHttp(url, jwt, customFetch, 3);
try {
const s = c.openStream();
await expect(s.queryValue("SELECT 1")).rejects
Expand All @@ -1012,7 +1012,7 @@ test("getVersion()", withClient(async (c) => {
return Promise.reject(new Error("testing rejection returned from customFetch()"));
}

const c = hrana.openHttp(url, jwt, customFetch);
const c = hrana.openHttp(url, jwt, customFetch, 3);
try {
const s = c.openStream();
await expect(s.queryValue("SELECT 1")).rejects
Expand Down
20 changes: 14 additions & 6 deletions src/http/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class HttpClient extends Client {
_endpoint: Endpoint | undefined;

/** @private */
constructor(url: URL, jwt: string | undefined, customFetch: unknown | undefined) {
constructor(url: URL, jwt: string | undefined, customFetch: unknown | undefined, protocolVersion: ProtocolVersion = 2) {
super();
this.#url = url;
this.#jwt = jwt;
Expand All @@ -66,11 +66,19 @@ export class HttpClient extends Client {
this.#closed = undefined;
this.#streams = new Set();

this._endpointPromise = findEndpoint(this.#fetch, this.#url);
this._endpointPromise.then(
(endpoint) => this._endpoint = endpoint,
(error) => this.#setClosed(error),
);
if (protocolVersion == 3) {
this._endpointPromise = findEndpoint(this.#fetch, this.#url);
this._endpointPromise.then(
(endpoint) => this._endpoint = endpoint,
(error) => this.#setClosed(error),
);
} else {
this._endpointPromise = Promise.resolve(fallbackEndpoint);
this._endpointPromise.then(
(endpoint) => this._endpoint = endpoint,
(error) => this.#setClosed(error),
);
}
}

/** Get the protocol version supported by the server. */
Expand Down
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { WebSocket } from "@libsql/isomorphic-ws";

import { subprotocols } from "./ws/client.js";
import { subprotocolsV2, subprotocolsV3 } from "./ws/client.js";
import { WebSocketUnsupportedError } from "./errors.js";

import { HttpClient } from "./http/client.js";
import { WsClient } from "./ws/client.js";
import { ProtocolVersion } from "./client.js";

export { WebSocket } from "@libsql/isomorphic-ws";
export type { RequestInit, Response } from "@libsql/isomorphic-fetch";
Expand All @@ -31,11 +32,17 @@ export { WsClient } from "./ws/client.js";
export { WsStream } from "./ws/stream.js";

/** Open a Hrana client over WebSocket connected to the given `url`. */
export function openWs(url: string | URL, jwt?: string): WsClient {
export function openWs(url: string | URL, jwt?: string, protocolVersion: ProtocolVersion = 2): WsClient {
if (typeof WebSocket === "undefined") {
throw new WebSocketUnsupportedError("WebSockets are not supported in this environment");
}
const socket = new WebSocket(url, Array.from(subprotocols.keys()));
var subprotocols = undefined;
if (protocolVersion == 3) {
subprotocols = Array.from(subprotocolsV3.keys());
} else {
subprotocols = Array.from(subprotocolsV2.keys());
}
const socket = new WebSocket(url, subprotocols);
return new WsClient(socket, jwt);
}

Expand All @@ -45,6 +52,6 @@ export function openWs(url: string | URL, jwt?: string): WsClient {
* from `@libsql/isomorphic-fetch`. This function is always called with a `Request` object from
* `@libsql/isomorphic-fetch`.
*/
export function openHttp(url: string | URL, jwt?: string, customFetch?: unknown | undefined): HttpClient {
return new HttpClient(url instanceof URL ? url : new URL(url), jwt, customFetch);
export function openHttp(url: string | URL, jwt?: string, customFetch?: unknown | undefined, protocolVersion: ProtocolVersion = 2): HttpClient {
return new HttpClient(url instanceof URL ? url : new URL(url), jwt, customFetch, protocolVersion);
}
9 changes: 7 additions & 2 deletions src/ws/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ export type Subprotocol = {
encoding: ProtocolEncoding,
};

export const subprotocols: Map<string, Subprotocol> = new Map([
export const subprotocolsV2: Map<string, Subprotocol> = new Map([
["hrana2", {version: 2, encoding: "json"}],
["hrana1", {version: 1, encoding: "json"}],
]);

export const subprotocolsV3: Map<string, Subprotocol> = new Map([
["hrana3-protobuf", {version: 3, encoding: "protobuf"}],
["hrana3", {version: 3, encoding: "json"}],
["hrana2", {version: 2, encoding: "json"}],
Expand Down Expand Up @@ -123,7 +128,7 @@ export class WsClient extends Client implements SqlOwner {
} else if (protocol === "") {
this.#subprotocol = {version: 1, encoding: "json"};
} else {
this.#subprotocol = subprotocols.get(protocol);
this.#subprotocol = subprotocolsV3.get(protocol);
if (this.#subprotocol === undefined) {
this.#setClosed(new ProtoError(
`Unrecognized WebSocket subprotocol: ${JSON.stringify(protocol)}`,
Expand Down

0 comments on commit df7970b

Please sign in to comment.