@ryuu-reinzz/haruka-lib 3.6.0 → 3.6.1

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