Skip to content

feat(stdlib): implement WHATWG Fetch Request/Response and URLPattern with regex runtime hardening - #26

Merged
phamtanminhtien merged 1 commit into
mainfrom
feat/whatwg-fetch-and-urlpattern
Sep 24, 2026
Merged

phamtanminhtien merged 1 commit into
mainfrom
feat/whatwg-fetch-and-urlpattern

Conversation

@phamtanminhtien

Copy link
Copy Markdown
Contributor

Overview

This PR completes WHATWG Fetch (Request, Response, fetch) compliance in node:http, implements WHATWG / WinterCG URLPattern in node:urlpattern and node:url, and hardens POSIX regex runtime handling for non-capturing groups and JS capture indices.

Key Changes

1. WHATWG Fetch (Request & Response)

  • Body Readers: Added full body decoders on both Request and Response:
    • .text(): Promise<string>
    • .json(): Promise<unknown>
    • .arrayBuffer(): Promise<ArrayBuffer>
    • .blob(): Promise<Blob>
    • .bytes(): Promise<Uint8Array>
    • .formData(): Promise<FormData> (handles multipart/form-data with boundary parser and application/x-www-form-urlencoded)
  • Body Serialization:
    • Automatically serializes FormData bodies to multipart/form-data with randomly generated boundaries.
    • Automatically normalizes BodyInit inputs (string, Blob, ArrayBuffer, Uint8Array, FormData, URLSearchParams).
  • Static Constructors:
    • Response.json(data, init)
    • Response.error()
    • Response.redirect(url, status)
  • Cloning & Immutability:
    • Request.clone() and Response.clone().
    • WHATWG fetch() integrated with Request & Response contracts.

2. WHATWG / WinterCG URLPattern

  • Fully implemented URLPattern in internal/typescriptgo/stdlib/urlpattern.ts per WHATWG URLPattern specification.
  • Supports matching across all 8 URL components: protocol, username, password, hostname, port, pathname, search, and hash.
  • Supports parameter patterns (:param), wildcards (*), optional tokens (:param?), and regex constraints (:id(\\d+)).
  • Supports base URL resolution, test(), and exec() returning structured URLPatternResult.
  • Exported in node:url, node:urlpattern, and registered as an automatic web global.

3. POSIX RegExp Runtime Hardening

  • Added support for non-capturing groups (?:...) in internal/runtime/native/regex/runtime.c.
  • Corrected regex group mapping so JS result arrays contain exactly 1 + js_group_count elements, skipping internal non-capturing groups.
  • Properly sets undefined sentinel for unmatched optional groups (e.g. a(b)?c on "ac").

4. Corpus Tests & Documentation

  • Updated internal/compiler/testdata/corpus/api/request.ts covering all body readers, methods, and headers.
  • Updated internal/compiler/testdata/corpus/api/response.ts covering status codes, body decoders, static constructors, and clones.
  • Added internal/compiler/testdata/corpus/api/urlpattern.ts verifying path matching, named parameters, wildcards, and URL component extraction.
  • Updated docs/typescript-parity-report.md maintaining 430/430 (100.0%) parity.

Verification

  • go test -count=1 ./... (All package tests passed, including corpus tests)
  • go test -count=1 ./internal/typescriptgo/... (Passed)
  • go run ./cmd/parity (430/430 100.0% passed)
  • go build ./cmd/scriptgo (Passed)

…with regex hardening

- Implement WHATWG Request and Response standard compliance in node:http:
  - Full body decoders: .text(), .json(), .arrayBuffer(), .blob(), .bytes(), .formData()
  - Static constructors: Response.json(), Response.error(), Response.redirect()
  - Automatic multipart/form-data generation for FormData bodies with boundary
  - Request/Response clone() support and BodyInit normalization
  - Integration with fetch() returning WHATWG Response instances
- Implement WHATWG / WinterCG URLPattern in node:urlpattern:
  - Pattern matching for 8 URL components (protocol, username, password, hostname, port, pathname, search, hash)
  - Named parameter captures (:id), wildcard captures (*), optional tokens (:param?), and custom regex constraints (:id(\\d+))
  - BaseURL resolution and full input dictionary / string matching support
  - Export URLPattern in node:url and web global scope
- Harden native POSIX regex runtime:
  - Add support for non-capturing groups (?:...)
  - Correct JS RegExp capture group index mapping in RegExp.exec
  - Return undefined sentinel for unmatched optional groups
- Add comprehensive corpus test suites for request, response, and urlpattern
- Update typescript-parity-report.md with verified 430/430 (100%) parity
@phamtanminhtien
phamtanminhtien merged commit 0f369ba into main Sep 24, 2026
7 checks passed
@phamtanminhtien
phamtanminhtien deleted the feat/whatwg-fetch-and-urlpattern branch September 24, 2026 03:57
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.

1 participant