Skip to content

Commit c1900ef

Browse files
committed
HACKS
1 parent 7288c09 commit c1900ef

2 files changed

Lines changed: 33 additions & 42 deletions

File tree

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export declare class Database {
4040
aggregate(): void
4141
table(): void
4242
authorizer(rulesObj: object): void
43-
loadExtension(path: string): void
43+
loadExtension(path: string, entryPoint?: string | undefined | null): void
4444
maxWriteReplicationIndex(): number
4545
exec(sql: string): void
4646
interrupt(): void

src/lib.rs

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -285,54 +285,22 @@ impl Database {
285285
))
286286
}
287287

288-
#[napi]
289-
pub fn backup(&self) -> Result<()> {
290-
todo!();
291-
}
292-
293-
#[napi]
294-
pub fn serialize(&self) -> Result<()> {
295-
todo!();
296-
}
297-
298-
#[napi]
299-
pub fn function(&self) -> Result<()> {
300-
todo!();
301-
}
302-
303-
#[napi]
304-
pub fn aggregate(&self) -> Result<()> {
305-
todo!();
306-
}
307-
308-
#[napi]
309-
pub fn table(&self) -> Result<()> {
310-
todo!();
311-
}
312-
313288
#[napi]
314289
pub fn authorizer(&self, env: Env, rules_obj: napi::JsObject) -> Result<()> {
315-
// Acquire database connection
316290
let conn = match &self.conn {
317291
Some(c) => c.clone(),
318292
None => {
319293
return Err(throw_database_closed_error(&env).into());
320294
}
321295
};
322-
323-
// Build allow/deny rules based on the provided object
324296
let mut builder = crate::auth::AuthorizerBuilder::new();
325-
326-
let prop_names = rules_obj.get_property_names()?; // JsObject (array)
297+
let prop_names = rules_obj.get_property_names()?;
327298
let len = prop_names.get_array_length()?;
328-
329299
for idx in 0..len {
330300
let key_js: napi::JsString = prop_names.get_element::<napi::JsString>(idx)?;
331301
let key = key_js.into_utf8()?.into_owned()?;
332-
333302
let value_js: napi::JsNumber = rules_obj.get_named_property(&key)?;
334303
let value = value_js.get_int32()?;
335-
336304
match value {
337305
0 => {
338306
// Authorization.ALLOW
@@ -351,29 +319,52 @@ impl Database {
351319
}
352320
}
353321
}
354-
355322
let authorizer = builder.build();
356-
357-
// Prepare closure
358323
let auth_arc = std::sync::Arc::new(authorizer);
359324
let closure = {
360325
let auth_arc = auth_arc.clone();
361326
move |ctx: &libsql::AuthContext| auth_arc.authorize(ctx)
362327
};
363-
364-
// Register with connection
365328
let rt = runtime()?;
366329
let guard_conn = rt.block_on(async { conn.lock().await });
367330
guard_conn
368331
.authorizer(Some(std::sync::Arc::new(closure)))
369332
.map_err(Error::from)?;
370-
371333
Ok(())
372334
}
373335

374336
#[napi]
375-
pub fn loadExtension(&self, _path: String) -> Result<()> {
376-
todo!();
337+
pub fn loadExtension(&self, path: String, entry_point: Option<String>) -> Result<()> {
338+
let rt = runtime()?;
339+
let conn = match &self.conn {
340+
Some(conn) => conn.clone(),
341+
None => {
342+
return Err(throw_sqlite_error(
343+
"The database connection is not open".to_string(),
344+
"SQLITE_NOTOPEN".to_string(),
345+
0,
346+
));
347+
}
348+
};
349+
350+
rt.block_on(async move {
351+
let conn = conn.lock().await;
352+
353+
// Enable extension loading
354+
conn.load_extension_enable().map_err(Error::from)?;
355+
356+
// Load the extension
357+
if let Err(err) = conn.load_extension(&path, entry_point.as_deref()) {
358+
// Disable extension loading on error
359+
let _ = conn.load_extension_disable();
360+
return Err(Error::from(err));
361+
}
362+
363+
// Disable extension loading after successful load
364+
conn.load_extension_disable().map_err(Error::from)?;
365+
366+
Ok(())
367+
}).map_err(|e| napi::Error::from(e))
377368
}
378369

379370
#[napi]

0 commit comments

Comments
 (0)