@astram/core 1.5.0 → 1.7.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.
@@ -9,7 +9,7 @@
9
9
  */
10
10
  import type { Decision, Action, Resource } from '../authorization/index.js';
11
11
  /** Categories of auditable event. */
12
- export type AuditEventType = 'authentication' | 'session_lifecycle' | 'authorization_decision' | 'permission_change' | 'admin_action';
12
+ export type AuditEventType = 'authentication' | 'session_lifecycle' | 'authorization_decision' | 'permission_change' | 'admin_action' | 'file_access';
13
13
  /**
14
14
  * One immutable, attributable audit record. Actor is referenced by stable id;
15
15
  * the presentation layer resolves it to a display label (never surfaces raw ids
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/audit/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAE5E,qCAAqC;AACrC,MAAM,MAAM,cAAc,GACtB,gBAAgB,GAChB,mBAAmB,GACnB,wBAAwB,GACxB,mBAAmB,GACnB,cAAc,CAAC;AAEnB;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,gEAAgE;IAChE,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,2EAA2E;IAC3E,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,mDAAmD;IACnD,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,8CAA8C;IAC9C,QAAQ,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,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AAC,IAAI,CAAC,CAAC;CAC1C;AAMD,cAAc,YAAY,CAAC;AAE3B,cAAc,mBAAmB,CAAC;AAElC,cAAc,gBAAgB,CAAC;AAE/B,cAAc,aAAa,CAAC;AAG5B,cAAc,qBAAqB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/audit/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAyCH,iFAAiF;AACjF,2EAA2E;AAC3E,yEAAyE;AACzE,0FAA0F;AAC1F,cAAc,YAAY,CAAC;AAC3B,qFAAqF;AACrF,cAAc,mBAAmB,CAAC;AAClC,wEAAwE;AACxE,cAAc,gBAAgB,CAAC;AAC/B,+EAA+E;AAC/E,cAAc,aAAa,CAAC;AAC5B,qFAAqF;AACrF,4DAA4D;AAC5D,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/audit/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AA0CH,iFAAiF;AACjF,2EAA2E;AAC3E,yEAAyE;AACzE,0FAA0F;AAC1F,cAAc,YAAY,CAAC;AAC3B,qFAAqF;AACrF,cAAc,mBAAmB,CAAC;AAClC,wEAAwE;AACxE,cAAc,gBAAgB,CAAC;AAC/B,+EAA+E;AAC/E,cAAc,aAAa,CAAC;AAC5B,qFAAqF;AACrF,4DAA4D;AAC5D,cAAc,qBAAqB,CAAC"}
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Framework database names (configurable-framework-db, spec §5.A, §10, §13; ADR-0037).
3
+ *
4
+ * The single source of the four framework database names. `SHIELD_DB_NAMESPACE` renames all
5
+ * four together (`beta` ⇒ `beta_app`, `beta_identity`, `beta_authz`, `beta_audit`); unset keeps
6
+ * today's `shield_*` names. Pure and zero-dep (ADR-0003): the caller passes an env-like record,
7
+ * nothing here reads `process.env`.
8
+ *
9
+ * SECURITY: fail closed (Shield #1) — a set-but-empty or non-matching namespace throws; a
10
+ * failure never returns a partial set. Names reach SQL only after this validation, and the
11
+ * pattern admits no quote, backtick, dot or whitespace.
12
+ */
13
+ /** The env variable that selects the namespace. */
14
+ export declare const FRAMEWORK_DB_NAMESPACE_ENV = "SHIELD_DB_NAMESPACE";
15
+ /** The namespace used when `SHIELD_DB_NAMESPACE` is unset (today's names). */
16
+ export declare const DEFAULT_FRAMEWORK_DB_NAMESPACE = "shield";
17
+ /**
18
+ * Allowed namespace. `<ns>_identity`, the longest name, stays within MariaDB's 64-character
19
+ * identifier limit (55 + 9).
20
+ */
21
+ export declare const FRAMEWORK_DB_NAMESPACE_PATTERN: RegExp;
22
+ /** The fixed database suffixes, one per framework module. */
23
+ export declare const FRAMEWORK_SCHEMA_SUFFIXES: Readonly<{
24
+ readonly app: "_app";
25
+ readonly identity: "_identity";
26
+ readonly authz: "_authz";
27
+ readonly audit: "_audit";
28
+ }>;
29
+ /** The resolved framework database names. Always frozen. */
30
+ export interface FrameworkSchemas {
31
+ readonly namespace: string;
32
+ readonly app: string;
33
+ readonly identity: string;
34
+ readonly authz: string;
35
+ readonly audit: string;
36
+ /**
37
+ * True on the `shield` default: `SHIELD_DB_NAMESPACE` unset, or set to `shield` explicitly
38
+ * (which behaves exactly like unset).
39
+ */
40
+ readonly isDefault: boolean;
41
+ }
42
+ /** Thrown for an invalid framework database configuration. Never carries a secret. */
43
+ export declare class FrameworkSchemaConfigError extends Error {
44
+ constructor(message: string);
45
+ }
46
+ /**
47
+ * Resolve the four framework database names from an env-like record.
48
+ * @throws FrameworkSchemaConfigError when `SHIELD_DB_NAMESPACE` is set but empty or invalid.
49
+ */
50
+ export declare function resolveFrameworkSchemas(env: Readonly<Record<string, string | undefined>>): FrameworkSchemas;
51
+ /**
52
+ * The DB account name for a matrix account role under a namespace: `<ns>_<role>`
53
+ * (`frameworkAccountName('beta', 'core')` ⇒ `beta_core`). Roles are the matrix account names
54
+ * without the `shield_` prefix (`core`, `migrate`, `audit_ops`, ...).
55
+ */
56
+ export declare function frameworkAccountName(namespace: string, role: string): string;
57
+ /**
58
+ * A database name a caller may hand to framework SQL (`opts.schema`, `forRoot({ schemas })`):
59
+ * lower-case letters, digits and `_`, 1..64 characters (MariaDB's identifier limit). Names come
60
+ * from the resolver or from operator config, both lower-case (MariaDB database names are
61
+ * case-sensitive on Linux, so a mixed-case name is refused rather than guessed). It admits no
62
+ * backtick, quote, dot, whitespace or `;`, so a name that passes can be backtick-quoted safely.
63
+ */
64
+ export declare const SCHEMA_NAME_PATTERN: RegExp;
65
+ /**
66
+ * Check a caller-supplied database name before it reaches SQL (configurable-framework-db,
67
+ * spec §11). Returns the name unchanged.
68
+ * @param label names the setting in the error (e.g. `opts.schema`); the value is never echoed.
69
+ * @throws FrameworkSchemaConfigError when the name is not a string matching
70
+ * {@link SCHEMA_NAME_PATTERN}.
71
+ */
72
+ export declare function assertSchemaName(name: unknown, label: string): string;
73
+ /** The four framework database names, as `FrameworkSchemas` or any object carrying them. */
74
+ export type FrameworkSchemaNameSet = Pick<FrameworkSchemas, 'app' | 'identity' | 'authz' | 'audit'>;
75
+ /**
76
+ * Check all four names of a caller-supplied set (`forRoot({ schemas })`, a store's `schemas`
77
+ * argument, `ctx.schemas`) with {@link assertSchemaName}. Returns the same object.
78
+ * @throws FrameworkSchemaConfigError naming `<label>.<field>` for the first bad name.
79
+ */
80
+ export declare function assertFrameworkSchemaNames<T extends FrameworkSchemaNameSet>(schemas: T, label: string): T;
81
+ /** The context shape {@link frameworkSchemasForMigration} reads (any `@astram/migrate` version). */
82
+ export interface MigrationSchemasContext {
83
+ readonly schemas?: FrameworkSchemas | undefined;
84
+ }
85
+ /**
86
+ * The framework database names for a framework migration's `up`/`down` (spec v2 §5.H, decided
87
+ * Q2). `@astram/guard` and `@astram/identity` do not depend on `@astram/migrate`, so their
88
+ * migrations may run under an older CLI that passes no `ctx.schemas`:
89
+ * - `ctx.schemas` present (current `@astram/migrate`) ⇒ those names, validated;
90
+ * - absent and `SHIELD_DB_NAMESPACE` unset (or `shield`) ⇒ the default `shield_*` names (today's
91
+ * behaviour);
92
+ * - absent and `SHIELD_DB_NAMESPACE` set to any other value ⇒ throws before any DDL, telling the
93
+ * operator to upgrade `@astram/migrate`.
94
+ *
95
+ * Usage in a migration file: `const s = frameworkSchemasForMigration(ctx, process.env);`.
96
+ * Pure: the caller passes the env.
97
+ * @throws FrameworkSchemaConfigError as above, or for an invalid name in `ctx.schemas`.
98
+ */
99
+ export declare function frameworkSchemasForMigration(ctx: MigrationSchemasContext | undefined, env: Readonly<Record<string, string | undefined>>): FrameworkSchemas;
100
+ /** The `@astram/migrate` ledger table when `APP_MIGRATION_TABLE` is unset. */
101
+ export declare const DEFAULT_MIGRATION_LEDGER_TABLE = "schema_migrations";
102
+ /** A ledger table name: a plain identifier, safe to backtick-quote. */
103
+ export declare const MIGRATION_LEDGER_TABLE_PATTERN: RegExp;
104
+ /** Where the `@astram/migrate` ledger (tracking table) lives. */
105
+ export interface MigrationLedgerLocation {
106
+ readonly database: string;
107
+ readonly table: string;
108
+ }
109
+ /**
110
+ * An operator-supplied ledger database name (`DDL_DB_NAME`, or a migrate job's `APP_DB_NAME` in
111
+ * default mode): any plain MariaDB identifier, mixed case included, so a name that worked with
112
+ * earlier `@astram/migrate` releases keeps working. Letters, digits and `_` only, 1..64: no
113
+ * backtick, quote, dot, whitespace or `;`, so it is safe to backtick-quote or single-quote.
114
+ * Resolver-produced names stay on the stricter {@link SCHEMA_NAME_PATTERN}.
115
+ */
116
+ export declare const LEDGER_DATABASE_NAME_PATTERN: RegExp;
117
+ /**
118
+ * Check an operator-supplied ledger database name before it reaches SQL. Returns it unchanged.
119
+ * @param label names the setting in the error (e.g. `DDL_DB_NAME`); the value is never echoed.
120
+ * @throws FrameworkSchemaConfigError when the name does not match
121
+ * {@link LEDGER_DATABASE_NAME_PATTERN}.
122
+ */
123
+ export declare function assertLedgerDatabaseName(name: unknown, label: string): string;
124
+ /**
125
+ * The ledger-location rule of the migrate CLI (configurable-framework-db spec 5.G):
126
+ * - namespace set ⇒ always `<ns>_app`; a `DDL_DB_NAME` other than `<ns>_app` throws;
127
+ * - namespace unset (or `shield`) ⇒ `DDL_DB_NAME`, else the default app database. (The CLI adds
128
+ * its released fallback to the job's `APP_DB_NAME` before `shield_app`.)
129
+ * Table: `APP_MIGRATION_TABLE`, else `schema_migrations`. The runtime boot check does not use this
130
+ * single location: it tries {@link migrationLedgerCandidates}. Pure: the caller passes the env.
131
+ * @throws FrameworkSchemaConfigError naming the variable on a conflicting or invalid name.
132
+ */
133
+ export declare function resolveMigrationLedgerLocation(schemas: FrameworkSchemas, env: Readonly<Record<string, string | undefined>>): MigrationLedgerLocation;
134
+ /**
135
+ * Where the runtime boot check looks for the ledger, in order (G-007 QA P1). A standard scaffold
136
+ * runs `astram-migrate` with the server's env, so in default mode migrate writes the ledger to
137
+ * `DDL_DB_NAME`, else the job's `APP_DB_NAME`, else `shield_app`; the boot check cannot know
138
+ * which env the migrate job had, so it tries each:
139
+ * - namespace set ⇒ only `<ns>_app` (as {@link resolveMigrationLedgerLocation}; `APP_DB_NAME`
140
+ * is ignored, a conflicting `DDL_DB_NAME` throws);
141
+ * - namespace unset (or `shield`) ⇒ `DDL_DB_NAME`, the runtime `APP_DB_NAME`, then the default
142
+ * app database; unset or blank values skipped, duplicates dropped.
143
+ * The caller uses the first location whose ledger table exists and must still find every shipped
144
+ * id there; when none exists it fails closed naming every location. Pure: the caller passes the
145
+ * env. Returns a frozen, non-empty list.
146
+ * @throws FrameworkSchemaConfigError naming the variable on a conflicting or invalid name.
147
+ */
148
+ export declare function migrationLedgerCandidates(schemas: FrameworkSchemas, env: Readonly<Record<string, string | undefined>>): readonly MigrationLedgerLocation[];
149
+ //# sourceMappingURL=framework-schemas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"framework-schemas.d.ts","sourceRoot":"","sources":["../../src/database/framework-schemas.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,mDAAmD;AACnD,eAAO,MAAM,0BAA0B,w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AAE,MAAM,GACZ,CAAC,CAUH;AAED,oGAAoG;AACpG,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,OAAO,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAAC;CACjD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,4BAA4B,CAC1C,GAAG,EAAE,uBAAuB,GAAG,SAAS,EACxC,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GAChD,gBAAgB,CAWlB;AAED,8EAA8E;AAC9E,eAAO,MAAM,8BAA8B,sBAAsB,CAAC;AAElE,uEAAuE;AACvE,eAAO,MAAM,8BAA8B,QAA0B,CAAC;AAEtE,iEAAiE;AACjE,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,4BAA4B,QAAyB,CAAC;AAEnE;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAO7E;AAYD;;;;;;;;GAQG;AACH,wBAAgB,8BAA8B,CAC5C,OAAO,EAAE,gBAAgB,EACzB,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GAChD,uBAAuB,CAmBzB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,gBAAgB,EACzB,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GAChD,SAAS,uBAAuB,EAAE,CAepC"}
@@ -0,0 +1,222 @@
1
+ /**
2
+ * Framework database names (configurable-framework-db, spec §5.A, §10, §13; ADR-0037).
3
+ *
4
+ * The single source of the four framework database names. `SHIELD_DB_NAMESPACE` renames all
5
+ * four together (`beta` ⇒ `beta_app`, `beta_identity`, `beta_authz`, `beta_audit`); unset keeps
6
+ * today's `shield_*` names. Pure and zero-dep (ADR-0003): the caller passes an env-like record,
7
+ * nothing here reads `process.env`.
8
+ *
9
+ * SECURITY: fail closed (Shield #1) — a set-but-empty or non-matching namespace throws; a
10
+ * failure never returns a partial set. Names reach SQL only after this validation, and the
11
+ * pattern admits no quote, backtick, dot or whitespace.
12
+ */
13
+ /** The env variable that selects the namespace. */
14
+ export const FRAMEWORK_DB_NAMESPACE_ENV = 'SHIELD_DB_NAMESPACE';
15
+ /** The namespace used when `SHIELD_DB_NAMESPACE` is unset (today's names). */
16
+ export const DEFAULT_FRAMEWORK_DB_NAMESPACE = 'shield';
17
+ /**
18
+ * Allowed namespace. `<ns>_identity`, the longest name, stays within MariaDB's 64-character
19
+ * identifier limit (55 + 9).
20
+ */
21
+ // Letters and digits only (spec v3): `_` is a one-character wildcard in MariaDB database-level
22
+ // grants, so a namespace containing `_` could make one environment's grants match another's
23
+ // databases (`team_a_app` also matches `team1a_app`).
24
+ export const FRAMEWORK_DB_NAMESPACE_PATTERN = /^[a-z][a-z0-9]{0,54}$/;
25
+ /** The fixed database suffixes, one per framework module. */
26
+ export const FRAMEWORK_SCHEMA_SUFFIXES = Object.freeze({
27
+ app: '_app',
28
+ identity: '_identity',
29
+ authz: '_authz',
30
+ audit: '_audit',
31
+ });
32
+ /** Thrown for an invalid framework database configuration. Never carries a secret. */
33
+ export class FrameworkSchemaConfigError extends Error {
34
+ constructor(message) {
35
+ super(message);
36
+ this.name = 'FrameworkSchemaConfigError';
37
+ }
38
+ }
39
+ function namesFor(namespace, isDefault) {
40
+ return Object.freeze({
41
+ namespace,
42
+ app: `${namespace}${FRAMEWORK_SCHEMA_SUFFIXES.app}`,
43
+ identity: `${namespace}${FRAMEWORK_SCHEMA_SUFFIXES.identity}`,
44
+ authz: `${namespace}${FRAMEWORK_SCHEMA_SUFFIXES.authz}`,
45
+ audit: `${namespace}${FRAMEWORK_SCHEMA_SUFFIXES.audit}`,
46
+ isDefault,
47
+ });
48
+ }
49
+ /**
50
+ * Resolve the four framework database names from an env-like record.
51
+ * @throws FrameworkSchemaConfigError when `SHIELD_DB_NAMESPACE` is set but empty or invalid.
52
+ */
53
+ export function resolveFrameworkSchemas(env) {
54
+ const raw = env[FRAMEWORK_DB_NAMESPACE_ENV];
55
+ if (raw === undefined)
56
+ return namesFor(DEFAULT_FRAMEWORK_DB_NAMESPACE, true);
57
+ if (!FRAMEWORK_DB_NAMESPACE_PATTERN.test(raw)) {
58
+ // The value is not echoed: it is config, not a secret, but the message stays a fixed shape.
59
+ throw new FrameworkSchemaConfigError(`${FRAMEWORK_DB_NAMESPACE_ENV} is set but ${raw === '' ? 'empty' : 'invalid'}; ` +
60
+ `expected ${FRAMEWORK_DB_NAMESPACE_PATTERN.source} (unset it for the default "${DEFAULT_FRAMEWORK_DB_NAMESPACE}")`);
61
+ }
62
+ // An explicit default namespace is the default: same names AND the same rules (ledger, old
63
+ // migrate fallback), so `SHIELD_DB_NAMESPACE=shield` never behaves differently from unset.
64
+ return namesFor(raw, raw === DEFAULT_FRAMEWORK_DB_NAMESPACE);
65
+ }
66
+ /**
67
+ * The DB account name for a matrix account role under a namespace: `<ns>_<role>`
68
+ * (`frameworkAccountName('beta', 'core')` ⇒ `beta_core`). Roles are the matrix account names
69
+ * without the `shield_` prefix (`core`, `migrate`, `audit_ops`, ...).
70
+ */
71
+ export function frameworkAccountName(namespace, role) {
72
+ return `${namespace}_${role}`;
73
+ }
74
+ /**
75
+ * A database name a caller may hand to framework SQL (`opts.schema`, `forRoot({ schemas })`):
76
+ * lower-case letters, digits and `_`, 1..64 characters (MariaDB's identifier limit). Names come
77
+ * from the resolver or from operator config, both lower-case (MariaDB database names are
78
+ * case-sensitive on Linux, so a mixed-case name is refused rather than guessed). It admits no
79
+ * backtick, quote, dot, whitespace or `;`, so a name that passes can be backtick-quoted safely.
80
+ */
81
+ export const SCHEMA_NAME_PATTERN = /^[a-z0-9_]{1,64}$/;
82
+ /**
83
+ * Check a caller-supplied database name before it reaches SQL (configurable-framework-db,
84
+ * spec §11). Returns the name unchanged.
85
+ * @param label names the setting in the error (e.g. `opts.schema`); the value is never echoed.
86
+ * @throws FrameworkSchemaConfigError when the name is not a string matching
87
+ * {@link SCHEMA_NAME_PATTERN}.
88
+ */
89
+ export function assertSchemaName(name, label) {
90
+ if (typeof name !== 'string' || !SCHEMA_NAME_PATTERN.test(name)) {
91
+ throw new FrameworkSchemaConfigError(`${label} is not a valid database name; expected ${SCHEMA_NAME_PATTERN.source}`);
92
+ }
93
+ return name;
94
+ }
95
+ /**
96
+ * Check all four names of a caller-supplied set (`forRoot({ schemas })`, a store's `schemas`
97
+ * argument, `ctx.schemas`) with {@link assertSchemaName}. Returns the same object.
98
+ * @throws FrameworkSchemaConfigError naming `<label>.<field>` for the first bad name.
99
+ */
100
+ export function assertFrameworkSchemaNames(schemas, label) {
101
+ if (typeof schemas !== 'object' || schemas === null) {
102
+ throw new FrameworkSchemaConfigError(`${label} must be an object with app, identity, authz and audit database names`);
103
+ }
104
+ for (const key of ['app', 'identity', 'authz', 'audit']) {
105
+ assertSchemaName(schemas[key], `${label}.${key}`);
106
+ }
107
+ return schemas;
108
+ }
109
+ /**
110
+ * The framework database names for a framework migration's `up`/`down` (spec v2 §5.H, decided
111
+ * Q2). `@astram/guard` and `@astram/identity` do not depend on `@astram/migrate`, so their
112
+ * migrations may run under an older CLI that passes no `ctx.schemas`:
113
+ * - `ctx.schemas` present (current `@astram/migrate`) ⇒ those names, validated;
114
+ * - absent and `SHIELD_DB_NAMESPACE` unset (or `shield`) ⇒ the default `shield_*` names (today's
115
+ * behaviour);
116
+ * - absent and `SHIELD_DB_NAMESPACE` set to any other value ⇒ throws before any DDL, telling the
117
+ * operator to upgrade `@astram/migrate`.
118
+ *
119
+ * Usage in a migration file: `const s = frameworkSchemasForMigration(ctx, process.env);`.
120
+ * Pure: the caller passes the env.
121
+ * @throws FrameworkSchemaConfigError as above, or for an invalid name in `ctx.schemas`.
122
+ */
123
+ export function frameworkSchemasForMigration(ctx, env) {
124
+ const given = ctx?.schemas;
125
+ if (given !== undefined)
126
+ return assertFrameworkSchemaNames(given, 'ctx.schemas');
127
+ const ns = env[FRAMEWORK_DB_NAMESPACE_ENV];
128
+ if (ns !== undefined && ns !== DEFAULT_FRAMEWORK_DB_NAMESPACE) {
129
+ throw new FrameworkSchemaConfigError(`${FRAMEWORK_DB_NAMESPACE_ENV} is set but this @astram/migrate does not pass ctx.schemas; ` +
130
+ 'upgrade @astram/migrate');
131
+ }
132
+ return namesFor(DEFAULT_FRAMEWORK_DB_NAMESPACE, true);
133
+ }
134
+ /** The `@astram/migrate` ledger table when `APP_MIGRATION_TABLE` is unset. */
135
+ export const DEFAULT_MIGRATION_LEDGER_TABLE = 'schema_migrations';
136
+ /** A ledger table name: a plain identifier, safe to backtick-quote. */
137
+ export const MIGRATION_LEDGER_TABLE_PATTERN = /^[A-Za-z0-9_$]{1,64}$/;
138
+ /**
139
+ * An operator-supplied ledger database name (`DDL_DB_NAME`, or a migrate job's `APP_DB_NAME` in
140
+ * default mode): any plain MariaDB identifier, mixed case included, so a name that worked with
141
+ * earlier `@astram/migrate` releases keeps working. Letters, digits and `_` only, 1..64: no
142
+ * backtick, quote, dot, whitespace or `;`, so it is safe to backtick-quote or single-quote.
143
+ * Resolver-produced names stay on the stricter {@link SCHEMA_NAME_PATTERN}.
144
+ */
145
+ export const LEDGER_DATABASE_NAME_PATTERN = /^[A-Za-z0-9_]{1,64}$/;
146
+ /**
147
+ * Check an operator-supplied ledger database name before it reaches SQL. Returns it unchanged.
148
+ * @param label names the setting in the error (e.g. `DDL_DB_NAME`); the value is never echoed.
149
+ * @throws FrameworkSchemaConfigError when the name does not match
150
+ * {@link LEDGER_DATABASE_NAME_PATTERN}.
151
+ */
152
+ export function assertLedgerDatabaseName(name, label) {
153
+ if (typeof name !== 'string' || !LEDGER_DATABASE_NAME_PATTERN.test(name)) {
154
+ throw new FrameworkSchemaConfigError(`${label} is not a valid database name; expected ${LEDGER_DATABASE_NAME_PATTERN.source}`);
155
+ }
156
+ return name;
157
+ }
158
+ function ledgerTable(env) {
159
+ const table = env.APP_MIGRATION_TABLE ?? DEFAULT_MIGRATION_LEDGER_TABLE;
160
+ if (!MIGRATION_LEDGER_TABLE_PATTERN.test(table)) {
161
+ throw new FrameworkSchemaConfigError(`APP_MIGRATION_TABLE is not a valid table name; expected ${MIGRATION_LEDGER_TABLE_PATTERN.source}`);
162
+ }
163
+ return table;
164
+ }
165
+ /**
166
+ * The ledger-location rule of the migrate CLI (configurable-framework-db spec 5.G):
167
+ * - namespace set ⇒ always `<ns>_app`; a `DDL_DB_NAME` other than `<ns>_app` throws;
168
+ * - namespace unset (or `shield`) ⇒ `DDL_DB_NAME`, else the default app database. (The CLI adds
169
+ * its released fallback to the job's `APP_DB_NAME` before `shield_app`.)
170
+ * Table: `APP_MIGRATION_TABLE`, else `schema_migrations`. The runtime boot check does not use this
171
+ * single location: it tries {@link migrationLedgerCandidates}. Pure: the caller passes the env.
172
+ * @throws FrameworkSchemaConfigError naming the variable on a conflicting or invalid name.
173
+ */
174
+ export function resolveMigrationLedgerLocation(schemas, env) {
175
+ const ddl = env.DDL_DB_NAME;
176
+ let database;
177
+ if (schemas.isDefault) {
178
+ database =
179
+ ddl === undefined
180
+ ? assertSchemaName(schemas.app, 'ledger')
181
+ : assertLedgerDatabaseName(ddl, 'DDL_DB_NAME');
182
+ }
183
+ else {
184
+ if (ddl !== undefined && ddl !== schemas.app) {
185
+ throw new FrameworkSchemaConfigError(`DDL_DB_NAME must be ${schemas.app} or unset when ` +
186
+ `${FRAMEWORK_DB_NAMESPACE_ENV}=${schemas.namespace}: the migration ledger lives in ` +
187
+ `${schemas.app} (spec 5.G).`);
188
+ }
189
+ database = schemas.app;
190
+ }
191
+ return { database, table: ledgerTable(env) };
192
+ }
193
+ /**
194
+ * Where the runtime boot check looks for the ledger, in order (G-007 QA P1). A standard scaffold
195
+ * runs `astram-migrate` with the server's env, so in default mode migrate writes the ledger to
196
+ * `DDL_DB_NAME`, else the job's `APP_DB_NAME`, else `shield_app`; the boot check cannot know
197
+ * which env the migrate job had, so it tries each:
198
+ * - namespace set ⇒ only `<ns>_app` (as {@link resolveMigrationLedgerLocation}; `APP_DB_NAME`
199
+ * is ignored, a conflicting `DDL_DB_NAME` throws);
200
+ * - namespace unset (or `shield`) ⇒ `DDL_DB_NAME`, the runtime `APP_DB_NAME`, then the default
201
+ * app database; unset or blank values skipped, duplicates dropped.
202
+ * The caller uses the first location whose ledger table exists and must still find every shipped
203
+ * id there; when none exists it fails closed naming every location. Pure: the caller passes the
204
+ * env. Returns a frozen, non-empty list.
205
+ * @throws FrameworkSchemaConfigError naming the variable on a conflicting or invalid name.
206
+ */
207
+ export function migrationLedgerCandidates(schemas, env) {
208
+ if (!schemas.isDefault) {
209
+ return Object.freeze([Object.freeze(resolveMigrationLedgerLocation(schemas, env))]);
210
+ }
211
+ const table = ledgerTable(env);
212
+ const databases = [];
213
+ for (const key of ['DDL_DB_NAME', 'APP_DB_NAME']) {
214
+ const raw = env[key];
215
+ if (raw === undefined || raw.trim() === '')
216
+ continue;
217
+ databases.push(assertLedgerDatabaseName(raw.trim(), key));
218
+ }
219
+ databases.push(assertSchemaName(schemas.app, 'ledger'));
220
+ return Object.freeze([...new Set(databases)].map((database) => Object.freeze({ database, table })));
221
+ }
222
+ //# sourceMappingURL=framework-schemas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"framework-schemas.js","sourceRoot":"","sources":["../../src/database/framework-schemas.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,mDAAmD;AACnD,MAAM,CAAC,MAAM,0BAA0B,GAAG,qBAAqB,CAAC;AAEhE,8EAA8E;AAC9E,MAAM,CAAC,MAAM,8BAA8B,GAAG,QAAQ,CAAC;AAEvD;;;GAGG;AACH,+FAA+F;AAC/F,4FAA4F;AAC5F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AAC,GAAG,CAAC,CAAC;QACrB,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,CAAC,IAAI,EAAE,KAAK,EAAE;YAAE,SAAS;QACrD,SAAS,CAAC,IAAI,CAAC,wBAAwB,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;IAC5D,CAAC;IACD,SAAS,CAAC,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC;IACxD,OAAO,MAAM,CAAC,MAAM,CAClB,CAAC,GAAG,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC,CAC9E,CAAC;AACJ,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Framework database names (configurable-framework-db, ADR-0037): the single source of the
3
+ * four `<namespace>_{app,identity,authz,audit}` names and the namespaced account defaults.
4
+ * Pure + zero-dep (ADR-0003).
5
+ */
6
+ export type { FrameworkSchemas, FrameworkSchemaNameSet, MigrationSchemasContext, } from './framework-schemas.js';
7
+ export { DEFAULT_FRAMEWORK_DB_NAMESPACE, FRAMEWORK_DB_NAMESPACE_ENV, FRAMEWORK_DB_NAMESPACE_PATTERN, FRAMEWORK_SCHEMA_SUFFIXES, FrameworkSchemaConfigError, frameworkAccountName, resolveFrameworkSchemas, SCHEMA_NAME_PATTERN, assertSchemaName, assertFrameworkSchemaNames, frameworkSchemasForMigration, DEFAULT_MIGRATION_LEDGER_TABLE, MIGRATION_LEDGER_TABLE_PATTERN, resolveMigrationLedgerLocation, LEDGER_DATABASE_NAME_PATTERN, assertLedgerDatabaseName, migrationLedgerCandidates, } from './framework-schemas.js';
8
+ export type { MigrationLedgerLocation } from './framework-schemas.js';
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EACV,gBAAgB,EAChB,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,8BAA8B,EAC9B,0BAA0B,EAC1B,8BAA8B,EAC9B,yBAAyB,EACzB,0BAA0B,EAC1B,oBAAoB,EACpB,uBAAuB,EAEvB,mBAAmB,EACnB,gBAAgB,EAChB,0BAA0B,EAC1B,4BAA4B,EAE5B,8BAA8B,EAC9B,8BAA8B,EAC9B,8BAA8B,EAG9B,4BAA4B,EAC5B,wBAAwB,EACxB,yBAAyB,GAC1B,MAAM,wBAAwB,CAAC;AAChC,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Framework database names (configurable-framework-db, ADR-0037): the single source of the
3
+ * four `<namespace>_{app,identity,authz,audit}` names and the namespaced account defaults.
4
+ * Pure + zero-dep (ADR-0003).
5
+ */
6
+ export { DEFAULT_FRAMEWORK_DB_NAMESPACE, FRAMEWORK_DB_NAMESPACE_ENV, FRAMEWORK_DB_NAMESPACE_PATTERN, FRAMEWORK_SCHEMA_SUFFIXES, FrameworkSchemaConfigError, frameworkAccountName, resolveFrameworkSchemas,
7
+ // Phase 4: caller-supplied name checks + the migration fallback helper (append-only).
8
+ SCHEMA_NAME_PATTERN, assertSchemaName, assertFrameworkSchemaNames, frameworkSchemasForMigration,
9
+ // Phase 6: the one migration-ledger location rule (migrate CLI + runtime boot check).
10
+ DEFAULT_MIGRATION_LEDGER_TABLE, MIGRATION_LEDGER_TABLE_PATTERN, resolveMigrationLedgerLocation,
11
+ // G-007 QA P1/P2-3: the boot check's candidate ledger locations and the operator ledger-name
12
+ // check (append-only).
13
+ LEDGER_DATABASE_NAME_PATTERN, assertLedgerDatabaseName, migrationLedgerCandidates, } from './framework-schemas.js';
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAOH,OAAO,EACL,8BAA8B,EAC9B,0BAA0B,EAC1B,8BAA8B,EAC9B,yBAAyB,EACzB,0BAA0B,EAC1B,oBAAoB,EACpB,uBAAuB;AACvB,sFAAsF;AACtF,mBAAmB,EACnB,gBAAgB,EAChB,0BAA0B,EAC1B,4BAA4B;AAC5B,sFAAsF;AACtF,8BAA8B,EAC9B,8BAA8B,EAC9B,8BAA8B;AAC9B,6FAA6F;AAC7F,uBAAuB;AACvB,4BAA4B,EAC5B,wBAAwB,EACxB,yBAAyB,GAC1B,MAAM,wBAAwB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -42,4 +42,6 @@ export * from './encryption/index.js';
42
42
  export * from './secrets/index.js';
