@cratis/chronicle 6.64.0 → 6.66.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 +2 -1
- package/ChronicleOptions.ts +65 -23
- package/README.md +1 -1
- package/connection/ChronicleConnection.auth.spec.ts +3 -3
- package/connection/ChronicleConnection.ts +24 -7
- package/connection/ChronicleConnectionString.spec.ts +85 -0
- package/connection/ChronicleConnectionString.ts +186 -12
- package/connection/ChronicleTlsMaterial.spec.ts +160 -0
- package/connection/ChronicleTlsMaterial.ts +121 -0
- package/connection/CompatibilityPreflight.ts +31 -5
- package/connection/LeastConnectionsLoadBalancerStrategy.ts +4 -2
- package/connection/LoadBalancerStrategyFactory.ts +7 -2
- package/connection/TokenProvider.ts +4 -1
- package/connection/fetchOAuthAccessToken.ts +4 -2
- package/connection/for_ChronicleConnection/when_checking_compatibility/before_append.spec.ts +12 -1
- package/connection/for_ChronicleConnection/when_creating_clients/with_correlated_multi_host_callers.spec.ts +1 -1
- package/connection/for_ChronicleConnection/when_deferring_client_creation/and_connecting.spec.ts +1 -1
- package/connection/for_CompatibilityPreflight/when_verifying/with_concurrent_calls.spec.ts +2 -2
- package/connection/for_CompatibilityPreflight/when_verifying/with_kernel_versions.spec.ts +38 -0
- package/connection/for_CompatibilityPreflight/when_verifying/with_the_check_skipped.spec.ts +23 -0
- package/connection/for_TraceContextMiddleware/when_calling/with_active_context.spec.ts +1 -1
- package/connection/for_TraceContextMiddleware/when_calling/with_grpc_instrumentation.spec.ts +1 -1
- package/dist/ChronicleClient.d.ts.map +1 -1
- package/dist/ChronicleClient.js +2 -1
- package/dist/ChronicleClient.js.map +1 -1
- package/dist/ChronicleOptions.d.ts +18 -0
- package/dist/ChronicleOptions.d.ts.map +1 -1
- package/dist/ChronicleOptions.js +55 -23
- package/dist/ChronicleOptions.js.map +1 -1
- package/dist/connection/ChronicleConnection.d.ts +3 -0
- package/dist/connection/ChronicleConnection.d.ts.map +1 -1
- package/dist/connection/ChronicleConnection.js +21 -7
- package/dist/connection/ChronicleConnection.js.map +1 -1
- package/dist/connection/ChronicleConnectionString.d.ts +50 -1
- package/dist/connection/ChronicleConnectionString.d.ts.map +1 -1
- package/dist/connection/ChronicleConnectionString.js +154 -12
- package/dist/connection/ChronicleConnectionString.js.map +1 -1
- package/dist/connection/ChronicleTlsMaterial.d.ts +54 -0
- package/dist/connection/ChronicleTlsMaterial.d.ts.map +1 -0
- package/dist/connection/ChronicleTlsMaterial.js +88 -0
- package/dist/connection/ChronicleTlsMaterial.js.map +1 -0
- package/dist/connection/CompatibilityPreflight.d.ts +7 -1
- package/dist/connection/CompatibilityPreflight.d.ts.map +1 -1
- package/dist/connection/CompatibilityPreflight.js +24 -4
- package/dist/connection/CompatibilityPreflight.js.map +1 -1
- package/dist/connection/LeastConnectionsLoadBalancerStrategy.d.ts +2 -1
- package/dist/connection/LeastConnectionsLoadBalancerStrategy.d.ts.map +1 -1
- package/dist/connection/LeastConnectionsLoadBalancerStrategy.js +3 -2
- package/dist/connection/LeastConnectionsLoadBalancerStrategy.js.map +1 -1
- package/dist/connection/LoadBalancerStrategyFactory.d.ts +2 -1
- package/dist/connection/LoadBalancerStrategyFactory.d.ts.map +1 -1
- package/dist/connection/LoadBalancerStrategyFactory.js +3 -2
- package/dist/connection/LoadBalancerStrategyFactory.js.map +1 -1
- package/dist/connection/TokenProvider.d.ts +4 -1
- package/dist/connection/TokenProvider.d.ts.map +1 -1
- package/dist/connection/TokenProvider.js +5 -1
- package/dist/connection/TokenProvider.js.map +1 -1
- package/dist/connection/fetchOAuthAccessToken.d.ts +2 -1
- package/dist/connection/fetchOAuthAccessToken.d.ts.map +1 -1
- package/dist/connection/fetchOAuthAccessToken.js +3 -2
- package/dist/connection/fetchOAuthAccessToken.js.map +1 -1
- package/dist/eventSequences/EventSequence.d.ts +3 -0
- package/dist/eventSequences/EventSequence.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.js +12 -3
- package/dist/eventSequences/EventSequence.js.map +1 -1
- package/dist/eventSequences/IEventSequence.d.ts +2 -0
- package/dist/eventSequences/IEventSequence.d.ts.map +1 -1
- package/dist/eventSequences/NamedTagsWithRegisteredEventSourceNotSupported.d.ts +1 -1
- package/dist/eventSequences/NamedTagsWithRegisteredEventSourceNotSupported.d.ts.map +1 -1
- package/dist/eventSequences/NamedTagsWithRegisteredEventSourceNotSupported.js +3 -3
- package/dist/eventSequences/NamedTagsWithRegisteredEventSourceNotSupported.js.map +1 -1
- package/dist/eventSequences/ensureNamedTagBatchIsSupported.d.ts +2 -2
- package/dist/eventSequences/ensureNamedTagBatchIsSupported.d.ts.map +1 -1
- package/dist/eventSequences/ensureNamedTagBatchIsSupported.js +10 -3
- package/dist/eventSequences/ensureNamedTagBatchIsSupported.js.map +1 -1
- 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/testing/ReactorScenario.d.ts.map +1 -1
- package/dist/testing/ReactorScenario.js +0 -2
- package/dist/testing/ReactorScenario.js.map +1 -1
- package/dist/transactions/UnitOfWork.d.ts +1 -0
- package/dist/transactions/UnitOfWork.d.ts.map +1 -1
- package/dist/transactions/UnitOfWork.js +17 -11
- package/dist/transactions/UnitOfWork.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/eventSequences/EventSequence.ts +14 -3
- package/eventSequences/IEventSequence.ts +3 -0
- package/eventSequences/NamedTagsWithRegisteredEventSourceNotSupported.ts +3 -3
- package/eventSequences/ensureNamedTagBatchIsSupported.ts +11 -3
- package/eventSequences/for_EventSequence/when_appending_named_tags/given.fixture.ts +3 -2
- package/eventSequences/for_EventSequence/when_appending_named_tags/with_kernel_versions.spec.ts +92 -0
- package/eventSequences/for_EventSequence/when_appending_named_tags/with_registered_sources.spec.ts +28 -22
- package/for_ChronicleClient/connectTimeout.spec.ts +29 -0
- package/for_ChronicleClient/when_starting_background_work/with_a_correlated_caller.spec.ts +1 -1
- package/for_ChronicleOptions/when_configuring_keep_alive/with_skip_keep_alive.spec.ts +1 -1
- package/for_ChronicleOptions/when_configuring_the_connect_timeout/with_connect_timeout.spec.ts +23 -0
- package/for_index/when_importing_connection_helpers.spec.ts +13 -0
- package/index.ts +1 -0
- package/package.json +1 -1
- package/patterns/for_Patterns/when_querying_through_the_connection/after_resetting_the_channel.spec.ts +1 -1
- package/testing/ReactorScenario.ts +0 -3
- package/testing/for_ReactorScenario/when_recording_side_effects/with_named_tags.spec.ts +11 -13
- package/transactions/UnitOfWork.ts +18 -14
- package/transactions/for_UnitOfWork/when_committing_events_with_named_tags.spec.ts +87 -29
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
import { Constructor } from '@cratis/fundamentals';
|
|
12
12
|
import { prepareSingleAppend } from './prepareSingleAppend.js';
|
|
13
13
|
import { prepareBatchAppend } from './prepareBatchAppend.js';
|
|
14
|
+
import { ensureNamedTagBatchIsSupported } from './ensureNamedTagBatchIsSupported.js';
|
|
14
15
|
import type { PreparedBatchAppend } from './PreparedBatchAppend.js';
|
|
15
16
|
import type { PreparedSingleAppend } from './PreparedSingleAppend.js';
|
|
16
17
|
import type { WireBatchEvent } from './WireBatchEvent.js';
|
|
@@ -56,7 +57,6 @@ import type { ConcurrencyScope } from './ConcurrencyScope.js';
|
|
|
56
57
|
import type { IEventSources } from '../eventSources/IEventSources.js';
|
|
57
58
|
import { deriveConcurrencyScope, planDerivedGuards, resolveBatchRouting, resolveSingleRouting } from './resolveEventSourceRouting.js';
|
|
58
59
|
import { IUnitOfWorkManager } from '../transactions/IUnitOfWorkManager.js';
|
|
59
|
-
import { ensureNamedTagBatchIsSupported } from './ensureNamedTagBatchIsSupported.js';
|
|
60
60
|
import { beginEventImport } from '../imports/beginEventImport.js';
|
|
61
61
|
import { importEvents } from '../imports/importEvents.js';
|
|
62
62
|
import type { IEventImport } from '../imports/IEventImport.js';
|
|
@@ -210,6 +210,11 @@ export class EventSequence implements IEventSequence {
|
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
+
/** @inheritdoc */
|
|
214
|
+
getKernelVersion(): Promise<string | undefined> {
|
|
215
|
+
return this._connection.getKernelVersion();
|
|
216
|
+
}
|
|
217
|
+
|
|
213
218
|
/** @inheritdoc */
|
|
214
219
|
async appendMany(eventSourceId: string, events: object[], options?: AppendOptions): Promise<AppendResult[]>;
|
|
215
220
|
async appendMany(events: EventForEventSourceId[], options?: AppendOptions): Promise<AppendResult[]>;
|
|
@@ -219,11 +224,11 @@ export class EventSequence implements IEventSequence {
|
|
|
219
224
|
options?: AppendOptions
|
|
220
225
|
): Promise<AppendResult[]> {
|
|
221
226
|
const prepared = prepareBatchAppend(eventSourceIdOrEvents, eventsOrOptions, options);
|
|
222
|
-
const { eventsForEventSourceIds, appendOptions, correlationId, concurrencyScopes, eventsToAppend
|
|
227
|
+
const { eventsForEventSourceIds, appendOptions, correlationId, concurrencyScopes, eventsToAppend } = prepared;
|
|
223
228
|
|
|
224
229
|
const wireEvents: WireBatchEvent[] = eventsToAppend;
|
|
225
230
|
const routings = eventsForEventSourceIds.map(event => resolveBatchRouting(this._eventSources, event, appendOptions));
|
|
226
|
-
|
|
231
|
+
await this.verifyNamedTagBatch(prepared, routings);
|
|
227
232
|
const explicitIds = new Set([...concurrencyScopes].filter(([, scope]) => scope !== undefined).map(([id]) => id));
|
|
228
233
|
const derivedGuards = planDerivedGuards(routings, eventsForEventSourceIds, appendOptions?.streamId, explicitIds);
|
|
229
234
|
const distinctEventSourceIds = [...new Set(eventsForEventSourceIds.map(_ => _.eventSourceId))];
|
|
@@ -263,6 +268,12 @@ export class EventSequence implements IEventSequence {
|
|
|
263
268
|
}, correlationId);
|
|
264
269
|
}
|
|
265
270
|
|
|
271
|
+
private async verifyNamedTagBatch(prepared: PreparedBatchAppend, routings: ReturnType<typeof resolveBatchRouting>[]): Promise<void> {
|
|
272
|
+
if (prepared.namedTags.some(tags => tags.length > 0) && routings.some(routing => routing?.eventSource !== undefined)) {
|
|
273
|
+
ensureNamedTagBatchIsSupported(true, true, await this.getKernelVersion());
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
266
277
|
private applyBatchRouting(wireEvents: WireBatchEvent[], routings: ReturnType<typeof resolveBatchRouting>[]): void {
|
|
267
278
|
routings.forEach((routing, index) => {
|
|
268
279
|
if (!routing) return;
|
|
@@ -24,6 +24,9 @@ export interface IEventSequence {
|
|
|
24
24
|
/** The unique identifier of this event sequence. */
|
|
25
25
|
readonly id: EventSequenceId;
|
|
26
26
|
|
|
27
|
+
/** Gets the connected kernel version, when available, after checking contract compatibility. */
|
|
28
|
+
getKernelVersion?(): Promise<string | undefined>;
|
|
29
|
+
|
|
27
30
|
/** Transactional append operations for this event sequence. */
|
|
28
31
|
readonly transactional: ITransactionalEventSequence;
|
|
29
32
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// Copyright (c) Cratis. All rights reserved.
|
|
2
2
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
3
|
|
|
4
|
-
/** Named-tag batches
|
|
4
|
+
/** Named-tag batches require a kernel that preserves registered event source routing. */
|
|
5
5
|
export class NamedTagsWithRegisteredEventSourceNotSupported extends Error {
|
|
6
6
|
constructor() {
|
|
7
|
-
super('Batches and unit-of-work commits
|
|
8
|
-
'
|
|
7
|
+
super('Batches and unit-of-work commits combining non-empty named tags with registered event sources require kernel 19.37.4 or later. ' +
|
|
8
|
+
'The connected kernel version is older, unknown, or unparseable. ' +
|
|
9
9
|
'Use single append calls instead. See https://github.com/Cratis/Chronicle/issues/4603.');
|
|
10
10
|
this.name = 'NamedTagsWithRegisteredEventSourceNotSupported';
|
|
11
11
|
}
|
|
@@ -3,9 +3,17 @@
|
|
|
3
3
|
|
|
4
4
|
import { NamedTagsWithRegisteredEventSourceNotSupported } from './NamedTagsWithRegisteredEventSourceNotSupported.js';
|
|
5
5
|
|
|
6
|
-
/** Rejects
|
|
7
|
-
export function ensureNamedTagBatchIsSupported(hasNamedTags: boolean, hasRegisteredEventSource: boolean): void {
|
|
8
|
-
if (hasNamedTags && hasRegisteredEventSource) {
|
|
6
|
+
/** Rejects only affected batches when the kernel cannot be confirmed to preserve registered routing. */
|
|
7
|
+
export function ensureNamedTagBatchIsSupported(hasNamedTags: boolean, hasRegisteredEventSource: boolean, kernelVersion?: string): void {
|
|
8
|
+
if (hasNamedTags && hasRegisteredEventSource && !supportsNamedTagBatch(kernelVersion)) {
|
|
9
9
|
throw new NamedTagsWithRegisteredEventSourceNotSupported();
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
+
|
|
13
|
+
function supportsNamedTagBatch(kernelVersion?: string): boolean {
|
|
14
|
+
// Development images and release builds share the same numeric release version.
|
|
15
|
+
const version = /^(\d+)\.(\d+)\.(\d+)(?:[-+][0-9A-Za-z.+-]+)?$/.exec(kernelVersion ?? '');
|
|
16
|
+
const minimum = [19, 37, 4];
|
|
17
|
+
const difference = version?.slice(1).map((part, index) => Number(part) - minimum[index]).find(part => part !== 0) ?? 0;
|
|
18
|
+
return version !== null && difference >= 0;
|
|
19
|
+
}
|
|
@@ -30,9 +30,10 @@ export function a_sequence(eventSources?: IEventSources) {
|
|
|
30
30
|
const appendManyForEventSources = vi.fn().mockImplementation(batch);
|
|
31
31
|
const appendManyForEventSourcesWithNamedTags = vi.fn().mockImplementation(batch);
|
|
32
32
|
const tailSequenceNumber = vi.fn().mockResolvedValue({ Data: { SequenceNumber: 0n } });
|
|
33
|
-
const
|
|
33
|
+
const getKernelVersion = vi.fn().mockResolvedValue('19.37.4');
|
|
34
|
+
const connection = { getKernelVersion, eventSequences: { append, appendWithNamedTags, appendManyForEventSources, appendManyForEventSourcesWithNamedTags, tailSequenceNumber } } as unknown as ChronicleConnection;
|
|
34
35
|
return {
|
|
35
36
|
sequence: new EventSequence(EventSequenceId.eventLog, 'store', 'tenant', connection, {} as IUnitOfWorkManager, undefined, undefined, eventSources),
|
|
36
|
-
append, appendWithNamedTags, appendManyForEventSources, appendManyForEventSourcesWithNamedTags, tailSequenceNumber
|
|
37
|
+
append, appendWithNamedTags, appendManyForEventSources, appendManyForEventSourcesWithNamedTags, tailSequenceNumber, getKernelVersion
|
|
37
38
|
};
|
|
38
39
|
}
|
package/eventSequences/for_EventSequence/when_appending_named_tags/with_kernel_versions.spec.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
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 { describe, it, beforeEach } from 'vitest';
|
|
5
|
+
import { should } from 'chai';
|
|
6
|
+
import { NamedTag } from '../../../events/index.js';
|
|
7
|
+
import { NamedTagsWithRegisteredEventSourceNotSupported } from '../../NamedTagsWithRegisteredEventSourceNotSupported.js';
|
|
8
|
+
import { Account, a_sequence, NamedTagRecorded, registered_sources } from './given.fixture.js';
|
|
9
|
+
|
|
10
|
+
should();
|
|
11
|
+
|
|
12
|
+
const namedTags = [new NamedTag('tenant', 'alpha')];
|
|
13
|
+
const entry = () => ({ eventSourceId: 'source-1', event: new NamedTagRecorded(), eventSource: Account, namedTags });
|
|
14
|
+
|
|
15
|
+
for (const version of ['19.37.4', '19.37.4-development', '19.38.0', '20.0.0']) {
|
|
16
|
+
describe(`when appending named tags with a registered source on kernel ${version}`, () => {
|
|
17
|
+
let calls: ReturnType<typeof a_sequence>;
|
|
18
|
+
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
calls = a_sequence(registered_sources());
|
|
21
|
+
calls.getKernelVersion.mockResolvedValue(version);
|
|
22
|
+
await calls.sequence.appendMany([entry()]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should send the named-tag batch', () => calls.appendManyForEventSourcesWithNamedTags.mock.calls.should.have.lengthOf(1));
|
|
26
|
+
it('should preserve the registered source', () => calls.appendManyForEventSourcesWithNamedTags.mock.calls[0][0].Events[0].EventSource.should.equal('Account'));
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
for (const version of ['19.37.3', '19.36.99', '1.0.0', undefined, '', 'unknown']) {
|
|
31
|
+
describe(`when appending named tags with a registered source on kernel ${version ?? 'unknown'}`, () => {
|
|
32
|
+
let calls: ReturnType<typeof a_sequence>;
|
|
33
|
+
let error: unknown;
|
|
34
|
+
|
|
35
|
+
beforeEach(async () => {
|
|
36
|
+
calls = a_sequence(registered_sources());
|
|
37
|
+
calls.getKernelVersion.mockResolvedValue(version);
|
|
38
|
+
try {
|
|
39
|
+
await calls.sequence.appendMany([entry()]);
|
|
40
|
+
} catch (caught) {
|
|
41
|
+
error = caught;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should throw the public operation-specific error', () => (error instanceof NamedTagsWithRegisteredEventSourceNotSupported).should.equal(true));
|
|
46
|
+
it('should mention the required kernel', () => (error as Error).message.should.contain('19.37.4'));
|
|
47
|
+
it('should not send the batch', () => calls.appendManyForEventSourcesWithNamedTags.mock.calls.should.have.lengthOf(0));
|
|
48
|
+
it('should not send a legacy batch', () => calls.appendManyForEventSources.mock.calls.should.have.lengthOf(0));
|
|
49
|
+
it('should not read a concurrency tail', () => calls.tailSequenceNumber.mock.calls.should.have.lengthOf(0));
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe(`when appending unaffected batches on kernel ${version ?? 'unknown'}`, () => {
|
|
53
|
+
let calls: ReturnType<typeof a_sequence>;
|
|
54
|
+
|
|
55
|
+
beforeEach(async () => {
|
|
56
|
+
calls = a_sequence(registered_sources());
|
|
57
|
+
calls.getKernelVersion.mockResolvedValue(version);
|
|
58
|
+
await calls.sequence.appendMany([{ ...entry(), namedTags: [] }]);
|
|
59
|
+
await calls.sequence.appendMany([{ ...entry(), eventSource: undefined }]);
|
|
60
|
+
await calls.sequence.append('source-1', new NamedTagRecorded(), { eventSource: Account, namedTags });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should send the registered batch without tags', () => calls.appendManyForEventSources.mock.calls.should.have.lengthOf(1));
|
|
64
|
+
it('should send the named-tag batch without a registered source', () => calls.appendManyForEventSourcesWithNamedTags.mock.calls.should.have.lengthOf(1));
|
|
65
|
+
it('should send a single append with both', () => calls.appendWithNamedTags.mock.calls.should.have.lengthOf(1));
|
|
66
|
+
it('should not need the kernel version', () => calls.getKernelVersion.mock.calls.should.have.lengthOf(0));
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for (const sharedSource of [false, true]) {
|
|
71
|
+
describe(`when appending named tags and a ${sharedSource ? 'shared' : 'separate'} registered source on an older kernel`, () => {
|
|
72
|
+
let calls: ReturnType<typeof a_sequence>;
|
|
73
|
+
let error: unknown;
|
|
74
|
+
|
|
75
|
+
beforeEach(async () => {
|
|
76
|
+
calls = a_sequence(registered_sources());
|
|
77
|
+
calls.getKernelVersion.mockResolvedValue('19.37.3');
|
|
78
|
+
try {
|
|
79
|
+
if (sharedSource) {
|
|
80
|
+
await calls.sequence.appendMany('source-1', [new NamedTagRecorded()], { eventSource: Account, namedTags });
|
|
81
|
+
} else {
|
|
82
|
+
await calls.sequence.appendMany([{ ...entry(), eventSource: undefined }, { ...entry(), namedTags: [] }]);
|
|
83
|
+
}
|
|
84
|
+
} catch (caught) {
|
|
85
|
+
error = caught;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should reject the combination', () => (error instanceof NamedTagsWithRegisteredEventSourceNotSupported).should.equal(true));
|
|
90
|
+
it('should not send', () => calls.appendManyForEventSourcesWithNamedTags.mock.calls.should.have.lengthOf(0));
|
|
91
|
+
});
|
|
92
|
+
}
|
package/eventSequences/for_EventSequence/when_appending_named_tags/with_registered_sources.spec.ts
CHANGED
|
@@ -4,50 +4,56 @@
|
|
|
4
4
|
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
5
|
import { ConcurrencyDimensions } from '../../../eventSources/index.js';
|
|
6
6
|
import { NamedTag } from '../../../events/index.js';
|
|
7
|
-
import {
|
|
7
|
+
import type { AppendOptions, EventForEventSourceId } from '../../index.js';
|
|
8
8
|
import { Account, a_sequence, NamedTagRecorded, registered_sources } from './given.fixture.js';
|
|
9
9
|
|
|
10
10
|
chai.should();
|
|
11
11
|
const namedTags = [new NamedTag('batch', 'b-1')];
|
|
12
|
+
const tagged = [{ Name: 'batch', Value: 'b-1' }];
|
|
12
13
|
const entry = (options: Partial<EventForEventSourceId> = {}): EventForEventSourceId =>
|
|
13
14
|
({ eventSourceId: 'source', event: new NamedTagRecorded(), ...options });
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
{ description: '
|
|
20
|
-
{ description: '
|
|
16
|
+
type WireEvent = { EventSource: string; EventSourceType: string; NamedTags: Array<{ Name: string; Value: string }> };
|
|
17
|
+
type Batch = { description: string; events: EventForEventSourceId[]; options?: AppendOptions; registered: boolean[]; tags: Array<typeof tagged> };
|
|
18
|
+
|
|
19
|
+
const batches: Batch[] = [
|
|
20
|
+
{ description: 'tags and a registered source on the same entry', events: [entry({ eventSource: Account, namedTags })], registered: [true], tags: [tagged] },
|
|
21
|
+
{ description: 'a tagged entry before an untagged registered entry', events: [entry({ namedTags }), entry({ eventSource: 'Account' })], registered: [false, true], tags: [tagged, []] },
|
|
22
|
+
{ description: 'an untagged registered entry before a tagged entry', events: [entry({ eventSource: 'Account' }), entry({ namedTags })], registered: [true, false], tags: [[], tagged] },
|
|
23
|
+
{ description: 'shared tags and a registered entry', events: [entry({ eventSource: 'Account' })], options: { namedTags }, registered: [true], tags: [tagged] },
|
|
24
|
+
{ description: 'per-entry tags and a shared registered source', events: [entry({ namedTags })], options: { eventSource: Account }, registered: [true], tags: [tagged] }
|
|
21
25
|
];
|
|
22
26
|
|
|
23
27
|
for (const batch of batches) {
|
|
24
28
|
describe(`when appending a batch with ${batch.description}`, () => {
|
|
25
29
|
let calls: ReturnType<typeof a_sequence>;
|
|
26
|
-
let
|
|
30
|
+
let wireEvents: WireEvent[];
|
|
27
31
|
beforeEach(async () => {
|
|
28
32
|
calls = a_sequence(registered_sources(ConcurrencyDimensions.eventSourceId));
|
|
29
|
-
|
|
33
|
+
await calls.sequence.appendMany(batch.events, batch.options);
|
|
34
|
+
wireEvents = calls.appendManyForEventSourcesWithNamedTags.mock.calls[0][0].Events;
|
|
30
35
|
});
|
|
31
|
-
it('should
|
|
32
|
-
(error as Error).should.be.instanceOf(NamedTagsWithRegisteredEventSourceNotSupported));
|
|
33
|
-
it('should explain the kernel limitation', () => (error as Error).message.should.contain('drops registered event source routing'));
|
|
34
|
-
it('should link to the kernel issue', () => (error as Error).message.should.contain('https://github.com/Cratis/Chronicle/issues/4603'));
|
|
36
|
+
it('should send one named-tag batch', () => calls.appendManyForEventSourcesWithNamedTags.mock.calls.should.have.lengthOf(1));
|
|
35
37
|
it('should not send a plain batch', () => calls.appendManyForEventSources.mock.calls.should.have.lengthOf(0));
|
|
36
|
-
it('should
|
|
37
|
-
|
|
38
|
-
it('should
|
|
39
|
-
|
|
38
|
+
it('should keep the registered event source on every registered event', () =>
|
|
39
|
+
wireEvents.map(wireEvent => wireEvent.EventSource === 'Account').should.deep.equal(batch.registered));
|
|
40
|
+
it('should keep the registered event source type on every registered event', () =>
|
|
41
|
+
wireEvents.filter((_, index) => batch.registered[index]).every(wireEvent => wireEvent.EventSourceType === 'Account').should.be.true);
|
|
42
|
+
it('should carry the named tags of each event', () => wireEvents.map(wireEvent => wireEvent.NamedTags).should.deep.equal(batch.tags));
|
|
40
43
|
});
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
describe('when appending a same-source batch with shared named tags and registered routing', () => {
|
|
44
|
-
let
|
|
47
|
+
let calls: ReturnType<typeof a_sequence>;
|
|
48
|
+
let wireEvents: WireEvent[];
|
|
45
49
|
beforeEach(async () => {
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
calls = a_sequence(registered_sources());
|
|
51
|
+
await calls.sequence.appendMany('source', [new NamedTagRecorded(), new NamedTagRecorded()], { eventSource: Account, namedTags });
|
|
52
|
+
wireEvents = calls.appendManyForEventSourcesWithNamedTags.mock.calls[0][0].Events;
|
|
48
53
|
});
|
|
49
|
-
it('should
|
|
50
|
-
(
|
|
54
|
+
it('should keep the registered event source on every event', () =>
|
|
55
|
+
wireEvents.map(wireEvent => wireEvent.EventSource).should.deep.equal(['Account', 'Account']));
|
|
56
|
+
it('should carry the shared named tags on every event', () => wireEvents.map(wireEvent => wireEvent.NamedTags).should.deep.equal([tagged, tagged]));
|
|
51
57
|
});
|
|
52
58
|
|
|
53
59
|
describe('when appending a registered-source batch with empty named tags', () => {
|
|
@@ -0,0 +1,29 @@
|
|
|
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 { describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import { ChronicleClient } from '../ChronicleClient.js';
|
|
6
|
+
import { ChronicleOptions } from '../ChronicleOptions.js';
|
|
7
|
+
|
|
8
|
+
const connections = vi.hoisted(() => [] as Record<string, unknown>[]);
|
|
9
|
+
vi.mock('../connection/ChronicleConnection.js', () => ({
|
|
10
|
+
ChronicleConnection: class {
|
|
11
|
+
constructor(options: Record<string, unknown>) { connections.push(options); }
|
|
12
|
+
disconnect() {}
|
|
13
|
+
}
|
|
14
|
+
}));
|
|
15
|
+
vi.mock('../connection/KernelKeepAlive.js', () => ({ KernelKeepAlive: class { async start() {} } }));
|
|
16
|
+
|
|
17
|
+
describe('when creating a client with a connect timeout', () => {
|
|
18
|
+
it('should give the connection the default connect timeout', () => {
|
|
19
|
+
const client = new ChronicleClient(ChronicleOptions.fromConnectionString('chronicle://localhost:35000', { discoveryPatterns: [] }));
|
|
20
|
+
client.dispose();
|
|
21
|
+
expect(connections.at(-1)?.connectTimeout).toBe(10_000);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should give the connection the configured connect timeout', () => {
|
|
25
|
+
const client = new ChronicleClient(ChronicleOptions.fromConnectionString('chronicle://localhost:35000', { discoveryPatterns: [], connectTimeout: 1_234 }));
|
|
26
|
+
client.dispose();
|
|
27
|
+
expect(connections.at(-1)?.connectTimeout).toBe(1_234);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -60,7 +60,7 @@ beforeEach(async () => {
|
|
|
60
60
|
let connections = 0;
|
|
61
61
|
server.add(ConnectionServiceDefinition, {
|
|
62
62
|
...Object.fromEntries(Object.keys(ConnectionServiceDefinition.methods).map(name => [name, unused])),
|
|
63
|
-
checkCompatibility: async () => ({ IsCompatible: true, Incompatibilities: [], ServerVersion: '
|
|
63
|
+
checkCompatibility: async () => ({ IsCompatible: true, Incompatibilities: [], ServerVersion: '19.37.4' }),
|
|
64
64
|
connectionKeepAlive: async () => ({}),
|
|
65
65
|
connect: async function* (request, callContext) {
|
|
66
66
|
const first = ++connections === 1;
|
|
@@ -11,7 +11,7 @@ describe('when configuring keep alive', () => {
|
|
|
11
11
|
it('should preserve the default', () => ChronicleOptions.development().skipKeepAlive.should.be.false);
|
|
12
12
|
it('should accept the factory option', () => ChronicleOptions.development({ skipKeepAlive: true }).skipKeepAlive.should.be.true);
|
|
13
13
|
for (const [query, option, expected] of [
|
|
14
|
-
['true', undefined, true], ['true', false, true], ['false', false, false], ['false', true, true], ['TRUE', false,
|
|
14
|
+
['true', undefined, true], ['true', false, true], ['false', false, false], ['false', true, true], ['TRUE', false, true]
|
|
15
15
|
] as const) {
|
|
16
16
|
it(`should combine string ${query} and option ${option} with OR precedence`, () => {
|
|
17
17
|
ChronicleOptions.fromConnectionString(`chronicle://localhost:35000?skipKeepAlive=${query}`, {
|
package/for_ChronicleOptions/when_configuring_the_connect_timeout/with_connect_timeout.spec.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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 } from 'vitest';
|
|
5
|
+
import { ChronicleOptions } from '../../ChronicleOptions.js';
|
|
6
|
+
|
|
7
|
+
chai.should();
|
|
8
|
+
|
|
9
|
+
describe('when configuring the connect timeout', () => {
|
|
10
|
+
it('should wait 10 seconds by default', () =>
|
|
11
|
+
ChronicleOptions.fromConnectionString('chronicle://localhost:35000').connectTimeout.should.equal(10_000));
|
|
12
|
+
|
|
13
|
+
it('should use the configured timeout', () =>
|
|
14
|
+
ChronicleOptions.development({ connectTimeout: 2_500 }).connectTimeout.should.equal(2_500));
|
|
15
|
+
|
|
16
|
+
it('should accept the largest supported timeout', () =>
|
|
17
|
+
ChronicleOptions.development({ connectTimeout: 4_294_967_295 }).connectTimeout.should.equal(4_294_967_295));
|
|
18
|
+
|
|
19
|
+
for (const connectTimeout of [0, -1, 0.5, 4_294_967_296, Number.NaN, Number.POSITIVE_INFINITY]) {
|
|
20
|
+
it(`should reject ${connectTimeout}`, () =>
|
|
21
|
+
(() => ChronicleOptions.development({ connectTimeout })).should.throw(TypeError, 'connectTimeout'));
|
|
22
|
+
}
|
|
23
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
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 } from 'vitest';
|
|
5
|
+
import { ChronicleConnectionString, ChronicleOptions } from '../index.js';
|
|
6
|
+
|
|
7
|
+
chai.should();
|
|
8
|
+
|
|
9
|
+
describe('when importing connection helpers from the package root', () => {
|
|
10
|
+
it('should export the connection string', () =>
|
|
11
|
+
ChronicleOptions.fromConnectionString(new ChronicleConnectionString('chronicle://localhost:35001'))
|
|
12
|
+
.connectionString.serverAddress.port.should.equal(35001));
|
|
13
|
+
});
|
package/index.ts
CHANGED
|
@@ -34,6 +34,7 @@ export { RejectedChronicleCredentials } from './connection/RejectedChronicleCred
|
|
|
34
34
|
export { ClientObserversRequireKeepAlive } from './connection/ClientObserversRequireKeepAlive.js';
|
|
35
35
|
export type { IChronicleClient } from './IChronicleClient.js';
|
|
36
36
|
export { ChronicleOptions } from './ChronicleOptions.js';
|
|
37
|
+
export { ChronicleConnectionString } from './connection/ChronicleConnectionString.js';
|
|
37
38
|
export { EventStore } from './EventStore.js';
|
|
38
39
|
export type { IEventStore } from './IEventStore.js';
|
|
39
40
|
export { EventStoreName } from './EventStoreName.js';
|
package/package.json
CHANGED
|
@@ -25,7 +25,7 @@ describe('when querying patterns through an event store after resetting its conn
|
|
|
25
25
|
const unused = async () => { throw new Error('Unexpected RPC'); };
|
|
26
26
|
server.add(ConnectionServiceDefinition, {
|
|
27
27
|
...Object.fromEntries(Object.keys(ConnectionServiceDefinition.methods).map(name => [name, unused])),
|
|
28
|
-
checkCompatibility: async () => ({ IsCompatible: true, Incompatibilities: [], ServerVersion: '
|
|
28
|
+
checkCompatibility: async () => ({ IsCompatible: true, Incompatibilities: [], ServerVersion: '19.37.4' })
|
|
29
29
|
} as ServiceImplementation<typeof ConnectionServiceDefinition>);
|
|
30
30
|
server.add(PatternsDefinition, {
|
|
31
31
|
allPatterns: unused, allPatternScopes: unused, matchingPatterns: unused, patternsForScope: unused,
|
|
@@ -19,7 +19,6 @@ import { getDeclaredEventStreamTypeFor, getEventStreamTypeFor } from '../events/
|
|
|
19
19
|
import type { IEventLog } from '../eventSequences/IEventLog.js';
|
|
20
20
|
import type { EventForEventSourceId } from '../eventSequences/EventForEventSourceId.js';
|
|
21
21
|
import type { AppendOptions } from '../eventSequences/AppendOptions.js';
|
|
22
|
-
import { ensureNamedTagBatchIsSupported } from '../eventSequences/ensureNamedTagBatchIsSupported.js';
|
|
23
22
|
import { EventStoreName } from '../EventStoreName.js';
|
|
24
23
|
import { EventStoreNamespaceName } from '../EventStoreNamespaceName.js';
|
|
25
24
|
import type { IEventStore } from '../IEventStore.js';
|
|
@@ -380,8 +379,6 @@ export class ReactorScenario {
|
|
|
380
379
|
}
|
|
381
380
|
// Production validates named tags before appending, failing the delivery on an invalid tag; so does recording.
|
|
382
381
|
for (const target of events) mergeNamedTags(target.namedTags);
|
|
383
|
-
ensureNamedTagBatchIsSupported(events.some(event => mergeNamedTags(event.namedTags).length > 0),
|
|
384
|
-
events.some(event => event.eventSource !== undefined));
|
|
385
382
|
for (const target of events) {
|
|
386
383
|
this._effects.push(Object.freeze({ kind: 'event', value: target.event, target, triggeringContext: context,
|
|
387
384
|
handler, deliveryIndex }));
|
|
@@ -6,7 +6,6 @@ import { beforeEach, chai, describe, it } from 'vitest';
|
|
|
6
6
|
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
7
7
|
import { InvalidNamedTag } from '../../../events/InvalidNamedTag.js';
|
|
8
8
|
import { NamedTag } from '../../../events/NamedTag.js';
|
|
9
|
-
import { NamedTagsWithRegisteredEventSourceNotSupported } from '../../../eventSequences/index.js';
|
|
10
9
|
import type { EventContext } from '../../../events/EventContext.js';
|
|
11
10
|
import { reactor } from '../../../reactors/reactor.js';
|
|
12
11
|
import { ReactorScenario } from '../../ReactorScenario.js';
|
|
@@ -86,24 +85,23 @@ describe('when recording a side effect with an invalid named tag', () => {
|
|
|
86
85
|
(error as Error).should.be.instanceOf(InvalidNamedTag));
|
|
87
86
|
});
|
|
88
87
|
|
|
89
|
-
for (const [description, reactorType] of [
|
|
90
|
-
['the same entry', NamedTagRegisteredSourceReactor],
|
|
91
|
-
['a tagged entry before a registered entry', NamedTagCrossEntryReactor],
|
|
92
|
-
['a registered entry before a tagged entry', NamedTagCrossEntryReversedReactor]
|
|
88
|
+
for (const [description, reactorType, effectCount] of [
|
|
89
|
+
['the same entry', NamedTagRegisteredSourceReactor, 1],
|
|
90
|
+
['a tagged entry before a registered entry', NamedTagCrossEntryReactor, 2],
|
|
91
|
+
['a registered entry before a tagged entry', NamedTagCrossEntryReversedReactor, 2]
|
|
93
92
|
] as const) {
|
|
94
93
|
describe(`when recording named tags and a registered source in ${description}`, () => {
|
|
95
94
|
let scenario: ReactorScenario;
|
|
96
|
-
let error: unknown;
|
|
97
95
|
beforeEach(async () => {
|
|
98
96
|
scenario = new ReactorScenario(reactorType, { artifacts, constraints: 'disabled' });
|
|
99
|
-
|
|
97
|
+
await scenario.when.forEventSource('trigger').events(new NamedTagInput());
|
|
100
98
|
});
|
|
101
|
-
it('should
|
|
102
|
-
|
|
103
|
-
it('should
|
|
104
|
-
|
|
105
|
-
it('should
|
|
106
|
-
|
|
99
|
+
it('should complete the delivery', () => scenario.results[0].completed.should.be.true);
|
|
100
|
+
it('should record every returned side effect', () => scenario.sideEffects.should.have.lengthOf(effectCount));
|
|
101
|
+
it('should keep the registered source on the registered effect', () =>
|
|
102
|
+
scenario.sideEffects.some(effect => effect.target.eventSource === 'Account').should.be.true);
|
|
103
|
+
it('should keep the named tags on the tagged effect', () =>
|
|
104
|
+
scenario.sideEffects.flatMap(effect => effect.target.namedTags ?? []).map(tag => `${tag.name}=${tag.value}`).should.deep.equal(['origin=trigger']));
|
|
107
105
|
});
|
|
108
106
|
}
|
|
109
107
|
|
|
@@ -108,11 +108,6 @@ export class UnitOfWork implements IUnitOfWork {
|
|
|
108
108
|
this.throwIfCompleted();
|
|
109
109
|
|
|
110
110
|
if (this._events.length > 0) {
|
|
111
|
-
// Validate the entire commit before writing any sequence, including combinations in different groups.
|
|
112
|
-
ensureNamedTagBatchIsSupported(
|
|
113
|
-
this._events.some(entry => (entry.eventForEventSourceId.namedTags?.length ?? 0) > 0),
|
|
114
|
-
this._events.some(entry => entry.eventForEventSourceId.eventSource !== undefined)
|
|
115
|
-
);
|
|
116
111
|
const resultsBySequenceNumber = new Map<number, AppendResult>();
|
|
117
112
|
const eventsByEventSequence = new Map<string, EventForEventSourceIdWithSequenceNumber[]>();
|
|
118
113
|
|
|
@@ -123,15 +118,7 @@ export class UnitOfWork implements IUnitOfWork {
|
|
|
123
118
|
eventsByEventSequence.set(key, events);
|
|
124
119
|
}
|
|
125
120
|
|
|
126
|
-
|
|
127
|
-
const eventSources = this._eventStore.eventSources;
|
|
128
|
-
if (eventSources) {
|
|
129
|
-
for (const [eventSequenceId, eventsForSequence] of eventsByEventSequence) {
|
|
130
|
-
const events = eventsForSequence.map(_ => _.eventForEventSourceId);
|
|
131
|
-
const explicitIds = new Set(this._concurrencyScopes.get(eventSequenceId)?.keys() ?? []);
|
|
132
|
-
planDerivedGuards(events.map(_ => resolveBatchRouting(eventSources, _)), events, undefined, explicitIds);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
121
|
+
await this.validateBatches(eventsByEventSequence);
|
|
135
122
|
|
|
136
123
|
for (const [eventSequenceId, eventsForSequence] of eventsByEventSequence) {
|
|
137
124
|
const sequence = this._eventStore.getEventSequence(new EventSequenceId(eventSequenceId));
|
|
@@ -179,6 +166,23 @@ export class UnitOfWork implements IUnitOfWork {
|
|
|
179
166
|
};
|
|
180
167
|
}
|
|
181
168
|
|
|
169
|
+
private async validateBatches(eventsByEventSequence: Map<string, EventForEventSourceIdWithSequenceNumber[]>): Promise<void> {
|
|
170
|
+
const hasNamedTags = this._events.some(entry => (entry.eventForEventSourceId.namedTags?.length ?? 0) > 0);
|
|
171
|
+
const hasRegisteredEventSource = this._events.some(entry => entry.eventForEventSourceId.eventSource !== undefined);
|
|
172
|
+
if (hasNamedTags && hasRegisteredEventSource) {
|
|
173
|
+
ensureNamedTagBatchIsSupported(true, true, await this._eventStore.eventLog.getKernelVersion?.());
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Resolve routing and required guards for every group first: a rejection must not follow a written group.
|
|
177
|
+
const eventSources = this._eventStore.eventSources;
|
|
178
|
+
if (!eventSources) return;
|
|
179
|
+
for (const [eventSequenceId, eventsForSequence] of eventsByEventSequence) {
|
|
180
|
+
const events = eventsForSequence.map(_ => _.eventForEventSourceId);
|
|
181
|
+
const explicitIds = new Set(this._concurrencyScopes.get(eventSequenceId)?.keys() ?? []);
|
|
182
|
+
planDerivedGuards(events.map(_ => resolveBatchRouting(eventSources, _)), events, undefined, explicitIds);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
182
186
|
private throwIfCompleted(): void {
|
|
183
187
|
if (this._isCommitted) {
|
|
184
188
|
throw new Error(`Unit of work ${this.correlationId} is already committed.`);
|