@cosmicdrift/kumiko-framework 0.339.0 → 0.341.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/index.d.ts +2 -1
- package/dist/api/index.js +1 -1
- package/dist/api/route-registrars.d.ts +4 -2
- package/dist/api/route-registrars.js +9 -1
- package/dist/api/server.d.ts +2 -1
- package/dist/api/server.js +43 -5
- package/dist/bun-db/index.d.ts +2 -2
- package/dist/bun-db/index.js +1 -1
- package/dist/bun-db/query.d.ts +25 -1
- package/dist/bun-db/query.js +136 -15
- package/dist/db/queries/shadow-swap.d.ts +2 -1
- package/dist/db/queries/shadow-swap.js +70 -7
- package/dist/db/query-api.d.ts +1 -1
- package/dist/db/query.d.ts +1 -1
- package/dist/engine/boot-validator/entity-handler.d.ts +1 -0
- package/dist/engine/boot-validator/entity-handler.js +48 -0
- package/dist/engine/define-handler.js +1 -0
- package/dist/engine/feature-ast/extractors/handlers.d.ts +2 -1
- package/dist/engine/feature-ast/extractors/handlers.js +11 -1
- package/dist/engine/feature-ast/extractors/hooks.d.ts +2 -1
- package/dist/engine/feature-ast/extractors/hooks.js +14 -0
- package/dist/engine/feature-ast/extractors/index.d.ts +1 -1
- package/dist/engine/feature-ast/extractors/index.js +1 -1
- package/dist/engine/feature-ast/patch.js +1 -0
- package/dist/engine/feature-ast/pattern-change-schema.d.ts +20 -0
- package/dist/engine/feature-ast/pattern-change-schema.js +10 -0
- package/dist/engine/feature-ast/patterns.d.ts +2 -1
- package/dist/engine/feature-ast/render.js +3 -0
- package/dist/engine/feature-entity-handlers.d.ts +2 -1
- package/dist/engine/feature-entity-handlers.js +4 -0
- package/dist/engine/feature-ui-extensions.js +9 -0
- package/dist/engine/index.d.ts +1 -1
- package/dist/engine/membership-roles.js +5 -1
- package/dist/engine/pattern-library/mixed-schemas.js +5 -0
- package/dist/engine/registry-state.js +38 -5
- package/dist/engine/registry-validate.js +2 -0
- package/dist/engine/steps/unsafe-projection-upsert.js +1 -1
- package/dist/engine/tier-resolver-extension.d.ts +2 -0
- package/dist/engine/types/index.d.ts +2 -2
- package/dist/entrypoint/index.js +25 -6
- package/dist/errors/classes.d.ts +5 -0
- package/dist/errors/classes.js +13 -0
- package/dist/errors/index.d.ts +1 -1
- package/dist/errors/index.js +1 -1
- package/dist/files/file-routes.js +7 -2
- package/dist/files/index.d.ts +1 -0
- package/dist/files/index.js +1 -0
- package/dist/files/provider-resolver.d.ts +2 -0
- package/dist/files/provider-resolver.js +16 -1
- package/dist/files/resolve-content-type.d.ts +11 -0
- package/dist/files/resolve-content-type.js +71 -0
- package/dist/files/types.d.ts +1 -1
- package/dist/files/types.js +5 -1
- package/dist/jobs/job-runner.js +1 -1
- package/dist/pipeline/dispatch-shared.d.ts +4 -1
- package/dist/pipeline/dispatch-shared.js +70 -9
- package/dist/pipeline/dispatch-write.js +35 -10
- package/dist/pipeline/dispatcher.d.ts +2 -0
- package/dist/pipeline/dispatcher.js +17 -1
- package/dist/pipeline/projection-rebuild.js +1 -1
- package/dist/rate-limit/bucket.d.ts +4 -0
- package/dist/rate-limit/bucket.js +25 -0
- package/dist/rate-limit/index.d.ts +2 -2
- package/dist/rate-limit/index.js +2 -2
- package/dist/rate-limit/middleware.d.ts +9 -0
- package/dist/rate-limit/middleware.js +33 -2
- package/dist/rate-limit/resolver.js +2 -1
- package/dist/redis/cache-sync-bus.d.ts +24 -0
- package/dist/redis/cache-sync-bus.js +152 -0
- package/dist/redis/cache-sync-topics.d.ts +17 -0
- package/dist/redis/cache-sync-topics.js +22 -0
- package/dist/redis/index.d.ts +4 -0
- package/dist/redis/index.js +2 -0
- package/dist/redis/pubsub-signal.d.ts +1 -0
- package/dist/redis/pubsub-signal.js +28 -0
- package/dist/stack/redis.d.ts +6 -1
- package/dist/stack/redis.js +7 -2
- package/dist/stack/test-stack.d.ts +15 -0
- package/dist/stack/test-stack.js +21 -1
- package/package.json +4 -4
- package/src/changes.json +54 -0
package/dist/entrypoint/index.js
CHANGED
|
@@ -45,6 +45,7 @@ import { createJobRunner } from "../jobs/job-runner.js";
|
|
|
45
45
|
import { createLifecycle } from "../lifecycle/index.js";
|
|
46
46
|
import { createNoopProvider } from "../observability/index.js";
|
|
47
47
|
import { dispatcherToWriteRef } from "../pipeline/dispatcher.js";
|
|
48
|
+
import { resolveCacheSyncBus } from "../redis/cache-sync-bus.js";
|
|
48
49
|
// --- Shared builders ------------------------------------------------------
|
|
49
50
|
//
|
|
50
51
|
// Three factories, three near-identical buildServer() + createJobRunner()
|
|
@@ -71,6 +72,24 @@ function mergeDispatcherOptions(caller, jobRunner) {
|
|
|
71
72
|
function resolveObservability(observability) {
|
|
72
73
|
return observability ?? createNoopProvider();
|
|
73
74
|
}
|
|
75
|
+
// One bus per process, decided before the file-provider resolver is built so
|
|
76
|
+
// the resolver subscribes to it; buildServer then receives it explicitly and
|
|
77
|
+
// does not create a second one.
|
|
78
|
+
function resolveEntrypointCacheSync(options, lifecycle) {
|
|
79
|
+
const { bus, owned } = resolveCacheSyncBus(options.dispatcherOptions?.cacheSync);
|
|
80
|
+
if (owned) {
|
|
81
|
+
lifecycle.registerShutdownHook("cacheSync", async () => {
|
|
82
|
+
await owned.close();
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
options: {
|
|
87
|
+
...options,
|
|
88
|
+
dispatcherOptions: { ...options.dispatcherOptions, cacheSync: bus ?? null },
|
|
89
|
+
},
|
|
90
|
+
context: withFileProviderResolver(options.registry, options.context, bus),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
74
93
|
function contextWithObservability(context, observability, effectiveFeatures) {
|
|
75
94
|
return {
|
|
76
95
|
...context,
|
|
@@ -177,7 +196,7 @@ function requireDispatcher(server, mode) {
|
|
|
177
196
|
export function createApiEntrypoint(options) {
|
|
178
197
|
const lifecycle = options.lifecycle ?? createLifecycle({ startReady: true });
|
|
179
198
|
const observability = resolveObservability(options.observability);
|
|
180
|
-
const context =
|
|
199
|
+
const { options: busOptions, context } = resolveEntrypointCacheSync(options, lifecycle);
|
|
181
200
|
// Boot-validation (Welle 2.6.c) — fail loud before traffic arrives:
|
|
182
201
|
// (a) Any jobs declared + no jobs-block → command-dispatcher would
|
|
183
202
|
// silently drop every event-triggered enqueue. Fix: add jobs:
|
|
@@ -211,7 +230,7 @@ export function createApiEntrypoint(options) {
|
|
|
211
230
|
// processLane is "both": worker-lane MSPs must run here or they'd never
|
|
212
231
|
// apply anywhere.
|
|
213
232
|
const { runLocal: runLocalDispatcher, ...dispatcherTunables } = options.eventDispatcher ?? {};
|
|
214
|
-
const server = buildApiServer({ ...
|
|
233
|
+
const server = buildApiServer({ ...busOptions, context }, lifecycle, runLocalDispatcher ? dispatcherTunables : { disabled: true }, apiJobRunner, runLocalDispatcher ? "both" : "api");
|
|
215
234
|
apiJobRunner?.attachDispatcher(dispatcherToWriteRef(server.dispatcher));
|
|
216
235
|
return {
|
|
217
236
|
app: server.app,
|
|
@@ -243,9 +262,9 @@ export function createApiEntrypoint(options) {
|
|
|
243
262
|
export function createWorkerEntrypoint(options) {
|
|
244
263
|
const lifecycle = options.lifecycle ?? createLifecycle({ startReady: true });
|
|
245
264
|
const observability = resolveObservability(options.observability);
|
|
246
|
-
const context =
|
|
265
|
+
const { options: busOptions, context } = resolveEntrypointCacheSync(options, lifecycle);
|
|
247
266
|
const jobRunner = buildJobRunnerWithHook(options.registry, contextWithObservability(context, observability, options.dispatcherOptions?.effectiveFeatures), options, "worker", lifecycle, "jobRunner");
|
|
248
|
-
const server = buildWorkerServer({ ...
|
|
267
|
+
const server = buildWorkerServer({ ...busOptions, context }, lifecycle, jobRunner);
|
|
249
268
|
jobRunner.attachDispatcher(dispatcherToWriteRef(server.dispatcher));
|
|
250
269
|
const eventDispatcher = requireDispatcher(server, "worker");
|
|
251
270
|
return {
|
|
@@ -268,7 +287,7 @@ export function createWorkerEntrypoint(options) {
|
|
|
268
287
|
export function createAllInOneEntrypoint(options) {
|
|
269
288
|
const lifecycle = options.lifecycle ?? createLifecycle({ startReady: true });
|
|
270
289
|
const observability = resolveObservability(options.observability);
|
|
271
|
-
const context =
|
|
290
|
+
const { options: busOptions, context } = resolveEntrypointCacheSync(options, lifecycle);
|
|
272
291
|
const jobRunnerContext = contextWithObservability(context, observability, options.dispatcherOptions?.effectiveFeatures);
|
|
273
292
|
// All-in-one consumes BOTH lanes: two runners, each with a BullMQ worker
|
|
274
293
|
// for its own lane's queue. Both runners hold queue-clients for both
|
|
@@ -286,7 +305,7 @@ export function createAllInOneEntrypoint(options) {
|
|
|
286
305
|
// every MSP (api-only, worker-only, both) must run here. `runLocal` is
|
|
287
306
|
// the API-mode flag — all-in-one is always local, strip it.
|
|
288
307
|
const { runLocal: _runLocal, ...allInOneDispatcherTunables } = options.eventDispatcher ?? {};
|
|
289
|
-
const server = buildApiServer({ ...
|
|
308
|
+
const server = buildApiServer({ ...busOptions, context }, lifecycle, allInOneDispatcherTunables, workerJobRunner, "both");
|
|
290
309
|
workerJobRunner.attachDispatcher(dispatcherToWriteRef(server.dispatcher));
|
|
291
310
|
apiJobRunner.attachDispatcher(dispatcherToWriteRef(server.dispatcher));
|
|
292
311
|
const eventDispatcher = requireDispatcher(server, "all-in-one");
|
package/dist/errors/classes.d.ts
CHANGED
|
@@ -112,3 +112,8 @@ export declare class RateLimitError extends KumikoError {
|
|
|
112
112
|
readonly details: RateLimitDetails;
|
|
113
113
|
constructor(details: RateLimitDetails, opts?: Pick<ErrorOpts, "i18nKey" | "cause">);
|
|
114
114
|
}
|
|
115
|
+
export declare class RateLimitUnavailableError extends KumikoError {
|
|
116
|
+
readonly code = "rate_limit_unavailable";
|
|
117
|
+
readonly httpStatus = 503;
|
|
118
|
+
constructor(opts?: Pick<ErrorOpts, "cause">);
|
|
119
|
+
}
|
package/dist/errors/classes.js
CHANGED
|
@@ -203,3 +203,16 @@ export class RateLimitError extends KumikoError {
|
|
|
203
203
|
this.details = details;
|
|
204
204
|
}
|
|
205
205
|
}
|
|
206
|
+
// The rate-limit backend (Redis) failed, so no decision could be made. The
|
|
207
|
+
// request is refused (fail-closed) with 503 instead of leaking a generic 500.
|
|
208
|
+
export class RateLimitUnavailableError extends KumikoError {
|
|
209
|
+
code = "rate_limit_unavailable";
|
|
210
|
+
httpStatus = 503;
|
|
211
|
+
constructor(opts) {
|
|
212
|
+
super({
|
|
213
|
+
message: "rate limiter unavailable",
|
|
214
|
+
i18nKey: "errors.rate_limit_unavailable",
|
|
215
|
+
...(opts?.cause && { cause: opts.cause }),
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
package/dist/errors/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type { FeatureDisabledDetails, FieldIssue, NotFoundDetails, PreconditionFailedDetails, RateLimitDetails, UnconfiguredDetails, UniqueViolationDetails, UnprocessableOpts, ValidationDetails, VersionConflictDetails, } from "./classes.js";
|
|
2
|
-
export { AccessDeniedError, ConflictError, FeatureDisabledError, IdempotentReplayError, InternalError, NotFoundError, PreconditionFailedError, RateLimitError, UnauthenticatedError, UnconfiguredError, UniqueViolationError, UnprocessableError, ValidationError, VersionConflictError, } from "./classes.js";
|
|
2
|
+
export { AccessDeniedError, ConflictError, FeatureDisabledError, IdempotentReplayError, InternalError, NotFoundError, PreconditionFailedError, RateLimitError, RateLimitUnavailableError, UnauthenticatedError, UnconfiguredError, UniqueViolationError, UnprocessableError, ValidationError, VersionConflictError, } from "./classes.js";
|
|
3
3
|
export type { ErrorCtorInput, ErrorOpts } from "./kumiko-error.js";
|
|
4
4
|
export { isKumikoError, KumikoError } from "./kumiko-error.js";
|
|
5
5
|
export { memberResolutionReadOnlyDenied } from "./member-resolution.js";
|
package/dist/errors/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { AccessDeniedError, ConflictError, FeatureDisabledError, IdempotentReplayError, InternalError, NotFoundError, PreconditionFailedError, RateLimitError, UnauthenticatedError, UnconfiguredError, UniqueViolationError, UnprocessableError, ValidationError, VersionConflictError, } from "./classes.js";
|
|
1
|
+
export { AccessDeniedError, ConflictError, FeatureDisabledError, IdempotentReplayError, InternalError, NotFoundError, PreconditionFailedError, RateLimitError, RateLimitUnavailableError, UnauthenticatedError, UnconfiguredError, UniqueViolationError, UnprocessableError, ValidationError, VersionConflictError, } from "./classes.js";
|
|
2
2
|
export { isKumikoError, KumikoError } from "./kumiko-error.js";
|
|
3
3
|
export { memberResolutionReadOnlyDenied } from "./member-resolution.js";
|
|
4
4
|
export { AgentReasons, FrameworkReasons } from "./reasons.js";
|
|
@@ -13,6 +13,7 @@ import { buildContentDispositionHeader } from "./content-disposition.js";
|
|
|
13
13
|
import { createFileContext } from "./file-handle.js";
|
|
14
14
|
import { fileRefEntity } from "./file-ref-entity.js";
|
|
15
15
|
import { fileRefsTable } from "./file-ref-table.js";
|
|
16
|
+
import { resolveContentType } from "./resolve-content-type.js";
|
|
16
17
|
import { buildStorageKey, parseMaxSize, resolveServedContentType, resolveUploadMimeType, validateFile, validateFileContent, } from "./types.js";
|
|
17
18
|
const DEFAULT_PRIVILEGED_ROLES = ["Admin", "SystemAdmin"];
|
|
18
19
|
// 15 minutes — long enough for a download to start, short enough that a
|
|
@@ -195,10 +196,14 @@ export function createFileRoutes(options) {
|
|
|
195
196
|
if (mimeTypeResolution.kind === "rejected") {
|
|
196
197
|
return c.json({ error: mimeTypeResolution.error }, 400);
|
|
197
198
|
}
|
|
198
|
-
mimeType =
|
|
199
|
+
mimeType = resolveContentType({
|
|
200
|
+
declared: mimeTypeResolution.mimeType,
|
|
201
|
+
bytes: data,
|
|
202
|
+
filename: file.name,
|
|
203
|
+
});
|
|
199
204
|
}
|
|
200
205
|
const fileRefId = generateId();
|
|
201
|
-
const storageKey = buildStorageKey(user.tenantId, entityType ?? "unattached", entityId
|
|
206
|
+
const storageKey = buildStorageKey(user.tenantId, entityType ?? "unattached", entityId, fieldName ?? "file", file.name, generateId(), contentValidation.kind === "normalized" ? contentValidation.extension : undefined);
|
|
202
207
|
// Write binary FIRST (outside the tx — network/disk I/O doesn't belong
|
|
203
208
|
// inside a PG connection's tx window). On DB-tx rollback below the bytes
|
|
204
209
|
// are orphaned in the provider; cleanup-jobs sweep those later. Losing a
|
package/dist/files/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export { createInMemoryFileProvider } from "./in-memory-provider.js";
|
|
|
10
10
|
export { createLocalProvider } from "./local-provider.js";
|
|
11
11
|
export type { FileProviderContext, FileProviderPlugin, FileProviderResolver, FileProviderResolverDeps, } from "./provider-resolver.js";
|
|
12
12
|
export { createFileProviderForTenant, isFileProviderPlugin, makeFileProviderResolver, } from "./provider-resolver.js";
|
|
13
|
+
export { resolveContentType } from "./resolve-content-type.js";
|
|
13
14
|
export { fileRefStorageDelta, filesStorageTrackingFeature, tenantStorageUsageTable, transferTenantStorageUsage, } from "./storage-tracking.js";
|
|
14
15
|
export type { FileContentValidationResult, FileMetadata, FileStorageProvider, FileValidationOptions, SignedUrlOptions, UploadMimeTypeResolution, WriteStreamOptions, } from "./types.js";
|
|
15
16
|
export { assertSafeStorageKey, buildStorageKey, normalizeMimeType, parseMaxSize, resolveUploadMimeType, sniffMimeType, tenantExportPrefix, tenantStoragePrefixes, validateFile, validateFileContent, } from "./types.js";
|
package/dist/files/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export { createFileRoutes, readFilesRouteOptions, resolveMaxUploadBodyBytes, } f
|
|
|
8
8
|
export { createInMemoryFileProvider } from "./in-memory-provider.js";
|
|
9
9
|
export { createLocalProvider } from "./local-provider.js";
|
|
10
10
|
export { createFileProviderForTenant, isFileProviderPlugin, makeFileProviderResolver, } from "./provider-resolver.js";
|
|
11
|
+
export { resolveContentType } from "./resolve-content-type.js";
|
|
11
12
|
export { fileRefStorageDelta, filesStorageTrackingFeature, tenantStorageUsageTable, transferTenantStorageUsage, } from "./storage-tracking.js";
|
|
12
13
|
export { assertSafeStorageKey, buildStorageKey, normalizeMimeType, parseMaxSize, resolveUploadMimeType, sniffMimeType, tenantExportPrefix, tenantStoragePrefixes, validateFile, validateFileContent, } from "./types.js";
|
|
13
14
|
export { createZipStream } from "./zip-stream.js";
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CacheSyncBus } from "@cosmicdrift/kumiko-types/cache-sync-types";
|
|
1
2
|
import type { FileProviderResolver } from "@cosmicdrift/kumiko-types/file-provider-resolver-types";
|
|
2
3
|
import type { DbConnection } from "../db/connection.js";
|
|
3
4
|
import type { TenantDb } from "../db/tenant-db.js";
|
|
@@ -54,5 +55,6 @@ export type FileProviderResolverDeps = {
|
|
|
54
55
|
readonly _configAccessorFactory?: ConfigAccessorFactory;
|
|
55
56
|
readonly secrets?: SecretsContext;
|
|
56
57
|
readonly db?: DbConnection | TenantDb;
|
|
58
|
+
readonly cacheSync?: CacheSyncBus;
|
|
57
59
|
};
|
|
58
60
|
export declare function makeFileProviderResolver(deps: FileProviderResolverDeps): FileProviderResolver;
|
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
// (moved here from there) so existing imports keep working.
|
|
14
14
|
import { EXT_FILE_PROVIDER, FILE_PROVIDER_CONFIG_KEY, FILE_STORAGE_PROVIDER_BOOT_SENTINEL, } from "../engine/extension-names.js";
|
|
15
15
|
import { SYSTEM_USER_ID } from "../engine/system-user.js";
|
|
16
|
+
import { SYSTEM_TENANT_ID } from "../engine/types/index.js";
|
|
17
|
+
import { CACHE_SYNC_TOPICS, isTenantConfigSyncMessage } from "../redis/cache-sync-topics.js";
|
|
16
18
|
const FEATURE_NAME = "file-foundation";
|
|
17
19
|
// extension-usage `options` is engine-payload (unknown) — structurally validate
|
|
18
20
|
// instead of casting blind.
|
|
@@ -83,6 +85,16 @@ export function makeFileProviderResolver(deps) {
|
|
|
83
85
|
// promise is evicted so a transient failure (or a secret genuinely not
|
|
84
86
|
// configured yet) doesn't permanently poison this tenant's entry.
|
|
85
87
|
const cache = new Map();
|
|
88
|
+
deps.cacheSync?.subscribe(CACHE_SYNC_TOPICS.tenantConfig, (message) => {
|
|
89
|
+
// skip: malformed message from another process
|
|
90
|
+
if (!isTenantConfigSyncMessage(message))
|
|
91
|
+
return;
|
|
92
|
+
if ("scope" in message || message.tenantId === SYSTEM_TENANT_ID)
|
|
93
|
+
cache.clear();
|
|
94
|
+
else
|
|
95
|
+
cache.delete(message.tenantId);
|
|
96
|
+
});
|
|
97
|
+
deps.cacheSync?.onResync(() => cache.clear());
|
|
86
98
|
return (tenantId) => {
|
|
87
99
|
const cached = cache.get(tenantId);
|
|
88
100
|
if (cached)
|
|
@@ -99,7 +111,10 @@ export function makeFileProviderResolver(deps) {
|
|
|
99
111
|
});
|
|
100
112
|
return createFileProviderForTenant({ config, registry: deps.registry, secrets: deps.secrets, _userId: SYSTEM_USER_ID }, tenantId);
|
|
101
113
|
})();
|
|
102
|
-
built.catch(() =>
|
|
114
|
+
built.catch(() => {
|
|
115
|
+
if (cache.get(tenantId) === built)
|
|
116
|
+
cache.delete(tenantId);
|
|
117
|
+
});
|
|
103
118
|
cache.set(tenantId, built);
|
|
104
119
|
return built;
|
|
105
120
|
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content type for a file whose sender may not have declared a useful one
|
|
3
|
+
* (mail attachments commonly arrive as application/octet-stream). A concrete
|
|
4
|
+
* declared type is returned untouched; a generic/missing one is resolved from
|
|
5
|
+
* the bytes first and the file extension only as fallback.
|
|
6
|
+
*/
|
|
7
|
+
export declare function resolveContentType(args: {
|
|
8
|
+
readonly declared: string | undefined;
|
|
9
|
+
readonly bytes: Uint8Array;
|
|
10
|
+
readonly filename: string | undefined;
|
|
11
|
+
}): string;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { normalizeMimeType, sniffMimeType } from "./types.js";
|
|
2
|
+
const OCTET_STREAM = "application/octet-stream";
|
|
3
|
+
const GENERIC_MIME_TYPES = new Set(["", OCTET_STREAM, "binary/octet-stream"]);
|
|
4
|
+
// Signatures that identify exactly one type. sniffMimeType also matches OLE and
|
|
5
|
+
// ZIP, which it maps to msword/docx — wrong for .xls/.xlsx/.zip, so they are
|
|
6
|
+
// handled separately below.
|
|
7
|
+
const UNAMBIGUOUS_SNIFFED_TYPES = new Set([
|
|
8
|
+
"image/png",
|
|
9
|
+
"image/jpeg",
|
|
10
|
+
"image/gif",
|
|
11
|
+
"image/webp",
|
|
12
|
+
"application/pdf",
|
|
13
|
+
]);
|
|
14
|
+
const ZIP_SIGNATURE = [0x50, 0x4b, 0x03, 0x04];
|
|
15
|
+
const XML_PROBE_BYTES = 512;
|
|
16
|
+
const EXTENSION_FALLBACK = {
|
|
17
|
+
xml: "application/xml",
|
|
18
|
+
pdf: "application/pdf",
|
|
19
|
+
png: "image/png",
|
|
20
|
+
jpg: "image/jpeg",
|
|
21
|
+
jpeg: "image/jpeg",
|
|
22
|
+
gif: "image/gif",
|
|
23
|
+
webp: "image/webp",
|
|
24
|
+
zip: "application/zip",
|
|
25
|
+
json: "application/json",
|
|
26
|
+
csv: "text/csv",
|
|
27
|
+
txt: "text/plain",
|
|
28
|
+
};
|
|
29
|
+
function hasPrefix(bytes, prefix) {
|
|
30
|
+
return prefix.every((byte, i) => bytes[i] === byte);
|
|
31
|
+
}
|
|
32
|
+
// Textual start only: a NUL in the probe means binary (or UTF-16, which is
|
|
33
|
+
// deliberately not detected). HTML also starts with `<` and must not be
|
|
34
|
+
// relabelled as XML.
|
|
35
|
+
function looksLikeXml(bytes) {
|
|
36
|
+
const probe = bytes.subarray(0, XML_PROBE_BYTES);
|
|
37
|
+
if (probe.includes(0))
|
|
38
|
+
return false;
|
|
39
|
+
const text = new TextDecoder().decode(probe).replace(/^/, "").trimStart();
|
|
40
|
+
if (text.startsWith("<?xml"))
|
|
41
|
+
return true;
|
|
42
|
+
if (!/^<[A-Za-z_]/.test(text))
|
|
43
|
+
return false;
|
|
44
|
+
return !/^<(html|head|body|script)\b/i.test(text);
|
|
45
|
+
}
|
|
46
|
+
function extensionOf(filename) {
|
|
47
|
+
const dot = filename.lastIndexOf(".");
|
|
48
|
+
return dot === -1 ? "" : filename.slice(dot + 1).toLowerCase();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Content type for a file whose sender may not have declared a useful one
|
|
52
|
+
* (mail attachments commonly arrive as application/octet-stream). A concrete
|
|
53
|
+
* declared type is returned untouched; a generic/missing one is resolved from
|
|
54
|
+
* the bytes first and the file extension only as fallback.
|
|
55
|
+
*/
|
|
56
|
+
export function resolveContentType(args) {
|
|
57
|
+
const declared = args.declared ?? "";
|
|
58
|
+
if (!GENERIC_MIME_TYPES.has(normalizeMimeType(declared)))
|
|
59
|
+
return declared;
|
|
60
|
+
const sniffed = sniffMimeType(args.bytes);
|
|
61
|
+
if (sniffed !== null && UNAMBIGUOUS_SNIFFED_TYPES.has(sniffed))
|
|
62
|
+
return sniffed;
|
|
63
|
+
if (looksLikeXml(args.bytes))
|
|
64
|
+
return "application/xml";
|
|
65
|
+
const fromExtension = EXTENSION_FALLBACK[extensionOf(args.filename ?? "")];
|
|
66
|
+
if (fromExtension !== undefined)
|
|
67
|
+
return fromExtension;
|
|
68
|
+
if (hasPrefix(args.bytes, ZIP_SIGNATURE))
|
|
69
|
+
return "application/zip";
|
|
70
|
+
return OCTET_STREAM;
|
|
71
|
+
}
|
package/dist/files/types.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ export type UploadMimeTypeResolution = {
|
|
|
47
47
|
*/
|
|
48
48
|
export declare function resolveUploadMimeType(fileName: string, declaredMimeType: string, content: Uint8Array): UploadMimeTypeResolution;
|
|
49
49
|
export declare function assertSafeStorageKey(key: string): void;
|
|
50
|
-
export declare function buildStorageKey(tenantId: TenantId, entityType: string, entityId: number | string, fieldName: string, fileName: string, uniqueId: string, extensionOverride?: string): string;
|
|
50
|
+
export declare function buildStorageKey(tenantId: TenantId, entityType: string, entityId: number | string | undefined, fieldName: string, fileName: string, uniqueId: string, extensionOverride?: string): string;
|
|
51
51
|
/** Fixed leading segment so one S3 lifecycle rule (Prefix: "exports/") can expire export bundles for every tenant without matching a buildStorageKey() upload. */
|
|
52
52
|
export declare function tenantExportPrefix(tenantId: TenantId): string;
|
|
53
53
|
/**
|
package/dist/files/types.js
CHANGED
|
@@ -261,6 +261,9 @@ export function assertSafeStorageKey(key) {
|
|
|
261
261
|
throw new Error(`storage key contains a path-traversal segment: "${key}"`);
|
|
262
262
|
}
|
|
263
263
|
}
|
|
264
|
+
// Object stores (S3/MinIO) reject keys with an empty path segment, so an
|
|
265
|
+
// upload without an owning entity needs a placeholder id segment.
|
|
266
|
+
const NO_ENTITY_ID_SEGMENT = "none";
|
|
264
267
|
export function buildStorageKey(tenantId, entityType, entityId, fieldName, fileName, uniqueId,
|
|
265
268
|
// Set when validateFileContent normalized the upload to a different real
|
|
266
269
|
// type — the storage key then reflects the sniffed type, not the user's
|
|
@@ -268,7 +271,8 @@ export function buildStorageKey(tenantId, entityType, entityId, fieldName, fileN
|
|
|
268
271
|
extensionOverride) {
|
|
269
272
|
const rawExt = extensionOverride ?? fileName.split(".").pop() ?? "";
|
|
270
273
|
const ext = /^[A-Za-z0-9]+$/.test(rawExt) ? rawExt.toLowerCase() : "bin";
|
|
271
|
-
|
|
274
|
+
const entitySegment = entityId === undefined || entityId === "" ? NO_ENTITY_ID_SEGMENT : entityId;
|
|
275
|
+
return `${tenantId}/${entityType}/${entitySegment}/${fieldName}/${uniqueId}.${ext}`;
|
|
272
276
|
}
|
|
273
277
|
const EXPORTS_PREFIX_SEGMENT = "exports";
|
|
274
278
|
/** Fixed leading segment so one S3 lifecycle rule (Prefix: "exports/") can expire export bundles for every tenant without matching a buildStorageKey() upload. */
|
package/dist/jobs/job-runner.js
CHANGED
|
@@ -677,7 +677,7 @@ export function createJobRunner(options) {
|
|
|
677
677
|
// must resolve for jobs the same way they do for write-handlers, or an
|
|
678
678
|
// app-author job calling ctx.notify(...)/ctx.config(...) hits a TypeError
|
|
679
679
|
// at runtime the write-handler path never would (framework#1532).
|
|
680
|
-
const notify = context._notifyFactory?.(jobSystemUser, tenantId);
|
|
680
|
+
const notify = context._notifyFactory?.(jobSystemUser, tenantId, selfRunner);
|
|
681
681
|
const configDb = context.db; // @cast-boundary db-operator
|
|
682
682
|
// Shared by the config accessor and ctx.derivatives below — both need the
|
|
683
683
|
// same tenant-scoped db, and building it twice would let the two calls
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import type { CacheSyncBus } from "@cosmicdrift/kumiko-types/cache-sync-types";
|
|
1
2
|
import type { SseBroker } from "../api/sse-broker.js";
|
|
2
3
|
import type { DbConnection, DbTx } from "../db/connection.js";
|
|
3
4
|
import type { buildEntityTable } from "../db/table-builder.js";
|
|
4
5
|
import type { defineTransitions } from "../engine/state-machine.js";
|
|
5
6
|
import type { EffectiveFeaturesResolver } from "../engine/tier-resolver-extension.js";
|
|
6
7
|
import type { TenantId } from "../engine/types/identifiers.js";
|
|
7
|
-
import type { AppContext, HandlerContext, JobRunnerRef, RateLimitDeclaration, Registry, SessionUser, WriteResult } from "../engine/types/index.js";
|
|
8
|
+
import type { AppContext, HandlerContext, JobRunnerRef, PayloadRateLimitOption, RateLimitDeclaration, Registry, SessionUser, WriteResult } from "../engine/types/index.js";
|
|
8
9
|
import { type WriteErrorInfo } from "../errors/index.js";
|
|
9
10
|
import { type EscapeHatchReportWindow, type getFallbackMeter, getFallbackTracer } from "../observability/index.js";
|
|
10
11
|
import { type AfterCommitHook } from "./dispatcher-utils.js";
|
|
@@ -40,6 +41,7 @@ export type DispatchContext = {
|
|
|
40
41
|
tableCache: Map<string, ReturnType<typeof buildEntityTable>>;
|
|
41
42
|
transitionCache: Map<string, ReturnType<typeof defineTransitions>>;
|
|
42
43
|
tenantTimezoneCache: TenantTimezoneCache;
|
|
44
|
+
cacheSync: CacheSyncBus | undefined;
|
|
43
45
|
escapeHatchReportWindow: EscapeHatchReportWindow;
|
|
44
46
|
tracer: ReturnType<typeof getFallbackTracer>;
|
|
45
47
|
meter: ReturnType<typeof getFallbackMeter>;
|
|
@@ -54,4 +56,5 @@ export declare function runStreamInstrumented<T>(ctx: DispatchContext, type: str
|
|
|
54
56
|
export declare function checkFeatureEnabled(ctx: DispatchContext, qualifiedHandler: string, tenantId: TenantId): Promise<import("../errors/index.js").FeatureDisabledError | undefined>;
|
|
55
57
|
export declare function ensureFeatureEnabled(ctx: DispatchContext, qualifiedHandler: string, tenantId: TenantId): Promise<void>;
|
|
56
58
|
export declare function enforceRateLimit(ctx: DispatchContext, rateLimit: RateLimitDeclaration | undefined, handlerName: string, user: SessionUser, isSystemScope: boolean): Promise<void>;
|
|
59
|
+
export declare function enforcePayloadRateLimits(ctx: DispatchContext, limits: readonly PayloadRateLimitOption[] | undefined, handlerName: string, user: SessionUser, data: unknown): Promise<void>;
|
|
57
60
|
export declare function resolveAuthClaimsFn(ctx: DispatchContext, user: SessionUser): Promise<Record<string, unknown>>;
|
|
@@ -5,7 +5,7 @@ import { createTenantDb, createUncheckedSystemDb, hasTenantColumn, } from "../db
|
|
|
5
5
|
import { createDerivativesContext } from "../derivatives/derivatives-context.js";
|
|
6
6
|
import { createSystemUser } from "../engine/system-user.js";
|
|
7
7
|
import { isRateLimitDisabled, resolveAgentExposure } from "../engine/types/index.js";
|
|
8
|
-
import { AccessDeniedError, FeatureDisabledError, InternalError, memberResolutionReadOnlyDenied, VersionConflictError, } from "../errors/index.js";
|
|
8
|
+
import { AccessDeniedError, FeatureDisabledError, InternalError, memberResolutionReadOnlyDenied, RateLimitError, RateLimitUnavailableError, VersionConflictError, } from "../errors/index.js";
|
|
9
9
|
import { archiveStream as archiveStreamHelper, isStreamArchived, restoreStream as restoreStreamHelper, } from "../event-store/archive.js";
|
|
10
10
|
import { IdempotentAppendConflictError as EventStoreIdempotentAppendConflictError, VersionConflictError as EventStoreVersionConflictError, } from "../event-store/errors.js";
|
|
11
11
|
import { getStreamVersion, loadAggregate, loadAggregateAsOf, } from "../event-store/event-store.js";
|
|
@@ -14,8 +14,9 @@ import { upcastStoredEvent, upcastStoredEvents } from "../event-store/upcaster.j
|
|
|
14
14
|
import { createFileContext } from "../files/file-handle.js";
|
|
15
15
|
import { DEFAULT_LOCALE, isValidLocaleTag } from "../i18n/request-locale.js";
|
|
16
16
|
import { createEscapeHatchReporter, createMetricsHandle, createNoopMetricsHandle, createSafeMetricsHandle, emitDispatcherError, emitDispatcherHandler, getFallbackTracer, observabilityContext, } from "../observability/index.js";
|
|
17
|
-
import { buildBucketKey } from "../rate-limit/index.js";
|
|
17
|
+
import { buildBucketKey, buildPayloadBucketKey, normalizePayloadBucketValue, } from "../rate-limit/index.js";
|
|
18
18
|
import { createTzContext, isValidIanaTimeZone } from "../time/index.js";
|
|
19
|
+
import { isPlainObject } from "../utils/is-plain-object.js";
|
|
19
20
|
import { INTERACTIVE_SIGN_IN_POLICY, resolveActiveMembershipFn } from "./active-membership.js";
|
|
20
21
|
import { appendDomainEventCore } from "./append-event-core.js";
|
|
21
22
|
import { resolveAuthClaims as runAuthClaimsResolver } from "./auth-claims-resolver.js";
|
|
@@ -92,6 +93,14 @@ function createSystemScopedDbGuard(handlerType, fieldName, hint) {
|
|
|
92
93
|
export function isMemberResolutionPrincipal(user) {
|
|
93
94
|
return user.origin === "member-resolution";
|
|
94
95
|
}
|
|
96
|
+
// DispatchContext.jobRunner is typed as the handleEvent-only JobRunnerRef but
|
|
97
|
+
// holds the concrete runner at boot; narrow instead of casting.
|
|
98
|
+
function isNotifyJobDispatcher(runner) {
|
|
99
|
+
return (typeof runner === "object" &&
|
|
100
|
+
runner !== null &&
|
|
101
|
+
"dispatch" in runner &&
|
|
102
|
+
typeof runner.dispatch === "function");
|
|
103
|
+
}
|
|
95
104
|
async function denyMemberResolutionWrite() {
|
|
96
105
|
throw memberResolutionReadOnlyDenied();
|
|
97
106
|
}
|
|
@@ -206,7 +215,9 @@ export async function buildHandlerContext(ctx, type, user, origin, tx, afterComm
|
|
|
206
215
|
userId: user.id,
|
|
207
216
|
...(reqCtx && { requestId: reqCtx.requestId }),
|
|
208
217
|
});
|
|
209
|
-
const notify = context._notifyFactory
|
|
218
|
+
const notify = context._notifyFactory
|
|
219
|
+
? context._notifyFactory(user, user.tenantId, isNotifyJobDispatcher(jobRunner) ? jobRunner : undefined)
|
|
220
|
+
: undefined;
|
|
210
221
|
// Mirror notify: only built when the config feature wired its factory.
|
|
211
222
|
const config = context._configAccessorFactory && db
|
|
212
223
|
? context._configAccessorFactory({
|
|
@@ -604,8 +615,11 @@ export async function buildHandlerContext(ctx, type, user, origin, tx, afterComm
|
|
|
604
615
|
// Request → Session → Boot-Default chain above.
|
|
605
616
|
const safeUserLocale = user.locale !== undefined && isValidLocaleTag(user.locale) ? user.locale : undefined;
|
|
606
617
|
const locale = reqCtx?.locale ?? safeUserLocale ?? context.defaultLocale ?? DEFAULT_LOCALE;
|
|
618
|
+
// The payload-bucket digest is dispatcher-internal; handlers must not be
|
|
619
|
+
// able to compute bucket keys.
|
|
620
|
+
const { _rateLimitPayloadDigest: _omitPayloadDigest, ...contextForHandler } = context;
|
|
607
621
|
const handlerContext = {
|
|
608
|
-
...
|
|
622
|
+
...contextForHandler,
|
|
609
623
|
registry,
|
|
610
624
|
db: exposedDb,
|
|
611
625
|
dbOutsideTransaction,
|
|
@@ -636,6 +650,7 @@ export async function buildHandlerContext(ctx, type, user, origin, tx, afterComm
|
|
|
636
650
|
// Propagate the feature-toggle resolver so the lifecycle pipeline,
|
|
637
651
|
// MSP runner, and ctx.hasFeature all pull from the same source.
|
|
638
652
|
...(effectiveFeatures && { effectiveFeatures }),
|
|
653
|
+
...(ctx.cacheSync && { cacheSync: ctx.cacheSync }),
|
|
639
654
|
// Lets write handlers call ctx.jobRunner.dispatch(...) directly, same
|
|
640
655
|
// as a follow-up job would (test-stack.ts wires the matching runner).
|
|
641
656
|
...(jobRunner && { jobRunner }),
|
|
@@ -894,11 +909,57 @@ export async function enforceRateLimit(ctx, rateLimit, handlerName, user, isSyst
|
|
|
894
909
|
message: `Handler "${handlerName}" declares rateLimit but no RateLimitResolver is configured. Load the rate-limiting feature or remove the option.`,
|
|
895
910
|
});
|
|
896
911
|
}
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
912
|
+
try {
|
|
913
|
+
await context.rateLimit.enforce(bucket.key, {
|
|
914
|
+
limit: effective.limit,
|
|
915
|
+
windowSeconds: effective.windowSeconds,
|
|
916
|
+
cost: effective.cost,
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
catch (error) {
|
|
920
|
+
// Backend failure (Redis down): fail closed as 503, same as L1/L2.
|
|
921
|
+
if (error instanceof RateLimitError)
|
|
922
|
+
throw error;
|
|
923
|
+
throw new RateLimitUnavailableError({
|
|
924
|
+
cause: error instanceof Error ? error : new Error(String(error)),
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
// Payload-field rate limits (additionalRateLimits). Runs after schema
|
|
929
|
+
// validation so the field is typed, regardless of what the handler later
|
|
930
|
+
// returns — hit and non-hit callers see the same limit behavior. SYSTEM
|
|
931
|
+
// identities (jobs, internal dispatches) are not the anonymous traffic this
|
|
932
|
+
// protects against.
|
|
933
|
+
export async function enforcePayloadRateLimits(ctx, limits, handlerName, user, data) {
|
|
934
|
+
// skip: no payload buckets declared, or a SYSTEM caller
|
|
935
|
+
if (!limits || isSystemIdentity(user))
|
|
936
|
+
return;
|
|
937
|
+
const { appContext: context } = ctx;
|
|
938
|
+
const digest = context._rateLimitPayloadDigest;
|
|
939
|
+
if (!context.rateLimit || !digest) {
|
|
940
|
+
throw new InternalError({
|
|
941
|
+
message: `Handler "${handlerName}" declares additionalRateLimits but no RateLimitResolver/payload digest is configured.`,
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
// Boot validation guarantees an object schema; anything else must not pass unlimited.
|
|
945
|
+
if (!isPlainObject(data)) {
|
|
946
|
+
throw new InternalError({
|
|
947
|
+
message: `Handler "${handlerName}" declares additionalRateLimits but its payload is not an object.`,
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
for (const { per, limit, windowSeconds, cost } of limits) {
|
|
951
|
+
const value = data[per.payloadField];
|
|
952
|
+
// skip: optional field not sent — nothing to bucket on
|
|
953
|
+
if (value === undefined)
|
|
954
|
+
continue;
|
|
955
|
+
if (typeof value !== "string") {
|
|
956
|
+
throw new InternalError({
|
|
957
|
+
message: `Handler "${handlerName}" additionalRateLimits payloadField "${per.payloadField}" is not a string.`,
|
|
958
|
+
});
|
|
959
|
+
}
|
|
960
|
+
const key = buildPayloadBucketKey(handlerName, per.payloadField, digest(normalizePayloadBucketValue(value)));
|
|
961
|
+
await context.rateLimit.enforce(key, { limit, windowSeconds, cost });
|
|
962
|
+
}
|
|
902
963
|
}
|
|
903
964
|
// Build the per-hook context every auth-claims invocation gets. Claims
|
|
904
965
|
// hooks run OUTSIDE any request transaction (login is itself the root
|
|
@@ -10,8 +10,9 @@ import { defineTransitions, guardTransition } from "../engine/state-machine.js";
|
|
|
10
10
|
import { HookPhases } from "../engine/types/index.js";
|
|
11
11
|
import { runValidation } from "../engine/validation.js";
|
|
12
12
|
import { AccessDeniedError, FrameworkReasons, InternalError, isKumikoError, memberResolutionReadOnlyDenied, NotFoundError, ValidationError, validationErrorFromZod, writeFailure, } from "../errors/index.js";
|
|
13
|
+
import { CACHE_SYNC_TOPICS } from "../redis/cache-sync-topics.js";
|
|
13
14
|
import { assertNoSecretLeak } from "../secrets/index.js";
|
|
14
|
-
import { buildHandlerContext, CONFIG_WRITE_RESET_TYPE, CONFIG_WRITE_SET_TYPE, checkFeatureEnabled, enforceRateLimit, isMemberResolutionPrincipal, resolveDbSource, runHandlerInstrumented, TENANT_TIMEZONE_CONFIG_KEY, } from "./dispatch-shared.js";
|
|
15
|
+
import { buildHandlerContext, CONFIG_WRITE_RESET_TYPE, CONFIG_WRITE_SET_TYPE, checkFeatureEnabled, enforcePayloadRateLimits, enforceRateLimit, isMemberResolutionPrincipal, resolveDbSource, runHandlerInstrumented, TENANT_TIMEZONE_CONFIG_KEY, } from "./dispatch-shared.js";
|
|
15
16
|
import { describeShape, extractNestedSpecs, isLifecycleResult, isWriteResultShape, prefixValidationPath, wrapToKumiko, } from "./dispatcher-utils.js";
|
|
16
17
|
import { handlerAccessError } from "./handler-access-error.js";
|
|
17
18
|
import { runProjections } from "./projections-runner.js";
|
|
@@ -44,12 +45,11 @@ function getTransitions(ctx, args) {
|
|
|
44
45
|
}
|
|
45
46
|
// A successful config:write:set/reset returns { key, scope, ... } — see
|
|
46
47
|
// bundled-features/src/config/handlers/{set,reset}.write.ts. Narrows the
|
|
47
|
-
// otherwise-`unknown` WriteResult.data
|
|
48
|
-
|
|
49
|
-
function isConfigWriteResultForKey(data, key) {
|
|
48
|
+
// otherwise-`unknown` WriteResult.data without an unchecked cast.
|
|
49
|
+
function isConfigWriteResult(data) {
|
|
50
50
|
if (typeof data !== "object" || data === null || !("key" in data))
|
|
51
51
|
return false;
|
|
52
|
-
return data.key ===
|
|
52
|
+
return typeof data.key === "string";
|
|
53
53
|
}
|
|
54
54
|
// fw#2462: dispatch-shared.ts caches the resolved tenant:config:timezone
|
|
55
55
|
// value per tenant. A successful write to that key must drop the stale
|
|
@@ -64,14 +64,30 @@ function invalidateTenantTimezoneCache(ctx, type, user, result) {
|
|
|
64
64
|
if (type !== CONFIG_WRITE_SET_TYPE && type !== CONFIG_WRITE_RESET_TYPE)
|
|
65
65
|
return;
|
|
66
66
|
// skip: write was for a different config key
|
|
67
|
-
if (!
|
|
67
|
+
if (!isConfigWriteResult(result.data) || result.data.key !== TENANT_TIMEZONE_CONFIG_KEY)
|
|
68
68
|
return;
|
|
69
|
-
if (result.data.scope === ConfigScopes.system)
|
|
69
|
+
if (result.data.scope === ConfigScopes.system)
|
|
70
70
|
ctx.tenantTimezoneCache.clear();
|
|
71
|
-
|
|
72
|
-
else {
|
|
71
|
+
else
|
|
73
72
|
ctx.tenantTimezoneCache.invalidate(user.tenantId);
|
|
74
|
-
|
|
73
|
+
}
|
|
74
|
+
// Every committed config write tells the other processes (config-derived
|
|
75
|
+
// caches such as the tenant timezone and the file provider live per process).
|
|
76
|
+
// Only after commit: a pod receiving this earlier would re-read the pre-write
|
|
77
|
+
// row and cache the stale value again.
|
|
78
|
+
function publishConfigWrite(ctx, type, user, result) {
|
|
79
|
+
// skip: failed write, nothing changed
|
|
80
|
+
if (!result.isSuccess)
|
|
81
|
+
return;
|
|
82
|
+
// skip: not a config:write:set/reset dispatch
|
|
83
|
+
if (type !== CONFIG_WRITE_SET_TYPE && type !== CONFIG_WRITE_RESET_TYPE)
|
|
84
|
+
return;
|
|
85
|
+
// skip: result carries no config key to announce
|
|
86
|
+
if (!isConfigWriteResult(result.data))
|
|
87
|
+
return;
|
|
88
|
+
const { key, scope } = result.data;
|
|
89
|
+
const message = scope === ConfigScopes.system ? { scope: "all", key } : { tenantId: user.tenantId, key };
|
|
90
|
+
ctx.cacheSync?.publish(CACHE_SYNC_TOPICS.tenantConfig, message);
|
|
75
91
|
}
|
|
76
92
|
// Runs lifecycle hooks for a handler result. inTransaction hooks fire NOW
|
|
77
93
|
// (they see the tx via ctx.db when batch/write opens a transaction).
|
|
@@ -329,6 +345,14 @@ async function executeWriteInner(ctx, type, payload, user, origin, tx, afterComm
|
|
|
329
345
|
if (!parsed.success) {
|
|
330
346
|
return writeFailure(validationErrorFromZod(parsed.error));
|
|
331
347
|
}
|
|
348
|
+
try {
|
|
349
|
+
await enforcePayloadRateLimits(ctx, handler.additionalRateLimits, type, user, parsed.data);
|
|
350
|
+
}
|
|
351
|
+
catch (e) {
|
|
352
|
+
if (isKumikoError(e))
|
|
353
|
+
return writeFailure(e);
|
|
354
|
+
throw e;
|
|
355
|
+
}
|
|
332
356
|
const hookErrors = runValidation(registry, type, parsed.data); // @cast-boundary engine-payload
|
|
333
357
|
if (hookErrors) {
|
|
334
358
|
return writeFailure(new ValidationError({
|
|
@@ -459,6 +483,7 @@ async function executeWriteInner(ctx, type, payload, user, origin, tx, afterComm
|
|
|
459
483
|
// repopulate the cache with the stale value for a full TTL.
|
|
460
484
|
afterCommitHooks.push(async () => {
|
|
461
485
|
invalidateTenantTimezoneCache(ctx, type, user, result);
|
|
486
|
+
publishConfigWrite(ctx, type, user, result);
|
|
462
487
|
});
|
|
463
488
|
}
|
|
464
489
|
// Response-guard: block Secret<> leaks in write responses (SaveContext
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CacheSyncBus } from "@cosmicdrift/kumiko-types/cache-sync-types";
|
|
1
2
|
import type { SseBroker } from "../api/sse-broker.js";
|
|
2
3
|
import type { EffectiveFeaturesResolver } from "../engine/tier-resolver-extension.js";
|
|
3
4
|
import type { TenantId } from "../engine/types/identifiers.js";
|
|
@@ -15,6 +16,7 @@ export type DispatcherOptions = {
|
|
|
15
16
|
membershipQuery?: string;
|
|
16
17
|
effectiveFeatures?: EffectiveFeaturesResolver;
|
|
17
18
|
sseBroker?: SseBroker;
|
|
19
|
+
cacheSync?: CacheSyncBus | null;
|
|
18
20
|
};
|
|
19
21
|
export type Dispatcher = {
|
|
20
22
|
write(type: HandlerType, payload: unknown, user: SessionUser, requestId?: string): Promise<WriteResult>;
|