Skip to content

Commit 2ed2cac

Browse files
committed
Add support for remote encryption
This adds a new `remoteEncryptionKey` option to database options, which is used on keep data on server side encrypted.
1 parent 8ec2c0a commit 2ed2cac

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface Options {
1111
syncPeriod?: number
1212
encryptionCipher?: string
1313
encryptionKey?: string
14+
remoteEncryptionKey?: string
1415
}
1516
/** Result of a database sync operation. */
1617
export interface SyncResult {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,12 @@ pub struct Options {
196196
pub encryptionCipher: Option<String>,
197197
// Encryption key for local encryption at rest.
198198
pub encryptionKey: Option<String>,
199+
// Encryption key for remote encryption at rest.
200+
pub remoteEncryptionKey: Option<String>,
199201
}
200202

201203
/// Access mode.
202-
///
204+
///
203205
/// The `better-sqlite3` API allows the caller to configure the format of
204206
/// query results. This struct encapsulates the different access mode configs.
205207
struct AccessMode {
@@ -247,7 +249,17 @@ impl Database {
247249
.and_then(|o| o.authToken.as_ref())
248250
.cloned()
249251
.unwrap_or_default();
250-
let builder = libsql::Builder::new_remote(path.clone(), auth_token);
252+
let mut builder = libsql::Builder::new_remote(path.clone(), auth_token);
253+
if let Some(encryption_key) = opts
254+
.as_ref()
255+
.and_then(|o| o.remoteEncryptionKey.as_ref())
256+
.cloned()
257+
{
258+
let encryption_context = libsql::EncryptionContext {
259+
key: libsql::EncryptionKey::Base64Encoded(encryption_key),
260+
};
261+
builder = builder.remote_encryption(Some(encryption_context));
262+
}
251263
rt.block_on(builder.build()).map_err(Error::from)?
252264
} else if let Some(options) = &opts {
253265
if let Some(sync_url) = &options.syncUrl {
@@ -280,6 +292,15 @@ impl Database {
280292
builder = builder.encryption_config(encryption_config);
281293
}
282294

295+
if let Some(remote_encryption_key) = &options.remoteEncryptionKey {
296+
let encryption_context = libsql::EncryptionContext {
297+
key: libsql::EncryptionKey::Base64Encoded(
298+
remote_encryption_key.to_string(),
299+
),
300+
};
301+
builder = builder.remote_encryption(Some(encryption_context));
302+
}
303+
283304
if let Some(period) = options.syncPeriod {
284305
if period > 0.0 {
285306
builder = builder.sync_interval(std::time::Duration::from_secs_f64(period));
@@ -865,9 +886,12 @@ impl Statement {
865886
}
866887
}
867888

868-
869889
#[napi]
870-
pub fn statement_iterate_sync(stmt: &Statement, _env: Env, params: Option<napi::JsUnknown>) -> Result<RowsIterator> {
890+
pub fn statement_iterate_sync(
891+
stmt: &Statement,
892+
_env: Env,
893+
params: Option<napi::JsUnknown>,
894+
) -> Result<RowsIterator> {
871895
let rt = runtime()?;
872896
let safe_ints = stmt.mode.safe_ints.load(Ordering::SeqCst);
873897
let raw = stmt.mode.raw.load(Ordering::SeqCst);
@@ -880,9 +904,8 @@ pub fn statement_iterate_sync(stmt: &Statement, _env: Env, params: Option<napi::
880904
let rows = stmt.query(params).await.map_err(Error::from)?;
881905
let mut column_names = Vec::new();
882906
for i in 0..rows.column_count() {
883-
column_names.push(
884-
std::ffi::CString::new(rows.column_name(i).unwrap().to_string()).unwrap(),
885-
);
907+
column_names
908+
.push(std::ffi::CString::new(rows.column_name(i).unwrap().to_string()).unwrap());
886909
}
887910
Ok::<_, napi::Error>((rows, column_names))
888911
})?;

0 commit comments

Comments
 (0)