Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ A complete rewrite of the library. The runtime `xdr.config(...)` schema-definiti
* **`lazy(() => schema)`** builder for defining recursive and forward-referencing schemas.
* **`BaseType` / `XdrType<T>`** are exported as the base class and interface for all schemas, along with a `DecodeOptions` `{ maxDepth }` option preserved from the prior recursion-depth guard.
* **`enumType` reserved-name and duplicate-value validation** — throws if a member name collides with a schema property (`name`, `kind`, `encode`, …) or if two members share a wire value.
* **Schema introspection API.** Every builder now returns a typed schema interface (`StructSchema`, `UnionSchema`, `ArraySchema`, `FixedArraySchema`, `OptionSchema`, `LazySchema`, `OpaqueSchema`, `VarOpaqueSchema`, `StringSchema`, `EnumSchema`) whose `kind` property is narrowed to a literal, plus a closed `AnySchema` union (with `PrimitiveSchema` covering the argument-less kinds). Schema-driven walkers — such as generic JSON converters — can cast once to `AnySchema` and `switch (schema.kind)` with exhaustiveness checking, instead of re-declaring internal shapes and casting. Supporting surface: `enumType` schemas expose `nameByValue` (wire value → member name); `opaque`/`varOpaque`/`string` expose their `length`/`maxLength`; `EnumMember`, `EnumName`, `Field`, `UnionArm`, and `UnionCase` helper types are exported.
* **`encode(value, { maxDepth })`** — the depth guard also applies to encoding (via the exported `EncodeOptions`), failing with `XdrError` on cyclic values fed to recursive schemas instead of overflowing the call stack.

### Changed
* **Build chain modernized:** Webpack + Babel → Rollup + esbuild; output is a clean dual ESM/CJS bundle with `.d.ts` emission via `rollup-plugin-dts`.
Expand Down
32 changes: 30 additions & 2 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,13 @@ Result.validate({ type: ResultType.error, code: 7 }); // true
Result.validate({ type: ResultType.error }); // false
```

The recursion-depth guard is now a decode option rather than a constructor
argument:
The recursion-depth guard is now an encode/decode option rather than a
constructor argument:

```ts
Result.decode(bytes, { maxDepth: 100 });
Result.validateXdr(bytes, { maxDepth: 100 });
Result.encode(value, { maxDepth: 100 }); // guards against cyclic values
```

## Errors
Expand Down Expand Up @@ -395,6 +396,33 @@ data, so build a new object instead of mutating a decoded value.
through composition, so prefer deriving types from schemas instead of
duplicating object shapes by hand.

### Schema introspection

Each builder returns a typed schema interface (`StructSchema`, `UnionSchema`,
`ArraySchema`, …) with `kind` narrowed to a literal. To write a generic
schema-driven walker (e.g. a JSON converter), cast the root once to the closed
`AnySchema` union and switch on `kind` — each branch narrows to the matching
interface, with compiler-checked exhaustiveness:

```ts
import type { AnySchema } from '@stellar/js-xdr';

function describe(schema: AnySchema): string {
switch (schema.kind) {
case 'struct':
return `struct { ${schema.entries.map(([name]) => name).join(', ')} }`;
case 'enum':
return `enum ${schema.name}`;
case 'array':
return `${describe(schema.element as AnySchema)}<>`;
// ...remaining kinds
}
}
```

`AnySchema` covers only the schemas this package creates; custom `BaseType`
subclasses must be handled before the cast.

For very large XDR definitions, or definitions with cyclic references, deriving
all value types through `Infer` can put significant pressure on the TypeScript
compiler and language server. In those cases, prefer explicit hand-written value
Expand Down
Loading