event-storage 1.3.0 → 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 +145 -0
- package/index.js +28 -6
- package/package.json +3 -1
- package/src/EventStore.js +49 -10
- package/src/EventStream.js +29 -6
- package/src/Storage/ReadOnlyStorage.js +4 -2
- package/src/utils/deprecations.js +19 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
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 const VERSION: string;
|
|
34
|
+
|
|
35
|
+
export class EventStream extends Readable {
|
|
36
|
+
constructor(name: string, eventStore: EventStore, minRevision?: number, maxRevision?: number, predicate?: EventPredicate, raw?: boolean);
|
|
37
|
+
name: string;
|
|
38
|
+
raw: boolean;
|
|
39
|
+
version: number;
|
|
40
|
+
minRevision: number;
|
|
41
|
+
maxRevision: number;
|
|
42
|
+
streamIndex: StreamIndexInfo;
|
|
43
|
+
predicate: EventPredicate;
|
|
44
|
+
|
|
45
|
+
from(revision: number): this;
|
|
46
|
+
until(revision: number): this;
|
|
47
|
+
fromStart(): this;
|
|
48
|
+
fromEnd(): this;
|
|
49
|
+
previous(amount: number): this;
|
|
50
|
+
following(amount: number): this;
|
|
51
|
+
backwards(): this;
|
|
52
|
+
forwards(): this;
|
|
53
|
+
where(predicate?: EventPredicate): this;
|
|
54
|
+
filter(predicate?: EventPredicate): this;
|
|
55
|
+
filter(
|
|
56
|
+
predicate: (chunk: any, options?: { signal?: AbortSignal }) => boolean | Promise<boolean>,
|
|
57
|
+
options: { concurrency?: number; signal?: AbortSignal }
|
|
58
|
+
): Readable;
|
|
59
|
+
next(): any | false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export class Consumer extends Readable {
|
|
63
|
+
constructor(storage: Storage, indexName: string, identifier: string, initialState?: Record<string, unknown>, startFrom?: number);
|
|
64
|
+
streamName: string;
|
|
65
|
+
position: number;
|
|
66
|
+
state: Record<string, unknown>;
|
|
67
|
+
start(): this;
|
|
68
|
+
stop(): void;
|
|
69
|
+
reset(initialState?: Record<string, unknown>, startFrom?: number): this;
|
|
70
|
+
setState(state: Record<string, unknown>): this;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class Storage extends EventEmitter {
|
|
74
|
+
static ReadOnly: typeof Storage;
|
|
75
|
+
static StorageLockedError: typeof StorageLockedError;
|
|
76
|
+
static LOCK_THROW: number;
|
|
77
|
+
static LOCK_RECLAIM: number;
|
|
78
|
+
|
|
79
|
+
initialized: boolean | null;
|
|
80
|
+
length: number;
|
|
81
|
+
indexDirectory: string;
|
|
82
|
+
storageFile: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export class StorageLockedError extends Error {}
|
|
86
|
+
|
|
87
|
+
export class Index {
|
|
88
|
+
static ReadOnly: typeof Index;
|
|
89
|
+
static Entry: unknown;
|
|
90
|
+
|
|
91
|
+
length: number;
|
|
92
|
+
metadata?: Record<string, unknown> | null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export class EventStore extends EventEmitter {
|
|
96
|
+
static Storage: typeof Storage;
|
|
97
|
+
static Index: typeof Index;
|
|
98
|
+
static VERSION: string;
|
|
99
|
+
|
|
100
|
+
storage: Storage;
|
|
101
|
+
streams: Record<string, StreamInfo>;
|
|
102
|
+
consumers: Map<string, Consumer>;
|
|
103
|
+
length: number;
|
|
104
|
+
|
|
105
|
+
constructor(storeName?: string, config?: Record<string, unknown>);
|
|
106
|
+
|
|
107
|
+
open(callback?: (err?: Error | null) => void): void;
|
|
108
|
+
close(callback?: (err?: Error | null) => void): void;
|
|
109
|
+
|
|
110
|
+
getStreamVersion(streamName: string): number;
|
|
111
|
+
getEventStream(streamName: string, minRevision?: number, maxRevision?: number, predicate?: EventPredicate, raw?: boolean): EventStream | false;
|
|
112
|
+
getAllEvents(minRevision?: number, maxRevision?: number, predicate?: EventPredicate, raw?: boolean): EventStream;
|
|
113
|
+
fromStreams(streamName: string, streamNames: string[], minRevision?: number, maxRevision?: number, predicate?: EventPredicate, raw?: boolean): EventStream;
|
|
114
|
+
getEventStreamForCategory(categoryName: string, minRevision?: number, maxRevision?: number, predicate?: EventPredicate, raw?: boolean): EventStream;
|
|
115
|
+
|
|
116
|
+
query(types: string[], matcher?: EventMatcher | null, minRevision?: number, raw?: boolean): { stream: EventStream; condition: CommitCondition };
|
|
117
|
+
createEventStream(streamName: string, matcher: EventMatcher, reindex?: boolean): EventStream;
|
|
118
|
+
|
|
119
|
+
commit(
|
|
120
|
+
streamName: string,
|
|
121
|
+
events: any[],
|
|
122
|
+
expectedVersion: number | CommitCondition,
|
|
123
|
+
metadata: Record<string, unknown>,
|
|
124
|
+
callback: (error: Error | null, commit?: Record<string, unknown>) => void
|
|
125
|
+
): void;
|
|
126
|
+
|
|
127
|
+
getConsumer(streamName: string, identifier: string, initialState?: Record<string, unknown>, since?: number): Consumer;
|
|
128
|
+
getConsumer(identifier: string): Consumer | null;
|
|
129
|
+
scanConsumers(
|
|
130
|
+
callback: (error: Error | null, consumers: Array<{ name: string; stream: string; identifier: string }>) => void,
|
|
131
|
+
autoStart?: boolean
|
|
132
|
+
): void;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export const LOCK_THROW: number;
|
|
136
|
+
export const LOCK_RECLAIM: number;
|
|
137
|
+
|
|
138
|
+
export function matches(document: Record<string, unknown>, matcher: Record<string, unknown>): boolean;
|
|
139
|
+
export function buildRawBufferMatcher(matcher: Record<string, unknown>): (buffer: Buffer, startOffset?: number) => boolean;
|
|
140
|
+
|
|
141
|
+
export default EventStore;
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
package/index.js
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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.
|
|
3
|
+
"version": "1.3.2",
|
|
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
|
*/
|
|
@@ -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
|
-
|
|
137
|
+
const storage = storageConfig.readOnly === true
|
|
138
|
+
? new ReadOnlyStorage(storeName, storageConfig)
|
|
139
|
+
: new Storage(storeName, storageConfig);
|
|
143
140
|
|
|
144
|
-
this.storage
|
|
141
|
+
this.mountStorage(storage, () => {
|
|
145
142
|
this.checkUnfinishedCommits();
|
|
146
143
|
this.emit('ready');
|
|
147
144
|
});
|
|
145
|
+
}
|
|
148
146
|
|
|
149
|
-
|
|
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.
|
package/src/EventStream.js
CHANGED
|
@@ -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
|
|
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
|
|
254
|
-
*
|
|
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
|
-
|
|
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;
|
|
@@ -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
|
/**
|
|
@@ -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
|
+
|