event-storage 1.3.0 → 1.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/index.d.ts ADDED
@@ -0,0 +1,142 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import { Readable } from 'node:stream';
3
+
4
+ export type EventMatcher = Record<string, unknown> | ((payload: any, metadata?: any) => boolean);
5
+ export type EventPredicate = EventMatcher | ((buffer: Buffer) => boolean) | null;
6
+
7
+ export interface StreamIndexInfo {
8
+ length: number;
9
+ metadata?: Record<string, unknown> | null;
10
+ }
11
+
12
+ export interface StreamInfo {
13
+ index: StreamIndexInfo;
14
+ matcher?: EventMatcher;
15
+ closed?: boolean;
16
+ }
17
+
18
+ export class CommitCondition {
19
+ constructor(types: string[], matcher: EventMatcher | null | undefined, noneMatchAfter: number, raw?: boolean);
20
+ types: string[];
21
+ matcher: EventMatcher | null;
22
+ raw: boolean;
23
+ noneMatchAfter: number;
24
+ }
25
+
26
+ export class OptimisticConcurrencyError extends Error {}
27
+
28
+ export const ExpectedVersion: {
29
+ Any: -1;
30
+ EmptyStream: 0;
31
+ };
32
+
33
+ export class EventStream extends Readable {
34
+ constructor(name: string, eventStore: EventStore, minRevision?: number, maxRevision?: number, predicate?: EventPredicate, raw?: boolean);
35
+ name: string;
36
+ raw: boolean;
37
+ version: number;
38
+ minRevision: number;
39
+ maxRevision: number;
40
+ streamIndex: StreamIndexInfo;
41
+ predicate: EventPredicate;
42
+
43
+ from(revision: number): this;
44
+ until(revision: number): this;
45
+ fromStart(): this;
46
+ fromEnd(): this;
47
+ previous(amount: number): this;
48
+ following(amount: number): this;
49
+ backwards(): this;
50
+ forwards(): this;
51
+ where(predicate?: EventPredicate): this;
52
+ filter(predicate?: EventPredicate): this;
53
+ filter(
54
+ predicate: (chunk: any, options?: { signal?: AbortSignal }) => boolean | Promise<boolean>,
55
+ options: { concurrency?: number; signal?: AbortSignal }
56
+ ): Readable;
57
+ next(): any | false;
58
+ }
59
+
60
+ export class Consumer extends Readable {
61
+ constructor(storage: Storage, indexName: string, identifier: string, initialState?: Record<string, unknown>, startFrom?: number);
62
+ streamName: string;
63
+ position: number;
64
+ state: Record<string, unknown>;
65
+ start(): this;
66
+ stop(): void;
67
+ reset(initialState?: Record<string, unknown>, startFrom?: number): this;
68
+ setState(state: Record<string, unknown>): this;
69
+ }
70
+
71
+ export class Storage extends EventEmitter {
72
+ static ReadOnly: typeof Storage;
73
+ static StorageLockedError: typeof StorageLockedError;
74
+ static LOCK_THROW: number;
75
+ static LOCK_RECLAIM: number;
76
+
77
+ initialized: boolean | null;
78
+ length: number;
79
+ indexDirectory: string;
80
+ storageFile: string;
81
+ }
82
+
83
+ export class StorageLockedError extends Error {}
84
+
85
+ export class Index {
86
+ static ReadOnly: typeof Index;
87
+ static Entry: unknown;
88
+
89
+ length: number;
90
+ metadata?: Record<string, unknown> | null;
91
+ }
92
+
93
+ export class EventStore extends EventEmitter {
94
+ static Storage: typeof Storage;
95
+ static Index: typeof Index;
96
+
97
+ storage: Storage;
98
+ streams: Record<string, StreamInfo>;
99
+ consumers: Map<string, Consumer>;
100
+ length: number;
101
+
102
+ constructor(storeName?: string, config?: Record<string, unknown>);
103
+
104
+ open(callback?: (err?: Error | null) => void): void;
105
+ close(callback?: (err?: Error | null) => void): void;
106
+
107
+ getStreamVersion(streamName: string): number;
108
+ getEventStream(streamName: string, minRevision?: number, maxRevision?: number, predicate?: EventPredicate, raw?: boolean): EventStream | false;
109
+ getAllEvents(minRevision?: number, maxRevision?: number, predicate?: EventPredicate, raw?: boolean): EventStream;
110
+ fromStreams(streamName: string, streamNames: string[], minRevision?: number, maxRevision?: number, predicate?: EventPredicate, raw?: boolean): EventStream;
111
+ getEventStreamForCategory(categoryName: string, minRevision?: number, maxRevision?: number, predicate?: EventPredicate, raw?: boolean): EventStream;
112
+
113
+ query(types: string[], matcher?: EventMatcher | null, minRevision?: number, raw?: boolean): { stream: EventStream; condition: CommitCondition };
114
+ createEventStream(streamName: string, matcher: EventMatcher, reindex?: boolean): EventStream;
115
+
116
+ commit(
117
+ streamName: string,
118
+ events: any[],
119
+ expectedVersion: number | CommitCondition,
120
+ metadata: Record<string, unknown>,
121
+ callback: (error: Error | null, commit?: Record<string, unknown>) => void
122
+ ): void;
123
+
124
+ getConsumer(streamName: string, identifier: string, initialState?: Record<string, unknown>, since?: number): Consumer;
125
+ getConsumer(identifier: string): Consumer | null;
126
+ scanConsumers(
127
+ callback: (error: Error | null, consumers: Array<{ name: string; stream: string; identifier: string }>) => void,
128
+ autoStart?: boolean
129
+ ): void;
130
+ }
131
+
132
+ export const LOCK_THROW: number;
133
+ export const LOCK_RECLAIM: number;
134
+
135
+ export function matches(document: Record<string, unknown>, matcher: Record<string, unknown>): boolean;
136
+ export function buildRawBufferMatcher(matcher: Record<string, unknown>): (buffer: Buffer, startOffset?: number) => boolean;
137
+
138
+ export default EventStore;
139
+
140
+
141
+
142
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "event-storage",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "type": "module",
5
5
  "description": "An optimized embedded event store for node.js",
