event-storage 1.3.3 → 1.3.5

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": "event-storage",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "type": "module",
5
5
  "description": "An optimized embedded event store for node.js",
6
6
  "keywords": [
@@ -193,15 +193,15 @@ class EventStream extends stream.Readable {
193
193
  * @returns {Array<object>}
194
194
  */
195
195
  get events() {
196
- if (this._events instanceof Array) {
197
- return this._events;
196
+ if (this.cachedEvents instanceof Array) {
197
+ return this.cachedEvents;
198
198
  }
199
- this._events = [];
199
+ this.cachedEvents = [];
200
200
  let next;
201
201
  while ((next = this.next()) !== false) {
202
- this._events.push(next.payload);
202
+ this.cachedEvents.push(next.payload);
203
203
  }
204
- return this._events;
204
+ return this.cachedEvents;
205
205
  }
206
206
 
207
207
  /**
@@ -242,7 +242,7 @@ class EventStream extends stream.Readable {
242
242
  */
243
243
  reset() {
244
244
  this._iterator = null;
245
- this._events = null;
245
+ this.cachedEvents = null;
246
246
  return this;
247
247
  }
248
248
 
@@ -259,7 +259,7 @@ class EventStream extends stream.Readable {
259
259
  this.predicate = predicate || null;
260
260
  this.rawMatcher = null;
261
261
  this._iterator = null;
262
- this._events = null;
262
+ this.cachedEvents = null;
263
263
  return this;
264
264
  }
265
265
 
@@ -5,6 +5,23 @@ import { normalizeRevision } from './utils/apiHelpers.js';
5
5
  /** Reusable sentinel used for missing or empty per-stream iterators. */
6
6
  const emptyIterator = Object.freeze({ next() { return { done: true }; } });
7
7
 
8
+ /**
9
+ * In raw mode `storage.readFrom` returns a `buffer` that is a transient view into a shared read/write
10
+ * buffer, only valid until the next read on that partition. The k-way merge holds the head of every
11
+ * per-stream iterator at once, so advancing one stream would otherwise clobber another stream's pending
12
+ * head when they read from the same partition. Copy each raw buffer into its own memory as it is produced
13
+ * so every held head stays valid across reads.
14
+ *
15
+ * @param {Iterator<{ buffer: Buffer }>} iterator
16
+ * @returns {Generator<{ buffer: Buffer }>}
17
+ */
18
+ function* detachRawBuffers(iterator) {
19
+ for (const entry of iterator) {
20
+ entry.buffer = Buffer.from(entry.buffer);
21
+ yield entry;
22
+ }
23
+ }
24
+
8
25
  /**
9
26
  * An event stream is a simple wrapper around an iterator over storage documents.
10
27
  * It implements a node readable stream interface.
@@ -34,16 +51,22 @@ class JoinEventStream extends EventStream {
34
51
  if (!streamIndex || streamIndex.length === 0) {
35
52
  return emptyIterator;
36
53
  }
37
- const from = streamIndex.find(this.minRevision, this.minRevision <= this.maxRevision);
38
- const until = streamIndex.find(this.maxRevision, this.minRevision > this.maxRevision);
39
- if (from === 0 || until === 0) {
54
+ const ascending = this.minRevision <= this.maxRevision;
55
+ const from = streamIndex.find(this.minRevision, ascending);
56
+ const until = streamIndex.find(this.maxRevision, !ascending);
57
+ if (
58
+ from === 0 ||
59
+ until === 0 ||
60
+ (ascending ? from > until : from < until)
61
+ ) {
40
62
  // find() returns 0 when the requested revision is outside the stream's range
41
63
  // (e.g. minRevision > all entries, or maxRevision < all entries).
42
64
  return emptyIterator;
43
65
  }
44
66
  // Raw mode: get { buffer, time64, sequenceNumber } for binary-header ordering.
45
67
  // Object mode: storage deserializes for us and we order by metadata.commitId.
46
- return eventStore.storage.readRange(from, until, streamIndex, this.raw);
68
+ const range = eventStore.storage.readRange(from, until, streamIndex, this.raw);
69
+ return this.raw ? detachRawBuffers(range) : range;
47
70
  });
48
71
  }
49
72
  this._iterator = null;