event-storage 1.3.1 → 1.3.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.
package/index.d.ts CHANGED
@@ -30,6 +30,8 @@ export const ExpectedVersion: {
30
30
  EmptyStream: 0;
31
31
  };
32
32
 
33
+ export const VERSION: string;
34
+
33
35
  export class EventStream extends Readable {
34
36
  constructor(name: string, eventStore: EventStore, minRevision?: number, maxRevision?: number, predicate?: EventPredicate, raw?: boolean);
35
37
  name: string;
@@ -93,6 +95,7 @@ export class Index {
93
95
  export class EventStore extends EventEmitter {
94
96
  static Storage: typeof Storage;
95
97
  static Index: typeof Index;
98
+ static VERSION: string;
96
99
 
97
100
  storage: Storage;
98
101
  streams: Record<string, StreamInfo>;
package/index.js CHANGED
@@ -1,6 +1,28 @@
1
- export { default as EventStore, default, ExpectedVersion, OptimisticConcurrencyError, CommitCondition, LOCK_THROW, LOCK_RECLAIM } from './src/EventStore.js';
2
- export { default as EventStream } from './src/EventStream.js';
3
- export { default as Storage, StorageLockedError } from './src/Storage.js';
4
- export { default as Index } from './src/Index.js';
5
- export { default as Consumer } from './src/Consumer.js';
6
- export { matches, buildRawBufferMatcher } from './src/utils/metadataUtil.js';
1
+ import packageJson from './package.json' with { type: 'json' };
2
+ import EventStore, { ExpectedVersion, OptimisticConcurrencyError, CommitCondition, LOCK_THROW, LOCK_RECLAIM } from './src/EventStore.js';
3
+ import EventStream from './src/EventStream.js';
4
+ import Storage, { StorageLockedError } from './src/Storage.js';
5
+ import Index from './src/Index.js';
6
+ import Consumer from './src/Consumer.js';
7
+ import { matches, buildRawBufferMatcher } from './src/utils/metadataUtil.js';
8
+
9
+ const VERSION = packageJson.version;
10
+ EventStore.VERSION = VERSION;
11
+
12
+ export {
13
+ EventStore,
14
+ EventStore as default,
15
+ ExpectedVersion,
16
+ OptimisticConcurrencyError,
17
+ CommitCondition,
18
+ LOCK_THROW,
19
+ LOCK_RECLAIM,
20
+ VERSION,
21
+ EventStream,
22
+ Storage,
23
+ StorageLockedError,
24
+ Index,
25
+ Consumer,
26
+ matches,
27
+ buildRawBufferMatcher
28
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "event-storage",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "type": "module",
5
5
  "description": "An optimized embedded event store for node.js",
6
6
  "keywords": [
package/src/EventStore.js CHANGED
@@ -129,24 +129,34 @@ class EventStore extends events.EventEmitter {
129
129
  * @param {object} storageConfig
130
130
  */
131
131
  initialize(storeName, storageConfig) {
132
+ this.storageConfig = storageConfig;
132
133
  this.streamsDirectory = path.resolve(storageConfig.indexDirectory);
133
-
134
134
  this.storeName = storeName;
135
- this.storage = (storageConfig.readOnly === true) ?
136
- new ReadOnlyStorage(storeName, storageConfig)
137
- : new Storage(storeName, storageConfig);
138
- this.streams = Object.create(null);
139
- this.streams._all = { index: this.storage.index };
140
135
  this.consumers = new Map();
141
136
 
142
- this.storage.on('index-created', this.registerStream.bind(this));
137
+ const storage = storageConfig.readOnly === true
138
+ ? new ReadOnlyStorage(storeName, storageConfig)
139
+ : new Storage(storeName, storageConfig);
143
140
 
144
- this.storage.on('opened', () => {
141
+ this.mountStorage(storage, () => {
145
142
  this.checkUnfinishedCommits();
146
143
  this.emit('ready');
147
144
  });
145
+ }
148
146
 
149
- this.storage.open();
147
+ /**
148
+ * Wire up a storage instance: reset the streams map, attach the index-created listener,
149
+ * register a one-shot opened handler, then open the storage.
150
+ * @private
151
+ * @param {ReadableStorage} storage
152
+ * @param {function} [onOpened] Called once when the storage is opened.
153
+ */
154
+ mountStorage(storage, onOpened) {
155
+ this.storage = storage;
156
+ this.streams = Object.create(null);
157
+ this.streams._all = { index: this.storage.index };
158
+ this.storage.on('index-created', this.registerStream.bind(this));
159
+ this.storage.open(onOpened);
150
160
  }
151
161
 
152
162
  /**
@@ -231,6 +241,35 @@ class EventStore extends events.EventEmitter {
231
241
  this.storage.close();
232
242
  }
233
243
 
244
+ /**
245
+ * Flush all pending writes, then re-open the store in read-only mode.
246
+ * Any registered consumers are stopped. The `callback` is invoked once the
247
+ * new read-only storage has finished opening.
248
+ *
249
+ * Does not re-emit `'ready'` — use the callback to react to the transition.
250
+ *
251
+ * @api
252
+ * @param {function} [callback] Called when the store is ready in read-only mode.
253
+ */
254
+ makeReadOnly(callback) {
255
+ if (this.storage instanceof ReadOnlyStorage) {
256
+ callback?.();
257
+ return
258
+ }
259
+ for (const consumer of this.consumers.values()) {
260
+ consumer.stop();
261
+ }
262
+ this.consumers.clear();
263
+
264
+ this.storage.flush();
265
+ this.storage.close();
266
+
267
+ const readOnlyConfig = Object.assign({}, this.storageConfig, { readOnly: true });
268
+ this.storageConfig = readOnlyConfig;
269
+
270
+ this.mountStorage(new ReadOnlyStorage(this.storeName, readOnlyConfig), callback);
271
+ }
272
+
234
273
  /**
235
274
  * Override EventEmitter.on() to delegate 'preCommit' and 'preRead' event registrations
236
275
  * to the underlying storage, so that `eventstore.on('preCommit', handler)` works naturally.
@@ -31,14 +31,16 @@ class ReadOnlyStorage extends ReadableStorage {
31
31
  * Will emit an 'opened' event if finished.
32
32
  *
33
33
  * @api
34
+ * @param {function(): void} [callback] Called after indexes open, before `'opened'` is emitted.
35
+ * Can be used as a synchronous alternative to listening to the `'opened'` event.
34
36
  * @returns {boolean}
35
37
  */
36
- open() {
38
+ open(callback) {
37
39
  if (!this.watcher) {
38
40
  this.watcher = new Watcher([this.dataDirectory, this.indexDirectory], this.storageFilesFilter);
39
41
  this.watcher.on('rename', this.onStorageFileChanged);
40
42
  }
41
- return super.open();
43
+ return super.open(callback);
42
44
  }
43
45
 
44
46
  /**