43
43
  export * from './metadata/index.js';
44
44
  export * from './admin/index.js';
45
+ export * from './storage/index.js';
46
+ export * from './database/index.js';
45
47
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAGH,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,qBAAqB,CAAC;AAKpC,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAGH,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,qBAAqB,CAAC;AAKpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,qBAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -47,4 +47,7 @@ export * from './metadata/index.js';
47
47
  // prior-layer seam changed): admin ports + query/command types + §11.3 safeguard
48
48
  // signatures + the query/command service factories + command-pipeline order.
49
49
  export * from './admin/index.js';
50
+ export * from './storage/index.js';
51
+ // Framework database names (configurable-framework-db, ADR-0037) — append-only.
52
+ export * from './database/index.js';
50
53
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,kEAAkE;AAClE,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAEnC,mFAAmF;AACnF,cAAc,qBAAqB,CAAC;AAEpC,4EAA4E;AAC5E,iFAAiF;AACjF,6EAA6E;AAC7E,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,kEAAkE;AAClE,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAEnC,mFAAmF;AACnF,cAAc,qBAAqB,CAAC;AAEpC,4EAA4E;AAC5E,iFAAiF;AACjF,6EAA6E;AAC7E,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AAEnC,gFAAgF;AAChF,cAAc,qBAAqB,CAAC"}
@@ -0,0 +1,90 @@
1
+ /**
2
+ * The pure file-ingest check (SPEC docs/features/file-storage/SPEC.md §5 D, §10, §13): decides
3
+ * whether one uploaded file is acceptable for a named {@link FileProfile} and, if so, returns the
4
+ * server-built metadata (`storageKey`, `sha256`, canonical `contentType`, `sizeBytes`, sanitised
5
+ * `originalName`) the storage layer writes. R-8 factory: `createFileIngest(deps)` over injected
6
+ * ports (`ByteDigest`, `newId`) and the profile table.
7
+ *
8
+ * ⛔ CLIENT INPUT IS NEVER TRUSTED: the declared MIME type only has to be IN the allowlist; the
9
+ * returned `contentType` is the profile's own spelling. The bytes must pass the profile's
10
+ * `magic` check. No part of the storage key comes from the request: it is
11
+ * `t/<tenantId>/<newId()>`, and `tenantId` (from the session) is re-checked as a canonical uuid.
12
+ * ⛔ NO LEAKAGE: every rejection is a {@link FileRejectedError} with a FIXED reason code; messages
13
+ * never echo the file name, the declared type or the bytes.
14
+ * ⛔ CHEAP CHECKS FIRST: size is checked before any parsing or hashing; an id is drawn only for an
15
+ * accepted file.
16
+ * ⛔ CEILING AT BOOT: a profile above {@link FILE_MAX_BYTES_CEILING} throws at construction.
17
+ *
18
+ * Pure: no IO, no clock, no randomness. ADR-0003: framework-agnostic — no framework / node imports.
19
+ */
20
+ import type { ByteDigest } from './ports.js';
21
+ /** Hard upper bound for any profile's `maxBytes` (50 MiB). No profile, route or env value can exceed it. */
22
+ export declare const FILE_MAX_BYTES_CEILING: number;
23
+ /**
24
+ * The fixed rejection reasons (SPEC §10). `createFileIngest` emits the first four; the multipart
25
+ * intake (guard `FileIntake`) emits `too_many_files` and `missing_file`.
26
+ */
27
+ export type FileRejectionReason = 'empty' | 'too_large' | 'type_not_allowed' | 'content_mismatch' | 'too_many_files' | 'missing_file';
28
+ /** A file was refused. The message is fixed per reason: no name, type or bytes are ever included. */
29
+ export declare class FileRejectedError extends Error {
30
+ readonly reason: FileRejectionReason;
31
+ constructor(reason: FileRejectionReason);
32
+ }
33
+ /** What an upload of one kind may be. Every app-added profile MUST supply its own `magic` check. */
34
+ export interface FileProfile {
35
+ /** Per-file byte cap, a positive integer at most {@link FILE_MAX_BYTES_CEILING}. */
36
+ readonly maxBytes: number;
37
+ /** Allowed declared content types (compared case-insensitively). The first match is returned as canonical. */
38
+ readonly contentTypes: readonly string[];
39
+ /** Allowed file-name extensions, with the leading dot (compared case-insensitively). */
40
+ readonly extensions: readonly string[];
41
+ /** Content check over the raw bytes. `false` (or a throw) rejects with `content_mismatch`. */
42
+ readonly magic: (bytes: Uint8Array) => boolean;
43
+ }
44
+ /** Ports and configuration for {@link createFileIngest}. */
45
+ export interface FileIngestDeps {
46
+ readonly digest: ByteDigest;
47
+ /** Server-side id for the storage key's last segment (e.g. a uuid v4). */
48
+ readonly newId: () => string;
49
+ /** Profiles by name; a request names one (e.g. `'xlsx'`). */
50
+ readonly profiles: Readonly<Record<string, FileProfile>>;
51
+ }
52
+ /** One file to check. `tenantId` comes from the session; everything else is client-supplied. */
53
+ export interface FileIngestInput {
54
+ readonly tenantId: string;
55
+ /** Name of a profile in `deps.profiles` (server-chosen by the route). */
56
+ readonly profile: string;
57
+ readonly bytes: Uint8Array;
58
+ readonly declaredContentType: string;
59
+ readonly originalName: string;
60
+ }
61
+ /** The accepted file's server-built metadata. */
62
+ export interface IngestedFile {
63
+ /** `t/<tenantId>/<newId()>`; never contains client input. */
64
+ readonly storageKey: string;
65
+ /** Lower-case hex sha256 of the exact bytes. */
66
+ readonly sha256: string;
67
+ /** Canonical content type from the profile allowlist, never the client header. */
68
+ readonly contentType: string;
69
+ readonly sizeBytes: number;
70
+ /** Sanitised display name (basename, no control characters, NFC, at most 255 code points). */
71
+ readonly originalName: string;
72
+ }
73
+ export interface FileIngest {
74
+ /** Check one file; resolves to its metadata or rejects with {@link FileRejectedError}. */
75
+ ingest(input: FileIngestInput): Promise<IngestedFile>;
76
+ /**
77
+ * The per-file byte cap of a named profile, or `undefined` when the profile is unknown. Lets a
78
+ * transport (guard `FileIntake`) size its parser limit from the configured cap. Optional so a
79
+ * hand-written `FileIngest` stays valid; `createFileIngest` always provides it.
80
+ */
81
+ maxBytesFor?(profile: string): number | undefined;
82
+ }
83
+ /**
84
+ * Build the ingest check. Validates every profile now (boot): a cap that is not a positive
85
+ * integer or exceeds {@link FILE_MAX_BYTES_CEILING} throws here, never at request time.
86
+ */
87
+ export declare function createFileIngest(deps: FileIngestDeps): FileIngest;
88
+ /** The `.xlsx` profile: 10 MiB default cap, the spreadsheetml content type, OOXML content check. */
89
+ export declare const XLSX_PROFILE: FileProfile;
90
+ //# sourceMappingURL=file-ingest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-ingest.d.ts","sourceRoot":"","sources":["../../src/storage/file-ingest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,4GAA4G;AAC5G,eAAO,MAAM,sBAAsB,QAAmB,CAAC;AAKvD;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAC3B,OAAO,GACP,WAAW,GACX,kBAAkB,GAClB,kBAAkB,GAClB,gBAAgB,GAChB,cAAc,CAAC;AAEnB,qGAAqG;AACrG,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,CAAC,MAAM,EAAE,mBAAmB,CAAC;gBACzB,MAAM,EAAE,mBAAmB;CAKxC;AAED,oGAAoG;AACpG,MAAM,WAAW,WAAW;IAC1B,oFAAoF;IACpF,QAAQ,CAAC,Q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