@harperfast/rocksdb-js 1.4.2 → 2.0.0

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/README.md CHANGED
@@ -668,12 +668,25 @@ complete, use the `'aftercommit'` event.
668
668
  ## Event API
669
669
 
670
670
  `rocksdb-js` provides a EventEmitter-like API that lets you asynchronously notify events to one or
671
- more synchronous listener callbacks. Events are scoped by database path.
671
+ more synchronous listener callbacks. There are two types of events:
672
+
673
+ - Per-database events: scoped by database path.
674
+ - Process-global events: not scoped by database path.
672
675
 
673
676
  Unlike `EventEmitter`, events are emitted asynchronously, but in the same order that the listeners
674
677
  were added.
675
678
 
676
679
  ```typescript
680
+ // Process-global events
681
+ RocksDatabase.on('log.warn', console.warn);
682
+
683
+ RocksDatabase.on('foo', (...args) => {
684
+ console.log(args);
685
+ });
686
+ RocksDatabase.notify('foo', 'bar');
687
+ RocksDatabase.off('foo', callback);
688
+
689
+ // Per-database events
677
690
  const callback = (name) => console.log(`Hi from ${name}`);
678
691
  db.addListener('foo', callback);
679
692
  db.notify('foo');
@@ -1103,6 +1116,18 @@ for (const entry of rangeIter) {
1103
1116
  Returns the size of the given transaction log sequence file in bytes. Omit the sequence number to
1104
1117
  get the total size of all the transaction log sequence files for this log.
1105
1118
 
1119
+ ### Transaction Log Initialization
1120
+
1121
+ When a database is opened, `rocksdb-js` will automatically discover the transaction log files. If
1122
+ a corrupt transaction log file is detected, the `log.warn` event is emitted on the global
1123
+ `RocksDatabase` instance. This should be wired up prior to opening the database.
1124
+
1125
+ ```typescript
1126
+ RocksDatabase.on('log.warn', console.warn);
1127
+
1128
+ const db = RocksDatabase.open('path/to/db');
1129
+ ```
1130
+
1106
1131
  ### Transaction Log Parser
1107
1132
 
1108
1133
  #### `parseTransactionLog(file)`
package/dist/index.cjs CHANGED
@@ -80,6 +80,10 @@ function locateBinding() {
80
80
  }
81
81
  const binding = req(locateBinding());
82
82
  const config = binding.config;
83
+ const addGlobalListener = binding.addListener;
84
+ const removeGlobalListener = binding.removeListener;
85
+ const globalListenerCount = binding.listenerCount;
86
+ const globalNotify = binding.notify;
83
87
  const constants = binding.constants;
84
88
  const NativeDatabase = binding.Database;
85
89
  const NativeIterator = binding.Iterator;
@@ -595,8 +599,6 @@ const VALUE_BUFFER = createFixedBuffer(64 * 1024);
595
599
  const ITERATOR_STATE_BUFFER = Buffer.allocUnsafeSlow(8);
596
600
  const ITERATOR_STATE = new Uint32Array(ITERATOR_STATE_BUFFER.buffer, ITERATOR_STATE_BUFFER.byteOffset, 2);
597
601
  const MAX_KEY_SIZE = 1024 * 1024;
598
- const RESET_BUFFER_MODE = 1024;
599
- const REUSE_BUFFER_MODE = 512;
600
602
  const SAVE_BUFFER_SIZE = 8192;
601
603
  /**
602
604
  * A store wraps the `NativeDatabase` binding and database settings so that a
@@ -886,7 +888,7 @@ var Store = class {
886
888
  encodeValue(value) {
887
889
  if (value && value["binary-data"]) return value["binary-data"];
888
890
  if (typeof this.encoder?.encode === "function") {
889
- if (this.encoder.copyBuffers) return this.encoder.encode(value, REUSE_BUFFER_MODE | RESET_BUFFER_MODE);
891
+ if (this.encoder.copyBuffers) return this.encoder.encode(value, 1536);
890
892
  const valueBuffer = this.encoder.encode(value);
891
893
  if (typeof valueBuffer === "string") return Buffer.from(valueBuffer);
892
894
  return valueBuffer;
@@ -1386,6 +1388,60 @@ var RocksDatabase = class RocksDatabase extends DBI {
1386
1388
  static config(options) {
1387
1389
  config(options);
1388
1390
  }
1391
+ /**
1392
+ * Registers a process-wide event listener. Internal events emitted by the
1393
+ * native binding use namespaced keys (e.g. `'transactionLog:warning'`).
1394
+ *
1395
+ * Listeners are not tied to any specific database — they fire for every
1396
+ * matching event emitted in this process.
1397
+ *
1398
+ * @example
1399
+ * ```typescript
1400
+ * RocksDatabase.on('transactionLog:warning', (warning) => {
1401
+ * console.warn(warning);
1402
+ * });
1403
+ * ```
1404
+ */
1405
+ static on(event, callback) {
1406
+ addGlobalListener(event, callback);
1407
+ }
1408
+ /**
1409
+ * Alias for {@link RocksDatabase.on}, mirroring the Node `EventEmitter` API.
1410
+ */
1411
+ static addListener(event, callback) {
1412
+ addGlobalListener(event, callback);
1413
+ }
1414
+ /**
1415
+ * Removes a previously-registered process-wide event listener. The
1416
+ * callback identity must match the one passed to {@link RocksDatabase.on}.
1417
+ *
1418
+ * @returns `true` if a matching listener was removed.
1419
+ */
1420
+ static off(event, callback) {
1421
+ return removeGlobalListener(event, callback);
1422
+ }
1423
+ /**
1424
+ * Alias for {@link RocksDatabase.off}, mirroring the Node `EventEmitter` API.
1425
+ */
1426
+ static removeListener(event, callback) {
1427
+ return removeGlobalListener(event, callback);
1428
+ }
1429
+ /**
1430
+ * Returns the number of process-wide listeners registered for the given event.
1431
+ */
1432
+ static listenerCount(event) {
1433
+ return globalListenerCount(event);
1434
+ }
1435
+ /**
1436
+ * Emits a process-wide event. Mostly intended for tests and as a peer to
1437
+ * {@link RocksDatabase.on} — native code should call `emitGlobalEvent` in
1438
+ * `napi/global_events.h` directly rather than round-tripping through JS.
1439
+ *
1440
+ * @returns `true` if there was at least one listener.
1441
+ */
1442
+ static notify(event, ...args) {
1443
+ return globalNotify(event, args);
1444
+ }
1389
1445
  destroy() {
1390
1446
  this.store.db.destroy();
1391
1447
  }
@@ -1861,7 +1917,6 @@ var RocksDatabase = class RocksDatabase extends DBI {
1861
1917
  //#region src/parse-transaction-log.ts
1862
1918
  const { TRANSACTION_LOG_TOKEN, TRANSACTION_LOG_ENTRY_HEADER_SIZE: TRANSACTION_LOG_ENTRY_HEADER_SIZE$1 } = constants;
1863
1919
  const MIN_VALID_TIMESTAMP = Date.UTC(2026, 0, 27);
1864
- const VALID_FLAGS_MASK = 1;
1865
1920
  /**
1866
1921
  * Loads an entire transaction log file into memory.
1867
1922
  * @param path - The path to the transaction log file.
@@ -1915,7 +1970,7 @@ function parseTransactionLog(path, options = {}) {
1915
1970
  const data = read(length);
1916
1971
  const entryAnomalies = [];
1917
1972
  if (!Number.isFinite(timestamp) || timestamp < MIN_VALID_TIMESTAMP) entryAnomalies.push(`timestamp ${timestamp} predates 2026-01-27 (possible corruption)`);
1918
- if ((flags & ~VALID_FLAGS_MASK) !== 0) entryAnomalies.push(`flags 0x${flags.toString(16).padStart(2, "0")} contains undefined bits (expected 0x00 or 0x01)`);
1973
+ if ((flags & -2) !== 0) entryAnomalies.push(`flags 0x${flags.toString(16).padStart(2, "0")} contains undefined bits (expected 0x00 or 0x01)`);
1919
1974
  const entry = {
1920
1975
  timestamp,
1921
1976
  length,
@@ -2132,7 +2187,7 @@ function loadLastPosition(transactionLog, readUncommitted) {
2132
2187
  //#region src/index.ts
2133
2188
  const versions = {
2134
2189
  rocksdb: version,
2135
- "rocksdb-js": "1.4.2"
2190
+ "rocksdb-js": "2.0.0"
2136
2191
  };
2137
2192
 
2138
2193
  //#endregion