From ab0a8fc2fb73bd73f1776f0c1d1004cc9564fa96 Mon Sep 17 00:00:00 2001 From: Ofri Wolfus Date: Thu, 29 Oct 2026 13:20:18 +0200 Subject: [PATCH] fix(types): derive schema operations from one total ValueType map Co-locate registry enumeration and mapping contracts with the typing change. Finish the ES2024 config and public-plus-worker check gate. Preserve earlier layer scopes and tool pins. --- cfds/base/schema.ts | 6 ++-- cfds/base/types/core-type.ts | 11 +++--- cfds/base/types/date-type.ts | 3 +- cfds/base/types/index.ts | 22 ++++-------- cfds/base/types/map-type.ts | 3 +- cfds/base/types/primitive-type.ts | 4 +++ cfds/base/types/richtext3-type.ts | 3 +- cfds/base/types/set-type.ts | 7 ++-- cfds/base/types/value-type.ts | 47 ++++++++++++++++++++++++++ deno.json | 1 + tests/core-value-serialization.test.ts | 47 +++++++++++++++++++++++++- tsconfig.json | 4 +-- 12 files changed, 119 insertions(+), 39 deletions(-) create mode 100644 cfds/base/types/value-type.ts diff --git a/cfds/base/schema.ts b/cfds/base/schema.ts index 56c4ec63..a5edd814 100644 --- a/cfds/base/schema.ts +++ b/cfds/base/schema.ts @@ -54,7 +54,9 @@ import type { RichText } from '../richtext/tree.ts'; import type { ValueType } from './types/index.ts'; /** - * A mapping between a schema type and its native variable type. + * A mapping between a schema type and its native variable type. The final + * `never` makes an unmapped ValueType a compile error instead of silently + * widening the field to `CoreValue`. * @group Schema */ export type FieldValue = T extends 'string' ? string @@ -64,7 +66,7 @@ export type FieldValue = T extends 'string' ? string : T extends 'set' ? Set : T extends 'map' ? Dictionary : T extends 'richtext' ? RichText - : CoreValue; + : never; /** * A definition of a single field in a schema. diff --git a/cfds/base/types/core-type.ts b/cfds/base/types/core-type.ts index 37111d3e..f961abd8 100644 --- a/cfds/base/types/core-type.ts +++ b/cfds/base/types/core-type.ts @@ -1,8 +1,5 @@ -import type { - IValueTypeOperations, - ValueType, - ValueTypeOptions, -} from './index.ts'; +import type { IValueTypeOperations, ValueTypeOptions } from './index.ts'; +import { kValueTypeCoreTypes, type ValueType } from './value-type.ts'; import type { DecodedValue } from '../../../base/core-types/encoding/index.ts'; import type { Change, EncodedChange } from '../../change/index.ts'; import { FieldChange } from '../../change/field-change.ts'; @@ -21,8 +18,8 @@ export abstract class CoreTypeOperations< private _coreType: CoreType; private _valueType: ValueType; - constructor(encType: CoreType, valueType: ValueType) { - this._coreType = encType; + constructor(valueType: ValueType) { + this._coreType = kValueTypeCoreTypes[valueType]; this._valueType = valueType; } diff --git a/cfds/base/types/date-type.ts b/cfds/base/types/date-type.ts index b5b55301..d84518a8 100644 --- a/cfds/base/types/date-type.ts +++ b/cfds/base/types/date-type.ts @@ -1,5 +1,4 @@ import { PrimitiveTypeOperations } from './primitive-type.ts'; -import { CoreType } from '../../../base/core-types/index.ts'; import type { DecodedValue } from '../../../base/core-types/encoding/index.ts'; import { assert } from '../../../base/error.ts'; @@ -12,7 +11,7 @@ import { assert } from '../../../base/error.ts'; */ export class DateTypeOperations extends PrimitiveTypeOperations { constructor() { - super(CoreType.Date, 'date'); + super('date'); } /** diff --git a/cfds/base/types/index.ts b/cfds/base/types/index.ts index 9b40eb23..38abdf93 100644 --- a/cfds/base/types/index.ts +++ b/cfds/base/types/index.ts @@ -8,21 +8,11 @@ import { MapTypeOperations } from './map-type.ts'; import { PrimitiveTypeOperations } from './primitive-type.ts'; import { SetTypeOperations } from './set-type.ts'; import { RichText3TypeOperations } from './richtext3-type.ts'; -import { - CoreType, - type CoreValue, - type Encoder, -} from '../../../base/core-types/index.ts'; +import type { CoreValue, Encoder } from '../../../base/core-types/index.ts'; import { DateTypeOperations } from './date-type.ts'; -export type ValueType = - | 'string' - | 'number' - | 'boolean' - | 'date' - | 'set' - | 'map' - | 'richtext'; +export { kValueTypeCoreTypes, type ValueType } from './value-type.ts'; +import type { ValueType } from './value-type.ts'; /** * Options common to value-type operations. `local`/`byCharacter` describe @@ -101,9 +91,9 @@ export interface IValueTypeOperations { /** Every `ValueType` has exactly one op set. `satisfies` + `Object.freeze` * make this exhaustive and immutable at compile time. */ const kTypeOperations = Object.freeze({ - string: new PrimitiveTypeOperations(CoreType.String, 'string'), - number: new PrimitiveTypeOperations(CoreType.Number, 'number'), - boolean: new PrimitiveTypeOperations(CoreType.Boolean, 'boolean'), + string: new PrimitiveTypeOperations('string'), + number: new PrimitiveTypeOperations('number'), + boolean: new PrimitiveTypeOperations('boolean'), date: new DateTypeOperations(), set: new SetTypeOperations('set'), map: new MapTypeOperations('map'), diff --git a/cfds/base/types/map-type.ts b/cfds/base/types/map-type.ts index 40955bb1..333e6708 100644 --- a/cfds/base/types/map-type.ts +++ b/cfds/base/types/map-type.ts @@ -11,7 +11,6 @@ import { FieldChange, FieldOperation } from '../../change/field-change.ts'; import { type ConcreteCoreValue, type CoreKey, - CoreType, coreValueEquals, } from '../../../base/core-types/index.ts'; @@ -19,7 +18,7 @@ export class MapTypeOperations extends CoreTypeOperations< Dictionary > { constructor(valueType: ValueType) { - super(CoreType.Dictionary, valueType); + super(valueType); } patch( diff --git a/cfds/base/types/primitive-type.ts b/cfds/base/types/primitive-type.ts index 0228bf02..ef82edc5 100644 --- a/cfds/base/types/primitive-type.ts +++ b/cfds/base/types/primitive-type.ts @@ -1,4 +1,5 @@ import { valueTypeEquals, ValueTypeOptions } from './index.ts'; +import type { ValueType } from './value-type.ts'; import { CoreTypeOperations } from './core-type.ts'; import { Change, EncodedChange } from '../../change/index.ts'; import { FieldChange, FieldOperation } from '../../change/field-change.ts'; @@ -7,6 +8,9 @@ import { ConcreteCoreValue } from '../../../base/core-types/index.ts'; export class PrimitiveTypeOperations< TValue extends ConcreteCoreValue, > extends CoreTypeOperations { + constructor(valueType: ValueType) { + super(valueType); + } patch( curValue: TValue | undefined, changes: Change[], diff --git a/cfds/base/types/richtext3-type.ts b/cfds/base/types/richtext3-type.ts index 182c6b0f..fe5b0bb5 100644 --- a/cfds/base/types/richtext3-type.ts +++ b/cfds/base/types/richtext3-type.ts @@ -3,7 +3,6 @@ import type { Change, EncodedChange } from '../../change/index.ts'; import type { RichTextChange } from '../../change/richtext-change.ts'; import { type CoreOptions, - CoreType, type CoreValue, coreValueClone, coreValueEquals, @@ -38,7 +37,7 @@ export interface RichTextSerializeOptions extends SerializeValueTypeOptions { export class RichText3TypeOperations extends CoreTypeOperations { constructor() { - super(CoreType.Object, 'richtext'); + super('richtext'); } patch( curValue: RichText | undefined, diff --git a/cfds/base/types/set-type.ts b/cfds/base/types/set-type.ts index d92abe99..02abf0b1 100644 --- a/cfds/base/types/set-type.ts +++ b/cfds/base/types/set-type.ts @@ -2,17 +2,14 @@ import type { ValueType, ValueTypeOptions } from './index.ts'; import type { Change, EncodedChange } from '../../change/index.ts'; import { CoreTypeOperations } from './core-type.ts'; import { FieldChange, FieldOperation } from '../../change/field-change.ts'; -import { - type ConcreteCoreValue, - CoreType, -} from '../../../base/core-types/index.ts'; +import type { ConcreteCoreValue } from '../../../base/core-types/index.ts'; import * as SetUtils from '../../../base/set.ts'; export class SetTypeOperations extends CoreTypeOperations< Set > { constructor(valueType: ValueType) { - super(CoreType.Set, valueType); + super(valueType); } patch( diff --git a/cfds/base/types/value-type.ts b/cfds/base/types/value-type.ts new file mode 100644 index 00000000..fb5d8208 --- /dev/null +++ b/cfds/base/types/value-type.ts @@ -0,0 +1,47 @@ +import { assert } from '../../../base/error.ts'; +import { CoreType } from '../../../base/core-types/index.ts'; + +/** + * Schema-facing value types. Kept in a dependency-free leaf so the + * ValueType -> CoreType table can be shared without an import cycle: + * `index.ts` imports the operation classes, and those classes import the map. + */ +export type ValueType = + | 'string' + | 'number' + | 'boolean' + | 'date' + | 'set' + | 'map' + | 'richtext'; + +/** + * Single source of truth linking every schema ValueType to its CoreType. + * + * WHY: the CFD operation table used to take both values as constructor + * arguments, so a new ValueType could compile without a CoreType mapping and + * silently fall back to `CoreValue`. `satisfies Record` + * makes a missing entry a compile error; the operation constructors now take + * only a ValueType and derive the CoreType here. + */ +export const kValueTypeCoreTypes = Object.freeze({ + string: CoreType.String, + number: CoreType.Number, + boolean: CoreType.Boolean, + date: CoreType.Date, + set: CoreType.Set, + map: CoreType.Dictionary, + richtext: CoreType.Object, +}) satisfies Record; + +// Guard against two ValueTypes claiming the same CoreType: the reverse lookup +// below would silently keep one of them. +const gSeenCoreTypes = new Map(); +for (const [valueType, coreType] of Object.entries(kValueTypeCoreTypes)) { + assert( + !gSeenCoreTypes.has(coreType), + `CoreType ${coreType} is mapped from both ` + + `"${gSeenCoreTypes.get(coreType)}" and "${valueType}"`, + ); + gSeenCoreTypes.set(coreType, valueType as ValueType); +} diff --git a/deno.json b/deno.json index 6a1b5daa..ee57a41a 100644 --- a/deno.json +++ b/deno.json @@ -46,6 +46,7 @@ "./link": "./cli/link.ts" }, "tasks": { + "check": "deno check mod.ts server.ts server-build.ts && deno check --node-modules-dir=none base/json-log/json-log-worker.ts", "build": "deno run -A system-assets/build-sys-assets.ts", "test": "deno run -A tests/run.ts", "test:smoke": "deno run -A tests/run.ts --runtime=deno --suite=SecurityBoundaries", diff --git a/tests/core-value-serialization.test.ts b/tests/core-value-serialization.test.ts index d0fb0ccc..850f39eb 100644 --- a/tests/core-value-serialization.test.ts +++ b/tests/core-value-serialization.test.ts @@ -1,6 +1,10 @@ import { deserialize } from '../cfds/base/object.ts'; import { kChecksumSerOptions } from '../cfds/base/item.ts'; -import { getTypeOperations } from '../cfds/base/types/index.ts'; +import { + getTypeOperations, + kValueTypeCoreTypes, + type ValueType, +} from '../cfds/base/types/index.ts'; import { assertNoLegacyRefs } from '../cfds/richtext/legacy-ref.ts'; import { kTypeOps } from '../base/core-types/type-ops.ts'; import { CoreType } from '../base/core-types/base.ts'; @@ -95,6 +99,47 @@ function assertLegacyIngress(doc: RichText): void { } export default function setup(): void { + TEST( + 'CoreValueSerialization', + 'kTypeOperations is frozen and exhaustive', + () => { + // WHY: `kValueTypeCoreTypes` satisfies Record, so + // deriving the key list from it keeps this test honest when a new + // ValueType is added. A missing operation makes the lookup fail. + for (const t of Object.keys(kValueTypeCoreTypes) as ValueType[]) { + const ops = getTypeOperations(t); + assertExists(ops, `getTypeOperations missing for: ${t}`); + assertEquals(ops.valueType, t); + } + }, + ); + + TEST( + 'CoreValueSerialization', + 'ValueType to CoreType map is total and bijective', + () => { + const reps: Record = { + string: 's', + number: 1, + boolean: true, + date: new Date(0), + set: new Set([1]), + map: mkDict([['k', 1]]), + richtext: { root: { tagName: 'div', children: [] } } as never, + }; + const valueTypes = Object.keys(kValueTypeCoreTypes) as ValueType[]; + // A shared CoreType would make the reverse lookup ambiguous. + assertEquals( + new Set(Object.values(kValueTypeCoreTypes)).size, + valueTypes.length, + ); + for (const t of valueTypes) { + assertEquals(getCoreType(reps[t]), kValueTypeCoreTypes[t]); + assertTrue(getTypeOperations(t).validate(reps[t])); + } + }, + ); + TEST( 'CoreValueSerialization', 'rejects legacy refs in pointers without stripping', diff --git a/tsconfig.json b/tsconfig.json index 01b5d60b..745565e7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "ES2020", + "target": "ES2024", "module": "ESNext", "moduleResolution": "bundler", "allowImportingTsExtensions": true, @@ -8,7 +8,7 @@ "strict": false, "skipLibCheck": true, "downlevelIteration": true, - "lib": ["ES2020", "DOM", "DOM.Iterable", "ESNext.Disposable"], + "lib": ["ES2024", "DOM", "DOM.Iterable", "ESNext.Disposable"], "resolveJsonModule": true, "allowSyntheticDefaultImports": true, "esModuleInterop": true,