@rdlabo/workers-hono-kit 0.10.3 → 0.10.4

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.
@@ -14,3 +14,5 @@ export { decodeOfflineSnapshotCursor, encodeOfflineSnapshotCursor } from './snap
14
14
  export type { OfflineSnapshotCursor } from './snapshot-cursor.js';
15
15
  export { assertOfflineJournalCursorRetained, compactOfflineJournal, OfflineJournalRebaselineRequiredError, } from './journal-retention.js';
16
16
  export type { CompactOfflineJournalOptions, OfflineJournalRetentionCandidate, OfflineJournalRetentionStore, OfflineJournalRetentionTransaction, } from './journal-retention.js';
17
+ export { assertOfflineJournalCoverage, runOfflineJournalMutation } from './journal-mutation.js';
18
+ export type { OfflineJournalMutationChange, OfflineJournalMutationStore, OfflineJournalMutationTransaction, } from './journal-mutation.js';
@@ -11,3 +11,4 @@ export { replicaNowIso } from './clock.js';
11
11
  export { defineRestDbMethodConverter } from './rest-db-method-converter.js';
12
12
  export { decodeOfflineSnapshotCursor, encodeOfflineSnapshotCursor } from './snapshot-cursor.js';
13
13
  export { assertOfflineJournalCursorRetained, compactOfflineJournal, OfflineJournalRebaselineRequiredError, } from './journal-retention.js';
14
+ export { assertOfflineJournalCoverage, runOfflineJournalMutation } from './journal-mutation.js';
@@ -0,0 +1,44 @@
1
+ /** One product-owned replica target affected by a domain mutation. */
2
+ export interface OfflineJournalMutationChange<TScope, TSourceKey extends string = string> {
3
+ /** Product-defined authorization and replica partition. */
4
+ readonly scope: TScope;
5
+ /** Replica source whose hydrated value is invalidated by the mutation. */
6
+ readonly sourceKey: TSourceKey;
7
+ /** Stable server identity within the source. */
8
+ readonly serverId: string | number;
9
+ /** Optional Outbox acknowledgement metadata committed with the journal row. */
10
+ readonly command?: {
11
+ readonly userId: string | number;
12
+ readonly commandId?: string;
13
+ };
14
+ }
15
+ /** Transaction-bound adapter that persists journal changes beside the domain write. */
16
+ export interface OfflineJournalMutationTransaction<TScope, TSourceKey extends string = string> {
17
+ /** Appends one journal entry using the product transaction. */
18
+ append(change: OfflineJournalMutationChange<TScope, TSourceKey>): Promise<void>;
19
+ }
20
+ /** Product adapter that owns the database transaction and journal schema. */
21
+ export interface OfflineJournalMutationStore<TScope, TSourceKey extends string = string, TTransaction extends OfflineJournalMutationTransaction<TScope, TSourceKey> = OfflineJournalMutationTransaction<TScope, TSourceKey>> {
22
+ /**
23
+ * Runs the domain write and every journal append in one database transaction.
24
+ * A throw from either the domain mutation or `append` must roll back all writes.
25
+ */
26
+ transaction<TResult>(operation: (transaction: TTransaction) => Promise<TResult>): Promise<TResult>;
27
+ }
28
+ /**
29
+ * Commits one domain mutation and its complete replica journal footprint atomically.
30
+ *
31
+ * `mutate` returns both the business result and every affected replica target. The shared
32
+ * state machine appends all targets before the product transaction may commit.
33
+ */
34
+ export declare function runOfflineJournalMutation<TScope, TSourceKey extends string, TResult, TTransaction extends OfflineJournalMutationTransaction<TScope, TSourceKey>>(options: {
35
+ readonly store: OfflineJournalMutationStore<TScope, TSourceKey, TTransaction>;
36
+ readonly mutate: (transaction: TTransaction) => Promise<{
37
+ readonly result: TResult;
38
+ readonly changes: readonly OfflineJournalMutationChange<TScope, TSourceKey>[];
39
+ }>;
40
+ /** Returns the canonical authorization/partition key for one product scope. */
41
+ readonly scopeKey: (scope: TScope) => string;
42
+ }): Promise<TResult>;
43
+ /** Fails CI when a product write route has no declared journal coverage decision. */
44
+ export declare function assertOfflineJournalCoverage<TWriteId extends string>(writeIds: readonly TWriteId[], coverage: Readonly<Record<TWriteId, readonly string[] | 'not-replicated'>>): void;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Commits one domain mutation and its complete replica journal footprint atomically.
3
+ *
4
+ * `mutate` returns both the business result and every affected replica target. The shared
5
+ * state machine appends all targets before the product transaction may commit.
6
+ */
7
+ export function runOfflineJournalMutation(options) {
8
+ return options.store.transaction(async (transaction) => {
9
+ const { result, changes } = await options.mutate(transaction);
10
+ const unique = new Map();
11
+ for (const change of changes) {
12
+ const scopeKey = options.scopeKey(change.scope);
13
+ if (!scopeKey || !change.sourceKey || String(change.serverId).length === 0) {
14
+ throw new Error('Offline journal mutation scope, source, and server keys must be non-empty.');
15
+ }
16
+ const serverKey = typeof change.serverId === 'number' ? `number:${change.serverId}` : `string:${change.serverId}`;
17
+ const key = `${scopeKey}\u0000${change.sourceKey}\u0000${serverKey}`;
18
+ const existing = unique.get(key);
19
+ if (existing === undefined) {
20
+ unique.set(key, change);
21
+ continue;
22
+ }
23
+ if (existing.command === undefined && change.command !== undefined) {
24
+ unique.set(key, { ...existing, command: change.command });
25
+ continue;
26
+ }
27
+ if (existing.command !== undefined &&
28
+ change.command !== undefined &&
29
+ (String(existing.command.userId) !== String(change.command.userId) ||
30
+ existing.command.commandId !== change.command.commandId)) {
31
+ throw new Error(`Offline journal mutation key '${key}' has conflicting command metadata.`);
32
+ }
33
+ }
34
+ for (const change of unique.values()) {
35
+ await transaction.append(change);
36
+ }
37
+ return result;
38
+ });
39
+ }
40
+ /** Fails CI when a product write route has no declared journal coverage decision. */
41
+ export function assertOfflineJournalCoverage(writeIds, coverage) {
42
+ const declared = Object.keys(coverage);
43
+ const missing = writeIds.filter((writeId) => !Object.hasOwn(coverage, writeId));
44
+ const unknown = declared.filter((writeId) => !writeIds.includes(writeId));
45
+ if (missing.length > 0 || unknown.length > 0) {
46
+ throw new Error(`Offline journal coverage mismatch: missing=[${missing.join(',')}], unknown=[${unknown.join(',')}].`);
47
+ }
48
+ for (const writeId of writeIds) {
49
+ const decision = coverage[writeId];
50
+ if (decision === 'not-replicated') {
51
+ continue;
52
+ }
53
+ if (decision.length === 0 ||
54
+ decision.some((sourceKey) => sourceKey.trim().length === 0) ||
55
+ new Set(decision).size !== decision.length) {
56
+ throw new Error(`Offline journal coverage for '${writeId}' must contain unique, non-empty source keys or 'not-replicated'.`);
57
+ }
58
+ }
59
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rdlabo/workers-hono-kit",
3
- "version": "0.10.3",
3
+ "version": "0.10.4",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"