@beechcms/core 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/dist/antivirus/noop-antivirus.provider.d.ts.map +1 -1
- package/dist/antivirus/virustotal-antivirus.provider.d.ts.map +1 -1
- package/dist/automations-grammar-words.d.ts +2 -0
- package/dist/automations-grammar-words.d.ts.map +1 -0
- package/dist/automations-grammar-words.js +8 -0
- package/dist/automations.repository.interface.d.ts +19 -0
- package/dist/automations.repository.interface.d.ts.map +1 -0
- package/dist/automations.repository.interface.js +3 -0
- package/dist/automations.runner.interface.d.ts +10 -0
- package/dist/automations.runner.interface.d.ts.map +1 -0
- package/dist/automations.runner.interface.js +3 -0
- package/dist/automations.runner.stub.d.ts +5 -0
- package/dist/automations.runner.stub.d.ts.map +1 -0
- package/dist/automations.runner.stub.js +7 -0
- package/dist/automations.types.d.ts +75 -0
- package/dist/automations.types.d.ts.map +1 -0
- package/dist/automations.types.js +3 -0
- package/dist/clock.d.ts +25 -0
- package/dist/clock.d.ts.map +1 -0
- package/dist/clock.js +11 -0
- package/dist/common/canonical-json.d.ts +30 -0
- package/dist/common/canonical-json.d.ts.map +1 -0
- package/dist/common/canonical-json.js +89 -0
- package/dist/content/content.repository.d.ts +2 -2
- package/dist/content/content.repository.d.ts.map +1 -1
- package/dist/content/deletion-ledger.d.ts +39 -0
- package/dist/content/deletion-ledger.d.ts.map +1 -0
- package/dist/content/deletion-ledger.js +3 -0
- package/dist/content-scan.repository.d.ts +9 -0
- package/dist/content-scan.repository.d.ts.map +1 -0
- package/dist/content-scan.repository.js +3 -0
- package/dist/content.repository.d.ts +247 -0
- package/dist/content.repository.d.ts.map +1 -0
- package/dist/content.repository.js +59 -0
- package/dist/dashboard-layout.d.ts +154 -0
- package/dist/dashboard-layout.d.ts.map +1 -0
- package/dist/dashboard-layout.js +186 -0
- package/dist/dashboard-layout.repository.d.ts +18 -0
- package/dist/dashboard-layout.repository.d.ts.map +1 -0
- package/dist/dashboard-layout.repository.js +3 -0
- package/dist/dashboard-permissions.d.ts +3 -0
- package/dist/dashboard-permissions.d.ts.map +1 -0
- package/dist/dashboard-permissions.js +11 -0
- package/dist/dashboard-scopes.d.ts +12 -0
- package/dist/dashboard-scopes.d.ts.map +1 -0
- package/dist/dashboard-scopes.js +30 -0
- package/dist/define-seed.d.ts +3 -0
- package/dist/define-seed.d.ts.map +1 -0
- package/dist/define-seed.js +5 -0
- package/dist/demo-data.repository.d.ts +5 -0
- package/dist/demo-data.repository.d.ts.map +1 -0
- package/dist/demo-data.repository.js +3 -0
- package/dist/email.provider.d.ts +32 -0
- package/dist/email.provider.d.ts.map +1 -0
- package/dist/email.provider.js +4 -0
- package/dist/engine/ddl/ddl.d.ts +216 -0
- package/dist/engine/ddl/ddl.d.ts.map +1 -0
- package/dist/engine/ddl/ddl.js +574 -0
- package/dist/engine/ddl/schema-mutator.d.ts +26 -0
- package/dist/engine/ddl/schema-mutator.d.ts.map +1 -0
- package/dist/engine/ddl/schema-mutator.js +3 -0
- package/dist/engine/ddl/seed-ddl-destructive.d.ts +40 -0
- package/dist/engine/ddl/seed-ddl-destructive.d.ts.map +1 -0
- package/dist/engine/ddl/seed-ddl-destructive.js +149 -0
- package/dist/engine/ddl/seed-ddl.d.ts +43 -0
- package/dist/engine/ddl/seed-ddl.d.ts.map +1 -0
- package/dist/engine/ddl/seed-ddl.js +115 -0
- package/dist/engine/introspection/introspection.d.ts +101 -0
- package/dist/engine/introspection/introspection.d.ts.map +1 -0
- package/dist/engine/introspection/introspection.js +127 -0
- package/dist/engine/introspection/schema-fingerprint.d.ts +73 -0
- package/dist/engine/introspection/schema-fingerprint.d.ts.map +1 -0
- package/dist/engine/introspection/schema-fingerprint.js +97 -0
- package/dist/engine/introspection.d.ts +101 -0
- package/dist/engine/introspection.d.ts.map +1 -0
- package/dist/engine/introspection.js +127 -0
- package/dist/engine/localization/localization.d.ts +127 -0
- package/dist/engine/localization/localization.d.ts.map +1 -0
- package/dist/engine/localization/localization.js +212 -0
- package/dist/engine/localization.d.ts +127 -0
- package/dist/engine/localization.d.ts.map +1 -0
- package/dist/engine/localization.js +212 -0
- package/dist/engine/privacy/policies.d.ts +58 -0
- package/dist/engine/privacy/policies.d.ts.map +1 -0
- package/dist/engine/privacy/policies.js +185 -0
- package/dist/engine/privacy/privacy.service.d.ts +65 -0
- package/dist/engine/privacy/privacy.service.d.ts.map +1 -0
- package/dist/engine/privacy/privacy.service.js +114 -0
- package/dist/engine/privacy.service.d.ts.map +1 -1
- package/dist/engine/query/query.d.ts +23 -0
- package/dist/engine/query/query.d.ts.map +1 -0
- package/dist/engine/query/query.js +301 -0
- package/dist/engine/query/relations.d.ts +28 -0
- package/dist/engine/query/relations.d.ts.map +1 -0
- package/dist/engine/query/relations.js +25 -0
- package/dist/engine/query/serialize.d.ts +22 -0
- package/dist/engine/query/serialize.d.ts.map +1 -0
- package/dist/engine/query/serialize.js +175 -0
- package/dist/engine/schema-fingerprint.d.ts +73 -0
- package/dist/engine/schema-fingerprint.d.ts.map +1 -0
- package/dist/engine/schema-fingerprint.js +97 -0
- package/dist/engine/seed-registry.d.ts.map +1 -1
- package/dist/engine/seeds/define-seed.d.ts +3 -0
- package/dist/engine/seeds/define-seed.d.ts.map +1 -0
- package/dist/engine/seeds/define-seed.js +5 -0
- package/dist/engine/seeds/seed-registry.d.ts +71 -0
- package/dist/engine/seeds/seed-registry.d.ts.map +1 -0
- package/dist/engine/seeds/seed-registry.js +167 -0
- package/dist/engine/seeds/seed-types-generator.d.ts +25 -0
- package/dist/engine/seeds/seed-types-generator.d.ts.map +1 -0
- package/dist/engine/seeds/seed-types-generator.js +117 -0
- package/dist/engine/seeds/seed-validation.d.ts +30 -0
- package/dist/engine/seeds/seed-validation.d.ts.map +1 -0
- package/dist/engine/seeds/seed-validation.js +364 -0
- package/dist/engine/seeds/seeds.d.ts +20 -0
- package/dist/engine/seeds/seeds.d.ts.map +1 -0
- package/dist/engine/seeds/seeds.js +21 -0
- package/dist/engine/validation/emptiness.d.ts +12 -0
- package/dist/engine/validation/emptiness.d.ts.map +1 -0
- package/dist/engine/validation/emptiness.js +33 -0
- package/dist/engine/validation/index.d.ts +1 -1
- package/dist/engine/validation/index.d.ts.map +1 -1
- package/dist/engine.d.ts +144 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +741 -0
- package/dist/file-types.d.ts +13 -0
- package/dist/file-types.d.ts.map +1 -0
- package/dist/file-types.js +85 -0
- package/dist/hooks.d.ts +29 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +3 -0
- package/dist/id-generator.d.ts +26 -0
- package/dist/id-generator.d.ts.map +1 -0
- package/dist/id-generator.js +12 -0
- package/dist/idempotency.repository.d.ts +13 -0
- package/dist/idempotency.repository.d.ts.map +1 -0
- package/dist/idempotency.repository.js +3 -0
- package/dist/kanban-position.repository.d.ts +19 -0
- package/dist/kanban-position.repository.d.ts.map +1 -0
- package/dist/kanban-position.repository.js +3 -0
- package/dist/kanban.d.ts +75 -0
- package/dist/kanban.d.ts.map +1 -0
- package/dist/kanban.js +82 -0
- package/dist/layout-permissions.d.ts +3 -0
- package/dist/layout-permissions.d.ts.map +1 -0
- package/dist/layout-permissions.js +11 -0
- package/dist/media/image-transformer.d.ts +34 -0
- package/dist/media/image-transformer.d.ts.map +1 -0
- package/dist/media/image-transformer.js +3 -0
- package/dist/media/media-transform.d.ts +88 -0
- package/dist/media/media-transform.d.ts.map +1 -0
- package/dist/media/media-transform.js +193 -0
- package/dist/media.repository.d.ts +61 -0
- package/dist/media.repository.d.ts.map +1 -0
- package/dist/media.repository.js +3 -0
- package/dist/notifications/notification-service.d.ts +1 -1
- package/dist/oauth/pkce.d.ts +1 -1
- package/dist/oauth/role-guard.d.ts.map +1 -1
- package/dist/oauth/scopes.d.ts +1 -1
- package/dist/oauth/scopes.d.ts.map +1 -1
- package/dist/policies.d.ts +10 -0
- package/dist/policies.d.ts.map +1 -0
- package/dist/policies.js +33 -0
- package/dist/queue/queue.stub.d.ts.map +1 -1
- package/dist/queue/scheduler.stub.d.ts.map +1 -1
- package/dist/queue.interface.d.ts +39 -0
- package/dist/queue.interface.d.ts.map +1 -0
- package/dist/queue.interface.js +3 -0
- package/dist/queue.stub.d.ts +6 -0
- package/dist/queue.stub.d.ts.map +1 -0
- package/dist/queue.stub.js +8 -0
- package/dist/rate-limit/token-bucket-rate-limiter.d.ts.map +1 -1
- package/dist/rbac/evaluate.d.ts +57 -0
- package/dist/rbac/evaluate.d.ts.map +1 -0
- package/dist/rbac/evaluate.js +113 -0
- package/dist/rbac/invitation.repository.d.ts +66 -0
- package/dist/rbac/invitation.repository.d.ts.map +1 -0
- package/dist/rbac/invitation.repository.js +3 -0
- package/dist/rbac/permissions.d.ts +39 -0
- package/dist/rbac/permissions.d.ts.map +1 -0
- package/dist/rbac/permissions.js +43 -0
- package/dist/rbac/types.d.ts +118 -0
- package/dist/rbac/types.d.ts.map +1 -0
- package/dist/rbac/types.js +3 -0
- package/dist/relations.d.ts +28 -0
- package/dist/relations.d.ts.map +1 -0
- package/dist/relations.js +25 -0
- package/dist/richtext-render.d.ts +11 -0
- package/dist/richtext-render.d.ts.map +1 -0
- package/dist/richtext-render.js +87 -0
- package/dist/richtext.d.ts +11 -0
- package/dist/richtext.d.ts.map +1 -0
- package/dist/richtext.js +13 -0
- package/dist/scheduler.interface.d.ts +4 -0
- package/dist/scheduler.interface.d.ts.map +1 -0
- package/dist/scheduler.interface.js +3 -0
- package/dist/scheduler.stub.d.ts +5 -0
- package/dist/scheduler.stub.d.ts.map +1 -0
- package/dist/scheduler.stub.js +7 -0
- package/dist/schema/canonical.d.ts +15 -0
- package/dist/schema/canonical.d.ts.map +1 -0
- package/dist/schema/canonical.js +37 -0
- package/dist/schema/define.d.ts +42 -0
- package/dist/schema/define.d.ts.map +1 -0
- package/dist/schema/define.js +56 -0
- package/dist/schema/emit.d.ts +14 -0
- package/dist/schema/emit.d.ts.map +1 -0
- package/dist/schema/emit.js +43 -0
- package/dist/schema/index.d.ts +12 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +13 -0
- package/dist/schema/manifest-seeds.d.ts +12 -0
- package/dist/schema/manifest-seeds.d.ts.map +1 -0
- package/dist/schema/manifest-seeds.js +48 -0
- package/dist/schema/manifest-validation.d.ts +13 -0
- package/dist/schema/manifest-validation.d.ts.map +1 -0
- package/dist/schema/manifest-validation.js +41 -0
- package/dist/schema/manifest.types.d.ts +54 -0
- package/dist/schema/manifest.types.d.ts.map +1 -0
- package/dist/schema/manifest.types.js +7 -0
- package/dist/schema-mutator.d.ts +26 -0
- package/dist/schema-mutator.d.ts.map +1 -0
- package/dist/schema-mutator.js +3 -0
- package/dist/seed-ddl-destructive.d.ts +40 -0
- package/dist/seed-ddl-destructive.d.ts.map +1 -0
- package/dist/seed-ddl-destructive.js +149 -0
- package/dist/seed-ddl.d.ts +40 -0
- package/dist/seed-ddl.d.ts.map +1 -0
- package/dist/seed-ddl.js +100 -0
- package/dist/seed-layout.d.ts +145 -0
- package/dist/seed-layout.d.ts.map +1 -0
- package/dist/seed-layout.js +192 -0
- package/dist/seed-layout.repository.d.ts +22 -0
- package/dist/seed-layout.repository.d.ts.map +1 -0
- package/dist/seed-layout.repository.js +3 -0
- package/dist/seed-registry.d.ts +71 -0
- package/dist/seed-registry.d.ts.map +1 -0
- package/dist/seed-registry.js +143 -0
- package/dist/seed-types-generator.d.ts +1 -1
- package/dist/seed-types-generator.d.ts.map +1 -1
- package/dist/seed-validation.d.ts +23 -0
- package/dist/seed-validation.d.ts.map +1 -0
- package/dist/seed-validation.js +224 -0
- package/dist/seed.repository.d.ts +37 -0
- package/dist/seed.repository.d.ts.map +1 -0
- package/dist/seed.repository.js +3 -0
- package/dist/seeds.d.ts +20 -0
- package/dist/seeds.d.ts.map +1 -0
- package/dist/seeds.js +21 -0
- package/dist/site-settings.repository.d.ts +16 -0
- package/dist/site-settings.repository.d.ts.map +1 -0
- package/dist/site-settings.repository.js +3 -0
- package/dist/slug-utils.d.ts +18 -0
- package/dist/slug-utils.d.ts.map +1 -0
- package/dist/slug-utils.js +32 -0
- package/dist/storage.d.ts +46 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +3 -0
- package/dist/transfer/csv.d.ts +34 -0
- package/dist/transfer/csv.d.ts.map +1 -0
- package/dist/transfer/csv.js +135 -0
- package/dist/transfer/flat-seed.d.ts +13 -0
- package/dist/transfer/flat-seed.d.ts.map +1 -0
- package/dist/transfer/flat-seed.js +45 -0
- package/dist/transfer/index.d.ts +7 -0
- package/dist/transfer/index.d.ts.map +1 -0
- package/dist/transfer/index.js +8 -0
- package/dist/transfer/ndjson.d.ts +25 -0
- package/dist/transfer/ndjson.d.ts.map +1 -0
- package/dist/transfer/ndjson.js +62 -0
- package/dist/transfer/row-mapping.d.ts +36 -0
- package/dist/transfer/row-mapping.d.ts.map +1 -0
- package/dist/transfer/row-mapping.js +90 -0
- package/dist/transfer/transfer.constants.d.ts +20 -0
- package/dist/transfer/transfer.constants.d.ts.map +1 -0
- package/dist/transfer/transfer.constants.js +21 -0
- package/dist/transfer/transfer.types.d.ts +46 -0
- package/dist/transfer/transfer.types.d.ts.map +1 -0
- package/dist/transfer/transfer.types.js +6 -0
- package/dist/types.d.ts +243 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/validation.d.ts +50 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +660 -0
- package/dist/webhook-validation.d.ts +3 -0
- package/dist/webhook-validation.d.ts.map +1 -0
- package/dist/webhook-validation.js +6 -0
- package/package.json +4 -4
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2024–2026 Flavio De Musso
|
|
3
|
+
import { cleanString, isPlainObject } from '../validation/primitives.js';
|
|
4
|
+
/**
|
|
5
|
+
* Locale code grammar (v1): ISO 639 language (2–3 lowercase letters), optionally followed by an ISO 3166
|
|
6
|
+
* region (2 uppercase letters) or a UN M.49 area (3 digits) — `it`, `en`, `pt-BR`, `es-419`. Script
|
|
7
|
+
* subtags (`zh-Hant`) are outside v1. The grammar doubles as an injection guard: validated codes are
|
|
8
|
+
* later interpolated into SQLite JSON paths.
|
|
9
|
+
*/
|
|
10
|
+
export const LOCALE_CODE_RE = /^[a-z]{2,3}(?:-(?:[A-Z]{2}|[0-9]{3}))?$/;
|
|
11
|
+
/** Branch types that may carry `localized: true`. */
|
|
12
|
+
export const LOCALIZABLE_BRANCH_TYPES = new Set(['text', 'richtext', 'json']);
|
|
13
|
+
/** True when `value` is a string matching {@link LOCALE_CODE_RE}. */
|
|
14
|
+
export function isLocaleCode(value) {
|
|
15
|
+
return typeof value === 'string' && LOCALE_CODE_RE.test(value);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* True when the branch is localized AND its type supports it. A malformed definition that slipped past
|
|
19
|
+
* seed validation (e.g. `localized: true` on a `number`) is treated as not localized, never as a dictionary.
|
|
20
|
+
*/
|
|
21
|
+
export function isLocalizedBranch(branch) {
|
|
22
|
+
return branch.localized === true && LOCALIZABLE_BRANCH_TYPES.has(branch.type);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Structural check used on READ paths (no config available): a non-empty plain object whose every key
|
|
26
|
+
* matches the locale-code grammar. A richtext envelope (`schemaVersion`, `doc`) and a TipTap doc
|
|
27
|
+
* (`type`, `content`) never match.
|
|
28
|
+
*/
|
|
29
|
+
export function isLocaleDictionary(value) {
|
|
30
|
+
if (!isPlainObject(value))
|
|
31
|
+
return false;
|
|
32
|
+
const keys = Object.keys(value);
|
|
33
|
+
return keys.length > 0 && keys.every(isLocaleCode);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Stricter check used on WRITE paths: a locale dictionary with at least one REGISTERED locale key.
|
|
37
|
+
* Without the registration requirement a legitimate json value such as `{"url": "…", "alt": "…"}` —
|
|
38
|
+
* whose keys happen to match the grammar — would be read as a dictionary of unregistered locales and
|
|
39
|
+
* silently emptied.
|
|
40
|
+
*/
|
|
41
|
+
export function isLocalizedWriteDictionary(value, config) {
|
|
42
|
+
return isLocaleDictionary(value) && Object.keys(value).some((key) => config.locales.includes(key));
|
|
43
|
+
}
|
|
44
|
+
function isBlankLocaleValue(value) {
|
|
45
|
+
return value === null || value === undefined || (typeof value === 'string' && cleanString(value) === '');
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Normalises any accepted write value of a localized branch into a {@link LocalizedPatch}:
|
|
49
|
+
* - a write dictionary keeps its registered locales, in `config.locales` order; unregistered locale
|
|
50
|
+
* keys are dropped;
|
|
51
|
+
* - any other value (string, richtext doc, non-dictionary json) is the default-locale value;
|
|
52
|
+
* - `null`, `undefined` and blank strings become `null` ("clear this locale").
|
|
53
|
+
* Idempotent: a patch passed back in yields the same patch.
|
|
54
|
+
*/
|
|
55
|
+
export function toLocalizedPatch(value, config) {
|
|
56
|
+
const source = isLocalizedWriteDictionary(value, config)
|
|
57
|
+
? value
|
|
58
|
+
: { [config.defaultLocale]: value };
|
|
59
|
+
const patch = {};
|
|
60
|
+
for (const locale of config.locales) {
|
|
61
|
+
if (!Object.hasOwn(source, locale))
|
|
62
|
+
continue;
|
|
63
|
+
patch[locale] = isBlankLocaleValue(source[locale]) ? null : source[locale];
|
|
64
|
+
}
|
|
65
|
+
return patch;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Removes `null` / `undefined` / blank-string entries. Returns `null` when nothing remains, so an
|
|
69
|
+
* all-cleared dictionary is stored as SQL NULL rather than `{}`. Keys are otherwise preserved as-is —
|
|
70
|
+
* including locales no longer registered, which must never be dropped by a write (brief §2, no data loss).
|
|
71
|
+
*/
|
|
72
|
+
export function compactLocalizedDictionary(value) {
|
|
73
|
+
const compact = {};
|
|
74
|
+
for (const [locale, localeValue] of Object.entries(value)) {
|
|
75
|
+
if (isBlankLocaleValue(localeValue))
|
|
76
|
+
continue;
|
|
77
|
+
compact[locale] = localeValue;
|
|
78
|
+
}
|
|
79
|
+
return Object.keys(compact).length > 0 ? compact : null;
|
|
80
|
+
}
|
|
81
|
+
/** Content locale used when neither stored `locales` nor a valid `defaultLanguage` is available. */
|
|
82
|
+
const FALLBACK_LOCALE = 'en';
|
|
83
|
+
/**
|
|
84
|
+
* Builds the project's {@link LocaleConfig}, repairing whatever a hand-edited settings row could break so
|
|
85
|
+
* the LocaleConfig invariant always holds:
|
|
86
|
+
* - `locales`: the stored list minus invalid codes and duplicates; when nothing remains (never configured,
|
|
87
|
+
* `[]`, corrupt), the implicit single-language config `[defaultLanguage]` (brief §2), which keeps the
|
|
88
|
+
* feature invisible to a mono-lingual project;
|
|
89
|
+
* - `defaultLocale`: the stored value when it belongs to `locales`, else `locales[0]`.
|
|
90
|
+
*/
|
|
91
|
+
export function resolveLocaleConfig(settings) {
|
|
92
|
+
const stored = [...new Set((settings.locales ?? []).filter(isLocaleCode))];
|
|
93
|
+
const implicit = isLocaleCode(settings.defaultLanguage) ? settings.defaultLanguage : FALLBACK_LOCALE;
|
|
94
|
+
const locales = stored.length > 0 ? stored : [implicit];
|
|
95
|
+
const defaultLocale = settings.defaultLocale && locales.includes(settings.defaultLocale)
|
|
96
|
+
? settings.defaultLocale
|
|
97
|
+
: locales[0];
|
|
98
|
+
return { locales, defaultLocale };
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* True when a STORED value of a localized branch is a locale dictionary. Text and richtext cannot hold an
|
|
102
|
+
* object keyed only by locale codes for any other reason; json can (`{"url": "…", "alt": "…"}`), so a json
|
|
103
|
+
* object counts only when at least one key is a registered locale — otherwise it is a legacy value written
|
|
104
|
+
* before the branch became localized.
|
|
105
|
+
*/
|
|
106
|
+
function isStoredLocaleDictionary(branch, value, config) {
|
|
107
|
+
if (!isLocalizedBranch(branch) || !isLocaleDictionary(value))
|
|
108
|
+
return false;
|
|
109
|
+
return branch.type !== 'json' || Object.keys(value).some((key) => config.locales.includes(key));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Merges a write into the value stored for a localized branch. The write path's only way to persist a
|
|
113
|
+
* localized value, so a translation the write does not mention is never dropped (brief §2):
|
|
114
|
+
* - `write === null` clears the whole field (an explicit null keeps its pre-localization meaning);
|
|
115
|
+
* - otherwise the write is normalised with {@link toLocalizedPatch}: every registered locale it names
|
|
116
|
+
* overwrites that locale, a `null` entry clears that locale only, and every other stored locale —
|
|
117
|
+
* including ones no longer registered — is kept as-is;
|
|
118
|
+
* - a stored legacy value (written before the branch became localized) is the default-locale value.
|
|
119
|
+
* @returns The compacted dictionary, or `null` when no locale keeps a value.
|
|
120
|
+
*/
|
|
121
|
+
export function applyLocalizedPatch(branch, stored, write, config) {
|
|
122
|
+
if (write === null)
|
|
123
|
+
return null;
|
|
124
|
+
const merged = isStoredLocaleDictionary(branch, stored, config)
|
|
125
|
+
? { ...stored }
|
|
126
|
+
: isBlankLocaleValue(stored) ? {} : { [config.defaultLocale]: stored };
|
|
127
|
+
Object.assign(merged, toLocalizedPatch(write, config));
|
|
128
|
+
return compactLocalizedDictionary(merged);
|
|
129
|
+
}
|
|
130
|
+
/** Aliases of the localized branches a write carries — the fields it must merge rather than replace. */
|
|
131
|
+
export function localizedAliasesIn(seed, data) {
|
|
132
|
+
return seed.branches
|
|
133
|
+
.filter((branch) => isLocalizedBranch(branch) && Object.hasOwn(data, branch.alias) && data[branch.alias] !== undefined)
|
|
134
|
+
.map((branch) => branch.alias);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Returns `data` with every localized branch it carries merged into `stored` via
|
|
138
|
+
* {@link applyLocalizedPatch}; other keys pass through untouched. `stored` is the entry's current values
|
|
139
|
+
* (`null` on create, which only compacts). Returns `data` itself when `config` is undefined or no localized
|
|
140
|
+
* branch is written.
|
|
141
|
+
*/
|
|
142
|
+
export function mergeLocalizedFields(seed, stored, data, config) {
|
|
143
|
+
if (!config)
|
|
144
|
+
return data;
|
|
145
|
+
const aliases = localizedAliasesIn(seed, data);
|
|
146
|
+
if (aliases.length === 0)
|
|
147
|
+
return data;
|
|
148
|
+
const merged = { ...data };
|
|
149
|
+
for (const branch of seed.branches) {
|
|
150
|
+
if (!aliases.includes(branch.alias))
|
|
151
|
+
continue;
|
|
152
|
+
merged[branch.alias] = applyLocalizedPatch(branch, stored?.[branch.alias], data[branch.alias], config);
|
|
153
|
+
}
|
|
154
|
+
return merged;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Resolves a stored localized value to one language: the requested locale, then the default locale, then the
|
|
158
|
+
* first stored translation (stored key order), then `null`. A legacy value that is not a dictionary is returned
|
|
159
|
+
* as-is. Non-localized branches return `value` unchanged. `buildSelectQuery` applies the same chain in SQL, so
|
|
160
|
+
* filters and ORDER BY compare exactly what a reader sees.
|
|
161
|
+
*/
|
|
162
|
+
export function resolveLocalizedValue(branch, value, locale, config) {
|
|
163
|
+
if (!isStoredLocaleDictionary(branch, value, config))
|
|
164
|
+
return value;
|
|
165
|
+
if (!isBlankLocaleValue(value[locale]))
|
|
166
|
+
return value[locale];
|
|
167
|
+
if (!isBlankLocaleValue(value[config.defaultLocale]))
|
|
168
|
+
return value[config.defaultLocale];
|
|
169
|
+
// An implicit config follows `defaultLanguage`; changing it before languages are configured leaves every
|
|
170
|
+
// dictionary without its default key. Returning null there would blank the public site (brief §2).
|
|
171
|
+
const firstStored = Object.values(value).find((translation) => !isBlankLocaleValue(translation));
|
|
172
|
+
return firstStored ?? null;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Returns a copy of `data` with every localized branch resolved to `locale` (default: the default locale)
|
|
176
|
+
* via {@link resolveLocalizedValue}. Returns `data` itself when `config` is undefined.
|
|
177
|
+
*/
|
|
178
|
+
export function resolveLocalizedFields(seed, data, config, locale) {
|
|
179
|
+
if (!config)
|
|
180
|
+
return data;
|
|
181
|
+
const target = locale ?? config.defaultLocale;
|
|
182
|
+
const resolved = { ...data };
|
|
183
|
+
for (const branch of seed.branches) {
|
|
184
|
+
if (isLocalizedBranch(branch) && Object.hasOwn(resolved, branch.alias)) {
|
|
185
|
+
resolved[branch.alias] = resolveLocalizedValue(branch, resolved[branch.alias], target, config);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return resolved;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The whole dictionary of a stored localized value, for readers asking every language (`?lang=all`):
|
|
192
|
+
* a stored dictionary as-is — unregistered locales included, nothing is hidden (brief §2) — a legacy
|
|
193
|
+
* value as `{ [defaultLocale]: value }` (the write path's reading of it), a blank value as `null`.
|
|
194
|
+
* Non-localized branches return `value` unchanged.
|
|
195
|
+
*/
|
|
196
|
+
export function asLocaleDictionary(branch, value, config) {
|
|
197
|
+
if (!isLocalizedBranch(branch))
|
|
198
|
+
return value;
|
|
199
|
+
if (isStoredLocaleDictionary(branch, value, config))
|
|
200
|
+
return value;
|
|
201
|
+
return isBlankLocaleValue(value) ? null : { [config.defaultLocale]: value };
|
|
202
|
+
}
|
|
203
|
+
/** Returns a copy of `data` with every localized branch it carries passed through {@link asLocaleDictionary}. */
|
|
204
|
+
export function asLocaleDictionaries(seed, data, config) {
|
|
205
|
+
const expanded = { ...data };
|
|
206
|
+
for (const branch of seed.branches) {
|
|
207
|
+
if (isLocalizedBranch(branch) && Object.hasOwn(expanded, branch.alias)) {
|
|
208
|
+
expanded[branch.alias] = asLocaleDictionary(branch, expanded[branch.alias], config);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return expanded;
|
|
212
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module engine/localization
|
|
3
|
+
* Field-level localization primitives for the Botanical Engine. A localized branch keeps its native
|
|
4
|
+
* TEXT column and stores a locale dictionary in it (`{"it":"Scarpa","en":"Shoe"}`), so ids, relations
|
|
5
|
+
* and metrics are untouched and toggling `Branch.localized` never emits DDL. Pure functions: no I/O and
|
|
6
|
+
* no settings access — callers pass the project's {@link LocaleConfig}.
|
|
7
|
+
*/
|
|
8
|
+
import type { Branch, BranchType, Seed } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Locale code grammar (v1): ISO 639 language (2–3 lowercase letters), optionally followed by an ISO 3166
|
|
11
|
+
* region (2 uppercase letters) or a UN M.49 area (3 digits) — `it`, `en`, `pt-BR`, `es-419`. Script
|
|
12
|
+
* subtags (`zh-Hant`) are outside v1. The grammar doubles as an injection guard: validated codes are
|
|
13
|
+
* later interpolated into SQLite JSON paths.
|
|
14
|
+
*/
|
|
15
|
+
export declare const LOCALE_CODE_RE: RegExp;
|
|
16
|
+
/** Branch types that may carry `localized: true`. */
|
|
17
|
+
export declare const LOCALIZABLE_BRANCH_TYPES: ReadonlySet<BranchType>;
|
|
18
|
+
/**
|
|
19
|
+
* Project-level language configuration. Invariant (guaranteed by its producer): `locales` is non-empty,
|
|
20
|
+
* every entry matches {@link LOCALE_CODE_RE}, and it contains `defaultLocale`.
|
|
21
|
+
*/
|
|
22
|
+
export interface LocaleConfig {
|
|
23
|
+
readonly locales: readonly string[];
|
|
24
|
+
readonly defaultLocale: string;
|
|
25
|
+
}
|
|
26
|
+
/** A locale dictionary as stored in the branch column: locale code → value. */
|
|
27
|
+
export type LocalizedDictionary = Record<string, unknown>;
|
|
28
|
+
/**
|
|
29
|
+
* A validated write for a localized branch: registered locale code → new value, or `null` meaning
|
|
30
|
+
* "clear this locale". Locales absent from the patch must be left untouched by the write path.
|
|
31
|
+
*/
|
|
32
|
+
export type LocalizedPatch = Record<string, unknown>;
|
|
33
|
+
/** True when `value` is a string matching {@link LOCALE_CODE_RE}. */
|
|
34
|
+
export declare function isLocaleCode(value: unknown): value is string;
|
|
35
|
+
/**
|
|
36
|
+
* True when the branch is localized AND its type supports it. A malformed definition that slipped past
|
|
37
|
+
* seed validation (e.g. `localized: true` on a `number`) is treated as not localized, never as a dictionary.
|
|
38
|
+
*/
|
|
39
|
+
export declare function isLocalizedBranch(branch: Pick<Branch, 'type' | 'localized'>): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Structural check used on READ paths (no config available): a non-empty plain object whose every key
|
|
42
|
+
* matches the locale-code grammar. A richtext envelope (`schemaVersion`, `doc`) and a TipTap doc
|
|
43
|
+
* (`type`, `content`) never match.
|
|
44
|
+
*/
|
|
45
|
+
export declare function isLocaleDictionary(value: unknown): value is LocalizedDictionary;
|
|
46
|
+
/**
|
|
47
|
+
* Stricter check used on WRITE paths: a locale dictionary with at least one REGISTERED locale key.
|
|
48
|
+
* Without the registration requirement a legitimate json value such as `{"url": "…", "alt": "…"}` —
|
|
49
|
+
* whose keys happen to match the grammar — would be read as a dictionary of unregistered locales and
|
|
50
|
+
* silently emptied.
|
|
51
|
+
*/
|
|
52
|
+
export declare function isLocalizedWriteDictionary(value: unknown, config: LocaleConfig): value is LocalizedDictionary;
|
|
53
|
+
/**
|
|
54
|
+
* Normalises any accepted write value of a localized branch into a {@link LocalizedPatch}:
|
|
55
|
+
* - a write dictionary keeps its registered locales, in `config.locales` order; unregistered locale
|
|
56
|
+
* keys are dropped;
|
|
57
|
+
* - any other value (string, richtext doc, non-dictionary json) is the default-locale value;
|
|
58
|
+
* - `null`, `undefined` and blank strings become `null` ("clear this locale").
|
|
59
|
+
* Idempotent: a patch passed back in yields the same patch.
|
|
60
|
+
*/
|
|
61
|
+
export declare function toLocalizedPatch(value: unknown, config: LocaleConfig): LocalizedPatch;
|
|
62
|
+
/**
|
|
63
|
+
* Removes `null` / `undefined` / blank-string entries. Returns `null` when nothing remains, so an
|
|
64
|
+
* all-cleared dictionary is stored as SQL NULL rather than `{}`. Keys are otherwise preserved as-is —
|
|
65
|
+
* including locales no longer registered, which must never be dropped by a write (brief §2, no data loss).
|
|
66
|
+
*/
|
|
67
|
+
export declare function compactLocalizedDictionary(value: LocalizedDictionary): LocalizedDictionary | null;
|
|
68
|
+
/**
|
|
69
|
+
* The settings subset {@link resolveLocaleConfig} reads. Structural, so this module stays free of
|
|
70
|
+
* settings imports; `SiteSettings` satisfies it.
|
|
71
|
+
*/
|
|
72
|
+
export interface LocaleSettings {
|
|
73
|
+
readonly locales?: readonly string[] | null;
|
|
74
|
+
readonly defaultLocale?: string | null;
|
|
75
|
+
readonly defaultLanguage: string;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Builds the project's {@link LocaleConfig}, repairing whatever a hand-edited settings row could break so
|
|
79
|
+
* the LocaleConfig invariant always holds:
|
|
80
|
+
* - `locales`: the stored list minus invalid codes and duplicates; when nothing remains (never configured,
|
|
81
|
+
* `[]`, corrupt), the implicit single-language config `[defaultLanguage]` (brief §2), which keeps the
|
|
82
|
+
* feature invisible to a mono-lingual project;
|
|
83
|
+
* - `defaultLocale`: the stored value when it belongs to `locales`, else `locales[0]`.
|
|
84
|
+
*/
|
|
85
|
+
export declare function resolveLocaleConfig(settings: LocaleSettings): LocaleConfig;
|
|
86
|
+
/**
|
|
87
|
+
* Merges a write into the value stored for a localized branch. The write path's only way to persist a
|
|
88
|
+
* localized value, so a translation the write does not mention is never dropped (brief §2):
|
|
89
|
+
* - `write === null` clears the whole field (an explicit null keeps its pre-localization meaning);
|
|
90
|
+
* - otherwise the write is normalised with {@link toLocalizedPatch}: every registered locale it names
|
|
91
|
+
* overwrites that locale, a `null` entry clears that locale only, and every other stored locale —
|
|
92
|
+
* including ones no longer registered — is kept as-is;
|
|
93
|
+
* - a stored legacy value (written before the branch became localized) is the default-locale value.
|
|
94
|
+
* @returns The compacted dictionary, or `null` when no locale keeps a value.
|
|
95
|
+
*/
|
|
96
|
+
export declare function applyLocalizedPatch(branch: Pick<Branch, 'type' | 'localized'>, stored: unknown, write: unknown, config: LocaleConfig): LocalizedDictionary | null;
|
|
97
|
+
/** Aliases of the localized branches a write carries — the fields it must merge rather than replace. */
|
|
98
|
+
export declare function localizedAliasesIn(seed: Pick<Seed, 'branches'>, data: Record<string, unknown>): string[];
|
|
99
|
+
/**
|
|
100
|
+
* Returns `data` with every localized branch it carries merged into `stored` via
|
|
101
|
+
* {@link applyLocalizedPatch}; other keys pass through untouched. `stored` is the entry's current values
|
|
102
|
+
* (`null` on create, which only compacts). Returns `data` itself when `config` is undefined or no localized
|
|
103
|
+
* branch is written.
|
|
104
|
+
*/
|
|
105
|
+
export declare function mergeLocalizedFields(seed: Pick<Seed, 'branches'>, stored: Record<string, unknown> | null, data: Record<string, unknown>, config: LocaleConfig | undefined): Record<string, unknown>;
|
|
106
|
+
/**
|
|
107
|
+
* Resolves a stored localized value to one language: the requested locale, then the default locale, then the
|
|
108
|
+
* first stored translation (stored key order), then `null`. A legacy value that is not a dictionary is returned
|
|
109
|
+
* as-is. Non-localized branches return `value` unchanged. `buildSelectQuery` applies the same chain in SQL, so
|
|
110
|
+
* filters and ORDER BY compare exactly what a reader sees.
|
|
111
|
+
*/
|
|
112
|
+
export declare function resolveLocalizedValue(branch: Pick<Branch, 'type' | 'localized'>, value: unknown, locale: string, config: LocaleConfig): unknown;
|
|
113
|
+
/**
|
|
114
|
+
* Returns a copy of `data` with every localized branch resolved to `locale` (default: the default locale)
|
|
115
|
+
* via {@link resolveLocalizedValue}. Returns `data` itself when `config` is undefined.
|
|
116
|
+
*/
|
|
117
|
+
export declare function resolveLocalizedFields(seed: Pick<Seed, 'branches'>, data: Record<string, unknown>, config: LocaleConfig | undefined, locale?: string): Record<string, unknown>;
|
|
118
|
+
/**
|
|
119
|
+
* The whole dictionary of a stored localized value, for readers asking every language (`?lang=all`):
|
|
120
|
+
* a stored dictionary as-is — unregistered locales included, nothing is hidden (brief §2) — a legacy
|
|
121
|
+
* value as `{ [defaultLocale]: value }` (the write path's reading of it), a blank value as `null`.
|
|
122
|
+
* Non-localized branches return `value` unchanged.
|
|
123
|
+
*/
|
|
124
|
+
export declare function asLocaleDictionary(branch: Pick<Branch, 'type' | 'localized'>, value: unknown, config: LocaleConfig): unknown;
|
|
125
|
+
/** Returns a copy of `data` with every localized branch it carries passed through {@link asLocaleDictionary}. */
|
|
126
|
+
export declare function asLocaleDictionaries(seed: Pick<Seed, 'branches'>, data: Record<string, unknown>, config: LocaleConfig): Record<string, unknown>;
|
|
127
|
+
//# sourceMappingURL=localization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localization.d.ts","sourceRoot":"","sources":["../../src/engine/localization.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2024–2026 Flavio De Musso
|
|
3
|
+
import { cleanString, isPlainObject } from './validation/primitives.js';
|
|
4
|
+
/**
|
|
5
|
+
* Locale code grammar (v1): ISO 639 language (2–3 lowercase letters), optionally followed by an ISO 3166
|
|
6
|
+
* region (2 uppercase letters) or a UN M.49 area (3 digits) — `it`, `en`, `pt-BR`, `es-419`. Script
|
|
7
|
+
* subtags (`zh-Hant`) are outside v1. The grammar doubles as an injection guard: validated codes are
|
|
8
|
+
* later interpolated into SQLite JSON paths.
|
|
9
|
+
*/
|
|
10
|
+
export const LOCALE_CODE_RE = /^[a-z]{2,3}(?:-(?:[A-Z]{2}|[0-9]{3}))?$/;
|
|
11
|
+
/** Branch types that may carry `localized: true`. */
|
|
12
|
+
export const LOCALIZABLE_BRANCH_TYPES = new Set(['text', 'richtext', 'json']);
|
|
13
|
+
/** True when `value` is a string matching {@link LOCALE_CODE_RE}. */
|
|
14
|
+
export function isLocaleCode(value) {
|
|
15
|
+
return typeof value === 'string' && LOCALE_CODE_RE.test(value);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* True when the branch is localized AND its type supports it. A malformed definition that slipped past
|
|
19
|
+
* seed validation (e.g. `localized: true` on a `number`) is treated as not localized, never as a dictionary.
|
|
20
|
+
*/
|
|
21
|
+
export function isLocalizedBranch(branch) {
|
|
22
|
+
return branch.localized === true && LOCALIZABLE_BRANCH_TYPES.has(branch.type);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Structural check used on READ paths (no config available): a non-empty plain object whose every key
|
|
26
|
+
* matches the locale-code grammar. A richtext envelope (`schemaVersion`, `doc`) and a TipTap doc
|
|
27
|
+
* (`type`, `content`) never match.
|
|
28
|
+
*/
|
|
29
|
+
export function isLocaleDictionary(value) {
|
|
30
|
+
if (!isPlainObject(value))
|
|
31
|
+
return false;
|
|
32
|
+
const keys = Object.keys(value);
|
|
33
|
+
return keys.length > 0 && keys.every(isLocaleCode);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Stricter check used on WRITE paths: a locale dictionary with at least one REGISTERED locale key.
|
|
37
|
+
* Without the registration requirement a legitimate json value such as `{"url": "…", "alt": "…"}` —
|
|
38
|
+
* whose keys happen to match the grammar — would be read as a dictionary of unregistered locales and
|
|
39
|
+
* silently emptied.
|
|
40
|
+
*/
|
|
41
|
+
export function isLocalizedWriteDictionary(value, config) {
|
|
42
|
+
return isLocaleDictionary(value) && Object.keys(value).some((key) => config.locales.includes(key));
|
|
43
|
+
}
|
|
44
|
+
function isBlankLocaleValue(value) {
|
|
45
|
+
return value === null || value === undefined || (typeof value === 'string' && cleanString(value) === '');
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Normalises any accepted write value of a localized branch into a {@link LocalizedPatch}:
|
|
49
|
+
* - a write dictionary keeps its registered locales, in `config.locales` order; unregistered locale
|
|
50
|
+
* keys are dropped;
|
|
51
|
+
* - any other value (string, richtext doc, non-dictionary json) is the default-locale value;
|
|
52
|
+
* - `null`, `undefined` and blank strings become `null` ("clear this locale").
|
|
53
|
+
* Idempotent: a patch passed back in yields the same patch.
|
|
54
|
+
*/
|
|
55
|
+
export function toLocalizedPatch(value, config) {
|
|
56
|
+
const source = isLocalizedWriteDictionary(value, config)
|
|
57
|
+
? value
|
|
58
|
+
: { [config.defaultLocale]: value };
|
|
59
|
+
const patch = {};
|
|
60
|
+
for (const locale of config.locales) {
|
|
61
|
+
if (!Object.hasOwn(source, locale))
|
|
62
|
+
continue;
|
|
63
|
+
patch[locale] = isBlankLocaleValue(source[locale]) ? null : source[locale];
|
|
64
|
+
}
|
|
65
|
+
return patch;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Removes `null` / `undefined` / blank-string entries. Returns `null` when nothing remains, so an
|
|
69
|
+
* all-cleared dictionary is stored as SQL NULL rather than `{}`. Keys are otherwise preserved as-is —
|
|
70
|
+
* including locales no longer registered, which must never be dropped by a write (brief §2, no data loss).
|
|
71
|
+
*/
|
|
72
|
+
export function compactLocalizedDictionary(value) {
|
|
73
|
+
const compact = {};
|
|
74
|
+
for (const [locale, localeValue] of Object.entries(value)) {
|
|
75
|
+
if (isBlankLocaleValue(localeValue))
|
|
76
|
+
continue;
|
|
77
|
+
compact[locale] = localeValue;
|
|
78
|
+
}
|
|
79
|
+
return Object.keys(compact).length > 0 ? compact : null;
|
|
80
|
+
}
|
|
81
|
+
/** Content locale used when neither stored `locales` nor a valid `defaultLanguage` is available. */
|
|
82
|
+
const FALLBACK_LOCALE = 'en';
|
|
83
|
+
/**
|
|
84
|
+
* Builds the project's {@link LocaleConfig}, repairing whatever a hand-edited settings row could break so
|
|
85
|
+
* the LocaleConfig invariant always holds:
|
|
86
|
+
* - `locales`: the stored list minus invalid codes and duplicates; when nothing remains (never configured,
|
|
87
|
+
* `[]`, corrupt), the implicit single-language config `[defaultLanguage]` (brief §2), which keeps the
|
|
88
|
+
* feature invisible to a mono-lingual project;
|
|
89
|
+
* - `defaultLocale`: the stored value when it belongs to `locales`, else `locales[0]`.
|
|
90
|
+
*/
|
|
91
|
+
export function resolveLocaleConfig(settings) {
|
|
92
|
+
const stored = [...new Set((settings.locales ?? []).filter(isLocaleCode))];
|
|
93
|
+
const implicit = isLocaleCode(settings.defaultLanguage) ? settings.defaultLanguage : FALLBACK_LOCALE;
|
|
94
|
+
const locales = stored.length > 0 ? stored : [implicit];
|
|
95
|
+
const defaultLocale = settings.defaultLocale && locales.includes(settings.defaultLocale)
|
|
96
|
+
? settings.defaultLocale
|
|
97
|
+
: locales[0];
|
|
98
|
+
return { locales, defaultLocale };
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* True when a STORED value of a localized branch is a locale dictionary. Text and richtext cannot hold an
|
|
102
|
+
* object keyed only by locale codes for any other reason; json can (`{"url": "…", "alt": "…"}`), so a json
|
|
103
|
+
* object counts only when at least one key is a registered locale — otherwise it is a legacy value written
|
|
104
|
+
* before the branch became localized.
|
|
105
|
+
*/
|
|
106
|
+
function isStoredLocaleDictionary(branch, value, config) {
|
|
107
|
+
if (!isLocalizedBranch(branch) || !isLocaleDictionary(value))
|
|
108
|
+
return false;
|
|
109
|
+
return branch.type !== 'json' || Object.keys(value).some((key) => config.locales.includes(key));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Merges a write into the value stored for a localized branch. The write path's only way to persist a
|
|
113
|
+
* localized value, so a translation the write does not mention is never dropped (brief §2):
|
|
114
|
+
* - `write === null` clears the whole field (an explicit null keeps its pre-localization meaning);
|
|
115
|
+
* - otherwise the write is normalised with {@link toLocalizedPatch}: every registered locale it names
|
|
116
|
+
* overwrites that locale, a `null` entry clears that locale only, and every other stored locale —
|
|
117
|
+
* including ones no longer registered — is kept as-is;
|
|
118
|
+
* - a stored legacy value (written before the branch became localized) is the default-locale value.
|
|
119
|
+
* @returns The compacted dictionary, or `null` when no locale keeps a value.
|
|
120
|
+
*/
|
|
121
|
+
export function applyLocalizedPatch(branch, stored, write, config) {
|
|
122
|
+
if (write === null)
|
|
123
|
+
return null;
|
|
124
|
+
const merged = isStoredLocaleDictionary(branch, stored, config)
|
|
125
|
+
? { ...stored }
|
|
126
|
+
: isBlankLocaleValue(stored) ? {} : { [config.defaultLocale]: stored };
|
|
127
|
+
Object.assign(merged, toLocalizedPatch(write, config));
|
|
128
|
+
return compactLocalizedDictionary(merged);
|
|
129
|
+
}
|
|
130
|
+
/** Aliases of the localized branches a write carries — the fields it must merge rather than replace. */
|
|
131
|
+
export function localizedAliasesIn(seed, data) {
|
|
132
|
+
return seed.branches
|
|
133
|
+
.filter((branch) => isLocalizedBranch(branch) && Object.hasOwn(data, branch.alias) && data[branch.alias] !== undefined)
|
|
134
|
+
.map((branch) => branch.alias);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Returns `data` with every localized branch it carries merged into `stored` via
|
|
138
|
+
* {@link applyLocalizedPatch}; other keys pass through untouched. `stored` is the entry's current values
|
|
139
|
+
* (`null` on create, which only compacts). Returns `data` itself when `config` is undefined or no localized
|
|
140
|
+
* branch is written.
|
|
141
|
+
*/
|
|
142
|
+
export function mergeLocalizedFields(seed, stored, data, config) {
|
|
143
|
+
if (!config)
|
|
144
|
+
return data;
|
|
145
|
+
const aliases = localizedAliasesIn(seed, data);
|
|
146
|
+
if (aliases.length === 0)
|
|
147
|
+
return data;
|
|
148
|
+
const merged = { ...data };
|
|
149
|
+
for (const branch of seed.branches) {
|
|
150
|
+
if (!aliases.includes(branch.alias))
|
|
151
|
+
continue;
|
|
152
|
+
merged[branch.alias] = applyLocalizedPatch(branch, stored?.[branch.alias], data[branch.alias], config);
|
|
153
|
+
}
|
|
154
|
+
return merged;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Resolves a stored localized value to one language: the requested locale, then the default locale, then the
|
|
158
|
+
* first stored translation (stored key order), then `null`. A legacy value that is not a dictionary is returned
|
|
159
|
+
* as-is. Non-localized branches return `value` unchanged. `buildSelectQuery` applies the same chain in SQL, so
|
|
160
|
+
* filters and ORDER BY compare exactly what a reader sees.
|
|
161
|
+
*/
|
|
162
|
+
export function resolveLocalizedValue(branch, value, locale, config) {
|
|
163
|
+
if (!isStoredLocaleDictionary(branch, value, config))
|
|
164
|
+
return value;
|
|
165
|
+
if (!isBlankLocaleValue(value[locale]))
|
|
166
|
+
return value[locale];
|
|
167
|
+
if (!isBlankLocaleValue(value[config.defaultLocale]))
|
|
168
|
+
return value[config.defaultLocale];
|
|
169
|
+
// An implicit config follows `defaultLanguage`; changing it before languages are configured leaves every
|
|
170
|
+
// dictionary without its default key. Returning null there would blank the public site (brief §2).
|
|
171
|
+
const firstStored = Object.values(value).find((translation) => !isBlankLocaleValue(translation));
|
|
172
|
+
return firstStored ?? null;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Returns a copy of `data` with every localized branch resolved to `locale` (default: the default locale)
|
|
176
|
+
* via {@link resolveLocalizedValue}. Returns `data` itself when `config` is undefined.
|
|
177
|
+
*/
|
|
178
|
+
export function resolveLocalizedFields(seed, data, config, locale) {
|
|
179
|
+
if (!config)
|
|
180
|
+
return data;
|
|
181
|
+
const target = locale ?? config.defaultLocale;
|
|
182
|
+
const resolved = { ...data };
|
|
183
|
+
for (const branch of seed.branches) {
|
|
184
|
+
if (isLocalizedBranch(branch) && Object.hasOwn(resolved, branch.alias)) {
|
|
185
|
+
resolved[branch.alias] = resolveLocalizedValue(branch, resolved[branch.alias], target, config);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return resolved;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The whole dictionary of a stored localized value, for readers asking every language (`?lang=all`):
|
|
192
|
+
* a stored dictionary as-is — unregistered locales included, nothing is hidden (brief §2) — a legacy
|
|
193
|
+
* value as `{ [defaultLocale]: value }` (the write path's reading of it), a blank value as `null`.
|
|
194
|
+
* Non-localized branches return `value` unchanged.
|
|
195
|
+
*/
|
|
196
|
+
export function asLocaleDictionary(branch, value, config) {
|
|
197
|
+
if (!isLocalizedBranch(branch))
|
|
198
|
+
return value;
|
|
199
|
+
if (isStoredLocaleDictionary(branch, value, config))
|
|
200
|
+
return value;
|
|
201
|
+
return isBlankLocaleValue(value) ? null : { [config.defaultLocale]: value };
|
|
202
|
+
}
|
|
203
|
+
/** Returns a copy of `data` with every localized branch it carries passed through {@link asLocaleDictionary}. */
|
|
204
|
+
export function asLocaleDictionaries(seed, data, config) {
|
|
205
|
+
const expanded = { ...data };
|
|
206
|
+
for (const branch of seed.branches) {
|
|
207
|
+
if (isLocalizedBranch(branch) && Object.hasOwn(expanded, branch.alias)) {
|
|
208
|
+
expanded[branch.alias] = asLocaleDictionary(branch, expanded[branch.alias], config);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return expanded;
|
|
212
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ActorContext, Branch, DataClassification, Seed } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Computes an un-salted SHA-256 hex digest of a string value.
|
|
4
|
+
* Used for basic digest operations.
|
|
5
|
+
* @param value - The raw string value to hash.
|
|
6
|
+
* @returns A Promise resolving to a 64-character hex string.
|
|
7
|
+
*/
|
|
8
|
+
export declare function sha256hex(value: string): Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Verifies if a candidate string matches a stored SHA-256 hash digest.
|
|
11
|
+
* @param stored - The expected 64-character SHA-256 hex digest.
|
|
12
|
+
* @param candidate - The raw candidate string to verify.
|
|
13
|
+
* @returns A Promise resolving to true if candidate matches stored digest, false otherwise.
|
|
14
|
+
*/
|
|
15
|
+
export declare function verifyHashField(stored: string, candidate: string): Promise<boolean>;
|
|
16
|
+
/**
|
|
17
|
+
* Resolved classification rules for a branch, bundling storage strategy and API visibility defaults.
|
|
18
|
+
*/
|
|
19
|
+
export interface ResolvedClassification {
|
|
20
|
+
/** Canonical data classification tier. */
|
|
21
|
+
classification: DataClassification;
|
|
22
|
+
/** Storage mechanism at rest (`plain`, `encrypt` via AES-GCM, or `hash` via HMAC-SHA256). */
|
|
23
|
+
storage: 'plain' | 'encrypt' | 'hash';
|
|
24
|
+
/** Default visibility rule for unauthenticated public API endpoints (`full` or `hidden`). */
|
|
25
|
+
publicVisibility: 'full' | 'hidden';
|
|
26
|
+
/** Default visibility rule for authenticated API endpoints (`full` or `hidden`). */
|
|
27
|
+
authVisibility: 'full' | 'hidden';
|
|
28
|
+
}
|
|
29
|
+
/** Normalizes raw policy strings to canonical DataClassification enum. */
|
|
30
|
+
export declare function normalizeClassification(raw?: string): DataClassification;
|
|
31
|
+
/**
|
|
32
|
+
* Resolves the 4-tier DataClassification for a branch, bundling its storage
|
|
33
|
+
* strategy at rest and its API visibility rules (public vs auth context).
|
|
34
|
+
* @param branch - The seed branch definition to inspect.
|
|
35
|
+
* @returns The resolved classification details and visibility rules.
|
|
36
|
+
*/
|
|
37
|
+
export declare function resolveClassification(branch: Branch): ResolvedClassification;
|
|
38
|
+
/**
|
|
39
|
+
* Resolves branch policies applying all defaults and classification rules.
|
|
40
|
+
* @param branch - The seed branch definition to inspect.
|
|
41
|
+
* @returns Complete required policies object for the branch.
|
|
42
|
+
*/
|
|
43
|
+
export declare function resolvePolicies(branch: Branch): Required<NonNullable<Branch['policies']>>;
|
|
44
|
+
/**
|
|
45
|
+
* Filters entry payload fields based on the caller's {@link ActorContext} and branch classification tier.
|
|
46
|
+
*
|
|
47
|
+
* Rules:
|
|
48
|
+
* - Public actor: receives `public` fields; `internal`, `confidential`, and `restricted` fields are omitted.
|
|
49
|
+
* - Authenticated actor: receives `public`, `internal`, and `confidential` fields; `restricted` fields are ALWAYS omitted.
|
|
50
|
+
* - System actor: receives all fields including `restricted` (used for internal worker/automation orchestration).
|
|
51
|
+
*
|
|
52
|
+
* @param data - Raw record fields object.
|
|
53
|
+
* @param seed - Content type seed definition containing branch definitions.
|
|
54
|
+
* @param actor - Context of the caller requesting the entry payload (defaults to authenticated).
|
|
55
|
+
* @returns Filtered data record containing only authorized fields for the given actor context.
|
|
56
|
+
*/
|
|
57
|
+
export declare function filterEntryForActor(data: Record<string, unknown>, seed: Seed, actor?: ActorContext): Record<string, unknown>;
|
|
58
|
+
//# sourceMappingURL=policies.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policies.d.ts","sourceRoot":"","sources":["../../../src/engine/privacy/policies.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAGjF;;;;;GAKG;AACH,wBAAsB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAK9D;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAEzF;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,0CAA0C;IAC1C,cAAc,EAAE,kBAAkB,CAAA;IAClC,6FAA6F;IAC7F,OAAO,EAAE,OAAO,GAAG,SAAS,GAAG,MAAM,CAAA;IACrC,6FAA6F;IAC7F,gBAAgB,EAAE,MAAM,GAAG,QAAQ,CAAA;IACnC,oFAAoF;IACpF,cAAc,EAAE,MAAM,GAAG,QAAQ,CAAA;CAClC;AAED,0EAA0E;AAC1E,wBAAgB,uBAAuB,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,kBAAkB,CAQxE;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,sBAAsB,CAmC5E;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CA2BzF;AAuBD;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,IAAI,EAAE,IAAI,EACV,KAAK,GAAE,YAAwC,GAC9C,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAgDzB"}
|