Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.9.0 -- 2025-12-19

- Add support for propagating remote encryption key

## 0.8.0 -- 2024-09-16

- Replace isomorphic-fetch dependency with cross-fetch
Expand Down
8 changes: 6 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@libsql/hrana-client",
"version": "0.8.0",
"version": "0.9.0",
"keywords": [
"hrana",
"libsql",
Expand Down
12 changes: 6 additions & 6 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function withClient(f: (c: hrana.Client) => Promise<void>): () => Promise<void>
if (isWs) {
client = hrana.openWs(url, jwt, 3);
} else if (isHttp) {
client = hrana.openHttp(url, jwt, undefined, 3);
client = hrana.openHttp(url, jwt, undefined, undefined, 3);
} else {
throw new Error("expected either ws or http URL");
}
Expand All @@ -50,7 +50,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, undefined, 3);
const client = hrana.openHttp(url, jwt, undefined, undefined, 3);
try {
await f(client);
} finally {
Expand Down Expand Up @@ -878,7 +878,7 @@ for (const useCursor of [false, true]) {
client.close();
}
} else if (isHttp) {
const client = hrana.openHttp(url, jwt, undefined, 3);
const client = hrana.openHttp(url, jwt, undefined, undefined, 3);
try {
const stream = client.openStream();
await f(stream, stream);
Expand Down Expand Up @@ -981,7 +981,7 @@ test("getVersion()", withClient(async (c) => {
return fetch(request);
}

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

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

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

#closed: Error | undefined;
#streams: Set<HttpStream>;
Expand All @@ -56,11 +57,12 @@ export class HttpClient extends Client {
_endpoint: Endpoint | undefined;

/** @private */
constructor(url: URL, jwt: string | undefined, customFetch: unknown | undefined, protocolVersion: ProtocolVersion = 2) {
constructor(url: URL, jwt: string | undefined, customFetch: unknown | undefined, remoteEncryptionKey?: string, protocolVersion: ProtocolVersion = 2) {
super();
this.#url = url;
this.#jwt = jwt;
this.#fetch = (customFetch as typeof fetch) ?? fetch;
this.#remoteEncryptionKey = remoteEncryptionKey;

this.#closed = undefined;
this.#streams = new Set();
Expand Down Expand Up @@ -112,7 +114,7 @@ export class HttpClient extends Client {
if (this.#closed !== undefined) {
throw new ClosedError("Client is closed", this.#closed);
}
const stream = new HttpStream(this, this.#url, this.#jwt, this.#fetch);
const stream = new HttpStream(this, this.#url, this.#jwt, this.#fetch, this.#remoteEncryptionKey);
this.#streams.add(stream);
return stream;
}
Expand Down
7 changes: 6 additions & 1 deletion src/http/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class HttpStream extends Stream implements SqlOwner {
#baseUrl: string;
#jwt: string | undefined;
#fetch: typeof fetch;
#remoteEncryptionKey: string | undefined;

#baton: string | undefined;
#queue: Queue<QueueEntry>;
Expand All @@ -65,12 +66,13 @@ export class HttpStream extends Stream implements SqlOwner {
#sqlIdAlloc: IdAlloc;

/** @private */
constructor(client: HttpClient, baseUrl: URL, jwt: string | undefined, customFetch: typeof fetch) {
constructor(client: HttpClient, baseUrl: URL, jwt: string | undefined, customFetch: typeof fetch, remoteEncryptionKey?: string) {
super(client.intMode);
this.#client = client;
this.#baseUrl = baseUrl.toString();
this.#jwt = jwt;
this.#fetch = customFetch;
this.#remoteEncryptionKey = remoteEncryptionKey;

this.#baton = undefined;
this.#queue = new Queue();
Expand Down Expand Up @@ -412,6 +414,9 @@ export class HttpStream extends Stream implements SqlOwner {
if (this.#jwt !== undefined) {
headers.set("authorization", `Bearer ${this.#jwt}`);
}
if (this.#remoteEncryptionKey !== undefined) {
headers.set("x-turso-encryption-key", this.#remoteEncryptionKey);
}

return new Request(url.toString(), {method: "POST", headers, body: bodyData});
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ export function openWs(url: string | URL, jwt?: string, protocolVersion: Protoco
* from `cross-fetch`. This function is always called with a `Request` object from
* `cross-fetch`.
*/
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);
export function openHttp(url: string | URL, jwt?: string, customFetch?: unknown | undefined, remoteEncryptionKey?: string, protocolVersion: ProtocolVersion = 2): HttpClient {
return new HttpClient(url instanceof URL ? url : new URL(url), jwt, customFetch, remoteEncryptionKey, protocolVersion);
}