6
6
  "keywords": [
@@ -24,6 +24,7 @@
24
24
  "exports": {
25
25
  ".": "./index.js"
26
26
  },
27
+ "types": "./index.d.ts",
27
28
  "scripts": {
28
29
  "test": "c8 --reporter=lcov --reporter=text mocha test/*.spec.js",
29
30
  "test:grep": "c8 --reporter=lcov --reporter=text mocha test/*.spec.js --grep",
@@ -45,6 +46,7 @@
45
46
  "src/Storage/*.js",
46
47
  "src/WatchesFile.js",
47
48
  "index.js",
49
+ "index.d.ts",
48
50
  "src/utils/*.js"
49
51
  ],
50
52
  "license": "MIT",
package/src/EventStore.js CHANGED
@@ -40,7 +40,7 @@ class OptimisticConcurrencyError extends Error {}
40
40
  class CommitCondition {
41
41
  /**
42
42
  * @param {string[]} types
43
- * @param {function(object, object): boolean|object|null} [matcher]
43
+ * @param {(function(object, object): boolean)|object|null} [matcher]
44
44
  * @param {number} noneMatchAfter
45
45
  * @param {boolean} [raw=false]
46
46
  */
@@ -2,8 +2,10 @@ import stream from 'stream';
2
2
  import { assert } from './utils/util.js';
3
3
  import { buildRawBufferMatcher, matches } from './utils/metadataUtil.js';
4
4
  import { normalizeRevision, normalizeMaxRevision } from './utils/apiHelpers.js';
5
+ import { emitDeprecationWarningOnce } from './utils/deprecations.js';
5
6
 
6
7
  const NDJSON_NEWLINE = Buffer.from('\n');
8
+ const FILTER_MATCHER_DEPRECATION_CODE = 'EVENTSTREAM_FILTER_MATCHER_DEPRECATED';
7
9
 
8
10
  /**
9
11
  * An event stream is a simple wrapper around an iterator over storage documents.
@@ -245,16 +247,15 @@ class EventStream extends stream.Readable {
245
247
  }
246
248
 
247
249
  /**
248
- * Apply a filter predicate to this stream. Only events for which `predicate(payload, metadata)`
249
- * returns a truthy value will be yielded. The predicate is stored as a first-class property
250
- * of the stream and applied in {@link EventStream#next}.
250
+ * Apply matcher semantics to this EventStream instance (legacy `filter()` behavior).
251
251
  *
252
252
  * @api
253
- * @param {function(object, object): boolean} predicate A function receiving `(payload, metadata)`.
254
- * Events for which the predicate returns falsy are skipped.
253
+ * @param {(function(object, object): boolean)|(function(Buffer): boolean)|object|null} [predicate]
254
+ * Matcher function/object for object mode, or buffer matcher function/object for raw mode.
255
+ * Omit to clear the current predicate.
255
256
  * @returns {EventStream} `this`
256
257
  */
257
- filter(predicate) {
258
+ where(predicate) {
258
259
  this.predicate = predicate || null;
259
260
  this.rawMatcher = null;
260
261
  this._iterator = null;
@@ -262,6 +263,28 @@ class EventStream extends stream.Readable {
262
263
  return this;
263
264
  }
264
265
 
266
+ /**
267
+ * Readable-compatible filter entry point.
268
+ *
269
+ * - `filter(callback, options)` delegates to Node's `Readable.filter(...)`.
270
+ * - `filter(matcher)` keeps legacy EventStream matcher behavior and is deprecated.
271
+ *
272
+ * @api
273
+ * @param {function|object|null} [predicate]
274
+ * @param {{concurrency?: number, signal?: AbortSignal}|undefined} [options]
275
+ * @returns {stream.Readable|EventStream}
276
+ */
277
+ filter(predicate, options) {
278
+ if (options !== undefined) {
279
+ return super.filter(predicate, options);
280
+ }
281
+ emitDeprecationWarningOnce(
282
+ FILTER_MATCHER_DEPRECATION_CODE,
283
+ 'EventStream.filter() with matcher semantics is deprecated. Use EventStream.where() for matcher/object predicates or pass options to filter() to use Readable.filter().'
284
+ );
285
+ return this.where(predicate);
286
+ }
287
+
265
288
  matchesPredicate(entry) {
266
289
  if (!this.predicate) {
267
290
  return true;
@@ -0,0 +1,19 @@
1
+ const emittedDeprecationWarnings = new Set();
2
+
3
+ /**
4
+ * Emit a deprecation warning only once per warning code.
5
+ *
6
+ * @param {string} code Unique warning code.
7
+ * @param {string} message Warning message.
8
+ * @returns {void}
9
+ */
10
+ function emitDeprecationWarningOnce(code, message) {
11
+ if (emittedDeprecationWarnings.has(code)) {
12
+ return;
13
+ }
14
+ emittedDeprecationWarnings.add(code);
15
+ process.emitWarning(message, 'DeprecationWarning', code);
16
+ }
17
+
18
+ export { emitDeprecationWarningOnce };
19
+