@korajs/auth 1.0.0-beta.13 → 1.0.0-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.cjs CHANGED
@@ -3619,6 +3619,8 @@ function generateSecureToken2() {
3619
3619
  // src/provider/built-in/sqlite-user-store.ts
3620
3620
  init_cjs_shims();
3621
3621
  var import_node_crypto8 = require("crypto");
3622
+ var import_node_fs = require("fs");
3623
+ var import_node_path = require("path");
3622
3624
  var SqliteUserStore = class {
3623
3625
  db;
3624
3626
  revocationStore = null;
@@ -3758,6 +3760,7 @@ var SqliteUserStore = class {
3758
3760
  };
3759
3761
  async function createSqliteUserStore(options) {
3760
3762
  const Database = await loadBetterSqlite3();
3763
+ ensureDatabaseDirectory(options.filename);
3761
3764
  const db = new Database(options.filename);
3762
3765
  return new SqliteUserStore(db);
3763
3766
  }
@@ -3794,6 +3797,10 @@ function rowToDevice(row) {
3794
3797
  lastSeenAt: row.last_seen_at
3795
3798
  };
3796
3799
  }
3800
+ function ensureDatabaseDirectory(filename) {
3801
+ if (filename === "" || filename === ":memory:" || filename.startsWith("file:")) return;
3802
+ (0, import_node_fs.mkdirSync)((0, import_node_path.dirname)(filename), { recursive: true });
3803
+ }
3797
3804
 
3798
3805
  // src/provider/built-in/postgres-user-store.ts
3799
3806
  init_cjs_shims();
@@ -3802,10 +3809,32 @@ var PostgresUserStore = class {
3802
3809
  sql;
3803
3810
  ready;
3804
3811
  revocationStore = null;
3805
- constructor(sql) {
3812
+ ownsClient;
3813
+ closed = false;
3814
+ constructor(sql, options = {}) {
3806
3815
  this.sql = sql;
3816
+ this.ownsClient = options.ownsClient ?? false;
3807
3817
  this.ready = this.ensureTables();
3808
3818
  }
3819
+ /**
3820
+ * Release the store's database connections, so a script using it can exit. Waits
3821
+ * for table creation to settle first. A client passed to the constructor is left
3822
+ * open unless `ownsClient` was set (end it yourself). Idempotent.
3823
+ *
3824
+ * @example
3825
+ * ```typescript
3826
+ * const users = await createPostgresUserStore({ connectionString })
3827
+ * const device = await users.findDevice(nodeId)
3828
+ * await users.close()
3829
+ * ```
3830
+ */
3831
+ async close() {
3832
+ if (this.closed) return;
3833
+ this.closed = true;
3834
+ await this.ready.catch(() => {
3835
+ });
3836
+ if (this.ownsClient) await this.sql.end?.({ timeout: 5 });
3837
+ }
3809
3838
  /** Token revocations stored in the same Postgres database as the users. */
3810
3839
  getTokenRevocationStore() {
3811
3840
  if (!this.revocationStore) {
@@ -3970,7 +3999,7 @@ var PostgresUserStore = class {
3970
3999
  async function createPostgresUserStore(options) {
3971
4000
  const postgresClient = await loadPostgresDeps();
3972
4001
  const sql = postgresClient(options.connectionString);
3973
- return new PostgresUserStore(sql);
4002
+ return new PostgresUserStore(sql, { ownsClient: true });
3974
4003
  }
3975
4004
  async function loadPostgresDeps() {
3976
4005
  try {
@@ -6342,6 +6371,8 @@ var OrgScopeResolver = class {
6342
6371
  // src/provider/oauth/sqlite-oauth-store.ts
6343
6372
  init_cjs_shims();
6344
6373
  var import_node_crypto11 = require("crypto");
6374
+ var import_node_fs2 = require("fs");
6375
+ var import_node_path2 = require("path");
6345
6376
  var SqliteOAuthStateStore = class {
6346
6377
  db;
6347
6378
  constructor(db) {
@@ -6480,6 +6511,7 @@ async function createSqliteLinkedIdentityStore(options) {
6480
6511
  }
6481
6512
  async function createSqliteOAuthStores(options) {
6482
6513
  const Database = await loadBetterSqlite32();
6514
+ ensureDatabaseDirectory2(options.filename);
6483
6515
  const db = new Database(options.filename);
6484
6516
  return {
6485
6517
  stateStore: new SqliteOAuthStateStore(db),
@@ -6523,6 +6555,10 @@ function rowToLinkedIdentity(row) {
6523
6555
  linkedAt: row.linked_at
6524
6556
  };
6525
6557
  }
6558
+ function ensureDatabaseDirectory2(filename) {
6559
+ if (filename === "" || filename === ":memory:" || filename.startsWith("file:")) return;
6560
+ (0, import_node_fs2.mkdirSync)((0, import_node_path2.dirname)(filename), { recursive: true });
6561
+ }
6526
6562
 
6527
6563
  // src/provider/oauth/postgres-oauth-store.ts
6528
6564
  init_cjs_shims();