@fabricorg/platform 0.1.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -61
- package/dist/actions/index.cjs +48 -0
- package/dist/actions/index.cjs.map +1 -0
- package/dist/actions/index.d.cts +116 -0
- package/dist/adapters/index.cjs +28 -0
- package/dist/adapters/index.cjs.map +1 -0
- package/dist/adapters/index.d.cts +83 -0
- package/dist/chunk-7KJHGAZY.cjs +99 -0
- package/dist/chunk-7KJHGAZY.cjs.map +1 -0
- package/dist/chunk-GB2PSZ33.cjs +74 -0
- package/dist/chunk-GB2PSZ33.cjs.map +1 -0
- package/dist/chunk-JAXDEK5Z.cjs +63 -0
- package/dist/chunk-JAXDEK5Z.cjs.map +1 -0
- package/dist/chunk-KQEBNUZI.cjs +70 -0
- package/dist/chunk-KQEBNUZI.cjs.map +1 -0
- package/dist/chunk-LHCSVEFT.cjs +8 -0
- package/dist/chunk-LHCSVEFT.cjs.map +1 -0
- package/dist/chunk-LYX73U3P.cjs +195 -0
- package/dist/chunk-LYX73U3P.cjs.map +1 -0
- package/dist/chunk-LZSE6QGQ.cjs +60 -0
- package/dist/chunk-LZSE6QGQ.cjs.map +1 -0
- package/dist/chunk-NPVIFZQV.cjs +107 -0
- package/dist/chunk-NPVIFZQV.cjs.map +1 -0
- package/dist/chunk-PG5LYTUX.cjs +4 -0
- package/dist/chunk-PG5LYTUX.cjs.map +1 -0
- package/dist/chunk-Q3WYIWCN.cjs +183 -0
- package/dist/chunk-Q3WYIWCN.cjs.map +1 -0
- package/dist/chunk-QETQGAXJ.cjs +35 -0
- package/dist/chunk-QETQGAXJ.cjs.map +1 -0
- package/dist/chunk-SDXDHVTV.cjs +555 -0
- package/dist/chunk-SDXDHVTV.cjs.map +1 -0
- package/dist/chunk-YZKUEC2J.cjs +31 -0
- package/dist/chunk-YZKUEC2J.cjs.map +1 -0
- package/dist/displays/index.cjs +24 -0
- package/dist/displays/index.cjs.map +1 -0
- package/dist/displays/index.d.cts +34 -0
- package/dist/events/index.cjs +36 -0
- package/dist/events/index.cjs.map +1 -0
- package/dist/events/index.d.cts +35 -0
- package/dist/evidence/index.cjs +6 -0
- package/dist/evidence/index.cjs.map +1 -0
- package/dist/evidence/index.d.cts +43 -0
- package/dist/ids/index.cjs +24 -0
- package/dist/ids/index.cjs.map +1 -0
- package/dist/ids/index.d.cts +19 -0
- package/dist/index.cjs +268 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +13 -0
- package/dist/modules/index.cjs +42 -0
- package/dist/modules/index.cjs.map +1 -0
- package/dist/modules/index.d.cts +73 -0
- package/dist/objects/index.cjs +24 -0
- package/dist/objects/index.cjs.map +1 -0
- package/dist/objects/index.d.cts +7 -0
- package/dist/policies/index.cjs +48 -0
- package/dist/policies/index.cjs.map +1 -0
- package/dist/policies/index.d.cts +143 -0
- package/dist/privacy/index.cjs +12 -0
- package/dist/privacy/index.cjs.map +1 -0
- package/dist/privacy/index.d.cts +77 -0
- package/dist/projections/index.cjs +12 -0
- package/dist/projections/index.cjs.map +1 -0
- package/dist/projections/index.d.cts +52 -0
- package/dist/state-machines/index.cjs +32 -0
- package/dist/state-machines/index.cjs.map +1 -0
- package/dist/state-machines/index.d.cts +60 -0
- package/dist/testing/index.cjs +21 -0
- package/dist/testing/index.cjs.map +1 -0
- package/dist/testing/index.d.cts +19 -0
- package/package.json +196 -111
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// adapters/index.ts
|
|
4
|
+
function adapterKey(adapterType, operation) {
|
|
5
|
+
return `${adapterType}:${operation}`;
|
|
6
|
+
}
|
|
7
|
+
var MissingAdapterRegistrationError = class extends Error {
|
|
8
|
+
constructor(adapterType, operation) {
|
|
9
|
+
super(`No adapter registered for adapterType "${adapterType}" and operation "${operation}"`);
|
|
10
|
+
this.name = "MissingAdapterRegistrationError";
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var AdapterRegistry = class {
|
|
14
|
+
adapters = /* @__PURE__ */ new Map();
|
|
15
|
+
register(adapter) {
|
|
16
|
+
const key = adapterKey(adapter.adapterType, adapter.operation);
|
|
17
|
+
if (this.adapters.has(key)) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
`Adapter already registered for adapterType "${adapter.adapterType}" and operation "${adapter.operation}"`
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
this.adapters.set(key, adapter);
|
|
23
|
+
}
|
|
24
|
+
resolve(adapterType, operation) {
|
|
25
|
+
return this.adapters.get(adapterKey(adapterType, operation));
|
|
26
|
+
}
|
|
27
|
+
require(adapterType, operation) {
|
|
28
|
+
const adapter = this.resolve(adapterType, operation);
|
|
29
|
+
if (!adapter) {
|
|
30
|
+
throw new MissingAdapterRegistrationError(adapterType, operation);
|
|
31
|
+
}
|
|
32
|
+
return adapter;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var DEFAULT_RETRY_POLICY = {
|
|
36
|
+
maxAttempts: 3,
|
|
37
|
+
initialDelayMs: 100,
|
|
38
|
+
backoffMultiplier: 2,
|
|
39
|
+
maxDelayMs: 1e3
|
|
40
|
+
};
|
|
41
|
+
var NO_RETRY_POLICY = {
|
|
42
|
+
idempotent: false,
|
|
43
|
+
maxAttempts: 1,
|
|
44
|
+
initialDelayMs: 0,
|
|
45
|
+
backoffMultiplier: 1,
|
|
46
|
+
maxDelayMs: 0
|
|
47
|
+
};
|
|
48
|
+
function resolveAdapterRetryPolicy(policy, defaultIdempotent) {
|
|
49
|
+
const idempotent = policy?.idempotent ?? defaultIdempotent;
|
|
50
|
+
if (!idempotent) {
|
|
51
|
+
return NO_RETRY_POLICY;
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
idempotent: true,
|
|
55
|
+
maxAttempts: Math.max(1, Math.min(policy?.maxAttempts ?? DEFAULT_RETRY_POLICY.maxAttempts, 5)),
|
|
56
|
+
initialDelayMs: Math.max(0, policy?.initialDelayMs ?? DEFAULT_RETRY_POLICY.initialDelayMs),
|
|
57
|
+
backoffMultiplier: Math.max(
|
|
58
|
+
1,
|
|
59
|
+
policy?.backoffMultiplier ?? DEFAULT_RETRY_POLICY.backoffMultiplier
|
|
60
|
+
),
|
|
61
|
+
maxDelayMs: Math.max(0, policy?.maxDelayMs ?? DEFAULT_RETRY_POLICY.maxDelayMs)
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function getRetryDelayMs(attempt, policy) {
|
|
65
|
+
const exponentialDelay = policy.initialDelayMs * policy.backoffMultiplier ** Math.max(0, attempt - 1);
|
|
66
|
+
return Math.min(exponentialDelay, policy.maxDelayMs);
|
|
67
|
+
}
|
|
68
|
+
async function executeWithAdapterRetry(options) {
|
|
69
|
+
const policy = resolveAdapterRetryPolicy(options.policy, options.defaultIdempotent);
|
|
70
|
+
const sleep = options.sleep ?? ((delayMs) => new Promise((resolve) => setTimeout(resolve, delayMs)));
|
|
71
|
+
let lastError;
|
|
72
|
+
for (let attempt = 1; attempt <= policy.maxAttempts; attempt++) {
|
|
73
|
+
try {
|
|
74
|
+
const result = await options.execute(attempt, policy.maxAttempts);
|
|
75
|
+
if (options.isSuccessful(result)) {
|
|
76
|
+
await options.onAttempt?.({ attempt, result, willRetry: false });
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
lastError = options.getError(result) ?? "Adapter returned unsuccessful result";
|
|
80
|
+
const willRetry = policy.idempotent && attempt < policy.maxAttempts;
|
|
81
|
+
const nextDelayMs = willRetry ? getRetryDelayMs(attempt, policy) : void 0;
|
|
82
|
+
await options.onAttempt?.({ attempt, result, error: lastError, willRetry, nextDelayMs });
|
|
83
|
+
if (!willRetry) {
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
await sleep(nextDelayMs ?? 0);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
89
|
+
const willRetry = policy.idempotent && attempt < policy.maxAttempts;
|
|
90
|
+
const nextDelayMs = willRetry ? getRetryDelayMs(attempt, policy) : void 0;
|
|
91
|
+
await options.onAttempt?.({ attempt, error: lastError, willRetry, nextDelayMs });
|
|
92
|
+
if (!willRetry) {
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
await sleep(nextDelayMs ?? 0);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
throw new Error(lastError ?? "Adapter retry exhausted");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
exports.AdapterRegistry = AdapterRegistry;
|
|
102
|
+
exports.MissingAdapterRegistrationError = MissingAdapterRegistrationError;
|
|
103
|
+
exports.executeWithAdapterRetry = executeWithAdapterRetry;
|
|
104
|
+
exports.getRetryDelayMs = getRetryDelayMs;
|
|
105
|
+
exports.resolveAdapterRetryPolicy = resolveAdapterRetryPolicy;
|
|
106
|
+
//# sourceMappingURL=chunk-NPVIFZQV.cjs.map
|
|
107
|
+
//# sourceMappingURL=chunk-NPVIFZQV.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../adapters/index.ts"],"names":[],"mappings":";;;AAiFA,SAAS,UAAA,CAAW,aAAqB,SAAA,EAA2B;AACnE,EAAA,OAAO,CAAA,EAAG,WAAW,CAAA,CAAA,EAAI,SAAS,CAAA,CAAA;AACnC;AAEO,IAAM,+BAAA,GAAN,cAA8C,KAAA,CAAM;AAAA,EAC1D,WAAA,CAAY,aAAqB,SAAA,EAAmB;AACnD,IAAA,KAAA,CAAM,CAAA,uCAAA,EAA0C,WAAW,CAAA,iBAAA,EAAoB,SAAS,CAAA,CAAA,CAAG,CAAA;AAC3F,IAAA,IAAA,CAAK,IAAA,GAAO,iCAAA;AAAA,EACb;AACD;AAEO,IAAM,kBAAN,MAAsB;AAAA,EACX,QAAA,uBAAe,GAAA,EAAmC;AAAA,EAEnE,SAAS,OAAA,EAAsC;AAC9C,IAAA,MAAM,GAAA,GAAM,UAAA,CAAW,OAAA,CA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AAM,KAAA;AAAA,MACP;AAEA,MAAA,MAAM,KAAA,CAAM,eAAe,CAAC,CAAA;AAAA,IAC7B;AAAA,EACD;AAEA,EAAA,MAAM,IAAI,KAAA,CAAM,SAAA,IAAa,yBAAyB,CAAA;AACvD","file":"chunk-NPVIFZQV.cjs","sourcesContent":["export interface AdapterExecutionContext {\n\ttenantId: string;\n\tspaceId: string;\n\tactionInvocationId: string;\n\tadapterInvocationId: string;\n\tcorrelationId: string;\n\tcausationId?: string;\n\tattempt: number;\n\tmaxAttempts: number;\n}\n\nexport interface AdapterExecutionResult {\n\tsuccess: boolean;\n\tcorrelationId?: string;\n\terror?: string;\n}\n\nexport type AdapterExecutor = (\n\tinput: Record<string, unknown>,\n\tcontext: AdapterExecutionContext,\n) => Promise<AdapterExecutionResult>;\n\nexport interface AdapterRetryPolicy {\n\t/** External calls must explicitly opt into retry by being idempotent. */\n\tidempotent: boolean;\n\t/** Total attempts including the first call. Non-idempotent policies are forced to 1. */\n\tmaxAttempts?: number;\n\t/** First backoff delay in milliseconds. */\n\tinitialDelayMs?: number;\n\t/** Exponential multiplier applied after each failed attempt. */\n\tbackoffMultiplier?: number;\n\t/** Upper bound for any single retry delay in milliseconds. */\n\tmaxDelayMs?: number;\n}\n\nexport interface ResolvedAdapterRetryPolicy {\n\tidempotent: boolean;\n\tmaxAttempts: number;\n\tinitialDelayMs: number;\n\tbackoffMultiplier: number;\n\tmaxDelayMs: number;\n}\n\nexport interface AdapterCircuitBreakerConfig {\n\tmode: \"monitor\" | \"enforce\";\n\tfailureThreshold: number;\n\twindowMs: number;\n\tcooldownMs: number;\n\tminimumSamples?: number;\n}\n\nexport interface AdapterImplementation {\n\tadapterType: string;\n\toperation: string;\n\tvendor: string;\n\t/** Whether this adapter operation can be safely retried by default. */\n\tidempotent: boolean;\n\tretryPolicy?: AdapterRetryPolicy;\n\t/** Defaults to monitor-only; enforce mode must be explicitly configured. */\n\tcircuitBreaker?: AdapterCircuitBreakerConfig;\n\texecute: AdapterExecutor;\n}\n\nexport interface AdapterAttempt<T> {\n\tattempt: number;\n\tresult?: T;\n\terror?: string;\n\twillRetry: boolean;\n\tnextDelayMs?: number;\n}\n\nexport interface ExecuteWithRetryOptions<T> {\n\tpolicy?: AdapterRetryPolicy;\n\tdefaultIdempotent: boolean;\n\texecute: (attempt: number, maxAttempts: number) => Promise<T>;\n\tisSuccessful: (result: T) => boolean;\n\tgetError: (result: T) => string | undefined;\n\tsleep?: (delayMs: number) => Promise<void>;\n\tonAttempt?: (attempt: AdapterAttempt<T>) => Promise<void> | void;\n}\n\nfunction adapterKey(adapterType: string, operation: string): string {\n\treturn `${adapterType}:${operation}`;\n}\n\nexport class MissingAdapterRegistrationError extends Error {\n\tconstructor(adapterType: string, operation: string) {\n\t\tsuper(`No adapter registered for adapterType \"${adapterType}\" and operation \"${operation}\"`);\n\t\tthis.name = \"MissingAdapterRegistrationError\";\n\t}\n}\n\nexport class AdapterRegistry {\n\tprivate readonly adapters = new Map<string, AdapterImplementation>();\n\n\tregister(adapter: AdapterImplementation): void {\n\t\tconst key = adapterKey(adapter.adapterType, adapter.operation);\n\t\tif (this.adapters.has(key)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Adapter already registered for adapterType \"${adapter.adapterType}\" and operation \"${adapter.operation}\"`,\n\t\t\t);\n\t\t}\n\n\t\tthis.adapters.set(key, adapter);\n\t}\n\n\tresolve(adapterType: string, operation: string): AdapterImplementation | undefined {\n\t\treturn this.adapters.get(adapterKey(adapterType, operation));\n\t}\n\n\trequire(adapterType: string, operation: string): AdapterImplementation {\n\t\tconst adapter = this.resolve(adapterType, operation);\n\t\tif (!adapter) {\n\t\t\tthrow new MissingAdapterRegistrationError(adapterType, operation);\n\t\t}\n\n\t\treturn adapter;\n\t}\n}\n\nconst DEFAULT_RETRY_POLICY: ResolvedAdapterRetryPolicy = {\n\tidempotent: true,\n\tmaxAttempts: 3,\n\tinitialDelayMs: 100,\n\tbackoffMultiplier: 2,\n\tmaxDelayMs: 1_000,\n};\n\nconst NO_RETRY_POLICY: ResolvedAdapterRetryPolicy = {\n\tidempotent: false,\n\tmaxAttempts: 1,\n\tinitialDelayMs: 0,\n\tbackoffMultiplier: 1,\n\tmaxDelayMs: 0,\n};\n\nexport function resolveAdapterRetryPolicy(\n\tpolicy: AdapterRetryPolicy | undefined,\n\tdefaultIdempotent: boolean,\n): ResolvedAdapterRetryPolicy {\n\tconst idempotent = policy?.idempotent ?? defaultIdempotent;\n\n\tif (!idempotent) {\n\t\treturn NO_RETRY_POLICY;\n\t}\n\n\treturn {\n\t\tidempotent: true,\n\t\tmaxAttempts: Math.max(1, Math.min(policy?.maxAttempts ?? DEFAULT_RETRY_POLICY.maxAttempts, 5)),\n\t\tinitialDelayMs: Math.max(0, policy?.initialDelayMs ?? DEFAULT_RETRY_POLICY.initialDelayMs),\n\t\tbackoffMultiplier: Math.max(\n\t\t\t1,\n\t\t\tpolicy?.backoffMultiplier ?? DEFAULT_RETRY_POLICY.backoffMultiplier,\n\t\t),\n\t\tmaxDelayMs: Math.max(0, policy?.maxDelayMs ?? DEFAULT_RETRY_POLICY.maxDelayMs),\n\t};\n}\n\nexport function getRetryDelayMs(attempt: number, policy: ResolvedAdapterRetryPolicy): number {\n\tconst exponentialDelay =\n\t\tpolicy.initialDelayMs * policy.backoffMultiplier ** Math.max(0, attempt - 1);\n\n\treturn Math.min(exponentialDelay, policy.maxDelayMs);\n}\n\nexport async function executeWithAdapterRetry<T>(options: ExecuteWithRetryOptions<T>): Promise<T> {\n\tconst policy = resolveAdapterRetryPolicy(options.policy, options.defaultIdempotent);\n\tconst sleep =\n\t\toptions.sleep ?? ((delayMs) => new Promise((resolve) => setTimeout(resolve, delayMs)));\n\tlet lastError: string | undefined;\n\n\tfor (let attempt = 1; attempt <= policy.maxAttempts; attempt++) {\n\t\ttry {\n\t\t\tconst result = await options.execute(attempt, policy.maxAttempts);\n\t\t\tif (options.isSuccessful(result)) {\n\t\t\t\tawait options.onAttempt?.({ attempt, result, willRetry: false });\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tlastError = options.getError(result) ?? \"Adapter returned unsuccessful result\";\n\t\t\tconst willRetry = policy.idempotent && attempt < policy.maxAttempts;\n\t\t\tconst nextDelayMs = willRetry ? getRetryDelayMs(attempt, policy) : undefined;\n\t\t\tawait options.onAttempt?.({ attempt, result, error: lastError, willRetry, nextDelayMs });\n\n\t\t\tif (!willRetry) {\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tawait sleep(nextDelayMs ?? 0);\n\t\t} catch (error) {\n\t\t\tlastError = error instanceof Error ? error.message : String(error);\n\t\t\tconst willRetry = policy.idempotent && attempt < policy.maxAttempts;\n\t\t\tconst nextDelayMs = willRetry ? getRetryDelayMs(attempt, policy) : undefined;\n\t\t\tawait options.onAttempt?.({ attempt, error: lastError, willRetry, nextDelayMs });\n\n\t\t\tif (!willRetry) {\n\t\t\t\tthrow error;\n\t\t\t}\n\n\t\t\tawait sleep(nextDelayMs ?? 0);\n\t\t}\n\t}\n\n\tthrow new Error(lastError ?? \"Adapter retry exhausted\");\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"chunk-PG5LYTUX.cjs"}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// projections/index.ts
|
|
4
|
+
async function replayEvents({
|
|
5
|
+
events,
|
|
6
|
+
scope,
|
|
7
|
+
initialState,
|
|
8
|
+
snapshot,
|
|
9
|
+
applyEvent
|
|
10
|
+
}) {
|
|
11
|
+
const warnings = [];
|
|
12
|
+
const scopedEvents = filterEventsByScope(await collectEvents(events), scope, warnings);
|
|
13
|
+
const orderedEvents = sortEvents(scopedEvents, isSubjectScoped(scope));
|
|
14
|
+
const uniqueEvents = removeDuplicateEvents(orderedEvents, warnings);
|
|
15
|
+
const resumableEvents = applyCursor(uniqueEvents, snapshot, isSubjectScoped(scope), warnings);
|
|
16
|
+
let state = snapshot?.snapshotData ?? initialState;
|
|
17
|
+
const appliedEvents = [];
|
|
18
|
+
let eventCursor = snapshot?.eventCursor ?? null;
|
|
19
|
+
let eventSequence = snapshot?.eventSequence ?? 0;
|
|
20
|
+
for (const event of resumableEvents) {
|
|
21
|
+
state = await applyEvent(state, event);
|
|
22
|
+
appliedEvents.push(event);
|
|
23
|
+
eventCursor = event.id;
|
|
24
|
+
eventSequence = event.sequence;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
state,
|
|
28
|
+
appliedEvents,
|
|
29
|
+
warnings,
|
|
30
|
+
eventCursor,
|
|
31
|
+
eventSequence
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
async function collectEvents(events) {
|
|
35
|
+
const collected = [];
|
|
36
|
+
if (Symbol.asyncIterator in events) {
|
|
37
|
+
for await (const event of events) {
|
|
38
|
+
collected.push(event);
|
|
39
|
+
}
|
|
40
|
+
return collected;
|
|
41
|
+
}
|
|
42
|
+
for (const event of events) {
|
|
43
|
+
collected.push(event);
|
|
44
|
+
}
|
|
45
|
+
return collected;
|
|
46
|
+
}
|
|
47
|
+
function filterEventsByScope(events, scope, warnings) {
|
|
48
|
+
if (scope.subjectType && !scope.subjectId || !scope.subjectType && scope.subjectId) {
|
|
49
|
+
warnings.push({
|
|
50
|
+
code: "subject_scope_incomplete",
|
|
51
|
+
message: "Both subjectType and subjectId are required for subject-scoped replay.",
|
|
52
|
+
subjectType: scope.subjectType,
|
|
53
|
+
subjectId: scope.subjectId
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return events.filter((event) => {
|
|
57
|
+
if (event.tenantId !== scope.tenantId || event.spaceId !== scope.spaceId) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
if (scope.subjectType && event.subjectType !== scope.subjectType) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if (scope.subjectId && event.subjectId !== scope.subjectId) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function removeDuplicateEvents(events, warnings) {
|
|
70
|
+
const seenEventIds = /* @__PURE__ */ new Set();
|
|
71
|
+
const uniqueEvents = [];
|
|
72
|
+
for (const event of events) {
|
|
73
|
+
if (seenEventIds.has(event.id)) {
|
|
74
|
+
warnings.push({
|
|
75
|
+
code: "duplicate_event",
|
|
76
|
+
message: `Duplicate event ${event.id} ignored during replay.`,
|
|
77
|
+
eventId: event.id,
|
|
78
|
+
subjectType: event.subjectType,
|
|
79
|
+
subjectId: event.subjectId
|
|
80
|
+
});
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
seenEventIds.add(event.id);
|
|
84
|
+
uniqueEvents.push(event);
|
|
85
|
+
}
|
|
86
|
+
recordMissingSequenceWarnings(uniqueEvents, warnings);
|
|
87
|
+
return uniqueEvents;
|
|
88
|
+
}
|
|
89
|
+
function recordMissingSequenceWarnings(events, warnings) {
|
|
90
|
+
const lastSequenceBySubject = /* @__PURE__ */ new Map();
|
|
91
|
+
for (const event of events) {
|
|
92
|
+
const subjectKey = `${event.subjectType}:${event.subjectId}`;
|
|
93
|
+
const previousSequence = lastSequenceBySubject.get(subjectKey);
|
|
94
|
+
const expectedSequence = previousSequence === void 0 ? event.sequence : previousSequence + 1;
|
|
95
|
+
if (previousSequence !== void 0 && event.sequence > expectedSequence) {
|
|
96
|
+
warnings.push({
|
|
97
|
+
code: "missing_sequence",
|
|
98
|
+
message: `Missing event sequence for ${subjectKey}: expected ${expectedSequence}, received ${event.sequence}.`,
|
|
99
|
+
eventId: event.id,
|
|
100
|
+
subjectType: event.subjectType,
|
|
101
|
+
subjectId: event.subjectId,
|
|
102
|
+
expectedSequence,
|
|
103
|
+
actualSequence: event.sequence
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
lastSequenceBySubject.set(subjectKey, Math.max(previousSequence ?? event.sequence, event.sequence));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function applyCursor(events, snapshot, subjectScoped, warnings) {
|
|
110
|
+
const eventCursor = snapshot?.eventCursor;
|
|
111
|
+
if (eventCursor) {
|
|
112
|
+
const cursorIndex = events.findIndex((event) => event.id === eventCursor);
|
|
113
|
+
if (cursorIndex >= 0) {
|
|
114
|
+
return events.slice(cursorIndex + 1);
|
|
115
|
+
}
|
|
116
|
+
warnings.push({
|
|
117
|
+
code: "cursor_not_found",
|
|
118
|
+
message: `Snapshot cursor ${eventCursor} was not found in the scoped event stream.`,
|
|
119
|
+
eventId: eventCursor
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
if (subjectScoped && snapshot?.eventSequence !== void 0 && snapshot.eventSequence !== null) {
|
|
123
|
+
return events.filter((event) => event.sequence > snapshot.eventSequence);
|
|
124
|
+
}
|
|
125
|
+
return events;
|
|
126
|
+
}
|
|
127
|
+
function sortEvents(events, subjectScoped) {
|
|
128
|
+
return [...events].sort(
|
|
129
|
+
(first, second) => subjectScoped ? compareSubjectEvents(first, second) : compareGlobalEvents(first, second)
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
function compareSubjectEvents(first, second) {
|
|
133
|
+
return compareValues(
|
|
134
|
+
[first.sequence, toTimestamp(first.recordedAt), toTimestamp(first.occurredAt), first.id],
|
|
135
|
+
[second.sequence, toTimestamp(second.recordedAt), toTimestamp(second.occurredAt), second.id]
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
function compareGlobalEvents(first, second) {
|
|
139
|
+
return compareValues(
|
|
140
|
+
[
|
|
141
|
+
toTimestamp(first.recordedAt),
|
|
142
|
+
toTimestamp(first.occurredAt),
|
|
143
|
+
first.actionInvocationId,
|
|
144
|
+
first.correlationId,
|
|
145
|
+
first.subjectType,
|
|
146
|
+
first.subjectId,
|
|
147
|
+
first.sequence,
|
|
148
|
+
first.id
|
|
149
|
+
],
|
|
150
|
+
[
|
|
151
|
+
toTimestamp(second.recordedAt),
|
|
152
|
+
toTimestamp(second.occurredAt),
|
|
153
|
+
second.actionInvocationId,
|
|
154
|
+
second.correlationId,
|
|
155
|
+
second.subjectType,
|
|
156
|
+
second.subjectId,
|
|
157
|
+
second.sequence,
|
|
158
|
+
second.id
|
|
159
|
+
]
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
function compareValues(firstValues, secondValues) {
|
|
163
|
+
for (const [index, firstValue] of firstValues.entries()) {
|
|
164
|
+
const secondValue = secondValues[index];
|
|
165
|
+
if (firstValue < secondValue) {
|
|
166
|
+
return -1;
|
|
167
|
+
}
|
|
168
|
+
if (firstValue > secondValue) {
|
|
169
|
+
return 1;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return 0;
|
|
173
|
+
}
|
|
174
|
+
function isSubjectScoped(scope) {
|
|
175
|
+
return Boolean(scope.subjectType && scope.subjectId);
|
|
176
|
+
}
|
|
177
|
+
function toTimestamp(value) {
|
|
178
|
+
return value instanceof Date ? value.getTime() : new Date(value).getTime();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
exports.replayEvents = replayEvents;
|
|
182
|
+
//# sourceMappingURL=chunk-Q3WYIWCN.cjs.map
|
|
183
|
+
//# sourceMappingURL=chunk-Q3WYIWCN.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../projections/index.ts"],"names":[],"mappings":";;;AA4DA,eAAsB,YAAA,CAA+E;AAAA,EACpG,MAAA;AAAA,EACA,KAAA;AAAA,EACA,YAAA;AAAA,EACA,QAAA;AAAA,EACA;AACD,CAAA,EAAiF;AAChF,EAAA,MAAM,WAA4B,EAAC;AACnC,EAAA,MAAM,eAAe,mBAAA,CAAoB,MAAM,cAAc,MAAM,CAAA,EAAG,OAAO,QAAQ,CAAA;AACrF,EAAA,MAAM,aAAA,GAAgB,UAAA,CAAW,YAAA,EAAc,eAAA,CAAgB,KAAK,CAAC,CAAA;AACrE,EAAA,MAAM,YAAA,GAAe,qBAAA,CAAsB,aAAA,EAAe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A,CAAM,OAAA,KAAY,IAAI,IAAA,CAAK,KAAK,CAAA,CAAE,OAAA,EAAQ;AAC1E","file":"chunk-Q3WYIWCN.cjs","sourcesContent":["export interface ReplayableAssetEvent<Payload = unknown> {\n\tid: string;\n\ttenantId: string;\n\tspaceId: string;\n\teventType: string;\n\tsubjectType: string;\n\tsubjectId: string;\n\tpayload: Payload;\n\tsequence: number;\n\toccurredAt: Date | string;\n\trecordedAt: Date | string;\n\tactionInvocationId: string;\n\tcorrelationId: string;\n}\n\nexport interface ReplayScope {\n\ttenantId: string;\n\tspaceId: string;\n\tsubjectType?: string;\n\tsubjectId?: string;\n}\n\nexport interface ReplaySnapshot<State = unknown> {\n\tsnapshotData: State;\n\teventCursor?: string | null;\n\teventSequence?: number | null;\n}\n\nexport type ReplayWarningCode =\n\t| \"cursor_not_found\"\n\t| \"duplicate_event\"\n\t| \"missing_sequence\"\n\t| \"subject_scope_incomplete\";\n\nexport interface ReplayWarning {\n\tcode: ReplayWarningCode;\n\tmessage: string;\n\teventId?: string;\n\tsubjectType?: string;\n\tsubjectId?: string;\n\texpectedSequence?: number;\n\tactualSequence?: number;\n}\n\nexport interface ReplayEventsOptions<State, Event extends ReplayableAssetEvent = ReplayableAssetEvent> {\n\tevents: Iterable<Event> | AsyncIterable<Event>;\n\tscope: ReplayScope;\n\tinitialState: State;\n\tsnapshot?: ReplaySnapshot<State> | null;\n\tapplyEvent: (state: State, event: Event) => State | Promise<State>;\n}\n\nexport interface ReplayEventsResult<State, Event extends ReplayableAssetEvent = ReplayableAssetEvent> {\n\tstate: State;\n\tappliedEvents: Event[];\n\twarnings: ReplayWarning[];\n\teventCursor: string | null;\n\teventSequence: number;\n}\n\nexport async function replayEvents<State, Event extends ReplayableAssetEvent = ReplayableAssetEvent>({\n\tevents,\n\tscope,\n\tinitialState,\n\tsnapshot,\n\tapplyEvent,\n}: ReplayEventsOptions<State, Event>): Promise<ReplayEventsResult<State, Event>> {\n\tconst warnings: ReplayWarning[] = [];\n\tconst scopedEvents = filterEventsByScope(await collectEvents(events), scope, warnings);\n\tconst orderedEvents = sortEvents(scopedEvents, isSubjectScoped(scope));\n\tconst uniqueEvents = removeDuplicateEvents(orderedEvents, warnings);\n\tconst resumableEvents = applyCursor(uniqueEvents, snapshot, isSubjectScoped(scope), warnings);\n\n\tlet state = snapshot?.snapshotData ?? initialState;\n\tconst appliedEvents: Event[] = [];\n\tlet eventCursor = snapshot?.eventCursor ?? null;\n\tlet eventSequence = snapshot?.eventSequence ?? 0;\n\n\tfor (const event of resumableEvents) {\n\t\tstate = await applyEvent(state, event);\n\t\tappliedEvents.push(event);\n\t\teventCursor = event.id;\n\t\teventSequence = event.sequence;\n\t}\n\n\treturn {\n\t\tstate,\n\t\tappliedEvents,\n\t\twarnings,\n\t\teventCursor,\n\t\teventSequence,\n\t};\n}\n\nasync function collectEvents<Event extends ReplayableAssetEvent>(\n\tevents: Iterable<Event> | AsyncIterable<Event>,\n): Promise<Event[]> {\n\tconst collected: Event[] = [];\n\n\tif (Symbol.asyncIterator in events) {\n\t\tfor await (const event of events) {\n\t\t\tcollected.push(event);\n\t\t}\n\n\t\treturn collected;\n\t}\n\n\tfor (const event of events) {\n\t\tcollected.push(event);\n\t}\n\n\treturn collected;\n}\n\nfunction filterEventsByScope<Event extends ReplayableAssetEvent>(\n\tevents: Event[],\n\tscope: ReplayScope,\n\twarnings: ReplayWarning[],\n): Event[] {\n\tif ((scope.subjectType && !scope.subjectId) || (!scope.subjectType && scope.subjectId)) {\n\t\twarnings.push({\n\t\t\tcode: \"subject_scope_incomplete\",\n\t\t\tmessage: \"Both subjectType and subjectId are required for subject-scoped replay.\",\n\t\t\tsubjectType: scope.subjectType,\n\t\t\tsubjectId: scope.subjectId,\n\t\t});\n\t}\n\n\treturn events.filter((event) => {\n\t\tif (event.tenantId !== scope.tenantId || event.spaceId !== scope.spaceId) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (scope.subjectType && event.subjectType !== scope.subjectType) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (scope.subjectId && event.subjectId !== scope.subjectId) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t});\n}\n\nfunction removeDuplicateEvents<Event extends ReplayableAssetEvent>(\n\tevents: Event[],\n\twarnings: ReplayWarning[],\n): Event[] {\n\tconst seenEventIds = new Set<string>();\n\tconst uniqueEvents: Event[] = [];\n\n\tfor (const event of events) {\n\t\tif (seenEventIds.has(event.id)) {\n\t\t\twarnings.push({\n\t\t\t\tcode: \"duplicate_event\",\n\t\t\t\tmessage: `Duplicate event ${event.id} ignored during replay.`,\n\t\t\t\teventId: event.id,\n\t\t\t\tsubjectType: event.subjectType,\n\t\t\t\tsubjectId: event.subjectId,\n\t\t\t});\n\t\t\tcontinue;\n\t\t}\n\n\t\tseenEventIds.add(event.id);\n\t\tuniqueEvents.push(event);\n\t}\n\n\trecordMissingSequenceWarnings(uniqueEvents, warnings);\n\n\treturn uniqueEvents;\n}\n\nfunction recordMissingSequenceWarnings<Event extends ReplayableAssetEvent>(\n\tevents: Event[],\n\twarnings: ReplayWarning[],\n): void {\n\tconst lastSequenceBySubject = new Map<string, number>();\n\n\tfor (const event of events) {\n\t\tconst subjectKey = `${event.subjectType}:${event.subjectId}`;\n\t\tconst previousSequence = lastSequenceBySubject.get(subjectKey);\n\t\tconst expectedSequence = previousSequence === undefined ? event.sequence : previousSequence + 1;\n\n\t\tif (previousSequence !== undefined && event.sequence > expectedSequence) {\n\t\t\twarnings.push({\n\t\t\t\tcode: \"missing_sequence\",\n\t\t\t\tmessage: `Missing event sequence for ${subjectKey}: expected ${expectedSequence}, received ${event.sequence}.`,\n\t\t\t\teventId: event.id,\n\t\t\t\tsubjectType: event.subjectType,\n\t\t\t\tsubjectId: event.subjectId,\n\t\t\t\texpectedSequence,\n\t\t\t\tactualSequence: event.sequence,\n\t\t\t});\n\t\t}\n\n\t\tlastSequenceBySubject.set(subjectKey, Math.max(previousSequence ?? event.sequence, event.sequence));\n\t}\n}\n\nfunction applyCursor<Event extends ReplayableAssetEvent>(\n\tevents: Event[],\n\tsnapshot: ReplaySnapshot | null | undefined,\n\tsubjectScoped: boolean,\n\twarnings: ReplayWarning[],\n): Event[] {\n\tconst eventCursor = snapshot?.eventCursor;\n\n\tif (eventCursor) {\n\t\tconst cursorIndex = events.findIndex((event) => event.id === eventCursor);\n\n\t\tif (cursorIndex >= 0) {\n\t\t\treturn events.slice(cursorIndex + 1);\n\t\t}\n\n\t\twarnings.push({\n\t\t\tcode: \"cursor_not_found\",\n\t\t\tmessage: `Snapshot cursor ${eventCursor} was not found in the scoped event stream.`,\n\t\t\teventId: eventCursor,\n\t\t});\n\t}\n\n\tif (subjectScoped && snapshot?.eventSequence !== undefined && snapshot.eventSequence !== null) {\n\t\treturn events.filter((event) => event.sequence > snapshot.eventSequence!);\n\t}\n\n\treturn events;\n}\n\nfunction sortEvents<Event extends ReplayableAssetEvent>(events: Event[], subjectScoped: boolean): Event[] {\n\treturn [...events].sort((first, second) =>\n\t\tsubjectScoped ? compareSubjectEvents(first, second) : compareGlobalEvents(first, second),\n\t);\n}\n\nfunction compareSubjectEvents(first: ReplayableAssetEvent, second: ReplayableAssetEvent): number {\n\treturn compareValues(\n\t\t[first.sequence, toTimestamp(first.recordedAt), toTimestamp(first.occurredAt), first.id],\n\t\t[second.sequence, toTimestamp(second.recordedAt), toTimestamp(second.occurredAt), second.id],\n\t);\n}\n\nfunction compareGlobalEvents(first: ReplayableAssetEvent, second: ReplayableAssetEvent): number {\n\treturn compareValues(\n\t\t[\n\t\t\ttoTimestamp(first.recordedAt),\n\t\t\ttoTimestamp(first.occurredAt),\n\t\t\tfirst.actionInvocationId,\n\t\t\tfirst.correlationId,\n\t\t\tfirst.subjectType,\n\t\t\tfirst.subjectId,\n\t\t\tfirst.sequence,\n\t\t\tfirst.id,\n\t\t],\n\t\t[\n\t\t\ttoTimestamp(second.recordedAt),\n\t\t\ttoTimestamp(second.occurredAt),\n\t\t\tsecond.actionInvocationId,\n\t\t\tsecond.correlationId,\n\t\t\tsecond.subjectType,\n\t\t\tsecond.subjectId,\n\t\t\tsecond.sequence,\n\t\t\tsecond.id,\n\t\t],\n\t);\n}\n\nfunction compareValues(firstValues: Array<number | string>, secondValues: Array<number | string>): number {\n\tfor (const [index, firstValue] of firstValues.entries()) {\n\t\tconst secondValue = secondValues[index];\n\n\t\tif (firstValue < secondValue) {\n\t\t\treturn -1;\n\t\t}\n\n\t\tif (firstValue > secondValue) {\n\t\t\treturn 1;\n\t\t}\n\t}\n\n\treturn 0;\n}\n\nfunction isSubjectScoped(scope: ReplayScope): boolean {\n\treturn Boolean(scope.subjectType && scope.subjectId);\n}\n\nfunction toTimestamp(value: Date | string): number {\n\treturn value instanceof Date ? value.getTime() : new Date(value).getTime();\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// objects/index.ts
|
|
4
|
+
var objectTypeRegistry = /* @__PURE__ */ new Set([
|
|
5
|
+
"AssetEvent",
|
|
6
|
+
"ActionInvocation",
|
|
7
|
+
"PolicyEvaluation",
|
|
8
|
+
"ApprovalRequest",
|
|
9
|
+
"ConsentRecord",
|
|
10
|
+
"AgentRun"
|
|
11
|
+
]);
|
|
12
|
+
function assertObjectType(type) {
|
|
13
|
+
if (!/^[A-Z][a-zA-Z0-9]*$/.test(type)) {
|
|
14
|
+
throw new Error(
|
|
15
|
+
`Invalid object type: "${type}". Object types must be PascalCase alphanumeric.`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function registerObjectType(type) {
|
|
20
|
+
assertObjectType(type);
|
|
21
|
+
objectTypeRegistry.add(type);
|
|
22
|
+
}
|
|
23
|
+
function isValidObjectType(type) {
|
|
24
|
+
return objectTypeRegistry.has(type);
|
|
25
|
+
}
|
|
26
|
+
function getRegisteredObjectTypes() {
|
|
27
|
+
return Array.from(objectTypeRegistry);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
exports.assertObjectType = assertObjectType;
|
|
31
|
+
exports.getRegisteredObjectTypes = getRegisteredObjectTypes;
|
|
32
|
+
exports.isValidObjectType = isValidObjectType;
|
|
33
|
+
exports.registerObjectType = registerObjectType;
|
|
34
|
+
//# sourceMappingURL=chunk-QETQGAXJ.cjs.map
|
|
35
|
+
//# sourceMappingURL=chunk-QETQGAXJ.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../objects/index.ts"],"names":[],"mappings":";;;AAAA,IAAM,kBAAA,uBAAyB,GAAA,CAAY;AAAA,EAC1C,YAAA;AAAA,EACA,kBAAA;AAAA,EACA,kBAAA;AAAA,EACA,iBAAA;AAAA,EACA,eAAA;AAAA,EACA;AACD,CAAC,CAAA;AAIM,SAAS,iBAAiB,IAAA,EAAgD;AAChF,EAAA,IAAI,CAAC,qBAAA,CAAsB,IAAA,CAAK,IAAI,CAAA,EAAG;AACtC,IAAA,MAAM,IAAI,KAAA;AAAA,MACT,yBAAyB,IAAI,CAAA,gDAAA;AAAA,KAC9B;AAAA,EACD;AACD;AAEO,SAAS,mBAAmB,IAAA,EAAoB;AACtD,EAAA,gBAAA,CAAiB,IAAI,CAAA;AACrB,EAAA,kBAAA,CAAmB,IAAI,IAAI,CAAA;AAC5B;AAEO,SAAS,kBAAkB,IAAA,EAAuB;AACxD,EAAA,OAAO,kBAAA,CAAmB,IAAI,IAAI,CAAA;AACnC;AAEO,SAAS,wBAAA,GAAqC;AACpD,EAAA,OAAO,KAAA,CAAM,KAAK,kBAAkB,CAAA;AACrC","file":"chunk-QETQGAXJ.cjs","sourcesContent":["const objectTypeRegistry = new Set<string>([\n\t\"AssetEvent\",\n\t\"ActionInvocation\",\n\t\"PolicyEvaluation\",\n\t\"ApprovalRequest\",\n\t\"ConsentRecord\",\n\t\"AgentRun\",\n]);\n\nexport type FabricObjectType = string;\n\nexport function assertObjectType(type: string): asserts type is FabricObjectType {\n\tif (!/^[A-Z][a-zA-Z0-9]*$/.test(type)) {\n\t\tthrow new Error(\n\t\t\t`Invalid object type: \"${type}\". Object types must be PascalCase alphanumeric.`,\n\t\t);\n\t}\n}\n\nexport function registerObjectType(type: string): void {\n\tassertObjectType(type);\n\tobjectTypeRegistry.add(type);\n}\n\nexport function isValidObjectType(type: string): boolean {\n\treturn objectTypeRegistry.has(type);\n}\n\nexport function getRegisteredObjectTypes(): string[] {\n\treturn Array.from(objectTypeRegistry);\n}\n"]}
|