| Crates.io | hedl-wasm |
| lib.rs | hedl-wasm |
| version | 1.2.0 |
| created_at | 2026-01-09 00:20:56.187277+00 |
| updated_at | 2026-01-21 03:03:32.653362+00 |
| description | WebAssembly bindings for HEDL with TypeScript support |
| homepage | https://dweve.com |
| repository | https://github.com/dweve/hedl |
| max_upload_size | |
| id | 2031331 |
| size | 294,468 |
WebAssembly bindings for HEDL—run HEDL parsing, validation, and conversion directly in browsers and Node.js with near-native performance.
JavaScript environments need structured data formats that don't sacrifice type safety or performance. JSON is ubiquitous but loses semantic information. YAML parsers are heavy. XML processing is complex. Running HEDL parsing in the browser or Node.js shouldn't require shipping a JavaScript reimplementation with different bugs and performance characteristics.
hedl-wasm provides complete WebAssembly bindings to the production-grade Rust HEDL implementation. Parse multi-megabyte HEDL documents at near-native speed in the browser. Validate data structures with full schema checking. Convert between HEDL and JSON/YAML/XML/CSV bidirectionally. Access the complete HEDL ecosystem from JavaScript with zero compromises on correctness or performance.
Production-ready WASM bindings with comprehensive features:
setMaxInputSize()npm install hedl-wasm
# or
yarn add hedl-wasm
# or
pnpm add hedl-wasm
<script type="module">
import init, { parse, validate, toJson } from './hedl_wasm.js';
await init(); // Initialize WASM module
const doc = parse(`
%VERSION: 1.0
%STRUCT: User: [id, name, age]
---
users: @User
| alice, Alice Smith, 30
| bob, Bob Jones, 25
`);
console.log('Parsed:', doc);
console.log('JSON:', doc.toJson()); // Requires "json" feature
</script>
const hedl = require('hedl-wasm');
const doc = hedl.parse(`
%VERSION: 1.0
---
config:
name: MyApp
version: 1.0.0
`);
console.log('Document:', doc);
import init, { parse, validate } from 'hedl-wasm';
await init();
const result = validate(hedlContent); // run_lint defaults to true
if (result.valid) {
console.log('Valid HEDL document');
} else {
result.errors.forEach(err => {
console.error(`Line ${err.line}: ${err.message}`);
});
}
Parse HEDL content into a structured document:
import { parse } from 'hedl-wasm';
const doc = parse(`
%VERSION: 1.0
%STRUCT: User: [id, name, email]
---
users: @User
| alice, Alice Smith, alice@example.com
| bob, Bob Jones, bob@example.com
`);
// Access document properties
console.log('Version:', doc.version);
console.log('Schemas:', doc.getSchemaNames());
console.log('Aliases:', doc.getAliases());
Returns: HedlDocument object with properties and methods:
version: string - HEDL version (e.g., "1.0")schemaCount: number - Number of schema definitionsaliasCount: number - Number of aliasesnestCount: number - Number of nest relationshipsrootItemCount: number - Number of root itemsMethods:
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 type (returns map of type names to counts)query(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(useDitto?: boolean): string - Convert to canonical HEDL stringThrows: JsError on parse failure with:
message: string - Error description with line numberValidate HEDL document and return diagnostics:
import { validate } from 'hedl-wasm';
const result = validate(hedlContent, true); // true to run lint checks
if (!result.valid) {
result.errors.forEach(err => {
console.error(`Line ${err.line} [${err.type}]: ${err.message}`);
});
}
result.warnings.forEach(warn => {
console.warn(`Line ${warn.line} [${warn.rule}]: ${warn.message}`);
});
Parameters:
input: string - HEDL document contentrunLint?: boolean - Enable lint checks (default: true, requires "full-validation" feature)Returns: ValidationResult object 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 (only if runLint is true)Format and canonicalize HEDL to standard style:
import { format } from 'hedl-wasm';
const formatted = format(messyHedl, true); // true for ditto optimization
console.log(formatted);
Parameters:
input: string - HEDL document contentuseDitto?: boolean - Use ditto operator (^) for repeated values (default: true)Returns: Normalized HEDL string with:
Get the HEDL library version:
import { version } from 'hedl-wasm';
console.log('HEDL version:', version());
Returns: Version string (e.g., "1.2.0")
Set the maximum input size in bytes for all parsing operations.
import { setMaxInputSize } from 'hedl-wasm';
// Allow processing up to 1 GB documents
setMaxInputSize(1024 * 1024 * 1024);
Parameters:
size: number - Maximum input size in bytesDefault: 500 MB (524,288,000 bytes)
This controls the maximum size of HEDL/JSON input strings that can be processed. Set to a higher value if you need to process larger documents. The size check is performed before parsing to prevent memory exhaustion.
Get the current maximum input size configuration.
import { getMaxInputSize } from 'hedl-wasm';
const currentLimit = getMaxInputSize();
console.log(`Current limit: ${currentLimit / (1024 * 1024)} MB`);
Returns: Current maximum input size in bytes
All conversion functions are available on HedlDocument objects after parsing, or as standalone functions on strings.
Convert HEDL string to JSON:
import { toJson } from 'hedl-wasm';
const json = toJson(hedlContent, true); // true for pretty-print
console.log(json);
Requires the "json" feature flag.
Parameters:
input: string - HEDL document contentpretty?: boolean - Pretty-print JSON (default: true)Convert JSON string to HEDL:
import { fromJson } from 'hedl-wasm';
const hedl = fromJson(jsonString, true);
Requires the "json" feature flag.
Convert HEDL to YAML:
import { toYaml } from 'hedl-wasm';
const yaml = toYaml(hedlContent);
Requires the "yaml" feature flag.
Convert YAML to HEDL:
import { fromYaml } from 'hedl-wasm';
const hedl = fromYaml(yamlContent, true);
Requires the "yaml" feature flag.
Convert HEDL to XML:
import { toXml } from 'hedl-wasm';
const xml = toXml(hedlContent);
Requires the "xml" feature flag.
Convert XML to HEDL:
import { fromXml } from 'hedl-wasm';
const hedl = fromXml(xmlContent, true);
Requires the "xml" feature flag.
Convert HEDL to CSV (first entity list):
import { toCsv } from 'hedl-wasm';
const csv = toCsv(hedlContent);
Requires the "csv" feature flag.
Convert CSV to HEDL:
import { fromCsv } from 'hedl-wasm';
const hedl = fromCsv(csvContent, 'User', true);
Parameters:
csv: string - CSV content (must have header row)typeName?: string - Entity type name (default: "Row")useDitto?: boolean - Use ditto optimization (default: true)Requires the "csv" feature flag.
Convert HEDL to TOON format:
import { toToon } from 'hedl-wasm';
const toon = toToon(hedlContent);
Requires the "toon" feature flag.
Convert TOON to HEDL:
import { fromToon } from 'hedl-wasm';
const hedl = fromToon(toonContent, true);
Requires the "toon" feature flag.
Get token usage statistics for HEDL vs JSON:
import { getStats } from 'hedl-wasm';
const stats = getStats(hedlContent);
console.log('HEDL bytes:', stats.hedlBytes);
console.log('HEDL tokens:', stats.hedlTokens);
console.log('JSON bytes:', stats.jsonBytes);
console.log('JSON tokens:', stats.jsonTokens);
console.log('Savings:', stats.savingsPercent + '%');
Requires the "statistics" feature flag.
Returns: TokenStats object 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 differenceCompare token counts between HEDL and JSON:
import { compareTokens } from 'hedl-wasm';
const comparison = compareTokens(hedlString, jsonString);
console.log('HEDL tokens:', comparison.hedl.tokens);
console.log('JSON tokens:', comparison.json.tokens);
console.log('Savings:', comparison.savings.percent + '%');
Requires the "token-tools" feature flag.
Returns: Object with:
hedl: { bytes: number, tokens: number, lines: number }json: { bytes: number, tokens: number }savings: { percent: number, tokens: number }Default limits prevent memory exhaustion:
import { setMaxInputSize, getMaxInputSize } from 'hedl-wasm';
// Default: 500 MB input limit
const doc = parse(hedlContent);
// Get current limit
const limit = getMaxInputSize();
console.log(`Current limit: ${limit / (1024 * 1024)} MB`);
// Set custom limit (e.g., 1 GB)
setMaxInputSize(1024 * 1024 * 1024);
Protection Against:
The size check is performed before parsing to prevent resource exhaustion.
All functions throw structured errors with line numbers:
try {
const doc = parse(invalidHedl);
} catch (error) {
console.error(`Parse error: ${error.message}`);
// Note: error.message includes line number information
}
Error Object (for validation errors):
When using the validate() function, errors are returned in the ValidationResult object:
line: number - Source line number (1-indexed)message: string - Human-readable descriptiontype: string - Error category (Syntax, Schema, Reference, etc.)Common Error Types:
Syntax - Invalid HEDL syntaxSchema - Type/schema mismatchReference - Unresolved referenceShapeMismatch - Column count mismatchOrphanRow - Child without parentUtf8 - Invalid UTF-8 encodingMaxSizeExceeded - Input too largeComplete type definitions included:
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 statisticsTypeScript Type Definitions (available in .d.ts):
JsonValue - JSON value union type (string | number | boolean | null | JsonObject | JsonArray)JsonPrimitive - JSON primitive types (string | number | boolean | null)JsonObject - JSON object type ({ [key: string]: JsonValue })JsonArray - JSON array type (JsonValue[])Type Files:
Optimized for web delivery:
Optimization Techniques:
wasm-opt -Os - Size optimization passBundle Variants:
hedl_wasm.js - ESM for browsers (bundler-friendly)hedl_wasm_node.js - CommonJS for Node.jshedl_wasm_bg.wasm - WebAssembly binaryhedl.d.ts - TypeScript definitionsWeb Applications: Parse and validate HEDL configuration files uploaded by users in the browser without server round-trip. Validate data structures client-side before submission.
Data Transformation Tools: Build web-based converters between HEDL and JSON/YAML/XML/CSV with instant client-side processing. No server infrastructure required.
LLM Context Planning: Estimate token counts for HEDL documents before sending to LLM APIs. Stay within context window limits (8K, 32K, 100K) with accurate projections.
Node.js Services: Parse HEDL API responses, validate data structures, convert between formats in backend services with near-native performance.
Browser Extensions: Process HEDL data in browser extensions (Chrome, Firefox) with full HEDL ecosystem access without bundling JavaScript reimplementation.
Electron Applications: Embed HEDL processing in Electron desktop apps with native performance through WebAssembly.
Database Integration: No direct Neo4j or Parquet integration in WASM. For graph database export or columnar storage, use the Rust crates (hedl-neo4j, hedl-parquet) in server environments.
File System Access: No direct file I/O—WASM runs in sandbox. Use JavaScript File API or Node.js fs module to read files, then pass content to WASM functions.
Network Operations: No HTTP fetching or network I/O. Use JavaScript fetch API or Node.js http module, then process responses with WASM functions.
Async/Await Interface: Functions are synchronous (blocking). For non-blocking processing, wrap calls in async functions or Web Workers.
Parsing: Near-native speed (within 10% of Rust implementation). Typically 50-100 MB/s on modern browsers.
Validation: O(n) time where n = total nodes. Syntax validation through parsing.
Conversion: JSON conversion preserves HEDL semantics. Bidirectional conversion is lossless.
Token Estimation: O(1) memory algorithm, efficient byte-level iteration optimized for ASCII. 3x faster than character-by-character approaches.
Memory: Scales linearly with document size. Large documents benefit from 500 MB default input limit protection.
Bundle Loading: Initial WASM module load adds ~50-100ms overhead (one-time cost per page load).
Runtime dependencies (always included):
wasm-bindgen 0.2 - JavaScript/WebAssembly interopserde-wasm-bindgen 0.6 - Serde integration for WASMhedl-core - Core HEDL parser and typeshedl-c14n - Document canonicalizationhedl-lint - Linting engineOptional format converters (controlled by feature flags):
hedl-json - JSON bidirectional conversion (default feature)hedl-yaml - YAML conversion (yaml feature)hedl-xml - XML conversion (xml feature)hedl-csv - CSV conversion (csv feature)hedl-toon - TOON format conversion (toon feature)Build dependencies:
wasm-pack - Build toolchainwasm-opt (from binaryen) - Size optimization (disabled in Cargo.toml, uses custom pipeline)Apache-2.0