Skip to content

Commit 4397c81

Browse files
authored
Merge pull request #25 from avinassh/remote-encryption
Add support for propagating remote encryption key
2 parents 95a1d21 + 270708f commit 4397c81

7 files changed

Lines changed: 29 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.9.0 -- 2025-12-19
4+
5+
- Add support for propagating remote encryption key
6+
37
## 0.8.0 -- 2024-09-16
48

59
- Replace isomorphic-fetch dependency with cross-fetch

package-lock.json

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@libsql/hrana-client",
3-
"version": "0.8.0",
3+
"version": "0.9.0",
44
"keywords": [
55
"hrana",
66
"libsql",

src/__tests__/client.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function withClient(f: (c: hrana.Client) => Promise<void>): () => Promise<void>
2525
if (isWs) {
2626
client = hrana.openWs(url, jwt, 3);
2727
} else if (isHttp) {
28-
client = hrana.openHttp(url, jwt, undefined, 3);
28+
client = hrana.openHttp(url, jwt, undefined, undefined, 3);
2929
} else {
3030
throw new Error("expected either ws or http URL");
3131
}
@@ -50,7 +50,7 @@ function withWsClient(f: (c: hrana.WsClient) => Promise<void>): () => Promise<vo
5050

5151
function withHttpClient(f: (c: hrana.HttpClient) => Promise<void>): () => Promise<void> {
5252
return async () => {
53-
const client = hrana.openHttp(url, jwt, undefined, 3);
53+
const client = hrana.openHttp(url, jwt, undefined, undefined, 3);
5454
try {
5555
await f(client);
5656
} finally {
@@ -878,7 +878,7 @@ for (const useCursor of [false, true]) {
878878
client.close();
879879
}
880880
} else if (isHttp) {
881-
const client = hrana.openHttp(url, jwt, undefined, 3);
881+
const client = hrana.openHttp(url, jwt, undefined, undefined, 3);
882882
try {
883883
const stream = client.openStream();
884884
await f(stream, stream);
@@ -981,7 +981,7 @@ test("getVersion()", withClient(async (c) => {
981981
return fetch(request);
982982
}
983983

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

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

1016-
const c = hrana.openHttp(url, jwt, customFetch, 3);
1016+
const c = hrana.openHttp(url, jwt, customFetch, undefined, 3);
10171017
try {
10181018
const s = c.openStream();
10191019
await expect(s.queryValue("SELECT 1")).rejects

src/http/client.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class HttpClient extends Client {
4646
#url: URL;
4747
#jwt: string | undefined;
4848
#fetch: typeof fetch;
49+
#remoteEncryptionKey: string | undefined;
4950

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

5859
/** @private */
59-
constructor(url: URL, jwt: string | undefined, customFetch: unknown | undefined, protocolVersion: ProtocolVersion = 2) {
60+
constructor(url: URL, jwt: string | undefined, customFetch: unknown | undefined, remoteEncryptionKey?: string, protocolVersion: ProtocolVersion = 2) {
6061
super();
6162
this.#url = url;
6263
this.#jwt = jwt;
6364
this.#fetch = (customFetch as typeof fetch) ?? fetch;
65+
this.#remoteEncryptionKey = remoteEncryptionKey;
6466

6567
this.#closed = undefined;
6668
this.#streams = new Set();
@@ -112,7 +114,7 @@ export class HttpClient extends Client {
112114
if (this.#closed !== undefined) {
113115
throw new ClosedError("Client is closed", this.#closed);
114116
}
115-
const stream = new HttpStream(this, this.#url, this.#jwt, this.#fetch);
117+
const stream = new HttpStream(this, this.#url, this.#jwt, this.#fetch, this.#remoteEncryptionKey);
116118
this.#streams.add(stream);
117119
return stream;
118120
}

src/http/stream.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class HttpStream extends Stream implements SqlOwner {
5353
#baseUrl: string;
5454
#jwt: string | undefined;
5555
#fetch: typeof fetch;
56+
#remoteEncryptionKey: string | undefined;
5657

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

6768
/** @private */
68-
constructor(client: HttpClient, baseUrl: URL, jwt: string | undefined, customFetch: typeof fetch) {
69+
constructor(client: HttpClient, baseUrl: URL, jwt: string | undefined, customFetch: typeof fetch, remoteEncryptionKey?: string) {
6970
super(client.intMode);
7071
this.#client = client;
7172
this.#baseUrl = baseUrl.toString();
7273
this.#jwt = jwt;
7374
this.#fetch = customFetch;
75+
this.#remoteEncryptionKey = remoteEncryptionKey;
7476

7577
this.#baton = undefined;
7678
this.#queue = new Queue();
@@ -412,6 +414,9 @@ export class HttpStream extends Stream implements SqlOwner {
412414
if (this.#jwt !== undefined) {
413415
headers.set("authorization", `Bearer ${this.#jwt}`);
414416
}
417+
if (this.#remoteEncryptionKey !== undefined) {
418+
headers.set("x-turso-encryption-key", this.#remoteEncryptionKey);
419+
}
415420

416421
return new Request(url.toString(), {method: "POST", headers, body: bodyData});
417422
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ export function openWs(url: string | URL, jwt?: string, protocolVersion: Protoco
5252
* from `cross-fetch`. This function is always called with a `Request` object from
5353
* `cross-fetch`.
5454
*/
55-
export function openHttp(url: string | URL, jwt?: string, customFetch?: unknown | undefined, protocolVersion: ProtocolVersion = 2): HttpClient {
56-
return new HttpClient(url instanceof URL ? url : new URL(url), jwt, customFetch, protocolVersion);
55+
export function openHttp(url: string | URL, jwt?: string, customFetch?: unknown | undefined, remoteEncryptionKey?: string, protocolVersion: ProtocolVersion = 2): HttpClient {
56+
return new HttpClient(url instanceof URL ? url : new URL(url), jwt, customFetch, remoteEncryptionKey, protocolVersion);
5757
}

0 commit comments

Comments
 (0)