hedl-wasm
Parse, validate, and convert HEDL documents directly in browsers and Node.js with near-native performance.
Every web developer has faced the same frustration: JSON is everywhere but loses type information, YAML parsers are heavy, and XML processing adds complexity. What if you could parse structured data at near-native speed, validate it with full schema checking, and convert between formats instantly, all without leaving JavaScript?
That's what hedl-wasm delivers. The complete Rust HEDL implementation, compiled to WebAssembly, running in your browser or Node.js environment. Parse multi-megabyte documents at 50-100 MB/s. Validate data structures client-side before they ever hit your server. Convert seamlessly between HEDL, JSON, YAML, XML, and CSV with zero compromises on correctness.
Installation
Quick Start
Browser (ESM)
Node.js (CommonJS)
const hedl = require;
const doc = hedl.;
console.log;
Node.js (ESM)
import init from 'hedl-wasm';
await ;
const result = ;
if else
Core API
parse(input: string): HedlDocument
Parse HEDL content into a structured document.
import from 'hedl-wasm';
const doc = ;
console.log;
console.log;
console.log;
Returns: HedlDocument with these properties:
version: string- HEDL version (e.g., "2.0")schemaCount: number- Number of schema definitionsaliasCount: number- Number of aliasesnestCount: number- Number of nest relationshipsrootItemCount: number- Number of root items
Methods:
getSchemaNames(): string[]- Get all schema type namesgetSchema(typeName: string): string[] | null- Get schema columns for a typegetAliases(): object- Get all aliases as a JSON objectgetNests(): object- Get all nest relationshipscountEntities(): object- Count entities by typequery(typeName?: string, id?: string): Array<{ type: string, id: string, fields: object }>- Query entities (requires "query-api" feature)toJson(): JsonValue- Convert to JSON object (requires "json" feature)toJsonString(pretty?: boolean): string- Convert to JSON string (requires "json" feature)toHedl(): string- Convert to canonical HEDL string
Throws: JsError on parse failure with line number information in the message.
validate(input: string, runLint?: boolean): ValidationResult
Validate HEDL documents and return detailed diagnostics.
import from 'hedl-wasm';
const result = ;
if
result..;
Parameters:
input: string- HEDL document contentrunLint?: boolean- Enable lint checks (default: true, requires "full-validation" feature)
Returns: ValidationResult with:
valid: boolean- Whether document is validerrors: Array<{ line: number, message: string, type: string }>- Parse and validation errorswarnings: Array<{ line: number, message: string, rule: string }>- Lint warnings
format(input: string): string
Format and canonicalize HEDL documents.
import from 'hedl-wasm';
const formatted = ;
Returns normalized HEDL with consistent indentation, float representation, and spacing.
version(): string
Get the HEDL library version.
import from 'hedl-wasm';
console.log;
setMaxInputSize(size: number): void
Configure the maximum input size in bytes for all parsing operations.
import from 'hedl-wasm';
// Allow processing up to 1 GB documents
;
Default is 500 MB. The size check runs before parsing to prevent memory exhaustion.
getMaxInputSize(): number
Get the current maximum input size configuration.
import from 'hedl-wasm';
const currentLimit = ;
console.log;
Format Conversion
All conversion functions work both as standalone functions on strings and as methods on HedlDocument objects.
toJson(input: string, pretty?: boolean): string
Convert HEDL to JSON. Requires the "json" feature flag.
import from 'hedl-wasm';
const json = ; // true for pretty-print
fromJson(json: string): string
Convert JSON to HEDL. Requires the "json" feature flag.
import from 'hedl-wasm';
const hedl = ;
toYaml(input: string): string
Convert HEDL to YAML. Requires the "yaml" feature flag.
import from 'hedl-wasm';
const yaml = ;
fromYaml(yaml: string): string
Convert YAML to HEDL. Requires the "yaml" feature flag.
import from 'hedl-wasm';
const hedl = ;
toXml(input: string): string
Convert HEDL to XML. Requires the "xml" feature flag.
import from 'hedl-wasm';
const xml = ;
fromXml(xml: string): string
Convert XML to HEDL. Requires the "xml" feature flag.
import from 'hedl-wasm';
const hedl = ;
toCsv(input: string): string
Convert HEDL to CSV (first entity list). Requires the "csv" feature flag.
import from 'hedl-wasm';
const csv = ;
fromCsv(csv: string, typeName?: string): string
Convert CSV to HEDL. Requires the "csv" feature flag.
import from 'hedl-wasm';
const hedl = ;
Parameters:
csv: string- CSV content (must have header row)typeName?: string- Entity type name (default: "Row")
toToon(input: string): string
Convert HEDL to TOON format. Requires the "toon" feature flag.
import from 'hedl-wasm';
const toon = ;
fromToon(toon: string): string
Convert TOON to HEDL. Requires the "toon" feature flag.
import from 'hedl-wasm';
const hedl = ;
Statistics and Analysis
getStats(input: string): TokenStats
Get token usage statistics comparing HEDL to JSON. Requires the "statistics" feature flag.
import from 'hedl-wasm';
const stats = ;
console.log;
console.log;
console.log;
console.log;
console.log;
Returns: TokenStats with:
hedlBytes: number- Input HEDL size in byteshedlTokens: number- Estimated token count for HEDLhedlLines: number- Line count in HEDLjsonBytes: number- Equivalent JSON size in bytesjsonTokens: number- Estimated token count for JSONsavingsPercent: number- Token savings percentagetokensSaved: number- Absolute token count difference
compareTokens(hedl: string, json: string): object
Compare token counts between HEDL and JSON. Requires the "token-tools" feature flag.
import from 'hedl-wasm';
const comparison = ;
console.log;
console.log;
console.log;
Returns: Object with:
hedl: { bytes: number, tokens: number, lines: number }json: { bytes: number, tokens: number }savings: { percent: number, tokens: number }
Error Handling
All functions throw structured errors with line number information.
try catch
When using validate(), errors are returned in the ValidationResult object with:
line: number- Source line number (1-indexed)message: string- Human-readable descriptiontype: string- Error category
Error Types:
Syntax- Invalid HEDL syntaxSchema- Type/schema mismatchReference- Unresolved referenceShapeMismatch- Column count mismatchOrphanRow- Child without parentUtf8- Invalid UTF-8 encodingMaxSizeExceeded- Input too large
TypeScript Support
Complete type definitions are included for full IntelliSense support.
import {
parse,
validate,
format,
toJson,
fromJson,
getStats,
compareTokens,
HedlDocument,
ValidationResult,
TokenStats
} from 'hedl-wasm';
const doc: HedlDocument = parse(content);
const result: ValidationResult = validate(content, false);
if (!result.valid) {
result.errors.forEach(err => {
console.error(`Line ${err.line}: ${err.message}`);
});
}
const stats: TokenStats = getStats(content);
console.log(`Savings: ${stats.savingsPercent}%`);
Exported Types:
HedlDocument- Parsed HEDL document with methodsValidationResult- Validation diagnosticsTokenStats- Token usage statisticsJsonValue- JSON value union typeJsonPrimitive- JSON primitive typesJsonObject- JSON object typeJsonArray- JSON array type
Bundle Sizes
Optimized for web delivery with wasm-opt -Os, tree-shaking support, and dead code elimination.
| Format | Size |
|---|---|
| Uncompressed | ~600 KB |
| Gzipped | ~200 KB |
| Brotli | ~180 KB |
Bundle Variants:
hedl_wasm.js- ESM for browsershedl_wasm_node.js- CommonJS for Node.jshedl_wasm_bg.wasm- WebAssembly binaryhedl.d.ts- TypeScript definitions
Performance
Parsing runs at near-native speed, typically within 10% of the pure Rust implementation. Expect 50-100 MB/s throughput on modern browsers. Token estimation uses an O(1) memory algorithm with efficient byte-level iteration, running 3x faster than character-by-character approaches.
Memory scales linearly with document size. Initial WASM module load adds approximately 50-100ms overhead, a one-time cost per page load.
Building from Source
Prerequisites
Build
# Build for browsers
# Build for bundlers (webpack, etc.)
# Build for Node.js
Output is placed in pkg/:
hedl_wasm.js- JavaScript glue codehedl_wasm.d.ts- TypeScript definitionshedl_wasm_bg.wasm- WebAssembly binary
License
Apache-2.0