@hiroosy/baileys-lite 0.3.0 → 0.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hiroosy/baileys-lite",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "A Lightweight Baileys built with NodeJs 18+",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,7 +1,8 @@
1
1
  import { appendFileSync, existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
2
2
 
3
3
  const MAGIC = '{"nsql":1}'
4
- const COMPACT_MIN_BYTES = 1024 * 1024
4
+ const COMPACT_MIN_BYTES = 256 * 1024
5
+ const COMPACT_GROWTH_RATIO = 0.5
5
6
  const CONTROL = new Set(['begin', 'commit', 'rollback'])
6
7
  const TOKEN = /\s+|'((?:[^']|'')*)'|"([^"]*)"|`([^`]*)`|\[([^\]]*)\]|(\d+\.?\d*(?:[eE][+-]?\d+)?|\.\d+(?:[eE][+-]?\d+)?)|([A-Za-z_]\w*)|([(),;=?*.-])/y
7
8
 
@@ -16,6 +17,8 @@ const lower = name => name.toLowerCase()
16
17
 
17
18
  const keyOf = (table, row) => JSON.stringify(table.pk.map(i => row[i]))
18
19
 
20
+ const sameRow = (a, b) => a.length === b.length && a.every((v, i) => v === b[i])
21
+
19
22
  const bind = (count, params) => {
20
23
  if (params.length !== count) throw new SqlError(`expected ${count} parameters, got ${params.length}`)
21
24
  return params.map(value => {
@@ -361,7 +364,7 @@ export class SqlDatabase {
361
364
  const values = bind(stmt.paramCount, params)
362
365
  return this.#statement(() => {
363
366
  const result = this.#execute(stmt, values)
364
- if (stmt.write && result.changes) this.#pending.push([stmt.text, values])
367
+ if (stmt.write && result.changes && !result.noop) this.#pending.push([stmt.text, values])
365
368
  return result
366
369
  })
367
370
  }
@@ -533,6 +536,7 @@ export class SqlDatabase {
533
536
  if (action === 'nothing') return { changes: 0, rows: [] }
534
537
  if (action === 'update') return this.#upsertConflict(table, rowKey, existing, stmt.onConflict.sets, row, params)
535
538
  if (stmt.replace) {
539
+ if (sameRow(existing, row)) return { changes: 1, rows: [], noop: true }
536
540
  this.#storeRow(table, rowKey, row)
537
541
  return { changes: 1, rows: [] }
538
542
  }
@@ -548,6 +552,8 @@ export class SqlDatabase {
548
552
  }
549
553
  if (table.pk.some(i => next[i] !== existing[i])) throw new SqlError('primary key cannot be changed on conflict')
550
554
  this.#checkNotNull(table, next)
555
+ // rewriting identical data (e.g. saveCreds with unchanged creds) must not grow the log
556
+ if (sameRow(existing, next)) return { changes: 1, rows: [], noop: true }
551
557
  this.#storeRow(table, rowKey, next)
552
558
  return { changes: 1, rows: [] }
553
559
  }
@@ -653,8 +659,13 @@ export class SqlDatabase {
653
659
  for (const line of lines.slice(1)) this.#replay(line)
654
660
  this.#path = path
655
661
  this.#logBytes = this.#baseBytes = Buffer.byteLength(text)
656
- if (fresh) writeFileSync(path, text)
657
- else if (tail !== '') this.#compact()
662
+ if (fresh) {
663
+ writeFileSync(path, text)
664
+ } else if (tail !== '' || lines.length > 2) {
665
+ // torn write, or the log holds more than a single snapshot line: rewrite as a clean
666
+ // snapshot so baseBytes reflects live data and old bloated files shrink on first open
667
+ this.#compact()
668
+ }
658
669
  }
659
670
 
660
671
  #replay(line) {
@@ -678,7 +689,10 @@ export class SqlDatabase {
678
689
  }
679
690
 
680
691
  #maybeCompact() {
681
- if (this.#path === null || this.#logBytes < COMPACT_MIN_BYTES || this.#logBytes < 2 * this.#baseBytes) return
692
+ if (this.#path === null) return
693
+ // baseBytes is always the size of a clean snapshot, so garbage = logBytes - baseBytes
694
+ const garbage = this.#logBytes - this.#baseBytes
695
+ if (garbage < Math.max(COMPACT_MIN_BYTES, this.#baseBytes * COMPACT_GROWTH_RATIO)) return
682
696
  try {
683
697
  this.#compact()
684
698
  } catch {}