Skip to content

Commit 6735e2b

Browse files
authored
Add support for cloud encryption (#335)
This patch adds support for cloud encryption for remote and sync operations. depends on libsql/hrana-client-ts#25
2 parents 5210326 + 1301b20 commit 6735e2b

File tree

11 files changed

+149
-15
lines changed

11 files changed

+149
-15
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local.db
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Cloud Encryption
2+
3+
These examples demonstrates how to use Turso Cloud encryption.
4+
5+
Visit the documentation here - [Cloud Encryption](https://docs.turso.tech/cloud/encryption)
6+
7+
## Install Dependencies
8+
9+
```bash
10+
npm i
11+
```
12+
13+
## Running
14+
15+
Execute the example which operates over remotely encrypted database:
16+
17+
```bash
18+
node remote.mjs
19+
```
20+
21+
Cloud encryption also supports sync:
22+
23+
```bash
24+
node sync.mjs
25+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "batch",
3+
"version": "1.0.0",
4+
"main": "remote.mjs",
5+
"author": "Turso Authors",
6+
"license": "MIT",
7+
"dependencies": {
8+
"@libsql/client": "^0.16.0"
9+
}
10+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createClient } from "@libsql/client";
2+
3+
const client = createClient({
4+
url: process.env.TURSO_DATABASE_URL,
5+
authToken: process.env.TURSO_AUTH_TOKEN,
6+
remoteEncryptionKey: process.env.TURSO_REMOTE_ENCRYPTION_KEY,
7+
});
8+
9+
await client.batch(
10+
[
11+
"CREATE TABLE IF NOT EXISTS users (email TEXT)",
12+
"INSERT INTO users VALUES ('first@example.com')",
13+
"INSERT INTO users VALUES ('second@example.com')",
14+
"INSERT INTO users VALUES ('third@example.com')",
15+
],
16+
"write",
17+
);
18+
19+
const result = await client.execute("SELECT * FROM users");
20+
21+
console.log("Users:", result.rows);

examples/cloud-encryption/sync.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createClient } from "@libsql/client";
2+
3+
const client = createClient({
4+
url: "file:local.db",
5+
syncUrl: process.env.TURSO_DATABASE_URL,
6+
authToken: process.env.TURSO_AUTH_TOKEN,
7+
remoteEncryptionKey: process.env.TURSO_REMOTE_ENCRYPTION_KEY,
8+
});
9+
10+
await client.batch(
11+
[
12+
"CREATE TABLE IF NOT EXISTS users (email TEXT)",
13+
"INSERT INTO users VALUES ('first@example.com')",
14+
"INSERT INTO users VALUES ('second@example.com')",
15+
"INSERT INTO users VALUES ('third@example.com')",
16+
],
17+
"write",
18+
);
19+
20+
const result = await client.execute("SELECT * FROM users");
21+
22+
console.log("Users:", result.rows);

package-lock.json

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

packages/libsql-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
},
105105
"dependencies": {
106106
"@libsql/core": "^0.16.0",
107-
"@libsql/hrana-client": "^0.7.0",
107+
"@libsql/hrana-client": "^0.9.0",
108108
"js-base64": "^3.7.5",
109109
"libsql": "^0.5.22",
110110
"promise-limit": "^2.7.0"

packages/libsql-client/src/http.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export function _createClient(config: ExpandedConfig): Client {
6666
config.intMode,
6767
config.fetch,
6868
config.concurrency,
69+
config.remoteEncryptionKey,
6970
);
7071
}
7172

@@ -79,6 +80,7 @@ export class HttpClient implements Client {
7980
#customFetch: Function | undefined;
8081
#concurrency: number;
8182
#authToken: string | undefined;
83+
#remoteEncryptionKey: string | undefined;
8284
#promiseLimitFunction: ReturnType<typeof promiseLimit<any>>;
8385

8486
/** @private */
@@ -88,17 +90,20 @@ export class HttpClient implements Client {
8890
intMode: IntMode,
8991
customFetch: Function | undefined,
9092
concurrency: number,
93+
remoteEncryptionKey: string | undefined,
9194
) {
9295
this.#url = url;
9396
this.#authToken = authToken;
9497
this.#intMode = intMode;
9598
this.#customFetch = customFetch;
9699
this.#concurrency = concurrency;
100+
this.#remoteEncryptionKey = remoteEncryptionKey;
97101

98102
this.#client = hrana.openHttp(
99103
this.#url,
100104
this.#authToken,
101105
this.#customFetch,
106+
remoteEncryptionKey,
102107
);
103108
this.#client.intMode = this.#intMode;
104109
this.protocol = "http";
@@ -292,6 +297,7 @@ export class HttpClient implements Client {
292297
this.#url,
293298
this.#authToken,
294299
this.#customFetch,
300+
this.#remoteEncryptionKey,
295301
);
296302
this.#client.intMode = this.#intMode;
297303
}

packages/libsql-client/src/sqlite3.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export function _createClient(config: ExpandedConfig): Client {
8181
const options = {
8282
authToken: config.authToken,
8383
encryptionKey: config.encryptionKey,
84+
remoteEncryptionKey: config.remoteEncryptionKey,
8485
syncUrl: config.syncUrl,
8586
syncPeriod: config.syncInterval,
8687
readYourWrites: config.readYourWrites,

packages/libsql-core/src/api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export interface Config {
1515
/** Encryption key for the database. */
1616
encryptionKey?: string;
1717

18+
/** Encryption key for encryption in Turso Cloud. */
19+
remoteEncryptionKey?: string;
20+
1821
/** URL of a remote server to synchronize database with. */
1922
syncUrl?: string;
2023

0 commit comments

Comments
 (0)