@ryuu-reinzz/haruka-lib 3.6.0 → 3.6.2

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.
@@ -7,6 +7,8 @@
7
7
  */
8
8
 
9
9
  import Database from "better-sqlite3";
10
+ import fs from "fs";
11
+ import path from "path";
10
12
  import {
11
13
  Mutex
12
14
  } from "async-mutex";
@@ -101,8 +103,10 @@ class WriteBuffer {
101
103
  }
102
104
 
103
105
  class AuthDatabase {
106
+
104
107
  constructor(dbPath = DEFAULT_DB, options = {}, BufferJSON) {
105
108
  this.dbPath = dbPath;
109
+ this._initWatcher();
106
110
  this.instanceId = `auth-${Date.now()}-${randomUUID()}`;
107
111
  this.disposed = false;
108
112
  this.isInitialized = false;
@@ -116,6 +120,23 @@ class AuthDatabase {
116
120
  this._registerCleanup();
117
121
  this.isInitialized = true;
118
122
  }
123
+ _initWatcher() {
124
+ const dir = path.dirname(this.dbPath);
125
+
126
+ try {
127
+ this.watcher = fs.watch(dir, (eventType, filename) => {
128
+ if (filename === path.basename(this.dbPath) && !fs.existsSync(this.dbPath)) {
129
+ console.log(`[AuthDB] File ${filename} terdeteksi dihapus. Membersihkan instance...`);
130
+ this._cleanup();
131
+ instances.delete(this.dbPath);
132
+ }
133
+ });
134
+
135
+ this.watcher.unref?.();
136
+ } catch (e) {
137
+ // Folder mungkin belum ada
138
+ }
139
+ }
119
140
 
120
141
  _initDatabase() {
121
142
  const db = new Database(this.dbPath);
@@ -357,25 +378,30 @@ class AuthDatabase {
357
378
  this.disposed = true;
358
379
 
359
380
  try {
381
+ if (this.watcher) this.watcher.close();
360
382
  if (this.flushTimer) clearTimeout(this.flushTimer);
361
383
  if (this.vacuumTimer) clearTimeout(this.vacuumTimer);
362
384
 
363
- const {
364
- upserts,
365
- deletes
366
- } = this.writeBuffer.toArrays();
367
- if (upserts.length || deletes.length) {
368
- this.txCommit(upserts, deletes);
385
+ if (fs.existsSync(this.dbPath)) {
386
+ const {
387
+ upserts,
388
+ deletes
389
+ } = this.writeBuffer.toArrays();
390
+ if (upserts.length || deletes.length) {
391
+ this.txCommit(upserts, deletes);
392
+ }
393
+ this.db.pragma("wal_checkpoint(TRUNCATE)");
394
+ this.db.pragma("incremental_vacuum");
395
+ this.db.pragma("optimize");
369
396
  }
370
397
 
371
- this.db.pragma("wal_checkpoint(TRUNCATE)");
372
- this.db.pragma("incremental_vacuum");
373
- this.db.pragma("optimize");
374
-
375
398
  this.db.close();
376
399
  this.cache.clear();
377
- } catch {}
400
+ } catch (e) {
401
+ // Silent fail jika database sudah tidak bisa diakses
402
+ }
378
403
  }
404
+
379
405
  }
380
406
 
381
407
  const instances = new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ryuu-reinzz/haruka-lib",
3
- "version": "3.6.0",
3
+ "version": "3.6.2",
4
4
  "description": "Library extra for bot WhatsApp",
5
5
  "main": "main/index.js",
6
6
  "type": "module",