@cratis/chronicle 6.54.0 → 6.56.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/ChronicleClient.ts +16 -3
- package/ChronicleOptions.ts +11 -0
- package/EventStore.ts +8 -3
- package/WellKnownTelemetryNames.ts +2 -0
- package/artifacts/refuseClientHostedObservers.ts +16 -0
- package/connection/ChronicleConnection.auth.spec.ts +2 -0
- package/connection/ChronicleConnection.ts +21 -9
- package/connection/ChronicleConnectionString.ts +26 -1
- package/connection/ClientObserversRequireKeepAlive.ts +10 -0
- package/connection/for_ChronicleConnection/when_deferring_client_creation/and_connecting.spec.ts +83 -0
- package/connection/for_TraceContextMiddleware/when_calling/with_active_context.spec.ts +1 -0
- package/connection/for_TraceContextMiddleware/when_calling/with_grpc_instrumentation.spec.ts +1 -0
- package/dist/ChronicleClient.d.ts +1 -0
- package/dist/ChronicleClient.d.ts.map +1 -1
- package/dist/ChronicleClient.js +16 -3
- package/dist/ChronicleClient.js.map +1 -1
- package/dist/ChronicleOptions.d.ts +4 -0
- package/dist/ChronicleOptions.d.ts.map +1 -1
- package/dist/ChronicleOptions.js +7 -0
- package/dist/ChronicleOptions.js.map +1 -1
- package/dist/EventStore.d.ts +2 -1
- package/dist/EventStore.d.ts.map +1 -1
- package/dist/EventStore.js +10 -3
- package/dist/EventStore.js.map +1 -1
- package/dist/WellKnownTelemetryNames.d.ts +2 -0
- package/dist/WellKnownTelemetryNames.d.ts.map +1 -1
- package/dist/WellKnownTelemetryNames.js +2 -0
- package/dist/WellKnownTelemetryNames.js.map +1 -1
- package/dist/artifacts/refuseClientHostedObservers.d.ts +4 -0
- package/dist/artifacts/refuseClientHostedObservers.d.ts.map +1 -0
- package/dist/artifacts/refuseClientHostedObservers.js +15 -0
- package/dist/artifacts/refuseClientHostedObservers.js.map +1 -0
- package/dist/connection/ChronicleConnection.d.ts +7 -1
- package/dist/connection/ChronicleConnection.d.ts.map +1 -1
- package/dist/connection/ChronicleConnection.js +13 -8
- package/dist/connection/ChronicleConnection.js.map +1 -1
- package/dist/connection/ChronicleConnectionString.d.ts +8 -0
- package/dist/connection/ChronicleConnectionString.d.ts.map +1 -1
- package/dist/connection/ChronicleConnectionString.js +21 -1
- package/dist/connection/ChronicleConnectionString.js.map +1 -1
- package/dist/connection/ClientObserversRequireKeepAlive.d.ts +7 -0
- package/dist/connection/ClientObserversRequireKeepAlive.d.ts.map +1 -0
- package/dist/connection/ClientObserversRequireKeepAlive.js +14 -0
- package/dist/connection/ClientObserversRequireKeepAlive.js.map +1 -0
- package/dist/eventSequences/EventSequence.d.ts +3 -0
- package/dist/eventSequences/EventSequence.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.js +29 -1
- package/dist/eventSequences/EventSequence.js.map +1 -1
- package/dist/eventSequences/IEventSequence.d.ts +12 -0
- package/dist/eventSequences/IEventSequence.d.ts.map +1 -1
- package/dist/eventSequences/ITransactionalEventSequence.d.ts +6 -0
- package/dist/eventSequences/ITransactionalEventSequence.d.ts.map +1 -1
- package/dist/eventSequences/TransactionalEventSequence.d.ts.map +1 -1
- package/dist/eventSequences/TransactionalEventSequence.js +3 -1
- package/dist/eventSequences/TransactionalEventSequence.js.map +1 -1
- package/dist/externalServices/ExternalServiceBuilder.js +1 -1
- package/dist/externalServices/ExternalServiceBuilder.js.map +1 -1
- package/dist/for_ChronicleClient/when_skipping_keep_alive/native-exit-probe.d.ts +2 -0
- package/dist/for_ChronicleClient/when_skipping_keep_alive/native-exit-probe.d.ts.map +1 -0
- package/dist/for_ChronicleClient/when_skipping_keep_alive/native-exit-probe.js +107 -0
- package/dist/for_ChronicleClient/when_skipping_keep_alive/native-exit-probe.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/reactors/Reactors.d.ts +2 -1
- package/dist/reactors/Reactors.d.ts.map +1 -1
- package/dist/reactors/Reactors.js +12 -3
- package/dist/reactors/Reactors.js.map +1 -1
- package/dist/readModels/for_ReadModels/given/a_stored_read_model.d.ts.map +1 -1
- package/dist/readModels/for_ReadModels/given/a_stored_read_model.js +1 -1
- package/dist/readModels/for_ReadModels/given/a_stored_read_model.js.map +1 -1
- package/dist/readModels/indexDecorator.js +1 -1
- package/dist/readModels/indexDecorator.js.map +1 -1
- package/dist/reducers/Reducers.d.ts +2 -1
- package/dist/reducers/Reducers.d.ts.map +1 -1
- package/dist/reducers/Reducers.js +8 -2
- package/dist/reducers/Reducers.js.map +1 -1
- package/dist/reducers/ReductionOutcome.d.ts.map +1 -1
- package/dist/reducers/ReductionOutcome.js +4 -1
- package/dist/reducers/ReductionOutcome.js.map +1 -1
- package/dist/testing/InProcessEventSequence.d.ts +2 -1
- package/dist/testing/InProcessEventSequence.d.ts.map +1 -1
- package/dist/testing/InProcessEventSequence.js +1 -0
- package/dist/testing/InProcessEventSequence.js.map +1 -1
- package/dist/transactions/AppendOptionNotSupportedInUnitOfWork.d.ts +13 -0
- package/dist/transactions/AppendOptionNotSupportedInUnitOfWork.d.ts.map +1 -0
- package/dist/transactions/AppendOptionNotSupportedInUnitOfWork.js +19 -0
- package/dist/transactions/AppendOptionNotSupportedInUnitOfWork.js.map +1 -0
- package/dist/transactions/ConflictingAppendOptionNamesInUnitOfWork.d.ts +14 -0
- package/dist/transactions/ConflictingAppendOptionNamesInUnitOfWork.d.ts.map +1 -0
- package/dist/transactions/ConflictingAppendOptionNamesInUnitOfWork.js +21 -0
- package/dist/transactions/ConflictingAppendOptionNamesInUnitOfWork.js.map +1 -0
- package/dist/transactions/IUnitOfWork.d.ts +3 -0
- package/dist/transactions/IUnitOfWork.d.ts.map +1 -1
- package/dist/transactions/TransactionalAppendOptions.d.ts +8 -0
- package/dist/transactions/TransactionalAppendOptions.d.ts.map +1 -1
- package/dist/transactions/UnitOfWork.d.ts.map +1 -1
- package/dist/transactions/UnitOfWork.js +4 -2
- package/dist/transactions/UnitOfWork.js.map +1 -1
- package/dist/transactions/concurrencyScopesAreEqual.d.ts.map +1 -1
- package/dist/transactions/concurrencyScopesAreEqual.js +4 -3
- package/dist/transactions/concurrencyScopesAreEqual.js.map +1 -1
- package/dist/transactions/index.d.ts +2 -0
- package/dist/transactions/index.d.ts.map +1 -1
- package/dist/transactions/index.js +2 -0
- package/dist/transactions/index.js.map +1 -1
- package/dist/transactions/resolveTransactionalAppendOptions.d.ts +8 -0
- package/dist/transactions/resolveTransactionalAppendOptions.d.ts.map +1 -0
- package/dist/transactions/resolveTransactionalAppendOptions.js +58 -0
- package/dist/transactions/resolveTransactionalAppendOptions.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/webhooks/WebhookDefinitionBuilder.d.ts.map +1 -1
- package/dist/webhooks/WebhookDefinitionBuilder.js +4 -2
- package/dist/webhooks/WebhookDefinitionBuilder.js.map +1 -1
- package/dist/webhooks/Webhooks.d.ts.map +1 -1
- package/dist/webhooks/Webhooks.js +2 -1
- package/dist/webhooks/Webhooks.js.map +1 -1
- package/eventSequences/EventSequence.spec.ts +55 -0
- package/eventSequences/EventSequence.ts +30 -1
- package/eventSequences/IEventSequence.ts +13 -0
- package/eventSequences/ITransactionalEventSequence.ts +6 -0
- package/eventSequences/TransactionalEventSequence.ts +3 -1
- package/eventSequences/for_TransactionalEventSequence/when_appending_many_with_append_option_names.spec.ts +26 -0
- package/eventSequences/for_TransactionalEventSequence/when_appending_many_with_unsupported_options.spec.ts +35 -0
- package/externalServices/ExternalServiceBuilder.ts +1 -1
- package/externalServices/ExternalServices.spec.ts +1 -1
- package/for_ChronicleClient/against_a_kernel/when_skipping_keep_alive.integration.spec.ts +80 -0
- package/for_ChronicleClient/when_skipping_keep_alive/and_connecting.spec.ts +158 -0
- package/for_ChronicleClient/when_skipping_keep_alive/native-exit-probe.ts +84 -0
- package/for_ChronicleClient/when_starting_background_work/with_a_correlated_caller.spec.ts +1 -0
- package/for_ChronicleOptions/when_configuring_keep_alive/with_skip_keep_alive.spec.ts +35 -0
- package/for_EventStore/when_skipping_keep_alive/when_registering_artifacts.spec.ts +51 -0
- package/index.ts +1 -0
- package/package.json +2 -2
- package/reactors/Reactors.ts +10 -3
- package/reactors/for_Reactors/when_skipping_keep_alive/with_a_reactor.spec.ts +48 -0
- package/readModels/for_ReadModels/given/a_stored_read_model.ts +1 -1
- package/readModels/indexDecorator.legacy.spec.ts +7 -7
- package/readModels/indexDecorator.ts +1 -1
- package/readModels/indexRegistration.spec.ts +4 -4
- package/readModels/standardFallbacks.standard.spec.ts +2 -2
- package/reducers/Reducers.ts +6 -2
- package/reducers/ReductionOutcome.ts +4 -1
- package/reducers/for_Reducers/when_skipping_keep_alive/with_a_reducer.spec.ts +53 -0
- package/telemetry/for_ChronicleTelemetry/given/a_client.fixture.ts +1 -0
- package/telemetry/for_ChronicleTelemetry/given/a_span_name_registry.fixture.ts +2 -0
- package/telemetry/for_ChronicleTelemetry/given/an_event_sequence.fixture.ts +2 -1
- package/telemetry/for_ChronicleTelemetry/when_naming_spans/with_convention_names.spec.ts +1 -0
- package/telemetry/for_ChronicleTelemetry/when_recording_identifiers/with_per_client_privacy.spec.ts +2 -1
- package/testing/InProcessEventSequence.ts +1 -0
- package/transactions/AppendOptionNotSupportedInUnitOfWork.ts +17 -0
- package/transactions/ConflictingAppendOptionNamesInUnitOfWork.ts +17 -0
- package/transactions/IUnitOfWork.ts +3 -0
- package/transactions/TransactionalAppendOptions.ts +11 -0
- package/transactions/UnitOfWork.ts +4 -2
- package/transactions/concurrencyScopesAreEqual.ts +4 -3
- package/transactions/for_UnitOfWork/when_adding_events_with_append_option_names/and_both_names_have_different_values.spec.ts +65 -0
- package/transactions/for_UnitOfWork/when_adding_events_with_append_option_names/and_both_names_have_the_same_value.spec.ts +22 -0
- package/transactions/for_UnitOfWork/when_adding_events_with_append_option_names/and_the_append_option_names_are_used.spec.ts +25 -0
- package/transactions/for_UnitOfWork/when_adding_events_with_concurrency_scopes/and_a_routing_field_is_empty_versus_named.spec.ts +40 -0
- package/transactions/for_UnitOfWork/when_adding_events_with_concurrency_scopes/and_routing_fields_are_undefined_or_empty.spec.ts +39 -0
- package/transactions/for_UnitOfWork/when_adding_events_with_unsupported_append_options/and_a_correlation_id_is_given.spec.ts +38 -0
- package/transactions/for_UnitOfWork/when_adding_events_with_unsupported_append_options/and_an_event_source_id_is_given.spec.ts +38 -0
- package/transactions/for_UnitOfWork/when_adding_events_with_unsupported_append_options/and_concurrency_scopes_are_given.spec.ts +38 -0
- package/transactions/for_UnitOfWork/when_adding_events_with_unsupported_append_options/and_the_option_is_undefined.spec.ts +24 -0
- package/transactions/index.ts +2 -0
- package/transactions/resolveTransactionalAppendOptions.ts +68 -0
- package/webhooks/WebhookDefinitionBuilder.ts +4 -2
- package/webhooks/Webhooks.ts +2 -1
|
@@ -46,8 +46,8 @@ describe('inferred read models with standard decorators', () => {
|
|
|
46
46
|
expect(registerMany).toHaveBeenCalledWith(expect.objectContaining({
|
|
47
47
|
ReadModels: [expect.objectContaining({
|
|
48
48
|
Indexes: [
|
|
49
|
-
{ PropertyPath: 'customerId' },
|
|
50
|
-
{ PropertyPath: 'lines.productId' }
|
|
49
|
+
{ PropertyPath: 'customerId', Properties: [] },
|
|
50
|
+
{ PropertyPath: 'lines.productId', Properties: [] }
|
|
51
51
|
]
|
|
52
52
|
})]
|
|
53
53
|
}));
|
package/reducers/Reducers.ts
CHANGED
|
@@ -33,6 +33,7 @@ import type { ClientArtifactsActivator } from '../artifacts/ClientArtifactsActiv
|
|
|
33
33
|
import type { ActivatedArtifact } from '../artifacts/ActivatedArtifact.js';
|
|
34
34
|
import { ArtifactKind } from '../artifacts/ArtifactKind.js';
|
|
35
35
|
import { ArtifactDelivery } from '../artifacts/ArtifactDelivery.js';
|
|
36
|
+
import { refuseClientHostedObservers } from '../artifacts/refuseClientHostedObservers.js';
|
|
36
37
|
import { withActivatedArtifact, runActivated } from '../artifacts/withActivatedArtifact.js';
|
|
37
38
|
|
|
38
39
|
/** Expression used to partition reducer observations by event source ID. */
|
|
@@ -134,7 +135,8 @@ export class Reducers implements IReducers {
|
|
|
134
135
|
private readonly _defaultSinkTypeId: string,
|
|
135
136
|
private readonly _eventStore?: IEventStore,
|
|
136
137
|
private readonly _artifactActivator?: ClientArtifactsActivator,
|
|
137
|
-
private readonly _readModelNamingPolicy?: ReadModelNamingPolicy
|
|
138
|
+
private readonly _readModelNamingPolicy?: ReadModelNamingPolicy,
|
|
139
|
+
private readonly _skipKeepAlive: boolean = false
|
|
138
140
|
) {
|
|
139
141
|
this._logger = createLogger('@cratis/chronicle/reducers', _connection.logger);
|
|
140
142
|
this._lifecycle = lifecycle;
|
|
@@ -167,6 +169,7 @@ export class Reducers implements IReducers {
|
|
|
167
169
|
|
|
168
170
|
/** @inheritdoc */
|
|
169
171
|
async register(): Promise<void> {
|
|
172
|
+
if (this._skipKeepAlive) refuseClientHostedObservers({ reactors: [], reducers: this._clientArtifacts.reducers });
|
|
170
173
|
if (this._registered || this._disposed) {
|
|
171
174
|
return;
|
|
172
175
|
}
|
|
@@ -377,7 +380,8 @@ export class Reducers implements IReducers {
|
|
|
377
380
|
EventSourceType: getEventSourceTypeFor(reducerType),
|
|
378
381
|
EventStreamType: getEventStreamTypeFor(reducerType)
|
|
379
382
|
}
|
|
380
|
-
}
|
|
383
|
+
},
|
|
384
|
+
PageDelivery: undefined
|
|
381
385
|
},
|
|
382
386
|
Value1: undefined
|
|
383
387
|
}
|
|
@@ -67,7 +67,10 @@ export class ReductionOutcome {
|
|
|
67
67
|
LastSuccessfulObservation: this.lastSuccessfullyObservedEvent,
|
|
68
68
|
ExceptionMessages: this.exceptionMessages,
|
|
69
69
|
ExceptionStackTrace: this.exceptionStackTrace,
|
|
70
|
-
ReadModelState: readModelState ?? ''
|
|
70
|
+
ReadModelState: readModelState ?? '',
|
|
71
|
+
DeliveryId: 0n,
|
|
72
|
+
RetryAfterMilliseconds: 0n,
|
|
73
|
+
Partitions: []
|
|
71
74
|
}
|
|
72
75
|
}
|
|
73
76
|
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { chai, describe, it, vi } from 'vitest';
|
|
5
|
+
import { Reducers } from '../../Reducers.js';
|
|
6
|
+
import { reducer } from '../../reducer.js';
|
|
7
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
8
|
+
import { ConnectionLifecycle } from '../../../connection/ConnectionLifecycle.js';
|
|
9
|
+
import { ChronicleConnection } from '../../../connection/ChronicleConnection.js';
|
|
10
|
+
import { ClientObserversRequireKeepAlive } from '../../../connection/ClientObserversRequireKeepAlive.js';
|
|
11
|
+
import { IClientArtifactsProvider } from '../../../artifacts/IClientArtifactsProvider.js';
|
|
12
|
+
|
|
13
|
+
chai.should();
|
|
14
|
+
@eventType('reducer-skip-item')
|
|
15
|
+
class Item {}
|
|
16
|
+
class Model {}
|
|
17
|
+
@reducer('late-reducer', undefined, Model)
|
|
18
|
+
class Observer { item() { return new Model(); } }
|
|
19
|
+
|
|
20
|
+
describe('when registering reducers while skipping keep alive', () => {
|
|
21
|
+
for (const scenario of ['initial', 'late', 'discovered', 'default']) {
|
|
22
|
+
it(`should enforce the keep alive requirement for ${scenario} registration`, async () => {
|
|
23
|
+
const observe = vi.fn().mockImplementation(async function* () {});
|
|
24
|
+
const registerMany = vi.fn().mockResolvedValue({});
|
|
25
|
+
const artifacts: IClientArtifactsProvider = { eventTypes: [Item], reducers: scenario === 'initial' ? [Observer] : [],
|
|
26
|
+
reactors: [], readModels: [Model], seeders: [], constraints: [], projections: [], webhooks: [], eventTypeMigrations: [], globalForHandlers: [] };
|
|
27
|
+
const lifecycle = new ConnectionLifecycle();
|
|
28
|
+
const connection = { reducers: { observe }, readModels: { registerMany } } as unknown as ChronicleConnection;
|
|
29
|
+
const manager = new Reducers(artifacts, connection, 'store', 'Default', lifecycle, 'sink',
|
|
30
|
+
undefined, undefined, undefined, scenario !== 'default');
|
|
31
|
+
try {
|
|
32
|
+
if (scenario !== 'initial') {
|
|
33
|
+
await manager.register();
|
|
34
|
+
artifacts.reducers.push(Observer);
|
|
35
|
+
await lifecycle.disconnected(() => {});
|
|
36
|
+
if (scenario === 'discovered') await manager.discover();
|
|
37
|
+
}
|
|
38
|
+
const [result] = await Promise.allSettled([manager.register()]);
|
|
39
|
+
result.status.should.equal(scenario === 'default' ? 'fulfilled' : 'rejected');
|
|
40
|
+
if (result.status === 'rejected') {
|
|
41
|
+
result.reason.should.be.instanceOf(ClientObserversRequireKeepAlive);
|
|
42
|
+
result.reason.observerKind.should.equal('Reducer');
|
|
43
|
+
result.reason.observerId.should.equal('late-reducer');
|
|
44
|
+
observe.mock.calls.should.have.lengthOf(0);
|
|
45
|
+
registerMany.mock.calls.should.have.lengthOf(0);
|
|
46
|
+
} else {
|
|
47
|
+
observe.mock.calls.should.have.lengthOf(1);
|
|
48
|
+
registerMany.mock.calls.should.have.lengthOf(1);
|
|
49
|
+
}
|
|
50
|
+
} finally { manager.dispose(); }
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
@@ -28,6 +28,7 @@ vi.mock('../../../connection/ChronicleConnection.js', () => ({
|
|
|
28
28
|
fromSequenceNumber: async () => ({ IsAuthorized: true, Data: [] }),
|
|
29
29
|
redact: async () => ({}),
|
|
30
30
|
redactForEventSource: async () => ({}),
|
|
31
|
+
copyEventsForEventSource: async () => ({ Response: { lo: 1n, hi: 2n } }),
|
|
31
32
|
completeStream: async () => ({ Response: { IsSuccess: true, SequenceNumber: 42n } })
|
|
32
33
|
};
|
|
33
34
|
}
|
|
@@ -11,6 +11,7 @@ export const legacySpans = {
|
|
|
11
11
|
getFromSequenceNumber: 'chronicle.event_sequences.get_from_sequence_number',
|
|
12
12
|
redact: 'chronicle.event_sequences.redact',
|
|
13
13
|
redactForEventSource: 'chronicle.event_sequences.redact_for_event_source',
|
|
14
|
+
copyForEventSource: 'chronicle.event_sequences.copy_for_event_source',
|
|
14
15
|
completeStream: 'chronicle.event_sequences.complete_stream',
|
|
15
16
|
getEventStore: 'chronicle.client.get_event_store',
|
|
16
17
|
getEventStores: 'chronicle.client.get_event_stores',
|
|
@@ -26,6 +27,7 @@ export const conventionSpans = {
|
|
|
26
27
|
getFromSequenceNumber: 'cratis.chronicle.client.event_sequence.get_from_sequence_number',
|
|
27
28
|
redact: 'cratis.chronicle.client.event_sequence.redact',
|
|
28
29
|
redactForEventSource: 'cratis.chronicle.client.event_sequence.redact_for_event_source',
|
|
30
|
+
copyForEventSource: 'cratis.chronicle.client.event_sequence.copy_for_event_source',
|
|
29
31
|
completeStream: 'cratis.chronicle.client.event_sequence.complete_stream',
|
|
30
32
|
getEventStore: 'cratis.chronicle.client.event_store.get',
|
|
31
33
|
getEventStores: 'cratis.chronicle.client.event_store.list',
|
|
@@ -21,7 +21,8 @@ export function eventSequence(telemetry?: ChronicleTelemetryOptions) {
|
|
|
21
21
|
forEventSourceIdAndEventTypes: vi.fn().mockResolvedValue({ IsAuthorized: true, Data: [] }),
|
|
22
22
|
fromSequenceNumber: vi.fn().mockResolvedValue({ IsAuthorized: true, Data: [] }),
|
|
23
23
|
redact: vi.fn().mockResolvedValue({}),
|
|
24
|
-
redactForEventSource: vi.fn().mockResolvedValue({})
|
|
24
|
+
redactForEventSource: vi.fn().mockResolvedValue({}),
|
|
25
|
+
copyEventsForEventSource: vi.fn().mockResolvedValue({ Response: { lo: 1n, hi: 2n } })
|
|
25
26
|
};
|
|
26
27
|
const connection = { eventSequences: services } as unknown as ChronicleConnection;
|
|
27
28
|
return { services, sequence: new EventSequence(EventSequenceId.eventLog, 'store', 'namespace', connection,
|
|
@@ -27,6 +27,7 @@ const operations = {
|
|
|
27
27
|
getFromSequenceNumber: (sequence: IEventSequence) => sequence.getFromSequenceNumber(sequenceNumber),
|
|
28
28
|
redact: (sequence: IEventSequence) => sequence.redact(sequenceNumber, 'reason'),
|
|
29
29
|
redactForEventSource: (sequence: IEventSequence) => sequence.redactForEventSource('source', 'reason'),
|
|
30
|
+
copyForEventSource: (sequence: IEventSequence) => sequence.copy('source', 'target'),
|
|
30
31
|
completeStream: (sequence: IEventSequence) => sequence.completeStream('stream-type', 'stream-id')
|
|
31
32
|
};
|
|
32
33
|
|
package/telemetry/for_ChronicleTelemetry/when_recording_identifiers/with_per_client_privacy.spec.ts
CHANGED
|
@@ -30,10 +30,11 @@ for (const [label, policy, expected] of [
|
|
|
30
30
|
await sequence.getForEventSourceIdAndEventTypes(source, [Recorded]);
|
|
31
31
|
await sequence.getFromSequenceNumber(new EventSequenceNumber(1n), source);
|
|
32
32
|
await sequence.redactForEventSource(source, 'private reason');
|
|
33
|
+
await sequence.copy(source, 'target');
|
|
33
34
|
services.append.mock.calls[0][0].EventSourceId.should.equal(source);
|
|
34
35
|
});
|
|
35
36
|
it('should apply the policy only to the canonical name on every operation', () => {
|
|
36
|
-
telemetry.spans.getFinishedSpans().should.have.lengthOf(
|
|
37
|
+
telemetry.spans.getFinishedSpans().should.have.lengthOf(9);
|
|
37
38
|
for (const span of telemetry.spans.getFinishedSpans()) {
|
|
38
39
|
should.equal(span.attributes['cratis.event_source.id'], expected);
|
|
39
40
|
should.equal(span.attributes['chronicle.event_source_id'], undefined);
|
|
@@ -395,6 +395,7 @@ export class InProcessEventSequence implements IEventSequence {
|
|
|
395
395
|
|
|
396
396
|
async redact(): Promise<void> { throw this.unsupported('redact', this.id.value, 'Redaction requires kernel storage.'); }
|
|
397
397
|
async redactForEventSource(): Promise<void> { throw this.unsupported('redactForEventSource', this.id.value, 'Redaction requires kernel storage.'); }
|
|
398
|
+
async copy(): Promise<Guid> { throw this.unsupported('copy', this.id.value, 'Copying events requires kernel jobs.'); }
|
|
398
399
|
async completeStream(): Promise<CompleteStreamResult> { throw this.unsupported('completeStream', this.id.value, 'Stream completion requires a kernel.'); }
|
|
399
400
|
|
|
400
401
|
private filterIds(types?: Constructor[]): string[] {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when an append option cannot be honored in a unit of work.
|
|
6
|
+
*/
|
|
7
|
+
export class AppendOptionNotSupportedInUnitOfWork extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* Initializes a new instance of the {@link AppendOptionNotSupportedInUnitOfWork} class.
|
|
10
|
+
* @param optionName - The unsupported append option name.
|
|
11
|
+
* @param guidance - How to supply the intended metadata instead.
|
|
12
|
+
*/
|
|
13
|
+
constructor(readonly optionName: string, guidance: string) {
|
|
14
|
+
super(`A unit of work cannot honour the append option '${optionName}'. ${guidance}`);
|
|
15
|
+
this.name = 'AppendOptionNotSupportedInUnitOfWork';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when an append option alias and its transactional name have different values.
|
|
6
|
+
*/
|
|
7
|
+
export class ConflictingAppendOptionNamesInUnitOfWork extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* Initializes a new instance of the {@link ConflictingAppendOptionNamesInUnitOfWork} class.
|
|
10
|
+
* @param appendOptionName - The ordinary append option name.
|
|
11
|
+
* @param transactionalOptionName - The transactional append option name.
|
|
12
|
+
*/
|
|
13
|
+
constructor(readonly appendOptionName: string, readonly transactionalOptionName: string) {
|
|
14
|
+
super(`The append options give both '${appendOptionName}' and '${transactionalOptionName}' with different values. Give only one of them.`);
|
|
15
|
+
this.name = 'ConflictingAppendOptionNamesInUnitOfWork';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -29,6 +29,9 @@ export interface IUnitOfWork {
|
|
|
29
29
|
* @param event - The event payload.
|
|
30
30
|
* @param options - Optional append metadata for this event: registered event source / stream routing, stream and source
|
|
31
31
|
* types, stream identifier, subject, occurrence time, tags, named tags and concurrency scope. Kept with the event until commit.
|
|
32
|
+
* Accepts streamType/streamId/sourceType as aliases of eventStreamType/eventStreamId/eventSourceType.
|
|
33
|
+
* @throws {@link ConflictingAppendOptionNamesInUnitOfWork} when both names have different values; the event is not added.
|
|
34
|
+
* @throws {@link AppendOptionNotSupportedInUnitOfWork} when an unsupported option is given; the event is not added.
|
|
32
35
|
* @throws {@link InvalidNamedTag} when a named tag is invalid; the event is not added.
|
|
33
36
|
*/
|
|
34
37
|
addEvent(eventSequenceId: EventSequenceId, eventSourceId: string, event: object, options?: TransactionalAppendOptions): void;
|
|
@@ -10,6 +10,8 @@ import type { TransactionalEventRouting } from './TransactionalEventRouting.js';
|
|
|
10
10
|
* Optional append metadata for an event added to a unit of work.
|
|
11
11
|
* Every value is kept with the event and carried to the append when the unit of work commits,
|
|
12
12
|
* where it is validated like any other append.
|
|
13
|
+
* `AppendOptions`' `correlationId`, `concurrencyScopes` and `eventSourceId` cannot be honored in a unit of work
|
|
14
|
+
* and throw {@link AppendOptionNotSupportedInUnitOfWork} when given.
|
|
13
15
|
*/
|
|
14
16
|
export interface TransactionalAppendOptions extends TransactionalEventRouting {
|
|
15
17
|
/** Optional stream type. When omitted or empty, the kernel selects the route. */
|
|
@@ -21,6 +23,15 @@ export interface TransactionalAppendOptions extends TransactionalEventRouting {
|
|
|
21
23
|
/** Optional source type. When omitted or empty, the kernel selects the route. */
|
|
22
24
|
readonly eventSourceType?: string;
|
|
23
25
|
|
|
26
|
+
/** Alias of {@link eventStreamType} (the `AppendOptions` name). Giving both with different values throws {@link ConflictingAppendOptionNamesInUnitOfWork}. */
|
|
27
|
+
readonly streamType?: string;
|
|
28
|
+
|
|
29
|
+
/** Alias of {@link eventStreamId} (the `AppendOptions` name). Giving both with different values throws {@link ConflictingAppendOptionNamesInUnitOfWork}. */
|
|
30
|
+
readonly streamId?: string;
|
|
31
|
+
|
|
32
|
+
/** Alias of {@link eventSourceType} (the `AppendOptions` name). Giving both with different values throws {@link ConflictingAppendOptionNamesInUnitOfWork}. */
|
|
33
|
+
readonly sourceType?: string;
|
|
34
|
+
|
|
24
35
|
/** Optional occurrence time. When omitted, the kernel supplies the timestamp. */
|
|
25
36
|
readonly occurred?: Date;
|
|
26
37
|
|
|
@@ -16,6 +16,7 @@ import type { ConcurrencyScope } from '../eventSequences/ConcurrencyScope.js';
|
|
|
16
16
|
import { ConflictingConcurrencyScopesInUnitOfWork } from './ConflictingConcurrencyScopesInUnitOfWork.js';
|
|
17
17
|
import { concurrencyScopesAreEqual } from './concurrencyScopesAreEqual.js';
|
|
18
18
|
import { toEventForEventSourceId } from './toEventForEventSourceId.js';
|
|
19
|
+
import { resolveTransactionalAppendOptions } from './resolveTransactionalAppendOptions.js';
|
|
19
20
|
import { IUnitOfWork } from './IUnitOfWork.js';
|
|
20
21
|
|
|
21
22
|
interface EventForEventSourceIdWithSequenceNumber {
|
|
@@ -56,8 +57,9 @@ export class UnitOfWork implements IUnitOfWork {
|
|
|
56
57
|
/** @inheritdoc */
|
|
57
58
|
addEvent(eventSequenceId: EventSequenceId, eventSourceId: string, event: object, options?: TransactionalAppendOptions): void {
|
|
58
59
|
this.throwIfCompleted();
|
|
59
|
-
const
|
|
60
|
-
const
|
|
60
|
+
const resolved = resolveTransactionalAppendOptions(options);
|
|
61
|
+
const eventForEventSourceId = toEventForEventSourceId(eventSourceId, event, resolved);
|
|
62
|
+
const scope = resolved?.concurrencyScope;
|
|
61
63
|
const scopesForSequence = this._concurrencyScopes.get(eventSequenceId.value) ?? new Map<string, ConcurrencyScope>();
|
|
62
64
|
if (scope !== undefined) {
|
|
63
65
|
const enrolled = scopesForSequence.get(eventSourceId);
|
|
@@ -14,10 +14,11 @@ export function concurrencyScopesAreEqual(first: ConcurrencyScope, second: Concu
|
|
|
14
14
|
const eventTypeKeys = (scope: ConcurrencyScope) =>
|
|
15
15
|
[...new Set((scope.eventTypes ?? []).map(_ => `${_.id.value}+${_.generation.value}`))].sort().join(',');
|
|
16
16
|
|
|
17
|
+
// Undefined and empty are the same on the wire, so they are the same here.
|
|
17
18
|
return first.sequenceNumber === second.sequenceNumber
|
|
18
19
|
&& (first.eventSourceId ?? false) === (second.eventSourceId ?? false)
|
|
19
|
-
&& first.eventStreamType === second.eventStreamType
|
|
20
|
-
&& first.eventStreamId === second.eventStreamId
|
|
21
|
-
&& first.eventSourceType === second.eventSourceType
|
|
20
|
+
&& (first.eventStreamType ?? '') === (second.eventStreamType ?? '')
|
|
21
|
+
&& (first.eventStreamId ?? '') === (second.eventStreamId ?? '')
|
|
22
|
+
&& (first.eventSourceType ?? '') === (second.eventSourceType ?? '')
|
|
22
23
|
&& eventTypeKeys(first) === eventTypeKeys(second);
|
|
23
24
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { EventSequenceId } from '../../../eventSequences/EventSequenceId.js';
|
|
6
|
+
import { ConflictingAppendOptionNamesInUnitOfWork } from '../../ConflictingAppendOptionNamesInUnitOfWork.js';
|
|
7
|
+
import type { UnitOfWork } from '../../UnitOfWork.js';
|
|
8
|
+
import { a_unit_of_work_with_a_recording_store } from '../given/a_unit_of_work_with_a_recording_store.js';
|
|
9
|
+
|
|
10
|
+
chai.should();
|
|
11
|
+
|
|
12
|
+
for (const [appendOptionName, transactionalOptionName] of [
|
|
13
|
+
['streamType', 'eventStreamType'],
|
|
14
|
+
['streamId', 'eventStreamId'],
|
|
15
|
+
['sourceType', 'eventSourceType']
|
|
16
|
+
]) {
|
|
17
|
+
describe(`when adding events and ${appendOptionName} and ${transactionalOptionName} have different values`, () => {
|
|
18
|
+
let unitOfWork: UnitOfWork;
|
|
19
|
+
let error: unknown;
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
({ unitOfWork } = a_unit_of_work_with_a_recording_store());
|
|
23
|
+
error = undefined;
|
|
24
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', { order: 0 });
|
|
25
|
+
try {
|
|
26
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', { order: 1 }, {
|
|
27
|
+
[appendOptionName]: 'private-alias-value',
|
|
28
|
+
[transactionalOptionName]: 'private-transactional-value',
|
|
29
|
+
concurrencyScope: { sequenceNumber: 2n }
|
|
30
|
+
});
|
|
31
|
+
} catch (caught) {
|
|
32
|
+
error = caught;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should reject the conflicting event', () => error!.should.be.instanceOf(ConflictingAppendOptionNamesInUnitOfWork));
|
|
37
|
+
it('should name the append option', () => (error as ConflictingAppendOptionNamesInUnitOfWork).appendOptionName.should.equal(appendOptionName));
|
|
38
|
+
it('should name the transactional option', () => (error as ConflictingAppendOptionNamesInUnitOfWork).transactionalOptionName.should.equal(transactionalOptionName));
|
|
39
|
+
it('should not disclose the alias value', () => (error as Error).message.should.not.contain('private-alias-value'));
|
|
40
|
+
it('should not disclose the transactional value', () => (error as Error).message.should.not.contain('private-transactional-value'));
|
|
41
|
+
it('should keep only the first event', () => unitOfWork.getEvents().should.deep.equal([{ order: 0 }]));
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('when adding an event after conflicting append option names were refused', () => {
|
|
46
|
+
let error: unknown;
|
|
47
|
+
|
|
48
|
+
beforeEach(() => {
|
|
49
|
+
const { unitOfWork } = a_unit_of_work_with_a_recording_store();
|
|
50
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', { order: 0 });
|
|
51
|
+
try {
|
|
52
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', { order: 1 }, {
|
|
53
|
+
streamId: 'private-alias-value', eventStreamId: 'private-transactional-value', concurrencyScope: { sequenceNumber: 2n }
|
|
54
|
+
});
|
|
55
|
+
} catch { /* The conflicting event is refused. */ }
|
|
56
|
+
error = undefined;
|
|
57
|
+
try {
|
|
58
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', { order: 2 }, { concurrencyScope: { sequenceNumber: 3n } });
|
|
59
|
+
} catch (caught) {
|
|
60
|
+
error = caught;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should not have enrolled the refused concurrency scope', () => chai.should().not.exist(error));
|
|
65
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { EventSequenceId } from '../../../eventSequences/EventSequenceId.js';
|
|
6
|
+
import type { EventForEventSourceId } from '../../../eventSequences/EventForEventSourceId.js';
|
|
7
|
+
import { a_unit_of_work_with_a_recording_store } from '../given/a_unit_of_work_with_a_recording_store.js';
|
|
8
|
+
|
|
9
|
+
chai.should();
|
|
10
|
+
|
|
11
|
+
describe('when adding events and both names have the same value', () => {
|
|
12
|
+
let entry: EventForEventSourceId;
|
|
13
|
+
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
const { unitOfWork, appends } = a_unit_of_work_with_a_recording_store();
|
|
16
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', {}, { streamId: 'order-1', eventStreamId: 'order-1' });
|
|
17
|
+
await unitOfWork.commit();
|
|
18
|
+
entry = appends[0].events[0];
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should accept the stream identifier', () => entry.eventStreamId!.should.equal('order-1'));
|
|
22
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { EventSequenceId } from '../../../eventSequences/EventSequenceId.js';
|
|
6
|
+
import type { EventForEventSourceId } from '../../../eventSequences/EventForEventSourceId.js';
|
|
7
|
+
import { a_unit_of_work_with_a_recording_store } from '../given/a_unit_of_work_with_a_recording_store.js';
|
|
8
|
+
|
|
9
|
+
chai.should();
|
|
10
|
+
|
|
11
|
+
describe('when adding events and the append option names are used', () => {
|
|
12
|
+
let batch: EventForEventSourceId[];
|
|
13
|
+
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
const { unitOfWork, appends } = a_unit_of_work_with_a_recording_store();
|
|
16
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', {}, { streamType: 'Orders', streamId: 'order-1', sourceType: 'Customer' });
|
|
17
|
+
await unitOfWork.commit();
|
|
18
|
+
batch = appends[0].events;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should keep the stream type', () => batch[0].eventStreamType!.should.equal('Orders'));
|
|
22
|
+
it('should keep the stream identifier', () => batch[0].eventStreamId!.should.equal('order-1'));
|
|
23
|
+
it('should keep the source type', () => batch[0].eventSourceType!.should.equal('Customer'));
|
|
24
|
+
it('should not leak alias keys', () => Object.keys(batch[0]).sort().should.deep.equal(['event', 'eventSourceId', 'eventSourceType', 'eventStreamId', 'eventStreamType']));
|
|
25
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { EventSequenceId } from '../../../eventSequences/EventSequenceId.js';
|
|
6
|
+
import type { ConcurrencyScope } from '../../../eventSequences/ConcurrencyScope.js';
|
|
7
|
+
import { ConflictingConcurrencyScopesInUnitOfWork } from '../../ConflictingConcurrencyScopesInUnitOfWork.js';
|
|
8
|
+
import { a_unit_of_work_with_a_recording_store } from '../given/a_unit_of_work_with_a_recording_store.js';
|
|
9
|
+
import type { UnitOfWork } from '../../UnitOfWork.js';
|
|
10
|
+
|
|
11
|
+
chai.should();
|
|
12
|
+
|
|
13
|
+
describe('when adding events with concurrency scopes and a routing field is empty versus named', () => {
|
|
14
|
+
let unitOfWork: UnitOfWork;
|
|
15
|
+
let errors: unknown[];
|
|
16
|
+
|
|
17
|
+
const tryAdd = (source: string, concurrencyScope: ConcurrencyScope) => {
|
|
18
|
+
try {
|
|
19
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, source, { source }, { concurrencyScope });
|
|
20
|
+
} catch (caught) {
|
|
21
|
+
errors.push(caught);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
({ unitOfWork } = a_unit_of_work_with_a_recording_store());
|
|
27
|
+
errors = [];
|
|
28
|
+
for (const field of ['eventStreamType', 'eventStreamId', 'eventSourceType'] as const) {
|
|
29
|
+
tryAdd(`${field}-undefined`, { sequenceNumber: 1n });
|
|
30
|
+
tryAdd(`${field}-undefined`, { sequenceNumber: 1n, [field]: 'Named' });
|
|
31
|
+
tryAdd(`${field}-empty`, { sequenceNumber: 1n, [field]: '' });
|
|
32
|
+
tryAdd(`${field}-empty`, { sequenceNumber: 1n, [field]: 'Named' });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should reject each conflicting event', () => {
|
|
37
|
+
errors.should.have.lengthOf(6);
|
|
38
|
+
errors.every(_ => _ instanceof ConflictingConcurrencyScopesInUnitOfWork).should.be.true;
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { EventSequenceId } from '../../../eventSequences/EventSequenceId.js';
|
|
6
|
+
import type { ConcurrencyScope } from '../../../eventSequences/ConcurrencyScope.js';
|
|
7
|
+
import { a_unit_of_work_with_a_recording_store, type RecordedAppend } from '../given/a_unit_of_work_with_a_recording_store.js';
|
|
8
|
+
|
|
9
|
+
chai.should();
|
|
10
|
+
|
|
11
|
+
describe('when adding events with concurrency scopes and the routing fields are undefined or empty', () => {
|
|
12
|
+
let appends: RecordedAppend[];
|
|
13
|
+
let errors: unknown[];
|
|
14
|
+
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
const recording = a_unit_of_work_with_a_recording_store();
|
|
17
|
+
errors = [];
|
|
18
|
+
const fields = ['eventStreamType', 'eventStreamId', 'eventSourceType'] as const;
|
|
19
|
+
for (const field of fields) {
|
|
20
|
+
const undefinedScope: ConcurrencyScope = { sequenceNumber: 1n };
|
|
21
|
+
const emptyScope: ConcurrencyScope = { sequenceNumber: 1n, [field]: '' };
|
|
22
|
+
const pairs = [[undefinedScope, emptyScope], [emptyScope, undefinedScope]];
|
|
23
|
+
pairs.forEach(([first, second], index) => {
|
|
24
|
+
const source = `${field}-${index}`;
|
|
25
|
+
try {
|
|
26
|
+
recording.unitOfWork.addEvent(EventSequenceId.eventLog, source, { order: 0 }, { concurrencyScope: first });
|
|
27
|
+
recording.unitOfWork.addEvent(EventSequenceId.eventLog, source, { order: 1 }, { concurrencyScope: second });
|
|
28
|
+
} catch (caught) {
|
|
29
|
+
errors.push(caught);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
await recording.unitOfWork.commit();
|
|
34
|
+
appends = recording.appends;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should not reject any event', () => errors.should.be.empty);
|
|
38
|
+
it('should append every event', () => appends[0].events.should.have.lengthOf(12));
|
|
39
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import type { AppendOptions } from '../../../eventSequences/AppendOptions.js';
|
|
6
|
+
import { EventSequenceId } from '../../../eventSequences/EventSequenceId.js';
|
|
7
|
+
import { AppendOptionNotSupportedInUnitOfWork } from '../../AppendOptionNotSupportedInUnitOfWork.js';
|
|
8
|
+
import { a_unit_of_work_with_a_recording_store, type RecordingUnitOfWork } from '../given/a_unit_of_work_with_a_recording_store.js';
|
|
9
|
+
|
|
10
|
+
chai.should();
|
|
11
|
+
|
|
12
|
+
describe('when adding events and a correlation identifier is given', () => {
|
|
13
|
+
let recording: RecordingUnitOfWork;
|
|
14
|
+
let error: unknown;
|
|
15
|
+
let buffered: ReadonlyArray<object>;
|
|
16
|
+
|
|
17
|
+
beforeEach(async () => {
|
|
18
|
+
recording = a_unit_of_work_with_a_recording_store();
|
|
19
|
+
const { unitOfWork } = recording;
|
|
20
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', { order: 0 });
|
|
21
|
+
const options: AppendOptions = { correlationId: 'private-correlation-id', subject: 's', concurrencyScope: { sequenceNumber: 2n } };
|
|
22
|
+
error = undefined;
|
|
23
|
+
try {
|
|
24
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', { order: 1 }, options);
|
|
25
|
+
} catch (caught) {
|
|
26
|
+
error = caught;
|
|
27
|
+
}
|
|
28
|
+
buffered = unitOfWork.getEvents();
|
|
29
|
+
await unitOfWork.commit();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should reject the unsupported option', () => error!.should.be.instanceOf(AppendOptionNotSupportedInUnitOfWork));
|
|
33
|
+
it('should name the option', () => (error as AppendOptionNotSupportedInUnitOfWork).optionName.should.equal('correlationId'));
|
|
34
|
+
it('should not disclose the value', () => (error as Error).message.should.not.contain('private-correlation-id'));
|
|
35
|
+
it('should keep only the first event', () => buffered.should.deep.equal([{ order: 0 }]));
|
|
36
|
+
it('should commit only the first event', () => recording.appends[0].events.should.deep.equal([{ eventSourceId: 'a-1', event: { order: 0 } }]));
|
|
37
|
+
it('should not enroll the refused scope', () => recording.appends[0].options.should.not.have.property('concurrencyScopes'));
|
|
38
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import type { AppendOptions } from '../../../eventSequences/AppendOptions.js';
|
|
6
|
+
import { EventSequenceId } from '../../../eventSequences/EventSequenceId.js';
|
|
7
|
+
import { AppendOptionNotSupportedInUnitOfWork } from '../../AppendOptionNotSupportedInUnitOfWork.js';
|
|
8
|
+
import { a_unit_of_work_with_a_recording_store, type RecordingUnitOfWork } from '../given/a_unit_of_work_with_a_recording_store.js';
|
|
9
|
+
|
|
10
|
+
chai.should();
|
|
11
|
+
|
|
12
|
+
describe('when adding events and an event source identifier is given', () => {
|
|
13
|
+
let recording: RecordingUnitOfWork;
|
|
14
|
+
let error: unknown;
|
|
15
|
+
let buffered: ReadonlyArray<object>;
|
|
16
|
+
|
|
17
|
+
beforeEach(async () => {
|
|
18
|
+
recording = a_unit_of_work_with_a_recording_store();
|
|
19
|
+
const { unitOfWork } = recording;
|
|
20
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', { order: 0 });
|
|
21
|
+
const options: AppendOptions = { eventSourceId: 'private-source-id', subject: 's', concurrencyScope: { sequenceNumber: 2n } };
|
|
22
|
+
error = undefined;
|
|
23
|
+
try {
|
|
24
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', { order: 1 }, options);
|
|
25
|
+
} catch (caught) {
|
|
26
|
+
error = caught;
|
|
27
|
+
}
|
|
28
|
+
buffered = unitOfWork.getEvents();
|
|
29
|
+
await unitOfWork.commit();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should reject the unsupported option', () => error!.should.be.instanceOf(AppendOptionNotSupportedInUnitOfWork));
|
|
33
|
+
it('should name the option', () => (error as AppendOptionNotSupportedInUnitOfWork).optionName.should.equal('eventSourceId'));
|
|
34
|
+
it('should not disclose the value', () => (error as Error).message.should.not.contain('private-source-id'));
|
|
35
|
+
it('should keep only the first event', () => buffered.should.deep.equal([{ order: 0 }]));
|
|
36
|
+
it('should commit only the first event', () => recording.appends[0].events.should.deep.equal([{ eventSourceId: 'a-1', event: { order: 0 } }]));
|
|
37
|
+
it('should not enroll the refused scope', () => recording.appends[0].options.should.not.have.property('concurrencyScopes'));
|
|
38
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import type { AppendOptions } from '../../../eventSequences/AppendOptions.js';
|
|
6
|
+
import { EventSequenceId } from '../../../eventSequences/EventSequenceId.js';
|
|
7
|
+
import { AppendOptionNotSupportedInUnitOfWork } from '../../AppendOptionNotSupportedInUnitOfWork.js';
|
|
8
|
+
import { a_unit_of_work_with_a_recording_store, type RecordingUnitOfWork } from '../given/a_unit_of_work_with_a_recording_store.js';
|
|
9
|
+
|
|
10
|
+
chai.should();
|
|
11
|
+
|
|
12
|
+
describe('when adding events and concurrency scopes are given', () => {
|
|
13
|
+
let recording: RecordingUnitOfWork;
|
|
14
|
+
let error: unknown;
|
|
15
|
+
let buffered: ReadonlyArray<object>;
|
|
16
|
+
|
|
17
|
+
beforeEach(async () => {
|
|
18
|
+
recording = a_unit_of_work_with_a_recording_store();
|
|
19
|
+
const { unitOfWork } = recording;
|
|
20
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', { order: 0 });
|
|
21
|
+
const options: AppendOptions = { concurrencyScopes: { 'private-source-id': { sequenceNumber: 7n } }, subject: 's', concurrencyScope: { sequenceNumber: 2n } };
|
|
22
|
+
error = undefined;
|
|
23
|
+
try {
|
|
24
|
+
unitOfWork.addEvent(EventSequenceId.eventLog, 'a-1', { order: 1 }, options);
|
|
25
|
+
} catch (caught) {
|
|
26
|
+
error = caught;
|
|
27
|
+
}
|
|
28
|
+
buffered = unitOfWork.getEvents();
|
|
29
|
+
await unitOfWork.commit();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should reject the unsupported option', () => error!.should.be.instanceOf(AppendOptionNotSupportedInUnitOfWork));
|
|
33
|
+
it('should name the option', () => (error as AppendOptionNotSupportedInUnitOfWork).optionName.should.equal('concurrencyScopes'));
|
|
34
|
+
it('should not disclose the value', () => (error as Error).message.should.not.contain('private-source-id'));
|
|
35
|
+
it('should keep only the first event', () => buffered.should.deep.equal([{ order: 0 }]));
|
|
36
|
+
it('should commit only the first event', () => recording.appends[0].events.should.deep.equal([{ eventSourceId: 'a-1', event: { order: 0 } }]));
|
|
37
|
+
it('should not enroll the refused scope', () => recording.appends[0].options.should.not.have.property('concurrencyScopes'));
|
|
38
|
+
});
|