Skip to content

refactor(middleware): convert redirects middleware and tests to TypeScript - #616

Merged
bkendall merged 4 commits into
prioritize_js_to_tsfrom
convert_middleware_redirects
Nov 7, 2026
Merged

bkendall merged 4 commits into
prioritize_js_to_tsfrom
convert_middleware_redirects

Conversation

@bkendall

@bkendall bkendall commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Converts src/middleware/redirects.js and test/unit/middleware/redirects.spec.js to TypeScript (src/middleware/redirects.ts and test/unit/middleware/redirects.spec.ts).

  • Reuses Redirect and Configuration types and patterns.configMatcher / patterns.createRaw from src/utils/patterns.ts.
  • Replaces internal Redirect constructor function with a typed class/interface while preserving exact path-to-regexp capture, query string preservation, and CommonJS factory export compatibility for src/superstatic.js.

@bkendall
bkendall requested review from Berlioz and joehan November 2, 2026 23:33
@bkendall
bkendall force-pushed the convert_middleware_redirects branch from ceabd30 to 5106350 Compare November 2, 2026 23:35

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request migrates the redirects middleware and its unit tests from JavaScript to TypeScript, refactoring the redirect logic into a structured Redirect class and updating tests to use modern async/await syntax. The review feedback suggests several key improvements: caching compiled Redirect instances with a WeakMap to boost performance, wrapping decodeURI in a try-catch block to prevent crashes on malformed URLs, passing configuration errors to next() instead of throwing them directly, and using indexOf and slice to parse query strings more robustly.

Comment thread src/middleware/redirects.ts Resolved
Comment thread src/middleware/redirects.ts Resolved
Comment thread src/middleware/redirects.ts Resolved
Comment thread src/middleware/redirects.ts Resolved
Comment thread src/middleware/redirects.ts Resolved
@bkendall
bkendall force-pushed the convert_middleware_redirects branch 2 times, most recently from 6d434df to dd43c70 Compare November 2, 2026 23:46

@joehan joehan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review: PR #616 (refactor(middleware): convert redirects middleware and tests to TypeScript)

Strengths & LGTM Aspects

  • Clean extraction of redirect config caching and pattern matching.
  • Strong test suite covering status codes and redirect targets.

Key Findings to Address

  1. return next(new Error(...)) fails open inside superstatic: In src/activator.js, _run() takes zero arguments. Passing an error to next(err) pops the next middleware instead of terminating the request, silently serving static files 200 OK on malformed redirect configs.
  2. decodeURI(req.url) before ? lets ?% bypass all redirects: Decoding the full req.url before splitting the query string catches URIError on requests like GET /source?discount=50%, calls next(), and bypasses all redirect rules to serve source.html with 200 OK.
  3. encodeURI(addQuery(dest, qs)) double-encodes query parameters: Reserved query escapes like %26 or %3A in the query string get double-encoded to %2526.
  4. 0-argument factory function: export = function () accepts 0 arguments, failing TS2554 when called as redirects(spec) by activator.js.

Comment thread src/middleware/redirects.ts Resolved
Comment thread src/middleware/redirects.ts Resolved
Comment thread src/middleware/redirects.ts Resolved
Comment thread src/middleware/redirects.ts Resolved
Converts src/utils/patterns.js to TypeScript with typed pattern matching utilities and adds unit tests in test/unit/utils/patterns.spec.ts.
@bkendall
bkendall force-pushed the convert_utils_patterns branch from b93843e to 9b1084a Compare November 4, 2026 21:51
…cript

Converts src/middleware/redirects.js and test/unit/middleware/redirects.spec.js to TypeScript (src/middleware/redirects.ts and test/unit/middleware/redirects.spec.ts).

- Reuses Redirect and Configuration types from src/config.ts and patterns.configMatcher / patterns.createRaw from src/utils/patterns.ts.

- Replaces internal Redirect constructor function with a typed class/interface while preserving exact path-to-regexp capture, query string preservation, and CommonJS factory export compatibility.
@bkendall
bkendall force-pushed the convert_middleware_redirects branch from dd43c70 to 0f187e2 Compare November 4, 2026 21:55
Base automatically changed from convert_utils_patterns to prioritize_js_to_ts November 7, 2026 00:15
@bkendall
bkendall merged commit acedfc7 into prioritize_js_to_ts Nov 7, 2026
16 checks passed
@bkendall
bkendall deleted the convert_middleware_redirects branch November 7, 2026 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants