Skip to content

v0.20.0 - SafeHTML, TypeScript Strict Mode Fixes & Enhanced Documentation

Choose a tag to compare

@Zheruel Zheruel released this 09 Oct 17:57
· 18 commits to main since this release
a243e8f

Summary

This release introduces type-safe XSS prevention with the new SafeHTML branded type, fixes TypeScript strict mode violations, improves documentation with comprehensive examples, and streamlines benchmark reporting by replacing markdown files with JSON data for the interactive documentation site.

Changes

New Features

SafeHTML Branded Type - Compile-Time XSS Prevention

Added a new branded type SafeHTML that provides compile-time protection against XSS attacks:

  • toSafeHTML() - Builder function with opinionated secure defaults:
    • Strips all HTML by default for maximum security
    • Removes script tags and dangerous content
    • Removes non-printable characters
    • Optional configuration to allow specific safe tags
  • unsafeSafeHTML() - Bypass for trusted input (use with caution)
  • Zero runtime overhead - Type safety enforced at compile time
  • 11 comprehensive tests covering all sanitization scenarios
  • Integrates seamlessly with existing branded types (Email, URL, Slug)

Branded Type Pattern Documentation

Added comprehensive documentation for extending the library with custom branded types:

  • "Extending with Custom Branded Types" section in README
  • Examples showing how to create domain-specific types (PhoneNumber, PostalCode, CreditCard)
  • Demonstrates type-safe constructors and validation patterns
  • Shows composability with built-in branded types

Enhanced JSDoc Examples

Improved developer experience with real-world examples across 20+ functions:

  • All examples use proper ```ts code blocks for syntax highlighting
  • Practical use cases: form validation, search normalization, file naming, etc.
  • Functions enhanced: capitalize, codePoints, deburr, detectScript, escapeHtml, graphemes, hashString, pad, padEnd, padStart, pluralize, randomString, reverse, singularize, smartSplit, stripHtml, toASCII

Bug Fixes

TypeScript Strict Mode Compliance

Resolved all TypeScript violations related to noUncheckedIndexedAccess:

  • Added non-null assertions for guaranteed valid array access in levenshtein, pad, fuzzyMatch, smartSplit
  • Fixed randomString to handle empty charset edge case with early return
  • Zero runtime cost - all assertions validated by algorithm guarantees
  • All 1207 tests passing

Infrastructure Changes

Bundle Size Adjustments

Updated size limits to accommodate new SafeHTML feature while maintaining reasonable constraints:

  • ESM: 9.1 KB → 9.5 KB (+400 bytes headroom)
  • CJS: 9.5 KB → 10 KB (+500 bytes headroom)
  • Current actual size: 8.84 KB ESM / 9.36 KB CJS (well under limits)
  • SafeHTML adds ~500 bytes for compile-time XSS prevention

Benchmark Reporting Improvements

Streamlined benchmark output for better integration with documentation site:

  • Removed: benchmark-results.md and bundle-size-results.md markdown files
  • Kept: bundle-sizes.json and performance-benchmarks.json for programmatic access
  • Updated bundle-size.ts to generate JSON-only output
  • JSON files copied to docs-src/public/ for serving in interactive docs
  • Cleaner output with summary statistics in console

Documentation Updates

  • CHANGELOG.md: Complete v0.20.0 release notes with categorized changes
  • README.md:
    • Added branded type extension guide with examples
    • Updated benchmark instructions to reflect new workflow
    • Linked to interactive benchmark viewer on documentation site
    • Updated bundle size limits in documentation

Testing

  • All 1207 tests passing
  • Added 11 new tests for SafeHTML functionality
  • Bundle size validation passing
  • TypeScript strict mode compilation successful

Security Considerations

The SafeHTML branded type provides an additional layer of security for applications that need to handle user-generated content. By enforcing sanitization at the type level, it helps prevent XSS vulnerabilities by making it impossible to use unsanitized strings where safe HTML is expected.

Breaking Changes

None. This release is fully backward compatible.