@open-mercato/shared 0.6.4-develop.4371.1.8f3030407e → 0.6.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.
- package/.turbo/turbo-build.log +1 -1
- package/AGENTS.md +10 -0
- package/dist/lib/auth/apiKeyAuthCache.js +17 -6
- package/dist/lib/auth/apiKeyAuthCache.js.map +2 -2
- package/dist/lib/commands/command-bus.js +56 -47
- package/dist/lib/commands/command-bus.js.map +2 -2
- package/dist/lib/commands/flush.js +23 -1
- package/dist/lib/commands/flush.js.map +2 -2
- package/dist/lib/commands/index.js +6 -1
- package/dist/lib/commands/index.js.map +2 -2
- package/dist/lib/commands/redo.js +106 -0
- package/dist/lib/commands/redo.js.map +7 -0
- package/dist/lib/commands/runCrudCommandWrite.js +38 -0
- package/dist/lib/commands/runCrudCommandWrite.js.map +7 -0
- package/dist/lib/commands/scope.js +51 -37
- package/dist/lib/commands/scope.js.map +2 -2
- package/dist/lib/commands/types.js.map +2 -2
- package/dist/lib/crud/errors.js +22 -0
- package/dist/lib/crud/errors.js.map +2 -2
- package/dist/lib/crud/factory.js +16 -0
- package/dist/lib/crud/factory.js.map +2 -2
- package/dist/lib/crud/optimistic-lock-command.js +109 -0
- package/dist/lib/crud/optimistic-lock-command.js.map +7 -0
- package/dist/lib/crud/optimistic-lock-headers.js +15 -0
- package/dist/lib/crud/optimistic-lock-headers.js.map +7 -0
- package/dist/lib/crud/optimistic-lock-store.js +52 -0
- package/dist/lib/crud/optimistic-lock-store.js.map +7 -0
- package/dist/lib/crud/optimistic-lock.js +172 -0
- package/dist/lib/crud/optimistic-lock.js.map +7 -0
- package/dist/lib/data/engine.js +2 -2
- package/dist/lib/data/engine.js.map +2 -2
- package/dist/lib/di/container.js +18 -2
- package/dist/lib/di/container.js.map +2 -2
- package/dist/lib/encryption/aes.js +37 -3
- package/dist/lib/encryption/aes.js.map +2 -2
- package/dist/lib/encryption/kms.js +57 -23
- package/dist/lib/encryption/kms.js.map +2 -2
- package/dist/lib/encryption/subscriber.js +41 -8
- package/dist/lib/encryption/subscriber.js.map +2 -2
- package/dist/lib/encryption/tenantDataEncryptionService.js +35 -7
- package/dist/lib/encryption/tenantDataEncryptionService.js.map +2 -2
- package/dist/lib/i18n/context.js +5 -0
- package/dist/lib/i18n/context.js.map +2 -2
- package/dist/lib/query/engine.js +41 -31
- package/dist/lib/query/engine.js.map +2 -2
- package/dist/lib/version.js +1 -1
- package/dist/lib/version.js.map +1 -1
- package/dist/modules/integrations/types.js.map +2 -2
- package/dist/modules/search.js.map +2 -2
- package/package.json +8 -9
- package/src/lib/auth/__tests__/apiKeyAuthCache.test.ts +35 -0
- package/src/lib/auth/apiKeyAuthCache.ts +20 -6
- package/src/lib/commands/__tests__/command-bus.cache.test.ts +2 -0
- package/src/lib/commands/__tests__/command-bus.undo-audit.test.ts +2 -0
- package/src/lib/commands/__tests__/command-bus.undo-toctou.test.ts +122 -0
- package/src/lib/commands/__tests__/flush.test.ts +110 -9
- package/src/lib/commands/__tests__/redo.test.ts +265 -0
- package/src/lib/commands/__tests__/runCrudCommandWrite.test.ts +390 -0
- package/src/lib/commands/__tests__/scope.test.ts +48 -0
- package/src/lib/commands/command-bus.ts +62 -44
- package/src/lib/commands/flush.ts +79 -2
- package/src/lib/commands/index.ts +9 -0
- package/src/lib/commands/redo.ts +235 -0
- package/src/lib/commands/runCrudCommandWrite.ts +82 -0
- package/src/lib/commands/scope.ts +70 -55
- package/src/lib/commands/types.ts +54 -1
- package/src/lib/crud/__tests__/crud-factory.test.ts +106 -0
- package/src/lib/crud/__tests__/optimistic-lock-command.test.ts +425 -0
- package/src/lib/crud/__tests__/optimistic-lock-store.test.ts +194 -0
- package/src/lib/crud/__tests__/optimistic-lock.test.ts +526 -0
- package/src/lib/crud/errors.ts +29 -0
- package/src/lib/crud/factory.ts +23 -0
- package/src/lib/crud/optimistic-lock-command.ts +305 -0
- package/src/lib/crud/optimistic-lock-headers.ts +30 -0
- package/src/lib/crud/optimistic-lock-store.ts +87 -0
- package/src/lib/crud/optimistic-lock.ts +379 -0
- package/src/lib/data/engine.ts +11 -8
- package/src/lib/di/container.ts +17 -1
- package/src/lib/encryption/__tests__/dek-lifecycle.test.ts +194 -0
- package/src/lib/encryption/__tests__/kms.test.ts +44 -6
- package/src/lib/encryption/__tests__/lookupHash.test.ts +113 -0
- package/src/lib/encryption/__tests__/subscriber.change-tracking.test.ts +96 -0
- package/src/lib/encryption/__tests__/subscriber.deep-decrypt-collections.test.ts +123 -0
- package/src/lib/encryption/__tests__/tenantDataEncryptionService.test.ts +68 -1
- package/src/lib/encryption/aes.ts +78 -2
- package/src/lib/encryption/kms.ts +76 -24
- package/src/lib/encryption/subscriber.ts +54 -9
- package/src/lib/encryption/tenantDataEncryptionService.ts +53 -8
- package/src/lib/i18n/context.tsx +11 -0
- package/src/lib/query/__tests__/resolve-registered-entity-table.test.ts +83 -0
- package/src/lib/query/engine.ts +59 -30
- package/src/modules/integrations/types.ts +14 -0
- package/src/modules/notifications/handler.ts +7 -0
- package/src/modules/search.ts +9 -0
- package/src/modules/vector.ts +7 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { CrudHttpError } from "./errors.js";
|
|
2
|
+
import {
|
|
3
|
+
OPTIMISTIC_LOCK_CONFLICT_CODE,
|
|
4
|
+
OPTIMISTIC_LOCK_CONFLICT_ERROR,
|
|
5
|
+
OPTIMISTIC_LOCK_ENV_VAR,
|
|
6
|
+
OPTIMISTIC_LOCK_HEADER_NAME
|
|
7
|
+
} from "./optimistic-lock-headers.js";
|
|
8
|
+
import {
|
|
9
|
+
normalizeIsoToken,
|
|
10
|
+
parseOptimisticLockEnv
|
|
11
|
+
} from "./optimistic-lock.js";
|
|
12
|
+
function toIsoOrNull(value) {
|
|
13
|
+
if (value == null) return null;
|
|
14
|
+
if (value instanceof Date) {
|
|
15
|
+
const ms = value.getTime();
|
|
16
|
+
return Number.isFinite(ms) ? new Date(ms).toISOString() : null;
|
|
17
|
+
}
|
|
18
|
+
const trimmed = String(value).trim();
|
|
19
|
+
if (!trimmed) return null;
|
|
20
|
+
return normalizeIsoToken(trimmed);
|
|
21
|
+
}
|
|
22
|
+
function resolveConfig(envValue) {
|
|
23
|
+
return parseOptimisticLockEnv(envValue !== void 0 ? envValue : process.env[OPTIMISTIC_LOCK_ENV_VAR]);
|
|
24
|
+
}
|
|
25
|
+
function isResourceLockEnabled(config, resourceKind) {
|
|
26
|
+
if (config.mode === "off") return false;
|
|
27
|
+
if (config.mode === "all") return true;
|
|
28
|
+
return config.entities.has(resourceKind.toLowerCase());
|
|
29
|
+
}
|
|
30
|
+
function readOptimisticLockExpected(source) {
|
|
31
|
+
if (!source) return null;
|
|
32
|
+
const headers = source instanceof Headers ? source : source.headers instanceof Headers ? source.headers : null;
|
|
33
|
+
if (!headers) return null;
|
|
34
|
+
const direct = headers.get(OPTIMISTIC_LOCK_HEADER_NAME);
|
|
35
|
+
if (typeof direct === "string" && direct.trim().length > 0) return direct.trim();
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
function buildOptimisticLockConflictBody(currentIso, expectedIso) {
|
|
39
|
+
return {
|
|
40
|
+
error: OPTIMISTIC_LOCK_CONFLICT_ERROR,
|
|
41
|
+
code: OPTIMISTIC_LOCK_CONFLICT_CODE,
|
|
42
|
+
currentUpdatedAt: currentIso,
|
|
43
|
+
expectedUpdatedAt: expectedIso
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function assertOptimisticLock(input) {
|
|
47
|
+
const config = resolveConfig(input.envValue);
|
|
48
|
+
if (!isResourceLockEnabled(config, input.resourceKind)) return;
|
|
49
|
+
const expectedIso = toIsoOrNull(input.expected);
|
|
50
|
+
if (expectedIso == null) return;
|
|
51
|
+
const currentIso = toIsoOrNull(input.current);
|
|
52
|
+
if (currentIso == null) return;
|
|
53
|
+
if (currentIso === expectedIso) return;
|
|
54
|
+
throw new CrudHttpError(409, buildOptimisticLockConflictBody(currentIso, expectedIso));
|
|
55
|
+
}
|
|
56
|
+
function enforceCommandOptimisticLock(input) {
|
|
57
|
+
const expected = input.expected !== void 0 && input.expected !== null ? input.expected : readOptimisticLockExpected(input.request ?? null);
|
|
58
|
+
assertOptimisticLock({
|
|
59
|
+
resourceKind: input.resourceKind,
|
|
60
|
+
resourceId: input.resourceId,
|
|
61
|
+
expected,
|
|
62
|
+
current: input.current,
|
|
63
|
+
envValue: input.envValue
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function enforceRecordGoneIsConflict(input) {
|
|
67
|
+
const config = resolveConfig(input.envValue);
|
|
68
|
+
if (!isResourceLockEnabled(config, input.resourceKind)) return;
|
|
69
|
+
const clientSupplied = input.expected !== void 0 && input.expected !== null ? input.expected : readOptimisticLockExpected(input.request ?? null);
|
|
70
|
+
const expectedIso = toIsoOrNull(clientSupplied);
|
|
71
|
+
if (expectedIso == null) return;
|
|
72
|
+
throw new CrudHttpError(409, buildOptimisticLockConflictBody(expectedIso, expectedIso));
|
|
73
|
+
}
|
|
74
|
+
function createCommandOptimisticLockGuardService(options = {}) {
|
|
75
|
+
const resolveExpected = options.resolveExpected ?? null;
|
|
76
|
+
return {
|
|
77
|
+
async enforce(input) {
|
|
78
|
+
const config = resolveConfig(input.envValue);
|
|
79
|
+
if (!isResourceLockEnabled(config, input.resourceKind)) return;
|
|
80
|
+
const clientSupplied = input.expected !== void 0 && input.expected !== null ? input.expected : readOptimisticLockExpected(input.request ?? null);
|
|
81
|
+
const expectedFromHeader = toIsoOrNull(clientSupplied);
|
|
82
|
+
let expected = expectedFromHeader;
|
|
83
|
+
if (resolveExpected) {
|
|
84
|
+
const resolverInput = {
|
|
85
|
+
expectedFromHeader,
|
|
86
|
+
resourceKind: input.resourceKind,
|
|
87
|
+
resourceId: input.resourceId
|
|
88
|
+
};
|
|
89
|
+
expected = await resolveExpected(resolverInput);
|
|
90
|
+
}
|
|
91
|
+
assertOptimisticLock({
|
|
92
|
+
resourceKind: input.resourceKind,
|
|
93
|
+
resourceId: input.resourceId,
|
|
94
|
+
expected,
|
|
95
|
+
current: input.current,
|
|
96
|
+
envValue: input.envValue
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export {
|
|
102
|
+
assertOptimisticLock,
|
|
103
|
+
buildOptimisticLockConflictBody,
|
|
104
|
+
createCommandOptimisticLockGuardService,
|
|
105
|
+
enforceCommandOptimisticLock,
|
|
106
|
+
enforceRecordGoneIsConflict,
|
|
107
|
+
readOptimisticLockExpected
|
|
108
|
+
};
|
|
109
|
+
//# sourceMappingURL=optimistic-lock-command.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/lib/crud/optimistic-lock-command.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Generalist command-level OSS optimistic-locking helper.\n *\n * The CRUD guard (`optimistic-lock.ts` + `makeCrudRoute`) only protects\n * mutations that flow through the CRUD factory. Domain writes implemented via\n * the Command pattern \u2014 sales document sub-resources (lines, adjustments,\n * shipments, payments, returns), status transitions, quote\u2192order conversion,\n * etc. \u2014 run their own logic inside a command handler and never reach the CRUD\n * guard for the **aggregate** they mutate. This helper lets any command enforce\n * the same `updated_at` version check against an arbitrary target record\n * (typically the aggregate root, e.g. the parent order/quote) and fail with the\n * identical structured 409 the CRUD path returns.\n *\n * Contract (mirrors the CRUD guard so clients see one behavior):\n * - The client sends the expected version via the\n * `x-om-ext-optimistic-lock-expected-updated-at` header (or a command\n * accepts it as a typed input field and passes it as `expected`).\n * - The command loads the current record (it usually already does) and passes\n * its `updated_at` as `current`.\n * - On mismatch the helper throws `CrudHttpError(409, OptimisticLockConflictBody)`.\n *\n * Strictly additive: when no expected token is present (no header, no input\n * field) the helper is a no-op, so existing API consumers that don't send the\n * header keep working. Respects the same `OM_OPTIMISTIC_LOCK` env contract\n * (default ON; `off` disables; allow-list scopes by `resourceKind`).\n *\n * Spec: .ai/specs/2026-05-25-oss-optimistic-locking.md (\u00A7 command-level checks)\n * .ai/specs/2026-05-28-optimistic-locking-coverage-completion.md (Phase 4)\n */\nimport { CrudHttpError } from './errors'\nimport {\n OPTIMISTIC_LOCK_CONFLICT_CODE,\n OPTIMISTIC_LOCK_CONFLICT_ERROR,\n OPTIMISTIC_LOCK_ENV_VAR,\n OPTIMISTIC_LOCK_HEADER_NAME,\n type OptimisticLockConflictBody,\n} from './optimistic-lock-headers'\nimport {\n normalizeIsoToken,\n parseOptimisticLockEnv,\n type OptimisticLockConfig,\n type OptimisticLockResolverInput,\n type ResolveExpectedUpdatedAt,\n} from './optimistic-lock'\n\nfunction toIsoOrNull(value: string | Date | null | undefined): string | null {\n if (value == null) return null\n if (value instanceof Date) {\n const ms = value.getTime()\n return Number.isFinite(ms) ? new Date(ms).toISOString() : null\n }\n const trimmed = String(value).trim()\n if (!trimmed) return null\n return normalizeIsoToken(trimmed)\n}\n\nfunction resolveConfig(envValue: string | null | undefined): OptimisticLockConfig {\n return parseOptimisticLockEnv(envValue !== undefined ? envValue : process.env[OPTIMISTIC_LOCK_ENV_VAR])\n}\n\nfunction isResourceLockEnabled(config: OptimisticLockConfig, resourceKind: string): boolean {\n if (config.mode === 'off') return false\n if (config.mode === 'all') return true\n return config.entities.has(resourceKind.toLowerCase())\n}\n\n/**\n * Extract the expected `updated_at` token from a request's headers (or a bare\n * `Headers` object). Returns the trimmed header value, or `null` when absent /\n * empty. Does NOT normalize \u2014 `assertOptimisticLock` normalizes both sides.\n */\nexport function readOptimisticLockExpected(\n source: Request | Headers | null | undefined,\n): string | null {\n if (!source) return null\n const headers = source instanceof Headers\n ? source\n : (source as Request).headers instanceof Headers\n ? (source as Request).headers\n : null\n if (!headers) return null\n const direct = headers.get(OPTIMISTIC_LOCK_HEADER_NAME)\n if (typeof direct === 'string' && direct.trim().length > 0) return direct.trim()\n return null\n}\n\nexport function buildOptimisticLockConflictBody(\n currentIso: string,\n expectedIso: string,\n): OptimisticLockConflictBody {\n return {\n error: OPTIMISTIC_LOCK_CONFLICT_ERROR,\n code: OPTIMISTIC_LOCK_CONFLICT_CODE,\n currentUpdatedAt: currentIso,\n expectedUpdatedAt: expectedIso,\n }\n}\n\nexport type AssertOptimisticLockInput = {\n resourceKind: string\n resourceId: string\n /** Client-provided expected version (header value or typed input field). */\n expected: string | Date | null | undefined\n /** Current version loaded from the DB (typically the aggregate root's `updatedAt`). */\n current: string | Date | null | undefined\n /** Override `OM_OPTIMISTIC_LOCK` (mostly for tests). */\n envValue?: string | null\n}\n\n/**\n * Pure version assertion. Throws `CrudHttpError(409, OptimisticLockConflictBody)`\n * when the expected and current versions disagree.\n *\n * No-op (returns silently) when:\n * - the env disables the guard for this `resourceKind`,\n * - `expected` is missing / unparseable (strictly additive \u2014 clients that\n * don't send the token are never blocked),\n * - `current` is missing / unparseable (let the command's own 404 fire).\n */\nexport function assertOptimisticLock(input: AssertOptimisticLockInput): void {\n const config = resolveConfig(input.envValue)\n if (!isResourceLockEnabled(config, input.resourceKind)) return\n\n const expectedIso = toIsoOrNull(input.expected)\n if (expectedIso == null) return\n\n const currentIso = toIsoOrNull(input.current)\n if (currentIso == null) return\n\n if (currentIso === expectedIso) return\n\n throw new CrudHttpError(409, buildOptimisticLockConflictBody(currentIso, expectedIso))\n}\n\nexport type EnforceCommandOptimisticLockInput = {\n resourceKind: string\n resourceId: string\n /** Current version loaded from the DB (the aggregate root's `updatedAt`). */\n current: string | Date | null | undefined\n /**\n * Explicit expected version \u2014 wins over the request header. Use when the\n * command accepts the token as a typed input field instead of (or in addition\n * to) the extension header.\n */\n expected?: string | Date | null\n /** Request whose headers carry the expected token (e.g. `ctx.request`). */\n request?: Request | Headers | null\n /** Override `OM_OPTIMISTIC_LOCK` (mostly for tests). */\n envValue?: string | null\n}\n\n/**\n * Command-handler convenience: resolves the expected version from an explicit\n * override or the request header, then delegates to `assertOptimisticLock`.\n *\n * ```ts\n * enforceCommandOptimisticLock({\n * resourceKind: 'sales.order',\n * resourceId: order.id,\n * current: order.updatedAt,\n * request: ctx.request,\n * })\n * ```\n */\nexport function enforceCommandOptimisticLock(input: EnforceCommandOptimisticLockInput): void {\n const expected = input.expected !== undefined && input.expected !== null\n ? input.expected\n : readOptimisticLockExpected(input.request ?? null)\n assertOptimisticLock({\n resourceKind: input.resourceKind,\n resourceId: input.resourceId,\n expected,\n current: input.current,\n envValue: input.envValue,\n })\n}\n\nexport type EnforceRecordGoneIsConflictInput = {\n resourceKind: string\n resourceId: string\n /** Explicit expected version \u2014 wins over the request header. */\n expected?: string | Date | null\n /** Request whose headers carry the expected token (e.g. `ctx.request`). */\n request?: Request | Headers | null\n /** Override `OM_OPTIMISTIC_LOCK` (mostly for tests). */\n envValue?: string | null\n}\n\n/**\n * Command-handler convenience for the *concurrent-delete* race: when a command\n * cannot find its target record (it was deleted in another tab) AND the client\n * opted into optimistic locking (sent the expected-version header / token),\n * throw the SAME structured `CrudHttpError(409, OptimisticLockConflictBody)`\n * the version-mismatch path returns \u2014 so a stale modal save surfaces the unified\n * \"Record changed\" conflict bar instead of a bare, generic 404.\n *\n * Strictly additive and fail-open: a no-op (returns silently) when the env\n * disables the guard for `resourceKind`, or when no expected token is present\n * (plain API consumers that never sent the header keep their existing 404).\n * The caller MUST still throw its own 404 afterwards for that no-token path:\n *\n * ```ts\n * if (!interaction) {\n * enforceRecordGoneIsConflict({ resourceKind: 'customers.interaction', resourceId: id, request: ctx.request })\n * throw new CrudHttpError(404, { error: 'Interaction not found' })\n * }\n * ```\n *\n * The gone record has no current version, so `currentUpdatedAt` echoes the\n * expected token (the body is used only for the conflict-bar copy + diagnostics;\n * the client keys off `code`, not the timestamps).\n */\nexport function enforceRecordGoneIsConflict(input: EnforceRecordGoneIsConflictInput): void {\n const config = resolveConfig(input.envValue)\n if (!isResourceLockEnabled(config, input.resourceKind)) return\n const clientSupplied = input.expected !== undefined && input.expected !== null\n ? input.expected\n : readOptimisticLockExpected(input.request ?? null)\n const expectedIso = toIsoOrNull(clientSupplied)\n if (expectedIso == null) return\n throw new CrudHttpError(409, buildOptimisticLockConflictBody(expectedIso, expectedIso))\n}\n\n/**\n * DI-resolvable command-level optimistic-lock guard. This is the framework\n * seam that lets BOTH layers protect Command-pattern writes through one\n * contract:\n *\n * - **OSS** registers the default service (header/explicit token compare \u2014\n * identical to calling `enforceCommandOptimisticLock` directly).\n * - **Enterprise** (`record_locks`) re-registers the same DI key with a\n * `resolveExpected` that reads the held pessimistic lock's version, so a\n * stale command write fails with the same structured 409 WITHOUT any\n * command handler changing. Mirrors how enterprise already replaces the\n * CRUD-path `crudMutationGuardService` (see `optimistic-lock.ts`).\n *\n * Command handlers depend only on this interface (resolved from the container),\n * never on a concrete implementation \u2014 that is what makes the next-PR\n * enterprise extension a pure DI swap.\n */\nexport type CommandOptimisticLockGuardService = {\n /**\n * Enforce the version check for a command-level mutation against an\n * aggregate/record. Async because an enterprise resolver may load the\n * expected token from a lock record. No-op (resolves silently) when the env\n * disables the guard for `resourceKind` or when no expected token is\n * resolved \u2014 strictly additive, exactly like {@link enforceCommandOptimisticLock}.\n * Throws `CrudHttpError(409, OptimisticLockConflictBody)` on mismatch.\n */\n enforce: (input: EnforceCommandOptimisticLockInput) => Promise<void>\n}\n\nexport type CreateCommandOptimisticLockGuardServiceOptions = {\n /**\n * Override how the expected version is derived. Receives\n * `{ expectedFromHeader, resourceKind, resourceId }` (where\n * `expectedFromHeader` is the normalized client-supplied token \u2014 explicit\n * input or request header) and returns the expected token (or `null` to\n * skip). Defaults to \"use the client-supplied token\", which is the OSS\n * behavior. The enterprise `record_locks` module plugs a lock-backed\n * resolver here. Mirrors the CRUD guard's `resolveExpected`.\n */\n resolveExpected?: ResolveExpectedUpdatedAt\n}\n\n/**\n * Build a {@link CommandOptimisticLockGuardService}. With no options it is\n * behaviourally identical to {@link enforceCommandOptimisticLock} (header/\n * explicit compare), so the OSS default is a thin wrapper. Pass\n * `resolveExpected` to override token resolution (enterprise extension point).\n */\nexport function createCommandOptimisticLockGuardService(\n options: CreateCommandOptimisticLockGuardServiceOptions = {},\n): CommandOptimisticLockGuardService {\n const resolveExpected: ResolveExpectedUpdatedAt | null = options.resolveExpected ?? null\n return {\n async enforce(input: EnforceCommandOptimisticLockInput): Promise<void> {\n const config = resolveConfig(input.envValue)\n if (!isResourceLockEnabled(config, input.resourceKind)) return\n\n const clientSupplied = input.expected !== undefined && input.expected !== null\n ? input.expected\n : readOptimisticLockExpected(input.request ?? null)\n const expectedFromHeader = toIsoOrNull(clientSupplied)\n\n let expected: string | null = expectedFromHeader\n if (resolveExpected) {\n const resolverInput: OptimisticLockResolverInput = {\n expectedFromHeader,\n resourceKind: input.resourceKind,\n resourceId: input.resourceId,\n }\n expected = await resolveExpected(resolverInput)\n }\n\n assertOptimisticLock({\n resourceKind: input.resourceKind,\n resourceId: input.resourceId,\n expected,\n current: input.current,\n envValue: input.envValue,\n })\n },\n }\n}\n"],
|
|
5
|
+
"mappings": "AA6BA,SAAS,qBAAqB;AAC9B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAIK;AAEP,SAAS,YAAY,OAAwD;AAC3E,MAAI,SAAS,KAAM,QAAO;AAC1B,MAAI,iBAAiB,MAAM;AACzB,UAAM,KAAK,MAAM,QAAQ;AACzB,WAAO,OAAO,SAAS,EAAE,IAAI,IAAI,KAAK,EAAE,EAAE,YAAY,IAAI;AAAA,EAC5D;AACA,QAAM,UAAU,OAAO,KAAK,EAAE,KAAK;AACnC,MAAI,CAAC,QAAS,QAAO;AACrB,SAAO,kBAAkB,OAAO;AAClC;AAEA,SAAS,cAAc,UAA2D;AAChF,SAAO,uBAAuB,aAAa,SAAY,WAAW,QAAQ,IAAI,uBAAuB,CAAC;AACxG;AAEA,SAAS,sBAAsB,QAA8B,cAA+B;AAC1F,MAAI,OAAO,SAAS,MAAO,QAAO;AAClC,MAAI,OAAO,SAAS,MAAO,QAAO;AAClC,SAAO,OAAO,SA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MAAM,WACN,2BAA2B,MAAM,WAAW,IAAI;AACpD,QAAM,cAAc,YAAY,cAAc;AAC9C,MAAI,eAAe,KAAM;AACzB,QAAM,IAAI,cAAc,KAAK,gCAAgC,aAAa,WAAW,CAAC;AACxF;AAkDO,SAAS,wCACd,UAA0D,CAAC,GACxB;AACnC,QAAM,kBAAmD,QAAQ,mBAAmB;AACpF,SAAO;AAAA,IACL,MAAM,QAAQ,OAAyD;AACrE,YAAM,SAAS,cAAc,MAAM,QAAQ;AAC3C,UAAI,CAAC,sBAAsB,QAAQ,MAAM,YAAY,EAAG;AAExD,YAAM,iBAAiB,MAAM,aAAa,UAAa,MAAM,aAAa,OACtE,MAAM,WACN,2BAA2B,MAAM,WAAW,IAAI;AACpD,YAAM,qBAAqB,YAAY,cAAc;AAErD,UAAI,WAA0B;AAC9B,UAAI,iBAAiB;AACnB,cAAM,gBAA6C;AAAA,UACjD;AAAA,UACA,cAAc,MAAM;AAAA,UACpB,YAAY,MAAM;AAAA,QACpB;AACA,mBAAW,MAAM,gBAAgB,aAAa;AAAA,MAChD;AAEA,2BAAqB;AAAA,QACnB,cAAc,MAAM;AAAA,QACpB,YAAY,MAAM;AAAA,QAClB;AAAA,QACA,SAAS,MAAM;AAAA,QACf,UAAU,MAAM;AAAA,MAClB,CAAC;AAAA,IACH;AAAA,EACF;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const OPTIMISTIC_LOCK_MODULE_ID = "optimistic_lock";
|
|
2
|
+
const OPTIMISTIC_LOCK_HEADER_NAME = "x-om-ext-optimistic-lock-expected-updated-at";
|
|
3
|
+
const OPTIMISTIC_LOCK_CONFLICT_CODE = "optimistic_lock_conflict";
|
|
4
|
+
const OPTIMISTIC_LOCK_CONFLICT_ERROR = "record_modified";
|
|
5
|
+
const OPTIMISTIC_LOCK_ENV_VAR = "OM_OPTIMISTIC_LOCK";
|
|
6
|
+
const OPTIMISTIC_LOCK_DEFAULT_PRIORITY = 50;
|
|
7
|
+
export {
|
|
8
|
+
OPTIMISTIC_LOCK_CONFLICT_CODE,
|
|
9
|
+
OPTIMISTIC_LOCK_CONFLICT_ERROR,
|
|
10
|
+
OPTIMISTIC_LOCK_DEFAULT_PRIORITY,
|
|
11
|
+
OPTIMISTIC_LOCK_ENV_VAR,
|
|
12
|
+
OPTIMISTIC_LOCK_HEADER_NAME,
|
|
13
|
+
OPTIMISTIC_LOCK_MODULE_ID
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=optimistic-lock-headers.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/lib/crud/optimistic-lock-headers.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Wire constants for the OSS opt-in optimistic-locking guard.\n *\n * The header name follows the project's extension-header convention\n * (`x-om-ext-<moduleId>-<key>`, see `umes/extension-headers.ts`). The\n * module id used by env opt-in is `optimistic_lock` (snake_case), but\n * the HTTP header itself uses dash-separated `optimistic-lock` because\n * many HTTP intermediaries (nginx, some fetch implementations) strip\n * underscored header names \u2014 see RFC 7230 \u00A73.2.6.\n *\n * Spec: .ai/specs/2026-05-25-oss-optimistic-locking.md \u00A73.2\n */\nexport const OPTIMISTIC_LOCK_MODULE_ID = 'optimistic_lock'\n\nexport const OPTIMISTIC_LOCK_HEADER_NAME = 'x-om-ext-optimistic-lock-expected-updated-at'\n\nexport const OPTIMISTIC_LOCK_CONFLICT_CODE = 'optimistic_lock_conflict'\n\nexport const OPTIMISTIC_LOCK_CONFLICT_ERROR = 'record_modified'\n\nexport const OPTIMISTIC_LOCK_ENV_VAR = 'OM_OPTIMISTIC_LOCK'\n\nexport const OPTIMISTIC_LOCK_DEFAULT_PRIORITY = 50\n\nexport type OptimisticLockConflictBody = {\n error: typeof OPTIMISTIC_LOCK_CONFLICT_ERROR\n code: typeof OPTIMISTIC_LOCK_CONFLICT_CODE\n currentUpdatedAt: string\n expectedUpdatedAt: string\n}\n"],
|
|
5
|
+
"mappings": "AAYO,MAAM,4BAA4B;AAElC,MAAM,8BAA8B;AAEpC,MAAM,gCAAgC;AAEtC,MAAM,iCAAiC;AAEvC,MAAM,0BAA0B;AAEhC,MAAM,mCAAmC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const GLOBAL_KEY = "__openMercatoOptimisticLockReaders__";
|
|
2
|
+
function readGlobal() {
|
|
3
|
+
try {
|
|
4
|
+
const value = globalThis[GLOBAL_KEY];
|
|
5
|
+
if (value && typeof value === "object") {
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
return {};
|
|
9
|
+
} catch {
|
|
10
|
+
return {};
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function writeGlobal(value) {
|
|
14
|
+
try {
|
|
15
|
+
;
|
|
16
|
+
globalThis[GLOBAL_KEY] = value;
|
|
17
|
+
} catch {
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function registerOptimisticLockReaders(readers) {
|
|
21
|
+
const existing = readGlobal();
|
|
22
|
+
writeGlobal({ ...existing, ...readers });
|
|
23
|
+
}
|
|
24
|
+
function registerOptimisticLockReaderIfAbsent(readers) {
|
|
25
|
+
const existing = readGlobal();
|
|
26
|
+
const next = { ...existing };
|
|
27
|
+
const written = [];
|
|
28
|
+
for (const [key, reader] of Object.entries(readers)) {
|
|
29
|
+
if (!(key in existing)) {
|
|
30
|
+
next[key] = reader;
|
|
31
|
+
written.push(key);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (written.length > 0) writeGlobal(next);
|
|
35
|
+
return written;
|
|
36
|
+
}
|
|
37
|
+
function getAllOptimisticLockReaders() {
|
|
38
|
+
return readGlobal();
|
|
39
|
+
}
|
|
40
|
+
function clearOptimisticLockReadersForTests() {
|
|
41
|
+
try {
|
|
42
|
+
delete globalThis[GLOBAL_KEY];
|
|
43
|
+
} catch {
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
clearOptimisticLockReadersForTests,
|
|
48
|
+
getAllOptimisticLockReaders,
|
|
49
|
+
registerOptimisticLockReaderIfAbsent,
|
|
50
|
+
registerOptimisticLockReaders
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=optimistic-lock-store.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/lib/crud/optimistic-lock-store.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Global registry of OSS optimistic-lock readers keyed by `resourceKind`.\n *\n * Multiple modules can contribute readers without conflicting on the\n * single Awilix `crudMutationGuardService` slot: each module calls\n * `registerOptimisticLockReaders({...})` from its `di.ts` at module-load\n * time, and any one of them can register the\n * `crudMutationGuardService` Awilix binding (Awilix replaces same-key\n * registrations, so the *last loaded* wins \u2014 but every binding points\n * to the same store-backed factory, so the resulting guard set is\n * identical regardless of order).\n *\n * Mirrors the `mutation-guard-store.ts` HMR-safe globalThis pattern.\n *\n * Spec: .ai/specs/2026-05-25-oss-optimistic-locking.md\n */\nimport type { OptimisticLockCurrentReader } from './optimistic-lock'\n\nconst GLOBAL_KEY = '__openMercatoOptimisticLockReaders__'\n\nfunction readGlobal(): Record<string, OptimisticLockCurrentReader> {\n try {\n const value = (globalThis as Record<string, unknown>)[GLOBAL_KEY]\n if (value && typeof value === 'object') {\n return value as Record<string, OptimisticLockCurrentReader>\n }\n return {}\n } catch {\n return {}\n }\n}\n\nfunction writeGlobal(value: Record<string, OptimisticLockCurrentReader>): void {\n try {\n ;(globalThis as Record<string, unknown>)[GLOBAL_KEY] = value\n } catch {\n // ignore global assignment failures in restricted runtimes\n }\n}\n\n/**\n * Register optimistic-lock readers for one or more `resourceKind` values.\n * Idempotent for same-key calls (later registration overrides earlier).\n */\nexport function registerOptimisticLockReaders(\n readers: Record<string, OptimisticLockCurrentReader>,\n): void {\n const existing = readGlobal()\n writeGlobal({ ...existing, ...readers })\n}\n\n/**\n * Register optimistic-lock readers only for keys that have no reader yet.\n * Use this for fallback / generic registrations (e.g. the auto-registration\n * driven by `makeCrudRoute`) so module-level hand-wired readers \u2014 which\n * register first via `di.ts` \u2014 always win.\n *\n * Returns the set of keys that were actually written, which makes the helper\n * easy to assert on in tests and useful for diagnostics in callers.\n */\nexport function registerOptimisticLockReaderIfAbsent(\n readers: Record<string, OptimisticLockCurrentReader>,\n): string[] {\n const existing = readGlobal()\n const next: Record<string, OptimisticLockCurrentReader> = { ...existing }\n const written: string[] = []\n for (const [key, reader] of Object.entries(readers)) {\n if (!(key in existing)) {\n next[key] = reader\n written.push(key)\n }\n }\n if (written.length > 0) writeGlobal(next)\n return written\n}\n\nexport function getAllOptimisticLockReaders(): Record<string, OptimisticLockCurrentReader> {\n return readGlobal()\n}\n\nexport function clearOptimisticLockReadersForTests(): void {\n try {\n delete (globalThis as Record<string, unknown>)[GLOBAL_KEY]\n } catch {\n // ignore\n }\n}\n"],
|
|
5
|
+
"mappings": "AAkBA,MAAM,aAAa;AAEnB,SAAS,aAA0D;AACjE,MAAI;AACF,UAAM,QAAS,WAAuC,UAAU;AAChE,QAAI,SAAS,OAAO,UAAU,UAAU;AACtC,aAAO;AAAA,IACT;AACA,WAAO,CAAC;AAAA,EACV,QAAQ;AACN,WAAO,CAAC;AAAA,EACV;AACF;AAEA,SAAS,YAAY,OAA0D;AAC7E,MAAI;AACF;AAAC,IAAC,WAAuC,UAAU,IAAI;AAAA,EACzD,QAAQ;AAAA,EAER;AACF;AAMO,SAAS,8BACd,SACM;AACN,QAAM,WAAW,WAAW;AAC5B,cAAY,EAAE,GAAG,UAAU,GAAG,QAAQ,CAAC;AACzC;AAWO,SAAS,qCACd,SACU;AACV,QAAM,WAAW,WAAW;AAC5B,QAAM,OAAoD,EAAE,GAAG,SAAS;AACxE,QAAM,UAAoB,CAAC;AAC3B,aAAW,CAAC,KAAK,MAAM,KAAK,OAAO,QAAQ,OAAO,GAAG;AACnD,QAAI,EAAE,OAAO,WAAW;AACtB,WAAK,GAAG,IAAI;AACZ,cAAQ,KAAK,GAAG;AAAA,IAClB;AAAA,EACF;AACA,MAAI,QAAQ,SAAS,EAAG,aAAY,IAAI;AACxC,SAAO;AACT;AAEO,SAAS,8BAA2E;AACzF,SAAO,WAAW;AACpB;AAEO,SAAS,qCAA2C;AACzD,MAAI;AACF,WAAQ,WAAuC,UAAU;AAAA,EAC3D,QAAQ;AAAA,EAER;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OPTIMISTIC_LOCK_CONFLICT_CODE,
|
|
3
|
+
OPTIMISTIC_LOCK_CONFLICT_ERROR,
|
|
4
|
+
OPTIMISTIC_LOCK_ENV_VAR,
|
|
5
|
+
OPTIMISTIC_LOCK_HEADER_NAME
|
|
6
|
+
} from "./optimistic-lock-headers.js";
|
|
7
|
+
import { getAllOptimisticLockReaders } from "./optimistic-lock-store.js";
|
|
8
|
+
const OPTIMISTIC_LOCK_OFF_TOKENS = /* @__PURE__ */ new Set([
|
|
9
|
+
"off",
|
|
10
|
+
"false",
|
|
11
|
+
"0",
|
|
12
|
+
"no",
|
|
13
|
+
"disabled",
|
|
14
|
+
"none"
|
|
15
|
+
]);
|
|
16
|
+
function parseOptimisticLockEnv(raw) {
|
|
17
|
+
if (raw == null) return { mode: "all" };
|
|
18
|
+
const trimmed = String(raw).trim();
|
|
19
|
+
if (trimmed === "") return { mode: "all" };
|
|
20
|
+
const tokens = trimmed.split(",").map((token) => token.trim().toLowerCase()).filter((token) => token.length > 0);
|
|
21
|
+
if (tokens.length === 0) return { mode: "all" };
|
|
22
|
+
if (tokens.some((token) => OPTIMISTIC_LOCK_OFF_TOKENS.has(token))) return { mode: "off" };
|
|
23
|
+
if (tokens.includes("all")) return { mode: "all" };
|
|
24
|
+
return { mode: "allowlist", entities: new Set(tokens) };
|
|
25
|
+
}
|
|
26
|
+
const defaultResolveExpectedUpdatedAt = ({ expectedFromHeader }) => expectedFromHeader;
|
|
27
|
+
function createGenericOptimisticLockReader(opts) {
|
|
28
|
+
const idField = opts.idField ?? "id";
|
|
29
|
+
const tenantField = opts.tenantField === null ? null : opts.tenantField ?? "tenantId";
|
|
30
|
+
const orgField = opts.orgField === null ? null : opts.orgField ?? "organizationId";
|
|
31
|
+
const softDeleteField = opts.softDeleteField === null ? null : opts.softDeleteField ?? "deletedAt";
|
|
32
|
+
const updatedAtField = opts.updatedAtField ?? "updatedAt";
|
|
33
|
+
const extraFilter = opts.extraFilter ?? {};
|
|
34
|
+
return async (em, { resourceId, tenantId, organizationId }) => {
|
|
35
|
+
const filter = { [idField]: resourceId };
|
|
36
|
+
if (tenantField) filter[tenantField] = tenantId;
|
|
37
|
+
if (orgField && organizationId) filter[orgField] = organizationId;
|
|
38
|
+
if (softDeleteField) filter[softDeleteField] = null;
|
|
39
|
+
for (const [key, value] of Object.entries(extraFilter)) filter[key] = value;
|
|
40
|
+
try {
|
|
41
|
+
const row = await em.findOne(opts.entity, filter, {
|
|
42
|
+
fields: [updatedAtField]
|
|
43
|
+
});
|
|
44
|
+
if (!row || typeof row !== "object") return null;
|
|
45
|
+
const value = row[updatedAtField];
|
|
46
|
+
if (value instanceof Date) return value.toISOString();
|
|
47
|
+
if (typeof value === "string" && value.length > 0) return value;
|
|
48
|
+
return null;
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function readHeader(headers, name) {
|
|
55
|
+
const direct = headers.get(name);
|
|
56
|
+
if (typeof direct === "string" && direct.trim().length > 0) return direct.trim();
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
function normalizeIsoToken(raw) {
|
|
60
|
+
const ms = Date.parse(raw);
|
|
61
|
+
if (!Number.isFinite(ms)) return null;
|
|
62
|
+
return new Date(ms).toISOString();
|
|
63
|
+
}
|
|
64
|
+
function buildConflictBody(currentIso, expectedIso) {
|
|
65
|
+
return {
|
|
66
|
+
error: OPTIMISTIC_LOCK_CONFLICT_ERROR,
|
|
67
|
+
code: OPTIMISTIC_LOCK_CONFLICT_CODE,
|
|
68
|
+
currentUpdatedAt: currentIso,
|
|
69
|
+
expectedUpdatedAt: expectedIso
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function createOptimisticLockGuardService(opts) {
|
|
73
|
+
const envValue = opts.envValue !== void 0 ? opts.envValue : process.env[OPTIMISTIC_LOCK_ENV_VAR];
|
|
74
|
+
const config = parseOptimisticLockEnv(envValue);
|
|
75
|
+
const resolveExpected = opts.resolveExpected ?? defaultResolveExpectedUpdatedAt;
|
|
76
|
+
const debugEnabled = process.env.OM_OPTIMISTIC_LOCK_DEBUG === "1";
|
|
77
|
+
function isEntityEnabled(resourceKind) {
|
|
78
|
+
if (config.mode === "off") return false;
|
|
79
|
+
if (config.mode === "all") return true;
|
|
80
|
+
return config.entities.has(resourceKind.toLowerCase());
|
|
81
|
+
}
|
|
82
|
+
async function validateMutation(input) {
|
|
83
|
+
if (config.mode === "off") {
|
|
84
|
+
return { ok: true, shouldRunAfterSuccess: false };
|
|
85
|
+
}
|
|
86
|
+
if (input.operation !== "update" && input.operation !== "delete") {
|
|
87
|
+
return { ok: true, shouldRunAfterSuccess: false };
|
|
88
|
+
}
|
|
89
|
+
if (!isEntityEnabled(input.resourceKind)) {
|
|
90
|
+
return { ok: true, shouldRunAfterSuccess: false };
|
|
91
|
+
}
|
|
92
|
+
const readers = opts.readers ?? getAllOptimisticLockReaders();
|
|
93
|
+
const reader = readers[input.resourceKind];
|
|
94
|
+
if (!reader) {
|
|
95
|
+
return { ok: true, shouldRunAfterSuccess: false };
|
|
96
|
+
}
|
|
97
|
+
const expectedRaw = readHeader(input.requestHeaders, OPTIMISTIC_LOCK_HEADER_NAME);
|
|
98
|
+
const resolvedExpected = await resolveExpected({
|
|
99
|
+
expectedFromHeader: expectedRaw,
|
|
100
|
+
resourceKind: input.resourceKind,
|
|
101
|
+
resourceId: input.resourceId
|
|
102
|
+
});
|
|
103
|
+
if (resolvedExpected == null) {
|
|
104
|
+
return { ok: true, shouldRunAfterSuccess: false };
|
|
105
|
+
}
|
|
106
|
+
const expectedIso = normalizeIsoToken(resolvedExpected);
|
|
107
|
+
if (expectedIso == null) {
|
|
108
|
+
return { ok: true, shouldRunAfterSuccess: false };
|
|
109
|
+
}
|
|
110
|
+
const em = opts.getEm();
|
|
111
|
+
const currentRaw = await reader(em, {
|
|
112
|
+
resourceKind: input.resourceKind,
|
|
113
|
+
resourceId: input.resourceId,
|
|
114
|
+
tenantId: input.tenantId,
|
|
115
|
+
organizationId: input.organizationId ?? null
|
|
116
|
+
});
|
|
117
|
+
if (currentRaw == null) {
|
|
118
|
+
return { ok: true, shouldRunAfterSuccess: false };
|
|
119
|
+
}
|
|
120
|
+
const currentIso = normalizeIsoToken(currentRaw);
|
|
121
|
+
if (currentIso == null) {
|
|
122
|
+
return { ok: true, shouldRunAfterSuccess: false };
|
|
123
|
+
}
|
|
124
|
+
if (currentIso === expectedIso) {
|
|
125
|
+
if (debugEnabled) {
|
|
126
|
+
console.log("[optimistic-lock] match", {
|
|
127
|
+
resourceKind: input.resourceKind,
|
|
128
|
+
resourceId: input.resourceId,
|
|
129
|
+
operation: input.operation,
|
|
130
|
+
currentIso,
|
|
131
|
+
expectedIso
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return { ok: true, shouldRunAfterSuccess: false };
|
|
135
|
+
}
|
|
136
|
+
if (debugEnabled) {
|
|
137
|
+
console.log("[optimistic-lock] CONFLICT", {
|
|
138
|
+
resourceKind: input.resourceKind,
|
|
139
|
+
resourceId: input.resourceId,
|
|
140
|
+
operation: input.operation,
|
|
141
|
+
tenantId: input.tenantId,
|
|
142
|
+
organizationId: input.organizationId ?? null,
|
|
143
|
+
expectedRaw: resolvedExpected,
|
|
144
|
+
expectedIso,
|
|
145
|
+
currentRaw,
|
|
146
|
+
currentIso
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
ok: false,
|
|
151
|
+
status: 409,
|
|
152
|
+
body: buildConflictBody(currentIso, expectedIso)
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
async function afterMutationSuccess(_input) {
|
|
156
|
+
}
|
|
157
|
+
function getConfig() {
|
|
158
|
+
return config;
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
validateMutation,
|
|
162
|
+
afterMutationSuccess,
|
|
163
|
+
getConfig
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
export {
|
|
167
|
+
createGenericOptimisticLockReader,
|
|
168
|
+
createOptimisticLockGuardService,
|
|
169
|
+
normalizeIsoToken,
|
|
170
|
+
parseOptimisticLockEnv
|
|
171
|
+
};
|
|
172
|
+
//# sourceMappingURL=optimistic-lock.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/lib/crud/optimistic-lock.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * OSS optimistic-locking guard service.\n *\n * Registered as `crudMutationGuardService` (by the platform DI bootstrap and\n * by hand-wiring modules that override the default reader). Compares the\n * client-sent expected `updated_at` (carried via the extension header\n * defined in `optimistic-lock-headers.ts`) against the current DB\n * `updated_at` for the target entity; on mismatch returns HTTP 409 with the\n * structured `OptimisticLockConflictBody`.\n *\n * **Default ON** (Phase 14, 2026-05-27). Activate / scope / disable via\n * `OM_OPTIMISTIC_LOCK`:\n * - unset / empty / whitespace \u2192 ON for every CRUD entity (`{ mode: 'all' }`)\n * - `all` \u2192 all entities (explicit form of the default)\n * - `customers.company,sales.order` \u2192 allow-list (lowercased, trimmed, deduped)\n * - `off` / `false` / `0` / `no` / `disabled` / `none` \u2192 fully disabled\n *\n * The guard is still strictly additive at runtime: clients that do not send\n * the `x-om-ext-optimistic-lock-expected-updated-at` header pass through\n * unchanged, so flipping the default to ON cannot introduce new 409s on\n * existing API consumers. Pages that opt into the round-trip (via\n * `CrudForm`'s `optimisticLockUpdatedAt` prop or by calling\n * `buildOptimisticLockHeader`) gain protection without any per-deployment\n * env change.\n *\n * Cannot be registered as a static `data/guards.ts` `MutationGuard` because\n * the static `validate(input)` receives only `MutationGuardInput` \u2014 no\n * container / em access. Stateful checks that need to read current DB state\n * MUST go through the DI service path (this file).\n *\n * Spec: .ai/specs/2026-05-25-oss-optimistic-locking.md\n */\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport type {\n CrudMutationGuardValidateInput,\n CrudMutationGuardValidationResult,\n CrudMutationGuardAfterSuccessInput,\n} from './mutation-guard'\nimport {\n OPTIMISTIC_LOCK_CONFLICT_CODE,\n OPTIMISTIC_LOCK_CONFLICT_ERROR,\n OPTIMISTIC_LOCK_ENV_VAR,\n OPTIMISTIC_LOCK_HEADER_NAME,\n type OptimisticLockConflictBody,\n} from './optimistic-lock-headers'\nimport { getAllOptimisticLockReaders } from './optimistic-lock-store'\n\nexport type OptimisticLockConfig =\n | { mode: 'off' }\n | { mode: 'all' }\n | { mode: 'allowlist'; entities: ReadonlySet<string> }\n\n/**\n * Tokens (case-insensitive, single-token-only) that explicitly disable the\n * guard. Spelled out as a fixed set so tests can pin them; deliberately the\n * same shape `parseBooleanToken` recognises so operators can mirror existing\n * habit. Mixing an off-token with other entities is invalid input \u2014 we treat\n * any presence of an off-token in the comma list as a request to disable.\n */\nconst OPTIMISTIC_LOCK_OFF_TOKENS: ReadonlySet<string> = new Set([\n 'off',\n 'false',\n '0',\n 'no',\n 'disabled',\n 'none',\n])\n\n/**\n * Pure parser for `OM_OPTIMISTIC_LOCK`. Exported separately so tests can\n * exercise the grammar without spinning up the full service.\n *\n * Default is **ON** (`{ mode: 'all' }`) \u2014 unset / empty / whitespace input\n * activates the guard for every CRUD entity. Operators opt out via\n * `OM_OPTIMISTIC_LOCK=off` (or `false` / `0` / `no` / `disabled` / `none`).\n */\nexport function parseOptimisticLockEnv(raw: string | undefined | null): OptimisticLockConfig {\n if (raw == null) return { mode: 'all' }\n const trimmed = String(raw).trim()\n if (trimmed === '') return { mode: 'all' }\n\n const tokens = trimmed\n .split(',')\n .map((token) => token.trim().toLowerCase())\n .filter((token) => token.length > 0)\n\n if (tokens.length === 0) return { mode: 'all' }\n if (tokens.some((token) => OPTIMISTIC_LOCK_OFF_TOKENS.has(token))) return { mode: 'off' }\n if (tokens.includes('all')) return { mode: 'all' }\n\n return { mode: 'allowlist', entities: new Set(tokens) }\n}\n\nexport type OptimisticLockResolverInput = {\n expectedFromHeader: string | null\n resourceKind: string\n resourceId: string\n}\n\n/**\n * Hook reserved for the enterprise `record_locks` module to override token\n * resolution (e.g. read the expected token from a lock record instead of\n * the request header). OSS keeps the default = \"what the client sent\".\n *\n * Documented as part of the enterprise extension contract; not used in OSS\n * itself.\n */\nexport type ResolveExpectedUpdatedAt = (\n input: OptimisticLockResolverInput,\n) => Promise<string | null> | string | null\n\nconst defaultResolveExpectedUpdatedAt: ResolveExpectedUpdatedAt = ({ expectedFromHeader }) =>\n expectedFromHeader\n\nexport type OptimisticLockCurrentReader = (\n em: EntityManager,\n input: { resourceKind: string; resourceId: string; tenantId: string; organizationId: string | null },\n) => Promise<string | null>\n\nexport type GenericOptimisticLockReaderOptions = {\n /** MikroORM entity class. */\n entity: unknown\n /** Primary key field. Defaults to `id`. */\n idField?: string\n /** Tenant scope field. Defaults to `tenantId`. Pass `null` to skip tenant scoping (rare \u2014 only when the entity itself has no `tenantId` column). */\n tenantField?: string | null\n /** Organization scope field. Defaults to `organizationId`. Pass `null` to skip organization scoping. */\n orgField?: string | null\n /** Soft-delete column. Defaults to `deletedAt`. Pass `null` to skip the implicit not-deleted filter. */\n softDeleteField?: string | null\n /** Optional fixed filter merged into every query (e.g. `{ kind: 'company' }` for a discriminated table). */\n extraFilter?: Record<string, unknown>\n /** Optional ORM field name carrying the timestamp. Defaults to `updatedAt`. */\n updatedAtField?: string\n}\n\n/**\n * Build a generic optimistic-lock reader for any ORM entity that follows the\n * platform conventions (`id` + `tenantId` + `organizationId` + `deletedAt` +\n * `updatedAt`). The reader projects only the timestamp column so PII never\n * materializes.\n *\n * Used by `makeCrudRoute` to auto-register one reader per CRUD route at\n * module-load time (see Phase 13 of the OSS optimistic-locking spec).\n * Module authors who need bespoke filtering (e.g. discriminator on a shared\n * table) keep registering their own reader via `registerOptimisticLockReaders`\n * \u2014 those hand-wired registrations win because they land first.\n *\n * Fail-open contract: if the underlying `findOne` throws (missing column,\n * schema drift, mid-migration) the reader returns `null`, which the guard\n * treats as \"entity already gone\" and lets the CRUD path's own 404 fire.\n * We MUST NOT throw out of the reader \u2014 that would 500 every mutation on\n * the affected entity instead of opting it out of the optimistic check.\n */\nexport function createGenericOptimisticLockReader(\n opts: GenericOptimisticLockReaderOptions,\n): OptimisticLockCurrentReader {\n const idField = opts.idField ?? 'id'\n const tenantField = opts.tenantField === null ? null : opts.tenantField ?? 'tenantId'\n const orgField = opts.orgField === null ? null : opts.orgField ?? 'organizationId'\n const softDeleteField = opts.softDeleteField === null ? null : opts.softDeleteField ?? 'deletedAt'\n const updatedAtField = opts.updatedAtField ?? 'updatedAt'\n const extraFilter = opts.extraFilter ?? {}\n\n return async (em, { resourceId, tenantId, organizationId }) => {\n const filter: Record<string, unknown> = { [idField]: resourceId }\n if (tenantField) filter[tenantField] = tenantId\n if (orgField && organizationId) filter[orgField] = organizationId\n if (softDeleteField) filter[softDeleteField] = null\n for (const [key, value] of Object.entries(extraFilter)) filter[key] = value\n\n try {\n const row = await em.findOne(opts.entity as never, filter as never, {\n fields: [updatedAtField] as never,\n })\n if (!row || typeof row !== 'object') return null\n const value = (row as Record<string, unknown>)[updatedAtField]\n if (value instanceof Date) return value.toISOString()\n if (typeof value === 'string' && value.length > 0) return value\n return null\n } catch {\n return null\n }\n }\n}\n\nexport type OptimisticLockGuardOptions = {\n /** EntityManager resolver. Container-bound via DI in real usage. */\n getEm: () => EntityManager\n /**\n * Maps `resourceKind` \u2192 reader that returns the current\n * `updated_at` as an ISO string (or null when not found).\n *\n * The reader receives the EM so module authors can choose the\n * right `findOne` shape for their entity (`findOneWithDecryption`\n * when sensitive, plain `findOne` otherwise \u2014 but only requesting\n * `updated_at` so no PII materializes).\n *\n * When omitted, the service pulls readers from the shared\n * `optimistic-lock-store` (the recommended pattern for multi-module\n * deployments \u2014 each module registers its own readers via\n * `registerOptimisticLockReaders(...)` at module-load time).\n */\n readers?: Record<string, OptimisticLockCurrentReader>\n /** Override env source (mostly for tests). Defaults to `process.env`. */\n envValue?: string | null\n /** Override the token resolver. Defaults to \"use the header value\". */\n resolveExpected?: ResolveExpectedUpdatedAt\n}\n\nexport type OptimisticLockGuardService = {\n validateMutation: (input: CrudMutationGuardValidateInput) => Promise<CrudMutationGuardValidationResult>\n afterMutationSuccess: (input: CrudMutationGuardAfterSuccessInput) => Promise<void>\n /** Exposed for tests / introspection. */\n getConfig: () => OptimisticLockConfig\n}\n\nfunction readHeader(headers: Headers, name: string): string | null {\n const direct = headers.get(name)\n if (typeof direct === 'string' && direct.trim().length > 0) return direct.trim()\n return null\n}\n\n/**\n * Normalize an `updated_at` token to a canonical ISO-8601 string, or `null`\n * when the input cannot be parsed. Exported so the command-level helper\n * (`optimistic-lock-command.ts`) compares timestamps with the EXACT same\n * normalization as the CRUD guard \u2014 otherwise the same instant could compare\n * unequal across the two paths.\n */\nexport function normalizeIsoToken(raw: string): string | null {\n const ms = Date.parse(raw)\n if (!Number.isFinite(ms)) return null\n return new Date(ms).toISOString()\n}\n\nfunction buildConflictBody(currentIso: string, expectedIso: string): OptimisticLockConflictBody {\n return {\n error: OPTIMISTIC_LOCK_CONFLICT_ERROR,\n code: OPTIMISTIC_LOCK_CONFLICT_CODE,\n currentUpdatedAt: currentIso,\n expectedUpdatedAt: expectedIso,\n }\n}\n\n/**\n * Factory for the optimistic-lock guard service.\n *\n * Usage from a module's `di.ts`:\n *\n * ```ts\n * import { asFunction } from 'awilix'\n * import { createOptimisticLockGuardService } from '@open-mercato/shared/lib/crud/optimistic-lock'\n *\n * container.register({\n * crudMutationGuardService: asFunction((cradle) => createOptimisticLockGuardService({\n * getEm: () => cradle.em,\n * readers: {\n * 'customers.company': async (em, { resourceId, tenantId }) => {\n * const row = await em.findOne(Company, { id: resourceId, tenantId }, { fields: ['updatedAt'] })\n * return row?.updatedAt ? row.updatedAt.toISOString() : null\n * },\n * },\n * })).singleton(),\n * })\n * ```\n */\nexport function createOptimisticLockGuardService(\n opts: OptimisticLockGuardOptions,\n): OptimisticLockGuardService {\n const envValue = opts.envValue !== undefined ? opts.envValue : process.env[OPTIMISTIC_LOCK_ENV_VAR]\n const config = parseOptimisticLockEnv(envValue)\n const resolveExpected = opts.resolveExpected ?? defaultResolveExpectedUpdatedAt\n const debugEnabled = process.env.OM_OPTIMISTIC_LOCK_DEBUG === '1'\n\n function isEntityEnabled(resourceKind: string): boolean {\n if (config.mode === 'off') return false\n if (config.mode === 'all') return true\n return config.entities.has(resourceKind.toLowerCase())\n }\n\n async function validateMutation(\n input: CrudMutationGuardValidateInput,\n ): Promise<CrudMutationGuardValidationResult> {\n if (config.mode === 'off') {\n return { ok: true, shouldRunAfterSuccess: false }\n }\n if (input.operation !== 'update' && input.operation !== 'delete') {\n return { ok: true, shouldRunAfterSuccess: false }\n }\n if (!isEntityEnabled(input.resourceKind)) {\n return { ok: true, shouldRunAfterSuccess: false }\n }\n const readers = opts.readers ?? getAllOptimisticLockReaders()\n const reader = readers[input.resourceKind]\n if (!reader) {\n return { ok: true, shouldRunAfterSuccess: false }\n }\n\n const expectedRaw = readHeader(input.requestHeaders, OPTIMISTIC_LOCK_HEADER_NAME)\n const resolvedExpected = await resolveExpected({\n expectedFromHeader: expectedRaw,\n resourceKind: input.resourceKind,\n resourceId: input.resourceId,\n })\n if (resolvedExpected == null) {\n return { ok: true, shouldRunAfterSuccess: false }\n }\n\n const expectedIso = normalizeIsoToken(resolvedExpected)\n if (expectedIso == null) {\n return { ok: true, shouldRunAfterSuccess: false }\n }\n\n const em = opts.getEm()\n const currentRaw = await reader(em, {\n resourceKind: input.resourceKind,\n resourceId: input.resourceId,\n tenantId: input.tenantId,\n organizationId: input.organizationId ?? null,\n })\n if (currentRaw == null) {\n return { ok: true, shouldRunAfterSuccess: false }\n }\n const currentIso = normalizeIsoToken(currentRaw)\n if (currentIso == null) {\n return { ok: true, shouldRunAfterSuccess: false }\n }\n\n if (currentIso === expectedIso) {\n if (debugEnabled) {\n // eslint-disable-next-line no-console\n console.log('[optimistic-lock] match', {\n resourceKind: input.resourceKind,\n resourceId: input.resourceId,\n operation: input.operation,\n currentIso,\n expectedIso,\n })\n }\n return { ok: true, shouldRunAfterSuccess: false }\n }\n\n if (debugEnabled) {\n // eslint-disable-next-line no-console\n console.log('[optimistic-lock] CONFLICT', {\n resourceKind: input.resourceKind,\n resourceId: input.resourceId,\n operation: input.operation,\n tenantId: input.tenantId,\n organizationId: input.organizationId ?? null,\n expectedRaw: resolvedExpected,\n expectedIso,\n currentRaw,\n currentIso,\n })\n }\n\n return {\n ok: false,\n status: 409,\n body: buildConflictBody(currentIso, expectedIso),\n }\n }\n\n async function afterMutationSuccess(_input: CrudMutationGuardAfterSuccessInput): Promise<void> {\n // no-op: optimistic check has no post-success cleanup\n }\n\n function getConfig(): OptimisticLockConfig {\n return config\n }\n\n return {\n validateMutation,\n afterMutationSuccess,\n getConfig,\n }\n}\n"],
|
|
5
|
+
"mappings": "AAsCA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,mCAAmC;AAc5C,MAAM,6BAAkD,oBAAI,IAAI;AAAA,EAC9D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAUM,SAAS,uBAAuB,KAAsD;AAC3F,MAAI,OAAO,KAAM,QAAO,EAAE,MAAM,MAAM;AACtC,QAAM,UAAU,OAAO,GAAG,EAAE,KAAK;AACjC,MAAI,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ACA;AAAA,QACF,CAAC;AAAA,MACH;AACA,aAAO,EAAE,IAAI,MAAM,uBAAuB,MAAM;AAAA,IAClD;AAEA,QAAI,cAAc;AAEhB,cAAQ,IAAI,8BAA8B;AAAA,QACxC,cAAc,MAAM;AAAA,QACpB,YAAY,MAAM;AAAA,QAClB,WAAW,MAAM;AAAA,QACjB,UAAU,MAAM;AAAA,QAChB,gBAAgB,MAAM,kBAAkB;AAAA,QACxC,aAAa;AAAA,QACb;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AAAA,IACH;AAEA,WAAO;AAAA,MACL,IAAI;AAAA,MACJ,QAAQ;AAAA,MACR,MAAM,kBAAkB,YAAY,WAAW;AAAA,IACjD;AAAA,EACF;AAEA,iBAAe,qBAAqB,QAA2D;AAAA,EAE/F;AAEA,WAAS,YAAkC;AACzC,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/lib/data/engine.js
CHANGED
|
@@ -389,7 +389,7 @@ class DefaultDataEngine {
|
|
|
389
389
|
enrichedPayload.crudAction = action;
|
|
390
390
|
if (coverageBaseDelta !== void 0) enrichedPayload.coverageBaseDelta = coverageBaseDelta;
|
|
391
391
|
if (ctx.syncOrigin) enrichedPayload.syncOrigin = ctx.syncOrigin;
|
|
392
|
-
|
|
392
|
+
await bus.emitEvent("query_index.delete_one", enrichedPayload).catch((err) => {
|
|
393
393
|
console.error("[data-engine] query_index.delete_one emit failed", err);
|
|
394
394
|
});
|
|
395
395
|
} else {
|
|
@@ -403,7 +403,7 @@ class DefaultDataEngine {
|
|
|
403
403
|
enrichedPayload.crudAction = action;
|
|
404
404
|
if (coverageBaseDelta !== void 0) enrichedPayload.coverageBaseDelta = coverageBaseDelta;
|
|
405
405
|
if (ctx.syncOrigin) enrichedPayload.syncOrigin = ctx.syncOrigin;
|
|
406
|
-
|
|
406
|
+
await bus.emitEvent("query_index.upsert_one", enrichedPayload).catch((err) => {
|
|
407
407
|
console.error("[data-engine] query_index.upsert_one emit failed", err);
|
|
408
408
|
});
|
|
409
409
|
}
|